Skip to content
This repository has been archived by the owner on Aug 19, 2024. It is now read-only.

Implemented FeeHistory API #1130

Merged
merged 9 commits into from
Feb 11, 2022
Merged

Conversation

aeharvlee
Copy link
Contributor

@aeharvlee aeharvlee commented Jan 24, 2022

Proposed changes

Most of the code is based on Ethereum's code, So please take a look Ethereum's FeeHistory API

Related docs

Differences with Geth's implementation.

  • Klaytn uses baseFeePerGas as 0 so there is no need to calculate this value.
  • Klaytn uses maxPriorityPerGas as 25 ston which is configured by governance so there is no need to calculate this value.
  • Removed cache because Gas-related values in Klaytn are all fixed, so it was determined that this API is unlikely to be used significantly.
  • Added OracleBackend but there are some differences from Etheruem's OracleBackend
    • Remove SubscribeChainHeadEvent because Klaytn uses fixed gasPrice policy, so we don't have to fetch and cache latest head.
    • Remove PendingBlockAndReceipts because there is no related apis about pending block in Klaytn backend.
    • Change GetReceipts to GetBlockReceipts because this fits to Klaytn backend operation.
  • Removed logic which cares block re-org situation because there is no reorg situation in Klaytn.

How to test this PR

First, please clone my origin repo.

1. Run added tests in this PR.

  • Run go test -run "TestEthereumAPI_*" -v ./... -count=1

2. Build ken and test with it.

1. Build ken with source codes related with this PR.

  • Run make ken

2. Run ken

  • In my case, I tested with CN 1 - PN 1 - EN 1 in my local.
  • Please note that you should enable eth namepsace to RPC_API and WS_API in kend.conf

3. Setup environment to send txs

  • To test this API efficiently, you should includes some txs into blocks.

Test logs

KEN
➜  corecell curl localhost:8551 -X POST -H "Content-Type: application/json" -d '{"jsonrpc":"2.0","method":"eth_feeHistory","params":["0x10", "latest", [0.1, 0.2, 0.3]],"id":1}'

{"jsonrpc":"2.0","id":1,"result":{"oldestBlock":"0xa5b","reward":[["0x0","0x0","0x0"],["0x0","0x0","0x0"],["0x0","0x0","0x0"],["0x0","0x0","0x0"],["0x0","0x0","0x0"],["0x5d21dba00","0x5d21dba00","0x5d21dba00"],["0x0","0x0","0x0"],["0x0","0x0","0x0"],["0x5d21dba00","0x5d21dba00","0x5d21dba00"],["0x0","0x0","0x0"],["0x0","0x0","0x0"],["0x5d21dba00","0x5d21dba00","0x5d21dba00"],["0x0","0x0","0x0"],["0x0","0x0","0x0"],["0x0","0x0","0x0"],["0x0","0x0","0x0"]],"baseFeePerGas":["0x0","0x0","0x0","0x0","0x0","0x0","0x0","0x0","0x0","0x0","0x0","0x0","0x0","0x0","0x0","0x0","0x0"],"gasUsedRatio":[0,0,0,0,0,0.0002952004000002952,0,0,0.00029504250000029504,0,0,0.0002963777000002964,0,0,0,0]}}
Beautified Result
{
  "jsonrpc": "2.0",
  "id": 1,
  "result": {
    "oldestBlock": "0xa5b",
    "reward": [
      [
        "0x0",
        "0x0",
        "0x0"
      ],
      [
        "0x0",
        "0x0",
        "0x0"
      ],
      [
        "0x0",
        "0x0",
        "0x0"
      ],
      [
        "0x0",
        "0x0",
        "0x0"
      ],
      [
        "0x0",
        "0x0",
        "0x0"
      ],
      [
        "0x5d21dba00",
        "0x5d21dba00",
        "0x5d21dba00"
      ],
      [
        "0x0",
        "0x0",
        "0x0"
      ],
      [
        "0x0",
        "0x0",
        "0x0"
      ],
      [
        "0x5d21dba00",
        "0x5d21dba00",
        "0x5d21dba00"
      ],
      [
        "0x0",
        "0x0",
        "0x0"
      ],
      [
        "0x0",
        "0x0",
        "0x0"
      ],
      [
        "0x5d21dba00",
        "0x5d21dba00",
        "0x5d21dba00"
      ],
      [
        "0x0",
        "0x0",
        "0x0"
      ],
      [
        "0x0",
        "0x0",
        "0x0"
      ],
      [
        "0x0",
        "0x0",
        "0x0"
      ],
      [
        "0x0",
        "0x0",
        "0x0"
      ]
    ],
    "baseFeePerGas": [
      "0x0",
      "0x0",
      "0x0",
      "0x0",
      "0x0",
      "0x0",
      "0x0",
      "0x0",
      "0x0",
      "0x0",
      "0x0",
      "0x0",
      "0x0",
      "0x0",
      "0x0",
      "0x0",
      "0x0"
    ],
    "gasUsedRatio": [
      0,
      0,
      0,
      0,
      0,
      0.0002952004000002952,
      0,
      0,
      0.00029504250000029504,
      0,
      0,
      0.0002963777000002964,
      0,
      0,
      0,
      0
    ]
  }
}

Should note that

  • It's totally normal that returned length of baseFeePerGas is 17(=0x10 + 0x1) because it should always return nextBaseFee based on the spec. Other fields lengths are all 16(=0x10).
  • Block numbers which includes txs are 2656(=0xa60), 2659(=0xa63), (2662 =0xa66).
    • You can calculate by your hands about gasUsedRatio.
      • e.g. Block#2656(=0xa60) used total gas 295200400 => 295200400 / UpperGasLimit == 0.0002952004000002952 which is the same with API return.

Related KEN logs

INFO[01/24,20:44:58 +09] [5] Inserted a new block                      number=2651 hash=1bffeb…94ba55 txs=0    gas=0         elapsed=840.496µs  processTxs=189ns     finalize=94.533µs  validateState=5.48µs    totalWrite=638.422µs   trieWrite=492.902µs
INFO[01/24,20:44:59 +09] [5] Inserted a new block                      number=2652 hash=50510d…76fec6 txs=0    gas=0         elapsed=823.627µs  processTxs=170ns     finalize=99.076µs  validateState=4.254µs   totalWrite=608.139µs   trieWrite=463.819µs
INFO[01/24,20:45:00 +09] [5] Inserted a new block                      number=2653 hash=49bc6a…72c248 txs=0    gas=0         elapsed=771.874µs  processTxs=281ns     finalize=89.018µs  validateState=3.98µs    totalWrite=583.611µs   trieWrite=459.231µs
INFO[01/24,20:45:01 +09] [5] Inserted a new block                      number=2654 hash=6b02a6…61401b txs=0    gas=0         elapsed=976.995µs  processTxs=207ns     finalize=112.059µs validateState=4.258µs   totalWrite=731.686µs   trieWrite=579.202µs
INFO[01/24,20:45:02 +09] [5] Inserted a new block                      number=2655 hash=538fc5…faca7b txs=0    gas=0         elapsed=934.676µs  processTxs=237ns     finalize=90.98µs   validateState=3.476µs   totalWrite=748.186µs   trieWrite=520.496µs
INFO[01/24,20:45:04 +09] [5] Inserted a new block                      number=2656 hash=74fbf3…9d5d1d txs=1000 gas=295200400 elapsed=83.426ms   processTxs=17.139ms  finalize=16.235ms  validateState=2.285ms   totalWrite=13.144699ms trieWrite=616.695µs
INFO[01/24,20:45:04 +09] [5] Inserted a new block                      number=2657 hash=41cdf7…aea6e4 txs=0    gas=0         elapsed=1.514ms    processTxs=185ns     finalize=159.547µs validateState=7.754µs   totalWrite=1.18245ms   trieWrite=925.718µs
INFO[01/24,20:45:05 +09] [5] Inserted a new block                      number=2658 hash=948b86…c7c29d txs=0    gas=0         elapsed=867.539µs  processTxs=178ns     finalize=121.523µs validateState=3.895µs   totalWrite=634.826µs   trieWrite=497.353µs
INFO[01/24,20:45:07 +09] [5] Inserted a new block                      number=2659 hash=d555b8…2a809b txs=1000 gas=295042500 elapsed=185.142ms  processTxs=22.453ms  finalize=16.466ms  validateState=1.990ms   totalWrite=112.245995ms trieWrite=975.246µs
INFO[01/24,20:45:07 +09] [5] Inserted a new block                      number=2660 hash=a84cd0…63c74e txs=0    gas=0         elapsed=1.026ms    processTxs=193ns     finalize=96.656µs  validateState=3.873µs   totalWrite=812.386µs    trieWrite=502.322µs
INFO[01/24,20:45:08 +09] [5] Inserted a new block                      number=2661 hash=21a055…ad479b txs=0    gas=0         elapsed=903.781µs  processTxs=220ns     finalize=97.358µs  validateState=5.586µs   totalWrite=694.225µs    trieWrite=515.3µs
INFO[01/24,20:45:10 +09] [28] Downloader queue stats                    receiptTasks=0 blockTasks=0 itemSize=522.66kB throttle=129
INFO[01/24,20:45:10 +09] [5] Inserted a new block                      number=2662 hash=de8541…546a79 txs=1000 gas=296377700 elapsed=65.200ms   processTxs=12.443ms  finalize=10.957ms  validateState=1.590ms   totalWrite=7.008975ms   trieWrite=688.665µs
INFO[01/24,20:45:10 +09] [5] Inserted a new block                      number=2663 hash=4edd29…f80c1a txs=0    gas=0         elapsed=953.165µs  processTxs=184ns     finalize=102.473µs validateState=3.912µs   totalWrite=717.364µs    trieWrite=555.916µs
INFO[01/24,20:45:11 +09] [5] Inserted a new block                      number=2664 hash=ee2edb…8fa433 txs=0    gas=0         elapsed=740.836µs  processTxs=161ns     finalize=91.187µs  validateState=3.581µs   totalWrite=555.074µs    trieWrite=423.219µs
INFO[01/24,20:45:12 +09] [5] Inserted a new block                      number=2665 hash=fe4a2b…b5641b txs=0    gas=0         elapsed=1.081ms    processTxs=270ns     finalize=169.678µs validateState=7.305µs   totalWrite=775.518µs    trieWrite=575.125µs
INFO[01/24,20:45:13 +09] [5] Inserted a new block                      number=2666 hash=7ce1ef…1cc883 txs=0    gas=0         elapsed=848.416µs  processTxs=176ns     finalize=118.564µs validateState=4.061µs   totalWrite=589.887µs    trieWrite=452.169µs
Geth

Specify reward percentiles

$ curl localhost:8545 -X POST -H "Content-Type: application/json" -d '{"jsonrpc":"2.0","method":"eth_feeHistory","params":["0x10", "latest", [0.1, 0.2, 0.3]],"id":1}'
{"jsonrpc":"2.0","id":1,"result":{"oldestBlock":"0xd228dd","reward":[["0x3b9aca00","0x3b9aca00","0x3b9aca00"],["0x3b9aca00","0x3b9aca00","0x3b9aca00"],["0x3b9aca00","0x3b9aca00","0x3b9aca00"],["0x3b9aca00","0x3b9aca00","0x3b9aca00"],["0x3b9aca00","0x3b9aca00","0x3b9aca00"],["0x3fd92b98","0x3fd92b98","0x3fd92b98"],["0x3b9aca00","0x3b9aca00","0x3b9aca00"],["0x3b9aca00","0x3b9aca00","0x3b9aca00"],["0x3b9aca00","0x3b9aca00","0x3b9aca00"],["0x40d1c0f0","0x40d1c0f0","0x40d1c0f0"],["0x3b9aca00","0x3b9aca00","0x3b9aca00"],["0x3b9aca00","0x3b9aca00","0x3b9aca00"],["0x9502f900","0x9502f900","0x9502f900"],["0x3b9aca00","0x3b9aca00","0x3b9aca00"],["0x3b9aca00","0x3b9aca00","0x3b9aca00"],["0x3b9aca00","0x3b9aca00","0x3b9aca00"]],"baseFeePerGas":["0x31bc8b70b6","0x302315d904","0x2f06e93aaa","0x32e0c409bb","0x35f6c31f2c","0x31e4232d6c","0x2f230a3b20","0x350520a56f","0x37ec9b6c52","0x38814bf9ab","0x32f9d702a4","0x2f273fd988","0x29d67aa866","0x2f0fc76273","0x34f1595f15","0x3b782e8404","0x37fe584321"],"gasUsedRatio":[0.3713660480529107,0.40775872004001795,0.8275560837363439,0.7426136284391863,0.1981128474818883,0.2791763666666667,0.9992401,0.7191034,0.5415432666666666,0.10859236666666666,0.20004521081502755,0.04908717914803387,0.999436,0.9998665,0.9931128333333333,0.2662043333333333]}}
Beautified Result
{
  "jsonrpc": "2.0",
  "id": 1,
  "result": {
    "oldestBlock": "0xd228dd",
    "reward": [
      [
        "0x3b9aca00",
        "0x3b9aca00",
        "0x3b9aca00"
      ],
      [
        "0x3b9aca00",
        "0x3b9aca00",
        "0x3b9aca00"
      ],
      [
        "0x3b9aca00",
        "0x3b9aca00",
        "0x3b9aca00"
      ],
      [
        "0x3b9aca00",
        "0x3b9aca00",
        "0x3b9aca00"
      ],
      [
        "0x3b9aca00",
        "0x3b9aca00",
        "0x3b9aca00"
      ],
      [
        "0x3fd92b98",
        "0x3fd92b98",
        "0x3fd92b98"
      ],
      [
        "0x3b9aca00",
        "0x3b9aca00",
        "0x3b9aca00"
      ],
      [
        "0x3b9aca00",
        "0x3b9aca00",
        "0x3b9aca00"
      ],
      [
        "0x3b9aca00",
        "0x3b9aca00",
        "0x3b9aca00"
      ],
      [
        "0x40d1c0f0",
        "0x40d1c0f0",
        "0x40d1c0f0"
      ],
      [
        "0x3b9aca00",
        "0x3b9aca00",
        "0x3b9aca00"
      ],
      [
        "0x3b9aca00",
        "0x3b9aca00",
        "0x3b9aca00"
      ],
      [
        "0x9502f900",
        "0x9502f900",
        "0x9502f900"
      ],
      [
        "0x3b9aca00",
        "0x3b9aca00",
        "0x3b9aca00"
      ],
      [
        "0x3b9aca00",
        "0x3b9aca00",
        "0x3b9aca00"
      ],
      [
        "0x3b9aca00",
        "0x3b9aca00",
        "0x3b9aca00"
      ]
    ],
    "baseFeePerGas": [
      "0x31bc8b70b6",
      "0x302315d904",
      "0x2f06e93aaa",
      "0x32e0c409bb",
      "0x35f6c31f2c",
      "0x31e4232d6c",
      "0x2f230a3b20",
      "0x350520a56f",
      "0x37ec9b6c52",
      "0x38814bf9ab",
      "0x32f9d702a4",
      "0x2f273fd988",
      "0x29d67aa866",
      "0x2f0fc76273",
      "0x34f1595f15",
      "0x3b782e8404",
      "0x37fe584321"
    ],
    "gasUsedRatio": [
      0.3713660480529107,
      0.40775872004001795,
      0.8275560837363439,
      0.7426136284391863,
      0.1981128474818883,
      0.2791763666666667,
      0.9992401,
      0.7191034,
      0.5415432666666666,
      0.10859236666666666,
      0.20004521081502755,
      0.04908717914803387,
      0.999436,
      0.9998665,
      0.9931128333333333,
      0.2662043333333333
    ]
  }
}

Didn't specify reward percentiles

$ curl localhost:8545 -X POST -H "Content-Type: application/json" -d '{"jsonrpc":"2.0","method":"eth_feeHistory","params":["0x10", "latest", []],"id":1}'
{"jsonrpc":"2.0","id":1,"result":{"oldestBlock":"0xd228dd","baseFeePerGas":["0x31bc8b70b6","0x302315d904","0x2f06e93aaa","0x32e0c409bb","0x35f6c31f2c","0x31e4232d6c","0x2f230a3b20","0x350520a56f","0x37ec9b6c52","0x38814bf9ab","0x32f9d702a4","0x2f273fd988","0x29d67aa866","0x2f0fc76273","0x34f1595f15","0x3b782e8404","0x37fe584321"],"gasUsedRatio":[0.3713660480529107,0.40775872004001795,0.8275560837363439,0.7426136284391863,0.1981128474818883,0.2791763666666667,0.9992401,0.7191034,0.5415432666666666,0.10859236666666666,0.20004521081502755,0.04908717914803387,0.999436,0.9998665,0.9931128333333333,0.2662043333333333]}}
Beautified Result
{
  "jsonrpc": "2.0",
  "id": 1,
  "result": {
    "oldestBlock": "0xd228dd",
    "baseFeePerGas": [
      "0x31bc8b70b6",
      "0x302315d904",
      "0x2f06e93aaa",
      "0x32e0c409bb",
      "0x35f6c31f2c",
      "0x31e4232d6c",
      "0x2f230a3b20",
      "0x350520a56f",
      "0x37ec9b6c52",
      "0x38814bf9ab",
      "0x32f9d702a4",
      "0x2f273fd988",
      "0x29d67aa866",
      "0x2f0fc76273",
      "0x34f1595f15",
      "0x3b782e8404",
      "0x37fe584321"
    ],
    "gasUsedRatio": [
      0.3713660480529107,
      0.40775872004001795,
      0.8275560837363439,
      0.7426136284391863,
      0.1981128474818883,
      0.2791763666666667,
      0.9992401,
      0.7191034,
      0.5415432666666666,
      0.10859236666666666,
      0.20004521081502755,
      0.04908717914803387,
      0.999436,
      0.9998665,
      0.9931128333333333,
      0.2662043333333333
    ]
  }
}

