git ssb

0+

wanderer🌟 / js-primea-hypervisor



Tree: eaf33c2d3536ae3ec69d335f257a01bfee3f9dd3

Files: eaf33c2d3536ae3ec69d335f257a01bfee3f9dd3 / index.js

3581 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 container = this._containerTypes[state.root['/'][3][0]]
33
34 // create a new actor instance
35 const actor = new Actor({
36 hypervisor: this,
37 state,
38 container,
39 id
40 })
41
42 // save the newly created instance
43 this.scheduler.update(actor)
44 return actor
45 }
46
47 /**
48 * gets an existsing actor
49 * @param {string} id - the actor's ID
50 * @returns {Promise}
51 */
52 async getActor (id) {
53 let actor = this.scheduler.getInstance(id)
54 if (actor) {
55 return actor
56 } else {
57 const resolve = this.scheduler.lock(id)
58 const actor = await this._loadActor(id)
59 await actor.startup()
60 resolve(actor)
61 return actor
62 }
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, message, id = {nonce: this.nonce++, parent: null}) {
72 const encoded = encodedID(id)
73 const idHash = await this._getHashFromObj(encoded)
74 const state = Buffer.from([type, 0, 0])
75
76 // save the container in the state
77 this.tree.set(idHash, state)
78
79 // create the container instance
80 const instance = await this._loadActor(idHash)
81
82 // send the intialization message
83 await instance.create(message)
84 return instance.mintCap()
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, args, typeId = Constructor.typeId) {
110 this._containerTypes[typeId] = {
111 Constructor: Constructor,
112 args: args
113 }
114 }
115}
116
117function encodedID (id) {
118 const nonce = Buffer.from([id.nonce])
119 if (id.parent) {
120 return Buffer.concat([nonce, id.parent])
121 } else {
122 return nonce
123 }
124}
125

Built with git-ssb-web