Skip to content

Commit

Permalink
rpcdaemon: fix transaction parsing for ExecutionPayload (#1278)
Browse files Browse the repository at this point in the history
  • Loading branch information
canepat authored Jun 24, 2023
1 parent f866de6 commit 5f3f7ad
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 3 deletions.
11 changes: 8 additions & 3 deletions silkworm/silkrpc/json/execution_payload.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,10 +56,15 @@ void from_json(const nlohmann::json& json, ExecutionPayload& execution_payload)
silkworm::from_hex(json.at("logsBloom").get<std::string>())->data(),
silkworm::kBloomByteLength);
// Parse transactions
std::vector<silkworm::Bytes> transactions;
std::vector<Bytes> transactions;
for (const auto& hex_transaction : json.at("transactions")) {
transactions.push_back(
*silkworm::from_hex(hex_transaction.get<std::string>()));
const auto hex_bytes{from_hex(hex_transaction.get<std::string>())};
if (hex_bytes) {
transactions.push_back(*hex_bytes);
} else {
throw std::system_error{std::make_error_code(std::errc::invalid_argument),
"ExecutionPayload: invalid hex transaction: " + hex_transaction.dump()};
}
}
// Optionally parse withdrawals
std::optional<std::vector<Withdrawal>> withdrawals;
Expand Down
21 changes: 21 additions & 0 deletions silkworm/silkrpc/json/execution_payload_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,27 @@ TEST_CASE("deserialize ExecutionPayloadV2", "[silkworm][rpc][json]") {
{.index = 6, .validator_index = 12, .address = 0xa94f5374fce5edbc8e2a8697c15331677e6ebf0b_address, .amount = 10'000},
});
}
SECTION("invalid hex transaction") {
const auto json = R"({
"parentHash":"0x3b8fb240d288781d4aac94d3fd16809ee413bc99294a085798a589dae51ddd4a",
"feeRecipient":"0xa94f5374fce5edbc8e2a8697c15331677e6ebf0b",
"stateRoot":"0xca3149fa9e37db08d1cd49c9061db1002ef1cd58db2210f2115c8c989b2bdf45",
"receiptsRoot":"0x56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421",
"logsBloom":"0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000",
"prevRandao":"0x0000000000000000000000000000000000000000000000000000000000000001",
"blockNumber":"0x1",
"gasLimit":"0x1c9c380",
"gasUsed":"0x0",
"timestamp":"0x5",
"extraData":"0x",
"baseFeePerGas":"0x7",
"blockHash":"0x3559e851470f6e7bbed1db474980683e8c315bfce99b2a6ef47c057c04de7858",
"transactions":["xyz"],
"withdrawals":[{"address":"0xa94f5374fce5edbc8e2a8697c15331677e6ebf0b","amount":"0x2710","index":"0x6","validatorIndex":"0xc"}]
})"_json;
ExecutionPayload payload;
CHECK_THROWS_AS(from_json(json, payload), std::system_error);
}
}

TEST_CASE("serialize ExecutionPayloadAndValue", "[silkworm][rpc][json]") {
Expand Down

0 comments on commit 5f3f7ad

Please sign in to comment.