Files: b9423af2cd45a86722df9ab973b4db265a8fcaa3 / index.js
3843 bytesRaw
1 | const crypto = require('crypto') |
2 | const Actor = require('./actor.js') |
3 | const Scheduler = require('./scheduler.js') |
4 | const {ID} = require('./systemObjects.js') |
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 {Tree} tree - a [radix tree](https://github.com/dfinity/js-dfinity-radix-tree) to store the state |
11 | */ |
12 | constructor (tree, containers = [], drivers = [], nonce = 0) { |
13 | this.tree = tree |
14 | this.scheduler = new Scheduler(this) |
15 | this._containerTypes = {} |
16 | this.nonce = nonce |
17 | containers.forEach(container => this.registerContainer(container)) |
18 | drivers.forEach(driver => this.registerDriver(driver)) |
19 | } |
20 | |
21 | /** |
22 | * sends a message |
23 | * @param {Object} message - the [message](https://github.com/primea/js-primea-message) to send |
24 | * @returns {Promise} a promise that resolves once the receiving container is loaded |
25 | */ |
26 | send (messages) { |
27 | if (!Array.isArray(messages)) { |
28 | messages = [messages] |
29 | } |
30 | this.scheduler.queue(messages) |
31 | } |
32 | |
33 | async loadActor (id) { |
34 | const state = await this.tree.get(id.id, true) |
35 | const [code, storage] = await Promise.all([ |
36 | this.tree.graph.get(state.node, '1'), |
37 | this.tree.graph.get(state.node, '2') |
38 | ]) |
39 | const [type, nonce] = state.value |
40 | const Container = this._containerTypes[type] |
41 | |
42 | // create a new actor instance |
43 | const actor = new Actor({ |
44 | hypervisor: this, |
45 | state, |
46 | Container, |
47 | id, |
48 | nonce, |
49 | type, |
50 | code, |
51 | storage: storage || [], |
52 | tree: this.tree |
53 | }) |
54 | |
55 | await actor.startup() |
56 | return actor |
57 | } |
58 | |
59 | /** |
60 | * creates an instance of an Actor |
61 | * @param {Integer} type - the type id for the container |
62 | * @param {Object} message - an intial [message](https://github.com/primea/js-primea-message) to send newly created actor |
63 | * @param {Object} id - the id for the actor |
64 | */ |
65 | createActor (type, code, id = {nonce: this.nonce++, parent: null}) { |
66 | const Container = this._containerTypes[type] |
67 | const encoded = encodedID(id) |
68 | let idHash = this._hash(encoded) |
69 | idHash = new ID(idHash) |
70 | const module = Container.onCreation(code, idHash, this.tree) |
71 | const metaData = [type, 0] |
72 | |
73 | // save the container in the state |
74 | this.tree.set(idHash.id, metaData).then(node => { |
75 | // save the code |
76 | node[1] = { |
77 | '/': code |
78 | } |
79 | // save the storage |
80 | node[2] = { |
81 | '/': [] |
82 | } |
83 | }) |
84 | |
85 | return { |
86 | id: idHash, |
87 | module |
88 | } |
89 | } |
90 | |
91 | _hash (buf) { |
92 | const hash = crypto.createHash('sha256') |
93 | hash.update(buf) |
94 | return hash.digest().slice(0, 20) |
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 | createStateRoot () { |
104 | return new Promise((resolve, reject) => { |
105 | if (!this.scheduler._running) { |
106 | this.tree.flush().then(resolve) |
107 | } else { |
108 | this.scheduler.on('idle', () => { |
109 | this.tree.flush().then(resolve) |
110 | }) |
111 | } |
112 | }) |
113 | } |
114 | |
115 | /** |
116 | * regirsters a container with the hypervisor |
117 | * @param {Class} Constructor - a Class for instantiating the container |
118 | * @param {*} args - any args that the contructor takes |
119 | * @param {Integer} typeId - the container's type identification ID |
120 | */ |
121 | registerContainer (Constructor) { |
122 | this._containerTypes[Constructor.typeId] = Constructor |
123 | } |
124 | |
125 | registerDriver (driver) { |
126 | this.scheduler.drivers.set(driver.id.id.toString('hex'), driver) |
127 | } |
128 | } |
129 | |
130 | function encodedID (id) { |
131 | const nonce = Buffer.from([id.nonce]) |
132 | if (id.parent) { |
133 | return Buffer.concat([nonce, id.parent.id]) |
134 | } else { |
135 | return nonce |
136 | } |
137 | } |
138 |
Built with git-ssb-web