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