Files: 7c1b9abb1401216b0a7325dfb2213933eda8826a / index.js
4844 bytesRaw
1 | const Buffer = require('safe-buffer').Buffer |
2 | const Actor = require('./actor.js') |
3 | const Scheduler = require('./scheduler.js') |
4 | const {decoder, generateId, ModuleRef, ActorRef} = require('primea-objects') |
5 | |
6 | const debug = require('debug')('lifecycle:createActor') |
7 | |
8 | module.exports = class Hypervisor { |
9 | /** |
10 | * The Hypervisor manages module instances by instantiating them and |
11 | * destroying them when possible. It also facilitates locating 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.modules - an array of modules to register |
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._modules = {} |
23 | this.nonce = opts.nonce || 0 |
24 | this.meter = opts.meter !== undefined ? opts.meter : true; |
25 | (opts.modules || []).forEach(mod => this.registerModule(mod)); |
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 module 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) |
48 | const [module, storage] = await Promise.all([ |
49 | this.tree.graph.tree(state.node, '1'), |
50 | this.tree.graph.get(state.node, '2') |
51 | ]) |
52 | await this.tree.graph.get(module[1][1], '') |
53 | const [type, nonce] = state.value |
54 | const Container = this._modules[type] |
55 | |
56 | // create a new actor instance |
57 | const actor = new Actor({ |
58 | hypervisor: this, |
59 | state, |
60 | Container, |
61 | id, |
62 | nonce, |
63 | module, |
64 | storage, |
65 | tree: this.tree |
66 | }) |
67 | |
68 | await actor.startup() |
69 | return actor |
70 | } |
71 | |
72 | /** |
73 | * creates an actor from a module and code |
74 | * @param {Module} mod - the module |
75 | * @param {Buffer} code - the code |
76 | * @return {ActorRef} |
77 | */ |
78 | newActor (mod, code) { |
79 | const modRef = this.createModule(mod, code) |
80 | return this.createActor(modRef) |
81 | } |
82 | |
83 | /** |
84 | * creates a modref from a module and code |
85 | * @param {Module} mod - the module |
86 | * @param {Buffer} code - the code |
87 | * @param {id} id - the id for the module |
88 | * @return {ModuleRef} |
89 | */ |
90 | createModule (mod, code, id = {nonce: this.nonce++, parent: null}) { |
91 | const moduleID = generateId(id) |
92 | const Module = this._modules[mod.typeId] |
93 | const {exports, state} = Module.onCreation(code) |
94 | return new ModuleRef(moduleID, mod.typeId, exports, state, code) |
95 | } |
96 | |
97 | /** |
98 | * creates an instance of an Actor |
99 | * @param {ModuleRef} type - the modref |
100 | * @param {Object} id - the id for the actor |
101 | * @return {ActorRef} |
102 | */ |
103 | createActor (modRef, id = {nonce: this.nonce++, parent: null}) { |
104 | const actorId = generateId(id) |
105 | const metaData = [modRef.type, 0] |
106 | debug('new id', actorId.toJSON()) |
107 | debug(modRef.toJSON()) |
108 | |
109 | // save the container in the state |
110 | this.tree.set(actorId.id, metaData).then(node => { |
111 | // save the code |
112 | node[1] = [modRef.id.id, { |
113 | '/': modRef.code['/'] |
114 | }] |
115 | // save the storage |
116 | node[2] = { |
117 | '/': modRef.state |
118 | } |
119 | }) |
120 | |
121 | return new ActorRef(actorId, modRef) |
122 | } |
123 | |
124 | /** |
125 | * creates a state root when scheduler is idle |
126 | * @returns {Promise} |
127 | */ |
128 | async createStateRoot () { |
129 | if (this.scheduler._running) { |
130 | await new Promise((resolve, reject) => { |
131 | this.scheduler.once('idle', resolve) |
132 | }) |
133 | } |
134 | |
135 | await this.tree.set(Buffer.from([0]), this.nonce) |
136 | return this.tree.flush() |
137 | } |
138 | |
139 | /** |
140 | * set the state root. The promise must resolve before creating or sending any more messages to the hypervisor |
141 | * @param {Buffer} stateRoot |
142 | * @return {Promise} |
143 | */ |
144 | async setStateRoot (stateRoot) { |
145 | this.tree.root = stateRoot |
146 | const node = await this.tree.get(Buffer.from([0])) |
147 | this.nonce = node.value |
148 | } |
149 | |
150 | /** |
151 | * registers a module with the hypervisor |
152 | * @param {Function} Constructor - the module's constructor |
153 | */ |
154 | registerModule (Constructor) { |
155 | this._modules[Constructor.typeId] = Constructor |
156 | } |
157 | |
158 | /** |
159 | * register a driver with the hypervisor |
160 | * @param {Driver} driver |
161 | */ |
162 | registerDriver (driver) { |
163 | driver.startup(this) |
164 | this.scheduler.drivers.set(driver.id.toString(), driver) |
165 | } |
166 | } |
167 |
Built with git-ssb-web