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