Skip to content

Commit

Permalink
merge with main
Browse files Browse the repository at this point in the history
  • Loading branch information
Arkenan committed Oct 31, 2024
2 parents 2dba084 + acd0365 commit 1af5ff9
Show file tree
Hide file tree
Showing 8 changed files with 103 additions and 19 deletions.
58 changes: 56 additions & 2 deletions cmd/ethereum_rust_l2/src/commands/wallet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,17 @@ pub(crate) enum Command {
help = "Specify the wallet in which you want to deposit your funds."
)]
to: Option<Address>,
#[clap(short = 'w', required = false)]
wait_for_receipt: bool,
#[clap(long, short = 'e', required = false)]
explorer_url: bool,
},
#[clap(about = "Finalize a pending withdrawal.")]
ClaimWithdraw { l2_withdrawal_tx_hash: H256 },
ClaimWithdraw {
l2_withdrawal_tx_hash: H256,
#[clap(short = 'w', required = false)]
wait_for_receipt: bool,
},
#[clap(about = "Transfer funds to another wallet.")]
Transfer {
// TODO: Parse ether instead.
Expand All @@ -56,6 +62,10 @@ pub(crate) enum Command {
token_address: Option<Address>,
#[clap(long = "to")]
to: Address,
#[clap(long = "nonce")]
nonce: Option<u64>,
#[clap(short = 'w', required = false)]
wait_for_receipt: bool,
#[clap(
long = "l1",
required = false,
Expand All @@ -79,6 +89,8 @@ pub(crate) enum Command {
help = "Specify the token address, the base token is used as default."
)]
token_address: Option<Address>,
#[clap(short = 'w', required = false)]
wait_for_receipt: bool,
#[clap(long, short = 'e', required = false)]
explorer_url: bool,
},
Expand Down Expand Up @@ -121,6 +133,8 @@ pub(crate) enum Command {
gas_price: Option<u64>,
#[clap(long = "priority-gas-price", required = false)]
priority_gas_price: Option<u64>,
#[clap(short = 'w', required = false)]
wait_for_receipt: bool,
},
#[clap(about = "Make a call to a contract")]
Call {
Expand Down Expand Up @@ -177,6 +191,8 @@ pub(crate) enum Command {
gas_price: Option<u64>,
#[clap(long = "priority-gas-price", required = false)]
priority_gas_price: Option<u64>,
#[clap(short = 'w', required = false)]
wait_for_receipt: bool,
},
}

