rpc: Pass mempool into MempoolToJSON

This commit is contained in:
MarcoFalke 2019-02-23 11:04:20 -05:00
parent 169dced9a4
commit fa5dc3534b
No known key found for this signature in database
GPG key ID: CE2B75697E69A548
7 changed files with 34 additions and 37 deletions

View file

@ -14,7 +14,7 @@ bool SignalsOptInRBF(const CTransaction &tx)
return false; return false;
} }
RBFTransactionState IsRBFOptIn(const CTransaction &tx, CTxMemPool &pool) RBFTransactionState IsRBFOptIn(const CTransaction& tx, const CTxMemPool& pool)
{ {
AssertLockHeld(pool.cs); AssertLockHeld(pool.cs);

View file

@ -23,6 +23,6 @@ bool SignalsOptInRBF(const CTransaction &tx);
// according to BIP 125 // according to BIP 125
// This involves checking sequence numbers of the transaction, as well // This involves checking sequence numbers of the transaction, as well
// as the sequence numbers of all in-mempool ancestors. // as the sequence numbers of all in-mempool ancestors.
RBFTransactionState IsRBFOptIn(const CTransaction &tx, CTxMemPool &pool) EXCLUSIVE_LOCKS_REQUIRED(pool.cs); RBFTransactionState IsRBFOptIn(const CTransaction& tx, const CTxMemPool& pool) EXCLUSIVE_LOCKS_REQUIRED(pool.cs);
#endif // BITCOIN_POLICY_RBF_H #endif // BITCOIN_POLICY_RBF_H

View file

@ -300,7 +300,7 @@ static bool rest_mempool_info(HTTPRequest* req, const std::string& strURIPart)
switch (rf) { switch (rf) {
case RetFormat::JSON: { case RetFormat::JSON: {
UniValue mempoolInfoObject = mempoolInfoToJSON(); UniValue mempoolInfoObject = MempoolInfoToJSON(::mempool);
std::string strJSON = mempoolInfoObject.write() + "\n"; std::string strJSON = mempoolInfoObject.write() + "\n";
req->WriteHeader("Content-Type", "application/json"); req->WriteHeader("Content-Type", "application/json");
@ -322,7 +322,7 @@ static bool rest_mempool_contents(HTTPRequest* req, const std::string& strURIPar
switch (rf) { switch (rf) {
case RetFormat::JSON: { case RetFormat::JSON: {
UniValue mempoolObject = mempoolToJSON(true); UniValue mempoolObject = MempoolToJSON(::mempool, true);
std::string strJSON = mempoolObject.write() + "\n"; std::string strJSON = mempoolObject.write() + "\n";
req->WriteHeader("Content-Type", "application/json"); req->WriteHeader("Content-Type", "application/json");

View file

@ -405,9 +405,9 @@ static std::string EntryDescriptionString()
" \"bip125-replaceable\" : true|false, (boolean) Whether this transaction could be replaced due to BIP125 (replace-by-fee)\n"; " \"bip125-replaceable\" : true|false, (boolean) Whether this transaction could be replaced due to BIP125 (replace-by-fee)\n";
} }
static void entryToJSON(UniValue &info, const CTxMemPoolEntry &e) EXCLUSIVE_LOCKS_REQUIRED(::mempool.cs) static void entryToJSON(const CTxMemPool& pool, UniValue& info, const CTxMemPoolEntry& e) EXCLUSIVE_LOCKS_REQUIRED(pool.cs)
{ {
AssertLockHeld(mempool.cs); AssertLockHeld(pool.cs);
UniValue fees(UniValue::VOBJ); UniValue fees(UniValue::VOBJ);
fees.pushKV("base", ValueFromAmount(e.GetFee())); fees.pushKV("base", ValueFromAmount(e.GetFee()));
@ -427,12 +427,12 @@ static void entryToJSON(UniValue &info, const CTxMemPoolEntry &e) EXCLUSIVE_LOCK
info.pushKV("ancestorcount", e.GetCountWithAncestors()); info.pushKV("ancestorcount", e.GetCountWithAncestors());
info.pushKV("ancestorsize", e.GetSizeWithAncestors()); info.pushKV("ancestorsize", e.GetSizeWithAncestors());
info.pushKV("ancestorfees", e.GetModFeesWithAncestors()); info.pushKV("ancestorfees", e.GetModFeesWithAncestors());
info.pushKV("wtxid", mempool.vTxHashes[e.vTxHashesIdx].first.ToString()); info.pushKV("wtxid", pool.vTxHashes[e.vTxHashesIdx].first.ToString());
const CTransaction& tx = e.GetTx(); const CTransaction& tx = e.GetTx();
std::set<std::string> setDepends; std::set<std::string> setDepends;
for (const CTxIn& txin : tx.vin) for (const CTxIn& txin : tx.vin)
{ {
if (mempool.exists(txin.prevout.hash)) if (pool.exists(txin.prevout.hash))
setDepends.insert(txin.prevout.hash.ToString()); setDepends.insert(txin.prevout.hash.ToString());
} }
@ -445,8 +445,8 @@ static void entryToJSON(UniValue &info, const CTxMemPoolEntry &e) EXCLUSIVE_LOCK
info.pushKV("depends", depends); info.pushKV("depends", depends);
UniValue spent(UniValue::VARR); UniValue spent(UniValue::VARR);
const CTxMemPool::txiter &it = mempool.mapTx.find(tx.GetHash()); const CTxMemPool::txiter& it = pool.mapTx.find(tx.GetHash());
const CTxMemPool::setEntries &setChildren = mempool.GetMemPoolChildren(it); const CTxMemPool::setEntries& setChildren = pool.GetMemPoolChildren(it);
for (CTxMemPool::txiter childiter : setChildren) { for (CTxMemPool::txiter childiter : setChildren) {
spent.push_back(childiter->GetTx().GetHash().ToString()); spent.push_back(childiter->GetTx().GetHash().ToString());
} }
@ -455,7 +455,7 @@ static void entryToJSON(UniValue &info, const CTxMemPoolEntry &e) EXCLUSIVE_LOCK
// Add opt-in RBF status // Add opt-in RBF status
bool rbfStatus = false; bool rbfStatus = false;
RBFTransactionState rbfState = IsRBFOptIn(tx, mempool); RBFTransactionState rbfState = IsRBFOptIn(tx, pool);
if (rbfState == RBFTransactionState::UNKNOWN) { if (rbfState == RBFTransactionState::UNKNOWN) {
throw JSONRPCError(RPC_MISC_ERROR, "Transaction is not in mempool"); throw JSONRPCError(RPC_MISC_ERROR, "Transaction is not in mempool");
} else if (rbfState == RBFTransactionState::REPLACEABLE_BIP125) { } else if (rbfState == RBFTransactionState::REPLACEABLE_BIP125) {
@ -465,25 +465,21 @@ static void entryToJSON(UniValue &info, const CTxMemPoolEntry &e) EXCLUSIVE_LOCK
info.pushKV("bip125-replaceable", rbfStatus); info.pushKV("bip125-replaceable", rbfStatus);
} }
UniValue mempoolToJSON(bool fVerbose) UniValue MempoolToJSON(const CTxMemPool& pool, bool verbose)
{ {
if (fVerbose) if (verbose) {
{ LOCK(pool.cs);
LOCK(mempool.cs);
UniValue o(UniValue::VOBJ); UniValue o(UniValue::VOBJ);
for (const CTxMemPoolEntry& e : mempool.mapTx) for (const CTxMemPoolEntry& e : pool.mapTx) {
{
const uint256& hash = e.GetTx().GetHash(); const uint256& hash = e.GetTx().GetHash();
UniValue info(UniValue::VOBJ); UniValue info(UniValue::VOBJ);
entryToJSON(info, e); entryToJSON(pool, info, e);
o.pushKV(hash.ToString(), info); o.pushKV(hash.ToString(), info);
} }
return o; return o;
} } else {
else
{
std::vector<uint256> vtxid; std::vector<uint256> vtxid;
mempool.queryHashes(vtxid); pool.queryHashes(vtxid);
UniValue a(UniValue::VARR); UniValue a(UniValue::VARR);
for (const uint256& hash : vtxid) for (const uint256& hash : vtxid)
@ -525,7 +521,7 @@ static UniValue getrawmempool(const JSONRPCRequest& request)
if (!request.params[0].isNull()) if (!request.params[0].isNull())
fVerbose = request.params[0].get_bool(); fVerbose = request.params[0].get_bool();
return mempoolToJSON(fVerbose); return MempoolToJSON(::mempool, fVerbose);
} }
static UniValue getmempoolancestors(const JSONRPCRequest& request) static UniValue getmempoolancestors(const JSONRPCRequest& request)
@ -591,7 +587,7 @@ static UniValue getmempoolancestors(const JSONRPCRequest& request)
const CTxMemPoolEntry &e = *ancestorIt; const CTxMemPoolEntry &e = *ancestorIt;
const uint256& _hash = e.GetTx().GetHash(); const uint256& _hash = e.GetTx().GetHash();
UniValue info(UniValue::VOBJ); UniValue info(UniValue::VOBJ);
entryToJSON(info, e); entryToJSON(::mempool, info, e);
o.pushKV(_hash.ToString(), info); o.pushKV(_hash.ToString(), info);
} }
return o; return o;
@ -661,7 +657,7 @@ static UniValue getmempooldescendants(const JSONRPCRequest& request)
const CTxMemPoolEntry &e = *descendantIt; const CTxMemPoolEntry &e = *descendantIt;
const uint256& _hash = e.GetTx().GetHash(); const uint256& _hash = e.GetTx().GetHash();
UniValue info(UniValue::VOBJ); UniValue info(UniValue::VOBJ);
entryToJSON(info, e); entryToJSON(::mempool, info, e);
o.pushKV(_hash.ToString(), info); o.pushKV(_hash.ToString(), info);
} }
return o; return o;
@ -700,7 +696,7 @@ static UniValue getmempoolentry(const JSONRPCRequest& request)
const CTxMemPoolEntry &e = *it; const CTxMemPoolEntry &e = *it;
UniValue info(UniValue::VOBJ); UniValue info(UniValue::VOBJ);
entryToJSON(info, e); entryToJSON(::mempool, info, e);
return info; return info;
} }
@ -1485,15 +1481,15 @@ static UniValue getchaintips(const JSONRPCRequest& request)
return res; return res;
} }
UniValue mempoolInfoToJSON() UniValue MempoolInfoToJSON(const CTxMemPool& pool)
{ {
UniValue ret(UniValue::VOBJ); UniValue ret(UniValue::VOBJ);
ret.pushKV("size", (int64_t) mempool.size()); ret.pushKV("size", (int64_t)pool.size());
ret.pushKV("bytes", (int64_t) mempool.GetTotalTxSize()); ret.pushKV("bytes", (int64_t)pool.GetTotalTxSize());
ret.pushKV("usage", (int64_t) mempool.DynamicMemoryUsage()); ret.pushKV("usage", (int64_t)pool.DynamicMemoryUsage());
size_t maxmempool = gArgs.GetArg("-maxmempool", DEFAULT_MAX_MEMPOOL_SIZE) * 1000000; size_t maxmempool = gArgs.GetArg("-maxmempool", DEFAULT_MAX_MEMPOOL_SIZE) * 1000000;
ret.pushKV("maxmempool", (int64_t) maxmempool); ret.pushKV("maxmempool", (int64_t) maxmempool);
ret.pushKV("mempoolminfee", ValueFromAmount(std::max(mempool.GetMinFee(maxmempool), ::minRelayTxFee).GetFeePerK())); ret.pushKV("mempoolminfee", ValueFromAmount(std::max(pool.GetMinFee(maxmempool), ::minRelayTxFee).GetFeePerK()));
ret.pushKV("minrelaytxfee", ValueFromAmount(::minRelayTxFee.GetFeePerK())); ret.pushKV("minrelaytxfee", ValueFromAmount(::minRelayTxFee.GetFeePerK()));
return ret; return ret;
@ -1522,7 +1518,7 @@ static UniValue getmempoolinfo(const JSONRPCRequest& request)
}, },
}.ToString()); }.ToString());
return mempoolInfoToJSON(); return MempoolInfoToJSON(::mempool);
} }
static UniValue preciousblock(const JSONRPCRequest& request) static UniValue preciousblock(const JSONRPCRequest& request)

