git ssb

0+

wanderer🌟 / js-primea-hypervisor



Tree: bb6ef21471bd09c766ab63064037d371607de931

Files: bb6ef21471bd09c766ab63064037d371607de931 / index.js

3604 bytesRaw
1const Actor = require('./actor.js')
2const Scheduler = require('./scheduler.js')
3const {ID} = require('./systemObjects.js')
4const crypto = require('crypto')
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, nonce = 0) {
13 this.tree = tree
14 this.scheduler = new Scheduler(this)
15 this._containerTypes = {}
16 this.nonce = nonce
17 }
18
19 /**
20 * sends a message
21 * @param {Object} cap - the capabilitly used to send the message
22 * @param {Object} message - the [message](https://github.com/primea/js-primea-message) to send
23 * @returns {Promise} a promise that resolves once the receiving container is loaded
24 */
25 send (messages) {
26 if (!Array.isArray(messages)) {
27 messages = [messages]
28 }
29 this.scheduler.queue(messages)
30 }
31
32 async loadActor (id) {
33 const state = await this.tree.get(id.id, true)
34 const [code, storage] = await Promise.all([
35 this.tree.graph.get(state.node, '1'),
36 this.tree.graph.get(state.node, '2')
37 ])
38 const [type, nonce] = state.value
39 const Container = this._containerTypes[type]
40
41 // create a new actor instance
42 const actor = new Actor({
43 hypervisor: this,
44 state,
45 Container,
46 id,
47 nonce,
48 type,
49 code,
50 storage: storage || [],
51 cachedb: this.tree.dag._dag,
52 tree: this.tree
53 })
54
55 await actor.startup()
56 return actor
57 }
58
59 /**
60 * creates an instance of an Actor
61 * @param {Integer} type - the type id for the container
62 * @param {Object} message - an intial [message](https://github.com/primea/js-primea-message) to send newly created actor
63 * @param {Object} id - the id for the actor
64 */
65 async createActor (type, code, id = {nonce: this.nonce++, parent: null}) {
66 const Container = this._containerTypes[type]
67 const encoded = encodedID(id)
68 let idHash = this._getHashFromObj(encoded)
69 idHash = new ID(idHash)
70 const module = await Container.onCreation(code, idHash, this.tree)
71 const metaData = [type, 0]
72
73 // save the container in the state
74 this.tree.set(idHash.id, metaData)
75 if (code) {
76 const node = await this.tree.get(idHash.id)
77 await this.tree.graph.set(node.node, '1', code)
78 }
79 return {
80 id: idHash,
81 module
82 }
83 }
84
85 // get a hash from a POJO
86 _getHashFromObj (obj) {
87 const hash = crypto.createHash('sha256')
88 hash.update(obj)
89 return hash.digest().slice(0, 20)
90 }
91
92 /**
93 * creates a state root starting from a given container and a given number of
94 * ticks
95 * @param {Number} ticks the number of ticks at which to create the state root
96 * @returns {Promise}
97 */
98 createStateRoot () {
99 return new Promise((resolve, reject) => {
100 this.scheduler.on('idle', () => {
101 this.tree.flush().then(resolve)
102 })
103 })
104 }
105
106 /**
107 * regirsters a container with the hypervisor
108 * @param {Class} Constructor - a Class for instantiating the container
109 * @param {*} args - any args that the contructor takes
110 * @param {Integer} typeId - the container's type identification ID
111 */
112 registerContainer (Constructor) {
113 this._containerTypes[Constructor.typeId] = Constructor
114 }
115}
116
117function encodedID (id) {
118 const nonce = Buffer.from([id.nonce])
119 if (id.parent) {
120 return Buffer.concat([nonce, id.parent.id])
121 } else {
122 return nonce
123 }
124}
125

Built with git-ssb-web