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