Consolidate CTransaction hex encode/decode into core_io.h, core_{read,write}.cpp

pull/4332/head
Jeff Garzik 11 years ago
parent 2920322871
commit ae775b5b31

@ -75,6 +75,7 @@ BITCOIN_CORE_H = \
coins.h \ coins.h \
compat.h \ compat.h \
core.h \ core.h \
core_io.h \
crypter.h \ crypter.h \
db.h \ db.h \
hash.h \ hash.h \
@ -185,6 +186,8 @@ libbitcoin_common_a_SOURCES = \
chainparams.cpp \ chainparams.cpp \
coins.cpp \ coins.cpp \
core.cpp \ core.cpp \
core_read.cpp \
core_write.cpp \
hash.cpp \ hash.cpp \
key.cpp \ key.cpp \
keystore.cpp \ keystore.cpp \

@ -0,0 +1,14 @@
#ifndef __BITCOIN_CORE_IO_H__
#define __BITCOIN_CORE_IO_H__
#include <string>
class CTransaction;
// core_read.cpp
extern bool DecodeHexTx(CTransaction& tx, const std::string& strHexTx);
// core_write.cpp
extern std::string EncodeHexTx(const CTransaction& tx);
#endif // __BITCOIN_CORE_IO_H__

@ -0,0 +1,25 @@
#include <vector>
#include "core_io.h"
#include "core.h"
#include "serialize.h"
using namespace std;
bool DecodeHexTx(CTransaction& tx, const std::string& strHexTx)
{
if (!IsHex(strHexTx))
return false;
vector<unsigned char> txData(ParseHex(strHexTx));
CDataStream ssData(txData, SER_NETWORK, PROTOCOL_VERSION);
try {
ssData >> tx;
}
catch (std::exception &e) {
return false;
}
return true;
}

@ -0,0 +1,15 @@
#include "core_io.h"
#include "core.h"
#include "serialize.h"
#include "util.h"
using namespace std;
string EncodeHexTx(const CTransaction& tx)
{
CDataStream ssTx(SER_NETWORK, PROTOCOL_VERSION);
ssTx << tx;
return HexStr(ssTx.begin(), ssTx.end());
}

@ -10,6 +10,7 @@
#include "main.h" #include "main.h"
#include "miner.h" #include "miner.h"
#include "pow.h" #include "pow.h"
#include "core_io.h"
#ifdef ENABLE_WALLET #ifdef ENABLE_WALLET
#include "db.h" #include "db.h"
#include "wallet.h" #include "wallet.h"
@ -472,9 +473,7 @@ Value getblocktemplate(const Array& params, bool fHelp)
Object entry; Object entry;
CDataStream ssTx(SER_NETWORK, PROTOCOL_VERSION); entry.push_back(Pair("data", EncodeHexTx(tx)));
ssTx << tx;
entry.push_back(Pair("data", HexStr(ssTx.begin(), ssTx.end())));
entry.push_back(Pair("hash", txHash.GetHex())); entry.push_back(Pair("hash", txHash.GetHex()));

