Skip to content

Commit

Permalink
Merge pull request #120 from ethereum/cleanups
Browse files Browse the repository at this point in the history
Cleanups
  • Loading branch information
chfast authored Aug 9, 2019
2 parents 7784826 + f4e1d60 commit e3a8e55
Show file tree
Hide file tree
Showing 7 changed files with 34 additions and 9 deletions.
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

0 comments on commit e3a8e55

Please sign in to comment.