Files: ad560c8f8c3258d82e01e7dcf6778f3b3d1e8265 / index.js
3189 bytesRaw
1 | const Actor = require('./actor.js') |
2 | const Scheduler = require('./scheduler.js') |
3 | |
4 | module.exports = class Hypervisor { |
5 | /** |
6 | * The Hypervisor manages the container instances by instantiating them and |
7 | * destorying them when possible. It also facilitates localating Containers |
8 | * @param {Tree} tree - a [radix tree](https://github.com/dfinity/js-dfinity-radix-tree) to store the state |
9 | */ |
10 | constructor (tree, nonce = 0) { |
11 | this.tree = tree |
12 | this.scheduler = new Scheduler(this) |
13 | this._containerTypes = {} |
14 | this.nonce = nonce |
15 | } |
16 | |
17 | /** |
18 | * sends a message |
19 | * @param {Object} cap - the capabilitly used to send the message |
20 | * @param {Object} message - the [message](https://github.com/primea/js-primea-message) to send |
21 | * @returns {Promise} a promise that resolves once the receiving container is loaded |
22 | */ |
23 | send (messages) { |
24 | if (!Array.isArray(messages)) { |
25 | messages = [messages] |
26 | } |
27 | this.scheduler.queue(messages) |
28 | } |
29 | |
30 | async loadActor (id) { |
31 | const state = await this.tree.getSubTree(id) |
32 | const {type, nonce} = Actor.deserializeMetaData(state.root['/'][3]) |
33 | const Container = this._containerTypes[type] |
34 | |
35 | // create a new actor instance |
36 | const actor = new Actor({ |
37 | hypervisor: this, |
38 | state, |
39 | Container, |
40 | id, |
41 | nonce, |
42 | type, |
43 | cachedb: this.tree.dag._dag |
44 | }) |
45 | |
46 | await actor.startup() |
47 | return actor |
48 | } |
49 | |
50 | /** |
51 | * creates an instance of an Actor |
52 | * @param {Integer} type - the type id for the container |
53 | * @param {Object} message - an intial [message](https://github.com/primea/js-primea-message) to send newly created actor |
54 | * @param {Object} id - the id for the actor |
55 | */ |
56 | async createActor (type, code, id = {nonce: this.nonce++, parent: null}) { |
57 | const Container = this._containerTypes[type] |
58 | const encoded = encodedID(id) |
59 | const idHash = await this._getHashFromObj(encoded) |
60 | const exports = await Container.onCreation(code, idHash, this.tree.dag._dag) |
61 | const metaData = Actor.serializeMetaData(type) |
62 | |
63 | // save the container in the state |
64 | this.tree.set(idHash, metaData) |
65 | return { |
66 | id: idHash, |
67 | exports: exports |
68 | } |
69 | } |
70 | |
71 | // get a hash from a POJO |
72 | _getHashFromObj (obj) { |
73 | return this.tree.constructor.getMerkleLink(obj) |
74 | } |
75 | |
76 | /** |
77 | * creates a state root starting from a given container and a given number of |
78 | * ticks |
79 | * @param {Number} ticks the number of ticks at which to create the state root |
80 | * @returns {Promise} |
81 | */ |
82 | createStateRoot () { |
83 | return new Promise((resolve, reject) => { |
84 | this.scheduler.on('idle', () => { |
85 | this.tree.flush().then(resolve) |
86 | }) |
87 | }) |
88 | } |
89 | |
90 | /** |
91 | * regirsters a container with the hypervisor |
92 | * @param {Class} Constructor - a Class for instantiating the container |
93 | * @param {*} args - any args that the contructor takes |
94 | * @param {Integer} typeId - the container's type identification ID |
95 | */ |
96 | registerContainer (Constructor) { |
97 | this._containerTypes[Constructor.typeId] = Constructor |
98 | } |
99 | } |
100 | |
101 | function encodedID (id) { |
102 | const nonce = Buffer.from([id.nonce]) |
103 | if (id.parent) { |
104 | return Buffer.concat([nonce, id.parent]) |
105 | } else { |
106 | return nonce |
107 | } |
108 | } |
109 |
Built with git-ssb-web