** Fetch empty blocks **

$ curl localhost:8545 -X POST -H "Content-Type: application/json" -d '{"jsonrpc":"2.0","method":"eth_feeHistory","params":["0x5", "0x10", [0.1, 0.2, 0.3]],"id":1}'
{"jsonrpc":"2.0","id":1,"result":{"oldestBlock":"0xc","reward":[["0x0","0x0","0x0"],["0x0","0x0","0x0"],["0x0","0x0","0x0"],["0x0","0x0","0x0"],["0x0","0x0","0x0"]],"baseFeePerGas":["0x0","0x0","0x0","0x0","0x0","0x0"],"gasUsedRatio":[0,0,0,0,0]}}
Beautified Result
{
  "jsonrpc": "2.0",
  "id": 1,
  "result": {
    "oldestBlock": "0xc",
    "reward": [
      [
        "0x0",
        "0x0",
        "0x0"
      ],
      [
        "0x0",
        "0x0",
        "0x0"
      ],
      [
        "0x0",
        "0x0",
        "0x0"
      ],
      [
        "0x0",
        "0x0",
        "0x0"
      ],
      [
        "0x0",
        "0x0",
        "0x0"
      ]
    ],
    "baseFeePerGas": [
      "0x0",
      "0x0",
      "0x0",
      "0x0",
      "0x0",
      "0x0"
    ],
    "gasUsedRatio": [
      0,
      0,
      0,
      0,
      0
    ]
  }
}

Types of changes

Please put an x in the boxes related to your change.

  • Bugfix
  • New feature or enhancement
  • Others

Checklist

Put an x in the boxes that apply. You can also fill these out after creating the PR. If you're unsure about any of them, don't hesitate to ask. We're here to help! This is simply a reminder of what we are going to look for before merging your code.

  • I have read the CONTRIBUTING GUIDELINES doc
  • I have signed the CLA
  • Lint and unit tests pass locally with my changes ($ make test)
  • I have added tests that prove my fix is effective or that my feature works
  • I have added necessary documentation (if appropriate)
  • Any dependent changes have been merged and published in downstream modules

Related issues

  • Please leave the issue numbers or links related to this PR here.

Further comments

If this is a relatively large or complex change, kick off the discussion by explaining why you chose the solution you did and what alternatives you considered, etc...

Most of the code is based on Ethereum's code.
So please take a look.

Differences
* Klaytn uses baseFeePerGas as 0.
* Klaytn uses maxPriorityPerGas as 25 ston which is configured by governance.
* Didn't applied cache because Gas-related values in Klaytn are all fixed, so it was determined that this API is unlikely to be used significantly.
* Added OracleBackend but there are some differences from Etheruem's backend
  * Remove SubscribeChainHeadEvent because Klaytn uses fixed gasPrice policy, so we don't have to fetch and cache latest head.
  * Remove PendingBlockAndReceipts because there is no related apis about pending block in Klaytn backend.
  * Change GetReceipts to GetBlockReceipts because this fits to Klaytn backend operation.
@kjhman21 kjhman21 removed this from the v1.8.0 milestone Feb 10, 2022
@aeharvlee aeharvlee linked an issue Feb 10, 2022 that may be closed by this pull request
5 tasks
jimni1222
jimni1222 previously approved these changes Feb 10, 2022
aidan-kwon
aidan-kwon previously approved these changes Feb 10, 2022
@aidan-kwon
Copy link
Member

@jeongkyun-oh @yoomee1313 PTAL

sirano11
sirano11 previously approved these changes Feb 11, 2022
IntrinsicGas now consider AccessList. This is feature is from upstream/dev branch.
aidan-kwon
aidan-kwon previously approved these changes Feb 11, 2022
jimni1222
jimni1222 previously approved these changes Feb 11, 2022
IntrinsicGas now consider AccessList. This is feature is from upstream/dev branch.
@aeharvlee aeharvlee dismissed stale reviews from jimni1222 and aidan-kwon via 0828f14 February 11, 2022 07:14
@aeharvlee aeharvlee merged commit d85dd88 into klaytn:dev Feb 11, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Support Ethereum dev tools compatibility
5 participants