Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Alloy providers migration]: Fix tests #6462

Merged
merged 7 commits into from
Nov 29, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 7 additions & 7 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion crates/anvil/core/src/eth/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use alloy_primitives::{Address, Bytes, TxHash, B256, B64, U256};
use alloy_rpc_types::{
pubsub::{Params as SubscriptionParams, SubscriptionKind},
state::StateOverride,
BlockId, BlockNumberOrTag as BlockNumber, CallRequest, Filter, TransactionRequest,
BlockId, BlockNumberOrTag as BlockNumber, CallRequest, Filter,
};
use ethers_core::types::{transaction::eip712::TypedData, GethDebugTracingOptions};

Expand Down
37 changes: 26 additions & 11 deletions crates/anvil/src/eth/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@ use alloy_rpc_types::{
Log,
Transaction,
TransactionReceipt,
TransactionRequest as AlloyTransactionRequest,
TxpoolContent,
TxpoolInspect,
// trace::{geth::{DefaultFrame, GethDebugTracingOptions, GethTrace},
Expand All @@ -59,8 +58,8 @@ use anvil_core::{
block::BlockInfo,
transaction::{
call_to_internal_tx_request, to_alloy_proof, to_ethers_access_list,
to_internal_tx_request, EthTransactionRequest, LegacyTransaction, PendingTransaction,
TransactionKind, TypedTransaction, TypedTransactionRequest,
EthTransactionRequest, LegacyTransaction, PendingTransaction, TransactionKind,
TypedTransaction, TypedTransactionRequest,
},
EthRequest,
},
Expand Down Expand Up @@ -867,9 +866,17 @@ impl EthApi {
pub async fn sign_transaction(&self, request: EthTransactionRequest) -> Result<String> {
node_info!("eth_signTransaction");

let from = request.from.map(Ok).unwrap_or_else(|| {
self.accounts()?.first().cloned().ok_or(BlockchainError::NoSignerAvailable).map(|a| a.to_ethers())
})?.to_alloy();
let from = request
.from
.map(Ok)
.unwrap_or_else(|| {
self.accounts()?
.first()
.cloned()
.ok_or(BlockchainError::NoSignerAvailable)
.map(|a| a.to_ethers())
})?
.to_alloy();

let (nonce, _) = self.request_nonce(&request, from).await?;

Expand All @@ -886,9 +893,17 @@ impl EthApi {
pub async fn send_transaction(&self, request: EthTransactionRequest) -> Result<TxHash> {
node_info!("eth_sendTransaction");

let from = request.from.map(Ok).unwrap_or_else(|| {
self.accounts()?.first().cloned().ok_or(BlockchainError::NoSignerAvailable).map(|a| a.to_ethers())
})?.to_alloy();
let from = request
.from
.map(Ok)
.unwrap_or_else(|| {
self.accounts()?
.first()
.cloned()
.ok_or(BlockchainError::NoSignerAvailable)
.map(|a| a.to_ethers())
})?
.to_alloy();

let (nonce, on_chain_nonce) = self.request_nonce(&request, from).await?;
let request = self.build_typed_tx_request(request, nonce)?;
Expand Down Expand Up @@ -1275,7 +1290,7 @@ impl EthApi {
return fork
.fee_history(block_count, BlockNumber::Number(number), &reward_percentiles)
.await
.map_err(|_| BlockchainError::DataUnavailable);
.map_err(BlockchainError::AlloyForkProvider);
}
}

Expand All @@ -1302,7 +1317,7 @@ impl EthApi {
oldest_block: U256::from(lowest),
base_fee_per_gas: Vec::new(),
gas_used_ratio: Vec::new(),
reward: Default::default(),
reward: Some(Default::default()),
};

let mut rewards = Vec::new();
Expand Down
34 changes: 28 additions & 6 deletions crates/anvil/src/eth/backend/fork.rs
Original file line number Diff line number Diff line change
Expand Up @@ -418,9 +418,15 @@ impl ClientFork {

pub async fn block_by_hash(&self, hash: B256) -> Result<Option<Block>, TransportError> {
if let Some(block) = self.storage_read().blocks.get(&hash).cloned() {
return Ok(Some(block));
return Ok(Some(self.convert_to_tx_only_block(block)));
}
let block = self.fetch_full_block(hash).await?.map(Into::into);

let block = self
.fetch_full_block(hash)
.await?
.map(Into::into)
.map(|b| self.convert_to_tx_only_block(b));

Ok(block)
}

Expand All @@ -442,10 +448,14 @@ impl ClientFork {
.copied()
.and_then(|hash| self.storage_read().blocks.get(&hash).cloned())
{
return Ok(Some(block));
return Ok(Some(self.convert_to_tx_only_block(block)));
}

let block = self.fetch_full_block(block_number).await?.map(Into::into);
let block = self
.fetch_full_block(block_number)
.await?
.map(Into::into)
.map(|b| self.convert_to_tx_only_block(b));
Ok(block)
}

Expand Down Expand Up @@ -477,7 +487,7 @@ impl ClientFork {
// also insert all transactions
let block_txs = match block.clone().transactions {
BlockTransactions::Full(txs) => txs,
_ => panic!("expected full block. This is a bug."),
_ => vec![],
};
storage.transactions.extend(block_txs.iter().map(|tx| (tx.hash, tx.clone())));
storage.hashes.insert(block_number, hash);
Expand Down Expand Up @@ -520,13 +530,18 @@ impl ClientFork {
.hash
// TODO: Nicer way to make a custom error from a TransportError
.expect("Missing block hash");
let block_number = block.header.number.expect("Missing block number");
if let Some(uncles) = self.storage_read().uncles.get(&block_hash) {
return Ok(uncles.get(index).cloned());
}

let mut uncles = Vec::with_capacity(block.uncles.len());
for (uncle_idx, _) in block.uncles.iter().enumerate() {
let uncle = match self.provider().get_uncle(block_hash, U64::from(uncle_idx)).await? {
let uncle = match self
.provider()
.get_uncle(block_number.to::<u64>(), U64::from(uncle_idx))
.await?
{
Some(u) => u,
None => return Ok(None),
};
Expand All @@ -553,6 +568,13 @@ impl ClientFork {
}
block.into_full_block(transactions)
}

/// Converts a full block into a block with only its tx hashes.
fn convert_to_tx_only_block(&self, mut block: Block) -> Block {
let hashes = block.transactions.iter().collect();
block.transactions = BlockTransactions::Hashes(hashes);
block
}
}

/// Contains all fork metadata
Expand Down
15 changes: 10 additions & 5 deletions crates/anvil/src/eth/backend/mem/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ use alloy_rpc_types::{
Block as AlloyBlock,
BlockId,
BlockNumberOrTag as BlockNumber,
BlockTransactions,
Filter,
FilteredParams,
Header as AlloyHeader,
Expand Down Expand Up @@ -1522,7 +1523,7 @@ impl Backend {
}

pub fn mined_block_by_number(&self, number: BlockNumber) -> Option<AlloyBlock> {
Some(self.convert_block(self.get_block(number)?))
Some(convert_to_tx_only_block(self.convert_block(self.get_block(number)?)))
}

pub fn get_full_block(&self, id: impl Into<BlockId>) -> Option<AlloyBlock> {
Expand Down Expand Up @@ -2116,10 +2117,7 @@ impl Backend {
}

if let Some(fork) = self.get_fork() {
return fork
.transaction_by_hash(hash)
.await
.map_err(|_| BlockchainError::DataUnavailable);
return fork.transaction_by_hash(hash).await.map_err(BlockchainError::AlloyForkProvider)
}

Ok(None)
Expand Down Expand Up @@ -2275,6 +2273,13 @@ fn get_pool_transactions_nonce(
None
}

/// Converts a full block into a block with only its tx hashes.
fn convert_to_tx_only_block(mut block: AlloyBlock) -> AlloyBlock {
let hashes = block.transactions.iter().collect();
block.transactions = BlockTransactions::Hashes(hashes);
block
}

#[async_trait::async_trait]
impl TransactionValidator for Backend {
async fn validate_pool_transaction(
Expand Down
Loading
Loading