Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cleanups and LOG fix #120

Merged
merged 5 commits into from
Aug 9, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions lib/evmone/instructions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -697,7 +697,10 @@ void op_log(execution_state& state, instr_argument arg) noexcept

const auto cost = int64_t(s) * 8;
if ((state.gas_left -= cost) < 0)
{
state.exit(EVMC_OUT_OF_GAS);
return;
}

std::array<evmc_bytes32, 4> topics;
for (auto i = 0; i < arg.p.number; ++i)
Expand Down
5 changes: 3 additions & 2 deletions test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,10 @@ add_subdirectory(bench)
add_subdirectory(internal_benchmarks)
add_subdirectory(unittests)

set(targets evm-test evmone-bench evmone-bench-internal evmone-unittests testutils)

set_target_properties(
evm-test evmone-bench evmone-bench-internal evmone-unittests testutils
PROPERTIES
${targets} PROPERTIES
ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/${CMAKE_INSTALL_LIBDIR}
LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/${CMAKE_INSTALL_LIBDIR}
RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/${CMAKE_INSTALL_BINDIR}
Expand Down
6 changes: 6 additions & 0 deletions test/unittests/bytecode_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,12 @@ TEST(bytecode, repeat)
EXPECT_EQ(0 * OP_STOP, "");
}

TEST(bytecode, to_name)
{
EXPECT_EQ(to_name(OP_SAR), "SAR");
EXPECT_EQ(to_name(OP_SAR, EVMC_HOMESTEAD), "UNDEFINED_INSTRUCTION:1d");
}

TEST(bytecode, decode)
{
const auto code = push(0x01e240) + OP_DUP1 + OP_GAS + "cc" + OP_REVERT;
Expand Down
6 changes: 6 additions & 0 deletions test/unittests/evm_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -735,11 +735,17 @@ TEST_F(evm, log_data_cost)
auto num_topics = op - OP_LOG0;
auto code = push(0) + (4 * OP_DUP1) + push(1) + push(0) + op;
auto cost = 407 + num_topics * 375;
EXPECT_EQ(recorded_logs.size(), 0);
execute(cost, code);
EXPECT_EQ(result.status_code, EVMC_SUCCESS);
EXPECT_EQ(recorded_logs.size(), 1);
recorded_logs.clear();

EXPECT_EQ(recorded_logs.size(), 0);
execute(cost - 1, code);
EXPECT_EQ(result.status_code, EVMC_OUT_OF_GAS);
EXPECT_EQ(recorded_logs.size(), 0) << to_name(op);
recorded_logs.clear();
}
}

Expand Down
16 changes: 15 additions & 1 deletion test/utils/bytecode.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#pragma once

#include <test/utils/utils.hpp>
#include <algorithm>

struct bytecode;

Expand Down Expand Up @@ -180,6 +181,19 @@ inline bytecode sload(bytecode index)
return index + OP_SLOAD;
}

inline std::string hex(evmc_opcode opcode) noexcept
{
return hex(static_cast<uint8_t>(opcode));
}

inline std::string to_name(evmc_opcode opcode, evmc_revision rev = EVMC_MAX_REVISION) noexcept
{
const auto names = evmc_get_instruction_names_table(rev);
if (const auto name = names[opcode]; name)
return name;

return "UNDEFINED_INSTRUCTION:" + hex(opcode);
}

inline std::string decode(bytes_view bytecode, evmc_revision rev)
{
Expand All @@ -206,7 +220,7 @@ inline std::string decode(bytes_view bytecode, evmc_revision rev)
}
}
else
s += " + \"" + to_hex({&opcode, 1}) + '"';
s += " + \"" + hex(opcode) + '"';
}

return s;
Expand Down
2 changes: 1 addition & 1 deletion test/utils/host_mock.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ class MockedHost : public evmc::Host
std::vector<log_record> recorded_logs;
std::vector<selfdestuct_record> recorded_selfdestructs;

private:
protected:
std::vector<bytes> m_recorded_calls_inputs;

void record_account_access(const evmc_address& addr)
Expand Down
5 changes: 0 additions & 5 deletions test/utils/utils.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,6 @@ inline std::string hex(uint8_t b) noexcept
return {hex_chars[b >> 4], hex_chars[b & 0xf]};
}

inline std::string hex(evmc_opcode opcode) noexcept
{
return hex(static_cast<uint8_t>(opcode));
}

bytes from_hex(std::string_view hex);
std::string to_hex(bytes_view bytes);

Expand Down