git ssb

0+

wanderer🌟 / js-primea-hypervisor



Tree: 3c71975dada7fb231be29756db22f3b9f71eab24

Files: 3c71975dada7fb231be29756db22f3b9f71eab24 / index.js

3648 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 tree: this.tree
52 })
53
54 await actor.startup()
55 return actor
56 }
57
58 /**
59 * creates an instance of an Actor
60 * @param {Integer} type - the type id for the container
61 * @param {Object} message - an intial [message](https://github.com/primea/js-primea-message) to send newly created actor
62 * @param {Object} id - the id for the actor
63 */
64 createActor (type, code, id = {nonce: this.nonce++, parent: null}) {
65 const Container = this._containerTypes[type]
66 const encoded = encodedID(id)
67 let idHash = this._hash(encoded)
68 idHash = new ID(idHash)
69 const module = Container.onCreation(code, idHash, this.tree)
70 const metaData = [type, 0]
71
72 // save the container in the state
73 this.tree.set(idHash.id, metaData).then(node => {
74 // save the code
75 node[1] = {
76 '/': code
77 }
78 // save the storage
79 node[2] = {
80 '/': []
81 }
82 })
83
84 return {
85 id: idHash,
86 module
87 }
88 }
89
90 _hash (buf) {
91 const hash = crypto.createHash('sha256')
92 hash.update(buf)
93 return hash.digest().slice(0, 20)
94 }
95
96 /**
97 * creates a state root starting from a given container and a given number of
98 * ticks
99 * @param {Number} ticks the number of ticks at which to create the state root
100 * @returns {Promise}
101 */
102 createStateRoot () {
103 return new Promise((resolve, reject) => {
104 if (!this.scheduler._running) {
105 this.tree.flush().then(resolve)
106 } else {
107 this.scheduler.on('idle', () => {
108 this.tree.flush().then(resolve)
109 })
110 }
111 })
112 }
113
114 /**
115 * regirsters a container with the hypervisor
116 * @param {Class} Constructor - a Class for instantiating the container
117 * @param {*} args - any args that the contructor takes
118 * @param {Integer} typeId - the container's type identification ID
119 */
120 registerContainer (Constructor) {
121 this._containerTypes[Constructor.typeId] = Constructor
122 }
123}
124
125function encodedID (id) {
126 const nonce = Buffer.from([id.nonce])
127 if (id.parent) {
128 return Buffer.concat([nonce, id.parent.id])
129 } else {
130 return nonce
131 }
132}
133

Built with git-ssb-web