onflow / flow-evm-gateway

FlowEVM Gateway implements an Ethereum-equivalent JSON-RPC API for EVM clients to use
https://developers.flow.com/evm/about
Apache License 2.0
11 stars 10 forks source link

Calculation of `gasUsed` field for blocks is inaccurate #443

Closed m-Peter closed 3 months ago

m-Peter commented 3 months ago

When fetching the following block with all its transactions:

curl -s -XPOST 'https://testnet.evm.nodes.onflow.org' --header 'Content-Type: application/json' --data-raw '{"jsonrpc":"2.0","method":"eth_getBlockByNumber","params":["0x9f2",true],"id":7}' | jq

we get back:

{
  "jsonrpc": "2.0",
  "id": 7,
  "result": {
    "number": "0x9f2",
    "hash": "0x6b2844981b110526dea03bfb5e68456d03b936f2e649850f8616841b1c0a2af9",
    "parentHash": "0x85834b966e4e486976dac3ca95158eeaf217df0f6d95276b08a08b2a8998f435",
    "nonce": "0x0100000000000000",
    "sha3Uncles": "0x0000000000000000000000000000000000000000000000000000000000000000",
    "logsBloom": "0x00000000000004000000000000000000000000000000000000800000000000800000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001000000000000000000008000000000000000020000000000000000001800000000000000000000000000000000400000000000000000000000000000000000000000000000000000000000000000000000000000000000001000000000000000000000000000000000000000000000000000000000000040000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000000000",
    "transactionsRoot": "0x1c85a38a5738b2aa21440ead76a5e07f047e99d8177867cf357c83bea94c2430",
    "stateRoot": "0x0000000000000000000000000000000000000000000000000000000000000000",
    "receiptsRoot": "0x29a6b3bf610d2b73bee197720a9e34f92317077437de3e8b61722e4a1fef9b48",
    "miner": "0x0000000000000000000000000000000000000000",
    "difficulty": "0x0",
    "totalDifficulty": "0x0",
    "extraData": "0x",
    "size": "0x234f",
    "gasLimit": "0xe4e1c0",
    "gasUsed": "0xe4e1c0",
    "timestamp": "0x66bd0111",
    "transactions": [
      {
        "blockHash": "0x6b2844981b110526dea03bfb5e68456d03b936f2e649850f8616841b1c0a2af9",
        "blockNumber": "0x9f2",
        "from": "0x0000000000000000000000023f946FFbc8829BFD",
        "gas": "0xe4e1c0",
        "gasPrice": "0x0",
        "hash": "0x24488eccbd33523548b7d12835eb442dd7c4d9d01ec5ea9b0e54eba2ac965737",
        "input": "0x...",
        "nonce": "0x3",
        "to": null,
        "transactionIndex": "0x0",
        "value": "0x0",
        "type": "0x0",
        "chainId": "0x221",
        "v": "0xff",
        "r": "0x23f946ffbc8829bfd",
        "s": "0x4"
      }
    ],
    "uncles": [],
    "mixHash": "0x0000000000000000000000000000000000000000000000000000000000000000",
    "baseFeePerGas": "0x0"
  }
}

where both gasLimit and gasUsed are equal to 15M. However, when fetching the transaction receipt for the block's transaction, the gasUsed is equal to 1_942_327. When calculating the block's gasUsed, we mistakenly used the gas limit set by the transaction author, and not the gasUsed from the transaction's receipt.