Skip to content

Commit 9c33ffd

Browse files
committed
Merge #8824: Refactor TxToJSON() and ScriptPubKeyToJSON()
0ff9320 refactor TxToJSON() and ScriptPubKeyToJSON() (jonnynewbs) Tree-SHA512: caf7d590829e221522edd5b1ab8ce67b53a2c6986d3bbe8477eab420b1007bf60f885ed0a25ba9587e468c00768360ddc31db37847e862858573eaed5ed8b0d6
2 parents 8d6d43e + 0ff9320 commit 9c33ffd

27 files changed

+62
-78
lines changed

src/core_write.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,8 @@ void TxToUniv(const CTransaction& tx, const uint256& hashBlock, UniValue& entry)
151151
entry.pushKV("txid", tx.GetHash().GetHex());
152152
entry.pushKV("hash", tx.GetWitnessHash().GetHex());
153153
entry.pushKV("version", tx.nVersion);
154+
entry.pushKV("size", (int)::GetSerializeSize(tx, SER_NETWORK, PROTOCOL_VERSION));
155+
entry.pushKV("vsize", (GetTransactionWeight(tx) + WITNESS_SCALE_FACTOR - 1) / WITNESS_SCALE_FACTOR);
154156
entry.pushKV("locktime", (int64_t)tx.nLockTime);
155157

156158
UniValue vin(UniValue::VARR);

src/rest.cpp

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
#include "chain.h"
77
#include "chainparams.h"
8+
#include "core_io.h"
89
#include "primitives/block.h"
910
#include "primitives/transaction.h"
1011
#include "validation.h"
@@ -56,10 +57,6 @@ struct CCoin {
5657
}
5758
};
5859

