git ssb

0+

wanderer🌟 / js-primea-hypervisor



Tree: d0a5ea25b9d19aad6f2be59441be57da023c9c70

Files: d0a5ea25b9d19aad6f2be59441be57da023c9c70 / index.js

4653 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 {Object} opts
13 * @param {Object} opts.tree - a [radix tree](https://github.com/dfinity/js-dfinity-radix-tree) to store the state
14 * @param {Array} opts.container - an array of containers to regester
15 * @param {Array} opts.drivers - an array of drivers to install
16 * @param {boolean} [opts.meter=true] - whether to meter gas or not
17 */
18 constructor (opts) {
19 opts.tree.dag.decoder = decoder
20 this.tree = opts.tree
21 this.scheduler = new Scheduler(this)
22 this._containerTypes = {}
23 this.nonce = opts.nonce || 0
24 this.meter = opts.meter !== undefined ? opts.meter : true;
25 (opts.containers || []).forEach(container => this.registerContainer(container));
26 (opts.drivers || []).forEach(driver => this.registerDriver(driver))
27 }
28
29 /**
30 * sends a message(s). If an array of message is given the all the messages will be sent at once
31 * @param {Object} message - the [message](https://github.com/primea/js-primea-message) to send
32 * @returns {Promise} a promise that resolves once the receiving container is loaded
33 */
34 send (messages) {
35 if (!Array.isArray(messages)) {
36 messages = [messages]
37 }
38 this.scheduler.queue(messages)
39 }
40
41 /**
42 * loads an actor from the tree given its id
43 * @param {ID} id
44 * @returns {Promise<Actor>}
45 */
46 async loadActor (id) {
47 const state = await this.tree.get(id.id, true)
48 const [code, storage] = await Promise.all([
49 this.tree.graph.get(state.node, '1'),
50 this.tree.graph.get(state.node, '2')
51 ])
52 const [type, nonce] = state.value
53 const Container = this._containerTypes[type]
54
55 // create a new actor instance
56 const actor = new Actor({
57 hypervisor: this,
58 state,
59 Container,
60 id,
61 nonce,
62 type,
63 code,
64 storage,
65 tree: this.tree
66 })
67
68 await actor.startup()
69 return actor
70 }
71
72 /**
73 * creates an instance of an Actor
74 * @param {Integer} type - the type id for the container
75 * @param {Object} message - an intial [message](https://github.com/primea/js-primea-message) to send newly created actor
76 * @param {Object} id - the id for the actor
77 */
78 createActor (type, code, id = {nonce: this.nonce++, parent: null}) {
79 const Container = this._containerTypes[type]
80 const encoded = encodedID(id)
81 id = this._hash(encoded)
82 id = new ID(id)
83 const module = Container.onCreation(code, id)
84 const metaData = [type, 0]
85
86 // save the container in the state
87 this.tree.set(id.id, metaData).then(node => {
88 // save the code
89 node[1] = {
90 '/': code
91 }
92 // save the storage
93 node[2] = {
94 '/': []
95 }
96 })
97
98 return {
99 id,
100 module
101 }
102 }
103
104 _hash (buf) {
105 const hash = crypto.createHash('sha256')
106 hash.update(buf)
107 return hash.digest().slice(0, 20)
108 }
109
110 /**
111 * creates a state root starting from a given container and a given number of
112 * ticks
113 * @param {Number} ticks the number of ticks at which to create the state root
114 * @returns {Promise}
115 */
116 async createStateRoot () {
117 if (this.scheduler._running) {
118 await new Promise((resolve, reject) => {
119 this.scheduler.once('idle', resolve)
120 })
121 }
122 await this.tree.set(Buffer.from([0]), this.nonce)
123 return this.tree.flush()
124 }
125
126 /**
127 * set the state root. The promise must resolve before creating or sending any more messages to the hypervisor
128 * @param {Buffer} stateRoot
129 * @return {Promise}
130 */
131 async setStateRoot (stateRoot) {
132 this.tree.root = stateRoot
133 const node = await this.tree.get(Buffer.from([0]))
134 this.nonce = node.value
135 }
136
137 /**
138 * regesters a container with the hypervisor
139 * @param {Function} Constructor - the container's constuctor
140 */
141 registerContainer (Constructor) {
142 this._containerTypes[Constructor.typeId] = Constructor
143 }
144
145 /**
146 * register a driver with the hypervisor
147 * @param {driver} driver
148 */
149 registerDriver (driver) {
150 driver.startup(this)
151 this.scheduler.drivers.set(driver.id.toString(), driver)
152 }
153}
154
155function encodedID (id) {
156 if (id.parent) {
157 return cbor.encode([id.nonce, id.parent.id])
158 } else {
159 return cbor.encode([id.nonce, null])
160 }
161}
162

Built with git-ssb-web