Browse Source

refactor TxToJSON() and ScriptPubKeyToJSON()

0.15
jonnynewbs 8 years ago committed by John Newbery
parent
commit
0ff9320bf5
  1. 2
      src/core_write.cpp
  2. 9
      src/rest.cpp
  3. 5
      src/rpc/blockchain.cpp
  4. 78
      src/rpc/rawtransaction.cpp
  5. 2
      test/util/data/blanktxv1.json
  6. 2
      test/util/data/blanktxv2.json
  7. 2
      test/util/data/tt-delin1-out.json
  8. 2
      test/util/data/tt-delout1-out.json
  9. 2
      test/util/data/tt-locktime317000-out.json
  10. 2
      test/util/data/txcreate1.json
  11. 2
      test/util/data/txcreate2.json
  12. 2
      test/util/data/txcreatedata1.json
  13. 2
      test/util/data/txcreatedata2.json
  14. 2
      test/util/data/txcreatedata_seq0.json
  15. 2
      test/util/data/txcreatedata_seq1.json
  16. 2
      test/util/data/txcreatemultisig1.json
  17. 2
      test/util/data/txcreatemultisig2.json
  18. 2
      test/util/data/txcreatemultisig3.json
  19. 2
      test/util/data/txcreatemultisig4.json
  20. 2
      test/util/data/txcreateoutpubkey1.json
  21. 2
      test/util/data/txcreateoutpubkey2.json
  22. 2
      test/util/data/txcreateoutpubkey3.json
  23. 2
      test/util/data/txcreatescript1.json
  24. 2
      test/util/data/txcreatescript2.json
  25. 2
      test/util/data/txcreatescript3.json
  26. 2
      test/util/data/txcreatescript4.json
  27. 2
      test/util/data/txcreatesignv1.json

2
src/core_write.cpp

@ -151,6 +151,8 @@ void TxToUniv(const CTransaction& tx, const uint256& hashBlock, UniValue& entry) @@ -151,6 +151,8 @@ void TxToUniv(const CTransaction& tx, const uint256& hashBlock, UniValue& entry)
entry.pushKV("txid", tx.GetHash().GetHex());
entry.pushKV("hash", tx.GetWitnessHash().GetHex());
entry.pushKV("version", tx.nVersion);
entry.pushKV("size", (int)::GetSerializeSize(tx, SER_NETWORK, PROTOCOL_VERSION));
entry.pushKV("vsize", (GetTransactionWeight(tx) + WITNESS_SCALE_FACTOR - 1) / WITNESS_SCALE_FACTOR);
entry.pushKV("locktime", (int64_t)tx.nLockTime);
UniValue vin(UniValue::VARR);

9
src/rest.cpp

