Skip to content

Commit ae775b5

Browse files
author
Jeff Garzik
committed
Consolidate CTransaction hex encode/decode into core_io.h, core_{read,write}.cpp
1 parent 2920322 commit ae775b5

File tree

7 files changed

+73
-33
lines changed

7 files changed

+73
-33
lines changed

src/Makefile.am

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ BITCOIN_CORE_H = \
7575
coins.h \
7676
compat.h \
7777
core.h \
78+
core_io.h \
7879
crypter.h \
7980
db.h \
8081
hash.h \
@@ -185,6 +186,8 @@ libbitcoin_common_a_SOURCES = \
185186
chainparams.cpp \
186187
coins.cpp \
187188
core.cpp \
189+
core_read.cpp \
190+
core_write.cpp \
188191
hash.cpp \
189192
key.cpp \
190193
keystore.cpp \

src/core_io.h

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
#ifndef __BITCOIN_CORE_IO_H__
2+
#define __BITCOIN_CORE_IO_H__
3+
4+
#include <string>
5+
6+
class CTransaction;
7+
8+
// core_read.cpp
9+
extern bool DecodeHexTx(CTransaction& tx, const std::string& strHexTx);
10+
11+
// core_write.cpp
12+
extern std::string EncodeHexTx(const CTransaction& tx);
13+
14+
#endif // __BITCOIN_CORE_IO_H__

src/core_read.cpp

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
2+
#include <vector>
3+
#include "core_io.h"
4+
#include "core.h"
5+
#include "serialize.h"
6+
7+
using namespace std;
8+
9+
bool DecodeHexTx(CTransaction& tx, const std::string& strHexTx)
10+
{
11+
if (!IsHex(strHexTx))
12+
return false;
13+
14+
vector<unsigned char> txData(ParseHex(strHexTx));
15+
CDataStream ssData(txData, SER_NETWORK, PROTOCOL_VERSION);
16+
try {
17+
ssData >> tx;
18+
}
19+
catch (std::exception &e) {
20+
return false;
21+
}
22+
23+
return true;
24+
}
25+

src/core_write.cpp

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
2+
#include "core_io.h"
3+
#include "core.h"
4+
#include "serialize.h"
5+
#include "util.h"
6+
7+
using namespace std;
8+
9+
string EncodeHexTx(const CTransaction& tx)
10+
{
11+
CDataStream ssTx(SER_NETWORK, PROTOCOL_VERSION);
12+
ssTx << tx;
13+
return HexStr(ssTx.begin(), ssTx.end());
14+
}
15+

src/rpcmining.cpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#include "main.h"
1111
#include "miner.h"
1212
#include "pow.h"
13+
#include "core_io.h"
1314
#ifdef ENABLE_WALLET
1415
#include "db.h"
1516
#include "wallet.h"
@@ -472,9 +473,7 @@ Value getblocktemplate(const Array& params, bool fHelp)
472473

473474
Object entry;
474475

475-
CDataStream ssTx(SER_NETWORK, PROTOCOL_VERSION);
476-
ssTx << tx;
477-
entry.push_back(Pair("data", HexStr(ssTx.begin(), ssTx.end())));
476+
entry.push_back(Pair("data", EncodeHexTx(tx)));
478477

479478
entry.push_back(Pair("hash", txHash.GetHex()));
480479

src/rpcrawtransaction.cpp

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

66
#include "base58.h"
77
#include "core.h"
8+
#include "core_io.h"
89
#include "init.h"
910
#include "keystore.h"
1011
#include "main.h"
@@ -182,9 +183,7 @@ Value getrawtransaction(const Array& params, bool fHelp)
182183
if (!GetTransaction(hash, tx, hashBlock, true))
183184
throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "No information available about transaction");
184185

185-
CDataStream ssTx(SER_NETWORK, PROTOCOL_VERSION);
186-
ssTx << tx;
187-
string strHex = HexStr(ssTx.begin(), ssTx.end());
186+
string strHex = EncodeHexTx(tx);
188187

