| 630 | } |
| 631 | |
| 632 | static bool rest_tx(const std::any& context, HTTPRequest* req, const std::string& strURIPart) |
| 633 | { |
| 634 | if (!CheckWarmup(req)) |
| 635 | return false; |
| 636 | std::string hashStr; |
| 637 | const RetFormat rf = ParseDataFormat(hashStr, strURIPart); |
| 638 | |
| 639 | uint256 hash; |
| 640 | if (!ParseHashStr(hashStr, hash)) |
| 641 | return RESTERR(req, HTTP_BAD_REQUEST, "Invalid hash: " + hashStr); |
| 642 | |
| 643 | if (g_txindex) { |
| 644 | g_txindex->BlockUntilSyncedToCurrentChain(); |
| 645 | } |
| 646 | |
| 647 | const NodeContext* const node = GetNodeContext(context, req); |
| 648 | if (!node) return false; |
| 649 | uint256 hashBlock = uint256(); |
| 650 | const CTransactionRef tx = GetTransaction(/* block_index */ nullptr, node->mempool.get(), hash, Params().GetConsensus(), hashBlock); |
| 651 | if (!tx) { |
| 652 | return RESTERR(req, HTTP_NOT_FOUND, hashStr + " not found"); |
| 653 | } |
| 654 | |
| 655 | switch (rf) { |
| 656 | case RetFormat::BINARY: { |
| 657 | CDataStream ssTx(SER_NETWORK, PROTOCOL_VERSION | RPCSerializationFlags()); |
| 658 | ssTx << tx; |
| 659 | |
| 660 | std::string binaryTx = ssTx.str(); |
| 661 | req->WriteHeader("Content-Type", "application/octet-stream"); |
| 662 | req->WriteReply(HTTP_OK, binaryTx); |
| 663 | return true; |
| 664 | } |
| 665 | |
| 666 | case RetFormat::HEX: { |
| 667 | CDataStream ssTx(SER_NETWORK, PROTOCOL_VERSION | RPCSerializationFlags()); |
| 668 | ssTx << tx; |
| 669 | |
| 670 | std::string strHex = HexStr(ssTx) + "\n"; |
| 671 | req->WriteHeader("Content-Type", "text/plain"); |
| 672 | req->WriteReply(HTTP_OK, strHex); |
| 673 | return true; |
| 674 | } |
| 675 | |
| 676 | case RetFormat::JSON: { |
| 677 | UniValue objTx(UniValue::VOBJ); |
| 678 | TxToUniv(*tx, hashBlock, objTx); |
| 679 | std::string strJSON = objTx.write() + "\n"; |
| 680 | req->WriteHeader("Content-Type", "application/json"); |
| 681 | req->WriteReply(HTTP_OK, strJSON); |
| 682 | return true; |
| 683 | } |
| 684 | |
| 685 | default: { |
| 686 | return RESTERR(req, HTTP_NOT_FOUND, "output format not found (available: " + AvailableDataFormatsString() + ")"); |
| 687 | } |
| 688 | } |
| 689 | } |
nothing calls this directly
no test coverage detected