-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
337 lines (306 loc) · 7.04 KB
/
index.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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
const crypto = require('crypto')
const cbor = require('borc')
const EventEmitter = require('events')
const Buffer = require('safe-buffer').Buffer
const TAGS = {
id: 41,
link: 42,
func: 43,
mod: 44,
actor: 45
}
/**
* a cbor decoder for the objects
* @type {Object}
*/
const decoder = new cbor.Decoder({
tags: {
[TAGS.id]: val => new ID(val),
[TAGS.func]: val => new FunctionRef({
identifier: val[0],
params: val[1],
actorId: val[2],
gas: val[3]
}),
[TAGS.mod]: val => {
const code = val.pop()['/']
return new ModuleRef(...val, code)
},
[TAGS.actor]: val => new ActorRef(...val),
[TAGS.link]: val => {
return {
'/': val
}
}
},
size: 4194304 // set heap usage to 4MB
})
/**
* an ID
*/
class ID {
/*
* @param {Buffer} id - the id as a buffer
*/
constructor (id) {
this.id = id
}
toString () {
return this.id.toString('hex')
}
toJSON () {
return `0x${this.toString()}`
}
static fromJSON (data) {
return new ID(Buffer.from(data.slice(2), 'hex'))
}
encodeCBOR (gen) {
return gen.write(new cbor.Tagged(TAGS.id, this.id))
}
}
/**
* A function reference
*/
class FunctionRef {
/**
* @param {Object} opts
* @param {Array} opts.identifier - the function's identifier
* @param {Boolean} opts.identifier[0] - true if private function
* @param {String} opts.identifier[1] - name of exported function, or table index if private
* @param {ID} opts.actorId - the id of the actor
* @param {Array} opts.params - the params of the function
* @param {Number} opts.gas - gas amount
*/
constructor (opts) {
this.identifier = opts.identifier
this.actorId = opts.actorId
this.params = opts.params
this.gas = opts.gas || 0
}
encodeCBOR (gen) {
return gen.write(new cbor.Tagged(TAGS.func, [
this.identifier,
this.params,
this.actorId,
this.gas
]))
}
toJSON (verbose = true) {
const json = {
type: 'funcref',
actorId: this.actorId.toJSON(),
private: this.identifier ? this.identifier[0] : null,
name: this.identifier ? this.identifier[1] : null,
gas: this.gas
}
if (verbose) {
json.params = this.params
}
return json
}
static fromJSON (data) {
return new FunctionRef({
identifier: [data.private, data.name],
actorId: ID.fromJSON(data.actorId),
params: data.params,
gas: data.gas
})
}
/**
* Creates a copy of the funcRef
* @returns {FunctionRef}
*/
copy () {
return new FunctionRef({
identifier: this.identifier,
actorId: this.actorId,
params: this.params,
gas: this.gas
})
}
}
/**
* An actor reference
*/
class ActorRef {
/**
* @param {ID} id - the id of the actor
* @param {ModuleRef} modRef - the modRef of the actor
*/
constructor (id, modRef) {
this.modRef = modRef
this.id = id
}
/**
* return a function reference given the name of the function
* @param {string} name
* @returns {FunctionRef}
*/
getFuncRef (name) {
const params = this.modRef.exports[name]
if (!params) {
throw new Error(`function "${name}" not found`)
}
return new FunctionRef({
identifier: [false, name],
params,
actorId: this.id
})
}
toJSON (verbose = true) {
return {
type: 'actorref',
id: this.id.toJSON(),
modref: this.modRef.toJSON(verbose)
}
}
static fromJSON (data) {
return new ActorRef(ID.fromJSON(data.id), ModuleRef.fromJSON(data.modref))
}
encodeCBOR (gen) {
return gen.write(new cbor.Tagged(TAGS.actor, [this.id, this.modRef]))
}
}
/**
* A module reference
*/
class ModuleRef {
/**
* @param {ID} id - the id of the module
* @param {Number} type - type id of the module
* @param {Object} exports - a map of exported function to params for the function, if any
* @param {Object} persist - persist of the module
* @param {Buffer} code - code of the module
*/
constructor (id, type, exports, persist, code) {
this.id = id
this.type = type
this.exports = exports
this.persist = persist
this.code = {'/': code}
}
toJSON (verbose = true) {
let code = this.code['/']
? `0x${this.code['/'].toString('hex')}`
: null
if (code && !verbose) {
code = code.substring(0, 10) + '...'
}
return {
type: 'modref',
id: this.id.toJSON(),
modType: this.type,
code,
exports: this.exports,
persist: this.persist
}
}
static fromJSON (data) {
return new ModuleRef(
ID.fromJSON(data.id),
data.modType,
data.exports,
data.persist.map(p => p == null ? undefined : p),
Buffer.from(data.code.slice(2), 'hex')
)
}
encodeCBOR (gen) {
return gen.write(new cbor.Tagged(TAGS.mod, [this.id, this.type, this.exports, this.persist, this.code]))
}
}
/**
* This implements Messages for Primea
*/
class Message extends EventEmitter {
/**
* @param {Object} opts
* @param {ArrayBuffer} opts.data - the payload of the message
* @param {Array<Object>} opts.caps - an array of capabilities to send in the message
*/
constructor (opts) {
super()
const defaults = this.constructor.defaults
this._opts = Object.assign(defaults, opts)
Object.keys(this._opts).forEach(key => {
Object.defineProperty(this, key, {
get: function () {
return this._opts[key]
},
set: function (y) {
this._opts[key] = y
}
})
})
}
static get defaults () {
return {
ticks: 0,
funcRef: null,
funcArguments: [],
funcParameters: [],
_fromId: new ID(Buffer.alloc(20)),
_fromTicks: 0
}
}
}
/**
* returns the type that the object is
* @param {*} obj
* @return {String}
*/
function getType (obj) {
if (obj) {
if (Buffer.isBuffer(obj)) {
return 'data'
} else if (Array.isArray(obj)) {
return 'elem'
} else if (obj['/']) {
return 'link'
} else if (obj.constructor.name === 'Message') {
return 'message'
} else if (obj.constructor.name === 'ID') {
return 'id'
} else if (obj.constructor.name === 'FunctionRef') {
return 'func'
} else if (obj.constructor.name === 'ModuleRef') {
return 'mod'
} else if (obj.constructor.name === 'ActorRef') {
return 'actor'
}
}
return 'invalid'
}
/**
* returns the ID of an actor
* @param {Object} id
* @param {Number} id.nonce - the actor's nonce
* @param {ID} id.parent - the actor's parent's ID
* @return {ID}
*/
function generateId (id) {
const encoded = _encodeActorId(id)
const hashed = _hash(encoded)
return new ID(hashed)
}
function _encodeActorId (id) {
if (id.parent) {
return cbor.encode([id.nonce, id.parent.id])
} else {
return cbor.encode([id.nonce, null])
}
}
function _hash (buf) {
const hash = crypto.createHash('sha256')
hash.update(buf)
return hash.digest().slice(0, 20)
}
module.exports = {
Message,
ID,
FunctionRef,
ModuleRef,
ActorRef,
decoder,
getType,
generateId
}