Skip to content

Commit

Permalink
Move sys tests to sys_tx_tests.rs
Browse files Browse the repository at this point in the history
  • Loading branch information
kpp committed Mar 26, 2024
1 parent a52b98f commit 0462b3a
Show file tree
Hide file tree
Showing 3 changed files with 114 additions and 99 deletions.
101 changes: 2 additions & 99 deletions module-system/module-implementations/sov-evm/src/tests/call_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,14 @@ use std::str::FromStr;

use alloy_rpc_types::request::{TransactionInput, TransactionRequest};
use reth_primitives::constants::ETHEREUM_BLOCK_GAS_LIMIT;
use reth_primitives::{
Address, BlockNumberOrTag, Bytes, TransactionKind, TransactionSignedEcRecovered,
TransactionSignedNoHash, U64,
};
use reth_primitives::{Address, BlockNumberOrTag, Bytes, TransactionKind, U64};
use revm::primitives::{SpecId, KECCAK_EMPTY, U256};
use sov_modules_api::default_context::DefaultContext;
use sov_modules_api::utils::generate_address;
use sov_modules_api::{Context, Module, StateMapAccessor, StateVecAccessor};

use crate::call::CallMessage;
use crate::evm::primitive_types::Receipt;
use crate::signer::SYSTEM_SIGNER;
use crate::smart_contracts::{
BlockHashContract, LogsContract, SelfDestructorContract, SimpleStorageContract, TestContract,
};
Expand Down Expand Up @@ -564,23 +560,6 @@ fn create_contract_message_with_fee<T: TestContract>(
.unwrap()
}

fn create_system_contract_message_with_fee<T: TestContract>(
dev_signer: &TestSigner,
nonce: u64,
contract: T,
max_fee_per_gas: u128,
) -> RlpEvmTransaction {
dev_signer
.sign_system_transaction_with_fee(
TransactionKind::Create,
contract.byte_code().to_vec(),
nonce,
0,
max_fee_per_gas,
)
.unwrap()
}

