Skip to content

Commit

Permalink
Merge branch 'main' into p2p-test-get-block-headers
Browse files Browse the repository at this point in the history
  • Loading branch information
fkrause98 committed Oct 31, 2024
2 parents c2fa0d3 + ef9c511 commit b4c450b
Show file tree
Hide file tree
Showing 50 changed files with 2,784 additions and 1,405 deletions.
3 changes: 3 additions & 0 deletions .github/workflows/hive.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@ jobs:
- simulation: discv4
name: "Devp2p discv4 tests"
run_command: make run-hive SIMULATION=devp2p TEST_PATTERN="discv4"
- simulation: snap
name: "Devp2p snap tests"
run_command: make run-hive SIMULATION=devp2p TEST_PATTERN="/AccountRange"
- simulation: engine
name: "Engine tests"
run_command: make run-hive SIMULATION=ethereum/engine TEST_PATTERN="/Blob Transactions On Block 1, Cancun Genesis|Blob Transactions On Block 1, Shanghai Genesis|Blob Transaction Ordering, Single Account, Single Blob|Blob Transaction Ordering, Single Account, Dual Blob|Blob Transaction Ordering, Multiple Accounts|Replace Blob Transactions|Parallel Blob Transactions|ForkchoiceUpdatedV3 Modifies Payload ID on Different Beacon Root|NewPayloadV3 After Cancun|NewPayloadV3 Versioned Hashes|ForkchoiceUpdated Version on Payload Request"
Expand Down
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ stop-localnet-silent:
@kurtosis enclave stop lambdanet >/dev/null 2>&1 || true
@kurtosis enclave rm lambdanet --force >/dev/null 2>&1 || true

HIVE_REVISION := ccf28e5c3e940b2bc4b4f387317ee6a46f5d15c8
HIVE_REVISION := 421852ec25e4e608fe5460656f4bf0637649619e
# Shallow clones can't specify a single revision, but at least we avoid working
# the whole history by making it shallow since a given date (one day before our
# target revision).
Expand Down
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
Loading

0 comments on commit b4c450b

Please sign in to comment.