Skip to content

Commit

Permalink
Test nonce against 0 when account is not opened. Minor change in
Browse files Browse the repository at this point in the history
argument names.
  • Loading branch information
yarkinwho committed Aug 31, 2023
1 parent 19dfb5f commit 3caaa22
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 10 deletions.
2 changes: 1 addition & 1 deletion include/evm_runtime/evm_contract.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ class [[eosio::contract]] evm_contract : public contract
[[eosio::action]] void bridgereg(eosio::name receiver, const eosio::asset& min_fee);
[[eosio::action]] void bridgeunreg(eosio::name receiver);

[[eosio::action]] void assertnonce(eosio::name receiver, uint64_t nonce);
[[eosio::action]] void assertnonce(eosio::name account, uint64_t next_nonce);

#ifdef WITH_TEST_ACTIONS
[[eosio::action]] void testtx(const std::optional<bytes>& orlptx, const evm_runtime::test::block_info& bi);
Expand Down
11 changes: 8 additions & 3 deletions src/actions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -736,11 +736,16 @@ void evm_contract::bridgeunreg(eosio::name receiver) {
}


void evm_contract::assertnonce(eosio::name receiver, uint64_t nonce) {
void evm_contract::assertnonce(eosio::name account, uint64_t next_nonce) {
nextnonces nextnonce_table(get_self(), get_self().value);

const nextnonce& next_nonce = nextnonce_table.get(receiver.value, "caller account has not been opened");
eosio::check(nonce == next_nonce.next_nonce, "wrong nonce");
auto next_nonce_iter = nextnonce_table.find(account.value);
if (next_nonce_iter == nextnonce_table.end()) {
eosio::check(0 == next_nonce, "wrong nonce");
}
else {
eosio::check(next_nonce_iter->next_nonce == next_nonce, "wrong nonce");
}
}

#ifdef WITH_TEST_ACTIONS
Expand Down
6 changes: 3 additions & 3 deletions tests/basic_evm_tester.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -344,9 +344,9 @@ transaction_trace_ptr basic_evm_tester::bridgeunreg(name receiver) {
mvo()("receiver", receiver));
}

transaction_trace_ptr basic_evm_tester::assertnonce(name receiver, uint64_t nonce) {
return basic_evm_tester::push_action(evm_account_name, "assertnonce"_n, receiver,
mvo()("receiver", receiver)("nonce", nonce));
transaction_trace_ptr basic_evm_tester::assertnonce(name account, uint64_t next_nonce) {
return basic_evm_tester::push_action(evm_account_name, "assertnonce"_n, account,
mvo()("account", account)("next_nonce", next_nonce));
}

transaction_trace_ptr basic_evm_tester::pushtx(const silkworm::Transaction& trx, name miner)
Expand Down
2 changes: 1 addition & 1 deletion tests/basic_evm_tester.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,7 @@ class basic_evm_tester : public testing::validating_tester
transaction_trace_ptr bridgereg(name receiver, asset min_fee, vector<account_name> extra_signers={evm_account_name});
transaction_trace_ptr bridgeunreg(name receiver);
transaction_trace_ptr exec(const exec_input& input, const std::optional<exec_callback>& callback);
transaction_trace_ptr assertnonce(name receiver, uint64_t nonce);
transaction_trace_ptr assertnonce(name account, uint64_t next_nonce);
transaction_trace_ptr pushtx(const silkworm::Transaction& trx, name miner = evm_account_name);
void call(name from, const evmc::bytes& to, const evmc::bytes& value, evmc::bytes& data, uint64_t gas_limit, name actor);
void admincall(const evmc::bytes& from, const evmc::bytes& to, const evmc::bytes& value, evmc::bytes& data, uint64_t gas_limit, name actor);
Expand Down
9 changes: 7 additions & 2 deletions tests/call_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -499,8 +499,13 @@ BOOST_FIXTURE_TEST_CASE(deploy_contract_function, call_evm_tester) try {
BOOST_FIXTURE_TEST_CASE(assetnonce_test, call_evm_tester) try {
auto alice_addr = make_reserved_address("alice"_n.to_uint64_t());

BOOST_REQUIRE_EXCEPTION(assertnonce("alice"_n, 0),
eosio_assert_message_exception, eosio_assert_message_is("caller account has not been opened"));
// nonce for not opened account is zero.
assertnonce("alice"_n, 0);
BOOST_REQUIRE_EXCEPTION(assertnonce("alice"_n, 1),
eosio_assert_message_exception, eosio_assert_message_is("wrong nonce"));

// Advance block so we do not generate same transaction.
produce_block();

open("alice"_n);
transfer_token("alice"_n, evm_account_name, make_asset(1000000), "alice");
Expand Down

0 comments on commit 3caaa22

Please sign in to comment.