View file

@ -11,6 +11,7 @@
class CBlock; class CBlock;
class CBlockIndex; class CBlockIndex;
class CTxMemPool;
class UniValue; class UniValue;
static constexpr int NUM_GETBLOCKSTATS_PERCENTILES = 5; static constexpr int NUM_GETBLOCKSTATS_PERCENTILES = 5;
@ -30,10 +31,10 @@ void RPCNotifyBlockChange(bool ibd, const CBlockIndex *);
UniValue blockToJSON(const CBlock& block, const CBlockIndex* tip, const CBlockIndex* blockindex, bool txDetails = false); UniValue blockToJSON(const CBlock& block, const CBlockIndex* tip, const CBlockIndex* blockindex, bool txDetails = false);
/** Mempool information to JSON */ /** Mempool information to JSON */
UniValue mempoolInfoToJSON(); UniValue MempoolInfoToJSON(const CTxMemPool& pool);
/** Mempool to JSON */ /** Mempool to JSON */
UniValue mempoolToJSON(bool fVerbose = false); UniValue MempoolToJSON(const CTxMemPool& pool, bool verbose = false);
/** Block header to JSON */ /** Block header to JSON */
UniValue blockheaderToJSON(const CBlockIndex* tip, const CBlockIndex* blockindex); UniValue blockheaderToJSON(const CBlockIndex* tip, const CBlockIndex* blockindex);

