git ssb

0+

wanderer🌟 / js-primea-hypervisor



Tree: ae084f349c08473f2357c07bd5c87a4cadec8607

Files: ae084f349c08473f2357c07bd5c87a4cadec8607 / index.js

3707 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) {
11 this.tree = tree
12 this.scheduler = new Scheduler()
13 this._containerTypes = {}
14 this.nonce = 0
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.get(id, true)
32 const container = this._containerTypes[state.value.type]
33
34 // create a new actor instance
35 const actor = new Actor({
36 hypervisor: this,
37 state: state,
38 container: container,
39 id: 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 this.nonce++
74 const idHash = await this._getHashFromObj(encoded)
75 const state = {
76 nonce: 0,
77 caps: {},
78 type: type
79 }
80
81 const code = message.data
82 if (code.length) {
83 state.code = code
84 }
85
86 // save the container in the state
87 await this.tree.set(idHash, state)
88
89 // create the container instance
90 const instance = await this._loadActor(idHash)
91
92 // send the intialization message
93 await instance.create(message)
94 return instance.mintCap()
95 }
96
97 // get a hash from a POJO
98 _getHashFromObj (obj) {
99 return this.tree.constructor.getMerkleLink(obj)
100 }
101
102 /**
103 * creates a state root starting from a given container and a given number of
104 * ticks
105 * @param {Number} ticks the number of ticks at which to create the state root
106 * @returns {Promise}
107 */
108 async createStateRoot (ticks) {
109 await this.scheduler.wait(ticks)
110 return this.tree.flush()
111 }
112
113 /**
114 * regirsters a container with the hypervisor
115 * @param {Class} Constructor - a Class for instantiating the container
116 * @param {*} args - any args that the contructor takes
117 * @param {Integer} typeId - the container's type identification ID
118 */
119 registerContainer (Constructor, args, typeId = Constructor.typeId) {
120 this._containerTypes[typeId] = {
121 Constructor: Constructor,
122 args: args
123 }
124 }
125}
126
127function encodedID (id) {
128 const nonce = Buffer.from([id.nonce])
129 if (id.parent) {
130 return Buffer.concat([nonce, id.parent])
131 } else {
132 return nonce
133 }
134}
135

Built with git-ssb-web