From ab0d99018c8456c7044d1d0eba95fd377bd787f0 Mon Sep 17 00:00:00 2001 From: Matias Romeo Date: Fri, 1 Sep 2023 17:21:14 -0300 Subject: [PATCH] admin action: change addopenbal interface --- contract/include/evm_runtime/evm_contract.hpp | 2 +- contract/src/admin_actions.cpp | 11 +++++--- contract/tests/admin_actions_tests.cpp | 26 +++++++++---------- contract/tests/basic_evm_tester.cpp | 5 ++-- contract/tests/basic_evm_tester.hpp | 2 +- 5 files changed, 25 insertions(+), 21 deletions(-) diff --git a/contract/include/evm_runtime/evm_contract.hpp b/contract/include/evm_runtime/evm_contract.hpp index d0e9b7d8..83387e5a 100644 --- a/contract/include/evm_runtime/evm_contract.hpp +++ b/contract/include/evm_runtime/evm_contract.hpp @@ -89,7 +89,7 @@ class [[eosio::contract]] evm_contract : public contract [[eosio::action]] void setkvstore(uint64_t account_id, const bytes& key, const std::optional& value); [[eosio::action]] void rmaccount(uint64_t id); [[eosio::action]] void addevmbal(uint64_t id, const bytes& delta, bool subtract); - [[eosio::action]] void addopenbal(name account, const asset& delta); + [[eosio::action]] void addopenbal(name account, const bytes& delta, bool subtract); [[eosio::action]] void freezeaccnt(uint64_t id, bool value); #endif diff --git a/contract/src/admin_actions.cpp b/contract/src/admin_actions.cpp index 459b6b27..5152e181 100644 --- a/contract/src/admin_actions.cpp +++ b/contract/src/admin_actions.cpp @@ -89,17 +89,20 @@ namespace evm_runtime { }); } -[[eosio::action]] void evm_contract::addopenbal(name account, const asset& delta) { +[[eosio::action]] void evm_contract::addopenbal(name account, const bytes& delta, bool subtract) { eosio::require_auth(get_self()); balances open_balances(get_self(), get_self().value); auto itr = open_balances.find(account.value); eosio::check(itr != open_balances.end(), "account not found"); - auto res = itr->balance.balance + delta; - eosio::check(res.amount >= 0, "negative final balance"); + auto d = to_uint256(delta); open_balances.modify(*itr, eosio::same_payer, [&](auto& row){ - row.balance.balance = res; + if(subtract) { + row.balance-=d; + } else { + row.balance+=d; + } }); } diff --git a/contract/tests/admin_actions_tests.cpp b/contract/tests/admin_actions_tests.cpp index bcc77d2d..ed89c498 100644 --- a/contract/tests/admin_actions_tests.cpp +++ b/contract/tests/admin_actions_tests.cpp @@ -393,7 +393,7 @@ BOOST_FIXTURE_TEST_CASE(addevmbal_add_tests, admin_action_tester) try { BOOST_REQUIRE_THROW(check_balances(), std::runtime_error); // We need to substract 0.0001 from the open balance of `evm_account_name` - addopenbal(evm_account_name, make_asset(-1)); + addopenbal(evm_account_name, minimum_natively_representable, true); check_balances(); @@ -402,24 +402,24 @@ BOOST_FIXTURE_TEST_CASE(addevmbal_add_tests, admin_action_tester) try { BOOST_FIXTURE_TEST_CASE(addopenbal_tests, admin_action_tester) try { open("alice"_n); transfer_token("alice"_n, evm_account_name, make_asset(100'0000), "alice"); - - BOOST_REQUIRE_EXCEPTION(addopenbal("beto"_n, make_asset(100'0000), "alice"_n), + + intx::uint256 minimum_natively_representable = intx::exp(10_u256, intx::uint256(18 - 4)); + + BOOST_REQUIRE_EXCEPTION(addopenbal("beto"_n, intx::uint256(100'0000)*minimum_natively_representable, false, "alice"_n), missing_auth_exception, eosio::testing::fc_exception_message_starts_with("missing authority")); - BOOST_REQUIRE_EXCEPTION(addopenbal("beto"_n, make_asset(100'0000)), + BOOST_REQUIRE_EXCEPTION(addopenbal("beto"_n, intx::uint256(100'0000)*minimum_natively_representable, false), eosio_assert_message_exception, eosio_assert_message_is("account not found")); - BOOST_REQUIRE_EXCEPTION(addopenbal("alice"_n, make_asset(-100'0001)), - eosio_assert_message_exception, eosio_assert_message_is("negative final balance")); + BOOST_REQUIRE_EXCEPTION(addopenbal("alice"_n, intx::uint256(100'0001)*minimum_natively_representable, true), + eosio_assert_message_exception, eosio_assert_message_is("decrementing more than available")); - BOOST_REQUIRE_EXCEPTION(addopenbal("alice"_n, make_asset(asset::max_amount-99'9999)), - eosio_assert_message_exception, eosio_assert_message_is("addition overflow")); + BOOST_REQUIRE_EXCEPTION(addopenbal("alice"_n, std::numeric_limits::max()-intx::uint256(99'9999)*minimum_natively_representable, false), + eosio_assert_message_exception, eosio_assert_message_is("accumulation overflow")); check_balances(); - intx::uint256 minimum_natively_representable = intx::exp(10_u256, intx::uint256(18 - 4)); - - addopenbal("alice"_n, make_asset(-1)); + addopenbal("alice"_n, minimum_natively_representable, true); BOOST_REQUIRE(intx::uint256(vault_balance("alice"_n)) == intx::uint256(99'9999)*minimum_natively_representable); // This will fail since the eosio.token balance of `evm_account_name` is 0.0001 greater than the sum of all balances (evm + open balances) @@ -430,7 +430,7 @@ BOOST_FIXTURE_TEST_CASE(addopenbal_tests, admin_action_tester) try { check_balances(); - addopenbal("alice"_n, make_asset(1)); + addopenbal("alice"_n, minimum_natively_representable, false); BOOST_REQUIRE(intx::uint256(vault_balance("alice"_n)) == intx::uint256(100'0000)*minimum_natively_representable); // This will fail since the eosio.token balance of `evm_account_name` is 0.0001 less than the sum of all balances (evm + open balances) @@ -439,7 +439,7 @@ BOOST_FIXTURE_TEST_CASE(addopenbal_tests, admin_action_tester) try { // This combination is to adjust just the eosio.token EOS balance of the `evm_account_name` transfer_token("alice"_n, evm_account_name, make_asset(1), evm_account_name.to_string()); - addopenbal(evm_account_name, make_asset(-1)); + addopenbal(evm_account_name, minimum_natively_representable, true); check_balances(); diff --git a/contract/tests/basic_evm_tester.cpp b/contract/tests/basic_evm_tester.cpp index 87019b97..a3f3dd9d 100644 --- a/contract/tests/basic_evm_tester.cpp +++ b/contract/tests/basic_evm_tester.cpp @@ -350,9 +350,10 @@ transaction_trace_ptr basic_evm_tester::addevmbal(uint64_t id, const intx::uint2 mvo()("id", id)("delta",d)("subtract",subtract)); } -transaction_trace_ptr basic_evm_tester::addopenbal(name account, const asset& delta, name actor) { +transaction_trace_ptr basic_evm_tester::addopenbal(name account, const intx::uint256& delta, bool subtract, name actor) { + auto d = to_bytes(delta); return basic_evm_tester::push_action(evm_account_name, "addopenbal"_n, actor, - mvo()("account", account)("delta",delta)); + mvo()("account", account)("delta",d)("subtract",subtract)); } void basic_evm_tester::pushtx(const silkworm::Transaction& trx, name miner) diff --git a/contract/tests/basic_evm_tester.hpp b/contract/tests/basic_evm_tester.hpp index 4851cf1b..c557ace6 100644 --- a/contract/tests/basic_evm_tester.hpp +++ b/contract/tests/basic_evm_tester.hpp @@ -243,7 +243,7 @@ class basic_evm_tester : public testing::validating_tester transaction_trace_ptr rmaccount(uint64_t id, name actor=evm_account_name); transaction_trace_ptr freezeaccnt(uint64_t id, bool value, name actor=evm_account_name); transaction_trace_ptr addevmbal(uint64_t id, const intx::uint256& delta, bool subtract, name actor=evm_account_name); - transaction_trace_ptr addopenbal(name account, const asset& delta, name actor=evm_account_name); + transaction_trace_ptr addopenbal(name account, const intx::uint256& delta, bool subtract, name actor=evm_account_name); void open(name owner); void close(name owner);