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