Files: cc5033335b1996f393363b44254f957946b87199 / index.js
4273 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 {actor, 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 actor |
95 | } |
96 | |
97 | /** |
98 | * creates a state root starting from a given container and a given number of |
99 | * ticks |
100 | * @param {Number} ticks the number of ticks at which to create the state root |
101 | * @returns {Promise} |
102 | */ |
103 | async createStateRoot () { |
104 | if (this.scheduler._running) { |
105 | await new Promise((resolve, reject) => { |
106 | this.scheduler.once('idle', resolve) |
107 | }) |
108 | } |
109 | await this.tree.set(Buffer.from([0]), this.nonce) |
110 | return this.tree.flush() |
111 | } |
112 | |
113 | /** |
114 | * set the state root. The promise must resolve before creating or sending any more messages to the hypervisor |
115 | * @param {Buffer} stateRoot |
116 | * @return {Promise} |
117 | */ |
118 | async setStateRoot (stateRoot) { |
119 | this.tree.root = stateRoot |
120 | const node = await this.tree.get(Buffer.from([0])) |
121 | this.nonce = node.value |
122 | } |
123 | |
124 | /** |
125 | * regesters a container with the hypervisor |
126 | * @param {Function} Constructor - the container's constuctor |
127 | */ |
128 | registerContainer (Constructor) { |
129 | this._containerTypes[Constructor.typeId] = Constructor |
130 | } |
131 | |
132 | /** |
133 | * register a driver with the hypervisor |
134 | * @param {driver} driver |
135 | */ |
136 | registerDriver (driver) { |
137 | driver.startup(this) |
138 | this.scheduler.drivers.set(driver.id.toString(), driver) |
139 | } |
140 | } |
141 |
Built with git-ssb-web