|
17 | 17 | #include "eth_api.hpp" |
18 | 18 |
|
19 | 19 | #include <algorithm> |
| 20 | +#include <cstring> |
20 | 21 | #include <exception> |
21 | 22 | #include <iostream> |
22 | 23 | #include <string> |
@@ -581,12 +582,45 @@ asio::awaitable<void> EthereumRpcApi::handle_eth_get_transaction_by_block_number |
581 | 582 |
|
582 | 583 | // https://eth.wiki/json-rpc/API#eth_gettransactionreceipt |
583 | 584 | asio::awaitable<void> EthereumRpcApi::handle_eth_get_transaction_receipt(const nlohmann::json& request, nlohmann::json& reply) { |
| 585 | + auto params = request["params"]; |
| 586 | + if (params.size() != 1) { |
| 587 | + auto error_msg = "invalid eth_getTransactionReceipt params: " + params.dump(); |
| 588 | + SILKRPC_ERROR << error_msg << "\n"; |
| 589 | + reply = make_json_error(request["id"], 100, error_msg); |
| 590 | + co_return; |
| 591 | + } |
| 592 | + auto transaction_hash = params[0].get<evmc::bytes32>(); |
| 593 | + SILKRPC_DEBUG << "transaction_hash: " << transaction_hash << "\n"; |
584 | 594 | auto tx = co_await database_->begin(); |
585 | 595 |
|
586 | 596 | try { |
587 | 597 | ethdb::TransactionDatabase tx_database{*tx}; |
| 598 | + reply = make_json_content(request["id"], nullptr); |
| 599 | + const auto block_with_hash = co_await core::rawdb::read_block_by_transaction_hash(tx_database, transaction_hash); |
| 600 | + auto receipts = co_await core::get_receipts(tx_database, block_with_hash.hash, block_with_hash.block.header.number); |
| 601 | + auto transactions = block_with_hash.block.transactions; |
| 602 | + if (receipts.size() != transactions.size()) { |
| 603 | + throw std::invalid_argument{"Unexpected size for receipts in handle_eth_get_transaction_receipt"}; |
| 604 | + } |
588 | 605 |
|
589 | | - reply = make_json_content(request["id"], to_quantity(0)); |
| 606 | + size_t tx_index = -1; |
| 607 | + for (size_t idx{0}; idx < transactions.size(); idx++) { |
| 608 | + auto ethash_hash{hash_of_transaction(transactions[idx])}; |
| 609 | + |
| 610 | + SILKRPC_TRACE << "tx " << idx << ") hash: " << silkworm::to_bytes32(ethash_hash.bytes) << "\n"; |
| 611 | + if (std::memcmp(transaction_hash.bytes, ethash_hash.bytes, silkworm::kHashLength) == 0) { |
| 612 | + tx_index = idx; |
| 613 | + break; |
| 614 | + } |
| 615 | + } |
| 616 | + |
| 617 | + if (tx_index == -1) { |
| 618 | + throw std::invalid_argument{"Unexpected transaction index in handle_eth_get_transaction_receipt"}; |
| 619 | + } |
| 620 | + reply = make_json_content(request["id"], receipts[tx_index]); |
| 621 | + } catch (const std::invalid_argument& iv) { |
| 622 | + SILKRPC_DEBUG << "invalid_argument: " << iv.what() << " processing request: " << request.dump() << "\n"; |
| 623 | + reply = make_json_content(request["id"], {}); |
590 | 624 | } catch (const std::exception& e) { |
591 | 625 | SILKRPC_ERROR << "exception: " << e.what() << " processing request: " << request.dump() << "\n"; |
592 | 626 | reply = make_json_error(request["id"], 100, e.what()); |
|
0 commit comments