Skip to content

Commit

Permalink
Change data type of value to const byte&
Browse files Browse the repository at this point in the history
  • Loading branch information
yarkinwho committed Aug 11, 2023
1 parent 8101a51 commit 9759072
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 21 deletions.
4 changes: 2 additions & 2 deletions include/evm_runtime/evm_contract.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -85,8 +85,8 @@ class [[eosio::contract]] evm_contract : public contract
[[eosio::action]] bool gc(uint32_t max);


[[eosio::action]] void call(eosio::name from, const bytes& to, uint128_t value, const bytes& data, uint64_t gas_limit);
[[eosio::action]] void admincall(const bytes& from, const bytes& to, uint128_t value, const bytes& data, uint64_t gas_limit);
[[eosio::action]] void call(eosio::name from, const bytes& to, const bytes& value, const bytes& data, uint64_t gas_limit);
[[eosio::action]] void admincall(const bytes& from, const bytes& to, const bytes& value, const bytes& data, uint64_t gas_limit);

#ifdef WITH_TEST_ACTIONS
[[eosio::action]] void testtx(const std::optional<bytes>& orlptx, const evm_runtime::test::block_info& bi);
Expand Down
16 changes: 12 additions & 4 deletions src/actions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -590,16 +590,24 @@ void evm_contract::call_(intx::uint256 s, const bytes& to, intx::uint256 value,
pushtx_act.send(get_self(), rlp);
}

