git ssb

0+

wanderer🌟 / js-primea-hypervisor



Tree: 22ee95842d46a4de2a91bcec0f067a6efaf315fb

Files: 22ee95842d46a4de2a91bcec0f067a6efaf315fb / index.js

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

Built with git-ssb-web