Skip to content

Commit

Permalink
C++: Add convenient result constructors
Browse files Browse the repository at this point in the history
  • Loading branch information
chfast committed Aug 4, 2022
1 parent 000b060 commit 1e257b5
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 15 deletions.
2 changes: 1 addition & 1 deletion examples/example_host.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ class ExampleHost : public evmc::Host

evmc::result call(const evmc_message& msg) noexcept final
{
return {EVMC_REVERT, msg.gas, msg.input_data, msg.input_size};
return evmc::result{EVMC_REVERT, msg.gas, msg.input_data, msg.input_size};
}

evmc_tx_context get_tx_context() const noexcept final { return tx_context; }
Expand Down
9 changes: 4 additions & 5 deletions include/evmc/evmc.h
Original file line number Diff line number Diff line change
Expand Up @@ -443,12 +443,11 @@ struct evmc_result
evmc_release_result_fn release;

/**
* The address of the contract created by create instructions.
* The address of the possibly created contract.
*
* This field has valid value only if:
* - it is a result of the Host method evmc_host_interface::call
* - and the result describes successful contract creation
* (evmc_result::status_code is ::EVMC_SUCCESS).
* The create address may be provided even though the contract creation has failed
* (evmc_result::status_code is not ::EVMC_SUCCESS). This is useful in situations
* when the address is observable, e.g. access to it remains warm.
* In all other cases the address MUST be null bytes.
*/
evmc_address create_address;
Expand Down
30 changes: 26 additions & 4 deletions include/evmc/evmc.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -346,13 +346,35 @@ class result : private evmc_result
/// @param _gas_left The amount of gas left.
/// @param _output_data The pointer to the output.
/// @param _output_size The output size.
result(evmc_status_code _status_code,
int64_t _gas_left,
const uint8_t* _output_data,
size_t _output_size) noexcept
explicit result(evmc_status_code _status_code,
int64_t _gas_left,
const uint8_t* _output_data,
size_t _output_size) noexcept
: evmc_result{make_result(_status_code, _gas_left, _output_data, _output_size)}
{}

/// Creates the result without output.
///
/// @param _status_code The status code.
/// @param _gas_left The amount of gas left.
explicit result(evmc_status_code _status_code = EVMC_INTERNAL_ERROR,
int64_t _gas_left = 0) noexcept
: evmc_result{make_result(_status_code, _gas_left, nullptr, 0)}
{}

/// Creates the result of contract creation.
///
/// @param _status_code The status code.
/// @param _gas_left The amount of gas left.
/// @param _create_address The address of the possibly created account.
explicit result(evmc_status_code _status_code,
int64_t _gas_left,
const evmc_address& _create_address) noexcept
: evmc_result{make_result(_status_code, _gas_left, nullptr, 0)}
{
create_address = _create_address;
}

/// Converting constructor from raw evmc_result.
///
/// This object takes ownership of the resources of @p res.
Expand Down
7 changes: 2 additions & 5 deletions test/unittests/cpp_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,7 @@ class NullHost : public evmc::Host
const evmc::address& /*beneficiary*/) noexcept final
{}

evmc::result call(const evmc_message& /*msg*/) noexcept final
{
return evmc::result{evmc_result{}};
}
evmc::result call(const evmc_message& /*msg*/) noexcept final { return evmc::result{}; }

evmc_tx_context get_tx_context() const noexcept final { return {}; }

Expand Down Expand Up @@ -788,7 +785,7 @@ TEST(cpp, result_move)

TEST(cpp, result_create_no_output)
{
auto r = evmc::result{EVMC_REVERT, 1, nullptr, 0};
auto r = evmc::result{EVMC_REVERT, 1};
EXPECT_EQ(r.status_code, EVMC_REVERT);
EXPECT_EQ(r.gas_left, 1);
EXPECT_FALSE(r.output_data);
Expand Down

0 comments on commit 1e257b5

Please sign in to comment.