Files: 455f5db59297ab334a58c1589e8743cebdab3817 / index.js
3839 bytesRaw
1 | const Kernel = 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) { |
11 | this.tree = tree |
12 | this.scheduler = new Scheduler() |
13 | this._containerTypes = {} |
14 | this.nonce = 0 |
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 | async send (cap, message) { |
24 | const id = cap.destId |
25 | const instance = await this.getInstance(id) |
26 | instance.queue(message) |
27 | } |
28 | |
29 | // loads an instance of a container from the state |
30 | async _loadInstance (id) { |
31 | const state = await this.tree.get(id, true) |
32 | const container = this._containerTypes[state.value.type] |
33 | |
34 | // create a new kernel instance |
35 | const kernel = new Kernel({ |
36 | hypervisor: this, |
37 | state: state.value, |
38 | node: state.node, |
39 | code: state.value.code, |
40 | container: container, |
41 | id: id |
42 | }) |
43 | |
44 | // save the newly created instance |
45 | this.scheduler.update(kernel) |
46 | return kernel |
47 | } |
48 | |
49 | /** |
50 | * gets an existsing container instances |
51 | * @param {string} id - the containers ID |
52 | * @returns {Promise} |
53 | */ |
54 | async getInstance (id) { |
55 | let instance = this.scheduler.getInstance(id) |
56 | if (instance) { |
57 | return instance |
58 | } else { |
59 | const resolve = this.scheduler.lock(id) |
60 | const instance = await this._loadInstance(id) |
61 | await instance.startup() |
62 | resolve(instance) |
63 | return instance |
64 | } |
65 | } |
66 | |
67 | /** |
68 | * creates an instance of a container |
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 instance |
71 | * @param {Object} id - the id for the instance |
72 | */ |
73 | async createInstance (type, message, id = {nonce: this.nonce, parent: null}) { |
74 | const encoded = encodedID(id) |
75 | this.nonce++ |
76 | const idHash = await this._getHashFromObj(encoded) |
77 | const state = { |
78 | nonce: 0, |
79 | caps: {}, |
80 | type: type |
81 | } |
82 | |
83 | const code = message.data |
84 | if (code.length) { |
85 | state.code = code |
86 | } |
87 | |
88 | // save the container in the state |
89 | await this.tree.set(idHash, state) |
90 | |
91 | // create the container instance |
92 | const instance = await this._loadInstance(idHash) |
93 | |
94 | // send the intialization message |
95 | await instance.create(message) |
96 | return instance.mintCap() |
97 | } |
98 | |
99 | // get a hash from a POJO |
100 | _getHashFromObj (obj) { |
101 | return this.tree.constructor.getMerkleLink(obj) |
102 | } |
103 | |
104 | /** |
105 | * creates a state root starting from a given container and a given number of |
106 | * ticks |
107 | * @param {Number} ticks the number of ticks at which to create the state root |
108 | * @returns {Promise} |
109 | */ |
110 | async createStateRoot (ticks) { |
111 | await this.scheduler.wait(ticks) |
112 | return this.tree.flush() |
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 {Interger} typeId - the container's type identification ID |
120 | */ |
121 | registerContainer (Constructor, args, typeId = Constructor.typeId) { |
122 | this._containerTypes[typeId] = { |
123 | Constructor: Constructor, |
124 | args: args |
125 | } |
126 | } |
127 | } |
128 | |
129 | function encodedID (id) { |
130 | const nonce = Buffer.from([id.nonce]) |
131 | if (id.parent) { |
132 | return Buffer.concat([nonce, id.parent]) |
133 | } else { |
134 | return nonce |
135 | } |
136 | } |
137 |
Built with git-ssb-web