Skip to content

Commit

Permalink
feat: balance verification sender (#526)
Browse files Browse the repository at this point in the history
* remove ef-tests feature

* subtract transaction cost to sender balance

* use `and_then` instead of map

* add additional failing tests

* remove kakarot submodule

* update skipped tests + gitignore

* update based on comment
  • Loading branch information
greged93 authored Oct 12, 2023
1 parent fad6e52 commit 2eaf9c7
Show file tree
Hide file tree
Showing 10 changed files with 55 additions and 34 deletions.
2 changes: 0 additions & 2 deletions .env.example

This file was deleted.

4 changes: 2 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,5 @@ broadcast
# Ignore the ef-tests
/crates/ef-testing/ethereum-tests

# Ignore Katana
.katana
# Ignore compiled Kakarot contracts
lib/kakarot
8 changes: 6 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,14 @@ clean-kakarot:
rm -rf lib/kakarot
mkdir -p lib/kakarot/build

# Runs all tests but integration tests
unit:
cargo test --lib

# Runs the repo tests
tests:
cargo nextest run --all-features
cargo nextest run

# Runs ef tests only
ef-test:
cargo nextest run --package ef-testing --test tests --features ef-tests
cargo nextest run --package ef-testing --test tests
11 changes: 11 additions & 0 deletions blockchain-tests-skip.yml
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,7 @@ filename:
- callcodecallcodecallcode_111_SuicideMiddle.json # ef-tests #485
- callcodecallcodecall_110_SuicideMiddle.json # ef-tests #485
- callcodecallcode_11_SuicideEnd.json # ef-tests #485
- touchAndGo.json # ef-tests #527
stCallCreateCallCodeTest:
- Call1024BalanceTooLow.json # ef-tests #493
- CallcodeLoseGasOOG.json # ef-tests #493
Expand Down Expand Up @@ -173,6 +174,7 @@ filename:
- createInitFailUndefinedInstruction.json # ef-tests #493
- createInitFail_OOGduringInit2.json # ef-tests #497
- callOutput3partialFail.json # ef-tests #493
- createNameRegistratorPerTxsNotEnoughGas.json # ef-tests #529
stCallDelegateCodesCallCodeHomestead:
- callcallcallcode_001.json # ef-tests #330
- callcallcallcode_001_OOGE.json # ef-tests #330
Expand Down Expand Up @@ -298,6 +300,7 @@ filename:
- codesizeValid.json # ef-tests #263
- create2CodeSizeLimit.json # ef-tests #264
- createCodeSizeLimit.json # ef-tests #265
- codesizeOOGInvalidSize.json # ef-tests #528
stCreate2:
- RevertDepthCreate2OOG.json # ef-tests #334
- RevertDepthCreate2OOGBerlin.json # ef-tests #334
Expand Down Expand Up @@ -772,6 +775,7 @@ filename:
- randomStatetest628.json # ef-tests #395
- randomStatetest636.json # ef-tests #395
- randomStatetest506.json # ef-tests #395
- randomStatetest642.json # ef-tests #533
stRefundTest:
- refundFF.json # ef-tests #405
- refundMax.json # ef-tests #405
Expand Down Expand Up @@ -1207,6 +1211,9 @@ filename:
- CallToNameRegistratorNotMuchMemory0.json # ef-test #490
- CallToNameRegistratorAddressTooBigLeft.json # ef-test #490
- CallToNameRegistratorTooMuchMemory2.json # ef-test #523
- suicideCaller.json # ef-test #530
- suicideCallerAddresTooBigLeft.json # ef-test #530
- suicideOrigin.json # ef-test #530
stStaticFlagEnabled:
- CallcodeToPrecompileFromCalledContract.json # ef-test #443
- CallcodeToPrecompileFromContractInitialization.json # ef-test #443
Expand All @@ -1227,6 +1234,10 @@ filename:
- walletExecuteOverDailyLimitOnlyOneOwner.json # ef-test #430
- walletExecuteOverDailyLimitOnlyOneOwnerNew.json # ef-test #430
- walletExecuteUnderDailyLimit.json # ef-test #430
- dayLimitConstructionOOG.json # ef-test #531
- walletConstructionOOG.json # ef-test #531
- multiOwnedConstructionNotEnoughGasPartial.json # ef-test #531
- walletKill.json # ef-test #531
stStaticCall:
- StaticcallToPrecompileFromContractInitialization.json # ef-tests #421
- static_CallContractToCreateContractAndCallItOOG.json # ef-tests #421
Expand Down
3 changes: 0 additions & 3 deletions crates/ef-testing/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,3 @@ zip = { workspace = true, optional = true }

[dev-dependencies]
tracing-subscriber = { workspace = true }

[features]
ef-tests = []
43 changes: 31 additions & 12 deletions crates/ef-testing/src/models/case.rs
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,25 @@ impl BlockchainTestCase {
LocalWallet::from_bytes(&sk.0).map_err(|err| RunnerError::Other(err.to_string()))?;
let sender_address = wallet.address().to_fixed_bytes();

// Get gas used from block header
let maybe_block = test.blocks.first();
let maybe_block_header = maybe_block.and_then(|block| block.block_header.as_ref());
let gas_used = maybe_block_header
.map(|block_header| block_header.gas_used.0)
.unwrap_or_default();

// Get gas price from transaction
let maybe_transaction = maybe_block.and_then(|block| {
block
.transactions
.as_ref()
.and_then(|transactions| transactions.first())
});
let gas_price = maybe_transaction
.and_then(|transaction| transaction.gas_price.map(|gas_price| gas_price.0))
.unwrap_or_default();
let transaction_cost = gas_price * gas_used;

let post_state = match test.post_state.clone().ok_or_else(|| {
RunnerError::Other(format!("missing post state for {}", test_case_name))
})? {
Expand All @@ -174,37 +193,37 @@ impl BlockchainTestCase {
let actual = sequencer.get_storage_at(address, k.0)?;
if actual != v.0 {
return Err(RunnerError::Other(format!(
"storage mismatch for {:#20x} at {:#32x}: expected {:#32x}, got {:#32x}",
address, k.0, v.0, actual
"{} storage mismatch for {:#20x} at {:#32x}: expected {:#32x}, got {:#32x}",
test_case_name, address, k.0, v.0, actual
)));
}
}
// Nonce
let actual = sequencer.get_nonce_at(address)?;
if actual != expected_state.nonce.0 {
return Err(RunnerError::Other(format!(
"nonce mismatch for {:#20x}: expected {:#32x}, got {:#32x}",
address, expected_state.nonce.0, actual
"{} nonce mismatch for {:#20x}: expected {:#32x}, got {:#32x}",
test_case_name, address, expected_state.nonce.0, actual
)));
}
// Bytecode
let actual = sequencer.get_code_at(address)?;
if actual != expected_state.code {
return Err(RunnerError::Other(format!(
"code mismatch for {:#20x}: expected {:#x}, got {:#x}",
address, expected_state.code, actual
"{} code mismatch for {:#20x}: expected {:#x}, got {:#x}",
test_case_name, address, expected_state.code, actual
)));
}
// Skip sender address because of the difference in gas cost
// Balance
let mut actual = sequencer.get_balance_at(address)?;
// Subtract transaction cost to sender balance
if address.0 == sender_address {
continue;
actual -= transaction_cost;
}
// Balance
let actual = sequencer.get_balance_at(address)?;
if actual != expected_state.balance.0 {
return Err(RunnerError::Other(format!(
"balance mismatch for {:#20x}: expected {:#32x}, got {:#32x}",
address, expected_state.balance.0, actual
"{} balance mismatch for {:#20x}: expected {:#32x}, got {:#32x}",
test_case_name, address, expected_state.balance.0, actual
)));
}
}
Expand Down
13 changes: 4 additions & 9 deletions crates/ef-testing/src/models/result.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,10 @@ impl CaseResult {
}

/// Assert that all the given tests passed and print the results to stdout.
pub(crate) fn assert_tests_pass(suite_name: &str, results: &[CaseResult]) {
pub(crate) fn assert_tests_pass(results: &[CaseResult]) {
let failed = categorize_results(results);

print_results(suite_name, &failed);
print_results(&failed);

assert!(failed.is_empty(), "Some tests failed (see above)");
}
Expand All @@ -53,16 +53,11 @@ pub(crate) fn categorize_results(results: &[CaseResult]) -> Vec<&CaseResult> {
}

/// Display the given test results to stdout.
pub(crate) fn print_results(suite_name: &str, failed: &[&CaseResult]) {
pub(crate) fn print_results(failed: &[&CaseResult]) {
for case in failed {
match &case.result {
Ok(_) => unreachable!(),
Err(err) => println!(
"[!] Suite {} Case {} failed:\n{}",
suite_name,
case.path.display(),
err,
),
Err(err) => println!("[!] Case {} failed:\n{}", case.path.display(), err,),
}
}
}
2 changes: 1 addition & 1 deletion crates/ef-testing/src/traits/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ pub trait Suite {

let results = Cases { test_cases }.run().await;

assert_tests_pass(&self.suite_name(), &results);
assert_tests_pass(&results);
}
}

Expand Down
2 changes: 0 additions & 2 deletions crates/ef-testing/tests/tests.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
#![cfg(feature = "ef-tests")]

use ef_testing::models::suite::BlockchainTestSuite;
use ef_testing::traits::Suite;
use std::format;
Expand Down
1 change: 0 additions & 1 deletion lib/kakarot
Submodule kakarot deleted from b41e25

0 comments on commit 2eaf9c7

Please sign in to comment.