git ssb

0+

wanderer🌟 / js-primea-hypervisor



Tree: d386ffea54d493af4f2eb1eeadc633dd579cdbe3

Files: d386ffea54d493af4f2eb1eeadc633dd579cdbe3 / index.js

3132 bytesRaw
1const Actor = require('./actor.js')
2const Scheduler = require('./scheduler.js')
3
4module.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 })
44
45 await actor.startup()
46 return actor
47 }
48
49 /**
50 * creates an instance of an Actor
51 * @param {Integer} type - the type id for the container
52 * @param {Object} message - an intial [message](https://github.com/primea/js-primea-message) to send newly created actor
53 * @param {Object} id - the id for the actor
54 */
55 async createActor (type, code, id = {nonce: this.nonce++, parent: null}) {
56 const Container = this._containerTypes[type]
57 await Container.validate(code)
58 const encoded = encodedID(id)
59 const idHash = await this._getHashFromObj(encoded)
60 const metaData = Actor.serializeMetaData(type)
61
62 // save the container in the state
63 this.tree.set(idHash, metaData)
64 return {
65 id: idHash,
66 exports: Container.exports(code, idHash)
67 }
68 }
69
70 // get a hash from a POJO
71 _getHashFromObj (obj) {
72 return this.tree.constructor.getMerkleLink(obj)
73 }
74
75 /**
76 * creates a state root starting from a given container and a given number of
77 * ticks
78 * @param {Number} ticks the number of ticks at which to create the state root
79 * @returns {Promise}
80 */
81 createStateRoot () {
82 return new Promise((resolve, reject) => {
83 this.scheduler.on('idle', () => {
84 this.tree.flush().then(resolve)
85 })
86 })
87 }
88
89 /**
90 * regirsters a container with the hypervisor
91 * @param {Class} Constructor - a Class for instantiating the container
92 * @param {*} args - any args that the contructor takes
93 * @param {Integer} typeId - the container's type identification ID
94 */
95 registerContainer (Constructor) {
96 this._containerTypes[Constructor.typeId] = Constructor
97 }
98}
99
100function encodedID (id) {
101 const nonce = Buffer.from([id.nonce])
102 if (id.parent) {
103 return Buffer.concat([nonce, id.parent])
104 } else {
105 return nonce
106 }
107}
108

Built with git-ssb-web