Files: 3d3fc0d82dd65f14b8533dcd2fb881c9fbbb1bd3 / index.js
3553 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() |
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 | async send (message) { |
24 | const id = message.funcRef.destId |
25 | const instance = await this.getActor(id) |
26 | instance.queue(message) |
27 | } |
28 | |
29 | // loads an instance of a container from the state |
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 | }) |
44 | |
45 | await actor.startup() |
46 | this.scheduler.update(actor) |
47 | return actor |
48 | } |
49 | |
50 | /** |
51 | * gets an existsing actor |
52 | * @param {string} id - the actor's ID |
53 | * @returns {Promise} |
54 | */ |
55 | async getActor (id) { |
56 | let actor = this.scheduler.getInstance(id) |
57 | if (!actor) { |
58 | const resolve = this.scheduler.lock(id) |
59 | actor = await this._loadActor(id) |
60 | resolve(actor) |
61 | } |
62 | return actor |
63 | } |
64 | |
65 | /** |
66 | * creates an instance of an Actor |
67 | * @param {Integer} type - the type id for the container |
68 | * @param {Object} message - an intial [message](https://github.com/primea/js-primea-message) to send newly created actor |
69 | * @param {Object} id - the id for the actor |
70 | */ |
71 | async createActor (type, code, id = {nonce: this.nonce++, parent: null}) { |
72 | const encoded = encodedID(id) |
73 | const idHash = await this._getHashFromObj(encoded) |
74 | const metaData = Actor.serializeMetaData(type) |
75 | |
76 | // save the container in the state |
77 | this.tree.set(idHash, metaData) |
78 | const Container = this._containerTypes[type] |
79 | await Container.validate(code) |
80 | const module = await Container.compile(code) |
81 | return { |
82 | id: idHash, |
83 | exports: Container.exports(module, idHash) |
84 | } |
85 | } |
86 | |
87 | // get a hash from a POJO |
88 | _getHashFromObj (obj) { |
89 | return this.tree.constructor.getMerkleLink(obj) |
90 | } |
91 | |
92 | /** |
93 | * creates a state root starting from a given container and a given number of |
94 | * ticks |
95 | * @param {Number} ticks the number of ticks at which to create the state root |
96 | * @returns {Promise} |
97 | */ |
98 | async createStateRoot (ticks = Infinity) { |
99 | await this.scheduler.wait(ticks) |
100 | return this.tree.flush() |
101 | } |
102 | |
103 | /** |
104 | * regirsters a container with the hypervisor |
105 | * @param {Class} Constructor - a Class for instantiating the container |
106 | * @param {*} args - any args that the contructor takes |
107 | * @param {Integer} typeId - the container's type identification ID |
108 | */ |
109 | registerContainer (Constructor) { |
110 | this._containerTypes[Constructor.typeId] = Constructor |
111 | } |
112 | } |
113 | |
114 | function encodedID (id) { |
115 | const nonce = Buffer.from([id.nonce]) |
116 | if (id.parent) { |
117 | return Buffer.concat([nonce, id.parent]) |
118 | } else { |
119 | return nonce |
120 | } |
121 | } |
122 |
Built with git-ssb-web