From e6dcfeec05d6527ab148550836937ff435e6449c Mon Sep 17 00:00:00 2001 From: "Wladimir J. van der Laan" Date: Mon, 27 Mar 2017 12:14:43 +0200 Subject: [PATCH 1/3] refactor: Move GetDifficulty out of `rpc/server.h` It has no business in `rpcserver.h`. Define it in the interface header of the implementation unit `rpcblockchain` where it is defined. Also modernize the signature to: double GetDifficulty(const CBlockIndex* blockindex = nullptr); (remove `extern`, replace `NULL` with `nullptr`) --- src/Makefile.am | 1 + src/rpc/blockchain.cpp | 9 ++------- src/rpc/blockchain.h | 20 ++++++++++++++++++++ src/rpc/mining.cpp | 1 + src/rpc/misc.cpp | 1 + src/rpc/server.h | 1 - 6 files changed, 25 insertions(+), 8 deletions(-) create mode 100644 src/rpc/blockchain.h diff --git a/src/Makefile.am b/src/Makefile.am index 8a3215688..30d027315 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -122,6 +122,7 @@ BITCOIN_CORE_H = \ protocol.h \ random.h \ reverselock.h \ + rpc/blockchain.h \ rpc/client.h \ rpc/protocol.h \ rpc/server.h \ diff --git a/src/rpc/blockchain.cpp b/src/rpc/blockchain.cpp index 96254a8cb..b0da683d9 100644 --- a/src/rpc/blockchain.cpp +++ b/src/rpc/blockchain.cpp @@ -3,6 +3,8 @@ // Distributed under the MIT software license, see the accompanying // file COPYING or http://www.opensource.org/licenses/mit-license.php. +#include "rpc/blockchain.h" + #include "amount.h" #include "chain.h" #include "chainparams.h" @@ -42,13 +44,6 @@ static CUpdatedBlock latestblock; extern void TxToJSON(const CTransaction& tx, const uint256 hashBlock, UniValue& entry); void ScriptPubKeyToJSON(const CScript& scriptPubKey, UniValue& out, bool fIncludeHex); -/** - * Get the difficulty of the net wrt to the given block index, or the chain tip if - * not provided. - * - * @return A floating point number that is a multiple of the main net minimum - * difficulty (4295032833 hashes). - */ double GetDifficulty(const CBlockIndex* blockindex) { if (blockindex == NULL) diff --git a/src/rpc/blockchain.h b/src/rpc/blockchain.h new file mode 100644 index 000000000..69cba8f01 --- /dev/null +++ b/src/rpc/blockchain.h @@ -0,0 +1,20 @@ +// Copyright (c) 2017 The Bitcoin Core developers +// Distributed under the MIT software license, see the accompanying +// file COPYING or http://www.opensource.org/licenses/mit-license.php. + +#ifndef BITCOIN_RPC_BLOCKCHAIN_H +#define BITCOIN_RPC_BLOCKCHAIN_H + +class CBlockIndex; + +/** + * Get the difficulty of the net wrt to the given block index, or the chain tip if + * not provided. + * + * @return A floating point number that is a multiple of the main net minimum + * difficulty (4295032833 hashes). + */ +double GetDifficulty(const CBlockIndex* blockindex = nullptr); + +#endif + diff --git a/src/rpc/mining.cpp b/src/rpc/mining.cpp index 4db8ffaa7..b823c159d 100644 --- a/src/rpc/mining.cpp +++ b/src/rpc/mining.cpp @@ -16,6 +16,7 @@ #include "miner.h" #include "net.h" #include "pow.h" +#include "rpc/blockchain.h" #include "rpc/server.h" #include "txmempool.h" #include "util.h" diff --git a/src/rpc/misc.cpp b/src/rpc/misc.cpp index de1bbe62e..24c5eeffe 100644 --- a/src/rpc/misc.cpp +++ b/src/rpc/misc.cpp @@ -9,6 +9,7 @@ #include "validation.h" #include "net.h" #include "netbase.h" +#include "rpc/blockchain.h" #include "rpc/server.h" #include "timedata.h" #include "util.h" diff --git a/src/rpc/server.h b/src/rpc/server.h index de14c7ed3..c3692d91b 100644 --- a/src/rpc/server.h +++ b/src/rpc/server.h @@ -191,7 +191,6 @@ extern std::vector ParseHexO(const UniValue& o, std::string strKe extern CAmount AmountFromValue(const UniValue& value); extern UniValue ValueFromAmount(const CAmount& amount); -extern double GetDifficulty(const CBlockIndex* blockindex = NULL); extern std::string HelpExampleCli(const std::string& methodname, const std::string& args); extern std::string HelpExampleRpc(const std::string& methodname, const std::string& args); From 8d8f28dd52fc9f78f57842f94842c8433f1811be Mon Sep 17 00:00:00 2001 From: "Wladimir J. van der Laan" Date: Mon, 27 Mar 2017 16:04:31 +0200 Subject: [PATCH 2/3] refactor: Move RPCNotifyBlockChange out of `rpc/server.h` --- src/init.cpp | 1 + src/rpc/blockchain.h | 3 +++ src/rpc/server.h | 1 - 3 files changed, 4 insertions(+), 1 deletion(-) diff --git a/src/init.cpp b/src/init.cpp index a78224d5d..c42c206a5 100644 --- a/src/init.cpp +++ b/src/init.cpp @@ -27,6 +27,7 @@ #include "policy/policy.h" #include "rpc/server.h" #include "rpc/register.h" +#include "rpc/blockchain.h" #include "script/standard.h" #include "script/sigcache.h" #include "scheduler.h" diff --git a/src/rpc/blockchain.h b/src/rpc/blockchain.h index 69cba8f01..8821f297f 100644 --- a/src/rpc/blockchain.h +++ b/src/rpc/blockchain.h @@ -16,5 +16,8 @@ class CBlockIndex; */ double GetDifficulty(const CBlockIndex* blockindex = nullptr); +/** Callback for when block tip changed. */ +void RPCNotifyBlockChange(bool ibd, const CBlockIndex *); + #endif diff --git a/src/rpc/server.h b/src/rpc/server.h index c3692d91b..34a9d3c24 100644 --- a/src/rpc/server.h +++ b/src/rpc/server.h @@ -198,7 +198,6 @@ bool StartRPC(); void InterruptRPC(); void StopRPC(); std::string JSONRPCExecBatch(const UniValue& vReq); -void RPCNotifyBlockChange(bool ibd, const CBlockIndex *); // Retrieves any serialization flags requested in command line argument int RPCSerializationFlags(); From f885b670290d39e697995fd2d35dc36edc104011 Mon Sep 17 00:00:00 2001 From: "Wladimir J. van der Laan" Date: Mon, 27 Mar 2017 16:12:02 +0200 Subject: [PATCH 3/3] refactor: Make rest.cpp dependency on `*toJSON` in `blockchain.cpp` explicit --- src/rest.cpp | 10 ++++------ src/rpc/blockchain.cpp | 4 ++-- src/rpc/blockchain.h | 17 +++++++++++++++++ 3 files changed, 23 insertions(+), 8 deletions(-) diff --git a/src/rest.cpp b/src/rest.cpp index 54eefcafe..9dcaf269d 100644 --- a/src/rest.cpp +++ b/src/rest.cpp @@ -9,6 +9,7 @@ #include "primitives/transaction.h" #include "validation.h" #include "httpserver.h" +#include "rpc/blockchain.h" #include "rpc/server.h" #include "streams.h" #include "sync.h" @@ -55,12 +56,9 @@ struct CCoin { } }; -extern void TxToJSON(const CTransaction& tx, const uint256 hashBlock, UniValue& entry); -extern UniValue blockToJSON(const CBlock& block, const CBlockIndex* blockindex, bool txDetails = false); -extern UniValue mempoolInfoToJSON(); -extern UniValue mempoolToJSON(bool fVerbose = false); -extern void ScriptPubKeyToJSON(const CScript& scriptPubKey, UniValue& out, bool fIncludeHex); -extern UniValue blockheaderToJSON(const CBlockIndex* blockindex); +/* 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) { diff --git a/src/rpc/blockchain.cpp b/src/rpc/blockchain.cpp index b0da683d9..f158065e7 100644 --- a/src/rpc/blockchain.cpp +++ b/src/rpc/blockchain.cpp @@ -101,7 +101,7 @@ UniValue blockheaderToJSON(const CBlockIndex* blockindex) return result; } -UniValue blockToJSON(const CBlock& block, const CBlockIndex* blockindex, bool txDetails = false) +UniValue blockToJSON(const CBlock& block, const CBlockIndex* blockindex, bool txDetails) { UniValue result(UniValue::VOBJ); result.push_back(Pair("hash", blockindex->GetBlockHash().GetHex())); @@ -378,7 +378,7 @@ void entryToJSON(UniValue &info, const CTxMemPoolEntry &e) info.push_back(Pair("depends", depends)); } -UniValue mempoolToJSON(bool fVerbose = false) +UniValue mempoolToJSON(bool fVerbose) { if (fVerbose) { diff --git a/src/rpc/blockchain.h b/src/rpc/blockchain.h index 8821f297f..c021441b0 100644 --- a/src/rpc/blockchain.h +++ b/src/rpc/blockchain.h @@ -5,7 +5,12 @@ #ifndef BITCOIN_RPC_BLOCKCHAIN_H #define BITCOIN_RPC_BLOCKCHAIN_H +class CBlock; class CBlockIndex; +class CScript; +class CTransaction; +class uint256; +class UniValue; /** * Get the difficulty of the net wrt to the given block index, or the chain tip if @@ -19,5 +24,17 @@ double GetDifficulty(const CBlockIndex* blockindex = nullptr); /** Callback for when block tip changed. */ void RPCNotifyBlockChange(bool ibd, const CBlockIndex *); +/** Block description to JSON */ +UniValue blockToJSON(const CBlock& block, const CBlockIndex* blockindex, bool txDetails = false); + +/** Mempool information to JSON */ +UniValue mempoolInfoToJSON(); + +/** Mempool to JSON */ +UniValue mempoolToJSON(bool fVerbose = false); + +/** Block header to JSON */ +UniValue blockheaderToJSON(const CBlockIndex* blockindex); + #endif