Commit 1bc67228 authored by George Hotz's avatar George Hotz

now getting nonce too high :)

parent aa48e6ae
import { ethers } from 'ethers' import { ethers } from 'ethers'
import assert from 'assert' import assert from 'assert'
import fs from 'fs' import fs from 'fs'
import { exit } from 'process'
const keccak256 = ethers.utils.keccak256 const keccak256 = ethers.utils.keccak256
const rlp = require('rlp') const rlp = require('rlp')
...@@ -15,23 +16,51 @@ type extblock struct { ...@@ -15,23 +16,51 @@ type extblock struct {
Uncles []*Header Uncles []*Header
} }
*/ */
function getTransactionRlp(tx : any) { function getTransactionRlp(tx : any): Buffer {
let dat = [ let dat: any
tx.nonce, if (tx.type == "0x2") {
tx.gasPrice, let accesslist = tx.accessList.map((x : any) => [x.address, x.storageKeys])
tx.gas, // london
tx.to, dat = [
tx.value, tx.chainId,
tx.input, tx.nonce,
tx.v, tx.maxPriorityFeePerGas,
tx.r, tx.maxFeePerGas,
tx.s, tx.gas,
]; tx.to,
dat = dat.map((x) => (x == "0x0") ? "0x" : x) tx.value,
tx.input,
accesslist,
tx.v,
tx.r,
tx.s,
]
dat = dat.map((x : any) => (x == "0x0") ? "0x" : x)
dat = Buffer.concat([Buffer.from([parseInt(tx.type)]), rlp.encode(dat)])
assert(keccak256(dat) == tx.hash)
} else {
// pre london
dat = [
tx.nonce,
tx.gasPrice,
tx.gas,
tx.to,
tx.value,
tx.input,
tx.v,
tx.r,
tx.s,
];
dat = dat.map((x : any) => (x == "0x0") ? "0x" : x)
dat = rlp.encode(dat)
assert(keccak256(dat) == tx.hash)
dat = rlp.decode(dat)
}
//console.log(tx)
return dat return dat
} }
function getBlockRlp(block : any) { function getBlockRlp(block : any): Buffer {
let dat = [ let dat = [
block['parentHash'], block['parentHash'],
block['sha3Uncles'], block['sha3Uncles'],
...@@ -50,20 +79,22 @@ function getBlockRlp(block : any) { ...@@ -50,20 +79,22 @@ function getBlockRlp(block : any) {
block['nonce'], block['nonce'],
block['baseFeePerGas'] block['baseFeePerGas']
]; ];
dat = dat.map((x) => (x == "0x0") ? "0x" : x) dat = dat.map((x : any) => (x == "0x0") ? "0x" : x)
return dat //console.log(dat)
let rdat = rlp.encode(dat)
assert(keccak256(rdat) == block['hash'])
return rdat
} }
async function main() { async function main() {
const blockNumber = 13247502; const blockNumber = 13247502;
let blockData = await provider.send("eth_getBlockByNumber", ["0x"+(blockNumber).toString(16), true]) let blockData = await provider.send("eth_getBlockByNumber", ["0x"+(blockNumber).toString(16), true])
const blockHeaderRlp = rlp.encode(getBlockRlp(blockData)) const blockHeaderRlp = getBlockRlp(blockData)
assert(keccak256(blockHeaderRlp) == blockData['hash'])
//console.log(blockData) //console.log(blockData)
const txsRlp = rlp.encode(blockData.transactions.map(getTransactionRlp)) const txsRlp = blockData.transactions.map(getTransactionRlp)
fs.writeFileSync(`data/block_${blockNumber}`, blockHeaderRlp) fs.writeFileSync(`data/block_${blockNumber}`, blockHeaderRlp)
fs.writeFileSync(`data/tx_${blockNumber}`, txsRlp) fs.writeFileSync(`data/txs_${blockNumber}`, rlp.encode(txsRlp))
} }
main().then(() => process.exit(0)) main().then(() => process.exit(0))
...@@ -32,7 +32,7 @@ func main() { ...@@ -32,7 +32,7 @@ func main() {
// read txs // read txs
var txs []*types.Transaction var txs []*types.Transaction
{ {
f, _ := os.Open("data/tx_13247502") f, _ := os.Open("data/txs_13247502")
defer f.Close() defer f.Close()
rlpheader := rlp.NewStream(f, 0) rlpheader := rlp.NewStream(f, 0)
rlpheader.Decode(&txs) rlpheader.Decode(&txs)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment