From 4f0a418b329e04f38261f77cb2caafa8c4999fad Mon Sep 17 00:00:00 2001 From: bear Date: Fri, 25 Aug 2023 16:12:33 +0800 Subject: [PATCH 1/2] Rename --- client/rpc-core/src/eth.rs | 26 +++---- client/rpc-core/src/types/block_number.rs | 95 ++++++++++++----------- client/rpc-core/src/types/filter.rs | 14 ++-- client/rpc-core/src/types/mod.rs | 2 +- client/rpc/src/eth/block.rs | 14 ++-- client/rpc/src/eth/execute.rs | 4 +- client/rpc/src/eth/fee.rs | 2 +- client/rpc/src/eth/filter.rs | 8 +- client/rpc/src/eth/mod.rs | 31 ++++---- client/rpc/src/eth/state.rs | 30 ++++--- client/rpc/src/eth/transaction.rs | 2 +- client/rpc/src/lib.rs | 20 ++--- 12 files changed, 132 insertions(+), 116 deletions(-) diff --git a/client/rpc-core/src/eth.rs b/client/rpc-core/src/eth.rs index 118fda550b..d7b1f75abb 100644 --- a/client/rpc-core/src/eth.rs +++ b/client/rpc-core/src/eth.rs @@ -70,7 +70,7 @@ pub trait EthApi { #[method(name = "eth_getBlockByNumber")] async fn block_by_number( &self, - number: BlockNumber, + number: BlockNumberOrHash, full: bool, ) -> RpcResult>; @@ -82,14 +82,14 @@ pub trait EthApi { #[method(name = "eth_getBlockTransactionCountByNumber")] async fn block_transaction_count_by_number( &self, - number: BlockNumber, + number: BlockNumberOrHash, ) -> RpcResult>; /// Returns the receipts of a block by number or hash. #[method(name = "eth_getBlockReceipts")] async fn block_transaction_receipts( &self, - number: BlockNumber, + number: BlockNumberOrHash, ) -> RpcResult>>; /// Returns the number of uncles in a block with given hash. @@ -98,7 +98,7 @@ pub trait EthApi { /// Returns the number of uncles in a block with given block number. #[method(name = "eth_getUncleCountByBlockNumber")] - fn block_uncles_count_by_number(&self, number: BlockNumber) -> RpcResult; + fn block_uncles_count_by_number(&self, number: BlockNumberOrHash) -> RpcResult; /// Returns an uncles at given block and index. #[method(name = "eth_getUncleByBlockHashAndIndex")] @@ -112,7 +112,7 @@ pub trait EthApi { #[method(name = "eth_getUncleByBlockNumberAndIndex")] fn uncle_by_block_number_and_index( &self, - number: BlockNumber, + number: BlockNumberOrHash, index: Index, ) -> RpcResult>; @@ -136,7 +136,7 @@ pub trait EthApi { #[method(name = "eth_getTransactionByBlockNumberAndIndex")] async fn transaction_by_block_number_and_index( &self, - number: BlockNumber, + number: BlockNumberOrHash, index: Index, ) -> RpcResult>; @@ -150,7 +150,7 @@ pub trait EthApi { /// Returns balance of the given account. #[method(name = "eth_getBalance")] - async fn balance(&self, address: H160, number: Option) -> RpcResult; + async fn balance(&self, address: H160, number: Option) -> RpcResult; /// Returns content of the storage at given address. #[method(name = "eth_getStorageAt")] @@ -158,7 +158,7 @@ pub trait EthApi { &self, address: H160, index: U256, - number: Option, + number: Option, ) -> RpcResult; /// Returns the number of transactions sent from given address at given time (block number). @@ -166,12 +166,12 @@ pub trait EthApi { async fn transaction_count( &self, address: H160, - number: Option, + number: Option, ) -> RpcResult; /// Returns the code at given address at given time (block number). #[method(name = "eth_getCode")] - async fn code_at(&self, address: H160, number: Option) -> RpcResult; + async fn code_at(&self, address: H160, number: Option) -> RpcResult; // ######################################################################## // Execute @@ -182,7 +182,7 @@ pub trait EthApi { async fn call( &self, request: CallRequest, - number: Option, + number: Option, state_overrides: Option>, ) -> RpcResult; @@ -191,7 +191,7 @@ pub trait EthApi { async fn estimate_gas( &self, request: CallRequest, - number: Option, + number: Option, ) -> RpcResult; // ######################################################################## @@ -207,7 +207,7 @@ pub trait EthApi { async fn fee_history( &self, block_count: U256, - newest_block: BlockNumber, + newest_block: BlockNumberOrHash, reward_percentiles: Option>, ) -> RpcResult; diff --git a/client/rpc-core/src/types/block_number.rs b/client/rpc-core/src/types/block_number.rs index 29956257e0..727be106ba 100644 --- a/client/rpc-core/src/types/block_number.rs +++ b/client/rpc-core/src/types/block_number.rs @@ -26,7 +26,7 @@ use serde::{ /// Represents rpc api block number param. #[derive(Copy, Clone, Debug, Eq, PartialEq, Default, Hash)] -pub enum BlockNumber { +pub enum BlockNumberOrHash { /// Hash Hash { /// block hash @@ -51,53 +51,53 @@ pub enum BlockNumber { Finalized, } -impl<'a> Deserialize<'a> for BlockNumber { - fn deserialize(deserializer: D) -> Result +impl<'a> Deserialize<'a> for BlockNumberOrHash { + fn deserialize(deserializer: D) -> Result where D: Deserializer<'a>, { - deserializer.deserialize_any(BlockNumberVisitor) + deserializer.deserialize_any(BlockNumberOrHashVisitor) } } -impl BlockNumber { +impl BlockNumberOrHash { /// Convert block number to min block target. pub fn to_min_block_num(&self) -> Option { match *self { - BlockNumber::Num(ref x) => Some(*x), - BlockNumber::Earliest => Some(0), + BlockNumberOrHash::Num(ref x) => Some(*x), + BlockNumberOrHash::Earliest => Some(0), _ => None, } } } -impl Serialize for BlockNumber { +impl Serialize for BlockNumberOrHash { fn serialize(&self, serializer: S) -> Result where S: Serializer, { match *self { - BlockNumber::Hash { + BlockNumberOrHash::Hash { hash, require_canonical, } => serializer.serialize_str(&format!( "{{ 'hash': '{}', 'requireCanonical': '{}' }}", hash, require_canonical )), - BlockNumber::Num(ref x) => serializer.serialize_str(&format!("0x{:x}", x)), - BlockNumber::Latest => serializer.serialize_str("latest"), - BlockNumber::Earliest => serializer.serialize_str("earliest"), - BlockNumber::Pending => serializer.serialize_str("pending"), - BlockNumber::Safe => serializer.serialize_str("safe"), - BlockNumber::Finalized => serializer.serialize_str("finalized"), + BlockNumberOrHash::Num(ref x) => serializer.serialize_str(&format!("0x{:x}", x)), + BlockNumberOrHash::Latest => serializer.serialize_str("latest"), + BlockNumberOrHash::Earliest => serializer.serialize_str("earliest"), + BlockNumberOrHash::Pending => serializer.serialize_str("pending"), + BlockNumberOrHash::Safe => serializer.serialize_str("safe"), + BlockNumberOrHash::Finalized => serializer.serialize_str("finalized"), } } } -struct BlockNumberVisitor; +struct BlockNumberOrHashVisitor; -impl<'a> Visitor<'a> for BlockNumberVisitor { - type Value = BlockNumber; +impl<'a> Visitor<'a> for BlockNumberOrHashVisitor { + type Value = BlockNumberOrHash; fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result { write!( @@ -146,11 +146,11 @@ impl<'a> Visitor<'a> for BlockNumberVisitor { } if let Some(number) = block_number { - return Ok(BlockNumber::Num(number)); + return Ok(BlockNumberOrHash::Num(number)); } if let Some(hash) = block_hash { - return Ok(BlockNumber::Hash { + return Ok(BlockNumberOrHash::Hash { hash, require_canonical, }); @@ -164,17 +164,22 @@ impl<'a> Visitor<'a> for BlockNumberVisitor { E: Error, { match value { - "latest" => Ok(BlockNumber::Latest), - "earliest" => Ok(BlockNumber::Earliest), - "pending" => Ok(BlockNumber::Pending), - "safe" => Ok(BlockNumber::Safe), - "finalized" => Ok(BlockNumber::Finalized), + "latest" => Ok(BlockNumberOrHash::Latest), + "earliest" => Ok(BlockNumberOrHash::Earliest), + "pending" => Ok(BlockNumberOrHash::Pending), + "safe" => Ok(BlockNumberOrHash::Safe), + "finalized" => Ok(BlockNumberOrHash::Finalized), _ if value.starts_with("0x") => u64::from_str_radix(&value[2..], 16) - .map(BlockNumber::Num) + .map(BlockNumberOrHash::Num) .map_err(|e| Error::custom(format!("Invalid block number: {}", e))), - _ => value.parse::().map(BlockNumber::Num).map_err(|_| { - Error::custom("Invalid block number: non-decimal or missing 0x prefix".to_string()) - }), + _ => value + .parse::() + .map(BlockNumberOrHash::Num) + .map_err(|_| { + Error::custom( + "Invalid block number: non-decimal or missing 0x prefix".to_string(), + ) + }), } } @@ -189,7 +194,7 @@ impl<'a> Visitor<'a> for BlockNumberVisitor { where E: Error, { - Ok(BlockNumber::Num(value)) + Ok(BlockNumberOrHash::Num(value)) } } @@ -197,28 +202,28 @@ impl<'a> Visitor<'a> for BlockNumberVisitor { mod tests { use super::*; - fn match_block_number(block_number: BlockNumber) -> Option { + fn match_block_number(block_number: BlockNumberOrHash) -> Option { match block_number { - BlockNumber::Num(number) => Some(number), - BlockNumber::Earliest => Some(0), - BlockNumber::Latest => Some(1000), - BlockNumber::Safe => Some(999), - BlockNumber::Finalized => Some(999), - BlockNumber::Pending => Some(1001), + BlockNumberOrHash::Num(number) => Some(number), + BlockNumberOrHash::Earliest => Some(0), + BlockNumberOrHash::Latest => Some(1000), + BlockNumberOrHash::Safe => Some(999), + BlockNumberOrHash::Finalized => Some(999), + BlockNumberOrHash::Pending => Some(1001), _ => None, } } #[test] fn block_number_deserialize() { - let bn_dec: BlockNumber = serde_json::from_str(r#""42""#).unwrap(); - let bn_hex: BlockNumber = serde_json::from_str(r#""0x45""#).unwrap(); - let bn_u64: BlockNumber = serde_json::from_str(r#"420"#).unwrap(); - let bn_tag_earliest: BlockNumber = serde_json::from_str(r#""earliest""#).unwrap(); - let bn_tag_latest: BlockNumber = serde_json::from_str(r#""latest""#).unwrap(); - let bn_tag_safe: BlockNumber = serde_json::from_str(r#""safe""#).unwrap(); - let bn_tag_finalized: BlockNumber = serde_json::from_str(r#""finalized""#).unwrap(); - let bn_tag_pending: BlockNumber = serde_json::from_str(r#""pending""#).unwrap(); + let bn_dec: BlockNumberOrHash = serde_json::from_str(r#""42""#).unwrap(); + let bn_hex: BlockNumberOrHash = serde_json::from_str(r#""0x45""#).unwrap(); + let bn_u64: BlockNumberOrHash = serde_json::from_str(r#"420"#).unwrap(); + let bn_tag_earliest: BlockNumberOrHash = serde_json::from_str(r#""earliest""#).unwrap(); + let bn_tag_latest: BlockNumberOrHash = serde_json::from_str(r#""latest""#).unwrap(); + let bn_tag_safe: BlockNumberOrHash = serde_json::from_str(r#""safe""#).unwrap(); + let bn_tag_finalized: BlockNumberOrHash = serde_json::from_str(r#""finalized""#).unwrap(); + let bn_tag_pending: BlockNumberOrHash = serde_json::from_str(r#""pending""#).unwrap(); assert_eq!(match_block_number(bn_dec).unwrap(), 42); assert_eq!(match_block_number(bn_hex).unwrap(), 69); diff --git a/client/rpc-core/src/types/filter.rs b/client/rpc-core/src/types/filter.rs index 4d7dd09a9a..9aca89dd8f 100644 --- a/client/rpc-core/src/types/filter.rs +++ b/client/rpc-core/src/types/filter.rs @@ -28,7 +28,7 @@ use serde::{ }; use serde_json::{from_value, Value}; -use crate::types::{BlockNumber, Log}; +use crate::types::{BlockNumberOrHash, Log}; /// Variadic value #[derive(Clone, Debug, Eq, PartialEq, Hash)] @@ -136,9 +136,9 @@ impl From<&VariadicValue>> for Vec> { #[serde(rename_all = "camelCase")] pub struct Filter { /// From Block - pub from_block: Option, + pub from_block: Option, /// To Block - pub to_block: Option, + pub to_block: Option, /// Block hash pub block_hash: Option, /// Address @@ -327,19 +327,19 @@ impl FilteredParams { pub fn filter_block_range(&self, block_number: u64) -> bool { let mut out = true; let filter = self.filter.clone().unwrap(); - if let Some(BlockNumber::Num(from)) = filter.from_block { + if let Some(BlockNumberOrHash::Num(from)) = filter.from_block { if from > block_number { out = false; } } if let Some(to) = filter.to_block { match to { - BlockNumber::Num(to) => { + BlockNumberOrHash::Num(to) => { if to < block_number { out = false; } } - BlockNumber::Earliest => { + BlockNumberOrHash::Earliest => { out = false; } _ => {} @@ -457,7 +457,7 @@ pub enum FilterType { #[derive(Clone, Debug)] pub struct FilterPoolItem { - pub last_poll: BlockNumber, + pub last_poll: BlockNumberOrHash, pub filter_type: FilterType, pub at_block: u64, pub pending_transaction_hashes: HashSet, diff --git a/client/rpc-core/src/types/mod.rs b/client/rpc-core/src/types/mod.rs index e5a7981cf1..70b3223959 100644 --- a/client/rpc-core/src/types/mod.rs +++ b/client/rpc-core/src/types/mod.rs @@ -44,7 +44,7 @@ pub use self::txpool::{Get, Summary, TransactionMap, TxPoolResult, TxPoolTransac pub use self::{ account_info::{AccountInfo, EthAccount, ExtAccountInfo, RecoveredAccount, StorageProof}, block::{Block, BlockTransactions, Header, Rich, RichBlock, RichHeader}, - block_number::BlockNumber, + block_number::BlockNumberOrHash, bytes::Bytes, call_request::{CallRequest, CallStateOverride}, fee::{FeeHistory, FeeHistoryCache, FeeHistoryCacheItem, FeeHistoryCacheLimit}, diff --git a/client/rpc/src/eth/block.rs b/client/rpc/src/eth/block.rs index 90939bc26c..39c3e84691 100644 --- a/client/rpc/src/eth/block.rs +++ b/client/rpc/src/eth/block.rs @@ -83,7 +83,7 @@ where pub async fn block_by_number( &self, - number: BlockNumber, + number: BlockNumberOrHash, full: bool, ) -> RpcResult> { let client = Arc::clone(&self.client); @@ -138,7 +138,7 @@ where _ => Ok(None), } } - None if number == BlockNumber::Pending => { + None if number == BlockNumberOrHash::Pending => { let api = client.runtime_api(); let best_hash = client.info().best_hash; @@ -195,9 +195,9 @@ where pub async fn block_transaction_count_by_number( &self, - number: BlockNumber, + number: BlockNumberOrHash, ) -> RpcResult> { - if let BlockNumber::Pending = number { + if let BlockNumberOrHash::Pending = number { // get the pending transactions count return Ok(Some(U256::from( self.graph.validated_pool().ready().count(), @@ -213,7 +213,7 @@ where pub async fn block_transaction_receipts( &self, - number: BlockNumber, + number: BlockNumberOrHash, ) -> RpcResult>> { let block_info = self.block_info_by_number(number).await?; let Some(statuses) = block_info.clone().statuses else { @@ -238,7 +238,7 @@ where Ok(U256::zero()) } - pub fn block_uncles_count_by_number(&self, _: BlockNumber) -> RpcResult { + pub fn block_uncles_count_by_number(&self, _: BlockNumberOrHash) -> RpcResult { Ok(U256::zero()) } @@ -248,7 +248,7 @@ where pub fn uncle_by_block_number_and_index( &self, - _: BlockNumber, + _: BlockNumberOrHash, _: Index, ) -> RpcResult> { Ok(None) diff --git a/client/rpc/src/eth/execute.rs b/client/rpc/src/eth/execute.rs index 00bd6656b2..abf92454de 100644 --- a/client/rpc/src/eth/execute.rs +++ b/client/rpc/src/eth/execute.rs @@ -76,7 +76,7 @@ where pub async fn call( &self, request: CallRequest, - number: Option, + number: Option, state_overrides: Option>, ) -> RpcResult { let CallRequest { @@ -408,7 +408,7 @@ where pub async fn estimate_gas( &self, request: CallRequest, - number: Option, + number: Option, ) -> RpcResult { let client = Arc::clone(&self.client); let block_data_cache = Arc::clone(&self.block_data_cache); diff --git a/client/rpc/src/eth/fee.rs b/client/rpc/src/eth/fee.rs index 92b0ee10c2..2abf78e544 100644 --- a/client/rpc/src/eth/fee.rs +++ b/client/rpc/src/eth/fee.rs @@ -53,7 +53,7 @@ where pub async fn fee_history( &self, block_count: U256, - newest_block: BlockNumber, + newest_block: BlockNumberOrHash, reward_percentiles: Option>, ) -> RpcResult { // The max supported range size is 1024 by spec. diff --git a/client/rpc/src/eth/filter.rs b/client/rpc/src/eth/filter.rs index 8a0f484173..4c609db540 100644 --- a/client/rpc/src/eth/filter.rs +++ b/client/rpc/src/eth/filter.rs @@ -130,7 +130,7 @@ where locked.insert( key, FilterPoolItem { - last_poll: BlockNumber::Num(best_number), + last_poll: BlockNumberOrHash::Num(best_number), filter_type, at_block: best_number, pending_transaction_hashes, @@ -209,7 +209,7 @@ where locked.insert( key, FilterPoolItem { - last_poll: BlockNumber::Num(next), + last_poll: BlockNumberOrHash::Num(next), filter_type: pool_item.filter_type.clone(), at_block: pool_item.at_block, pending_transaction_hashes: HashSet::new(), @@ -241,7 +241,7 @@ where locked.insert( key, FilterPoolItem { - last_poll: BlockNumber::Num(best_number + 1), + last_poll: BlockNumberOrHash::Num(best_number + 1), filter_type: pool_item.filter_type.clone(), at_block: pool_item.at_block, pending_transaction_hashes: current_hashes.clone(), @@ -261,7 +261,7 @@ where locked.insert( key, FilterPoolItem { - last_poll: BlockNumber::Num(best_number + 1), + last_poll: BlockNumberOrHash::Num(best_number + 1), filter_type: pool_item.filter_type.clone(), at_block: pool_item.at_block, pending_transaction_hashes: HashSet::new(), diff --git a/client/rpc/src/eth/mod.rs b/client/rpc/src/eth/mod.rs index 14b2d896e3..78dcd2ce50 100644 --- a/client/rpc/src/eth/mod.rs +++ b/client/rpc/src/eth/mod.rs @@ -136,7 +136,10 @@ where } } - pub async fn block_info_by_number(&self, number: BlockNumber) -> RpcResult> { + pub async fn block_info_by_number( + &self, + number: BlockNumberOrHash, + ) -> RpcResult> { let id = match frontier_backend_client::native_block_id::( self.client.as_ref(), self.backend.as_ref(), @@ -339,7 +342,7 @@ where async fn block_by_number( &self, - number: BlockNumber, + number: BlockNumberOrHash, full: bool, ) -> RpcResult> { self.block_by_number(number, full).await @@ -351,14 +354,14 @@ where async fn block_transaction_count_by_number( &self, - number: BlockNumber, + number: BlockNumberOrHash, ) -> RpcResult> { self.block_transaction_count_by_number(number).await } async fn block_transaction_receipts( &self, - number: BlockNumber, + number: BlockNumberOrHash, ) -> RpcResult>> { self.block_transaction_receipts(number).await } @@ -367,7 +370,7 @@ where self.block_uncles_count_by_hash(hash) } - fn block_uncles_count_by_number(&self, number: BlockNumber) -> RpcResult { + fn block_uncles_count_by_number(&self, number: BlockNumberOrHash) -> RpcResult { self.block_uncles_count_by_number(number) } @@ -381,7 +384,7 @@ where fn uncle_by_block_number_and_index( &self, - number: BlockNumber, + number: BlockNumberOrHash, index: Index, ) -> RpcResult> { self.uncle_by_block_number_and_index(number, index) @@ -405,7 +408,7 @@ where async fn transaction_by_block_number_and_index( &self, - number: BlockNumber, + number: BlockNumberOrHash, index: Index, ) -> RpcResult> { self.transaction_by_block_number_and_index(number, index) @@ -421,7 +424,7 @@ where // State // ######################################################################## - async fn balance(&self, address: H160, number: Option) -> RpcResult { + async fn balance(&self, address: H160, number: Option) -> RpcResult { self.balance(address, number).await } @@ -429,7 +432,7 @@ where &self, address: H160, index: U256, - number: Option, + number: Option, ) -> RpcResult { self.storage_at(address, index, number).await } @@ -437,12 +440,12 @@ where async fn transaction_count( &self, address: H160, - number: Option, + number: Option, ) -> RpcResult { self.transaction_count(address, number).await } - async fn code_at(&self, address: H160, number: Option) -> RpcResult { + async fn code_at(&self, address: H160, number: Option) -> RpcResult { self.code_at(address, number).await } @@ -453,7 +456,7 @@ where async fn call( &self, request: CallRequest, - number: Option, + number: Option, state_overrides: Option>, ) -> RpcResult { self.call(request, number, state_overrides).await @@ -462,7 +465,7 @@ where async fn estimate_gas( &self, request: CallRequest, - number: Option, + number: Option, ) -> RpcResult { self.estimate_gas(request, number).await } @@ -478,7 +481,7 @@ where async fn fee_history( &self, block_count: U256, - newest_block: BlockNumber, + newest_block: BlockNumberOrHash, reward_percentiles: Option>, ) -> RpcResult { self.fee_history(block_count, newest_block, reward_percentiles) diff --git a/client/rpc/src/eth/state.rs b/client/rpc/src/eth/state.rs index 1f7f00ca5e..0da91f3f18 100644 --- a/client/rpc/src/eth/state.rs +++ b/client/rpc/src/eth/state.rs @@ -46,9 +46,13 @@ where P: TransactionPool + 'static, A: ChainApi + 'static, { - pub async fn balance(&self, address: H160, number: Option) -> RpcResult { - let number = number.unwrap_or(BlockNumber::Latest); - if number == BlockNumber::Pending { + pub async fn balance( + &self, + address: H160, + number: Option, + ) -> RpcResult { + let number = number.unwrap_or(BlockNumberOrHash::Latest); + if number == BlockNumberOrHash::Pending { let api = pending_runtime_api(self.client.as_ref(), self.graph.as_ref())?; Ok(api .account_basic(self.client.info().best_hash, address) @@ -81,10 +85,10 @@ where &self, address: H160, index: U256, - number: Option, + number: Option, ) -> RpcResult { - let number = number.unwrap_or(BlockNumber::Latest); - if number == BlockNumber::Pending { + let number = number.unwrap_or(BlockNumberOrHash::Latest); + if number == BlockNumberOrHash::Pending { let api = pending_runtime_api(self.client.as_ref(), self.graph.as_ref())?; Ok(api .storage_at(self.client.info().best_hash, address, index) @@ -116,9 +120,9 @@ where pub async fn transaction_count( &self, address: H160, - number: Option, + number: Option, ) -> RpcResult { - if let Some(BlockNumber::Pending) = number { + if let Some(BlockNumberOrHash::Pending) = number { let substrate_hash = self.client.info().best_hash; let nonce = self @@ -168,9 +172,13 @@ where .nonce) } - pub async fn code_at(&self, address: H160, number: Option) -> RpcResult { - let number = number.unwrap_or(BlockNumber::Latest); - if number == BlockNumber::Pending { + pub async fn code_at( + &self, + address: H160, + number: Option, + ) -> RpcResult { + let number = number.unwrap_or(BlockNumberOrHash::Latest); + if number == BlockNumberOrHash::Pending { let api = pending_runtime_api(self.client.as_ref(), self.graph.as_ref())?; Ok(api .account_code_at(self.client.info().best_hash, address) diff --git a/client/rpc/src/eth/transaction.rs b/client/rpc/src/eth/transaction.rs index cfd23c3c51..f7e7ad9789 100644 --- a/client/rpc/src/eth/transaction.rs +++ b/client/rpc/src/eth/transaction.rs @@ -173,7 +173,7 @@ where pub async fn transaction_by_block_number_and_index( &self, - number: BlockNumber, + number: BlockNumberOrHash, index: Index, ) -> RpcResult> { let index = index.value(); diff --git a/client/rpc/src/lib.rs b/client/rpc/src/lib.rs index 6d52fb4d68..c03bfc13e4 100644 --- a/client/rpc/src/lib.rs +++ b/client/rpc/src/lib.rs @@ -73,7 +73,7 @@ pub mod frontier_backend_client { }; use sp_state_machine::OverlayedChanges; // Frontier - use fc_rpc_core::types::BlockNumber; + use fc_rpc_core::types::BlockNumberOrHash; /// Implements a default runtime storage override. /// It assumes that the balances and nonces are stored in pallet `system.account`, and @@ -188,26 +188,26 @@ pub mod frontier_backend_client { pub async fn native_block_id( client: &C, backend: &dyn fc_api::Backend, - number: Option, + number: Option, ) -> RpcResult>> where B: BlockT, C: HeaderBackend + 'static, { - Ok(match number.unwrap_or(BlockNumber::Latest) { - BlockNumber::Hash { hash, .. } => { + Ok(match number.unwrap_or(BlockNumberOrHash::Latest) { + BlockNumberOrHash::Hash { hash, .. } => { if let Ok(Some(hash)) = load_hash::(client, backend, hash).await { Some(BlockId::Hash(hash)) } else { None } } - BlockNumber::Num(number) => Some(BlockId::Number(number.unique_saturated_into())), - BlockNumber::Latest => Some(BlockId::Hash(client.info().best_hash)), - BlockNumber::Earliest => Some(BlockId::Number(Zero::zero())), - BlockNumber::Pending => None, - BlockNumber::Safe => Some(BlockId::Hash(client.info().finalized_hash)), - BlockNumber::Finalized => Some(BlockId::Hash(client.info().finalized_hash)), + BlockNumberOrHash::Num(number) => Some(BlockId::Number(number.unique_saturated_into())), + BlockNumberOrHash::Latest => Some(BlockId::Hash(client.info().best_hash)), + BlockNumberOrHash::Earliest => Some(BlockId::Number(Zero::zero())), + BlockNumberOrHash::Pending => None, + BlockNumberOrHash::Safe => Some(BlockId::Hash(client.info().finalized_hash)), + BlockNumberOrHash::Finalized => Some(BlockId::Hash(client.info().finalized_hash)), }) } From cb4559ddfc7643b0d768fe4a5040d6d262c0bcfc Mon Sep 17 00:00:00 2001 From: bear Date: Fri, 25 Aug 2023 16:25:40 +0800 Subject: [PATCH 2/2] Rename function param --- client/rpc-core/src/eth.rs | 32 ++++++++++++------- client/rpc/src/eth/block.rs | 16 +++++----- client/rpc/src/eth/execute.rs | 8 ++--- client/rpc/src/eth/mod.rs | 60 ++++++++++++++++++++--------------- client/rpc/src/eth/state.rs | 30 +++++++++--------- 5 files changed, 81 insertions(+), 65 deletions(-) diff --git a/client/rpc-core/src/eth.rs b/client/rpc-core/src/eth.rs index d7b1f75abb..0a9db1a182 100644 --- a/client/rpc-core/src/eth.rs +++ b/client/rpc-core/src/eth.rs @@ -70,7 +70,7 @@ pub trait EthApi { #[method(name = "eth_getBlockByNumber")] async fn block_by_number( &self, - number: BlockNumberOrHash, + number_or_hash: BlockNumberOrHash, full: bool, ) -> RpcResult>; @@ -82,14 +82,14 @@ pub trait EthApi { #[method(name = "eth_getBlockTransactionCountByNumber")] async fn block_transaction_count_by_number( &self, - number: BlockNumberOrHash, + number_or_hash: BlockNumberOrHash, ) -> RpcResult>; /// Returns the receipts of a block by number or hash. #[method(name = "eth_getBlockReceipts")] async fn block_transaction_receipts( &self, - number: BlockNumberOrHash, + number_or_hash: BlockNumberOrHash, ) -> RpcResult>>; /// Returns the number of uncles in a block with given hash. @@ -98,7 +98,7 @@ pub trait EthApi { /// Returns the number of uncles in a block with given block number. #[method(name = "eth_getUncleCountByBlockNumber")] - fn block_uncles_count_by_number(&self, number: BlockNumberOrHash) -> RpcResult; + fn block_uncles_count_by_number(&self, number_or_hash: BlockNumberOrHash) -> RpcResult; /// Returns an uncles at given block and index. #[method(name = "eth_getUncleByBlockHashAndIndex")] @@ -112,7 +112,7 @@ pub trait EthApi { #[method(name = "eth_getUncleByBlockNumberAndIndex")] fn uncle_by_block_number_and_index( &self, - number: BlockNumberOrHash, + number_or_hash: BlockNumberOrHash, index: Index, ) -> RpcResult>; @@ -136,7 +136,7 @@ pub trait EthApi { #[method(name = "eth_getTransactionByBlockNumberAndIndex")] async fn transaction_by_block_number_and_index( &self, - number: BlockNumberOrHash, + number_or_hash: BlockNumberOrHash, index: Index, ) -> RpcResult>; @@ -150,7 +150,11 @@ pub trait EthApi { /// Returns balance of the given account. #[method(name = "eth_getBalance")] - async fn balance(&self, address: H160, number: Option) -> RpcResult; + async fn balance( + &self, + address: H160, + number_or_hash: Option, + ) -> RpcResult; /// Returns content of the storage at given address. #[method(name = "eth_getStorageAt")] @@ -158,7 +162,7 @@ pub trait EthApi { &self, address: H160, index: U256, - number: Option, + number_or_hash: Option, ) -> RpcResult; /// Returns the number of transactions sent from given address at given time (block number). @@ -166,12 +170,16 @@ pub trait EthApi { async fn transaction_count( &self, address: H160, - number: Option, + number_or_hash: Option, ) -> RpcResult; /// Returns the code at given address at given time (block number). #[method(name = "eth_getCode")] - async fn code_at(&self, address: H160, number: Option) -> RpcResult; + async fn code_at( + &self, + address: H160, + number_or_hash: Option, + ) -> RpcResult; // ######################################################################## // Execute @@ -182,7 +190,7 @@ pub trait EthApi { async fn call( &self, request: CallRequest, - number: Option, + number_or_hash: Option, state_overrides: Option>, ) -> RpcResult; @@ -191,7 +199,7 @@ pub trait EthApi { async fn estimate_gas( &self, request: CallRequest, - number: Option, + number_or_hash: Option, ) -> RpcResult; // ######################################################################## diff --git a/client/rpc/src/eth/block.rs b/client/rpc/src/eth/block.rs index 39c3e84691..1e64d6020f 100644 --- a/client/rpc/src/eth/block.rs +++ b/client/rpc/src/eth/block.rs @@ -83,7 +83,7 @@ where pub async fn block_by_number( &self, - number: BlockNumberOrHash, + number_or_hash: BlockNumberOrHash, full: bool, ) -> RpcResult> { let client = Arc::clone(&self.client); @@ -94,7 +94,7 @@ where match frontier_backend_client::native_block_id::( client.as_ref(), backend.as_ref(), - Some(number), + Some(number_or_hash), ) .await? { @@ -138,7 +138,7 @@ where _ => Ok(None), } } - None if number == BlockNumberOrHash::Pending => { + None if number_or_hash == BlockNumberOrHash::Pending => { let api = client.runtime_api(); let best_hash = client.info().best_hash; @@ -195,16 +195,16 @@ where pub async fn block_transaction_count_by_number( &self, - number: BlockNumberOrHash, + number_or_hash: BlockNumberOrHash, ) -> RpcResult> { - if let BlockNumberOrHash::Pending = number { + if let BlockNumberOrHash::Pending = number_or_hash { // get the pending transactions count return Ok(Some(U256::from( self.graph.validated_pool().ready().count(), ))); } - let block_info = self.block_info_by_number(number).await?; + let block_info = self.block_info_by_number(number_or_hash).await?; match block_info.block { Some(block) => Ok(Some(U256::from(block.transactions.len()))), None => Ok(None), @@ -213,9 +213,9 @@ where pub async fn block_transaction_receipts( &self, - number: BlockNumberOrHash, + number_or_hash: BlockNumberOrHash, ) -> RpcResult>> { - let block_info = self.block_info_by_number(number).await?; + let block_info = self.block_info_by_number(number_or_hash).await?; let Some(statuses) = block_info.clone().statuses else { return Ok(None); }; diff --git a/client/rpc/src/eth/execute.rs b/client/rpc/src/eth/execute.rs index abf92454de..82415b1fc8 100644 --- a/client/rpc/src/eth/execute.rs +++ b/client/rpc/src/eth/execute.rs @@ -76,7 +76,7 @@ where pub async fn call( &self, request: CallRequest, - number: Option, + number_or_hash: Option, state_overrides: Option>, ) -> RpcResult { let CallRequest { @@ -105,7 +105,7 @@ where let (substrate_hash, api) = match frontier_backend_client::native_block_id::( self.client.as_ref(), self.backend.as_ref(), - number, + number_or_hash, ) .await? { @@ -408,7 +408,7 @@ where pub async fn estimate_gas( &self, request: CallRequest, - number: Option, + number_or_hash: Option, ) -> RpcResult { let client = Arc::clone(&self.client); let block_data_cache = Arc::clone(&self.block_data_cache); @@ -420,7 +420,7 @@ where let (substrate_hash, api) = match frontier_backend_client::native_block_id::( self.client.as_ref(), self.backend.as_ref(), - number, + number_or_hash, ) .await? { diff --git a/client/rpc/src/eth/mod.rs b/client/rpc/src/eth/mod.rs index 78dcd2ce50..d182356ee5 100644 --- a/client/rpc/src/eth/mod.rs +++ b/client/rpc/src/eth/mod.rs @@ -138,12 +138,12 @@ where pub async fn block_info_by_number( &self, - number: BlockNumberOrHash, + number_or_hash: BlockNumberOrHash, ) -> RpcResult> { let id = match frontier_backend_client::native_block_id::( self.client.as_ref(), self.backend.as_ref(), - Some(number), + Some(number_or_hash), ) .await? { @@ -342,10 +342,10 @@ where async fn block_by_number( &self, - number: BlockNumberOrHash, + number_or_hash: BlockNumberOrHash, full: bool, ) -> RpcResult> { - self.block_by_number(number, full).await + self.block_by_number(number_or_hash, full).await } async fn block_transaction_count_by_hash(&self, hash: H256) -> RpcResult> { @@ -354,24 +354,24 @@ where async fn block_transaction_count_by_number( &self, - number: BlockNumberOrHash, + number_or_hash: BlockNumberOrHash, ) -> RpcResult> { - self.block_transaction_count_by_number(number).await + self.block_transaction_count_by_number(number_or_hash).await } async fn block_transaction_receipts( &self, - number: BlockNumberOrHash, + number_or_hash: BlockNumberOrHash, ) -> RpcResult>> { - self.block_transaction_receipts(number).await + self.block_transaction_receipts(number_or_hash).await } fn block_uncles_count_by_hash(&self, hash: H256) -> RpcResult { self.block_uncles_count_by_hash(hash) } - fn block_uncles_count_by_number(&self, number: BlockNumberOrHash) -> RpcResult { - self.block_uncles_count_by_number(number) + fn block_uncles_count_by_number(&self, number_or_hash: BlockNumberOrHash) -> RpcResult { + self.block_uncles_count_by_number(number_or_hash) } fn uncle_by_block_hash_and_index( @@ -384,10 +384,10 @@ where fn uncle_by_block_number_and_index( &self, - number: BlockNumberOrHash, + number_or_hash: BlockNumberOrHash, index: Index, ) -> RpcResult> { - self.uncle_by_block_number_and_index(number, index) + self.uncle_by_block_number_and_index(number_or_hash, index) } // ######################################################################## @@ -408,10 +408,10 @@ where async fn transaction_by_block_number_and_index( &self, - number: BlockNumberOrHash, + number_or_hash: BlockNumberOrHash, index: Index, ) -> RpcResult> { - self.transaction_by_block_number_and_index(number, index) + self.transaction_by_block_number_and_index(number_or_hash, index) .await } @@ -424,29 +424,37 @@ where // State // ######################################################################## - async fn balance(&self, address: H160, number: Option) -> RpcResult { - self.balance(address, number).await + async fn balance( + &self, + address: H160, + number_or_hash: Option, + ) -> RpcResult { + self.balance(address, number_or_hash).await } async fn storage_at( &self, address: H160, index: U256, - number: Option, + number_or_hash: Option, ) -> RpcResult { - self.storage_at(address, index, number).await + self.storage_at(address, index, number_or_hash).await } async fn transaction_count( &self, address: H160, - number: Option, + number_or_hash: Option, ) -> RpcResult { - self.transaction_count(address, number).await + self.transaction_count(address, number_or_hash).await } - async fn code_at(&self, address: H160, number: Option) -> RpcResult { - self.code_at(address, number).await + async fn code_at( + &self, + address: H160, + number_or_hash: Option, + ) -> RpcResult { + self.code_at(address, number_or_hash).await } // ######################################################################## @@ -456,18 +464,18 @@ where async fn call( &self, request: CallRequest, - number: Option, + number_or_hash: Option, state_overrides: Option>, ) -> RpcResult { - self.call(request, number, state_overrides).await + self.call(request, number_or_hash, state_overrides).await } async fn estimate_gas( &self, request: CallRequest, - number: Option, + number_or_hash: Option, ) -> RpcResult { - self.estimate_gas(request, number).await + self.estimate_gas(request, number_or_hash).await } // ######################################################################## diff --git a/client/rpc/src/eth/state.rs b/client/rpc/src/eth/state.rs index 0da91f3f18..90e34f8d6d 100644 --- a/client/rpc/src/eth/state.rs +++ b/client/rpc/src/eth/state.rs @@ -49,10 +49,10 @@ where pub async fn balance( &self, address: H160, - number: Option, + number_or_hash: Option, ) -> RpcResult { - let number = number.unwrap_or(BlockNumberOrHash::Latest); - if number == BlockNumberOrHash::Pending { + let number_or_hash = number_or_hash.unwrap_or(BlockNumberOrHash::Latest); + if number_or_hash == BlockNumberOrHash::Pending { let api = pending_runtime_api(self.client.as_ref(), self.graph.as_ref())?; Ok(api .account_basic(self.client.info().best_hash, address) @@ -61,7 +61,7 @@ where } else if let Ok(Some(id)) = frontier_backend_client::native_block_id::( self.client.as_ref(), self.backend.as_ref(), - Some(number), + Some(number_or_hash), ) .await { @@ -85,10 +85,10 @@ where &self, address: H160, index: U256, - number: Option, + number_or_hash: Option, ) -> RpcResult { - let number = number.unwrap_or(BlockNumberOrHash::Latest); - if number == BlockNumberOrHash::Pending { + let number_or_hash = number_or_hash.unwrap_or(BlockNumberOrHash::Latest); + if number_or_hash == BlockNumberOrHash::Pending { let api = pending_runtime_api(self.client.as_ref(), self.graph.as_ref())?; Ok(api .storage_at(self.client.info().best_hash, address, index) @@ -96,7 +96,7 @@ where } else if let Ok(Some(id)) = frontier_backend_client::native_block_id::( self.client.as_ref(), self.backend.as_ref(), - Some(number), + Some(number_or_hash), ) .await { @@ -120,9 +120,9 @@ where pub async fn transaction_count( &self, address: H160, - number: Option, + number_or_hash: Option, ) -> RpcResult { - if let Some(BlockNumberOrHash::Pending) = number { + if let Some(BlockNumberOrHash::Pending) = number_or_hash { let substrate_hash = self.client.info().best_hash; let nonce = self @@ -151,7 +151,7 @@ where let id = match frontier_backend_client::native_block_id::( self.client.as_ref(), self.backend.as_ref(), - number, + number_or_hash, ) .await? { @@ -175,10 +175,10 @@ where pub async fn code_at( &self, address: H160, - number: Option, + number_or_hash: Option, ) -> RpcResult { - let number = number.unwrap_or(BlockNumberOrHash::Latest); - if number == BlockNumberOrHash::Pending { + let number_or_hash = number_or_hash.unwrap_or(BlockNumberOrHash::Latest); + if number_or_hash == BlockNumberOrHash::Pending { let api = pending_runtime_api(self.client.as_ref(), self.graph.as_ref())?; Ok(api .account_code_at(self.client.info().best_hash, address) @@ -187,7 +187,7 @@ where } else if let Ok(Some(id)) = frontier_backend_client::native_block_id::( self.client.as_ref(), self.backend.as_ref(), - Some(number), + Some(number_or_hash), ) .await {