git ssb

0+

wanderer🌟 / js-primea-hypervisor



Tree: 1ffd915b15f2f2a528bc6e5329be0be2c9cab33b

Files: 1ffd915b15f2f2a528bc6e5329be0be2c9cab33b / index.js

3334 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(this)
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 send (messages) {
24 if (!Array.isArray(messages)) {
25 messages = [messages]
26 }
27 this.scheduler.queue(messages)
28 }
29
30 async loadActor (id) {
31 const state = await this.tree.getSubTree(id)
32 const code = state.get(Buffer.from([0]))
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 code,
45 cachedb: this.tree.dag._dag
46 })
47
48 await actor.startup()
49 return actor
50 }
51
52 /**
53 * creates an instance of an Actor
54 * @param {Integer} type - the type id for the container
55 * @param {Object} message - an intial [message](https://github.com/primea/js-primea-message) to send newly created actor
56 * @param {Object} id - the id for the actor
57 */
58 async createActor (type, code, id = {nonce: this.nonce++, parent: null}) {
59 const Container = this._containerTypes[type]
60 const encoded = encodedID(id)
61 const idHash = await this._getHashFromObj(encoded)
62 const module = await Container.onCreation(code, idHash, this.tree.dag._dag)
63 const metaData = Actor.serializeMetaData(type)
64
65 // save the container in the state
66 this.tree.set(idHash, metaData)
67 if (code) {
68 this.tree.set(Buffer.concat([idHash, Buffer.from([0])]), code)
69 }
70 return {
71 id: idHash,
72 module: module
73 }
74 }
75
76 // get a hash from a POJO
77 _getHashFromObj (obj) {
78 return this.tree.constructor.getMerkleLink(obj)
79 }
80
81 /**
82 * creates a state root starting from a given container and a given number of
83 * ticks
84 * @param {Number} ticks the number of ticks at which to create the state root
85 * @returns {Promise}
86 */
87 createStateRoot () {
88 return new Promise((resolve, reject) => {
89 this.scheduler.on('idle', () => {
90 this.tree.flush().then(resolve)
91 })
92 })
93 }
94
95 /**
96 * regirsters a container with the hypervisor
97 * @param {Class} Constructor - a Class for instantiating the container
98 * @param {*} args - any args that the contructor takes
99 * @param {Integer} typeId - the container's type identification ID
100 */
101 registerContainer (Constructor) {
102 this._containerTypes[Constructor.typeId] = Constructor
103 }
104}
105
106function encodedID (id) {
107 const nonce = Buffer.from([id.nonce])
108 if (id.parent) {
109 return Buffer.concat([nonce, id.parent])
110 } else {
111 return nonce
112 }
113}
114

Built with git-ssb-web