-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrun.js
275 lines (220 loc) · 9.17 KB
/
run.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
// Standard imports
const fs = require("fs");
const loader = require(__dirname + "/node_modules/assemblyscript/lib/loader");
const fetch = require("node-fetch");
// Read & re-compile the WASM module
const compiled = new WebAssembly.Module(fs.readFileSync(__dirname + "/build/untouched.wasm"));
// Initialize the imports that the WASM module expects: memory, table, and any JS functions
// For importing memory and table, use the "--importMemory" and "--ImportTable" flags with asc (AssemblyScript compiler)
const wasm_memory = new WebAssembly.Memory({initial: 1});
const wasm_table = new WebAssembly.Table({initial: 50, element:"anyfunc"});
const imports = {
env: {
abort(_msg, _file, line, column) {
console.error("abort called at index.ts:" + line + ":" + column);
},
memory: wasm_memory,
table: wasm_table
}
};
// Instantiate the WASM module
const instance = loader.instantiateSync(compiled, imports);
const SP_SERVER = "http://localhost:3000";
/*-------------------------------------------------------------------------------------------------------------*/
function list2wasmArray(list, wasmArrayView) {
for (let i=0; i<list.length; i++) {
wasmArrayView[i] = list[i];
}
}
function array2list(array) {
let l = [];
for (let i=0; i<array.length; i++) {
l[l.length] = array[i];
}
return l;
}
class State {
/*
State contains the following:
- state_root: Int32Array(1)
This is the hash of the ee_state array
- ee_state: Int32Array(5)
This EE maintains the balances of 5 accounts, which are stored in this array
*/
constructor(state_root, ee_state) {
this.state_root = state_root;
this.ee_state = ee_state;
}
}
class WASMState extends State {
constructor(instance, ptr) {
let memory_array = new Int32Array(instance.memory.buffer);
// Fetch the object's internal variables using memory offsets from the base pointer
// https://github.com/AssemblyScript/assemblyscript/tree/master/lib/loader
// state_root is WASM Array<u32>(1) / JS Int32Array(1)
let state_root = instance.__getArrayView(memory_array[ptr + instance.STATE_STATE_ROOT_OFFSET >>> 2]);
// ee_state is Array<u32>(5) / JS Int32Array(5)
let ee_state = instance.__getArrayView(memory_array[ptr + instance.STATE_EE_STATE_OFFSET >>> 2]);
super(state_root, ee_state);
// Store the WASM pointer to the WASM state object
this.ptr = ptr;
}
}
/*-------------------------------------------------------------------------------------------------------------*/
console.log("****** DEMO 1: Local WASM EE Execution ******");
console.log("");
let pre_state_ptr = instance.getPreState();
let pre_state = new WASMState(instance, pre_state_ptr);
let post_state_ptr = instance.getPostState();
let post_state = new WASMState(instance, post_state_ptr);
let working_state_ptr = instance.getWorkingState();
let working_state = new WASMState(instance, working_state_ptr);
console.log("[*] Writing pre_state into WASM memory")
for (let i=0; i<5; i++) {
pre_state.ee_state[i] = i;
}
instance.updateRoot(pre_state.ptr);
console.log("\tpre_state.ee_state:", pre_state.ee_state);
console.log("\tpre_state.state_root:", pre_state.state_root[0].toString(16));
console.log("");
console.log("[*] Calling WASM setUp() function")
instance.setUp(working_state.ptr);
console.log("\tworking_state.ee_state:", working_state.ee_state);
console.log("\tworking_state.state_root:", working_state.state_root[0].toString(16));
console.log("");
console.log("[*] Executing transaction `transfer(3, 0, 2)`");
instance.transfer(3, 0, 2);
console.log("\tworking_state.ee_state:", working_state.ee_state);
console.log("\tworking_state.state_root:", working_state.state_root[0].toString(16));
console.log("");
console.log("[*] Calling WASM cleanUp() function")
instance.cleanUp(working_state.ptr);
console.log("\tpost_state.ee_state:", post_state.ee_state);
console.log("\tpost_state.state_root:", post_state.state_root[0].toString(16));
console.log("");
console.log("");
/*-------------------------------------------------------------------------------------------------------------*/
function verifyBlock(instance, block) {
let pre_state_ptr = instance.getPreState();
let pre_state = new WASMState(instance, pre_state_ptr);
list2wasmArray(block['pre']['ee_state'], pre_state.ee_state);
list2wasmArray(block['pre']['state_root'], pre_state.state_root);
// console.log("pre_state.ee_state:", pre_state.ee_state);
// console.log("pre_state.state_root:", pre_state.state_root[0].toString(16));
instance.updateRoot(pre_state.ptr);
instance.setUp();
for (let i=0; i<block['transactions'].length; i++) {
let tx = block['transactions'][i];
console.log("tx:", tx);
instance.transfer(tx[0], tx[1], tx[2]);
}
instance.cleanUp();
let post_state_ptr = instance.getPostState();
let post_state = new WASMState(instance, post_state_ptr);
// console.log("post_state.ee_state:", post_state.ee_state);
// console.log("post_state.state_root:", post_state.state_root[0].toString(16));
return post_state.state_root==parseInt(block['post']['state_root'], 16);
}
function createBlock(instance, pre_state_tx_input) {
let pre_state_ptr = instance.getPreState();
let pre_state = new WASMState(instance, pre_state_ptr);
list2wasmArray(pre_state_tx_input['pre']['ee_state'], pre_state.ee_state);
list2wasmArray(pre_state_tx_input['pre']['state_root'], pre_state.state_root);
// console.log("pre_state.ee_state:", pre_state.ee_state);
// console.log("pre_state.state_root:", pre_state.state_root[0].toString(16));
instance.updateRoot(pre_state.ptr);
instance.setUp();
for (let i=0; i<pre_state_tx_input['transactions'].length; i++) {
let tx = pre_state_tx_input['transactions'][i];
// console.log("tx:", tx);
instance.transfer(tx[0], tx[1], tx[2]);
}
instance.cleanUp();
let post_state_ptr = instance.getPostState();
let post_state = new WASMState(instance, post_state_ptr);
// console.log("post_state.ee_state:", post_state.ee_state);
// console.log("post_state.state_root:", post_state.state_root[0].toString(16));
let block = {
'pre': pre_state_tx_input['pre'],
'transactions': pre_state_tx_input['transactions'],
'post': {
'ee_state': array2list(post_state.ee_state),
'state_root': post_state.state_root[0].toString(16)
}
};
return block;
}
function createGenesisBlock(instance, pre_state_input) {
let pre_state_ptr = instance.getPreState();
let pre_state = new WASMState(instance, pre_state_ptr);
list2wasmArray(pre_state_input['pre']['ee_state'], pre_state.ee_state);
// console.log("pre_state.ee_state:", pre_state.ee_state);
// console.log("pre_state.state_root:", pre_state.state_root[0].toString(16));
instance.updateRoot(pre_state.ptr);
instance.setUp();
instance.cleanUp();
let post_state_ptr = instance.getPostState();
let post_state = new WASMState(instance, post_state_ptr);
// console.log("post_state.ee_state:", post_state.ee_state);
// console.log("post_state.state_root:", post_state.state_root[0].toString(16));
let block = {
'pre': pre_state_input['pre'],
'post': {
'ee_state': array2list(post_state.ee_state),
'state_root': post_state.state_root[0].toString(16)
}
};
return block;
}
let genesis_state = { pre: { ee_state: [ 100, 42, 26, 18, 230 ] } };
let genesis_block = createGenesisBlock(instance, genesis_state);
// console.log("Genesis Block:");
// console.log(genesis_block);
// console.log("");
function printChain(chaindata) {
for (let i=0; i<chaindata.length; i++) {
console.log("Block", i);
console.log(chaindata[i]);
}
}
async function postGenesisBlock() {
console.log("");
console.log("[*] Posting the genesis block to the SP")
let response = await fetch(SP_SERVER+"/updateLatestBlock", {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify(genesis_block)
});
console.log("The latest chain is updated to:");
printChain(JSON.parse(await response.json()));
}
async function getLatestBlock() {
console.log("[*] Fetching the latest block from the SP")
let latest_block = await fetch(SP_SERVER+"/getLatestBlock", {
method: 'GET'
});
console.log("The latest block is:");
let latest_block_data = await latest_block.json();
console.log(latest_block_data);
return latest_block_data;
}
async function makeNextBlock(txs) {
let latest_block = await getLatestBlock();
console.log("");
console.log("[*] Posting the next block to the SP")
// console.log(await latest_block);
let next_block_input = {'pre': latest_block['post'], 'transactions': txs};
let next_block = createBlock(instance, next_block_input);
// console.log("next_block:", next_block);
let current_chain = await fetch(SP_SERVER+"/updateLatestBlock", {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify(next_block)
});
console.log("The latest chain is updated to:");
printChain(JSON.parse(await current_chain.json()));
}
/*-------------------------------------------------------------------------------------------------------------*/
console.log("****** DEMO 2: Executing & Verifying Blocks From SP ******");
console.log("");
postGenesisBlock().then(() => makeNextBlock([[3, 0 ,1]])).then(() => makeNextBlock([[1, 4 ,18]]));