-
Notifications
You must be signed in to change notification settings - Fork 8
/
actor.js
130 lines (117 loc) · 3.2 KB
/
actor.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
const errors = require('./errors.json')
const nope = () => {}
const debug = require('debug')('lifecycle:error')
module.exports = class Actor {
/**
* the Actor manages the various message passing functions and provides
* an interface for the containers to use
* @param {Object} opts
* @param {ID} opts.id - the UUID of the Actor
* @param {Object} opts.module - the module this actor was created from
* @param {Object} opts.state - the state of the module
* @param {Object} opts.storage - the actor's persistent storage
* @param {Object} opts.hypervisor - the instance of the hypervisor
* @param {Number} opts.nonce
* @param {Function} opts.Container - the module constructor and arguments
*/
constructor (opts) {
Object.assign(this, opts)
this.ticks = 0
this.running = false
this.container = new this.Container(this)
if (!this.hypervisor.meter) {
this.incrementTicks = nope
}
}
/**
* Runs the shutdown routine for the actor
*/
shutdown () {
// saves the nonce and storage to the state
this.state.value[1] = this.nonce
this.state.node[2] = {'/': this.storage}
}
/**
* Runs the startup routine for the actor
*/
startup () {
return this.container.onStartup()
}
/**
* run the Actor with a given message
* @param {object} message - the message to run
* @param {String} method - which method to run
* @returns {Promise}
*/
async runMessage (message) {
if (message._fromTicks > this.ticks) {
this.ticks = message._fromTicks
}
try {
this.currentMessage = message
await this.container.onMessage(message)
} catch (e) {
debug(e)
message.emit('execution:error', e)
}
message.emit('done', this)
}
/**
* updates the number of ticks that the actor has run
* @param {Number} count - the number of ticks to add
*/
incrementTicks (count) {
this.currentMessage.funcRef.gas -= count
if (this.currentMessage.funcRef.gas < 0) {
throw new Error(errors.OUT_OF_GAS)
}
this.ticks += count
}
/**
* creates an actor from a module and code
* @param {Module} mod - the module
* @param {Buffer} code - the code
* @return {ActorRef}
*/
newActor (mod, code) {
const modRef = this.createModule(mod, code)
return this.createActor(modRef)
}
/**
* creates a modref from a module and code
* @param {Module} mod - the module
* @param {Buffer} code - the code
* @return {ModuleRef}
*/
createModule (mod, code) {
const id = this._generateNextId()
return this.hypervisor.createModule(mod, code, id)
}
/**
* creates an actor from a modref
* @param {ModuleRef} modRef - the modref
* @return {ActorRef}
*/
createActor (modRef) {
const id = this._generateNextId()
return this.hypervisor.createActor(modRef, id)
}
_generateNextId () {
const id = {
nonce: this.nonce,
parent: this.id
}
this.nonce++
return id
}
/**
* sends a message
* @param {Message} message - the message
*/
send (message) {
message._fromTicks = this.ticks
message._fromId = this.id
this.incrementTicks(message.funcRef.gas)
this.hypervisor.scheduler.queue([message])
}
}