Skip to content

Commit 6c34fc4

Browse files
author
michael
committed
doc: add function comments
1 parent 90d65fd commit 6c34fc4

5 files changed

Lines changed: 61 additions & 15 deletions

File tree

core/api/README.md

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -605,10 +605,11 @@ Request
605605
{
606606
"id": 1,
607607
"jsonrpc": "2.0",
608-
"method": "eth_getTransactionReceipt",
608+
"method": "eth_feeHistory",
609609
"params": [
610-
"0x8",
611-
"0x1b4"
610+
"0x1",
611+
"latest"
612+
[20, 30],
612613
]
613614
}
614615
```
@@ -622,9 +623,9 @@ Response
622623
"jsonrpc": "2.0",
623624
"result": {
624625
"oldestBlock": "0x0",
625-
"reward": null,
626-
"baseFeePerGas": [],
627-
"gasUsedRatio": []
626+
"baseFeePerGas": ["0x539","0x539"],
627+
"gasUsedRatio": [0.0],
628+
"reward": [["0x0"]]
628629
},
629630
"id": 2
630631
}

core/api/src/jsonrpc/impl/web3.rs

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@ use protocol::traits::{APIAdapter, Context, Interoperation};
99
use protocol::types::{
1010
Block, BlockNumber, Bytes, CellDepWithPubKey, Hash, Hasher, Header, Hex, Proposal, Receipt,
1111
SignatureComponents, SignatureR, SignatureS, SignedTransaction, TxResp, UnverifiedTransaction,
12-
BASE_FEE_PER_GAS, H160, H256, H64, MAX_RPC_GAS_CAP, MIN_TRANSACTION_GAS_LIMIT, U256,
12+
BASE_FEE_PER_GAS, H160, H256, H64, MAX_FEE_HISTORY, MAX_RPC_GAS_CAP, MIN_TRANSACTION_GAS_LIMIT,
13+
U256,
1314
};
1415
use protocol::{
1516
async_trait, ckb_blake2b_256, codec::ProtocolCodec, lazy::PROTOCOL_VERSION, ProtocolResult,
@@ -147,7 +148,7 @@ impl<Adapter: APIAdapter> Web3RpcImpl<Adapter> {
147148
let oldest_block_number = latest_block_number
148149
.saturating_sub(block_count.as_u64())
149150
.saturating_add(1);
150-
151+
151152
let mut bash_fee_per_gases: Vec<U256> = Vec::new();
152153
let mut gas_used_ratios: Vec<f64> = Vec::new();
153154
let mut reward: Vec<Vec<U256>> = Vec::new();
@@ -165,7 +166,7 @@ impl<Adapter: APIAdapter> Web3RpcImpl<Adapter> {
165166
let gas_used_ratio = calculate_gas_used_ratio(&block);
166167
gas_used_ratios.push(gas_used_ratio);
167168
bash_fee_per_gases.push(block.header.base_fee_per_gas);
168-
bash_fee_per_gases.push(BASE_FEE_PER_GAS.into());
169+
bash_fee_per_gases.push(next_block_base_fee_per_gas());
169170

170171
if let Some(reward_percentiles) = reward_percentiles.clone() {
171172
let txs = block.tx_hashes;
@@ -768,8 +769,8 @@ impl<Adapter: APIAdapter + 'static> Web3RpcServer for Web3RpcImpl<Adapter> {
768769
BlockCount::U64Type(n) => blocks_count = n.into(),
769770
}
770771
// Between 1 and 1024 blocks can be requested in a single query.
771-
if blocks_count > 1024.into() {
772-
blocks_count = 1024.into();
772+
if blocks_count > MAX_FEE_HISTORY.into() {
773+
blocks_count = MAX_FEE_HISTORY.into();
773774
}
774775
if blocks_count == 0.into() {
775776
return Ok(Web3FeeHistory::ZeroBlockCount(FeeHistoryEmpty {
@@ -841,8 +842,10 @@ impl<Adapter: APIAdapter + 'static> Web3RpcServer for Web3RpcImpl<Adapter> {
841842
.await
842843
.map_err(|e| Error::Custom(e.to_string()))?
843844
.unwrap();
844-
let base_fee_per_gas =
845-
vec![first_block.header.base_fee_per_gas, BASE_FEE_PER_GAS.into()];
845+
let base_fee_per_gas = vec![
846+
first_block.header.base_fee_per_gas,
847+
next_block_base_fee_per_gas(),
848+
];
846849
let gas_used_ratio = vec![calculate_gas_used_ratio(&first_block)];
847850

848851
match reward_percentiles {
@@ -1039,6 +1042,10 @@ fn calculate_effective_priority_fees_index(
10391042
.saturating_sub(1)
10401043
}
10411044

1045+
fn next_block_base_fee_per_gas() -> U256 {
1046+
BASE_FEE_PER_GAS.into()
1047+
}
1048+
10421049
fn mock_header_by_call_req(latest_header: Header, call_req: &Web3CallRequest) -> Header {
10431050
Header {
10441051
prev_hash: latest_header.prev_hash,

core/api/src/jsonrpc/web3_types.rs

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -725,6 +725,16 @@ pub enum BlockCount {
725725
U256Type(U256),
726726
}
727727

728+
/// Response type for `eth_feeHistory` RPC call.
729+
/// Three types of response are possible:
730+
/// 1. With reward, it returned when request parameter REWARDPERCENTILES is not
731+
/// null: `{"oldestBlock": "0x...", "reward": [["0x...", ...], ...],
732+
/// "baseFeePerGas": ["0x...", ...], "gasUsedRatio": [0.0, ...]}` 2. Without
733+
/// reward, it returned when request parameter REWARDPERCENTILES is null:
734+
/// `{"oldestBlock": "0x...", "baseFeePerGas": ["0x...", ...], "gasUsedRatio":
735+
/// [0.0, ...]}` 3. Zero block count, it returned when request parameter
736+
/// BLOCKCOUNT is 0: `{"oldestBlock": "0x...", "baseFeePerGas": [],
737+
/// "gasUsedRatio": []}` See https://docs.infura.io/infura/networks/ethereum/json-rpc-methods/eth_feehistory
728738
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq)]
729739
#[serde(rename_all = "camelCase", untagged)]
730740
pub enum Web3FeeHistory {
@@ -733,27 +743,51 @@ pub enum Web3FeeHistory {
733743
ZeroBlockCount(FeeHistoryEmpty),
734744
}
735745

746+
/// Response type for `eth_feeHistory` RPC call with parameter REWARDPERCENTILES
747+
/// is not null.
736748
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq)]
737749
#[serde(rename_all = "camelCase")]
738750
pub struct FeeHistoryWithReward {
751+
/// Lowest number block of the returned range.
739752
pub oldest_block: U256,
740-
pub reward: Vec<Vec<U256>>,
753+
/// An array of block base fees per gas.
754+
/// This includes the next block after the newest of the returned range,
755+
/// because this value can be derived from the newest block. Zeroes are
756+
/// returned for pre-EIP-1559 blocks.
741757
pub base_fee_per_gas: Vec<U256>,
758+
/// An array of block gas used ratios. These are calculated as the ratio
759+
/// of `gasUsed` and `gasLimit`.
742760
pub gas_used_ratio: Vec<f64>,
761+
/// An (optional) array of effective priority fee per gas data points from a
762+
/// single block. All zeroes are returned if the block is empty.
763+
pub reward: Vec<Vec<U256>>,
743764
}
744765

