Skip to content

Commit

Permalink
fix: switch base contracts in run_l2_tx_inner()
Browse files Browse the repository at this point in the history
  • Loading branch information
grw-ms committed Sep 21, 2023
1 parent 4a58732 commit 47cdf04
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 54 deletions.
76 changes: 46 additions & 30 deletions src/hardhat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -246,11 +246,11 @@ impl<S: Send + Sync + 'static + ForkSource + std::fmt::Debug> HardhatNamespaceT
#[cfg(test)]
mod tests {
use super::*;
use crate::{http_fork_source::HttpForkSource, node::InMemoryNode, testing};
use crate::{http_fork_source::HttpForkSource, node::InMemoryNode};
use std::str::FromStr;
use zksync_basic_types::H256;
use zksync_basic_types::{Nonce, H256};
use zksync_core::api_server::web3::backend_jsonrpc::namespaces::eth::EthNamespaceT;
use zksync_types::api::BlockNumber;
use zksync_types::{api::BlockNumber, fee::Fee, l2::L2Tx};

#[tokio::test]
async fn test_set_balance() {
Expand Down Expand Up @@ -374,7 +374,6 @@ mod tests {
let node = InMemoryNode::<HttpForkSource>::default();
let hardhat: HardhatNamespaceImpl<HttpForkSource> =
HardhatNamespaceImpl::new(node.get_inner());

let to_impersonate =
Address::from_str("0xd8da6bf26964af9d7eed9e03e53415d37aa96045").unwrap();

Expand All @@ -385,46 +384,63 @@ mod tests {
.unwrap();
assert!(result);

// construct a tx
let mut tx = L2Tx::new(
Address::random(),
vec![],
Nonce(0),
Fee {
gas_limit: U256::from(1_000_000),
max_fee_per_gas: U256::from(250_000_000),
max_priority_fee_per_gas: U256::from(250_000_000),
gas_per_pubdata_limit: U256::from(20000),
},
to_impersonate,
U256::one(),
None,
Default::default(),
);
tx.set_input(vec![], H256::random());

// try to execute the tx- should fail without signature
assert!(node.apply_txs(vec![tx.clone()]).is_err());

// impersonate the account
let result = hardhat
.impersonate_account(to_impersonate)
.await
.expect("impersonate_account");

// result should be true
assert!(result);

// testing::apply_tx should use the impersonated account as the sender
{
let _hash = testing::apply_tx(&node, H256::repeat_byte(0x01));
let inner = node.get_inner();
let lock = inner.read().unwrap();
let tx = lock
.tx_results
.get(&H256::repeat_byte(0x01))
.expect("tx exists");

// check that the tx was sent from the impersonated account
assert_eq!(tx.tx.initiator_account(), to_impersonate);
}
// impersonating the same account again should return false
let result = hardhat
.impersonate_account(to_impersonate)
.await
.expect("impersonate_account");
assert!(!result);

// execution should now succeed
assert!(node.apply_txs(vec![tx.clone()]).is_ok());

// stop impersonating the account
let result = hardhat
.stop_impersonating_account(to_impersonate)
.await
.expect("stop_impersonating_account");

// result should be true
assert!(result);

// testing::apply_tx should use a random account as the sender
{
let _hash = testing::apply_tx(&node, H256::repeat_byte(0x02));
let inner = node.get_inner();
let lock = inner.read().unwrap();
let tx = lock
.tx_results
.get(&H256::repeat_byte(0x02))
.expect("tx exists");

// check that the tx was sent from the impersonated account
assert_ne!(tx.tx.initiator_account(), to_impersonate);
}
// stop impersonating the same account again should return false
let result = hardhat
.stop_impersonating_account(to_impersonate)
.await
.expect("stop_impersonating_account");
assert!(!result);

// execution should now fail again
assert!(node.apply_txs(vec![tx]).is_err());
}
}
44 changes: 20 additions & 24 deletions src/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -562,18 +562,12 @@ impl<S: std::fmt::Debug + ForkSource> InMemoryNodeInner<S> {
/// Sets the `impersonated_account` field of the node.
/// This field is used to override the `tx.initiator_account` field of the transaction in the `run_l2_tx` method.
pub fn set_impersonated_account(&mut self, address: Address) -> bool {
return self.impersonated_accounts.insert(address);

// Use SystemContracts without signature verification
self.system_contracts = SystemContracts::from_options(&Options::BuiltInWithoutSecurity);
self.impersonated_accounts.insert(address)
}

/// Clears the `impersonated_account` field of the node.
pub fn stop_impersonating_account(&mut self, address: Address) -> bool {
return self.impersonated_accounts.remove(&address);

// Restore previous SystemContracts
self.system_contracts = SystemContracts::from_options(&Options::BuiltIn);
self.impersonated_accounts.remove(&address)
}
}

Expand Down Expand Up @@ -955,7 +949,24 @@ impl<S: ForkSource + std::fmt::Debug> InMemoryNode<S> {

let mut oracle_tools = OracleTools::new(&mut storage_view, HistoryEnabled);

let bootloader_code = inner.system_contracts.contracts(execution_mode);
// if we are impersonating an account, we need to use non-verifying system contracts
let nonverifying_contracts;
let bootloader_code = {
if inner
.impersonated_accounts
.contains(&l2_tx.common_data.initiator_address)
{
tracing::info!(
"🕵️ Executing tx from impersonated account {:?}",
l2_tx.common_data.initiator_address
);
nonverifying_contracts =
SystemContracts::from_options(&Options::BuiltInWithoutSecurity);
nonverifying_contracts.contracts(execution_mode)
} else {
inner.system_contracts.contracts(execution_mode)
}
};

let block_context = inner.create_block_context();
let block_properties = InMemoryNodeInner::<S>::create_block_properties(bootloader_code);
Expand Down Expand Up @@ -1128,21 +1139,6 @@ impl<S: ForkSource + std::fmt::Debug> InMemoryNode<S> {
/// Runs L2 transaction and commits it to a new block.
fn run_l2_tx(&self, l2_tx: L2Tx, execution_mode: TxExecutionMode) -> Result<(), String> {
let tx_hash = l2_tx.hash();
{
let inner = self
.inner
.read()
.map_err(|e| format!("Failed to acquire read lock: {}", e))?;
if inner
.impersonated_accounts
.contains(&l2_tx.common_data.initiator_address)
{
tracing::info!(
"🕵️ Executing tx from impersonated account {:?}",
l2_tx.common_data.initiator_address
);
}
}
log::info!("");
log::info!("Executing {}", format!("{:?}", tx_hash).bold());
let (keys, result, block, bytecodes) =
Expand Down

0 comments on commit 47cdf04

Please sign in to comment.