Files: dcd4e757706b7946eff933674387a29bc6270077 / index.js
4316 bytesRaw
1 | const Buffer = require('safe-buffer').Buffer |
2 | const Actor = require('./actor.js') |
3 | const Scheduler = require('./scheduler.js') |
4 | const {decoder, generateActorId} = require('primea-objects') |
5 | |
6 | module.exports = class Hypervisor { |
7 | /** |
8 | * The Hypervisor manages the container instances by instantiating them and |
9 | * destorying them when possible. It also facilitates localating 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.container - an array of containers to regester |
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._containerTypes = {} |
21 | this.nonce = opts.nonce || 0 |
22 | this.meter = opts.meter !== undefined ? opts.meter : true; |
23 | (opts.containers || []).forEach(container => this.registerContainer(container)); |
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 container 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 [code, storage] = await Promise.all([ |
47 | this.tree.graph.get(state.node, '1'), |
48 | this.tree.graph.get(state.node, '2') |
49 | ]) |
50 | const [type, nonce] = state.value |
51 | const Container = this._containerTypes[type] |
52 | |
53 | // create a new actor instance |
54 | const actor = new Actor({ |
55 | hypervisor: this, |
56 | state, |
57 | Container, |
58 | id, |
59 | nonce, |
60 | type, |
61 | code, |
62 | storage, |
63 | tree: this.tree |
64 | }) |
65 | |
66 | await actor.startup() |
67 | return actor |
68 | } |
69 | |
70 | /** |
71 | * creates an instance of an Actor |
72 | * @param {Integer} type - the type id for the container |
73 | * @param {Object} message - an intial [message](https://github.com/primea/js-primea-message) to send newly created actor |
74 | * @param {Object} id - the id for the actor |
75 | */ |
76 | createActor (type, code, id = {nonce: this.nonce++, parent: null}) { |
77 | const Container = this._containerTypes[type] |
78 | const actorId = generateActorId(id) |
79 | const {modRef: module, state} = Container.onCreation(code, actorId) |
80 | const metaData = [type, 0] |
81 | |
82 | // save the container in the state |
83 | this.tree.set(actorId.id, metaData).then(node => { |
84 | // save the code |
85 | node[1] = { |
86 | '/': code |
87 | } |
88 | // save the storage |
89 | node[2] = { |
90 | '/': state |
91 | } |
92 | }) |
93 | |
94 | return { |
95 | id: actorId, |
96 | module |
97 | } |
98 | } |
99 | |
100 | /** |
101 | * creates a state root starting from a given container and a given number of |
102 | * ticks |
103 | * @param {Number} ticks the number of ticks at which to create the state root |
104 | * @returns {Promise} |
105 | */ |
106 | async createStateRoot () { |
107 | if (this.scheduler._running) { |
108 | await new Promise((resolve, reject) => { |
109 | this.scheduler.once('idle', resolve) |
110 | }) |
111 | } |
112 | await this.tree.set(Buffer.from([0]), this.nonce) |
113 | return this.tree.flush() |
114 | } |
115 | |
116 | /** |
117 | * set the state root. The promise must resolve before creating or sending any more messages to the hypervisor |
118 | * @param {Buffer} stateRoot |
119 | * @return {Promise} |
120 | */ |
121 | async setStateRoot (stateRoot) { |
122 | this.tree.root = stateRoot |
123 | const node = await this.tree.get(Buffer.from([0])) |
124 | this.nonce = node.value |
125 | } |
126 | |
127 | /** |
128 | * regesters a container with the hypervisor |
129 | * @param {Function} Constructor - the container's constuctor |
130 | */ |
131 | registerContainer (Constructor) { |
132 | this._containerTypes[Constructor.typeId] = Constructor |
133 | } |
134 | |
135 | /** |
136 | * register a driver with the hypervisor |
137 | * @param {driver} driver |
138 | */ |
139 | registerDriver (driver) { |
140 | driver.startup(this) |
141 | this.scheduler.drivers.set(driver.id.toString(), driver) |
142 | } |
143 | } |
144 |
Built with git-ssb-web