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