void evm_contract::call(eosio::name from, const bytes& to, uint128_t value, const bytes& data, uint64_t gas_limit) {
void evm_contract::call(eosio::name from, const bytes& to, const bytes& value, const bytes& data, uint64_t gas_limit) {
assert_unfrozen();
require_auth(from);

call_(from.value, to, intx::uint256(intx::uint128(value)), data, gas_limit, get_and_increment_nonce(from));
// Prepare v
eosio::check(value.size() == sizeof(intx::uint256), "invalid value");
intx::uint256 v = intx::be::unsafe::load<intx::uint256>((const uint8_t *)value.data());

call_(from.value, to, v, data, gas_limit, get_and_increment_nonce(from));
}

void evm_contract::admincall(const bytes& from, const bytes& to, uint128_t value, const bytes& data, uint64_t gas_limit) {
void evm_contract::admincall(const bytes& from, const bytes& to, const bytes& value, const bytes& data, uint64_t gas_limit) {
assert_unfrozen();
require_auth(get_self());

// Prepare v
eosio::check(value.size() == sizeof(intx::uint256), "invalid value");
intx::uint256 v = intx::be::unsafe::load<intx::uint256>((const uint8_t *)value.data());

// Prepare s
eosio::check(from.size() == kAddressLength, err_msg_invalid_addr);
Expand All @@ -623,7 +631,7 @@ void evm_contract::admincall(const bytes& from, const bytes& to, uint128_t value
nonce = account->nonce;
}

call_(s, to, intx::uint256(intx::uint128(value)), data, gas_limit, nonce);
call_(s, to, v, data, gas_limit, nonce);
}

#ifdef WITH_TEST_ACTIONS
Expand Down
16 changes: 12 additions & 4 deletions tests/basic_evm_tester.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -291,7 +291,7 @@ transaction_trace_ptr basic_evm_tester::exec(const exec_input& input, const std:
return basic_evm_tester::push_action(evm_account_name, "exec"_n, evm_account_name, bytes{binary_data.begin(), binary_data.end()});
}

void basic_evm_tester::call(name from, const evmc::bytes& to, uint128_t value, evmc::bytes& data, uint64_t gas_limit, name actor)
void basic_evm_tester::call(name from, const evmc::bytes& to, const evmc::bytes& value, evmc::bytes& data, uint64_t gas_limit, name actor)
{
bytes to_bytes;
to_bytes.resize(to.size());
Expand All @@ -301,10 +301,14 @@ void basic_evm_tester::call(name from, const evmc::bytes& to, uint128_t value, e
data_bytes.resize(data.size());
memcpy(data_bytes.data(), data.data(), data.size());

push_action(evm_account_name, "call"_n, actor, mvo()("from", from)("to", to_bytes)("value", value)("data", data_bytes)("gas_limit", gas_limit));
bytes value_bytes;
value_bytes.resize(value.size());
memcpy(value_bytes.data(), value.data(), value.size());

push_action(evm_account_name, "call"_n, actor, mvo()("from", from)("to", to_bytes)("value", value_bytes)("data", data_bytes)("gas_limit", gas_limit));
}

void basic_evm_tester::admincall(const evmc::bytes& from, const evmc::bytes& to, uint128_t value, evmc::bytes& data, uint64_t gas_limit, name actor)
void basic_evm_tester::admincall(const evmc::bytes& from, const evmc::bytes& to, const evmc::bytes& value, evmc::bytes& data, uint64_t gas_limit, name actor)
{
bytes to_bytes;
to_bytes.resize(to.size());
Expand All @@ -318,7 +322,11 @@ void basic_evm_tester::admincall(const evmc::bytes& from, const evmc::bytes& to,
from_bytes.resize(from.size());
memcpy(from_bytes.data(), from.data(), from.size());

push_action(evm_account_name, "admincall"_n, actor, mvo()("from", from_bytes)("to", to_bytes)("value", value)("data", data_bytes)("gas_limit", gas_limit));
bytes value_bytes;
value_bytes.resize(value.size());
memcpy(value_bytes.data(), value.data(), value.size());

push_action(evm_account_name, "admincall"_n, actor, mvo()("from", from_bytes)("to", to_bytes)("value", value_bytes)("data", data_bytes)("gas_limit", gas_limit));
}

void basic_evm_tester::pushtx(const silkworm::Transaction& trx, name miner)
Expand Down
4 changes: 2 additions & 2 deletions tests/basic_evm_tester.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -200,8 +200,8 @@ class basic_evm_tester : public testing::validating_tester

transaction_trace_ptr exec(const exec_input& input, const std::optional<exec_callback>& callback);
void pushtx(const silkworm::Transaction& trx, name miner = evm_account_name);
void call(name from, const evmc::bytes& to, uint128_t value, evmc::bytes& data, uint64_t gas_limit, name actor);
void admincall(const evmc::bytes& from, const evmc::bytes& to, uint128_t value, evmc::bytes& data, uint64_t gas_limit, name actor);
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);
evmc::address deploy_contract(evm_eoa& eoa, evmc::bytes bytecode);

void addegress(const std::vector<name>& accounts);
Expand Down
38 changes: 29 additions & 9 deletions tests/call_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,10 @@ struct call_evm_tester : basic_evm_tester {
data += evmc::from_hex("29e99f07").value(); // sha3(test(uint256))[:4]
data += evmc::bytes32{amount}; // value

call(eos, to, 0, data, 500000, actor);
evmc::bytes32 v;
intx::be::store(v.bytes, intx::uint256(0));

call(eos, to, silkworm::Bytes(v), data, 500000, actor);
}

void call_testpay(const evmc::address& contract_addr, uint128_t amount, name eos, name actor) {
Expand All @@ -80,7 +83,10 @@ struct call_evm_tester : basic_evm_tester {
silkworm::Bytes data;
data += evmc::from_hex("a1a7d817").value(); // sha3(testpay())[:4]

call(eos, to, amount, data, 500000, actor);
evmc::bytes32 v;
intx::be::store(v.bytes, intx::uint256(intx::uint128(amount)));

call(eos, to, silkworm::Bytes(v), data, 500000, actor);
}

void call_notpayable(const evmc::address& contract_addr, uint128_t amount, name eos, name actor) {
Expand All @@ -90,7 +96,10 @@ struct call_evm_tester : basic_evm_tester {
silkworm::Bytes data;
data += evmc::from_hex("d79e1b6a").value(); // sha3(notpayable())[:4]

call(eos, to, amount, data, 500000, actor);
evmc::bytes32 v;
intx::be::store(v.bytes, intx::uint256(intx::uint128(amount)));

call(eos, to, silkworm::Bytes(v), data, 500000, actor);
}

void admincall_testpay(const evmc::address& contract_addr, uint128_t amount, evm_eoa& eoa, name actor) {
Expand All @@ -101,7 +110,10 @@ struct call_evm_tester : basic_evm_tester {
silkworm::Bytes data;
data += evmc::from_hex("a1a7d817").value(); // sha3(testpay())[:4]

admincall(from, to, amount, data, 500000, actor);
evmc::bytes32 v;
intx::be::store(v.bytes, intx::uint256(intx::uint128(amount)));

admincall(from, to, silkworm::Bytes(v), data, 500000, actor);
}

void admincall_notpayable(const evmc::address& contract_addr, uint128_t amount, evm_eoa& eoa, name actor) {
Expand All @@ -112,7 +124,10 @@ struct call_evm_tester : basic_evm_tester {
silkworm::Bytes data;
data += evmc::from_hex("d79e1b6a").value(); // sha3(notpayable())[:4]

admincall(from, to, amount, data, 500000, actor);
evmc::bytes32 v;
intx::be::store(v.bytes, intx::uint256(intx::uint128(amount)));

admincall(from, to, silkworm::Bytes(v), data, 500000, actor);
}

void admincall_test(const evmc::address& contract_addr, uint64_t amount, evm_eoa& eoa, name actor) {
Expand All @@ -122,7 +137,10 @@ struct call_evm_tester : basic_evm_tester {
data += evmc::from_hex("29e99f07").value(); // sha3(test(uint256))[:4]
data += evmc::bytes32{amount}; // value

admincall(from, to, 0, data, 500000, actor);
evmc::bytes32 v;
intx::be::store(v.bytes, intx::uint256(0));

admincall(from, to, silkworm::Bytes(v), data, 500000, actor);
}

intx::uint256 get_count(const evmc::address& contract_addr, std::optional<exec_callback> callback={}, std::optional<bytes> context={}) {
Expand Down Expand Up @@ -446,13 +464,15 @@ BOOST_FIXTURE_TEST_CASE(deploy_contract_function, call_evm_tester) try {
auto alice_addr = make_reserved_address("alice"_n.to_uint64_t());
open("alice"_n);
transfer_token("alice"_n, evm_account_name, make_asset(1000000), "alice");



evmc::bytes32 v;

auto to = evmc::bytes();

auto data = evmc::from_hex(contract_bytecode);

call("alice"_n, to, 0, *data, 1000000, "alice"_n); // nonce 0->1
call("alice"_n, to, silkworm::Bytes(v), *data, 1000000, "alice"_n); // nonce 0->1

auto addr = silkworm::create_address(alice_addr, 0);

Expand All @@ -465,7 +485,7 @@ BOOST_FIXTURE_TEST_CASE(deploy_contract_function, call_evm_tester) try {

auto from = evmc::bytes{std::begin(alice_addr.bytes), std::end(alice_addr.bytes)};

admincall(from, to, 0, *data, 1000000, evm_account_name); // nonce 2->3
admincall(from, to, silkworm::Bytes(v), *data, 1000000, evm_account_name); // nonce 2->3

addr = silkworm::create_address(alice_addr, 2);
call_test(addr, 2222, "alice"_n, "alice"_n); // nonce 3->4
Expand Down

0 comments on commit 9759072

Please sign in to comment.