git ssb

0+

wanderer🌟 / js-primea-hypervisor



Tree: 426c0bb6c8135b071cb6598828943842a782bb7f

Files: 426c0bb6c8135b071cb6598828943842a782bb7f / index.js

3569 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._hash(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 const node = await this.tree.set(idHash.id, metaData)
75 // save the code
76 node[1] = {
77 '/': code
78 }
79 // save the storage
80 node[2] = {
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 this.scheduler.on('idle', () => {
105 this.tree.flush().then(resolve)
106 })
107 })
108 }
109
110 /**
111 * regirsters a container with the hypervisor
112 * @param {Class} Constructor - a Class for instantiating the container
113 * @param {*} args - any args that the contructor takes
114 * @param {Integer} typeId - the container's type identification ID
115 */
116 registerContainer (Constructor) {
117 this._containerTypes[Constructor.typeId] = Constructor
118 }
119}
120
121function encodedID (id) {
122 const nonce = Buffer.from([id.nonce])
123 if (id.parent) {
124 return Buffer.concat([nonce, id.parent.id])
125 } else {
126 return nonce
127 }
128}
129

Built with git-ssb-web