mirror of
https://github.com/dashpay/dash.git
synced 2024-12-26 04:22:55 +01:00
merge bitcoin#17564: Use mempool from node context instead of global
This commit is contained in:
parent
a250f2c977
commit
465ea129bc
@ -269,7 +269,7 @@ public:
|
||||
}
|
||||
return true;
|
||||
}
|
||||
void findCoins(std::map<COutPoint, Coin>& coins) override { return FindCoins(coins); }
|
||||
void findCoins(std::map<COutPoint, Coin>& coins) override { return FindCoins(m_node, coins); }
|
||||
double guessVerificationProgress(const uint256& block_hash) override
|
||||
{
|
||||
LOCK(cs_main);
|
||||
|
@ -287,8 +287,8 @@ public:
|
||||
}
|
||||
int64_t getTotalBytesRecv() override { return m_context.connman ? m_context.connman->GetTotalBytesRecv() : 0; }
|
||||
int64_t getTotalBytesSent() override { return m_context.connman ? m_context.connman->GetTotalBytesSent() : 0; }
|
||||
size_t getMempoolSize() override { return ::mempool.size(); }
|
||||
size_t getMempoolDynamicUsage() override { return ::mempool.DynamicMemoryUsage(); }
|
||||
size_t getMempoolSize() override { return m_context.mempool ? m_context.mempool->size() : 0; }
|
||||
size_t getMempoolDynamicUsage() override { return m_context.mempool ? m_context.mempool->DynamicMemoryUsage() : 0; }
|
||||
bool getHeaderTip(int& height, int64_t& block_time) override
|
||||
{
|
||||
LOCK(::cs_main);
|
||||
|
@ -4,14 +4,16 @@
|
||||
|
||||
#include <node/coin.h>
|
||||
|
||||
#include <node/context.h>
|
||||
#include <txmempool.h>
|
||||
#include <validation.h>
|
||||
|
||||
void FindCoins(std::map<COutPoint, Coin>& coins)
|
||||
void FindCoins(const NodeContext& node, std::map<COutPoint, Coin>& coins)
|
||||
{
|
||||
LOCK2(cs_main, ::mempool.cs);
|
||||
assert(node.mempool);
|
||||
LOCK2(cs_main, node.mempool->cs);
|
||||
CCoinsViewCache& chain_view = ::ChainstateActive().CoinsTip();
|
||||
CCoinsViewMemPool mempool_view(&chain_view, ::mempool);
|
||||
CCoinsViewMemPool mempool_view(&chain_view, *node.mempool);
|
||||
for (auto& coin : coins) {
|
||||
if (!mempool_view.GetCoin(coin.first, coin.second)) {
|
||||
// Either the coin is not in the CCoinsViewCache or is spent. Clear it.
|
||||
|
@ -9,14 +9,16 @@
|
||||
|
||||
class COutPoint;
|
||||
class Coin;
|
||||
struct NodeContext;
|
||||
|
||||
/**
|
||||
* Look up unspent output information. Returns coins in the mempool and in the
|
||||
* current chain UTXO set. Iterates through all the keys in the map and
|
||||
* populates the values.
|
||||
*
|
||||
* @param[in] node The node context to use for lookup
|
||||
* @param[in,out] coins map to fill
|
||||
*/
|
||||
void FindCoins(std::map<COutPoint, Coin>& coins);
|
||||
void FindCoins(const NodeContext& node, std::map<COutPoint, Coin>& coins);
|
||||
|
||||
#endif // BITCOIN_NODE_COIN_H
|
||||
|
@ -18,6 +18,7 @@
|
||||
TransactionError BroadcastTransaction(NodeContext& node, const CTransactionRef tx, std::string& err_string, const CAmount& max_tx_fee, bool relay, bool wait_callback, bool bypass_limits)
|
||||
{
|
||||
assert(node.connman);
|
||||
assert(node.mempool);
|
||||
std::promise<void> promise;
|
||||
uint256 hashTx = tx->GetHash();
|
||||
bool callback_set = false;
|
||||
@ -33,11 +34,11 @@ TransactionError BroadcastTransaction(NodeContext& node, const CTransactionRef t
|
||||
// So if the output does exist, then this transaction exists in the chain.
|
||||
if (!existingCoin.IsSpent()) return TransactionError::ALREADY_IN_CHAIN;
|
||||
}
|
||||
if (!mempool.exists(hashTx)) {
|
||||
if (!node.mempool->exists(hashTx)) {
|
||||
// Transaction is not already in the mempool. Submit it.
|
||||
CValidationState state;
|
||||
bool fMissingInputs;
|
||||
if (!AcceptToMemoryPool(mempool, state, std::move(tx), &fMissingInputs,
|
||||
if (!AcceptToMemoryPool(*node.mempool, state, std::move(tx), &fMissingInputs,
|
||||
bypass_limits, max_tx_fee)) {
|
||||
if (state.IsInvalid()) {
|
||||
err_string = FormatStateMessage(state);
|
||||
|
@ -109,6 +109,7 @@ void TestGUI(interfaces::Node& node)
|
||||
test.CreateAndProcessBlock({}, GetScriptForRawPubKey(test.coinbaseKey.GetPubKey()));
|
||||
}
|
||||
node.context()->connman = std::move(test.m_node.connman);
|
||||
node.context()->mempool = std::move(test.m_node.mempool);
|
||||
std::shared_ptr<CWallet> wallet = std::make_shared<CWallet>(node.context()->chain.get(), WalletLocation(), CreateMockWalletDatabase());
|
||||
AddWallet(wallet);
|
||||
bool firstRun;
|
||||
|
39
src/rest.cpp
39
src/rest.cpp
@ -8,6 +8,7 @@
|
||||
#include <core_io.h>
|
||||
#include <httpserver.h>
|
||||
#include <index/txindex.h>
|
||||
#include <node/context.h>
|
||||
#include <primitives/block.h>
|
||||
#include <primitives/transaction.h>
|
||||
#include <rpc/blockchain.h>
|
||||
@ -16,6 +17,7 @@
|
||||
#include <streams.h>
|
||||
#include <sync.h>
|
||||
#include <txmempool.h>
|
||||
#include <util/check.h>
|
||||
#include <util/strencodings.h>
|
||||
#include <validation.h>
|
||||
#include <version.h>
|
||||
@ -64,6 +66,24 @@ static bool RESTERR(HTTPRequest* req, enum HTTPStatusCode status, std::string me
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the node context mempool.
|
||||
*
|
||||
* Set the HTTP error and return nullptr if node context
|
||||
* mempool is not found.
|
||||
*
|
||||
* @param[in] req the HTTP request
|
||||
* return pointer to the mempool or nullptr if no mempool found
|
||||
*/
|
||||
static CTxMemPool* GetMemPool(HTTPRequest* req)
|
||||
{
|
||||
if (!g_rpc_node || !g_rpc_node->mempool) {
|
||||
RESTERR(req, HTTP_NOT_FOUND, "Mempool disabled or instance not found");
|
||||
return nullptr;
|
||||
}
|
||||
return g_rpc_node->mempool;
|
||||
}
|
||||
|
||||
static RetFormat ParseDataFormat(std::string& param, const std::string& strReq)
|
||||
{
|
||||
const std::string::size_type pos = strReq.rfind('.');
|
||||
@ -290,12 +310,14 @@ static bool rest_mempool_info(HTTPRequest* req, const std::string& strURIPart)
|
||||
{
|
||||
if (!CheckWarmup(req))
|
||||
return false;
|
||||
const CTxMemPool* mempool = GetMemPool(req);
|
||||
if (!mempool) return false;
|
||||
std::string param;
|
||||
const RetFormat rf = ParseDataFormat(param, strURIPart);
|
||||
|
||||
switch (rf) {
|
||||
case RetFormat::JSON: {
|
||||
UniValue mempoolInfoObject = MempoolInfoToJSON(::mempool);
|
||||
UniValue mempoolInfoObject = MempoolInfoToJSON(*mempool);
|
||||
|
||||
std::string strJSON = mempoolInfoObject.write() + "\n";
|
||||
req->WriteHeader("Content-Type", "application/json");
|
||||
@ -310,14 +332,15 @@ static bool rest_mempool_info(HTTPRequest* req, const std::string& strURIPart)
|
||||
|
||||
static bool rest_mempool_contents(HTTPRequest* req, const std::string& strURIPart)
|
||||
{
|
||||
if (!CheckWarmup(req))
|
||||
return false;
|
||||
if (!CheckWarmup(req)) return false;
|
||||
const CTxMemPool* mempool = GetMemPool(req);
|
||||
if (!mempool) return false;
|
||||
std::string param;
|
||||
const RetFormat rf = ParseDataFormat(param, strURIPart);
|
||||
|
||||
switch (rf) {
|
||||
case RetFormat::JSON: {
|
||||
UniValue mempoolObject = MempoolToJSON(::mempool, true);
|
||||
UniValue mempoolObject = MempoolToJSON(*mempool, true);
|
||||
|
||||
std::string strJSON = mempoolObject.write() + "\n";
|
||||
req->WriteHeader("Content-Type", "application/json");
|
||||
@ -495,11 +518,13 @@ static bool rest_getutxos(HTTPRequest* req, const std::string& strURIPart)
|
||||
};
|
||||
|
||||
if (fCheckMemPool) {
|
||||
const CTxMemPool* mempool = GetMemPool(req);
|
||||
if (!mempool) return false;
|
||||
// use db+mempool as cache backend in case user likes to query mempool
|
||||
LOCK2(cs_main, mempool.cs);
|
||||
LOCK2(cs_main, mempool->cs);
|
||||
CCoinsViewCache& viewChain = ::ChainstateActive().CoinsTip();
|
||||
CCoinsViewMemPool viewMempool(&viewChain, mempool);
|
||||
process_utxos(viewMempool, mempool);
|
||||
CCoinsViewMemPool viewMempool(&viewChain, *mempool);
|
||||
process_utxos(viewMempool, *mempool);
|
||||
} else {
|
||||
LOCK(cs_main); // no need to lock mempool!
|
||||
process_utxos(::ChainstateActive().CoinsTip(), CTxMemPool());
|
||||
|
@ -561,7 +561,7 @@ static UniValue getrawmempool(const JSONRPCRequest& request)
|
||||
if (!request.params[0].isNull())
|
||||
fVerbose = request.params[0].get_bool();
|
||||
|
||||
return MempoolToJSON(::mempool, fVerbose);
|
||||
return MempoolToJSON(EnsureMemPool(), fVerbose);
|
||||
}
|
||||
|
||||
static UniValue getmempoolancestors(const JSONRPCRequest& request)
|
||||
@ -592,6 +592,7 @@ static UniValue getmempoolancestors(const JSONRPCRequest& request)
|
||||
|
||||
uint256 hash = ParseHashV(request.params[0], "parameter 1");
|
||||
|
||||
const CTxMemPool& mempool = EnsureMemPool();
|
||||
LOCK(mempool.cs);
|
||||
|
||||
CTxMemPool::txiter it = mempool.mapTx.find(hash);
|
||||
@ -617,7 +618,7 @@ static UniValue getmempoolancestors(const JSONRPCRequest& request)
|
||||
const CTxMemPoolEntry &e = *ancestorIt;
|
||||
const uint256& _hash = e.GetTx().GetHash();
|
||||
UniValue info(UniValue::VOBJ);
|
||||
entryToJSON(::mempool, info, e);
|
||||
entryToJSON(mempool, info, e);
|
||||
o.pushKV(_hash.ToString(), info);
|
||||
}
|
||||
return o;
|
||||
@ -654,6 +655,7 @@ static UniValue getmempooldescendants(const JSONRPCRequest& request)
|
||||
|
||||
uint256 hash = ParseHashV(request.params[0], "parameter 1");
|
||||
|
||||
const CTxMemPool& mempool = EnsureMemPool();
|
||||
LOCK(mempool.cs);
|
||||
|
||||
CTxMemPool::txiter it = mempool.mapTx.find(hash);
|
||||
@ -679,7 +681,7 @@ static UniValue getmempooldescendants(const JSONRPCRequest& request)
|
||||
const CTxMemPoolEntry &e = *descendantIt;
|
||||
const uint256& _hash = e.GetTx().GetHash();
|
||||
UniValue info(UniValue::VOBJ);
|
||||
entryToJSON(::mempool, info, e);
|
||||
entryToJSON(mempool, info, e);
|
||||
o.pushKV(_hash.ToString(), info);
|
||||
}
|
||||
return o;
|
||||
@ -703,6 +705,7 @@ static UniValue getmempoolentry(const JSONRPCRequest& request)
|
||||
|
||||
uint256 hash = ParseHashV(request.params[0], "parameter 1");
|
||||
|
||||
const CTxMemPool& mempool = EnsureMemPool();
|
||||
LOCK(mempool.cs);
|
||||
|
||||
CTxMemPool::txiter it = mempool.mapTx.find(hash);
|
||||
@ -712,7 +715,7 @@ static UniValue getmempoolentry(const JSONRPCRequest& request)
|
||||
|
||||
const CTxMemPoolEntry &e = *it;
|
||||
UniValue info(UniValue::VOBJ);
|
||||
entryToJSON(::mempool, info, e);
|
||||
entryToJSON(mempool, info, e);
|
||||
return info;
|
||||
}
|
||||
|
||||
@ -1307,6 +1310,7 @@ static UniValue gettxout(const JSONRPCRequest& request)
|
||||
CCoinsViewCache* coins_view = &::ChainstateActive().CoinsTip();
|
||||
|
||||
if (fMempool) {
|
||||
const CTxMemPool& mempool = EnsureMemPool();
|
||||
LOCK(mempool.cs);
|
||||
CCoinsViewMemPool view(coins_view, mempool);
|
||||
if (!view.GetCoin(out, coin) || mempool.isSpent(out)) {
|
||||
@ -1725,7 +1729,7 @@ static UniValue getmempoolinfo(const JSONRPCRequest& request)
|
||||
},
|
||||
}.Check(request);
|
||||
|
||||
return MempoolInfoToJSON(::mempool);
|
||||
return MempoolInfoToJSON(EnsureMemPool());
|
||||
}
|
||||
|
||||
static UniValue preciousblock(const JSONRPCRequest& request)
|
||||
@ -2327,11 +2331,13 @@ static UniValue savemempool(const JSONRPCRequest& request)
|
||||
},
|
||||
}.Check(request);
|
||||
|
||||
if (!::mempool.IsLoaded()) {
|
||||
const CTxMemPool& mempool = EnsureMemPool();
|
||||
|
||||
if (!mempool.IsLoaded()) {
|
||||
throw JSONRPCError(RPC_MISC_ERROR, "The mempool was not loaded yet");
|
||||
}
|
||||
|
||||
if (!DumpMempool(::mempool)) {
|
||||
if (!DumpMempool(mempool)) {
|
||||
throw JSONRPCError(RPC_MISC_ERROR, "Unable to dump mempool to disk");
|
||||
}
|
||||
|
||||
|
@ -430,6 +430,7 @@ static UniValue getmininginfo(const JSONRPCRequest& request)
|
||||
}.Check(request);
|
||||
|
||||
LOCK(cs_main);
|
||||
const CTxMemPool& mempool = EnsureMemPool();
|
||||
|
||||
UniValue obj(UniValue::VOBJ);
|
||||
obj.pushKV("blocks", (int)::ChainActive().Height());
|
||||
@ -469,7 +470,7 @@ static UniValue prioritisetransaction(const JSONRPCRequest& request)
|
||||
uint256 hash = ParseHashV(request.params[0].get_str(), "txid");
|
||||
CAmount nAmount = request.params[1].get_int64();
|
||||
|
||||
mempool.PrioritiseTransaction(hash, nAmount);
|
||||
EnsureMemPool().PrioritiseTransaction(hash, nAmount);
|
||||
return true;
|
||||
}
|
||||
|
||||
@ -697,6 +698,7 @@ static UniValue getblocktemplate(const JSONRPCRequest& request)
|
||||
throw JSONRPCError(RPC_CLIENT_IN_INITIAL_DOWNLOAD, PACKAGE_NAME " is syncing with network...");
|
||||
|
||||
static unsigned int nTransactionsUpdatedLast;
|
||||
const CTxMemPool& mempool = EnsureMemPool();
|
||||
|
||||
if (!lpval.isNull())
|
||||
{
|
||||
@ -731,7 +733,7 @@ static UniValue getblocktemplate(const JSONRPCRequest& request)
|
||||
if (g_best_block_cv.wait_until(lock, checktxtime) == std::cv_status::timeout)
|
||||
{
|
||||
// Timeout: Check transactions for update
|
||||
// without holding ::mempool.cs to avoid deadlocks
|
||||
// without holding the mempool lock to avoid deadlocks
|
||||
if (mempool.GetTransactionsUpdated() != nTransactionsUpdatedLastLP)
|
||||
break;
|
||||
checktxtime += std::chrono::seconds(10);
|
||||
|
@ -645,6 +645,7 @@ static UniValue combinerawtransaction(const JSONRPCRequest& request)
|
||||
CCoinsView viewDummy;
|
||||
CCoinsViewCache view(&viewDummy);
|
||||
{
|
||||
const CTxMemPool& mempool = EnsureMemPool();
|
||||
LOCK(cs_main);
|
||||
LOCK(mempool.cs);
|
||||
CCoinsViewCache &viewChain = ::ChainstateActive().CoinsTip();
|
||||
@ -768,7 +769,7 @@ static UniValue signrawtransactionwithkey(const JSONRPCRequest& request)
|
||||
for (const CTxIn& txin : mtx.vin) {
|
||||
coins[txin.prevout]; // Create empty map entry keyed by prevout.
|
||||
}
|
||||
FindCoins(coins);
|
||||
FindCoins(*g_rpc_node, coins);
|
||||
|
||||
return SignTransaction(mtx, request.params[2], &keystore, coins, true, request.params[3]);
|
||||
}
|
||||
@ -901,6 +902,8 @@ static UniValue testmempoolaccept(const JSONRPCRequest& request)
|
||||
throw JSONRPCError(RPC_INVALID_PARAMETER, "second argument (maxfeerate) must be numeric");
|
||||
}
|
||||
|
||||
CTxMemPool& mempool = EnsureMemPool();
|
||||
|
||||
UniValue result(UniValue::VARR);
|
||||
UniValue result_0(UniValue::VOBJ);
|
||||
result_0.pushKV("txid", tx_hash.GetHex());
|
||||
@ -1438,6 +1441,7 @@ UniValue utxoupdatepsbt(const JSONRPCRequest& request)
|
||||
CCoinsView viewDummy;
|
||||
CCoinsViewCache view(&viewDummy);
|
||||
{
|
||||
const CTxMemPool& mempool = EnsureMemPool();
|
||||
LOCK2(cs_main, mempool.cs);
|
||||
CCoinsViewCache &viewChain = ::ChainstateActive().CoinsTip();
|
||||
CCoinsViewMemPool viewMempool(&viewChain, mempool);
|
||||
|
@ -110,14 +110,10 @@ BOOST_AUTO_TEST_CASE(rpc_rawsign)
|
||||
std::string notsigned = r.get_str();
|
||||
std::string privkey1 = "\"XEwTRsCX3CiWSQf8YmKMTeb84KyTbibkUv9mDTZHQ5MwuKG2ZzES\"";
|
||||
std::string privkey2 = "\"XDmZ7LjGd94Q81eUBjb2h6uV5Y14s7fmeXWEGYabfBJP8RVpprBu\"";
|
||||
NodeContext node;
|
||||
node.chain = interfaces::MakeChain(node);
|
||||
g_rpc_node = &node;
|
||||
r = CallRPC(std::string("signrawtransactionwithkey ")+notsigned+" [] "+prevout);
|
||||
BOOST_CHECK(find_value(r.get_obj(), "complete").get_bool() == false);
|
||||
r = CallRPC(std::string("signrawtransactionwithkey ")+notsigned+" ["+privkey1+","+privkey2+"] "+prevout);
|
||||
BOOST_CHECK(find_value(r.get_obj(), "complete").get_bool() == true);
|
||||
g_rpc_node = nullptr;
|
||||
}
|
||||
|
||||
BOOST_AUTO_TEST_CASE(rpc_createraw_op_return)
|
||||
|
Loading…
Reference in New Issue
Block a user