git ssb

0+

wanderer🌟 / js-primea-hypervisor



Tree: 3f06f72bb36d2b3efb72366b6b5db8a966a3273a

Files: 3f06f72bb36d2b3efb72366b6b5db8a966a3273a / index.js

3840 bytesRaw
1const crypto = require('crypto')
2const Actor = require('./actor.js')
3const Scheduler = require('./scheduler.js')
4const {ID, decoder} = require('primea-objects')
5const cbor = require('borc')
6
7module.exports = class Hypervisor {
8 /**
9 * The Hypervisor manages the container instances by instantiating them and
10 * destorying them when possible. It also facilitates localating Containers
11 * @param {Tree} tree - a [radix tree](https://github.com/dfinity/js-dfinity-radix-tree) to store the state
12 */
13 constructor (tree, containers = [], drivers = [], nonce = 0) {
14 tree.dag.decoder = decoder
15 this.tree = tree
16 this.scheduler = new Scheduler(this)
17 this._containerTypes = {}
18 this.nonce = nonce
19 containers.forEach(container => this.registerContainer(container))
20 drivers.forEach(driver => this.registerDriver(driver))
21 }
22
23 /**
24 * sends a message
25 * @param {Object} message - the [message](https://github.com/primea/js-primea-message) to send
26 * @returns {Promise} a promise that resolves once the receiving container is loaded
27 */
28 send (messages) {
29 if (!Array.isArray(messages)) {
30 messages = [messages]
31 }
32 this.scheduler.queue(messages)
33 }
34
35 async loadActor (id) {
36 const state = await this.tree.get(id.id, true)
37 const [code, storage] = await Promise.all([
38 this.tree.graph.get(state.node, '1'),
39 this.tree.graph.get(state.node, '2')
40 ])
41 const [type, nonce] = state.value
42 const Container = this._containerTypes[type]
43
44 // create a new actor instance
45 const actor = new Actor({
46 hypervisor: this,
47 state,
48 Container,
49 id,
50 nonce,
51 type,
52 code,
53 storage,
54 tree: this.tree
55 })
56
57 await actor.startup()
58 return actor
59 }
60
61 /**
62 * creates an instance of an Actor
63 * @param {Integer} type - the type id for the container
64 * @param {Object} message - an intial [message](https://github.com/primea/js-primea-message) to send newly created actor
65 * @param {Object} id - the id for the actor
66 */
67 createActor (type, code, id = {nonce: this.nonce++, parent: null}) {
68 const Container = this._containerTypes[type]
69 const encoded = encodedID(id)
70 id = this._hash(encoded)
71 id = new ID(id)
72 const module = Container.onCreation(code, id, this.tree)
73 const metaData = [type, 0]
74
75 // save the container in the state
76 this.tree.set(id.id, metaData).then(node => {
77 // save the code
78 node[1] = {
79 '/': code
80 }
81 // save the storage
82 node[2] = {
83 '/': []
84 }
85 })
86
87 return {
88 id,
89 module
90 }
91 }
92
93 _hash (buf) {
94 const hash = crypto.createHash('sha256')
95 hash.update(buf)
96 return hash.digest().slice(0, 20)
97 }
98
99 /**
100 * creates a state root starting from a given container and a given number of
101 * ticks
102 * @param {Number} ticks the number of ticks at which to create the state root
103 * @returns {Promise}
104 */
105 async createStateRoot () {
106 if (this.scheduler._running) {
107 await new Promise((resolve, reject) => {
108 this.scheduler.once('idle', resolve)
109 })
110 }
111 return this.tree.flush().then(sr => {
112 console.log(sr.toString('hex'))
113 return sr
114 })
115 }
116
117 /**
118 * regirsters a container with the hypervisor
119 * @param {Class} Constructor - a Class for instantiating the container
120 * @param {*} args - any args that the contructor takes
121 * @param {Integer} typeId - the container's type identification ID
122 */
123 registerContainer (Constructor) {
124 this._containerTypes[Constructor.typeId] = Constructor
125 }
126
127 registerDriver (driver) {
128 this.scheduler.drivers.set(driver.id.toString(), driver)
129 }
130}
131
132function encodedID (id) {
133 if (id.parent) {
134 return cbor.encode([id.nonce, id.parent.id])
135 } else {
136 return cbor.encode([id.nonce, null])
137 }
138}
139

Built with git-ssb-web