-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathindex.js
More file actions
191 lines (163 loc) · 5.33 KB
/
Copy pathindex.js
File metadata and controls
191 lines (163 loc) · 5.33 KB
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
const cbor = require('borc')
const Hypervisor = require('primea-hypervisor')
const EgressDriver = require('primea-hypervisor/egressDriver')
const { ID, Message, decoder: objectDecoder } = require('primea-objects')
const WasmContainer = require('primea-wasm-container')
const level = require('level-browserify')
const RadixTree = require('dfinity-radix-tree')
const RemoteDataStore = require('dfinity-radix-tree/remoteDatastore')
const DfinityTx = require('dfinity-tx')
class TestWasmContainer extends WasmContainer {
constructor (actor) {
super(actor)
this._storage = new Map()
const self = this
const inter = {
test: {
printStr: (ref) => console.log('test.printStr:', self.refs.get(ref, 'data').toString()),
printData: (ref) => console.log('test.printData:', self.refs.get(ref, 'data')),
printAny: (ref) => console.log('test.printAny:', self.refs.get(ref, 'anyref')),
printFunc: (ref) => console.log('test.printFunc:', self.refs.get(ref, 'func')),
printModule: (ref) => console.log('test.printModule:', self.refs.get(ref, 'mod')),
printActor: (ref) => console.log('test.printActor:', self.refs.get(ref, 'actor')),
print: (val) => console.log('test.print:', val)
},
env: {
abort: () => {
console.log('abort!')
}
}
}
this.interface = Object.assign(this.interface, inter)
}
}
const decoder = new cbor.Decoder({
tags: Object.assign(objectDecoder._knownTags, DfinityTx.getDecoder()._knownTags)
})
const IO_ACTOR_ID = Buffer.from([])
module.exports = class PrimeaClient {
constructor(opts={}) {
const defaults = this.constructor.defaults
this._opts = Object.assign(defaults, opts)
const db = level(this._opts.dbPath)
const rootHash = this._opts.rootHash
const treeOpts = {
root: rootHash
}
if (this._opts.remoteURI) {
treeOpts.dag = new RemoteDataStore(db, { uri: this._opts.remoteURI })
console.log('new RemoteDataStore @', this._opts.remoteURI)
} else {
treeOpts.db = db
}
const tree = new RadixTree(treeOpts)
this.logger = new EgressDriver()
this.hypervisor = new Hypervisor({
tree,
meter: this._opts.meter,
modules: this._opts.modules,
defaultDriver: this.logger,
onCreate: this._opts.onCreate
})
}
resetDatastore() {
return this.setStateRoot(RadixTree.emptyTreeState)
}
async ingress (raw) {
const [ tx, pk, sig ] = decoder.decodeFirst(raw)
const args = tx.args.map(arg => {
if (typeof arg === 'object' && arg.constructor.name === 'Tagged') {
return decoder.decodeFirst(cbor.encode(arg))
}
return arg
})
let id, actor, funcRef
if (typeof tx.funcName == 'object' && tx.funcName.constructor && tx.funcName.constructor.name == 'FunctionRef') {
funcRef = tx.funcName
actor = funcRef.actorId
} else if (Buffer.isBuffer(tx.actorId) && IO_ACTOR_ID.equals(tx.actorId)) {
actor = await this.hypervisor.newActor(this._opts.modules[0], args.shift())
if (tx.funcName) {
funcRef = actor.getFuncRef(tx.funcName)
}
} else if (typeof tx.funcName == 'string') {
id = this._getId(tx.actorId)
const _actor = await this.hypervisor.loadActor(id)
actor = _actor.container.actorSelf
funcRef = actor.getFuncRef(tx.funcName)
}
// cast Number to i64
if (funcRef && funcRef.params && funcRef.params.includes('i64') && args.length <= funcRef.params.length) {
let argsIndex = 0
for (let i = 0; i < funcRef.params.length; i++) {
if (funcRef.params[i] === 'i64') {
args.splice(argsIndex, 0, 0)
argsIndex++
}
argsIndex++
}
}
if (funcRef) {
funcRef.gas = tx.ticks
this.hypervisor.send(new Message({
funcRef,
funcArguments: args
}))
}
return cbor.encode(actor)
}
async getActor (id) {
id = this._getId(id)
const actor = await this.hypervisor.loadActor(id)
const actorRef = actor.container.actorSelf
return cbor.encode(actorRef)
}
async getLink (link) {
const res = await this.hypervisor.tree.graph._dag.get(link)
return cbor.encode(res)
}
async getNonce (id) {
id = this._getId(id)
const node = await this.hypervisor.tree.get(id.id)
const res = node.value[1]
return cbor.encode(res)
}
async getCode (id) {
id = this._getId(id)
const node = await this.hypervisor.tree.get(id.id)
const module = await this.hypervisor.tree.graph.get(node.node, '1')
await this.hypervisor.tree.graph.get(module[1], '')
return cbor.encode(module[1]['/'])
}
async getStorage (id) {
id = this._getId(id)
const node = await this.hypervisor.tree.get(id.id)
const res = await this.hypervisor.tree.graph.get(node.node, '2')
return cbor.encode(res)
}
async getStateRoot () {
const res = await this.hypervisor.createStateRoot()
return res
}
setStateRoot (root) {
return this.hypervisor.setStateRoot(root)
}
_getId (id) {
if (typeof id == 'object' && id.constructor && id.constructor.name === 'ID') {
return id
}
try {
return decoder.decodeFirst(id)
} catch (e) {
return new ID(id)
}
}
static get defaults () {
return {
dbPath: './testdb',
rootHash: 0,
modules: [TestWasmContainer],
meter: true
}
}
}