git ssb

0+

wanderer🌟 / js-primea-hypervisor



Tree: 755514bf413e3af2218088969d281e9b6e56e8d7

Files: 755514bf413e3af2218088969d281e9b6e56e8d7 / index.js

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

Built with git-ssb-web