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