@ -5,6 +5,7 @@ @@ -5,6 +5,7 @@
#include "chain.h"
#include "chainparams.h"
#include "core_io.h"
#include "primitives/block.h"
#include "primitives/transaction.h"
#include "validation.h"
@ -56,10 +57,6 @@ struct CCoin { @@ -56,10 +57,6 @@ struct CCoin {
}
};
/* Defined in rawtransaction.cpp */
void TxToJSON(const CTransaction& tx, const uint256 hashBlock, UniValue& entry);
void ScriptPubKeyToJSON(const CScript& scriptPubKey, UniValue& out, bool fIncludeHex);
static bool RESTERR(HTTPRequest* req, enum HTTPStatusCode status, std::string message)
{
req->WriteHeader("Content-Type", "text/plain");
@ -383,7 +380,7 @@ static bool rest_tx(HTTPRequest* req, const std::string& strURIPart) @@ -383,7 +380,7 @@ static bool rest_tx(HTTPRequest* req, const std::string& strURIPart)
case RF_JSON: {
UniValue objTx(UniValue::VOBJ);
TxToJSON(*tx, hashBlock, objTx);
TxToUniv(*tx, hashBlock, objTx);
std::string strJSON = objTx.write() + "\n";
req->WriteHeader("Content-Type", "application/json");
req->WriteReply(HTTP_OK, strJSON);
@ -577,7 +574,7 @@ static bool rest_getutxos(HTTPRequest* req, const std::string& strURIPart) @@ -577,7 +574,7 @@ static bool rest_getutxos(HTTPRequest* req, const std::string& strURIPart)
// include the script in a json output
UniValue o(UniValue::VOBJ);
ScriptPubKeyToJSON(coin.out.scriptPubKey, o, true);
ScriptPubKeyToUniv(coin.out.scriptPubKey, o, true);
utxo.push_back(Pair("scriptPubKey", o));
utxos.push_back(utxo);
}

5
src/rpc/blockchain.cpp

@ -12,6 +12,7 @@ @@ -12,6 +12,7 @@
#include "coins.h"
#include "consensus/validation.h"
#include "validation.h"
#include "core_io.h"
#include "policy/policy.h"
#include "primitives/transaction.h"
#include "rpc/server.h"
@ -123,7 +124,7 @@ UniValue blockToJSON(const CBlock& block, const CBlockIndex* blockindex, bool tx @@ -123,7 +124,7 @@ UniValue blockToJSON(const CBlock& block, const CBlockIndex* blockindex, bool tx
if(txDetails)
{
UniValue objTx(UniValue::VOBJ);
TxToJSON(*tx, uint256(), objTx);
TxToUniv(*tx, uint256(), objTx);
txs.push_back(objTx);
}
else
@ -975,7 +976,7 @@ UniValue gettxout(const JSONRPCRequest& request) @@ -975,7 +976,7 @@ UniValue gettxout(const JSONRPCRequest& request)
ret.push_back(Pair("confirmations", pindex->nHeight - coins.nHeight + 1));
ret.push_back(Pair("value", ValueFromAmount(coins.vout[n].nValue)));
UniValue o(UniValue::VOBJ);
ScriptPubKeyToJSON(coins.vout[n].scriptPubKey, o, true);
ScriptPubKeyToUniv(coins.vout[n].scriptPubKey, o, true);
ret.push_back(Pair("scriptPubKey", o));
ret.push_back(Pair("version", coins.nVersion));
ret.push_back(Pair("coinbase", coins.fCoinBase));

78
src/rpc/rawtransaction.cpp

@ -34,77 +34,15 @@ @@ -34,77 +34,15 @@
#include <univalue.h>
void ScriptPubKeyToJSON(const CScript& scriptPubKey, UniValue& out, bool fIncludeHex)
{
txnouttype type;
std::vector<CTxDestination> addresses;
int nRequired;
out.push_back(Pair("asm", ScriptToAsmStr(scriptPubKey)));
if (fIncludeHex)
out.push_back(Pair("hex", HexStr(scriptPubKey.begin(), scriptPubKey.end())));
if (!ExtractDestinations(scriptPubKey, type, addresses, nRequired)) {
out.push_back(Pair("type", GetTxnOutputType(type)));
return;
}
out.push_back(Pair("reqSigs", nRequired));
out.push_back(Pair("type", GetTxnOutputType(type)));
UniValue a(UniValue::VARR);
BOOST_FOREACH(const CTxDestination& addr, addresses)
a.push_back(CBitcoinAddress(addr).ToString());
out.push_back(Pair("addresses", a));
}
void TxToJSON(const CTransaction& tx, const uint256 hashBlock, UniValue& entry)
{
entry.push_back(Pair("txid", tx.GetHash().GetHex()));
entry.push_back(Pair("hash", tx.GetWitnessHash().GetHex()));
entry.push_back(Pair("size", (int)::GetSerializeSize(tx, SER_NETWORK, PROTOCOL_VERSION)));
entry.push_back(Pair("vsize", (int)::GetVirtualTransactionSize(tx)));
entry.push_back(Pair("version", tx.nVersion));
entry.push_back(Pair("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.push_back(Pair("coinbase", HexStr(txin.scriptSig.begin(), txin.scriptSig.end())));
else {
in.push_back(Pair("txid", txin.prevout.hash.GetHex()));
in.push_back(Pair("vout", (int64_t)txin.prevout.n));
UniValue o(UniValue::VOBJ);
o.push_back(Pair("asm", ScriptToAsmStr(txin.scriptSig, true)));
o.push_back(Pair("hex", HexStr(txin.scriptSig.begin(), txin.scriptSig.end())));
in.push_back(Pair("scriptSig", o));
}
if (tx.HasWitness()) {
UniValue txinwitness(UniValue::VARR);
for (unsigned int j = 0; j < tx.vin[i].scriptWitness.stack.size(); j++) {
std::vector<unsigned char> item = tx.vin[i].scriptWitness.stack[j];
txinwitness.push_back(HexStr(item.begin(), item.end()));
}
in.push_back(Pair("txinwitness", txinwitness));
}
in.push_back(Pair("sequence", (int64_t)txin.nSequence));
vin.push_back(in);
}
entry.push_back(Pair("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.push_back(Pair("value", ValueFromAmount(txout.nValue)));
out.push_back(Pair("n", (int64_t)i));
UniValue o(UniValue::VOBJ);
ScriptPubKeyToJSON(txout.scriptPubKey, o, true);
out.push_back(Pair("scriptPubKey", o));
vout.push_back(out);
}
entry.push_back(Pair("vout", vout));
// 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);
if (!hashBlock.IsNull()) {
entry.push_back(Pair("blockhash", hashBlock.GetHex()));
@ -525,7 +463,7 @@ UniValue decoderawtransaction(const JSONRPCRequest& request) @@ -525,7 +463,7 @@ UniValue decoderawtransaction(const JSONRPCRequest& request)
throw JSONRPCError(RPC_DESERIALIZATION_ERROR, "TX decode failed");
UniValue result(UniValue::VOBJ);
TxToJSON(CTransaction(std::move(mtx)), uint256(), result);
TxToUniv(CTransaction(std::move(mtx)), uint256(), result);
return result;
}
@ -565,7 +503,7 @@ UniValue decodescript(const JSONRPCRequest& request) @@ -565,7 +503,7 @@ UniValue decodescript(const JSONRPCRequest& request)
} else {
// Empty scripts are valid
}
ScriptPubKeyToJSON(script, r, false);
ScriptPubKeyToUniv(script, r, false);
UniValue type;
type = find_value(r, "type");

2
test/util/data/blanktxv1.json

@ -2,6 +2,8 @@ @@ -2,6 +2,8 @@
"txid": "d21633ba23f70118185227be58a63527675641ad37967e2aa461559f577aec43",
"hash": "d21633ba23f70118185227be58a63527675641ad37967e2aa461559f577aec43",
"version": 1,
"size": 10,
"vsize": 10,
"locktime": 0,
"vin": [
],

2
test/util/data/blanktxv2.json

@ -2,6 +2,8 @@ @@ -2,6 +2,8 @@
"txid": "4ebd325a4b394cff8c57e8317ccf5a8d0e2bdf1b8526f8aad6c8e43d8240621a",
"hash": "4ebd325a4b394cff8c57e8317ccf5a8d0e2bdf1b8526f8aad6c8e43d8240621a",
"version": 2,
"size": 10,
"vsize": 10,
"locktime": 0,
"vin": [
],

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

@ -2,6 +2,8 @@ @@ -2,6 +2,8 @@
"txid": "81b2035be1da1abe745c6141174a73d151009ec17b3d5ebffa2e177408c50dfd",
"hash": "81b2035be1da1abe745c6141174a73d151009ec17b3d5ebffa2e177408c50dfd",
"version": 1,
"size": 3040,
"vsize": 3040,
"locktime": 0,
"vin": [
{

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

@ -2,6 +2,8 @@ @@ -2,6 +2,8 @@
"txid": "c46ccd75b5050e942b2e86a3648f843f525fe6fc000bf0534ba5973063354493",
"hash": "c46ccd75b5050e942b2e86a3648f843f525fe6fc000bf0534ba5973063354493",
"version": 1,
"size": 3155,
"vsize": 3155,
"locktime": 0,
"vin": [
{

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

@ -2,6 +2,8 @@ @@ -2,6 +2,8 @@
"txid": "aded538f642c17e15f4d3306b8be7e1a4d1ae0c4616d641ab51ea09ba65e5cb5",
"hash": "aded538f642c17e15f4d3306b8be7e1a4d1ae0c4616d641ab51ea09ba65e5cb5",
"version": 1,
"size": 3189,
"vsize": 3189,
"locktime": 317000,
"vin": [
{

2
test/util/data/txcreate1.json

@ -2,6 +2,8 @@ @@ -2,6 +2,8 @@
"txid": "fe7d174f42dce0cffa7a527e9bc8368956057619ec817648f6138b98f2533e8f",
"hash": "fe7d174f42dce0cffa7a527e9bc8368956057619ec817648f6138b98f2533e8f",
"version": 2,
"size": 201,
"vsize": 201,
"locktime": 0,
"vin": [
{

2
test/util/data/txcreate2.json

@ -2,6 +2,8 @@ @@ -2,6 +2,8 @@
"txid": "0481afb29931341d0d7861d8a2f6f26456fa042abf54a23e96440ed7946e0715",
"hash": "0481afb29931341d0d7861d8a2f6f26456fa042abf54a23e96440ed7946e0715",
"version": 2,
"size": 19,
"vsize": 19,
"locktime": 0,
"vin": [
],

2
test/util/data/txcreatedata1.json

@ -2,6 +2,8 @@ @@ -2,6 +2,8 @@
"txid": "07894b4d12fe7853dd911402db1620920d261b9627c447f931417d330c25f06e",
"hash": "07894b4d12fe7853dd911402db1620920d261b9627c447f931417d330c25f06e",
"version": 1,
"size": 176,
"vsize": 176,
"locktime": 0,
"vin": [
{

2
test/util/data/txcreatedata2.json

@ -2,6 +2,8 @@ @@ -2,6 +2,8 @@
"txid": "c14b007fa3a6c1e7765919c1d14c1cfc2b8642c3a5d3be4b1fa8c4ccfec98bb0",
"hash": "c14b007fa3a6c1e7765919c1d14c1cfc2b8642c3a5d3be4b1fa8c4ccfec98bb0",
"version": 2,
"size": 176,
"vsize": 176,
"locktime": 0,
"vin": [
{

2
test/util/data/txcreatedata_seq0.json

@ -2,6 +2,8 @@ @@ -2,6 +2,8 @@
"txid": "8df6ed527472542dd5e137c242a7c5a9f337ac34f7b257ae4af886aeaebb51b0",
"hash": "8df6ed527472542dd5e137c242a7c5a9f337ac34f7b257ae4af886aeaebb51b0",
"version": 2,
"size": 85,
"vsize": 85,
"locktime": 0,
"vin": [
{

2
test/util/data/txcreatedata_seq1.json

@ -2,6 +2,8 @@ @@ -2,6 +2,8 @@
"txid": "c4dea671b0d7b48f8ab10bc46650e8329d3c5766931f548f513847a19f5ba75b",
"hash": "c4dea671b0d7b48f8ab10bc46650e8329d3c5766931f548f513847a19f5ba75b",
"version": 1,
"size": 126,
"vsize": 126,
"locktime": 0,
"vin": [
{

2
test/util/data/txcreatemultisig1.json

@ -2,6 +2,8 @@ @@ -2,6 +2,8 @@
"txid": "0d1d4edfc217d9db3ab6a9298f26a52eae3c52f55a6cb8ccbc14f7c727572894",
"hash": "0d1d4edfc217d9db3ab6a9298f26a52eae3c52f55a6cb8ccbc14f7c727572894",
"version": 1,
"size": 124,
"vsize": 124,
"locktime": 0,
"vin": [
],

2
test/util/data/txcreatemultisig2.json

@ -2,6 +2,8 @@ @@ -2,6 +2,8 @@
"txid": "0d861f278a3b7bce7cb5a88d71e6e6a903336f95ad5a2c29b295b63835b6eee3",
"hash": "0d861f278a3b7bce7cb5a88d71e6e6a903336f95ad5a2c29b295b63835b6eee3",
"version": 1,
"size": 42,
"vsize": 42,
"locktime": 0,
"vin": [
],

2
test/util/data/txcreatemultisig3.json

@ -2,6 +2,8 @@ @@ -2,6 +2,8 @@
"txid": "ccc552220b46a3b5140048b03395987ce4f0fa1ddf8c635bba1fa44e0f8c1d7f",
"hash": "ccc552220b46a3b5140048b03395987ce4f0fa1ddf8c635bba1fa44e0f8c1d7f",
"version": 1,
"size": 53,
"vsize": 53,
"locktime": 0,
"vin": [
],

2
test/util/data/txcreatemultisig4.json

@ -2,6 +2,8 @@ @@ -2,6 +2,8 @@
"txid": "5e8b1cc73234e208d4b7ca9075f136b908c34101be7a048df4ba9ac758b61567",
"hash": "5e8b1cc73234e208d4b7ca9075f136b908c34101be7a048df4ba9ac758b61567",
"version": 1,
"size": 42,
"vsize": 42,
"locktime": 0,
"vin": [
],

2
test/util/data/txcreateoutpubkey1.json

@ -2,6 +2,8 @@ @@ -2,6 +2,8 @@
"txid": "f42b38ac12e3fafc96ba1a9ba70cbfe326744aef75df5fb9db5d6e2855ca415f",
"hash": "f42b38ac12e3fafc96ba1a9ba70cbfe326744aef75df5fb9db5d6e2855ca415f",
"version": 1,
"size": 54,
"vsize": 54,
"locktime": 0,
"vin": [
],

2
test/util/data/txcreateoutpubkey2.json

@ -2,6 +2,8 @@ @@ -2,6 +2,8 @@
"txid": "70f2a088cde460e677415fa1fb71895e90c231e6ed38ed203a35b6f848e9cc73",
"hash": "70f2a088cde460e677415fa1fb71895e90c231e6ed38ed203a35b6f848e9cc73",
"version": 1,
"size": 41,
"vsize": 41,
"locktime": 0,
"vin": [
],

2
test/util/data/txcreateoutpubkey3.json

@ -2,6 +2,8 @@ @@ -2,6 +2,8 @@
"txid": "bfc7e898ee9f6a9652d7b8cca147e2da134502e2ada0f279ed634fc8cf833f8c",
"hash": "bfc7e898ee9f6a9652d7b8cca147e2da134502e2ada0f279ed634fc8cf833f8c",
"version": 1,
"size": 42,
"vsize": 42,
"locktime": 0,
"vin": [
],

2
test/util/data/txcreatescript1.json

@ -2,6 +2,8 @@ @@ -2,6 +2,8 @@
"txid": "f0851b68202f736b792649cfc960259c2374badcb644ab20cac726b5f72f61c9",
"hash": "f0851b68202f736b792649cfc960259c2374badcb644ab20cac726b5f72f61c9",
"version": 1,
"size": 20,
"vsize": 20,
"locktime": 0,
"vin": [
],

2
test/util/data/txcreatescript2.json

@ -2,6 +2,8 @@ @@ -2,6 +2,8 @@
"txid": "6e07a7cc075e0703f32ee8c4e5373fe654bfbc315148fda364e1be286ff290d0",
"hash": "6e07a7cc075e0703f32ee8c4e5373fe654bfbc315148fda364e1be286ff290d0",
"version": 1,
"size": 42,
"vsize": 42,
"locktime": 0,
"vin": [
],

2
test/util/data/txcreatescript3.json

@ -2,6 +2,8 @@ @@ -2,6 +2,8 @@
"txid": "8a234037b088e987c877030efc83374a07441c321bf9dc6dd2f206bc26507df8",
"hash": "8a234037b088e987c877030efc83374a07441c321bf9dc6dd2f206bc26507df8",
"version": 1,
"size": 53,
"vsize": 53,
"locktime": 0,
"vin": [
],

2
test/util/data/txcreatescript4.json

@ -2,6 +2,8 @@ @@ -2,6 +2,8 @@
"txid": "24225cf5e9391100d6b218134b9f03383ca4c880a1f634ac12990cf28b66adbc",
"hash": "24225cf5e9391100d6b218134b9f03383ca4c880a1f634ac12990cf28b66adbc",
"version": 1,
"size": 42,
"vsize": 42,
"locktime": 0,
"vin": [
],

2
test/util/data/txcreatesignv1.json

@ -2,6 +2,8 @@ @@ -2,6 +2,8 @@
"txid": "977e7cd286cb72cd470d539ba6cb48400f8f387d97451d45cdb8819437a303af",
"hash": "977e7cd286cb72cd470d539ba6cb48400f8f387d97451d45cdb8819437a303af",
"version": 1,
"size": 224,
"vsize": 224,
"locktime": 0,
"vin": [
{

Loading…
Cancel
Save