git ssb

0+

wanderer🌟 / js-primea-hypervisor



Tree: 8b45001673d47c52d4b2968aec2f1e3493835633

Files: 8b45001673d47c52d4b2968aec2f1e3493835633 / index.js

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

Built with git-ssb-web