From f767a997f24b21f396ce660e12507f4fa064b8bb Mon Sep 17 00:00:00 2001 From: evalir Date: Mon, 18 Dec 2023 10:22:38 -0400 Subject: [PATCH] chore: address provider review comments (#87) * chore: remove self: sync from defs * chore: remove most generics * chore: default for BlockId * chore: remove unnecesary reassignment * chore: auto impl on mut/rc * chore: unnecesary assignment --- crates/providers/src/provider.rs | 196 ++++++++---------------------- crates/rpc-types/src/eth/block.rs | 6 + 2 files changed, 56 insertions(+), 146 deletions(-) diff --git a/crates/providers/src/provider.rs b/crates/providers/src/provider.rs index 78865849a05..27f721a1855 100644 --- a/crates/providers/src/provider.rs +++ b/crates/providers/src/provider.rs @@ -38,26 +38,20 @@ pub struct Provider { /// Once the new Provider trait is stable, this trait will be removed. #[cfg_attr(target_arch = "wasm32", async_trait::async_trait(?Send))] #[cfg_attr(not(target_arch = "wasm32"), async_trait::async_trait)] -#[auto_impl(&, Arc, Box)] +#[auto_impl(&, &mut, Rc, Arc, Box)] pub trait TempProvider: Send + Sync { /// Gets the transaction count of the corresponding address. async fn get_transaction_count( &self, address: Address, tag: Option, - ) -> TransportResult - where - Self: Sync; + ) -> TransportResult; /// Gets the last block number available. - async fn get_block_number(&self) -> TransportResult - where - Self: Sync; + async fn get_block_number(&self) -> TransportResult; /// Gets the balance of the account at the specified tag, which defaults to latest. - async fn get_balance(&self, address: Address, tag: Option) -> TransportResult - where - Self: Sync; + async fn get_balance(&self, address: Address, tag: Option) -> TransportResult; /// Gets a block by either its hash, tag, or number, with full transactions or only hashes. async fn get_block(&self, id: BlockId, full: bool) -> TransportResult> { @@ -72,23 +66,17 @@ pub trait TempProvider: Send + Sync { &self, hash: BlockHash, full: bool, - ) -> TransportResult> - where - Self: Sync; + ) -> TransportResult>; /// Gets a block by [BlockNumberOrTag], with full transactions or only hashes. - async fn get_block_by_number + Send + Sync>( + async fn get_block_by_number( &self, - number: B, + number: BlockNumberOrTag, full: bool, - ) -> TransportResult> - where - Self: Sync; + ) -> TransportResult>; /// Gets the chain ID. - async fn get_chain_id(&self) -> TransportResult - where - Self: Sync; + async fn get_chain_id(&self) -> TransportResult; /// Gets the specified storage value from [Address]. async fn get_storage_at( @@ -99,90 +87,56 @@ pub trait TempProvider: Send + Sync { ) -> TransportResult; /// Gets the bytecode located at the corresponding [Address]. - async fn get_code_at + Send + Sync>( - &self, - address: Address, - tag: B, - ) -> TransportResult - where - Self: Sync; + async fn get_code_at(&self, address: Address, tag: BlockId) -> TransportResult; /// Gets a [Transaction] by its [TxHash]. - async fn get_transaction_by_hash(&self, hash: TxHash) -> TransportResult - where - Self: Sync; + async fn get_transaction_by_hash(&self, hash: TxHash) -> TransportResult; /// Retrieves a [`Vec`] with the given [Filter]. - async fn get_logs(&self, filter: Filter) -> TransportResult> - where - Self: Sync; + async fn get_logs(&self, filter: Filter) -> TransportResult>; /// Gets the accounts in the remote node. This is usually empty unless you're using a local /// node. - async fn get_accounts(&self) -> TransportResult> - where - Self: Sync; + async fn get_accounts(&self) -> TransportResult>; /// Gets the current gas price. - async fn get_gas_price(&self) -> TransportResult - where - Self: Sync; + async fn get_gas_price(&self) -> TransportResult; /// Gets a [TransactionReceipt] if it exists, by its [TxHash]. async fn get_transaction_receipt( &self, hash: TxHash, - ) -> TransportResult> - where - Self: Sync; + ) -> TransportResult>; /// Returns a collection of historical gas information [FeeHistory] which /// can be used to calculate the EIP1559 fields `maxFeePerGas` and `maxPriorityFeePerGas`. - async fn get_fee_history + Send + Sync>( + async fn get_fee_history( &self, block_count: U256, - last_block: B, + last_block: BlockNumberOrTag, reward_percentiles: &[f64], - ) -> TransportResult - where - Self: Sync; + ) -> TransportResult; /// Gets the selected block [BlockNumberOrTag] receipts. async fn get_block_receipts( &self, block: BlockNumberOrTag, - ) -> TransportResult> - where - Self: Sync; + ) -> TransportResult>; /// Gets an uncle block through the tag [BlockId] and index [U64]. - async fn get_uncle + Send + Sync>( - &self, - tag: B, - idx: U64, - ) -> TransportResult> - where - Self: Sync; + async fn get_uncle(&self, tag: BlockId, idx: U64) -> TransportResult>; /// Gets syncing info. - async fn syncing(&self) -> TransportResult - where - Self: Sync; + async fn syncing(&self) -> TransportResult; /// Execute a smart contract call with [CallRequest] without publishing a transaction. - async fn call(&self, tx: CallRequest, block: Option) -> TransportResult - where - Self: Sync; + async fn call(&self, tx: CallRequest, block: Option) -> TransportResult; /// Estimate the gas needed for a transaction. - async fn estimate_gas(&self, tx: CallRequest, block: Option) -> TransportResult - where - Self: Sync; + async fn estimate_gas(&self, tx: CallRequest, block: Option) -> TransportResult; /// Sends an already-signed transaction. - async fn send_raw_transaction(&self, tx: Bytes) -> TransportResult - where - Self: Sync; + async fn send_raw_transaction(&self, tx: Bytes) -> TransportResult; /// Estimates the EIP1559 `maxFeePerGas` and `maxPriorityFeePerGas` fields. /// Receives an optional [EstimatorFunction] that can be used to modify @@ -190,54 +144,40 @@ pub trait TempProvider: Send + Sync { async fn estimate_eip1559_fees( &self, estimator: Option, - ) -> TransportResult<(U256, U256)> - where - Self: Sync; + ) -> TransportResult<(U256, U256)>; #[cfg(feature = "anvil")] - async fn set_code(&self, address: Address, code: &'static str) -> TransportResult<()> - where - Self: Sync; + async fn set_code(&self, address: Address, code: &'static str) -> TransportResult<()>; async fn get_proof( &self, address: Address, keys: Vec, block: Option, - ) -> TransportResult - where - Self: Sync; + ) -> TransportResult; async fn create_access_list( &self, request: CallRequest, block: Option, - ) -> TransportResult - where - Self: Sync; + ) -> TransportResult; /// Parity trace transaction. async fn trace_transaction( &self, hash: TxHash, - ) -> TransportResult> - where - Self: Sync; + ) -> TransportResult>; async fn debug_trace_transaction( &self, hash: TxHash, trace_options: GethDebugTracingOptions, - ) -> TransportResult - where - Self: Sync; + ) -> TransportResult; async fn trace_block( &self, block: BlockNumberOrTag, - ) -> TransportResult> - where - Self: Sync; + ) -> TransportResult>; async fn raw_request(&self, method: &'static str, params: P) -> TransportResult where @@ -317,12 +257,12 @@ impl TempProvider for Provider { } /// Gets a block by [BlockNumberOrTag], with full transactions or only hashes. - async fn get_block_by_number + Send + Sync>( + async fn get_block_by_number( &self, - number: B, + number: BlockNumberOrTag, full: bool, ) -> TransportResult> { - self.inner.prepare("eth_getBlockByNumber", (number.into(), full)).await + self.inner.prepare("eth_getBlockByNumber", (number, full)).await } /// Gets the chain ID. @@ -346,12 +286,8 @@ impl TempProvider for Provider { } /// Gets the bytecode located at the corresponding [Address]. - async fn get_code_at + Send + Sync>( - &self, - address: Address, - tag: B, - ) -> TransportResult { - self.inner.prepare("eth_getCode", (address, tag.into())).await + async fn get_code_at(&self, address: Address, tag: BlockId) -> TransportResult { + self.inner.prepare("eth_getCode", (address, tag)).await } /// Gets a [Transaction] by its [TxHash]. @@ -385,15 +321,13 @@ impl TempProvider for Provider { /// Returns a collection of historical gas information [FeeHistory] which /// can be used to calculate the EIP1559 fields `maxFeePerGas` and `maxPriorityFeePerGas`. - async fn get_fee_history + Send + Sync>( + async fn get_fee_history( &self, block_count: U256, - last_block: B, + last_block: BlockNumberOrTag, reward_percentiles: &[f64], ) -> TransportResult { - self.inner - .prepare("eth_feeHistory", (block_count, last_block.into(), reward_percentiles)) - .await + self.inner.prepare("eth_feeHistory", (block_count, last_block, reward_percentiles)).await } /// Gets the selected block [BlockNumberOrTag] receipts. @@ -401,19 +335,12 @@ impl TempProvider for Provider { &self, block: BlockNumberOrTag, ) -> TransportResult> - where - Self: Sync, - { +where { self.inner.prepare("eth_getBlockReceipts", block).await } /// Gets an uncle block through the tag [BlockId] and index [U64]. - async fn get_uncle + Send + Sync>( - &self, - tag: B, - idx: U64, - ) -> TransportResult> { - let tag = tag.into(); + async fn get_uncle(&self, tag: BlockId, idx: U64) -> TransportResult> { match tag { BlockId::Hash(hash) => { self.inner.prepare("eth_getUncleByBlockHashAndIndex", (hash, idx)).await @@ -431,27 +358,20 @@ impl TempProvider for Provider { /// Execute a smart contract call with [CallRequest] without publishing a transaction. async fn call(&self, tx: CallRequest, block: Option) -> TransportResult { - self.inner - .prepare("eth_call", (tx, block.unwrap_or(BlockId::Number(BlockNumberOrTag::Latest)))) - .await + self.inner.prepare("eth_call", (tx, block.unwrap_or_default())).await } /// Estimate the gas needed for a transaction. async fn estimate_gas(&self, tx: CallRequest, block: Option) -> TransportResult { if let Some(block_id) = block { - let params = (tx, block_id); - self.inner.prepare("eth_estimateGas", params).await + self.inner.prepare("eth_estimateGas", (tx, block_id)).await } else { - let params = tx; - self.inner.prepare("eth_estimateGas", params).await + self.inner.prepare("eth_estimateGas", (tx,)).await } } /// Sends an already-signed transaction. - async fn send_raw_transaction(&self, tx: Bytes) -> TransportResult - where - Self: Sync, - { + async fn send_raw_transaction(&self, tx: Bytes) -> TransportResult { self.inner.prepare("eth_sendRawTransaction", tx).await } @@ -517,10 +437,7 @@ impl TempProvider for Provider { &self, request: CallRequest, block: Option, - ) -> TransportResult - where - Self: Sync, - { + ) -> TransportResult { self.inner .prepare( "eth_createAccessList", @@ -533,10 +450,7 @@ impl TempProvider for Provider { async fn trace_transaction( &self, hash: TxHash, - ) -> TransportResult> - where - Self: Sync, - { + ) -> TransportResult> { self.inner.prepare("trace_transaction", vec![hash]).await } @@ -544,20 +458,14 @@ impl TempProvider for Provider { &self, hash: TxHash, trace_options: GethDebugTracingOptions, - ) -> TransportResult - where - Self: Sync, - { + ) -> TransportResult { self.inner.prepare("debug_traceTransaction", (hash, trace_options)).await } async fn trace_block( &self, block: BlockNumberOrTag, - ) -> TransportResult> - where - Self: Sync, - { + ) -> TransportResult> { self.inner.prepare("trace_block", block).await } @@ -567,17 +475,13 @@ impl TempProvider for Provider { where P: Serialize + Send + Sync + Clone, R: Serialize + DeserializeOwned + Send + Sync + Unpin + 'static, - Self: Sync, { let res: R = self.inner.prepare(method, ¶ms).await?; Ok(res) } #[cfg(feature = "anvil")] - async fn set_code(&self, address: Address, code: &'static str) -> TransportResult<()> - where - Self: Sync, - { + async fn set_code(&self, address: Address, code: &'static str) -> TransportResult<()> { self.inner.prepare("anvil_setCode", (address, code)).await } } diff --git a/crates/rpc-types/src/eth/block.rs b/crates/rpc-types/src/eth/block.rs index 63c15cafccb..d29f966fb6d 100644 --- a/crates/rpc-types/src/eth/block.rs +++ b/crates/rpc-types/src/eth/block.rs @@ -568,6 +568,12 @@ impl BlockId { } } +impl Default for BlockId { + fn default() -> Self { + BlockId::Number(BlockNumberOrTag::Latest) + } +} + impl From for BlockId { fn from(num: u64) -> Self { BlockNumberOrTag::Number(num).into()