get_block.ts 2.5 KB
Newer Older
George Hotz's avatar
George Hotz committed
1 2 3
import { ethers } from 'ethers'
import assert from 'assert'
import fs from 'fs'
4
import { exit } from 'process'
George Hotz's avatar
George Hotz committed
5 6 7 8 9 10 11

const keccak256 = ethers.utils.keccak256
const rlp = require('rlp')

// local geth
const provider = new ethers.providers.JsonRpcProvider('http://192.168.1.213:8545')

George Hotz's avatar
George Hotz committed
12 13 14 15 16 17 18
/*
type extblock struct {
	Header *Header
	Txs    []*Transaction
	Uncles []*Header
}
*/
19 20
function getTransactionRlp(tx : any): Buffer {
  let dat: any
21
  // TODO: there are also type 1 transactions
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
  if (tx.type == "0x2") {
    let accesslist = tx.accessList.map((x : any) => [x.address, x.storageKeys])
    // london
    dat = [
      tx.chainId,
      tx.nonce,
      tx.maxPriorityFeePerGas,
      tx.maxFeePerGas,
      tx.gas,
      tx.to,
      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)
61 62
  return dat
}
George Hotz's avatar
George Hotz committed
63

64
function getBlockRlp(block : any): Buffer {
George Hotz's avatar
George Hotz committed
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82
  let dat = [
    block['parentHash'],
    block['sha3Uncles'],
    block['miner'],
    block['stateRoot'],
    block['transactionsRoot'],
    block['receiptsRoot'],
    block['logsBloom'],
    block['difficulty'],
    block['number'],
    block['gasLimit'],
    block['gasUsed'],
    block['timestamp'],
    block['extraData'],
    block['mixHash'],
    block['nonce'],
    block['baseFeePerGas']
  ];
83 84 85 86 87
  dat = dat.map((x : any) => (x == "0x0") ? "0x" : x)
  //console.log(dat)
  let rdat = rlp.encode(dat)
  assert(keccak256(rdat) == block['hash'])
  return rdat
George Hotz's avatar
George Hotz committed
88 89
}

George Hotz's avatar
George Hotz committed
90
async function getBlock(blockNumber: Number) {
George Hotz's avatar
George Hotz committed
91
  let blockData = await provider.send("eth_getBlockByNumber", ["0x"+(blockNumber).toString(16), true])
92
  const blockHeaderRlp = getBlockRlp(blockData)
93
  //console.log(blockData)
94
  const txsRlp = blockData.transactions.map(getTransactionRlp)
George Hotz's avatar
George Hotz committed
95

George Hotz's avatar
George Hotz committed
96
  fs.writeFileSync(`data/block_${blockNumber}`, blockHeaderRlp)
97
  fs.writeFileSync(`data/txs_${blockNumber}`, rlp.encode(txsRlp))
George Hotz's avatar
George Hotz committed
98 99
}

George Hotz's avatar
George Hotz committed
100 101

async function main() {
102 103
  /*await getBlock(13247501)
  await getBlock(13247502)*/
104 105
  await getBlock(13284491)
  await getBlock(13284492)
George Hotz's avatar
George Hotz committed
106 107
}

George Hotz's avatar
George Hotz committed
108
main().then(() => process.exit(0))