Skip to content

Commit

Permalink
Refactor get_block_by_number to use BlockNumber enum and adjust loggi…
Browse files Browse the repository at this point in the history
…ng level to trace
  • Loading branch information
itsyaasir committed Jan 30, 2025
1 parent a481c5b commit 03e1935
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 7 deletions.
30 changes: 24 additions & 6 deletions bin/reth/tests/commands/bitfinity_node_it.rs
Original file line number Diff line number Diff line change
Expand Up @@ -366,7 +366,7 @@ pub mod eth_server {

use alloy_consensus::constants::{EMPTY_RECEIPTS, EMPTY_TRANSACTIONS};
use alloy_rlp::Bytes;
use did::{keccak, H256};
use did::{keccak, BlockNumber, H256, U64};
use ethereum_json_rpc_client::CertifiedResult;
use jsonrpsee::{core::RpcResult, proc_macros::rpc};
use reth_trie::EMPTY_ROOT_HASH;
Expand Down Expand Up @@ -398,7 +398,10 @@ pub mod eth_server {

/// Returns a block by block number
#[method(name = "getBlockByNumber")]
async fn get_block_by_number(&self, number: U256) -> RpcResult<did::Block<did::H256>>;
async fn get_block_by_number(
&self,
number: BlockNumber,
) -> RpcResult<did::Block<did::H256>>;

/// Returns the chain ID
#[method(name = "chainId")]
Expand Down Expand Up @@ -441,7 +444,7 @@ pub mod eth_server {
let block_counter = current_block.clone();

let block_task = Some(tokio::spawn(async move {
let mut interval = tokio::time::interval(std::time::Duration::from_secs(1));
let mut interval = tokio::time::interval(std::time::Duration::from_millis(100));
loop {
interval.tick().await;
block_counter.fetch_add(1, std::sync::atomic::Ordering::Relaxed);
Expand Down Expand Up @@ -491,8 +494,21 @@ pub mod eth_server {

#[async_trait::async_trait]
impl EthServer for EthImpl {
async fn get_block_by_number(&self, number: U256) -> RpcResult<did::Block<did::H256>> {
let block_num = number.to::<u64>();
async fn get_block_by_number(
&self,
number: BlockNumber,
) -> RpcResult<did::Block<did::H256>> {
let block_num = match number {
BlockNumber::Latest | BlockNumber::Finalized | BlockNumber::Safe => {
self.current_block.load(std::sync::atomic::Ordering::Relaxed)
}
BlockNumber::Earliest => 0,
BlockNumber::Pending => {
self.current_block.fetch_add(1, std::sync::atomic::Ordering::Relaxed)
}
BlockNumber::Number(num) => num.as_u64(),
};

let mut block = did::Block {
number: block_num.into(),
timestamp: did::U256::from(1234567890_u64 + block_num),
Expand All @@ -504,7 +520,9 @@ pub mod eth_server {
parent_hash: if block_num == 0 {
H256::zero()
} else {
self.get_block_by_number(U256::from(block_num - 1)).await?.hash
self.get_block_by_number(BlockNumber::Number(U64::from(block_num - 1)))
.await?
.hash
},
..Default::default()
};
Expand Down
2 changes: 1 addition & 1 deletion bin/reth/tests/commands/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ pub fn init_logs() -> eyre::Result<Option<FileWorkerGuard>> {
let mut tracer = RethTracer::new();
let stdout = LayerInfo::new(
LogFormat::Terminal,
"info".to_string(),
"trace".to_string(),
String::new(),
Some("always".to_string()),
);
Expand Down

0 comments on commit 03e1935

Please sign in to comment.