git ssb

0+

wanderer🌟 / js-primea-hypervisor



Tree: 3f8c23e36f6d90d0c86b778fb27c24c5aa57c8a1

Files: 3f8c23e36f6d90d0c86b778fb27c24c5aa57c8a1 / index.js

3592 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 (message) {
24 const id = message.funcRef.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 await actor.startup()
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 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, code, 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 const Container = this._containerTypes[type]
80 await Container.validate(code)
81 const module = await Container.compile(code)
82 return {
83 id: idHash,
84 exports: Container.exports(module, idHash)
85 }
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) {
111 this._containerTypes[Constructor.typeId] = Constructor
112 }
113}
114
115function encodedID (id) {
116 const nonce = Buffer.from([id.nonce])
117 if (id.parent) {
118 return Buffer.concat([nonce, id.parent])
119 } else {
120 return nonce
121 }
122}
123

Built with git-ssb-web