@ -5,6 +5,7 @@
#include "base58.h" #include "base58.h"
#include "core.h" #include "core.h"
#include "core_io.h"
#include "init.h" #include "init.h"
#include "keystore.h" #include "keystore.h"
#include "main.h" #include "main.h"
@ -182,9 +183,7 @@ Value getrawtransaction(const Array& params, bool fHelp)
if (!GetTransaction(hash, tx, hashBlock, true)) if (!GetTransaction(hash, tx, hashBlock, true))
throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "No information available about transaction"); throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "No information available about transaction");
CDataStream ssTx(SER_NETWORK, PROTOCOL_VERSION); string strHex = EncodeHexTx(tx);
ssTx << tx;
string strHex = HexStr(ssTx.begin(), ssTx.end());
if (!fVerbose) if (!fVerbose)
return strHex; return strHex;
@ -388,9 +387,7 @@ Value createrawtransaction(const Array& params, bool fHelp)
rawTx.vout.push_back(out); rawTx.vout.push_back(out);
} }
CDataStream ss(SER_NETWORK, PROTOCOL_VERSION); return EncodeHexTx(rawTx);
ss << rawTx;
return HexStr(ss.begin(), ss.end());
} }
Value decoderawtransaction(const Array& params, bool fHelp) Value decoderawtransaction(const Array& params, bool fHelp)
@ -444,15 +441,12 @@ Value decoderawtransaction(const Array& params, bool fHelp)
+ HelpExampleRpc("decoderawtransaction", "\"hexstring\"") + HelpExampleRpc("decoderawtransaction", "\"hexstring\"")
); );
vector<unsigned char> txData(ParseHexV(params[0], "argument")); RPCTypeCheck(params, list_of(str_type));
CDataStream ssData(txData, SER_NETWORK, PROTOCOL_VERSION);
CTransaction tx; CTransaction tx;
try {
ssData >> tx; if (!DecodeHexTx(tx, params[0].get_str()))
}
catch (std::exception &e) {
throw JSONRPCError(RPC_DESERIALIZATION_ERROR, "TX decode failed"); throw JSONRPCError(RPC_DESERIALIZATION_ERROR, "TX decode failed");
}
Object result; Object result;
TxToJSON(tx, 0, result); TxToJSON(tx, 0, result);
@ -723,9 +717,7 @@ Value signrawtransaction(const Array& params, bool fHelp)
} }
Object result; Object result;
CDataStream ssTx(SER_NETWORK, PROTOCOL_VERSION); result.push_back(Pair("hex", EncodeHexTx(mergedTx)));
ssTx << mergedTx;
result.push_back(Pair("hex", HexStr(ssTx.begin(), ssTx.end())));
result.push_back(Pair("complete", fComplete)); result.push_back(Pair("complete", fComplete));
return result; return result;
@ -754,25 +746,18 @@ Value sendrawtransaction(const Array& params, bool fHelp)
+ HelpExampleRpc("sendrawtransaction", "\"signedhex\"") + HelpExampleRpc("sendrawtransaction", "\"signedhex\"")
); );
RPCTypeCheck(params, list_of(str_type)(bool_type));
// parse hex string from parameter // parse hex string from parameter
vector<unsigned char> txData(ParseHexV(params[0], "parameter"));
CDataStream ssData(txData, SER_NETWORK, PROTOCOL_VERSION);
CTransaction tx; CTransaction tx;
if (!DecodeHexTx(tx, params[0].get_str()))
throw JSONRPCError(RPC_DESERIALIZATION_ERROR, "TX decode failed");
uint256 hashTx = tx.GetHash();
bool fOverrideFees = false; bool fOverrideFees = false;
if (params.size() > 1) if (params.size() > 1)
fOverrideFees = params[1].get_bool(); fOverrideFees = params[1].get_bool();
// deserialize binary data stream
try {
ssData >> tx;
}
catch (std::exception &e) {
throw JSONRPCError(RPC_DESERIALIZATION_ERROR, "TX decode failed");
}
const uint256 &hashTx = tx.GetHash();
CCoinsViewCache &view = *pcoinsTip; CCoinsViewCache &view = *pcoinsTip;
CCoins existingCoins; CCoins existingCoins;
bool fHaveMempool = mempool.exists(hashTx); bool fHaveMempool = mempool.exists(hashTx);

@ -4,6 +4,7 @@
// file COPYING or http://www.opensource.org/licenses/mit-license.php. // file COPYING or http://www.opensource.org/licenses/mit-license.php.
#include "base58.h" #include "base58.h"
#include "core_io.h"
#include "rpcserver.h" #include "rpcserver.h"
#include "init.h" #include "init.h"
#include "net.h" #include "net.h"
@ -1550,9 +1551,7 @@ Value gettransaction(const Array& params, bool fHelp)
ListTransactions(wtx, "*", 0, false, details, filter); ListTransactions(wtx, "*", 0, false, details, filter);
entry.push_back(Pair("details", details)); entry.push_back(Pair("details", details));
CDataStream ssTx(SER_NETWORK, PROTOCOL_VERSION); string strHex = EncodeHexTx(static_cast<CTransaction>(wtx));
ssTx << static_cast<CTransaction>(wtx);
string strHex = HexStr(ssTx.begin(), ssTx.end());
entry.push_back(Pair("hex", strHex)); entry.push_back(Pair("hex", strHex));
return entry; return entry;

Loading…
Cancel
Save