-
Notifications
You must be signed in to change notification settings - Fork 8
/
lib.ts
464 lines (389 loc) · 12.5 KB
/
lib.ts
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
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
import BN = require('bn.js')
import Account from 'ethereumjs-account'
import { keccak256, ecsign, stripZeros } from 'ethereumjs-util'
import { encode, decode } from 'rlp'
import { Multiproof, verifyMultiproof, makeMultiproof, flatEncodeInstructions } from '../multiproof'
import VM from 'ethereumjs-vm'
import { Transaction } from 'ethereumjs-tx'
import { getOpcodesForHF } from 'ethereumjs-vm/dist/evm/opcodes'
const assert = require('assert')
const { promisify } = require('util')
const Wallet = require('ethereumjs-wallet')
const Trie = require('merkle-patricia-tree/secure')
export interface TestSuite {
preStateRoot: Buffer
blockData: Buffer
postStateRoot: Buffer
}
export interface RunnerArgs {
stateless: boolean
fork: string
test: string
scout: string
dist: string
forkConfig: string
jsontrace: boolean
debug: boolean
data: string
gasLimit: number
value: number
}
export interface TestGetterArgs {
test: string
}
export interface SimulationData {
from: Buffer
to: Buffer
value: BN
nonce: BN
}
export interface AccountInfo {
address: Buffer
privateKey: Buffer
account: Account
}
export async function generateTestSuite(): Promise<TestSuite> {
const trie = new Trie()
// Generate random accounts
const accounts = await generateAccounts(trie, 5000)
const preStateRoot = trie.root
// Generate txes
const [txes, addrs, multiproof, simulationData] = await generateTxes(trie, accounts, 70)
// Serialize witnesses and tx data
const blockData = encode([txes, addrs, ...rawMultiproof(multiproof as Multiproof, true)])
// Apply txes on top of trie to compute post state root
for (const tx of simulationData as SimulationData[]) {
await transfer(trie, tx)
}
return {
preStateRoot,
blockData,
postStateRoot: trie.root,
}
}
async function generateTxes(trie: any, accounts: AccountInfo[], count = 50) {
const txes = []
const simulationData = []
const root = trie.root
const toProve: any = {}
for (let i = 0; i < count; i++) {
const from = accounts[i].address
const to = accounts[i + 1].address
const value = new BN('00000000000000000000000000000000000000000000000000000000000000ff', 16)
const nonce = new BN('0000000000000000000000000000000000000000000000000000000000000000', 16)
simulationData.push({ from, to, value, nonce })
const fromKey = from.toString('hex')
if (!toProve[fromKey]) {
toProve[fromKey] = []
}
toProve[fromKey].push({ txId: i, fieldIdx: 3 })
const toKey = to.toString('hex')
if (!toProve[toKey]) {
toProve[toKey] = []
}
toProve[toKey].push({ txId: i, fieldIdx: 0 })
const txRlp = encode([
to,
stripZeros(value.toBuffer('be', 32)),
stripZeros(nonce.toBuffer('be', 32)),
])
const txHash = keccak256(txRlp)
const txSig = ecsign(txHash, accounts[i].privateKey)
assert(txSig.r.byteLength === 32)
assert(txSig.s.byteLength === 32)
assert(txSig.v < 256)
txes.push([
to,
stripZeros(value.toBuffer('be', 32)),
stripZeros(nonce.toBuffer('be', 32)),
from,
])
}
// Make sure keys are unique and sort them
const unsortedAddrs = Object.keys(toProve).map(s => Buffer.from(s, 'hex'))
const keys = unsortedAddrs.map(a => keccak256(a))
keys.sort(Buffer.compare)
const sortedAddrs = sortAddrsByHash(unsortedAddrs)
const proof = await makeMultiproof(trie, keys)
// Verify proof is valid
assert(verifyMultiproof(root, proof, keys))
// Modify txes and replace from and to addresses
// with their index in the keys array
for (let i = 0; i < sortedAddrs.length; i++) {
const addr = sortedAddrs[i]
const addrData = toProve[addr.toString('hex')]
for (const instance of addrData) {
txes[instance.txId][instance.fieldIdx] = i
}
}
return [txes, sortedAddrs, proof, simulationData]
}
export async function transfer(trie: any, tx: SimulationData) {
const { from, to, value, nonce } = tx
assert(value.gten(0))
const fromAcc = await getAccount(trie, from)
const toAcc = await getAccount(trie, to)
assert(new BN(fromAcc.balance).gte(value))
assert(new BN(fromAcc.nonce).eq(nonce))
const newFromBalance = new BN(fromAcc.balance).sub(value)
fromAcc.balance = newFromBalance.toBuffer()
fromAcc.nonce = nonce.addn(1).toBuffer()
const newToBalance = new BN(toAcc.balance).add(value)
toAcc.balance = newToBalance.toBuffer()
await putAccount(trie, from, fromAcc)
await putAccount(trie, to, toAcc)
}
// Sort addresses based on their hashes.
// Naive algorithm
export function sortAddrsByHash(addrs: Buffer[]): Buffer[] {
const keys = addrs.map((a: Buffer) => keccak256(a))
keys.sort(Buffer.compare)
const sortedAddrs = new Array(keys.length).fill(undefined)
for (const a of addrs) {
let idx = -1
const h = keccak256(a)
for (let i = 0; i < keys.length; i++) {
const k = keys[i]
if (h.equals(k)) {
idx = i
}
}
assert(idx >= 0)
sortedAddrs[idx] = a
}
return sortedAddrs
}
export async function generateAccounts(trie: any, count = 500): Promise<AccountInfo[]> {
const accounts = []
for (let i = 0; i < count; i++) {
const wallet = Wallet.generate()
const address = wallet.getAddress()
const privateKey = wallet.getPrivateKey()
const account = new Account()
account.balance = new BN('ffffff', 16).toBuffer()
accounts.push({
address,
privateKey,
account,
})
await putAccount(trie, address, account)
}
return accounts
}
export async function stateTestRunner(runnerArgs: RunnerArgs, test: any): Promise<TestSuite> {
const trie = new Trie()
const [accounts, codeHashes, bytecode] = await getTestsAccounts(trie, test)
const preStateRoot = trie.root
const [txes, addrs, multiproof, simulationData, pks] = await getTestsTxes(trie, accounts, test)
const blockData = encode([
txes,
addrs,
...rawMultiproof(multiproof as Multiproof, true),
codeHashes,
bytecode,
])
// Execute txes on top of trie to compute post state root
let i = 0
for (const tx of simulationData as SimulationData[]) {
const pk = (pks as Buffer[])[0]
await execute(runnerArgs, trie, tx, pk)
i = i + 1
}
return {
preStateRoot,
blockData,
postStateRoot: trie.root,
}
}
export async function getTestsTxes(trie: any, accounts: AccountInfo[], test: any) {
const txes = []
const pks = []
const simulationData = []
const root = trie.root
const toProve: any = {}
const from = accounts[1].address
const to = accounts[0].address
const value = new BN(test.transaction.value[0].substring(2), 16)
const nonce = new BN(test.pre['0x' + from.toString('hex')].nonce.substring(2), 16)
simulationData.push({ from, to, value, nonce })
const fromKey = from.toString('hex')
if (!toProve[fromKey]) {
toProve[fromKey] = []
}
toProve[fromKey].push({ txId: 0, fieldIdx: 3 })
const toKey = to.toString('hex')
if (!toProve[toKey]) {
toProve[toKey] = []
}
toProve[toKey].push({ txId: 0, fieldIdx: 0 })
txes.push([to, stripZeros(value.toBuffer('be', 32)), stripZeros(nonce.toBuffer('be', 32)), from])
pks.push(accounts[1].privateKey)
// Make sure keys are unique and sort them
const unsortedAddrs = Object.keys(toProve).map(s => Buffer.from(s, 'hex'))
const keys = unsortedAddrs.map(a => keccak256(a))
keys.sort(Buffer.compare)
const sortedAddrs = sortAddrsByHash(unsortedAddrs)
const proof = await makeMultiproof(trie, keys)
// Verify proof is valid
assert(verifyMultiproof(root, proof, keys))
// Modify txes and replace from and to addresses
// with their index in the keys array
for (let i = 0; i < sortedAddrs.length; i++) {
const addr = sortedAddrs[i]
const addrData = toProve[addr.toString('hex')]
for (const instance of addrData) {
txes[instance.txId][instance.fieldIdx] = i
}
}
return [txes, sortedAddrs, proof, simulationData, pks]
}
async function execute(options: any, trie: any, tx: SimulationData, pk: any) {
const rawTx = {
nonce: '0x' + tx.nonce.toString('hex'),
gasLimit: '0x61a80',
gasPrice: '0x1',
value: '0x' + tx.value.toString('hex'),
from: '0x' + tx.from.toString('hex'),
to: '0x' + tx.to.toString('hex'),
}
const vm = new VM({
state: trie,
hardfork: options.forkConfig.toLowerCase(),
})
await runTx(vm, rawTx, pk)
}
async function runTx(vm: any, rawTx: any, pk: any) {
const tx = new Transaction(rawTx)
tx.sign(pk)
const results = await vm.runTx({
tx: tx,
})
return results
}
export async function getTestsAccounts(
trie: any,
test: any,
): Promise<[AccountInfo[], Buffer[], Buffer[]]> {
const accounts: AccountInfo[] = []
const codeHashes: Buffer[] = []
const bytecode: Buffer[] = []
const privateKey = test.transaction.secretKey
for (const address in test.pre) {
const acct = test.pre[address]
const code = Buffer.from(acct.code.substring(2), 'hex')
const codeHash = keccak256(code)
const acct_data = {
nonce: acct.nonce,
balance: acct.balance,
codeHash: codeHash,
}
const account = new Account(acct_data)
const addr_buf = Buffer.from(address.substring(2), 'hex')
accounts.push({
address: addr_buf,
privateKey: Buffer.from(privateKey.substring(2), 'hex'),
account: account,
})
await putAccount(trie, addr_buf, account)
await new Promise((resolve, reject) => {
account.setCode(trie, code, (err: any, codeHash: Buffer) => {
if (err) {
return reject(err)
}
codeHashes.push(codeHash)
bytecode.push(code)
resolve(codeHash)
})
})
}
return [accounts, codeHashes, bytecode]
}
async function putAccount(trie: any, address: Buffer, account: Account) {
await promisify(trie.put.bind(trie))(address, account.serialize())
}
async function getAccount(trie: any, address: Buffer): Promise<Account> {
const raw = await promisify(trie.get.bind(trie))(address)
if (!raw) {
return new Account()
} else {
return new Account(raw)
}
}
export function rawMultiproof(proof: Multiproof, flatInstructions: boolean = false): any {
const keys = []
const values = []
for (const kv of proof.keyvals) {
const raw = decode(kv)
keys.push(raw[0])
values.push(raw[1])
}
if (flatInstructions) {
return [proof.hashes, keys, values, flatEncodeInstructions(proof.instructions)]
} else {
return [
proof.hashes,
keys,
values,
proof.instructions.map(i => {
if (i.value !== undefined) return [i.kind, i.value]
return [i.kind]
}),
]
}
}
export function getBasicBlockIndices(code: Buffer): number[][] {
const TERMINATING_OPS = ['JUMP', 'JUMPI', 'STOP', 'RETURN', 'REVERT', 'SELFDESTRUCT']
const opcodes = getOpcodesForHF('istanbul')
const getOp = (i: number) => (opcodes[code[i]] ? opcodes[code[i]].name : 'INVALID')
// [start, end) indices
const blocks = [[0, -1]]
for (let i = 0; i < code.length; i++) {
const op = getOp(i)
// Skip push args
if (op === 'PUSH') {
i += code[i] - 0x5f
}
// Current instruction terminates block or next instruction is JUMPDEST
if (TERMINATING_OPS.includes(op) || (i + 1 < code.length && getOp(i + 1) === 'JUMPDEST')) {
blocks[blocks.length - 1][1] = i + 1
// Create new block if not at end of code
if (i + 1 < code.length) {
blocks.push([i + 1, -1])
}
}
}
// Close block if no terminating instruction at the end
if (blocks[blocks.length - 1][1] === undefined) {
blocks[blocks.length - 1][1] = code.length
}
return blocks
}
/**
* Does a single pass over bytecode to find the list
* of all basic blocks (i.e. blocks of code with no
* control flow change).
*/
export function getBasicBlocks(code: Buffer): Buffer[] {
const blocks = getBasicBlockIndices(code)
// Slice code based on block indices
return blocks.map((b: number[]) => code.slice(b[0], b[1]))
}
/**
* Divides code into basic blocks and constructs a MPT
* with these blocks as leaves. The key for each block is
* the index of the first byte of that block in the bytecode.
*/
export async function merkelizeCode(code: Buffer): Promise<any> {
const blockIndices = getBasicBlockIndices(code)
const trie = new Trie()
const putP = promisify(trie.put.bind(trie))
// Keys are indices into the bytecode. Determine key length by
// how large the last index is.
const keyLength = new BN(code.length - 1).byteLength()
for (let i = 0; i < blockIndices.length; i++) {
const key = new BN(blockIndices[i][0]).toBuffer('be', keyLength)
const val = code.slice(blockIndices[i][0], blockIndices[i][1])
await putP(key, val)
}
return trie
}