Skip to content

Commit

Permalink
properly populate block txs and fix a couple of bugs
Browse files Browse the repository at this point in the history
  • Loading branch information
itegulov committed Oct 28, 2024
1 parent 5595f5f commit 5281b48
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 17 deletions.
69 changes: 59 additions & 10 deletions src/node/eth.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1487,7 +1487,9 @@ mod tests {
use zksync_basic_types::vm::VmVersion;
use zksync_basic_types::{web3, Nonce};
use zksync_multivm::utils::get_max_batch_gas_limit;
use zksync_types::l2::TransactionType;
use zksync_types::{
api,
api::{BlockHashObject, BlockNumber, BlockNumberObject, TransactionReceipt},
utils::deployed_address_create,
Bloom, K256PrivateKey, EMPTY_UNCLES_HASH,
Expand Down Expand Up @@ -1642,7 +1644,7 @@ mod tests {
async fn test_get_block_by_hash_for_produced_block() {
let node = InMemoryNode::<HttpForkSource>::default();
let tx_hash = H256::repeat_byte(0x01);
let (expected_block_hash, _) = testing::apply_tx(&node, tx_hash);
let (expected_block_hash, _, _) = testing::apply_tx(&node, tx_hash);
let genesis_block = node
.get_block_by_number(BlockNumber::from(0), false)
.await
Expand Down Expand Up @@ -1788,7 +1790,7 @@ mod tests {
async fn test_get_block_by_number_for_produced_block() {
let node = InMemoryNode::<HttpForkSource>::default();
let tx_hash = H256::repeat_byte(0x01);
let (expected_block_hash, _) = testing::apply_tx(&node, tx_hash);
let (expected_block_hash, _, _) = testing::apply_tx(&node, tx_hash);
let expected_block_number = 1;
let genesis_block = node
.get_block_by_number(BlockNumber::from(0), false)
Expand Down Expand Up @@ -1838,6 +1840,53 @@ mod tests {
assert_ne!(actual_block.logs_bloom, Bloom::zero());
}

#[tokio::test]
async fn test_get_block_by_number_for_produced_block_full_txs() {
let node = InMemoryNode::<HttpForkSource>::default();
let tx_hash = H256::repeat_byte(0x01);
let (block_hash, _, tx) = testing::apply_tx(&node, tx_hash);
let expected_block_number = 1;

let mut actual_block = node
.get_block_by_number(BlockNumber::Number(U64::from(expected_block_number)), true)
.await
.expect("failed fetching block by number")
.expect("no block");

assert_eq!(actual_block.transactions.len(), 1);
let actual_tx = match actual_block.transactions.remove(0) {
TransactionVariant::Full(tx) => tx,
TransactionVariant::Hash(_) => unreachable!(),
};
let expected_tx = api::Transaction {
hash: tx_hash,
nonce: U256::from(0),
block_hash: Some(block_hash),
block_number: Some(U64::from(expected_block_number)),
transaction_index: Some(U64::from(0)),
from: Some(tx.initiator_account()),
to: tx.recipient_account(),
value: U256::from(1),
gas_price: Some(tx.common_data.fee.max_fee_per_gas),
gas: tx.common_data.fee.gas_limit,
input: Default::default(),
v: actual_tx.v, // Checked separately, see below
r: actual_tx.r, // Checked separately, see below
s: actual_tx.s, // Checked separately, see below
raw: None,
transaction_type: Some(U64::from(TransactionType::EIP712Transaction as u32)),
access_list: None,
max_fee_per_gas: Some(tx.common_data.fee.max_fee_per_gas),
max_priority_fee_per_gas: Some(tx.common_data.fee.max_priority_fee_per_gas),
chain_id: U256::from(260),
l1_batch_number: Some(U64::from(1)),
l1_batch_tx_index: Some(U64::from(0)),
};
assert_eq!(expected_tx, actual_tx);

// TODO: Verify that the TX is signed properly (use alloy to abstract from zksync-core code?)
}

#[tokio::test]
async fn test_get_block_by_number_uses_fork_source_if_missing_number() {
let mock_server = MockServer::run_with_config(ForkBlockConfig {
Expand Down Expand Up @@ -2016,7 +2065,7 @@ mod tests {
async fn test_get_block_transaction_count_by_hash_for_produced_block() {
let node = InMemoryNode::<HttpForkSource>::default();

let (expected_block_hash, _) = testing::apply_tx(&node, H256::repeat_byte(0x01));
let (expected_block_hash, _, _) = testing::apply_tx(&node, H256::repeat_byte(0x01));
let actual_transaction_count = node
.get_block_transaction_count_by_hash(expected_block_hash)
.await
Expand Down Expand Up @@ -2227,7 +2276,7 @@ mod tests {
async fn test_get_transaction_receipt_uses_produced_block_hash() {
let node = InMemoryNode::<HttpForkSource>::default();
let tx_hash = H256::repeat_byte(0x01);
let (expected_block_hash, _) = testing::apply_tx(&node, tx_hash);
let (expected_block_hash, _, _) = testing::apply_tx(&node, tx_hash);

let actual_tx_receipt = node
.get_transaction_receipt(tx_hash)
Expand Down Expand Up @@ -2309,7 +2358,7 @@ mod tests {
.new_block_filter()
.await
.expect("failed creating filter");
let (block_hash, _) = testing::apply_tx(&node, H256::repeat_byte(0x1));
let (block_hash, _, _) = testing::apply_tx(&node, H256::repeat_byte(0x1));

match node
.get_filter_changes(filter_id)
Expand Down Expand Up @@ -3125,7 +3174,7 @@ mod tests {
async fn test_get_transaction_by_block_hash_and_index_returns_none_for_invalid_block_hash() {
let node = InMemoryNode::<HttpForkSource>::default();
let input_tx_hash = H256::repeat_byte(0x01);
let (input_block_hash, _) = testing::apply_tx(&node, input_tx_hash);
let (input_block_hash, _, _) = testing::apply_tx(&node, input_tx_hash);
let invalid_block_hash = H256::repeat_byte(0xab);
assert_ne!(input_block_hash, invalid_block_hash);

Expand All @@ -3141,7 +3190,7 @@ mod tests {
async fn test_get_transaction_by_block_hash_and_index_returns_none_for_invalid_index() {
let node = InMemoryNode::<HttpForkSource>::default();
let input_tx_hash = H256::repeat_byte(0x01);
let (input_block_hash, _) = testing::apply_tx(&node, input_tx_hash);
let (input_block_hash, _, _) = testing::apply_tx(&node, input_tx_hash);

let result = node
.get_transaction_by_block_hash_and_index(input_block_hash, U64::from(10))
Expand All @@ -3155,7 +3204,7 @@ mod tests {
async fn test_get_transaction_by_block_hash_and_index_returns_transaction_for_valid_input() {
let node = InMemoryNode::<HttpForkSource>::default();
let input_tx_hash = H256::repeat_byte(0x01);
let (input_block_hash, _) = testing::apply_tx(&node, input_tx_hash);
let (input_block_hash, _, _) = testing::apply_tx(&node, input_tx_hash);

let actual_tx = node
.get_transaction_by_block_hash_and_index(input_block_hash, U64::from(0))
Expand Down Expand Up @@ -3278,7 +3327,7 @@ mod tests {
{
let node = InMemoryNode::<HttpForkSource>::default();
let input_tx_hash = H256::repeat_byte(0x01);
let (input_block_hash, _) = testing::apply_tx(&node, input_tx_hash);
let (input_block_hash, _, _) = testing::apply_tx(&node, input_tx_hash);
let invalid_block_hash = H256::repeat_byte(0xab);
assert_ne!(input_block_hash, invalid_block_hash);

Expand Down Expand Up @@ -3311,7 +3360,7 @@ mod tests {
async fn test_get_transaction_by_block_number_and_index_returns_transaction_for_valid_input() {
let node = InMemoryNode::<HttpForkSource>::default();
let input_tx_hash = H256::repeat_byte(0x01);
let (_, input_block_number) = testing::apply_tx(&node, input_tx_hash);
let (_, input_block_number, _) = testing::apply_tx(&node, input_tx_hash);

let actual_tx = node
.get_transaction_by_block_number_and_index(
Expand Down
8 changes: 6 additions & 2 deletions src/node/in_memory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ use std::{
};

use std::convert::TryInto;
use zksync_basic_types::web3::Index;
use zksync_basic_types::{
web3::keccak256, web3::Bytes, AccountTreeId, Address, L1BatchNumber, L2BlockNumber, H160, H256,
U256, U64,
Expand Down Expand Up @@ -1464,8 +1465,11 @@ impl<S: ForkSource + std::fmt::Debug + Clone> InMemoryNode<S> {
let hash = compute_hash(block_ctx.miniblock, l2_tx.hash());

let mut transaction = zksync_types::api::Transaction::from(l2_tx);
transaction.block_hash = Some(inner.current_miniblock_hash);
transaction.block_number = Some(U64::from(inner.current_miniblock));
transaction.block_hash = Some(hash);
transaction.block_number = Some(U64::from(block_ctx.miniblock));
transaction.transaction_index = Some(Index::zero());
transaction.l1_batch_number = Some(U64::from(batch_env.number.0));
transaction.l1_batch_tx_index = Some(Index::zero());

let parent_block_hash = inner
.block_hashes
Expand Down
14 changes: 9 additions & 5 deletions src/testing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -452,7 +452,10 @@ impl TransactionBuilder {
Default::default(),
)
.unwrap();
tx.set_input(vec![], self.tx_hash);
tx.set_input(
tx.common_data.input_data().unwrap_or_default().into(),
self.tx_hash,
);
tx
}
}
Expand All @@ -461,7 +464,7 @@ impl TransactionBuilder {
pub fn apply_tx<T: ForkSource + std::fmt::Debug + Clone>(
node: &InMemoryNode<T>,
tx_hash: H256,
) -> (H256, U64) {
) -> (H256, U64, L2Tx) {
let next_miniblock = node
.get_inner()
.read()
Expand All @@ -472,9 +475,10 @@ pub fn apply_tx<T: ForkSource + std::fmt::Debug + Clone>(
let tx = TransactionBuilder::new().set_hash(tx_hash).build();

node.set_rich_account(tx.common_data.initiator_address);
node.apply_txs(vec![tx]).expect("failed applying tx");
node.apply_txs(vec![tx.clone()])
.expect("failed applying tx");

(produced_block_hash, U64::from(next_miniblock))
(produced_block_hash, U64::from(next_miniblock), tx)
}

/// Deploys a contract with the given bytecode.
Expand Down Expand Up @@ -964,7 +968,7 @@ mod test {
#[tokio::test]
async fn test_apply_tx() {
let node = InMemoryNode::<HttpForkSource>::default();
let (actual_block_hash, actual_block_number) = apply_tx(&node, H256::repeat_byte(0x01));
let (actual_block_hash, actual_block_number, _) = apply_tx(&node, H256::repeat_byte(0x01));

assert_eq!(
H256::from_str("0xd97ba6a5ab0f2d7fbfc697251321cce20bff3da2b0ddaf12c80f80f0ab270b15")
Expand Down

0 comments on commit 5281b48

Please sign in to comment.