git ssb

0+

wanderer🌟 / js-primea-hypervisor



Tree: fc3efb6d1a8e5ef2e4cbd0017fa9649b221b8793

Files: fc3efb6d1a8e5ef2e4cbd0017fa9649b221b8793 / index.js

3406 bytesRaw
1const Actor = require('./actor.js')
2const Scheduler = require('./scheduler.js')
3const {ID} = require('./systemObjects.js')
4
5module.exports = class Hypervisor {
6 /**
7 * The Hypervisor manages the container instances by instantiating them and
8 * destorying them when possible. It also facilitates localating Containers
9 * @param {Tree} tree - a [radix tree](https://github.com/dfinity/js-dfinity-radix-tree) to store the state
10 */
11 constructor (tree, nonce = 0) {
12 this.tree = tree
13 this.scheduler = new Scheduler(this)
14 this._containerTypes = {}
15 this.nonce = nonce
16 }
17
18 /**
19 * sends a message
20 * @param {Object} cap - the capabilitly used to send the message
21 * @param {Object} message - the [message](https://github.com/primea/js-primea-message) to send
22 * @returns {Promise} a promise that resolves once the receiving container is loaded
23 */
24 send (messages) {
25 if (!Array.isArray(messages)) {
26 messages = [messages]
27 }
28 this.scheduler.queue(messages)
29 }
30
31 async loadActor (id) {
32 const state = await this.tree.getSubTree(id.id)
33 const code = state.get(Buffer.from([0]))
34 const {type, nonce} = Actor.deserializeMetaData(state.root['/'][3])
35 const Container = this._containerTypes[type]
36
37 // create a new actor instance
38 const actor = new Actor({
39 hypervisor: this,
40 state,
41 Container,
42 id,
43 nonce,
44 type,
45 code,
46 cachedb: this.tree.dag._dag
47 })
48
49 await actor.startup()
50 return actor
51 }
52
53 /**
54 * creates an instance of an Actor
55 * @param {Integer} type - the type id for the container
56 * @param {Object} message - an intial [message](https://github.com/primea/js-primea-message) to send newly created actor
57 * @param {Object} id - the id for the actor
58 */
59 async createActor (type, code, id = {nonce: this.nonce++, parent: null}) {
60 const Container = this._containerTypes[type]
61 const encoded = encodedID(id)
62 let idHash = await this._getHashFromObj(encoded)
63 idHash = new ID(idHash)
64 const module = await Container.onCreation(code, idHash, this.tree)
65 const metaData = Actor.serializeMetaData(type)
66
67 // save the container in the state
68 this.tree.set(idHash.id, metaData)
69 if (code) {
70 this.tree.set(Buffer.concat([idHash.id, Buffer.from([0])]), code)
71 }
72 return {
73 id: idHash,
74 module: module
75 }
76 }
77
78 // get a hash from a POJO
79 _getHashFromObj (obj) {
80 return this.tree.constructor.getMerkleLink(obj)
81 }
82
83 /**
84 * creates a state root starting from a given container and a given number of
85 * ticks
86 * @param {Number} ticks the number of ticks at which to create the state root
87 * @returns {Promise}
88 */
89 createStateRoot () {
90 return new Promise((resolve, reject) => {
91 this.scheduler.on('idle', () => {
92 this.tree.flush().then(resolve)
93 })
94 })
95 }
96
97 /**
98 * regirsters a container with the hypervisor
99 * @param {Class} Constructor - a Class for instantiating the container
100 * @param {*} args - any args that the contructor takes
101 * @param {Integer} typeId - the container's type identification ID
102 */
103 registerContainer (Constructor) {
104 this._containerTypes[Constructor.typeId] = Constructor
105 }
106}
107
108function encodedID (id) {
109 const nonce = Buffer.from([id.nonce])
110 if (id.parent) {
111 return Buffer.concat([nonce, id.parent.id])
112 } else {
113 return nonce
114 }
115}
116

Built with git-ssb-web