Skip to content

Commit

Permalink
Merge branch 'main' into add-pool-tx-order
Browse files Browse the repository at this point in the history
  • Loading branch information
Romsters authored Dec 17, 2024
2 parents b7439f8 + 100e3f3 commit ecfd109
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 10 deletions.
4 changes: 2 additions & 2 deletions crates/core/src/node/eth.rs
Original file line number Diff line number Diff line change
Expand Up @@ -567,15 +567,15 @@ impl EthNamespaceT for InMemoryNode {

let maybe_result = {
// try retrieving transaction from memory, and if unavailable subsequently from the fork
reader.tx_results.get(&hash).and_then(|TransactionResult { info, .. }| {
reader.tx_results.get(&hash).and_then(|TransactionResult { info, receipt, .. }| {
let input_data = info.tx.common_data.input.clone().or(None)?;
let chain_id = info.tx.common_data.extract_chain_id().or(None)?;
Some(zksync_types::api::Transaction {
hash,
nonce: U256::from(info.tx.common_data.nonce.0),
block_hash: Some(hash),
block_number: Some(U64::from(info.miniblock_number)),
transaction_index: Some(U64::from(0)),
transaction_index: Some(receipt.transaction_index),
from: Some(info.tx.initiator_account()),
to: info.tx.recipient_account(),
value: info.tx.execute.value,
Expand Down
19 changes: 11 additions & 8 deletions crates/core/src/node/in_memory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1658,6 +1658,7 @@ impl InMemoryNode {
pub fn run_l2_tx<W: WriteStorage, H: HistoryMode>(
&self,
l2_tx: L2Tx,
l2_tx_index: U64,
block_ctx: &BlockContext,
batch_env: &L1BatchEnv,
vm: &mut Vm<W, H>,
Expand Down Expand Up @@ -1733,7 +1734,7 @@ impl InMemoryNode {
block_number: Some(block_ctx.miniblock.into()),
l1_batch_number: Some(U64::from(batch_env.number.0)),
transaction_hash: Some(tx_hash),
transaction_index: Some(U64::zero()),
transaction_index: Some(l2_tx_index),
log_index: Some(U256::from(log_idx)),
transaction_log_index: Some(U256::from(log_idx)),
log_type: None,
Expand All @@ -1748,7 +1749,7 @@ impl InMemoryNode {
}
let tx_receipt = TransactionReceipt {
transaction_hash: tx_hash,
transaction_index: U64::from(0),
transaction_index: l2_tx_index,
block_hash: block_ctx.hash,
block_number: block_ctx.miniblock.into(),
l1_batch_tx_index: None,
Expand Down Expand Up @@ -1814,18 +1815,20 @@ impl InMemoryNode {

// Execute transactions and bootloader
let mut executed_tx_hashes = Vec::with_capacity(tx_hashes.len());
let mut tx_index = U64::from(0);
for tx in txs {
// Executing a next transaction means that a previous transaction was either rolled back (in which case its snapshot
// was already removed), or that we build on top of it (in which case, it can be removed now).
vm.pop_snapshot_no_rollback();
// Save pre-execution VM snapshot.
vm.make_snapshot();
let hash = tx.hash();
if let Err(e) = self.run_l2_tx(tx, &block_ctx, &batch_env, &mut vm) {
if let Err(e) = self.run_l2_tx(tx, tx_index, &block_ctx, &batch_env, &mut vm) {
tracing::error!("Error while executing transaction: {e}");
vm.rollback_to_the_latest_snapshot();
} else {
executed_tx_hashes.push(hash);
tx_index += U64::from(1);
}
}
vm.execute(InspectExecutionMode::Bootloader);
Expand All @@ -1842,7 +1845,7 @@ impl InMemoryNode {
let mut transactions = Vec::new();
let mut tx_receipts = Vec::new();
let mut debug_calls = Vec::new();
for tx_hash in &executed_tx_hashes {
for (index, tx_hash) in executed_tx_hashes.iter().enumerate() {
let Some(tx_result) = inner.tx_results.get(tx_hash) else {
// Skipping halted transaction
continue;
Expand All @@ -1853,7 +1856,7 @@ impl InMemoryNode {
let mut transaction = zksync_types::api::Transaction::from(tx_result.info.tx.clone());
transaction.block_hash = Some(block_ctx.hash);
transaction.block_number = Some(U64::from(block_ctx.miniblock));
transaction.transaction_index = Some(Index::zero());
transaction.transaction_index = Some(index.into());
transaction.l1_batch_number = Some(U64::from(batch_env.number.0));
transaction.l1_batch_tx_index = Some(Index::zero());
if transaction.transaction_type == Some(U64::zero())
Expand Down Expand Up @@ -2076,7 +2079,7 @@ mod tests {
.unwrap();
let (block_ctx, batch_env, mut vm) = test_vm(&node, system_contracts.clone());
let err = node
.run_l2_tx(tx, &block_ctx, &batch_env, &mut vm)
.run_l2_tx(tx, U64::from(0), &block_ctx, &batch_env, &mut vm)
.unwrap_err();
assert_eq!(err.to_string(), "exceeds block gas limit");
}
Expand All @@ -2097,7 +2100,7 @@ mod tests {
.unwrap();
let (block_ctx, batch_env, mut vm) = test_vm(&node, system_contracts.clone());
let err = node
.run_l2_tx(tx, &block_ctx, &batch_env, &mut vm)
.run_l2_tx(tx, U64::from(0), &block_ctx, &batch_env, &mut vm)
.unwrap_err();

assert_eq!(
Expand All @@ -2122,7 +2125,7 @@ mod tests {
.unwrap();
let (block_ctx, batch_env, mut vm) = test_vm(&node, system_contracts.clone());
let err = node
.run_l2_tx(tx, &block_ctx, &batch_env, &mut vm)
.run_l2_tx(tx, U64::from(0), &block_ctx, &batch_env, &mut vm)
.unwrap_err();

assert_eq!(
Expand Down
16 changes: 16 additions & 0 deletions e2e-tests-rust/tests/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -435,3 +435,19 @@ async fn pool_txs_order_fees() -> anyhow::Result<()> {
assert_eq!(&tx_hashes[2], pending_tx0.tx_hash());
Ok(())
}

#[tokio::test]
async fn transactions_have_index() -> anyhow::Result<()> {
let provider = init_testing_provider(|node| node.no_mine()).await?;
let tx1 = provider.tx().with_rich_from(0).register().await?;
let tx2 = provider.tx().with_rich_from(1).register().await?;

provider.anvil_mine(Some(U256::from(1)), None).await?;

let receipt1 = tx1.wait_until_finalized().await?;
let receipt2 = tx2.wait_until_finalized().await?;

assert_eq!(receipt1.transaction_index(), 0.into());
assert_eq!(receipt2.transaction_index(), 1.into());
Ok(())
}

0 comments on commit ecfd109

Please sign in to comment.