git ssb

0+

wanderer🌟 / js-primea-hypervisor



Tree: 678b7ff5368bd4b1ad116d6108754e315d173ee9

Files: 678b7ff5368bd4b1ad116d6108754e315d173ee9 / index.js

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

Built with git-ssb-web