git ssb

0+

wanderer🌟 / js-primea-hypervisor



Tree: dde59deea46cd600f319fa2ec8e88988386c3466

Files: dde59deea46cd600f319fa2ec8e88988386c3466 / index.js

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

Built with git-ssb-web