git ssb

0+

wanderer🌟 / js-primea-hypervisor



Tree: e47dbb585f72b65914431ec10f554e43226dc121

Files: e47dbb585f72b65914431ec10f554e43226dc121 / index.js

3613 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 (cap, message) {
24 const id = cap.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 // save the newly created instance
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 await actor.startup()
61 resolve(actor)
62 }
63 return actor
64 }
65
66 /**
67 * creates an instance of an Actor
68 * @param {Integer} type - the type id for the container
69 * @param {Object} message - an intial [message](https://github.com/primea/js-primea-message) to send newly created actor
70 * @param {Object} id - the id for the actor
71 */
72 async createActor (type, message, id = {nonce: this.nonce++, parent: null}) {
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
80 // create the container instance
81 const instance = await this._loadActor(idHash)
82
83 // send the intialization message
84 await instance.create(message)
85 return instance.mintCap()
86 }
87
88 // get a hash from a POJO
89 _getHashFromObj (obj) {
90 return this.tree.constructor.getMerkleLink(obj)
91 }
92
93 /**
94 * creates a state root starting from a given container and a given number of
95 * ticks
96 * @param {Number} ticks the number of ticks at which to create the state root
97 * @returns {Promise}
98 */
99 async createStateRoot (ticks = Infinity) {
100 await this.scheduler.wait(ticks)
101 return this.tree.flush()
102 }
103
104 /**
105 * regirsters a container with the hypervisor
106 * @param {Class} Constructor - a Class for instantiating the container
107 * @param {*} args - any args that the contructor takes
108 * @param {Integer} typeId - the container's type identification ID
109 */
110 registerContainer (Constructor, args, typeId = Constructor.typeId) {
111 this._containerTypes[typeId] = {
112 Constructor,
113 args
114 }
115 }
116}
117
118function encodedID (id) {
119 const nonce = Buffer.from([id.nonce])
120 if (id.parent) {
121 return Buffer.concat([nonce, id.parent])
122 } else {
123 return nonce
124 }
125}
126

Built with git-ssb-web