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

C++: Add convenient result constructors #660

Merged
merged 2 commits into from
Aug 10, 2022
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
5 changes: 4 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,11 @@ and this project adheres to [Semantic Versioning].
[#617](https://github.com/ethereum/evmc/pull/617)
- Support for Visual Studio 2022.
[#619](https://github.com/ethereum/evmc/pull/619)
- C++ types `evmc::address` and `evmc::bytes32` are convertible to `std::basic_string_view<uint8_t>`.
- C++ types `evmc::address` and `evmc::bytes32` are convertible to `std::basic_string_view<uint8_t>`
.
[#636](https://github.com/ethereum/evmc/pull/636)
- Convenient constructors for C++ `evmc::result`.
[#660](https://github.com/ethereum/evmc/pull/660)
- Rust: The `EvmcVm::set_option` has been added.
[#614](https://github.com/ethereum/evmc/pull/614)

Expand Down
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.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm, in what case does the address remain warm?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In most cases of failing contact creation, but not all. Obviously.

* In all other cases the address MUST be null bytes.
*/
evmc_address create_address;
Expand Down
36 changes: 30 additions & 6 deletions include/evmc/evmc.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -346,15 +346,39 @@ 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.
explicit result(evmc_result const& res) noexcept : evmc_result{res} {}
///
/// This object takes ownership of the resources of @p res.
explicit result(const evmc_result& res) noexcept : evmc_result{res} {}

/// Destructor responsible for automatically releasing attached resources.
~result() noexcept
Expand All @@ -371,7 +395,7 @@ class result : private evmc_result

/// Move assignment operator.
///
/// The self-assigment MUST never happen.
/// The self-assignment MUST never happen.
///
/// @param other The other result object.
/// @return The reference to the left-hand side object.
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