git ssb

0+

wanderer🌟 / js-primea-hypervisor



Tree: 1ee5f08dcf25af27f0dce6fdc1eab39b185d202f

Files: 1ee5f08dcf25af27f0dce6fdc1eab39b185d202f / index.js

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

Built with git-ssb-web