View file

@ -764,7 +764,7 @@ std::vector<CTxMemPool::indexed_transaction_set::const_iterator> CTxMemPool::Get
return iters; return iters;
} }
void CTxMemPool::queryHashes(std::vector<uint256>& vtxid) void CTxMemPool::queryHashes(std::vector<uint256>& vtxid) const
{ {
LOCK(cs); LOCK(cs);
auto iters = GetSortedDepthAndScore(); auto iters = GetSortedDepthAndScore();

View file

@ -184,7 +184,7 @@ private:
const LockPoints& lp; const LockPoints& lp;
}; };
// extracts a transaction hash from CTxMempoolEntry or CTransactionRef // extracts a transaction hash from CTxMemPoolEntry or CTransactionRef
struct mempoolentry_txid struct mempoolentry_txid
{ {
typedef uint256 result_type; typedef uint256 result_type;
@ -588,7 +588,7 @@ public:
void clear(); void clear();
void _clear() EXCLUSIVE_LOCKS_REQUIRED(cs); //lock free void _clear() EXCLUSIVE_LOCKS_REQUIRED(cs); //lock free
bool CompareDepthAndScore(const uint256& hasha, const uint256& hashb); bool CompareDepthAndScore(const uint256& hasha, const uint256& hashb);
void queryHashes(std::vector<uint256>& vtxid); void queryHashes(std::vector<uint256>& vtxid) const;
bool isSpent(const COutPoint& outpoint) const; bool isSpent(const COutPoint& outpoint) const;
unsigned int GetTransactionsUpdated() const; unsigned int GetTransactionsUpdated() const;
void AddTransactionsUpdated(unsigned int n); void AddTransactionsUpdated(unsigned int n);