|
| 1 | +import BN = require('bn.js') |
| 2 | +import { keccak256, stripZeros } from 'ethereumjs-util' |
| 3 | +import { encode } from 'rlp' |
| 4 | +import { Multiproof, makeMultiproof } from '../multiproof' |
| 5 | +import { TestSuite, sortAddrsByHash, rawMultiproof, SimulationData, transfer } from './lib' |
| 6 | +const { promisify } = require('util') |
| 7 | +const Trie = require('merkle-patricia-tree/secure') |
| 8 | + |
| 9 | +export interface AccountData { |
| 10 | + address: Buffer |
| 11 | + accountProof: Buffer[] |
| 12 | + nonce: BN |
| 13 | + balance: BN |
| 14 | + codeHash: Buffer |
| 15 | + storageHash: Buffer |
| 16 | + storageProof: Buffer[] |
| 17 | +} |
| 18 | + |
| 19 | +export interface TransactionData { |
| 20 | + to: Buffer |
| 21 | + value: BN |
| 22 | + nonce: BN |
| 23 | + from: Buffer |
| 24 | +} |
| 25 | + |
| 26 | +export async function generateRealisticTestSuite(data: any): Promise<TestSuite> { |
| 27 | + const trie = new Trie() |
| 28 | + |
| 29 | + const accData = [] |
| 30 | + const addrs = [] |
| 31 | + for (const acc of data.accounts) { |
| 32 | + accData.push(accountDataFromJSON(acc.result)) |
| 33 | + addrs.push(accData[accData.length - 1].address) |
| 34 | + } |
| 35 | + |
| 36 | + const multiproof = await turboproofFromAccountData(trie, accData) |
| 37 | + const preStateRoot = trie.root |
| 38 | + |
| 39 | + const sortedAddrs = sortAddrsByHash(addrs) |
| 40 | + const txes = [] |
| 41 | + const simulationData = [] |
| 42 | + for (const rawTx of data.transactions) { |
| 43 | + const rawTxData = rawTx.result ? rawTx.result : rawTx |
| 44 | + const txData = transactionDataFromJSON(rawTxData) |
| 45 | + simulationData.push({ |
| 46 | + to: txData.to, |
| 47 | + value: txData.value, |
| 48 | + nonce: txData.nonce, |
| 49 | + from: txData.from, |
| 50 | + }) |
| 51 | + const toIdx = sortedAddrs.findIndex((a: Buffer) => a.equals(txData.to)) |
| 52 | + const fromIdx = sortedAddrs.findIndex((a: Buffer) => a.equals(txData.from)) |
| 53 | + if (toIdx === -1 || fromIdx === -1) { |
| 54 | + throw new Error('Invalid transaction sender/recipient') |
| 55 | + } |
| 56 | + txes.push([ |
| 57 | + toIdx, |
| 58 | + stripZeros(txData.value.toBuffer('be', 32)), |
| 59 | + stripZeros(txData.nonce.toBuffer('be', 32)), |
| 60 | + fromIdx, |
| 61 | + ]) |
| 62 | + } |
| 63 | + |
| 64 | + const blockData = encode([txes, sortedAddrs, ...rawMultiproof(multiproof, true)]) |
| 65 | + |
| 66 | + // Apply txes on top of trie to compute post state root |
| 67 | + for (const tx of simulationData as SimulationData[]) { |
| 68 | + await transfer(trie, tx) |
| 69 | + } |
| 70 | + |
| 71 | + return { |
| 72 | + preStateRoot, |
| 73 | + blockData, |
| 74 | + postStateRoot: trie.root, |
| 75 | + } |
| 76 | +} |
| 77 | + |
| 78 | +export async function turboproofFromAccountData( |
| 79 | + trie: any, |
| 80 | + data: AccountData[], |
| 81 | +): Promise<Multiproof> { |
| 82 | + const putRaw = promisify(trie._putRaw.bind(trie)) |
| 83 | + const addrs = [] |
| 84 | + const preStateRoot = keccak256(data[0].accountProof[0]) |
| 85 | + trie.root = preStateRoot |
| 86 | + for (const accountData of data) { |
| 87 | + addrs.push(accountData.address) |
| 88 | + for (const node of accountData.accountProof) { |
| 89 | + await putRaw(keccak256(node), node) |
| 90 | + } |
| 91 | + } |
| 92 | + const keys = addrs.map((a: Buffer) => keccak256(a)) |
| 93 | + keys.sort(Buffer.compare) |
| 94 | + |
| 95 | + return makeMultiproof(trie, keys) |
| 96 | +} |
| 97 | + |
| 98 | +export function accountDataFromJSON(data: any): AccountData { |
| 99 | + return { |
| 100 | + address: toBuffer(data.address), |
| 101 | + accountProof: data.accountProof.map((n: string) => toBuffer(n)), |
| 102 | + nonce: toBN(data.nonce), |
| 103 | + balance: toBN(data.balance), |
| 104 | + codeHash: toBuffer(data.codeHash), |
| 105 | + storageHash: toBuffer(data.storageHash), |
| 106 | + storageProof: data.storageProof.map((n: string) => toBuffer(n)), |
| 107 | + } |
| 108 | +} |
| 109 | + |
| 110 | +export function transactionDataFromJSON(data: any): TransactionData { |
| 111 | + return { |
| 112 | + to: toBuffer(data.to), |
| 113 | + value: toBN(data.value), |
| 114 | + nonce: toBN(data.nonce), |
| 115 | + from: toBuffer(data.from), |
| 116 | + } |
| 117 | +} |
| 118 | + |
| 119 | +export function toBuffer(str: string): Buffer { |
| 120 | + return Buffer.from(str.slice(2), 'hex') |
| 121 | +} |
| 122 | + |
| 123 | +function toBN(str: string): BN { |
| 124 | + return new BN(str.slice(2), 16) |
| 125 | +} |
0 commit comments