766+
/// Response type for `eth_feeHistory` RPC call with parameter REWARDPERCENTILES
767+
/// is null.
745768
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq)]
746769
#[serde(rename_all = "camelCase")]
747770
pub struct FeeHistoryWithoutReward {
771+
/// Lowest number block of the returned range.
748772
pub oldest_block: U256,
773+
/// An array of block base fees per gas.
774+
/// This includes the next block after the newest of the returned range,
775+
/// because this value can be derived from the newest block. Zeroes are
776+
/// returned for pre-EIP-1559 blocks.
749777
pub base_fee_per_gas: Vec<U256>,
778+
/// An array of block gas used ratios. These are calculated as the ratio
779+
/// of `gasUsed` and `gasLimit`.
750780
pub gas_used_ratio: Vec<f64>,
751781
}
752782

783+
/// Response type for `eth_feeHistory` RPC call with parameter BLOCKCOUNT is 0.
753784
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq)]
754785
#[serde(rename_all = "camelCase")]
755786
pub struct FeeHistoryEmpty {
787+
/// Lowest number block of the returned range.
756788
pub oldest_block: U256,
789+
/// An array of block gas used ratios. These are calculated as the ratio
790+
/// of `gasUsed` and `gasLimit`.
757791
pub gas_used_ratio: Option<Vec<f64>>,
758792
}
759793

protocol/src/types/block.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,10 @@ use crate::types::{
1010
pub type BlockNumber = u64;
1111

1212
pub const MAX_BLOCK_GAS_LIMIT: u64 = 30_000_000;
13+
// MAX_FEE_HISTORY is the maximum number of blocks that can be retrieved for a
14+
// fee history request. Between 1 and 1024 blocks can be requested in a single
15+
// query. reference: https://docs.infura.io/infura/networks/ethereum/json-rpc-methods/eth_feehistory/
16+
pub const MAX_FEE_HISTORY: u64 = 1024;
1317
pub const MAX_RPC_GAS_CAP: u64 = 50_000_000;
1418
pub const BASE_FEE_PER_GAS: u64 = 0x539;
1519

tests/e2e/src/eth_feeHistory.test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ describe("eth_feeHistory", () => {
2222
baseFeePerGas: [],
2323
gasUsedRatio: [],
2424
oldestBlock: "0x0",
25-
reward: null,
25+
// reward: null,
2626
});
2727
}, 100000);
2828
});

0 commit comments

Comments
 (0)