git ssb

0+

wanderer🌟 / js-primea-hypervisor



Tree: 77a97926a01c38ec889ed82e584cdf833cad6a31

Files: 77a97926a01c38ec889ed82e584cdf833cad6a31 / index.js

3633 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 message.tag = cap.tag
25 const id = cap.destId
26 const instance = await this.getActor(id)
27 instance.queue(message)
28 }
29
30 // loads an instance of a container from the state
31 async _loadActor (id) {
32 const state = await this.tree.getSubTree(id)
33 const {type, nonce} = Actor.deserializeMetaData(state.root['/'][3])
34 const container = this._containerTypes[type]
35
36 // create a new actor instance
37 const actor = new Actor({
38 hypervisor: this,
39 state,
40 container,
41 id,
42 nonce,
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 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