diff --git a/src/core_write.cpp b/src/core_write.cpp --- a/src/core_write.cpp +++ b/src/core_write.cpp @@ -189,6 +189,8 @@ entry.pushKV("txid", tx.GetId().GetHex()); entry.pushKV("hash", tx.GetHash().GetHex()); entry.pushKV("version", tx.nVersion); + entry.pushKV("size", + (int)::GetSerializeSize(tx, SER_NETWORK, PROTOCOL_VERSION)); entry.pushKV("locktime", (int64_t)tx.nLockTime); UniValue vin(UniValue::VARR); diff --git a/src/rest.cpp b/src/rest.cpp --- a/src/rest.cpp +++ b/src/rest.cpp @@ -6,6 +6,7 @@ #include "chain.h" #include "chainparams.h" #include "config.h" +#include "core_io.h" #include "httpserver.h" #include "primitives/block.h" #include "primitives/transaction.h" @@ -430,7 +431,7 @@ case RF_JSON: { UniValue objTx(UniValue::VOBJ); - TxToJSON(config, *tx, hashBlock, objTx); + TxToUniv(*tx, hashBlock, objTx); std::string strJSON = objTx.write() + "\n"; req->WriteHeader("Content-Type", "application/json"); req->WriteReply(HTTP_OK, strJSON); @@ -645,7 +646,7 @@ // include the script in a json output UniValue o(UniValue::VOBJ); - ScriptPubKeyToJSON(config, coin.out.scriptPubKey, o, true); + ScriptPubKeyToUniv(coin.out.scriptPubKey, o, true); utxo.pushKV("scriptPubKey", o); utxos.push_back(utxo); } diff --git a/src/rpc/blockchain.cpp b/src/rpc/blockchain.cpp --- a/src/rpc/blockchain.cpp +++ b/src/rpc/blockchain.cpp @@ -12,11 +12,11 @@ #include "coins.h" #include "config.h" #include "consensus/validation.h" +#include "core_io.h" #include "hash.h" #include "policy/policy.h" #include "primitives/transaction.h" #include "rpc/server.h" -#include "rpc/tojson.h" #include "streams.h" #include "sync.h" #include "txdb.h" @@ -118,7 +118,7 @@ for (const auto &tx : block.vtx) { if (txDetails) { UniValue objTx(UniValue::VOBJ); - TxToJSON(config, *tx, uint256(), objTx); + TxToUniv(*tx, uint256(), objTx); txs.push_back(objTx); } else { txs.push_back(tx->GetId().GetHex()); @@ -1147,7 +1147,7 @@ } ret.pushKV("value", ValueFromAmount(coin.GetTxOut().nValue)); UniValue o(UniValue::VOBJ); - ScriptPubKeyToJSON(config, coin.GetTxOut().scriptPubKey, o, true); + ScriptPubKeyToUniv(coin.GetTxOut().scriptPubKey, o, true); ret.pushKV("scriptPubKey", o); ret.pushKV("coinbase", coin.IsCoinBase()); diff --git a/src/rpc/rawtransaction.cpp b/src/rpc/rawtransaction.cpp --- a/src/rpc/rawtransaction.cpp +++ b/src/rpc/rawtransaction.cpp @@ -36,93 +36,14 @@ #include -void ScriptPubKeyToJSON(const Config &config, const CScript &scriptPubKey, - UniValue &out, bool fIncludeHex) { - txnouttype type; - std::vector addresses; - int nRequired; - - out.pushKV("asm", ScriptToAsmStr(scriptPubKey)); - if (fIncludeHex) { - out.pushKV("hex", HexStr(scriptPubKey.begin(), scriptPubKey.end())); - } - - if (!ExtractDestinations(scriptPubKey, type, addresses, nRequired)) { - out.pushKV("type", GetTxnOutputType(type)); - return; - } - - out.pushKV("reqSigs", nRequired); - out.pushKV("type", GetTxnOutputType(type)); - - UniValue a(UniValue::VARR); - for (const CTxDestination &addr : addresses) { - a.push_back(EncodeDestination(addr)); - } - - out.pushKV("addresses", a); -} - -void TxToJSON(const Config &config, const CTransaction &tx, - const uint256 hashBlock, UniValue &entry) { - entry.pushKV("txid", tx.GetId().GetHex()); - entry.pushKV("hash", tx.GetHash().GetHex()); - entry.pushKV("size", - int(::GetSerializeSize(tx, SER_NETWORK, PROTOCOL_VERSION))); - entry.pushKV("version", tx.nVersion); - entry.pushKV("locktime", int64_t(tx.nLockTime)); - - UniValue vin(UniValue::VARR); - for (unsigned int i = 0; i < tx.vin.size(); i++) { - const CTxIn &txin = tx.vin[i]; - UniValue in(UniValue::VOBJ); - if (tx.IsCoinBase()) { - in.pushKV("coinbase", - HexStr(txin.scriptSig.begin(), txin.scriptSig.end())); - } else { - in.pushKV("txid", txin.prevout.GetTxId().GetHex()); - in.pushKV("vout", int64_t(txin.prevout.GetN())); - UniValue o(UniValue::VOBJ); - o.pushKV("asm", ScriptToAsmStr(txin.scriptSig, true)); - o.pushKV("hex", - HexStr(txin.scriptSig.begin(), txin.scriptSig.end())); - in.pushKV("scriptSig", o); - } - - in.pushKV("sequence", int64_t(txin.nSequence)); - vin.push_back(in); - } - - entry.pushKV("vin", vin); - UniValue vout(UniValue::VARR); - for (unsigned int i = 0; i < tx.vout.size(); i++) { - const CTxOut &txout = tx.vout[i]; - UniValue out(UniValue::VOBJ); - out.pushKV("value", ValueFromAmount(txout.nValue)); - out.pushKV("n", int64_t(i)); - UniValue o(UniValue::VOBJ); - ScriptPubKeyToJSON(config, txout.scriptPubKey, o, true); - out.pushKV("scriptPubKey", o); - vout.push_back(out); - } - - entry.pushKV("vout", vout); - - if (!hashBlock.IsNull()) { - entry.pushKV("blockhash", hashBlock.GetHex()); - BlockMap::iterator mi = mapBlockIndex.find(hashBlock); - if (mi != mapBlockIndex.end() && (*mi).second) { - CBlockIndex *pindex = (*mi).second; - if (chainActive.Contains(pindex)) { - entry.pushKV("confirmations", - 1 + chainActive.Height() - pindex->nHeight); - entry.pushKV("time", pindex->GetBlockTime()); - entry.pushKV("blocktime", pindex->GetBlockTime()); - } else { - entry.pushKV("confirmations", 0); - } - } - } +void TxToJSON(const CTransaction &tx, const uint256 hashBlock, + UniValue &entry) { + // Call into TxToUniv() in bitcoin-common to decode the transaction hex. + // + // Blockchain contextual information (confirmations and blocktime) is not + // available to code in bitcoin-common, so we query them here and push the + // data into the returned UniValue. + TxToUniv(tx, uint256(), entry); } static UniValue getrawtransaction(const Config &config, @@ -252,7 +173,7 @@ UniValue result(UniValue::VOBJ); result.pushKV("hex", strHex); - TxToJSON(config, *tx, hashBlock, result); + TxToUniv(*tx, hashBlock, result); return result; } @@ -649,7 +570,7 @@ } UniValue result(UniValue::VOBJ); - TxToJSON(config, CTransaction(std::move(mtx)), uint256(), result); + TxToUniv(CTransaction(std::move(mtx)), uint256(), result); return result; } @@ -693,7 +614,7 @@ // Empty scripts are valid. } - ScriptPubKeyToJSON(config, script, r, false); + ScriptPubKeyToUniv(script, r, false); UniValue type; type = find_value(r, "type"); diff --git a/src/rpc/tojson.h b/src/rpc/tojson.h --- a/src/rpc/tojson.h +++ b/src/rpc/tojson.h @@ -11,8 +11,6 @@ class CScript; -void ScriptPubKeyToJSON(const Config &config, const CScript &scriptPubKey, - UniValue &out, bool fIncludeHex); void TxToJSON(const Config &config, const CTransaction &tx, const uint256 hashBlock, UniValue &entry); UniValue blockToJSON(const Config &config, const CBlock &block, diff --git a/test/util/data/blanktxv1.json b/test/util/data/blanktxv1.json --- a/test/util/data/blanktxv1.json +++ b/test/util/data/blanktxv1.json @@ -2,6 +2,7 @@ "txid": "d21633ba23f70118185227be58a63527675641ad37967e2aa461559f577aec43", "hash": "d21633ba23f70118185227be58a63527675641ad37967e2aa461559f577aec43", "version": 1, + "size": 10, "locktime": 0, "vin": [ ], diff --git a/test/util/data/blanktxv2.json b/test/util/data/blanktxv2.json --- a/test/util/data/blanktxv2.json +++ b/test/util/data/blanktxv2.json @@ -2,6 +2,7 @@ "txid": "4ebd325a4b394cff8c57e8317ccf5a8d0e2bdf1b8526f8aad6c8e43d8240621a", "hash": "4ebd325a4b394cff8c57e8317ccf5a8d0e2bdf1b8526f8aad6c8e43d8240621a", "version": 2, + "size": 10, "locktime": 0, "vin": [ ], diff --git a/test/util/data/tt-delin1-out.json b/test/util/data/tt-delin1-out.json --- a/test/util/data/tt-delin1-out.json +++ b/test/util/data/tt-delin1-out.json @@ -2,6 +2,7 @@ "txid": "81b2035be1da1abe745c6141174a73d151009ec17b3d5ebffa2e177408c50dfd", "hash": "81b2035be1da1abe745c6141174a73d151009ec17b3d5ebffa2e177408c50dfd", "version": 1, + "size": 3040, "locktime": 0, "vin": [ { diff --git a/test/util/data/tt-delout1-out.json b/test/util/data/tt-delout1-out.json --- a/test/util/data/tt-delout1-out.json +++ b/test/util/data/tt-delout1-out.json @@ -2,6 +2,7 @@ "txid": "c46ccd75b5050e942b2e86a3648f843f525fe6fc000bf0534ba5973063354493", "hash": "c46ccd75b5050e942b2e86a3648f843f525fe6fc000bf0534ba5973063354493", "version": 1, + "size": 3155, "locktime": 0, "vin": [ { diff --git a/test/util/data/tt-locktime317000-out.json b/test/util/data/tt-locktime317000-out.json --- a/test/util/data/tt-locktime317000-out.json +++ b/test/util/data/tt-locktime317000-out.json @@ -2,6 +2,7 @@ "txid": "aded538f642c17e15f4d3306b8be7e1a4d1ae0c4616d641ab51ea09ba65e5cb5", "hash": "aded538f642c17e15f4d3306b8be7e1a4d1ae0c4616d641ab51ea09ba65e5cb5", "version": 1, + "size": 3189, "locktime": 317000, "vin": [ { diff --git a/test/util/data/txcreate1.json b/test/util/data/txcreate1.json --- a/test/util/data/txcreate1.json +++ b/test/util/data/txcreate1.json @@ -2,6 +2,7 @@ "txid": "fe7d174f42dce0cffa7a527e9bc8368956057619ec817648f6138b98f2533e8f", "hash": "fe7d174f42dce0cffa7a527e9bc8368956057619ec817648f6138b98f2533e8f", "version": 2, + "size": 201, "locktime": 0, "vin": [ { diff --git a/test/util/data/txcreate2.json b/test/util/data/txcreate2.json --- a/test/util/data/txcreate2.json +++ b/test/util/data/txcreate2.json @@ -2,6 +2,7 @@ "txid": "0481afb29931341d0d7861d8a2f6f26456fa042abf54a23e96440ed7946e0715", "hash": "0481afb29931341d0d7861d8a2f6f26456fa042abf54a23e96440ed7946e0715", "version": 2, + "size": 19, "locktime": 0, "vin": [ ], diff --git a/test/util/data/txcreatedata1.json b/test/util/data/txcreatedata1.json --- a/test/util/data/txcreatedata1.json +++ b/test/util/data/txcreatedata1.json @@ -2,6 +2,7 @@ "txid": "07894b4d12fe7853dd911402db1620920d261b9627c447f931417d330c25f06e", "hash": "07894b4d12fe7853dd911402db1620920d261b9627c447f931417d330c25f06e", "version": 1, + "size": 176, "locktime": 0, "vin": [ { diff --git a/test/util/data/txcreatedata2.json b/test/util/data/txcreatedata2.json --- a/test/util/data/txcreatedata2.json +++ b/test/util/data/txcreatedata2.json @@ -2,6 +2,7 @@ "txid": "c14b007fa3a6c1e7765919c1d14c1cfc2b8642c3a5d3be4b1fa8c4ccfec98bb0", "hash": "c14b007fa3a6c1e7765919c1d14c1cfc2b8642c3a5d3be4b1fa8c4ccfec98bb0", "version": 2, + "size": 176, "locktime": 0, "vin": [ { diff --git a/test/util/data/txcreatedata_seq0.json b/test/util/data/txcreatedata_seq0.json --- a/test/util/data/txcreatedata_seq0.json +++ b/test/util/data/txcreatedata_seq0.json @@ -2,6 +2,7 @@ "txid": "8df6ed527472542dd5e137c242a7c5a9f337ac34f7b257ae4af886aeaebb51b0", "hash": "8df6ed527472542dd5e137c242a7c5a9f337ac34f7b257ae4af886aeaebb51b0", "version": 2, + "size": 85, "locktime": 0, "vin": [ { diff --git a/test/util/data/txcreatedata_seq1.json b/test/util/data/txcreatedata_seq1.json --- a/test/util/data/txcreatedata_seq1.json +++ b/test/util/data/txcreatedata_seq1.json @@ -2,6 +2,7 @@ "txid": "c4dea671b0d7b48f8ab10bc46650e8329d3c5766931f548f513847a19f5ba75b", "hash": "c4dea671b0d7b48f8ab10bc46650e8329d3c5766931f548f513847a19f5ba75b", "version": 1, + "size": 126, "locktime": 0, "vin": [ { diff --git a/test/util/data/txcreatemultisig1.json b/test/util/data/txcreatemultisig1.json --- a/test/util/data/txcreatemultisig1.json +++ b/test/util/data/txcreatemultisig1.json @@ -2,6 +2,7 @@ "txid": "0d1d4edfc217d9db3ab6a9298f26a52eae3c52f55a6cb8ccbc14f7c727572894", "hash": "0d1d4edfc217d9db3ab6a9298f26a52eae3c52f55a6cb8ccbc14f7c727572894", "version": 1, + "size": 124, "locktime": 0, "vin": [ ], diff --git a/test/util/data/txcreatemultisig2.json b/test/util/data/txcreatemultisig2.json --- a/test/util/data/txcreatemultisig2.json +++ b/test/util/data/txcreatemultisig2.json @@ -2,6 +2,7 @@ "txid": "0d861f278a3b7bce7cb5a88d71e6e6a903336f95ad5a2c29b295b63835b6eee3", "hash": "0d861f278a3b7bce7cb5a88d71e6e6a903336f95ad5a2c29b295b63835b6eee3", "version": 1, + "size": 42, "locktime": 0, "vin": [ ], diff --git a/test/util/data/txcreateoutpubkey1.json b/test/util/data/txcreateoutpubkey1.json --- a/test/util/data/txcreateoutpubkey1.json +++ b/test/util/data/txcreateoutpubkey1.json @@ -2,6 +2,7 @@ "txid": "f42b38ac12e3fafc96ba1a9ba70cbfe326744aef75df5fb9db5d6e2855ca415f", "hash": "f42b38ac12e3fafc96ba1a9ba70cbfe326744aef75df5fb9db5d6e2855ca415f", "version": 1, + "size": 54, "locktime": 0, "vin": [ ], diff --git a/test/util/data/txcreatescript1.json b/test/util/data/txcreatescript1.json --- a/test/util/data/txcreatescript1.json +++ b/test/util/data/txcreatescript1.json @@ -2,6 +2,7 @@ "txid": "f0851b68202f736b792649cfc960259c2374badcb644ab20cac726b5f72f61c9", "hash": "f0851b68202f736b792649cfc960259c2374badcb644ab20cac726b5f72f61c9", "version": 1, + "size": 20, "locktime": 0, "vin": [ ], diff --git a/test/util/data/txcreatescript2.json b/test/util/data/txcreatescript2.json --- a/test/util/data/txcreatescript2.json +++ b/test/util/data/txcreatescript2.json @@ -2,6 +2,7 @@ "txid": "6e07a7cc075e0703f32ee8c4e5373fe654bfbc315148fda364e1be286ff290d0", "hash": "6e07a7cc075e0703f32ee8c4e5373fe654bfbc315148fda364e1be286ff290d0", "version": 1, + "size": 42, "locktime": 0, "vin": [ ], diff --git a/test/util/data/txcreatesignv1.json b/test/util/data/txcreatesignv1.json --- a/test/util/data/txcreatesignv1.json +++ b/test/util/data/txcreatesignv1.json @@ -2,6 +2,7 @@ "txid": "bea13b5f794dc351d1d890b525b0af423c49ab49c667c8fd95cbba2fb283cd5e", "hash": "bea13b5f794dc351d1d890b525b0af423c49ab49c667c8fd95cbba2fb283cd5e", "version": 1, + "size": 223, "locktime": 0, "vin": [ {