git ssb

0+

wanderer🌟 / js-primea-hypervisor



Tree: ff617aaaa70d98abc58ed432aac680e76748c148

Files: ff617aaaa70d98abc58ed432aac680e76748c148 / index.js

3440 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()
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 actor = await this.getActor(id)
26 actor.queue(message)
27 }
28
29 async _loadActor (id) {
30 const state = await this.tree.getSubTree(id)
31 const {type, nonce} = Actor.deserializeMetaData(state.root['/'][3])
32 const container = this._containerTypes[type]
33
34 // create a new actor instance
35 const actor = new Actor({
36 hypervisor: this,
37 state,
38 container,
39 id,
40 nonce,
41 type
42 })
43
44 await actor.startup()
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.getActor(id)
56 if (!actor) {
57 const resolve = this.scheduler.lock(id)
58 actor = await this._loadActor(id)
59 resolve(actor)
60 }
61 return actor
62 }
63
64 /**
65 * creates an instance of an Actor
66 * @param {Integer} type - the type id for the container
67 * @param {Object} message - an intial [message](https://github.com/primea/js-primea-message) to send newly created actor
68 * @param {Object} id - the id for the actor
69 */
70 async createActor (type, code, id = {nonce: this.nonce++, parent: null}) {
71 const Container = this._containerTypes[type]
72 await Container.validate(code)
73 const encoded = encodedID(id)
74 const idHash = await this._getHashFromObj(encoded)
75 const metaData = Actor.serializeMetaData(type)
76
77 // save the container in the state
78 this.tree.set(idHash, metaData)
79 return {
80 id: idHash,
81 exports: Container.exports(code, idHash)
82 }
83 }
84
85 // get a hash from a POJO
86 _getHashFromObj (obj) {
87 return this.tree.constructor.getMerkleLink(obj)
88 }
89
90 /**
91 * creates a state root starting from a given container and a given number of
92 * ticks
93 * @param {Number} ticks the number of ticks at which to create the state root
94 * @returns {Promise}
95 */
96 async createStateRoot (ticks = Infinity) {
97 await this.scheduler.wait(ticks)
98 return this.tree.flush()
99 }
100
101 /**
102 * regirsters a container with the hypervisor
103 * @param {Class} Constructor - a Class for instantiating the container
104 * @param {*} args - any args that the contructor takes
105 * @param {Integer} typeId - the container's type identification ID
106 */
107 registerContainer (Constructor) {
108 this._containerTypes[Constructor.typeId] = Constructor
109 }
110}
111
112function encodedID (id) {
113 const nonce = Buffer.from([id.nonce])
114 if (id.parent) {
115 return Buffer.concat([nonce, id.parent])
116 } else {
117 return nonce
118 }
119}
120

Built with git-ssb-web