MCPcopy Create free account
hub / github.com/bitcoin/bitcoin / rest_tx

Function rest_tx

src/rest.cpp:839–896  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

837}
838
839static bool rest_tx(const std::any& context, HTTPRequest* req, const std::string& uri_part)
840{
841 if (!CheckWarmup(req))
842 return false;
843 std::string hashStr;
844 const RESTResponseFormat rf = ParseDataFormat(hashStr, uri_part);
845
846 auto hash{Txid::FromHex(hashStr)};
847 if (!hash) {
848 return RESTERR(req, HTTP_BAD_REQUEST, "Invalid hash: " + hashStr);
849 }
850
851 if (g_txindex) {
852 g_txindex->BlockUntilSyncedToCurrentChain();
853 }
854
855 const NodeContext* const node = GetNodeContext(context, req);
856 if (!node) return false;
857 uint256 hashBlock = uint256();
858 const CTransactionRef tx{GetTransaction(/*block_index=*/nullptr, node->mempool.get(), *hash, node->chainman->m_blockman, hashBlock)};
859 if (!tx) {
860 return RESTERR(req, HTTP_NOT_FOUND, hashStr + " not found");
861 }
862
863 switch (rf) {
864 case RESTResponseFormat::BINARY: {
865 DataStream ssTx;
866 ssTx << TX_WITH_WITNESS(tx);
867
868 req->WriteHeader("Content-Type", "application/octet-stream");
869 req->WriteReply(HTTP_OK, ssTx);
870 return true;
871 }
872
873 case RESTResponseFormat::HEX: {
874 DataStream ssTx;
875 ssTx << TX_WITH_WITNESS(tx);
876
877 std::string strHex = HexStr(ssTx) + "\n";
878 req->WriteHeader("Content-Type", "text/plain");
879 req->WriteReply(HTTP_OK, strHex);
880 return true;
881 }
882
883 case RESTResponseFormat::JSON: {
884 UniValue objTx(UniValue::VOBJ);
885 TxToUniv(*tx, /*block_hash=*/hashBlock, /*entry=*/ objTx);
886 std::string strJSON = objTx.write() + "\n";
887 req->WriteHeader("Content-Type", "application/json");
888 req->WriteReply(HTTP_OK, strJSON);
889 return true;
890 }
891
892 default: {
893 return RESTERR(req, HTTP_NOT_FOUND, "output format not found (available: " + AvailableDataFormatsString() + ")");
894 }
895 }
896}

Callers

nothing calls this directly

Calls 15

CheckWarmupFunction · 0.85
ParseDataFormatFunction · 0.85
FromHexFunction · 0.85
RESTERRFunction · 0.85
GetNodeContextFunction · 0.85
GetTransactionFunction · 0.85
TxToUnivFunction · 0.85
WriteHeaderMethod · 0.80
uint256Class · 0.70
HexStrFunction · 0.50

Tested by

no test coverage detected