diff --git a/contract/tests/account_id_tests.cpp b/contract/tests/account_id_tests.cpp index c0f0ea9a..8345fcea 100644 --- a/contract/tests/account_id_tests.cpp +++ b/contract/tests/account_id_tests.cpp @@ -12,17 +12,16 @@ struct account_id_tester : basic_evm_tester { std::optional get_max_account_id() { const auto& db = control->db(); const auto* t_id = db.find( boost::make_tuple( evm_account_name, evm_account_name, "account"_n ) ); - if ( !t_id ) { - return {}; - } + if ( !t_id ) return {}; const auto& idx = db.get_index(); if( idx.begin() == idx.end() ) return {}; decltype(t_id->id) next_tid(t_id->id._id + 1); auto itr = idx.lower_bound(boost::make_tuple(next_tid)); - if(itr == idx.end()) return {}; - if(itr->t_id._id > t_id->id._id) --itr; + if(itr == idx.end() || itr->t_id == next_tid) --itr; + + if(itr->t_id._id != t_id->id._id) return {}; return itr->primary_key; } @@ -73,7 +72,7 @@ struct account_id_tester : basic_evm_tester { BOOST_AUTO_TEST_SUITE(account_id_tests) -BOOST_FIXTURE_TEST_CASE(basic_test, account_id_tester) try { +BOOST_FIXTURE_TEST_CASE(new_account_id_logic, account_id_tester) try { BOOST_CHECK(!get_max_account_id().has_value()); BOOST_REQUIRE_EXCEPTION(get_config2(), fc::out_of_range_exception, [](const fc::out_of_range_exception& e) {return true;}); @@ -81,7 +80,7 @@ BOOST_FIXTURE_TEST_CASE(basic_test, account_id_tester) try { // Fund evm1 address with 100 EOS evm_eoa evm1; const int64_t to_bridge = 1000000; - transfer_token("alice"_n, "evm"_n, make_asset(to_bridge), evm1.address_0x()); + transfer_token("alice"_n, evm_account_name, make_asset(to_bridge), evm1.address_0x()); BOOST_CHECK(get_max_account_id().value() == 0); BOOST_CHECK(get_config2().next_account_id == 1); @@ -165,4 +164,91 @@ BOOST_FIXTURE_TEST_CASE(basic_test, account_id_tester) try { } FC_LOG_AND_RETHROW() +BOOST_FIXTURE_TEST_CASE(transition_from_0_5_1, account_id_tester) try { + + // Set old code + set_code(evm_account_name, testing::contracts::evm_runtime_wasm_0_5_1()); + set_abi(evm_account_name, testing::contracts::evm_runtime_abi_0_5_1().data()); + + BOOST_CHECK(!get_max_account_id().has_value()); + + // Fund evm1 address with 100 EOS + evm_eoa evm1; + const int64_t to_bridge = 1000000; + transfer_token("alice"_n, evm_account_name, make_asset(to_bridge), evm1.address_0x()); + BOOST_CHECK(get_max_account_id().value() == 0); + + // Deploy Factory and Test contracts + auto contract_addr = deploy_contract(evm1, evmc::from_hex(factory_and_test_bytecode).value()); + uint64_t contract_account_id = find_account_by_address(contract_addr).value().id; + BOOST_CHECK(get_max_account_id().value() == 1); + + // Call 'Factory::deploy' + auto txn = generate_tx(contract_addr, 0, 1'000'000); + + silkworm::Bytes data; + data += evmc::from_hex("2b85ba38").value(); //deploy + data += evmc::from_hex(int_str32(555)).value(); //salt=555 + txn.data = data; + evm1.sign(txn); + pushtx(txn); + + BOOST_CHECK(get_max_account_id().value() == 2); + + // Recover TestContract address + auto test_contract_address = find_account_by_id(2).value().address; + + // Call 'TestContract::set_foo(1234)' + txn = generate_tx(test_contract_address, 0, 1'000'000); + data.clear(); + data += evmc::from_hex("e5d5dfbc").value(); //set_foo + data += evmc::from_hex(int_str32(1234)).value(); //val=1234 + txn.data = data; + evm1.sign(txn); + pushtx(txn); + + BOOST_CHECK(get_storage_slots_count(2) == 1); + + // Call 'TestContract::killme' + txn = generate_tx(test_contract_address, 0, 1'000'000); + data.clear(); + data += evmc::from_hex("24d97a4a").value(); //killme + txn.data = data; + evm1.sign(txn); + pushtx(txn); + + BOOST_CHECK(get_storage_slots_count(2) == 1); + BOOST_CHECK(get_max_account_id().value() == 1); + + // Make a transfer to a new address to create a new account + evm_eoa evm2; + transfer_token("alice"_n, evm_account_name, make_asset(to_bridge), evm2.address_0x()); + BOOST_CHECK(get_max_account_id().value() == 2); + + // Set new code + set_code(evm_account_name, testing::contracts::evm_runtime_wasm()); + set_abi(evm_account_name, testing::contracts::evm_runtime_abi().data()); + + BOOST_REQUIRE_EXCEPTION(get_config2(), fc::out_of_range_exception, [](const fc::out_of_range_exception& e) {return true;}); + + // Call 'Factory::deploy' again + txn = generate_tx(contract_addr, 0, 1'000'000); + data.clear(); + data += evmc::from_hex("2b85ba38").value(); //deploy + data += evmc::from_hex(int_str32(555)).value(); //salt=555 + txn.data = data; + evm1.sign(txn); + pushtx(txn); + + BOOST_CHECK(get_max_account_id().value() == 3); + BOOST_CHECK(get_config2().next_account_id == 4); + + // Make a transfer to a new address to create a new account + evm_eoa evm3; + transfer_token("alice"_n, evm_account_name, make_asset(to_bridge), evm3.address_0x()); + BOOST_CHECK(get_max_account_id().value() == 4); + BOOST_CHECK(get_config2().next_account_id == 5); + +} FC_LOG_AND_RETHROW() + BOOST_AUTO_TEST_SUITE_END() diff --git a/contract/tests/contracts.hpp.in b/contract/tests/contracts.hpp.in index f606450c..ecef1ee8 100644 --- a/contract/tests/contracts.hpp.in +++ b/contract/tests/contracts.hpp.in @@ -40,6 +40,9 @@ struct contracts { static std::vector evm_read_callback_wasm() { return read_wasm("${CMAKE_CURRENT_SOURCE_DIR}/contracts/evm_read_callback/evm_read_callback.wasm"); } static std::vector evm_read_callback_abi() { return read_abi("${CMAKE_CURRENT_SOURCE_DIR}/contracts/evm_read_callback/evm_read_callback.abi"); } + static std::vector evm_runtime_wasm_0_5_1() { return read_wasm("${CMAKE_CURRENT_SOURCE_DIR}/contracts/evm_runtime_wasm_0_5_1/evm_runtime.wasm"); } + static std::vector evm_runtime_abi_0_5_1() { return read_abi("${CMAKE_CURRENT_SOURCE_DIR}/contracts/evm_runtime_wasm_0_5_1/evm_runtime.abi"); } + static std::string eth_test_folder() { return "${CMAKE_CURRENT_SOURCE_DIR}/../../silkworm/third_party/tests"; } diff --git a/contract/tests/contracts/evm_runtime_wasm_0_5_1/evm_runtime.abi b/contract/tests/contracts/evm_runtime_wasm_0_5_1/evm_runtime.abi new file mode 100644 index 00000000..e2c7a430 --- /dev/null +++ b/contract/tests/contracts/evm_runtime_wasm_0_5_1/evm_runtime.abi @@ -0,0 +1,394 @@ +{ + "version": "eosio::abi/1.2", + "types": [], + "structs": [{ + "name": "account", + "base": "", + "fields": [{ + "name": "id", + "type": "uint64" + },{ + "name": "eth_address", + "type": "bytes" + },{ + "name": "nonce", + "type": "uint64" + },{ + "name": "balance", + "type": "bytes" + },{ + "name": "code_id", + "type": "uint64?" + } + ] + },{ + "name": "account_code", + "base": "", + "fields": [{ + "name": "id", + "type": "uint64" + },{ + "name": "ref_count", + "type": "uint32" + },{ + "name": "code", + "type": "bytes" + },{ + "name": "code_hash", + "type": "bytes" + } + ] + },{ + "name": "addegress", + "base": "", + "fields": [{ + "name": "accounts", + "type": "name[]" + } + ] + },{ + "name": "allowed_egress_account", + "base": "", + "fields": [{ + "name": "account", + "type": "name" + } + ] + },{ + "name": "balance", + "base": "", + "fields": [{ + "name": "owner", + "type": "name" + },{ + "name": "balance", + "type": "balance_with_dust" + } + ] + },{ + "name": "balance_with_dust", + "base": "", + "fields": [{ + "name": "balance", + "type": "asset" + },{ + "name": "dust", + "type": "uint64" + } + ] + },{ + "name": "close", + "base": "", + "fields": [{ + "name": "owner", + "type": "name" + } + ] + },{ + "name": "config", + "base": "", + "fields": [{ + "name": "version", + "type": "varuint32" + },{ + "name": "chainid", + "type": "uint64" + },{ + "name": "genesis_time", + "type": "time_point_sec" + },{ + "name": "ingress_bridge_fee", + "type": "asset" + },{ + "name": "gas_price", + "type": "uint64" + },{ + "name": "miner_cut", + "type": "uint32" + },{ + "name": "status", + "type": "uint32" + } + ] + },{ + "name": "exec", + "base": "", + "fields": [{ + "name": "input", + "type": "exec_input" + },{ + "name": "callback", + "type": "exec_callback?" + } + ] + },{ + "name": "exec_callback", + "base": "", + "fields": [{ + "name": "contract", + "type": "name" + },{ + "name": "action", + "type": "name" + } + ] + },{ + "name": "exec_input", + "base": "", + "fields": [{ + "name": "context", + "type": "bytes?" + },{ + "name": "from", + "type": "bytes?" + },{ + "name": "to", + "type": "bytes" + },{ + "name": "data", + "type": "bytes" + },{ + "name": "value", + "type": "bytes?" + } + ] + },{ + "name": "fee_parameters", + "base": "", + "fields": [{ + "name": "gas_price", + "type": "uint64?" + },{ + "name": "miner_cut", + "type": "uint32?" + },{ + "name": "ingress_bridge_fee", + "type": "asset?" + } + ] + },{ + "name": "freeze", + "base": "", + "fields": [{ + "name": "value", + "type": "bool" + } + ] + },{ + "name": "gc", + "base": "", + "fields": [{ + "name": "max", + "type": "uint32" + } + ] + },{ + "name": "gcstore", + "base": "", + "fields": [{ + "name": "id", + "type": "uint64" + },{ + "name": "storage_id", + "type": "uint64" + } + ] + },{ + "name": "init", + "base": "", + "fields": [{ + "name": "chainid", + "type": "uint64" + },{ + "name": "fee_params", + "type": "fee_parameters" + } + ] + },{ + "name": "nextnonce", + "base": "", + "fields": [{ + "name": "owner", + "type": "name" + },{ + "name": "next_nonce", + "type": "uint64" + } + ] + },{ + "name": "open", + "base": "", + "fields": [{ + "name": "owner", + "type": "name" + } + ] + },{ + "name": "pushtx", + "base": "", + "fields": [{ + "name": "miner", + "type": "name" + },{ + "name": "rlptx", + "type": "bytes" + } + ] + },{ + "name": "removeegress", + "base": "", + "fields": [{ + "name": "accounts", + "type": "name[]" + } + ] + },{ + "name": "setfeeparams", + "base": "", + "fields": [{ + "name": "fee_params", + "type": "fee_parameters" + } + ] + },{ + "name": "storage", + "base": "", + "fields": [{ + "name": "id", + "type": "uint64" + },{ + "name": "key", + "type": "bytes" + },{ + "name": "value", + "type": "bytes" + } + ] + },{ + "name": "withdraw", + "base": "", + "fields": [{ + "name": "owner", + "type": "name" + },{ + "name": "quantity", + "type": "asset" + },{ + "name": "to", + "type": "name$" + } + ] + } + ], + "actions": [{ + "name": "addegress", + "type": "addegress", + "ricardian_contract": "" + },{ + "name": "close", + "type": "close", + "ricardian_contract": "" + },{ + "name": "exec", + "type": "exec", + "ricardian_contract": "" + },{ + "name": "freeze", + "type": "freeze", + "ricardian_contract": "" + },{ + "name": "gc", + "type": "gc", + "ricardian_contract": "" + },{ + "name": "init", + "type": "init", + "ricardian_contract": "" + },{ + "name": "open", + "type": "open", + "ricardian_contract": "" + },{ + "name": "pushtx", + "type": "pushtx", + "ricardian_contract": "" + },{ + "name": "removeegress", + "type": "removeegress", + "ricardian_contract": "" + },{ + "name": "setfeeparams", + "type": "setfeeparams", + "ricardian_contract": "" + },{ + "name": "withdraw", + "type": "withdraw", + "ricardian_contract": "" + } + ], + "tables": [{ + "name": "account", + "index_type": "i64", + "key_names": [], + "key_types": [], + "type": "account" + },{ + "name": "accountcode", + "index_type": "i64", + "key_names": [], + "key_types": [], + "type": "account_code" + },{ + "name": "balances", + "index_type": "i64", + "key_names": [], + "key_types": [], + "type": "balance" + },{ + "name": "config", + "index_type": "i64", + "key_names": [], + "key_types": [], + "type": "config" + },{ + "name": "egresslist", + "index_type": "i64", + "key_names": [], + "key_types": [], + "type": "allowed_egress_account" + },{ + "name": "gcstore", + "index_type": "i64", + "key_names": [], + "key_types": [], + "type": "gcstore" + },{ + "name": "inevm", + "index_type": "i64", + "key_names": [], + "key_types": [], + "type": "balance_with_dust" + },{ + "name": "nextnonces", + "index_type": "i64", + "key_names": [], + "key_types": [], + "type": "nextnonce" + },{ + "name": "storage", + "index_type": "i64", + "key_names": [], + "key_types": [], + "type": "storage" + } + ], + "ricardian_clauses": [], + "error_messages": [], + "abi_extensions": [], + "variants": [], + "action_results": [{ + "name": "gc", + "result_type": "bool" + } + ] +} \ No newline at end of file diff --git a/contract/tests/contracts/evm_runtime_wasm_0_5_1/evm_runtime.wasm b/contract/tests/contracts/evm_runtime_wasm_0_5_1/evm_runtime.wasm new file mode 100644 index 00000000..7b83b735 Binary files /dev/null and b/contract/tests/contracts/evm_runtime_wasm_0_5_1/evm_runtime.wasm differ