Expand Down Expand Up @@ -258,6 +274,7 @@ impl Command {
amount,
token_address,
to,
wait_for_receipt,
explorer_url: _,
} => {
if to.is_some() {
Expand All @@ -275,7 +292,9 @@ impl Command {
amount,
token_address: None,
to: cfg.contracts.common_bridge,
wait_for_receipt,
l1: true,
nonce: None,
explorer_url: false,
}
.run(cfg)
Expand All @@ -285,6 +304,7 @@ impl Command {
}
Command::ClaimWithdraw {
l2_withdrawal_tx_hash,
wait_for_receipt,
} => {
let (withdrawal_l2_block_number, claimed_amount) = match rollup_client
.get_transaction_by_hash(l2_withdrawal_tx_hash)
Expand Down Expand Up @@ -329,11 +349,17 @@ impl Command {
.await?;

println!("Withdrawal claim sent: {tx_hash:#x}");

if wait_for_receipt {
wait_for_transaction_receipt(&eth_client, tx_hash).await?;
}
}
Command::Transfer {
amount,
token_address,
to,
nonce,
wait_for_receipt,
l1,
explorer_url: _,
} => {
Expand All @@ -351,7 +377,7 @@ impl Command {
} else {
cfg.network.l2_chain_id
},
nonce: client.get_nonce(from).await?,
nonce: nonce.unwrap_or(client.get_nonce(from).await?),
max_fee_per_gas: client.get_gas_price().await?.as_u64() * 100,
gas_limit: 21000 * 100,
..Default::default()
Expand All @@ -369,12 +395,17 @@ impl Command {
"[{}] Transfer sent: {tx_hash:#x}",
if l1 { "L1" } else { "L2" }
);

if wait_for_receipt {
wait_for_transaction_receipt(&client, tx_hash).await?;
}
}
Command::Withdraw {
amount,
to,
nonce,
token_address: _,
wait_for_receipt,
explorer_url: _,
} => {
let withdraw_transaction = PrivilegedL2Transaction {
Expand All @@ -393,6 +424,10 @@ impl Command {
.await?;

println!("Withdrawal sent: {tx_hash:#x}");

if wait_for_receipt {
wait_for_transaction_receipt(&rollup_client, tx_hash).await?;
}
}
Command::WithdrawalProof { tx_hash } => {
let (_index, path) = get_withdraw_merkle_proof(&rollup_client, tx_hash).await?;
Expand All @@ -414,6 +449,7 @@ impl Command {
gas_limit,
gas_price,
priority_gas_price,
wait_for_receipt,
} => {
let client = match l1 {
true => eth_client,
Expand Down Expand Up @@ -442,6 +478,10 @@ impl Command {
"[{}] Transaction sent: {tx_hash:#x}",
if l1 { "L1" } else { "L2" }
);

if wait_for_receipt {
wait_for_transaction_receipt(&client, tx_hash).await?;
}
}
Command::Call {
to,
Expand Down Expand Up @@ -482,6 +522,7 @@ impl Command {
gas_limit,
gas_price,
priority_gas_price,
wait_for_receipt,
} => {
let client = match l1 {
true => eth_client,
Expand All @@ -507,8 +548,21 @@ impl Command {

println!("Contract deployed in tx: {deployment_tx_hash:#x}");
println!("Contract address: {deployed_contract_address:#x}");

if wait_for_receipt {
wait_for_transaction_receipt(&client, deployment_tx_hash).await?;
}
}
};
Ok(())
}
}

pub async fn wait_for_transaction_receipt(client: &EthClient, tx_hash: H256) -> eyre::Result<()> {
println!("Waiting for transaction receipt...");
while client.get_transaction_receipt(tx_hash).await?.is_none() {
tokio::time::sleep(std::time::Duration::from_secs(1)).await;
}
println!("Transaction confirmed");
Ok(())
}
2 changes: 2 additions & 0 deletions crates/blockchain/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ pub enum InvalidBlockError {
GasUsedMismatch,
#[error("Blob gas used doesn't match value in header")]
BlobGasUsedMismatch,
#[error("Invalid transaction: {0}")]
InvalidTransaction(String),
}

#[derive(Debug, thiserror::Error)]
Expand Down
7 changes: 7 additions & 0 deletions crates/blockchain/mempool.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ pub fn filter_transactions(
if filter.only_plain_txs && is_blob_tx || filter.only_blob_txs && !is_blob_tx {
return false;
}

// Filter by tip & base_fee
if let Some(min_tip) = filter.min_tip {
if !tx
Expand All @@ -87,7 +88,13 @@ pub fn filter_transactions(
{
return false;
}
// This is a temporary fix to avoid invalid transactions to be included.
// This should be removed once https://github.com/lambdaclass/ethereum_rust/issues/680
// is addressed.
} else if tx.effective_gas_tip(filter.base_fee).is_none() {
return false;
}

// Filter by blob gas fee
if let (true, Some(blob_fee)) = (is_blob_tx, filter.blob_fee) {
if !tx.max_fee_per_blob_gas().is_some_and(|fee| fee >= blob_fee) {
Expand Down
34 changes: 23 additions & 11 deletions crates/blockchain/payload.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ use crate::{
GAS_LIMIT_BOUND_DIVISOR, GAS_PER_BLOB, MAX_BLOB_GAS_PER_BLOCK, MIN_GAS_LIMIT,
TARGET_BLOB_GAS_PER_BLOCK, TX_GAS_COST,
},
error::ChainError,
error::{ChainError, InvalidBlockError},
mempool::{self, PendingTxFilter},
};

Expand Down Expand Up @@ -222,7 +222,7 @@ pub fn apply_withdrawals(context: &mut PayloadBuildContext) -> Result<(), EvmErr
/// Returns two transaction queues, one for plain and one for blob txs
fn fetch_mempool_transactions(
context: &mut PayloadBuildContext,
) -> Result<(TransactionQueue, TransactionQueue), StoreError> {
) -> Result<(TransactionQueue, TransactionQueue), ChainError> {
let tx_filter = PendingTxFilter {
/*TODO(https://github.com/lambdaclass/ethereum_rust/issues/680): add tip filter */
base_fee: context.base_fee_per_gas(),
Expand All @@ -245,12 +245,12 @@ fn fetch_mempool_transactions(
TransactionQueue::new(
mempool::filter_transactions(&plain_tx_filter, store)?,
context.base_fee_per_gas(),
),
)?,
// Blob txs
TransactionQueue::new(
mempool::filter_transactions(&blob_tx_filter, store)?,
context.base_fee_per_gas(),
),
)?,
))
}

Expand Down Expand Up @@ -320,7 +320,7 @@ pub fn fill_transactions(context: &mut PayloadBuildContext) -> Result<(), ChainE
// Execute tx
let receipt = match apply_transaction(&head_tx, context) {
Ok(receipt) => {
txs.shift();
txs.shift()?;
// Pull transaction from the mempool
mempool::remove_transaction(
tx_hash,
Expand Down Expand Up @@ -464,26 +464,33 @@ impl From<HeadTransaction> for Transaction {

impl TransactionQueue {
/// Creates a new TransactionQueue from a set of transactions grouped by sender and sorted by nonce
fn new(mut txs: HashMap<Address, Vec<MempoolTransaction>>, base_fee: Option<u64>) -> Self {
fn new(
mut txs: HashMap<Address, Vec<MempoolTransaction>>,
base_fee: Option<u64>,
) -> Result<Self, ChainError> {
let mut heads = Vec::new();
for (address, txs) in txs.iter_mut() {
// Pull the first tx from each list and add it to the heads list
// This should be a newly filtered tx list so we are guaranteed to have a first element
let head_tx = txs.remove(0);
heads.push(HeadTransaction {
// We already ran this method when filtering the transactions from the mempool so it shouldn't fail
tip: head_tx.effective_gas_tip(base_fee).unwrap(),
tip: head_tx
.effective_gas_tip(base_fee)
.ok_or(ChainError::InvalidBlock(
InvalidBlockError::InvalidTransaction("Attempted to add an invalid transaction to the block. The transaction filter must have failed.".to_owned()),
))?,
tx: head_tx,
sender: *address,
});
}
// Sort heads by higest tip (and lowest timestamp if tip is equal)
heads.sort();
TransactionQueue {
Ok(TransactionQueue {
heads,
txs,
base_fee,
}
})
}

/// Remove all transactions from the queue
Expand Down Expand Up @@ -513,15 +520,19 @@ impl TransactionQueue {

/// Remove the top transaction
/// Add a tx from the same sender to the head transactions
fn shift(&mut self) {
fn shift(&mut self) -> Result<(), ChainError> {
let tx = self.heads.remove(0);
if let Some(txs) = self.txs.get_mut(&tx.sender) {
// Fetch next head
if !txs.is_empty() {
let head_tx = txs.remove(0);
let head = HeadTransaction {
// We already ran this method when filtering the transactions from the mempool so it shouldn't fail
tip: head_tx.effective_gas_tip(self.base_fee).unwrap(),
tip: head_tx.effective_gas_tip(self.base_fee).ok_or(
ChainError::InvalidBlock(
InvalidBlockError::InvalidTransaction("Attempted to add an invalid transaction to the block. The transaction filter must have failed.".to_owned()),
),
)?,
tx: head_tx,
sender: tx.sender,
};
Expand All @@ -533,6 +544,7 @@ impl TransactionQueue {
self.heads.insert(index, head);
}
}
Ok(())
}
}

Expand Down
5 changes: 2 additions & 3 deletions crates/networking/rpc/eth/account.rs
Original file line number Diff line number Diff line change
Expand Up @@ -160,9 +160,8 @@ impl RpcHandler for GetTransactionCountRequest {
);

let Some(block_number) = self.block.resolve_block_number(&context.storage)? else {
return Err(RpcErr::Internal(
"Could not resolve block number".to_owned(),
)); // Should we return Null here?
return serde_json::to_value("0x0")
.map_err(|error| RpcErr::Internal(error.to_string()));
};

let nonce = context
Expand Down
10 changes: 9 additions & 1 deletion crates/networking/rpc/types/block_identifier.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,15 @@ impl BlockIdentifier {
BlockTag::Finalized => storage.get_finalized_block_number(),
BlockTag::Safe => storage.get_safe_block_number(),
BlockTag::Latest => storage.get_latest_block_number(),
BlockTag::Pending => storage.get_pending_block_number(),
BlockTag::Pending => {
storage
.get_pending_block_number()
// If there are no pending blocks, we return the latest block number
.and_then(|pending_block_number| match pending_block_number {
Some(block_number) => Ok(Some(block_number)),
None => storage.get_latest_block_number(),
})
}
},
}
}
Expand Down
2 changes: 1 addition & 1 deletion test_data/el-stability-check.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ tasks:

- name: run_task_matrix
title: "Check block proposals from all client pairs"
timeout: 2m
timeout: 3m
configVars:
matrixValues: "validatorPairNames"
config:
Expand Down
4 changes: 3 additions & 1 deletion test_data/network_params.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,6 @@ assertoor_params:
run_block_proposal_check: false
run_blob_transaction_test: true
tests:
- 'https://raw.githubusercontent.com/lambdaclass/lambda_ethereum_rust/refs/heads/main/test_data/el-stability-check.yml'
- 'https://raw.githubusercontent.com/lambdaclass/lambda_ethereum_rust/refs/heads/main/test_data/el-stability-check.yml'
tx_spammer_params:
tx_spammer_extra_args: ["--accounts=10", --txcount=10]

0 comments on commit 1af5ff9

Please sign in to comment.