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

Blockchain tests support: Fix tx receipt rlp encoding #680

Merged
merged 2 commits into from
Aug 22, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
state: Properly use cumulative_gas_used in receipt encoding
  • Loading branch information
rodiazet committed Aug 22, 2023
commit 77506c272409b2b3877ba06841d09f04833b06d2
5 changes: 3 additions & 2 deletions test/state/state.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,8 @@ std::variant<TransactionReceipt, std::error_code> transition(State& state, const
std::erase_if(state.get_accounts(),
[](const std::pair<const address, Account>& p) noexcept { return p.second.destructed; });

auto receipt = TransactionReceipt{tx.type, result.status_code, gas_used, host.take_logs(), {}};
// Cumulative gas used is unknown in this scope.
chfast marked this conversation as resolved.
Show resolved Hide resolved
chfast marked this conversation as resolved.
Show resolved Hide resolved
TransactionReceipt receipt{tx.type, result.status_code, gas_used, {}, host.take_logs(), {}, {}};

// Cannot put it into constructor call because logs are std::moved from host instance.
receipt.logs_bloom_filter = compute_bloom_filter(receipt.logs);
Expand Down Expand Up @@ -275,7 +276,7 @@ std::variant<TransactionReceipt, std::error_code> transition(State& state, const
{
const auto prefix = receipt.type == Transaction::Type::eip1559 ? bytes{0x02} : bytes{};
return prefix + rlp::encode_tuple(receipt.status == EVMC_SUCCESS,
static_cast<uint64_t>(receipt.gas_used),
static_cast<uint64_t>(receipt.cumulative_gas_used),
bytes_view(receipt.logs_bloom_filter), receipt.logs);
}

Expand Down
14 changes: 14 additions & 0 deletions test/state/state.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -134,11 +134,25 @@ struct Log
std::vector<hash256> topics;
};

/// Transaction Receipt
///
/// This struct is used in two contexts:
/// 1. As the formally specified, RLP-encode transaction receipt included in the Ethereum blocks.
/// 2. As the internal representation of the transaction execution result.
/// These both roles share most, but not all the information. There are some fields that cannot be
/// assigned in the single transaction execution context. There are also fields that are not a part
/// of the RLP-encoded transaction receipts.
/// TODO: Consider splitting the struct into two based on the duality explained above.
struct TransactionReceipt
{
Copy link
Member

Choose a reason for hiding this comment

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

Add documentation of TransactionReceipt.

/// Transaction Receipt
///
/// This struct is used in two contexts:
/// 1. As the formally specified, RLP-encode transaction receipt included in the Ethereum blocks.
/// 2. As the internal representation of the transaction execution result.
/// These both roles share most, but not all the information. There are some fields that cannot be assigned in the single transaction execution context. There are also fields that are not a part of the RLP-encoded transaction receipts.
/// TODO: Consider splitting the struct into two based on the duality explained above.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done

Transaction::Type type = Transaction::Type::legacy;
evmc_status_code status = EVMC_INTERNAL_ERROR;

/// Amount of gas used by this transaction.
chfast marked this conversation as resolved.
Show resolved Hide resolved
int64_t gas_used = 0;

/// Amount of gas used by this and previous transactions in the block.
chfast marked this conversation as resolved.
Show resolved Hide resolved
int64_t cumulative_gas_used = 0;
chfast marked this conversation as resolved.
Show resolved Hide resolved
std::vector<Log> logs;
BloomFilter logs_bloom_filter;
};
Expand Down
1 change: 1 addition & 0 deletions test/t8n/t8n.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,7 @@ int main(int argc, const char* argv[])
j_receipt["transactionHash"] = hex0x(computed_tx_hash);
j_receipt["gasUsed"] = hex0x(static_cast<uint64_t>(receipt.gas_used));
cumulative_gas_used += receipt.gas_used;
receipt.cumulative_gas_used = cumulative_gas_used;
chfast marked this conversation as resolved.
Show resolved Hide resolved
j_receipt["cumulativeGasUsed"] = hex0x(cumulative_gas_used);

j_receipt["blockHash"] = hex0x(bytes32{});
Expand Down
4 changes: 2 additions & 2 deletions test/unittests/state_mpt_hash_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ TEST(state_mpt_hash, legacy_and_eip1559_receipt_three_logs_no_logs)
TransactionReceipt receipt0{};
receipt0.type = evmone::state::Transaction::Type::legacy;
receipt0.status = EVMC_SUCCESS;
receipt0.gas_used = 0x24522;
receipt0.cumulative_gas_used = 0x24522;

Log l0;
l0.addr = 0x84bf5c35c54a994c72ff9d8b4cca8f5034153a2c_address;
Expand Down Expand Up @@ -238,7 +238,7 @@ TEST(state_mpt_hash, legacy_and_eip1559_receipt_three_logs_no_logs)
TransactionReceipt receipt1{};
receipt1.type = evmone::state::Transaction::Type::eip1559;
receipt1.status = EVMC_SUCCESS;
receipt1.gas_used = 0x2cd9b;
receipt1.cumulative_gas_used = 0x2cd9b;
receipt1.logs_bloom_filter = compute_bloom_filter(receipt1.logs);

EXPECT_EQ(mpt_hash(std::array{receipt0, receipt1}),
Expand Down