pub(crate) fn create_contract_transaction<T: TestContract>(
dev_signer: &TestSigner,
nonce: u64,
Expand Down Expand Up @@ -723,7 +702,7 @@ pub(crate) fn get_evm_config(
(config, dev_signer, contract_addr)
}

fn get_evm_config_starting_base_fee(
pub(crate) fn get_evm_config_starting_base_fee(
signer_balance: U256,
block_gas_limit: Option<u64>,
starting_base_fee: u64,
Expand Down Expand Up @@ -861,79 +840,3 @@ fn test_l1_fee_not_enough_funds() {
let db_coinbase = evm.accounts.get(&config.coinbase, &mut working_set);
assert!(db_coinbase.is_none());
}

#[test]
fn test_system_caller() {
let (config, dev_signer, _) =
get_evm_config_starting_base_fee(U256::from_str("1000000").unwrap(), None, 1);

let (evm, mut working_set) = get_evm(&config);
let l1_fee_rate = 1;

let deploy_message =
create_system_contract_message_with_fee(&dev_signer, 0, BlockHashContract::default(), 1);

let signed_no_hash = TransactionSignedNoHash::try_from(deploy_message.clone()).unwrap();
assert!(
signed_no_hash.recover_signer().is_none(),
"System signed message must be unrecoverable"
);

let signed_recovered: TransactionSignedEcRecovered =
TransactionSignedEcRecovered::try_from(deploy_message.clone()).unwrap();
assert_eq!(
signed_recovered.signer(),
SYSTEM_SIGNER,
"SYSTEM_SIGNATURE must be transformed into SYSTEM_SIGNER"
);

let system_account = evm.accounts.get(&SYSTEM_SIGNER, &mut working_set);
assert!(
system_account.is_none(),
"There is no system account before call"
); // That's optional but if the acc will exist in the future its balance must be zero.

evm.begin_soft_confirmation_hook([5u8; 32], &[10u8; 32], l1_fee_rate, &mut working_set);
{
let sender_address = generate_address::<C>("sender");
let sequencer_address = generate_address::<C>("sequencer");
let context = C::new(sender_address, sequencer_address, 1);

evm.call(
CallMessage {
txs: vec![deploy_message],
},
&context,
&mut working_set,
)
.unwrap();
}
evm.end_soft_confirmation_hook(&mut working_set);
evm.finalize_hook(&[99u8; 32].into(), &mut working_set.accessory_state());

let system_account = evm.accounts.get(&SYSTEM_SIGNER, &mut working_set).unwrap();
// The system caller balance is unchanged(if exists)/or should be 0
assert_eq!(system_account.info.balance, U256::from(0));
assert_eq!(system_account.info.nonce, 1);

let coinbase_account = evm.accounts.get(&config.coinbase, &mut working_set);
assert!(coinbase_account.is_none());

assert_eq!(
evm.receipts
.iter(&mut working_set.accessory_state())
.collect::<Vec<_>>(),
[Receipt {
receipt: reth_primitives::Receipt {
tx_type: reth_primitives::TxType::Eip1559,
success: true,
cumulative_gas_used: 114235,
logs: vec![],
},
gas_used: 114235,
log_index_start: 0,
diff_size: 477,
error: None,
},]
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ mod cfg_tests;
mod genesis_tests;
mod hooks_tests;
mod queries;
mod sys_tx_tests;
pub(crate) mod test_signer;
mod tx_tests;

Expand Down
111 changes: 111 additions & 0 deletions module-system/module-implementations/sov-evm/src/tests/sys_tx_tests.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
use std::str::FromStr;

use reth_primitives::{TransactionKind, TransactionSignedEcRecovered, TransactionSignedNoHash};
use revm::primitives::U256;
use sov_modules_api::default_context::DefaultContext;
use sov_modules_api::utils::generate_address;
use sov_modules_api::{Context, Module, StateMapAccessor, StateVecAccessor};

use crate::call::CallMessage;
use crate::evm::primitive_types::Receipt;
use crate::signer::SYSTEM_SIGNER;
use crate::smart_contracts::{BlockHashContract, TestContract};
use crate::tests::call_tests::get_evm_config_starting_base_fee;
use crate::tests::genesis_tests::get_evm;
use crate::tests::test_signer::TestSigner;
use crate::RlpEvmTransaction;

type C = DefaultContext;

fn create_system_contract_message_with_fee<T: TestContract>(
dev_signer: &TestSigner,
nonce: u64,
contract: T,
max_fee_per_gas: u128,
) -> RlpEvmTransaction {
dev_signer
.sign_system_transaction_with_fee(
TransactionKind::Create,
contract.byte_code().to_vec(),
nonce,
0,
max_fee_per_gas,
)
.unwrap()
}

#[test]
fn test_system_caller() {
let (config, dev_signer, _) =
get_evm_config_starting_base_fee(U256::from_str("1000000").unwrap(), None, 1);

let (evm, mut working_set) = get_evm(&config);
let l1_fee_rate = 1;

let deploy_message =
create_system_contract_message_with_fee(&dev_signer, 0, BlockHashContract::default(), 1);

let signed_no_hash = TransactionSignedNoHash::try_from(deploy_message.clone()).unwrap();
assert!(
signed_no_hash.recover_signer().is_none(),
"System signed message must be unrecoverable"
);

let signed_recovered: TransactionSignedEcRecovered =
TransactionSignedEcRecovered::try_from(deploy_message.clone()).unwrap();
assert_eq!(
signed_recovered.signer(),
SYSTEM_SIGNER,
"SYSTEM_SIGNATURE must be transformed into SYSTEM_SIGNER"
);

let system_account = evm.accounts.get(&SYSTEM_SIGNER, &mut working_set);
assert!(
system_account.is_none(),
"There is no system account before call"
); // That's optional but if the acc will exist in the future its balance must be zero.

evm.begin_soft_confirmation_hook([5u8; 32], &[10u8; 32], l1_fee_rate, &mut working_set);
{
let sender_address = generate_address::<C>("sender");
let sequencer_address = generate_address::<C>("sequencer");
let context = C::new(sender_address, sequencer_address, 1);

evm.call(
CallMessage {
txs: vec![deploy_message],
},
&context,
&mut working_set,
)
.unwrap();
}
evm.end_soft_confirmation_hook(&mut working_set);
evm.finalize_hook(&[99u8; 32].into(), &mut working_set.accessory_state());

let system_account = evm.accounts.get(&SYSTEM_SIGNER, &mut working_set).unwrap();
// The system caller balance is unchanged(if exists)/or should be 0
assert_eq!(system_account.info.balance, U256::from(0));
assert_eq!(system_account.info.nonce, 1);

let coinbase_account = evm.accounts.get(&config.coinbase, &mut working_set);
assert!(coinbase_account.is_none());

assert_eq!(
evm.receipts
.iter(&mut working_set.accessory_state())
.collect::<Vec<_>>(),
[Receipt {
receipt: reth_primitives::Receipt {
tx_type: reth_primitives::TxType::Eip1559,
success: true,
cumulative_gas_used: 114235,
logs: vec![],
},
gas_used: 114235,
log_index_start: 0,
diff_size: 477,
error: None,
},]
)
}

0 comments on commit 0462b3a

Please sign in to comment.