Skip to content

Commit

Permalink
Recover l1 fee compression discount
Browse files Browse the repository at this point in the history
  • Loading branch information
yaziciahmet committed Dec 20, 2024
1 parent 1792171 commit 5ecdb1c
Showing 1 changed file with 82 additions and 10 deletions.
92 changes: 82 additions & 10 deletions crates/evm/src/tests/call_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ use sov_rollup_interface::spec::SpecId as SovSpecId;
use crate::call::CallMessage;
use crate::evm::primitive_types::Receipt;
use crate::evm::DbAccount;
use crate::handler::L1_FEE_OVERHEAD;
use crate::handler::{BROTLI_COMPRESSION_PERCENTAGE, L1_FEE_OVERHEAD};
use crate::smart_contracts::{
BlockHashContract, InfiniteLoopContract, LogsContract, SelfDestructorContract,
SimpleStorageContract, TestContract,
Expand Down Expand Up @@ -1680,7 +1680,7 @@ fn test_l1_fee_compression_discount() {
let (config, dev_signer, _) =
get_evm_config_starting_base_fee(U256::from_str("100000000000000").unwrap(), None, 1);

let (mut evm, mut working_set) = get_evm(&config);
let (mut evm, mut working_set) = get_evm_with_spec(&config, SovSpecId::Genesis);
let l1_fee_rate = 1;

let soft_confirmation_info = HookSoftConfirmationInfo {
Expand All @@ -1689,7 +1689,7 @@ fn test_l1_fee_compression_discount() {
da_slot_height: 1,
da_slot_txs_commitment: [42u8; 32],
pre_state_root: [10u8; 32].to_vec(),
current_spec: SovSpecId::Fork1,
current_spec: SovSpecId::Genesis,
pub_key: vec![],
deposit_data: vec![],
l1_fee_rate,
Expand All @@ -1699,7 +1699,7 @@ fn test_l1_fee_compression_discount() {
evm.begin_soft_confirmation_hook(&soft_confirmation_info, &mut working_set);
{
let sender_address = generate_address::<C>("sender");
let context = C::new(sender_address, 2, SovSpecId::Fork1, l1_fee_rate);
let context = C::new(sender_address, 2, SovSpecId::Genesis, l1_fee_rate);
let call_tx = dev_signer
.sign_default_transaction_with_priority_fee(
TxKind::Call(Address::random()),
Expand Down Expand Up @@ -1736,30 +1736,102 @@ fn test_l1_fee_compression_discount() {
assert_eq!(config.coinbase, PRIORITY_FEE_VAULT);

let gas_fee_paid = 21000;
let tx1_diff_size = 46;
let tx1_diff_size = 140;

let expected_db_balance = U256::from(
let mut expected_db_balance = U256::from(
100000000000000u64
- 1000
- gas_fee_paid * 10000001
- tx1_diff_size
- L1_FEE_OVERHEAD as u64,
);
let expected_base_fee_vault_balance = U256::from(gas_fee_paid * 10000000);
let expected_coinbase_balance = U256::from(gas_fee_paid);
let expected_l1_fee_vault_balance = U256::from(tx1_diff_size + L1_FEE_OVERHEAD as u64);
let mut expected_base_fee_vault_balance = U256::from(gas_fee_paid * 10000000);
let mut expected_coinbase_balance = U256::from(gas_fee_paid);
let mut expected_l1_fee_vault_balance = U256::from(tx1_diff_size + L1_FEE_OVERHEAD as u64);

assert_eq!(db_account.balance, expected_db_balance);
assert_eq!(base_fee_vault.balance, expected_base_fee_vault_balance);
assert_eq!(coinbase_account.balance, expected_coinbase_balance);
assert_eq!(l1_fee_vault.balance, expected_l1_fee_vault_balance);

// Set up the next transaction with the fork 1 activated
let soft_confirmation_info = HookSoftConfirmationInfo {
l2_height: 3,
da_slot_hash: [5u8; 32],
da_slot_height: 1,
da_slot_txs_commitment: [42u8; 32],
pre_state_root: [99u8; 32].to_vec(),
current_spec: SovSpecId::Fork1, // Compression discount is enabled
pub_key: vec![],
deposit_data: vec![],
l1_fee_rate,
timestamp: 0,
};

evm.begin_soft_confirmation_hook(&soft_confirmation_info, &mut working_set);
{
let sender_address = generate_address::<C>("sender");
let context = C::new(sender_address, 3, SovSpecId::Fork1, l1_fee_rate);
let simple_tx = dev_signer
.sign_default_transaction_with_priority_fee(
TxKind::Call(Address::random()),
vec![],
1,
1000,
20000000,
1,
)
.unwrap();
evm.call(
CallMessage {
txs: vec![simple_tx],
},
&context,
&mut working_set,
)
.unwrap();
}
evm.end_soft_confirmation_hook(&soft_confirmation_info, &mut working_set);
evm.finalize_hook(&[98u8; 32].into(), &mut working_set.accessory_state());

let db_account = evm
.accounts
.get(&dev_signer.address(), &mut working_set)
.unwrap();
let base_fee_vault = evm.accounts.get(&BASE_FEE_VAULT, &mut working_set).unwrap();
let l1_fee_vault = evm.accounts.get(&L1_FEE_VAULT, &mut working_set).unwrap();

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

// gas fee remains the same
let tx2_diff_size = 46;

expected_db_balance -=
U256::from(gas_fee_paid * 10000001 + 1000 + tx2_diff_size + L1_FEE_OVERHEAD as u64);
expected_base_fee_vault_balance += U256::from(gas_fee_paid * 10000000);
expected_coinbase_balance += U256::from(gas_fee_paid);
expected_l1_fee_vault_balance += U256::from(tx2_diff_size + L1_FEE_OVERHEAD as u64);

assert_eq!(db_account.balance, expected_db_balance);
assert_eq!(base_fee_vault.balance, expected_base_fee_vault_balance);
assert_eq!(coinbase_account.balance, expected_coinbase_balance);
assert_eq!(l1_fee_vault.balance, expected_l1_fee_vault_balance);

// assert comression discount
assert_eq!(
tx1_diff_size * BROTLI_COMPRESSION_PERCENTAGE as u64 / 100,
tx2_diff_size
);

assert_eq!(
evm.receipts
.iter(&mut working_set.accessory_state())
.map(|r| r.l1_diff_size)
.collect::<Vec<_>>(),
[53, 94, 154, 94, tx1_diff_size]
[255, 561, 1019, 561, tx1_diff_size, tx2_diff_size]
);
}

Expand Down

0 comments on commit 5ecdb1c

Please sign in to comment.