189188
if (!fVerbose)
190189
return strHex;
@@ -388,9 +387,7 @@ Value createrawtransaction(const Array& params, bool fHelp)
388387
rawTx.vout.push_back(out);
389388
}
390389

391-
CDataStream ss(SER_NETWORK, PROTOCOL_VERSION);
392-
ss << rawTx;
393-
return HexStr(ss.begin(), ss.end());
390+
return EncodeHexTx(rawTx);
394391
}
395392

396393
Value decoderawtransaction(const Array& params, bool fHelp)
@@ -444,15 +441,12 @@ Value decoderawtransaction(const Array& params, bool fHelp)
444441
+ HelpExampleRpc("decoderawtransaction", "\"hexstring\"")
445442
);
446443

447-
vector<unsigned char> txData(ParseHexV(params[0], "argument"));
448-
CDataStream ssData(txData, SER_NETWORK, PROTOCOL_VERSION);
444+
RPCTypeCheck(params, list_of(str_type));
445+
449446
CTransaction tx;
450-
try {
451-
ssData >> tx;
452-
}
453-
catch (std::exception &e) {
447+
448+
if (!DecodeHexTx(tx, params[0].get_str()))
454449
throw JSONRPCError(RPC_DESERIALIZATION_ERROR, "TX decode failed");
455-
}
456450

457451
Object result;
458452
TxToJSON(tx, 0, result);
@@ -723,9 +717,7 @@ Value signrawtransaction(const Array& params, bool fHelp)
723717
}
724718

725719
Object result;
726-
CDataStream ssTx(SER_NETWORK, PROTOCOL_VERSION);
727-
ssTx << mergedTx;
728-
result.push_back(Pair("hex", HexStr(ssTx.begin(), ssTx.end())));
720+
result.push_back(Pair("hex", EncodeHexTx(mergedTx)));
729721
result.push_back(Pair("complete", fComplete));
730722

731723
return result;
@@ -754,25 +746,18 @@ Value sendrawtransaction(const Array& params, bool fHelp)
754746
+ HelpExampleRpc("sendrawtransaction", "\"signedhex\"")
755747
);
756748

749+
RPCTypeCheck(params, list_of(str_type)(bool_type));
757750

758751
// parse hex string from parameter
759-
vector<unsigned char> txData(ParseHexV(params[0], "parameter"));
760-
CDataStream ssData(txData, SER_NETWORK, PROTOCOL_VERSION);
761752
CTransaction tx;
753+
if (!DecodeHexTx(tx, params[0].get_str()))
754+
throw JSONRPCError(RPC_DESERIALIZATION_ERROR, "TX decode failed");
755+
uint256 hashTx = tx.GetHash();
762756

763757
bool fOverrideFees = false;
764758
if (params.size() > 1)
765759
fOverrideFees = params[1].get_bool();
766760

767-
// deserialize binary data stream
768-
try {
769-
ssData >> tx;
770-
}
771-
catch (std::exception &e) {
772-
throw JSONRPCError(RPC_DESERIALIZATION_ERROR, "TX decode failed");
773-
}
774-
const uint256 &hashTx = tx.GetHash();
775-
776761
CCoinsViewCache &view = *pcoinsTip;
777762
CCoins existingCoins;
778763
bool fHaveMempool = mempool.exists(hashTx);

src/rpcwallet.cpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
55

66
#include "base58.h"
7+
#include "core_io.h"
78
#include "rpcserver.h"
89
#include "init.h"
910
#include "net.h"
@@ -1550,9 +1551,7 @@ Value gettransaction(const Array& params, bool fHelp)
15501551
ListTransactions(wtx, "*", 0, false, details, filter);
15511552
entry.push_back(Pair("details", details));
15521553

1553-
CDataStream ssTx(SER_NETWORK, PROTOCOL_VERSION);
1554-
ssTx << static_cast<CTransaction>(wtx);
1555-
string strHex = HexStr(ssTx.begin(), ssTx.end());
1554+
string strHex = EncodeHexTx(static_cast<CTransaction>(wtx));
15561555
entry.push_back(Pair("hex", strHex));
15571556

15581557
return entry;

0 commit comments

Comments
 (0)