59-
/* Defined in rawtransaction.cpp */
60-
void TxToJSON(const CTransaction& tx, const uint256 hashBlock, UniValue& entry);
61-
void ScriptPubKeyToJSON(const CScript& scriptPubKey, UniValue& out, bool fIncludeHex);
62-
6360
static bool RESTERR(HTTPRequest* req, enum HTTPStatusCode status, std::string message)
6461
{
6562
req->WriteHeader("Content-Type", "text/plain");
@@ -383,7 +380,7 @@ static bool rest_tx(HTTPRequest* req, const std::string& strURIPart)
383380

384381
case RF_JSON: {
385382
UniValue objTx(UniValue::VOBJ);
386-
TxToJSON(*tx, hashBlock, objTx);
383+
TxToUniv(*tx, hashBlock, objTx);
387384
std::string strJSON = objTx.write() + "\n";
388385
req->WriteHeader("Content-Type", "application/json");
389386
req->WriteReply(HTTP_OK, strJSON);
@@ -577,7 +574,7 @@ static bool rest_getutxos(HTTPRequest* req, const std::string& strURIPart)
577574

578575
// include the script in a json output
579576
UniValue o(UniValue::VOBJ);
580-
ScriptPubKeyToJSON(coin.out.scriptPubKey, o, true);
577+
ScriptPubKeyToUniv(coin.out.scriptPubKey, o, true);
581578
utxo.push_back(Pair("scriptPubKey", o));
582579
utxos.push_back(utxo);
583580
}

src/rpc/blockchain.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
#include "coins.h"
1313
#include "consensus/validation.h"
1414
#include "validation.h"
15+
#include "core_io.h"
1516
#include "policy/policy.h"
1617
#include "primitives/transaction.h"
1718
#include "rpc/server.h"
@@ -123,7 +124,7 @@ UniValue blockToJSON(const CBlock& block, const CBlockIndex* blockindex, bool tx
123124
if(txDetails)
124125
{
125126
UniValue objTx(UniValue::VOBJ);
126-
TxToJSON(*tx, uint256(), objTx);
127+
TxToUniv(*tx, uint256(), objTx);
127128
txs.push_back(objTx);
128129
}
129130
else
@@ -975,7 +976,7 @@ UniValue gettxout(const JSONRPCRequest& request)
975976
ret.push_back(Pair("confirmations", pindex->nHeight - coins.nHeight + 1));
976977
ret.push_back(Pair("value", ValueFromAmount(coins.vout[n].nValue)));
977978
UniValue o(UniValue::VOBJ);
978-
ScriptPubKeyToJSON(coins.vout[n].scriptPubKey, o, true);
979+
ScriptPubKeyToUniv(coins.vout[n].scriptPubKey, o, true);
979980
ret.push_back(Pair("scriptPubKey", o));
980981
ret.push_back(Pair("version", coins.nVersion));
981982
ret.push_back(Pair("coinbase", coins.fCoinBase));

src/rpc/rawtransaction.cpp

Lines changed: 8 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -34,77 +34,15 @@
3434

3535
#include <univalue.h>
3636

37-
void ScriptPubKeyToJSON(const CScript& scriptPubKey, UniValue& out, bool fIncludeHex)
38-
{
39-
txnouttype type;
40-
std::vector<CTxDestination> addresses;
41-
int nRequired;
42-
43-
out.push_back(Pair("asm", ScriptToAsmStr(scriptPubKey)));
44-
if (fIncludeHex)
45-
out.push_back(Pair("hex", HexStr(scriptPubKey.begin(), scriptPubKey.end())));
46-
47-
if (!ExtractDestinations(scriptPubKey, type, addresses, nRequired)) {
48-
out.push_back(Pair("type", GetTxnOutputType(type)));
49-
return;
50-
}
51-
52-
out.push_back(Pair("reqSigs", nRequired));
53-
out.push_back(Pair("type", GetTxnOutputType(type)));
54-
55-
UniValue a(UniValue::VARR);
56-
BOOST_FOREACH(const CTxDestination& addr, addresses)
57-
a.push_back(CBitcoinAddress(addr).ToString());
58-
out.push_back(Pair("addresses", a));
59-
}
6037

6138
void TxToJSON(const CTransaction& tx, const uint256 hashBlock, UniValue& entry)
6239
{
63-
entry.push_back(Pair("txid", tx.GetHash().GetHex()));
64-
entry.push_back(Pair("hash", tx.GetWitnessHash().GetHex()));
65-
entry.push_back(Pair("size", (int)::GetSerializeSize(tx, SER_NETWORK, PROTOCOL_VERSION)));
66-
entry.push_back(Pair("vsize", (int)::GetVirtualTransactionSize(tx)));
67-
entry.push_back(Pair("version", tx.nVersion));
68-
entry.push_back(Pair("locktime", (int64_t)tx.nLockTime));
69-
70-
UniValue vin(UniValue::VARR);
71-
for (unsigned int i = 0; i < tx.vin.size(); i++) {
72-
const CTxIn& txin = tx.vin[i];
73-
UniValue in(UniValue::VOBJ);
74-
if (tx.IsCoinBase())
75-
in.push_back(Pair("coinbase", HexStr(txin.scriptSig.begin(), txin.scriptSig.end())));
76-
else {
77-
in.push_back(Pair("txid", txin.prevout.hash.GetHex()));
78-
in.push_back(Pair("vout", (int64_t)txin.prevout.n));
79-
UniValue o(UniValue::VOBJ);
80-
o.push_back(Pair("asm", ScriptToAsmStr(txin.scriptSig, true)));
81-
o.push_back(Pair("hex", HexStr(txin.scriptSig.begin(), txin.scriptSig.end())));
82-
in.push_back(Pair("scriptSig", o));
83-
}
84-
if (tx.HasWitness()) {
85-
UniValue txinwitness(UniValue::VARR);
86-
for (unsigned int j = 0; j < tx.vin[i].scriptWitness.stack.size(); j++) {
87-
std::vector<unsigned char> item = tx.vin[i].scriptWitness.stack[j];
88-
txinwitness.push_back(HexStr(item.begin(), item.end()));
89-
}
90-
in.push_back(Pair("txinwitness", txinwitness));
91-
}
92-
in.push_back(Pair("sequence", (int64_t)txin.nSequence));
93-
vin.push_back(in);
94-
}
95-
entry.push_back(Pair("vin", vin));
96-
UniValue vout(UniValue::VARR);
97-
for (unsigned int i = 0; i < tx.vout.size(); i++) {
98-
const CTxOut& txout = tx.vout[i];
99-
UniValue out(UniValue::VOBJ);
100-
out.push_back(Pair("value", ValueFromAmount(txout.nValue)));
101-
out.push_back(Pair("n", (int64_t)i));
102-
UniValue o(UniValue::VOBJ);
103-
ScriptPubKeyToJSON(txout.scriptPubKey, o, true);
104-
out.push_back(Pair("scriptPubKey", o));
105-
vout.push_back(out);
106-
}
107-
entry.push_back(Pair("vout", vout));
40+
// Call into TxToUniv() in bitcoin-common to decode the transaction hex.
41+
//
42+
// Blockchain contextual information (confirmations and blocktime) is not
43+
// available to code in bitcoin-common, so we query them here and push the
44+
// data into the returned UniValue.
45+
TxToUniv(tx, uint256(), entry);
10846

10947
if (!hashBlock.IsNull()) {
11048
entry.push_back(Pair("blockhash", hashBlock.GetHex()));
@@ -525,7 +463,7 @@ UniValue decoderawtransaction(const JSONRPCRequest& request)
525463
throw JSONRPCError(RPC_DESERIALIZATION_ERROR, "TX decode failed");
526464

527465
UniValue result(UniValue::VOBJ);
528-
TxToJSON(CTransaction(std::move(mtx)), uint256(), result);
466+
TxToUniv(CTransaction(std::move(mtx)), uint256(), result);
529467

530468
return result;
531469
}
@@ -565,7 +503,7 @@ UniValue decodescript(const JSONRPCRequest& request)
565503
} else {
566504
// Empty scripts are valid
567505
}
568-
ScriptPubKeyToJSON(script, r, false);
506+
ScriptPubKeyToUniv(script, r, false);
569507

570508
UniValue type;
571509
type = find_value(r, "type");

test/util/data/blanktxv1.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
"txid": "d21633ba23f70118185227be58a63527675641ad37967e2aa461559f577aec43",
33
"hash": "d21633ba23f70118185227be58a63527675641ad37967e2aa461559f577aec43",
44
"version": 1,
5+
"size": 10,
6+
"vsize": 10,
57
"locktime": 0,
68
"vin": [
79
],

test/util/data/blanktxv2.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
"txid": "4ebd325a4b394cff8c57e8317ccf5a8d0e2bdf1b8526f8aad6c8e43d8240621a",
33
"hash": "4ebd325a4b394cff8c57e8317ccf5a8d0e2bdf1b8526f8aad6c8e43d8240621a",
44
"version": 2,
5+
"size": 10,
6+
"vsize": 10,
57
"locktime": 0,
68
"vin": [
79
],

test/util/data/tt-delin1-out.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
"txid": "81b2035be1da1abe745c6141174a73d151009ec17b3d5ebffa2e177408c50dfd",
33
"hash": "81b2035be1da1abe745c6141174a73d151009ec17b3d5ebffa2e177408c50dfd",
44
"version": 1,
5+
"size": 3040,
6+
"vsize": 3040,
57
"locktime": 0,
68
"vin": [
79
{

test/util/data/tt-delout1-out.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
"txid": "c46ccd75b5050e942b2e86a3648f843f525fe6fc000bf0534ba5973063354493",
33
"hash": "c46ccd75b5050e942b2e86a3648f843f525fe6fc000bf0534ba5973063354493",
44
"version": 1,
5+
"size": 3155,
6+
"vsize": 3155,
57
"locktime": 0,
68
"vin": [
79
{

test/util/data/tt-locktime317000-out.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
"txid": "aded538f642c17e15f4d3306b8be7e1a4d1ae0c4616d641ab51ea09ba65e5cb5",
33
"hash": "aded538f642c17e15f4d3306b8be7e1a4d1ae0c4616d641ab51ea09ba65e5cb5",
44
"version": 1,
5+
"size": 3189,
6+
"vsize": 3189,
57
"locktime": 317000,
68
"vin": [
79
{

test/util/data/txcreate1.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
"txid": "fe7d174f42dce0cffa7a527e9bc8368956057619ec817648f6138b98f2533e8f",
33
"hash": "fe7d174f42dce0cffa7a527e9bc8368956057619ec817648f6138b98f2533e8f",
44
"version": 2,
5+
"size": 201,
6+
"vsize": 201,
57
"locktime": 0,
68
"vin": [
79
{

0 commit comments

Comments
 (0)