git ssb

0+

wanderer🌟 / js-primea-hypervisor



Tree: aff7fe3dbdadaa720feb5561633c1d5eb583f80d

Files: aff7fe3dbdadaa720feb5561633c1d5eb583f80d / index.js

4722 bytesRaw
1const Buffer = require('safe-buffer').Buffer
2const Actor = require('./actor.js')
3const Scheduler = require('./scheduler.js')
4const {decoder, generateId, ModuleRef, ActorRef} = require('primea-objects')
5
6module.exports = class Hypervisor {
7 /**
8 * The Hypervisor manages module instances by instantiating them and
9 * destroying them when possible. It also facilitates locating Containers
10 * @param {Object} opts
11 * @param {Object} opts.tree - a [radix tree](https://github.com/dfinity/js-dfinity-radix-tree) to store the state
12 * @param {Array} opts.modules - an array of modules to register
13 * @param {Array} opts.drivers - an array of drivers to install
14 * @param {boolean} [opts.meter=true] - whether to meter gas or not
15 */
16 constructor (opts) {
17 opts.tree.dag.decoder = decoder
18 this.tree = opts.tree
19 this.scheduler = new Scheduler(this)
20 this._modules = {}
21 this.nonce = opts.nonce || 0
22 this.meter = opts.meter !== undefined ? opts.meter : true;
23 (opts.modules || []).forEach(mod => this.registerModule(mod));
24 (opts.drivers || []).forEach(driver => this.registerDriver(driver))
25 }
26
27 /**
28 * sends a message(s). If an array of message is given the all the messages will be sent at once
29 * @param {Object} message - the [message](https://github.com/primea/js-primea-message) to send
30 * @returns {Promise} a promise that resolves once the receiving module is loaded
31 */
32 send (messages) {
33 if (!Array.isArray(messages)) {
34 messages = [messages]
35 }
36 this.scheduler.queue(messages)
37 }
38
39 /**
40 * loads an actor from the tree given its id
41 * @param {ID} id
42 * @returns {Promise<Actor>}
43 */
44 async loadActor (id) {
45 const state = await this.tree.get(id.id)
46 const [module, storage] = await Promise.all([
47 this.tree.graph.tree(state.node, '1'),
48 this.tree.graph.get(state.node, '2')
49 ])
50 await this.tree.graph.get(module[1][1], '')
51 const [type, nonce] = state.value
52 const Container = this._modules[type]
53
54 // create a new actor instance
55 const actor = new Actor({
56 hypervisor: this,
57 state,
58 Container,
59 id,
60 nonce,
61 module,
62 storage,
63 tree: this.tree
64 })
65
66 await actor.startup()
67 return actor
68 }
69
70 /**
71 * creates an actor from a module and code
72 * @param {Module} mod - the module
73 * @param {Buffer} code - the code
74 * @return {ActorRef}
75 */
76 newActor (mod, code) {
77 const modRef = this.createModule(mod, code)
78 return this.createActor(modRef)
79 }
80
81 /**
82 * creates a modref from a module and code
83 * @param {Module} mod - the module
84 * @param {Buffer} code - the code
85 * @param {id} id - the id for the module
86 * @return {ModuleRef}
87 */
88 createModule (mod, code, id = {nonce: this.nonce++, parent: null}) {
89 const moduleID = generateId(id)
90 const Module = this._modules[mod.typeId]
91 const {exports, state} = Module.onCreation(code)
92 return new ModuleRef(moduleID, mod.typeId, exports, state, code)
93 }
94
95 /**
96 * creates an instance of an Actor
97 * @param {ModuleRef} type - the modref
98 * @param {Object} id - the id for the actor
99 * @return {ActorRef}
100 */
101 createActor (modRef, id = {nonce: this.nonce++, parent: null}) {
102 const actorId = generateId(id)
103 const metaData = [modRef.type, 0]
104
105 // save the container in the state
106 this.tree.set(actorId.id, metaData).then(node => {
107 // save the code
108 node[1] = [modRef.id.id, {
109 '/': modRef.code['/']
110 }]
111 // save the storage
112 node[2] = {
113 '/': modRef.state
114 }
115 })
116
117 return new ActorRef(actorId, modRef)
118 }
119
120 /**
121 * creates a state root when scheduler is idle
122 * @returns {Promise}
123 */
124 async createStateRoot () {
125 if (this.scheduler._running) {
126 await new Promise((resolve, reject) => {
127 this.scheduler.once('idle', resolve)
128 })
129 }
130
131 await this.tree.set(Buffer.from([0]), this.nonce)
132 return this.tree.flush()
133 }
134
135 /**
136 * set the state root. The promise must resolve before creating or sending any more messages to the hypervisor
137 * @param {Buffer} stateRoot
138 * @return {Promise}
139 */
140 async setStateRoot (stateRoot) {
141 this.tree.root = stateRoot
142 const node = await this.tree.get(Buffer.from([0]))
143 this.nonce = node.value
144 }
145
146 /**
147 * registers a module with the hypervisor
148 * @param {Function} Constructor - the module's constructor
149 */
150 registerModule (Constructor) {
151 this._modules[Constructor.typeId] = Constructor
152 }
153
154 /**
155 * register a driver with the hypervisor
156 * @param {Driver} driver
157 */
158 registerDriver (driver) {
159 driver.startup(this)
160 this.scheduler.drivers.set(driver.id.toString(), driver)
161 }
162}
163

Built with git-ssb-web