Skip to content

Commit 42d102c

Browse files
committed
refactor TxToJSON() and ScriptPubKeyToJSON()
1 parent d8b4b63 commit 42d102c

File tree

5 files changed

+19
-83
lines changed

5 files changed

+19
-83
lines changed

src/core_write.cpp

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

157159
UniValue vin(UniValue::VARR);

src/rest.cpp

Lines changed: 3 additions & 4 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 "main.h"
@@ -58,11 +59,9 @@ struct CCoin {
5859
}
5960
};
6061

61-
extern void TxToJSON(const CTransaction& tx, const uint256 hashBlock, UniValue& entry);
6262
extern UniValue blockToJSON(const CBlock& block, const CBlockIndex* blockindex, bool txDetails = false);
6363
extern UniValue mempoolInfoToJSON();
6464
extern UniValue mempoolToJSON(bool fVerbose = false);
65-
extern void ScriptPubKeyToJSON(const CScript& scriptPubKey, UniValue& out, bool fIncludeHex);
6665
extern UniValue blockheaderToJSON(const CBlockIndex* blockindex);
6766

6867
static bool RESTERR(HTTPRequest* req, enum HTTPStatusCode status, string message)
@@ -387,7 +386,7 @@ static bool rest_tx(HTTPRequest* req, const std::string& strURIPart)
387386

388387
case RF_JSON: {
389388
UniValue objTx(UniValue::VOBJ);
390-
TxToJSON(tx, hashBlock, objTx);
389+
TxToUniv(tx, hashBlock, objTx);
391390
string strJSON = objTx.write() + "\n";
392391
req->WriteHeader("Content-Type", "application/json");
393392
req->WriteReply(HTTP_OK, strJSON);
@@ -578,7 +577,7 @@ static bool rest_getutxos(HTTPRequest* req, const std::string& strURIPart)
578577

579578
// include the script in a json output
580579
UniValue o(UniValue::VOBJ);
581-
ScriptPubKeyToJSON(coin.out.scriptPubKey, o, true);
580+
ScriptPubKeyToUniv(coin.out.scriptPubKey, o, true);
582581
utxo.push_back(Pair("scriptPubKey", o));
583582
utxos.push_back(utxo);
584583
}

src/rpc/blockchain.cpp

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#include "checkpoints.h"
1010
#include "coins.h"
1111
#include "consensus/validation.h"
12+
#include "core_io.h"
1213
#include "main.h"
1314
#include "policy/policy.h"
1415
#include "primitives/transaction.h"
@@ -40,9 +41,6 @@ static std::mutex cs_blockchange;
4041
static std::condition_variable cond_blockchange;
4142
static CUpdatedBlock latestblock;
4243

43-
extern void TxToJSON(const CTransaction& tx, const uint256 hashBlock, UniValue& entry);
44-
void ScriptPubKeyToJSON(const CScript& scriptPubKey, UniValue& out, bool fIncludeHex);
45-
4644
double GetDifficulty(const CBlockIndex* blockindex)
4745
{
4846
// Floating point number that is a multiple of the minimum difficulty,
@@ -124,7 +122,7 @@ UniValue blockToJSON(const CBlock& block, const CBlockIndex* blockindex, bool tx
124122
if(txDetails)
125123
{
126124
UniValue objTx(UniValue::VOBJ);
127-
TxToJSON(tx, uint256(), objTx);
125+
TxToUniv(tx, uint256(), objTx);
128126
txs.push_back(objTx);
129127
}
130128
else
@@ -924,7 +922,7 @@ UniValue gettxout(const UniValue& params, bool fHelp)
924922
ret.push_back(Pair("confirmations", pindex->nHeight - coins.nHeight + 1));
925923
ret.push_back(Pair("value", ValueFromAmount(coins.vout[n].nValue)));
926924
UniValue o(UniValue::VOBJ);
927-
ScriptPubKeyToJSON(coins.vout[n].scriptPubKey, o, true);
925+
ScriptPubKeyToUniv(coins.vout[n].scriptPubKey, o, true);
928926
ret.push_back(Pair("scriptPubKey", o));
929927
ret.push_back(Pair("version", coins.nVersion));
930928
ret.push_back(Pair("coinbase", coins.fCoinBase));

src/rpc/rawtransaction.cpp

Lines changed: 8 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -35,80 +35,14 @@
3535

3636
using namespace std;
3737

38-
void ScriptPubKeyToJSON(const CScript& scriptPubKey, UniValue& out, bool fIncludeHex)
39-
{
40-
txnouttype type;
41-
vector<CTxDestination> addresses;
42-
int nRequired;
43-
44-
out.push_back(Pair("asm", ScriptToAsmStr(scriptPubKey)));
45-
if (fIncludeHex)
46-
out.push_back(Pair("hex", HexStr(scriptPubKey.begin(), scriptPubKey.end())));
47-
48-
if (!ExtractDestinations(scriptPubKey, type, addresses, nRequired)) {
49-
out.push_back(Pair("type", GetTxnOutputType(type)));
50-
return;
51-
}
52-
53-
out.push_back(Pair("reqSigs", nRequired));
54-
out.push_back(Pair("type", GetTxnOutputType(type)));
55-
56-
UniValue a(UniValue::VARR);
57-
BOOST_FOREACH(const CTxDestination& addr, addresses)
58-
a.push_back(CBitcoinAddress(addr).ToString());
59-
out.push_back(Pair("addresses", a));
60-
}
61-
6238
void TxToJSON(const CTransaction& tx, const uint256 hashBlock, UniValue& entry)
6339
{
64-
entry.push_back(Pair("txid", tx.GetHash().GetHex()));
65-
entry.push_back(Pair("hash", tx.GetWitnessHash().GetHex()));
66-
entry.push_back(Pair("size", (int)::GetSerializeSize(tx, SER_NETWORK, PROTOCOL_VERSION)));
67-
entry.push_back(Pair("vsize", (int)::GetVirtualTransactionSize(tx)));
68-
entry.push_back(Pair("version", tx.nVersion));
69-
entry.push_back(Pair("locktime", (int64_t)tx.nLockTime));
70-
71-
UniValue vin(UniValue::VARR);
72-
for (unsigned int i = 0; i < tx.vin.size(); i++) {
73-
const CTxIn& txin = tx.vin[i];
74-
UniValue in(UniValue::VOBJ);
75-
if (tx.IsCoinBase())
76-
in.push_back(Pair("coinbase", HexStr(txin.scriptSig.begin(), txin.scriptSig.end())));
77-
else {
78-
in.push_back(Pair("txid", txin.prevout.hash.GetHex()));
79-
in.push_back(Pair("vout", (int64_t)txin.prevout.n));
80-
UniValue o(UniValue::VOBJ);
81-
o.push_back(Pair("asm", ScriptToAsmStr(txin.scriptSig, true)));
82-
o.push_back(Pair("hex", HexStr(txin.scriptSig.begin(), txin.scriptSig.end())));
83-
in.push_back(Pair("scriptSig", o));
84-
}
85-
if (!tx.wit.IsNull()) {
86-
if (!tx.wit.vtxinwit[i].IsNull()) {
87-
UniValue txinwitness(UniValue::VARR);
88-
for (unsigned int j = 0; j < tx.wit.vtxinwit[i].scriptWitness.stack.size(); j++) {
89-
std::vector<unsigned char> item = tx.wit.vtxinwit[i].scriptWitness.stack[j];
90-
txinwitness.push_back(HexStr(item.begin(), item.end()));
91-
}
92-
in.push_back(Pair("txinwitness", txinwitness));
93-
}
94-
95-
}
96-
in.push_back(Pair("sequence", (int64_t)txin.nSequence));
97-
vin.push_back(in);
98-
}
99-
entry.push_back(Pair("vin", vin));
100-
UniValue vout(UniValue::VARR);
101-
for (unsigned int i = 0; i < tx.vout.size(); i++) {
102-
const CTxOut& txout = tx.vout[i];
103-
UniValue out(UniValue::VOBJ);
104-
out.push_back(Pair("value", ValueFromAmount(txout.nValue)));
105-
out.push_back(Pair("n", (int64_t)i));
106-
UniValue o(UniValue::VOBJ);
107-
ScriptPubKeyToJSON(txout.scriptPubKey, o, true);
108-
out.push_back(Pair("scriptPubKey", o));
109-
vout.push_back(out);
110-
}
111-
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);
11246

11347
if (!hashBlock.IsNull()) {
11448
entry.push_back(Pair("blockhash", hashBlock.GetHex()));
@@ -512,7 +446,7 @@ UniValue decoderawtransaction(const UniValue& params, bool fHelp)
512446
throw JSONRPCError(RPC_DESERIALIZATION_ERROR, "TX decode failed");
513447

514448
UniValue result(UniValue::VOBJ);
515-
TxToJSON(tx, uint256(), result);
449+
TxToUniv(tx, uint256(), result);
516450

517451
return result;
518452
}
@@ -552,7 +486,7 @@ UniValue decodescript(const UniValue& params, bool fHelp)
552486
} else {
553487
// Empty scripts are valid
554488
}
555-
ScriptPubKeyToJSON(script, r, false);
489+
ScriptPubKeyToUniv(script, r, false);
556490

557491
r.push_back(Pair("p2sh", CBitcoinAddress(CScriptID(script)).ToString()));
558492
return r;

src/rpc/server.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ namespace RPCServer
3131

3232
class CBlockIndex;
3333
class CNetAddr;
34+
class CTransaction;
3435

3536
/** Wrapper for UniValue::VType, which includes typeAny:
3637
* Used to denote don't care type. Only used by RPCTypeCheckObj */
@@ -188,6 +189,8 @@ extern std::string HelpRequiringPassphrase();
188189
extern std::string HelpExampleCli(const std::string& methodname, const std::string& args);
189190
extern std::string HelpExampleRpc(const std::string& methodname, const std::string& args);
190191

192+
extern void TxToJSON(const CTransaction& tx, const uint256 hashBlock, UniValue& entry);
193+
191194
extern void EnsureWalletIsUnlocked();
192195

193196
bool StartRPC();

0 commit comments

Comments
 (0)