Skip to content

Commit d4a513b

Browse files
committed
merge bitcoin#15288: Remove wallet -> node global function calls
1 parent e5c21c9 commit d4a513b

22 files changed

+287
-145
lines changed

contrib/devtools/circular-dependencies.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,18 @@
88
'core_write.cpp': 'core_io.cpp',
99
}
1010

11+
# Directories with header-based modules, where the assumption that .cpp files
12+
# define functions and variables declared in corresponding .h files is
13+
# incorrect.
14+
HEADER_MODULE_PATHS = [
15+
'interfaces/'
16+
]
17+
1118
def module_name(path):
1219
if path in MAPPING:
1320
path = MAPPING[path]
21+
if any(path.startswith(dirpath) for dirpath in HEADER_MODULE_PATHS):
22+
return path
1423
if path.endswith(".h"):
1524
return path[:-2]
1625
if path.endswith(".c"):

src/Makefile.bench.include

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ bench_bench_dash_SOURCES += bench/coin_selection.cpp
7676
bench_bench_dash_SOURCES += bench/wallet_balance.cpp
7777
endif
7878

79-
bench_bench_dash_LDADD += $(BACKTRACE_LIB) $(BOOST_LIBS) $(BDB_LIBS) $(SSL_LIBS) $(CRYPTO_LIBS) $(MINIUPNPC_LIBS) $(EVENT_PTHREADS_LIBS) $(EVENT_LIBS) $(BLS_LIBS) $(GMP_LIBS)
79+
bench_bench_dash_LDADD += $(BACKTRACE_LIB) $(BOOST_LIBS) $(BDB_LIBS) $(SSL_LIBS) $(CRYPTO_LIBS) $(EVENT_PTHREADS_LIBS) $(EVENT_LIBS) $(MINIUPNPC_LIBS) $(EVENT_PTHREADS_LIBS) $(EVENT_LIBS) $(BLS_LIBS) $(GMP_LIBS)
8080
bench_bench_dash_LDFLAGS = $(LDFLAGS_WRAP_EXCEPTIONS) $(RELDFLAGS) $(AM_LDFLAGS) $(LIBTOOL_APP_LDFLAGS)
8181

8282
CLEAN_BITCOIN_BENCH = bench/*.gcda bench/*.gcno $(GENERATED_BENCH_FILES)

src/coinjoin/util.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ CTransactionBuilder::CTransactionBuilder(std::shared_ptr<CWallet> pwalletIn, con
111111
tallyItem(tallyItemIn)
112112
{
113113
// Generate a feerate which will be used to consider if the remainder is dust and will go into fees or not
114-
coinControl.m_discard_feerate = ::GetDiscardRate(*pwallet.get(), ::feeEstimator);
114+
coinControl.m_discard_feerate = ::GetDiscardRate(*pwallet.get());
115115
// Generate a feerate which will be used by calculations of this class and also by CWallet::CreateTransaction
116116
coinControl.m_feerate = std::max(::feeEstimator.estimateSmartFee((int)pwallet->m_confirm_target, nullptr, true), pwallet->m_pay_tx_fee);
117117
// Change always goes back to origin
@@ -307,7 +307,7 @@ bool CTransactionBuilder::Commit(std::string& strResult)
307307
}
308308

309309
CValidationState state;
310-
if (!pwallet->CommitTransaction(tx, {}, {}, dummyReserveKey, g_connman.get(), state)) {
310+
if (!pwallet->CommitTransaction(tx, {}, {}, dummyReserveKey, state)) {
311311
strResult = state.GetRejectReason();
312312
return false;
313313
}

src/interfaces/chain.cpp

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,18 @@
66

77
#include <chain.h>
88
#include <chainparams.h>
9+
#include <interfaces/wallet.h>
10+
#include <net.h>
11+
#include <policy/fees.h>
12+
#include <policy/policy.h>
913
#include <primitives/block.h>
14+
#include <primitives/transaction.h>
15+
#include <protocol.h>
1016
#include <sync.h>
17+
#include <threadsafety.h>
18+
#include <timedata.h>
19+
#include <txmempool.h>
20+
#include <ui_interface.h>
1121
#include <uint256.h>
1222
#include <util/system.h>
1323
#include <validation.h>
@@ -132,6 +142,17 @@ class LockImpl : public Chain::Lock
132142
}
133143
return nullopt;
134144
}
145+
bool checkFinalTx(const CTransaction& tx) override
146+
{
147+
LockAnnotation lock(::cs_main);
148+
return CheckFinalTx(tx);
149+
}
150+
bool submitToMemoryPool(CTransactionRef tx, CAmount absurd_fee, CValidationState& state) override
151+
{
152+
LockAnnotation lock(::cs_main);
153+
return AcceptToMemoryPool(mempool, state, tx, nullptr /* missing inputs */,
154+
false /* bypass limits */, absurd_fee);
155+
}
135156
};
136157

137158
class LockingStateImpl : public LockImpl, public UniqueLock<CCriticalSection>
@@ -177,6 +198,56 @@ class ChainImpl : public Chain
177198
LOCK(cs_main);
178199
return GuessVerificationProgress(Params().TxData(), LookupBlockIndex(block_hash));
179200
}
201+
void relayTransaction(const uint256& txid) override
202+
{
203+
CInv inv(MSG_TX, txid);
204+
g_connman->ForEachNode([&inv](CNode* node) { node->PushInventory(inv); });
205+
}
206+
void getTransactionAncestry(const uint256& txid, size_t& ancestors, size_t& descendants) override
207+
{
208+
::mempool.GetTransactionAncestry(txid, ancestors, descendants);
209+
}
210+
bool checkChainLimits(CTransactionRef tx) override
211+
{
212+
LockPoints lp;
213+
CTxMemPoolEntry entry(tx, 0, 0, 0, false, 0, lp);
214+
CTxMemPool::setEntries ancestors;
215+
auto limit_ancestor_count = gArgs.GetArg("-limitancestorcount", DEFAULT_ANCESTOR_LIMIT);
216+
auto limit_ancestor_size = gArgs.GetArg("-limitancestorsize", DEFAULT_ANCESTOR_SIZE_LIMIT) * 1000;
217+
auto limit_descendant_count = gArgs.GetArg("-limitdescendantcount", DEFAULT_DESCENDANT_LIMIT);
218+
auto limit_descendant_size = gArgs.GetArg("-limitdescendantsize", DEFAULT_DESCENDANT_SIZE_LIMIT) * 1000;
219+
std::string unused_error_string;
220+
LOCK(::mempool.cs);
221+
return ::mempool.CalculateMemPoolAncestors(entry, ancestors, limit_ancestor_count, limit_ancestor_size,
222+
limit_descendant_count, limit_descendant_size, unused_error_string);
223+
}
224+
CFeeRate estimateSmartFee(int num_blocks, bool conservative, FeeCalculation* calc) override
225+
{
226+
return ::feeEstimator.estimateSmartFee(num_blocks, calc, conservative);
227+
}
228+
unsigned int estimateMaxBlocks() override
229+
{
230+
return ::feeEstimator.HighestTargetTracked(FeeEstimateHorizon::LONG_HALFLIFE);
231+
}
232+
CFeeRate mempoolMinFee() override
233+
{
234+
return ::mempool.GetMinFee(gArgs.GetArg("-maxmempool", DEFAULT_MAX_MEMPOOL_SIZE) * 1000000);
235+
}
236+
bool hasDescendantsInMempool(const uint256& txid) override
237+
{
238+
LOCK(::mempool.cs);
239+
auto it_mp = ::mempool.mapTx.find(txid);
240+
return it_mp != ::mempool.mapTx.end() && it_mp->GetCountWithDescendants() > 1;
241+
}
242+
CAmount maxTxFee() override { return ::maxTxFee; }
243+
bool getPruneMode() override { return ::fPruneMode; }
244+
bool p2pEnabled() override { return g_connman != nullptr; }
245+
bool isInitialBlockDownload() override { return ::ChainstateActive().IsInitialBlockDownload(); }
246+
int64_t getAdjustedTime() override { return GetAdjustedTime(); }
247+
void initMessage(const std::string& message) override { ::uiInterface.InitMessage(message); }
248+
void initWarning(const std::string& message) override { InitWarning(message); }
249+
void initError(const std::string& message) override { InitError(message); }
250+
void loadWallet(std::unique_ptr<Wallet> wallet) override { ::uiInterface.LoadWallet(wallet); }
180251
};
181252

182253
} // namespace

src/interfaces/chain.h

Lines changed: 68 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,20 +5,29 @@
55
#ifndef BITCOIN_INTERFACES_CHAIN_H
66
#define BITCOIN_INTERFACES_CHAIN_H
77

8-
#include <optional.h>
8+
#include <optional.h> // For Optional and nullopt
9+
#include <primitives/transaction.h> // For CTransactionRef
910

1011
#include <memory>
12+
#include <stddef.h>
1113
#include <stdint.h>
1214
#include <string>
1315
#include <vector>
1416

1517
class CBlock;
1618
class CScheduler;
19+
class CValidationState;
20+
class CFeeRate;
1721
class uint256;
1822
struct CBlockLocator;
23+
struct FeeCalculation;
24+
25+
typedef std::shared_ptr<const CTransaction> CTransactionRef;
1926

2027
namespace interfaces {
2128

29+
class Wallet;
30+
2231
//! Interface for giving wallet processes access to blockchain state.
2332
class Chain
2433
{
@@ -102,6 +111,13 @@ class Chain
102111
//! is guaranteed to be an ancestor of the block used to create the
103112
//! locator.
104113
virtual Optional<int> findLocatorFork(const CBlockLocator& locator) = 0;
114+
115+
//! Check if transaction will be final given chain height current time.
116+
virtual bool checkFinalTx(const CTransaction& tx) = 0;
117+
118+
//! Add transaction to memory pool if the transaction fee is below the
119+
//! amount specified by absurd_fee (as a safeguard). */
120+
virtual bool submitToMemoryPool(CTransactionRef tx, CAmount absurd_fee, CValidationState& state) = 0;
105121
};
106122

107123
//! Return Lock interface. Chain is locked when this is called, and
@@ -127,6 +143,57 @@ class Chain
127143
//! Estimate fraction of total transactions verified if blocks up to
128144
//! the specified block hash are verified.
129145
virtual double guessVerificationProgress(const uint256& block_hash) = 0;
146+
147+
//! Check if transaction has descendants in mempool.
148+
virtual bool hasDescendantsInMempool(const uint256& txid) = 0;
149+
150+
//! Relay transaction.
151+
virtual void relayTransaction(const uint256& txid) = 0;
152+
153+
//! Calculate mempool ancestor and descendant counts for the given transaction.
154+
virtual void getTransactionAncestry(const uint256& txid, size_t& ancestors, size_t& descendants) = 0;
155+
156+
//! Check chain limits.
157+
virtual bool checkChainLimits(CTransactionRef tx) = 0;
158+
159+
//! Estimate smart fee.
160+
virtual CFeeRate estimateSmartFee(int num_blocks, bool conservative, FeeCalculation* calc = nullptr) = 0;
161+
162+
//! Fee estimator max target.
163+
virtual unsigned int estimateMaxBlocks() = 0;
164+
165+
//! Pool min fee.
166+
virtual CFeeRate mempoolMinFee() = 0;
167+
168+
//! Get node max tx fee setting (-maxtxfee).
169+
//! This could be replaced by a per-wallet max fee, as proposed at
170+
//! https://github.com/bitcoin/bitcoin/issues/15355
171+
//! But for the time being, wallets call this to access the node setting.
172+
virtual CAmount maxTxFee() = 0;
173+
174+
//! Check if pruning is enabled.
175+
virtual bool getPruneMode() = 0;
176+
177+
//! Check if p2p enabled.
178+
virtual bool p2pEnabled() = 0;
179+
180+
// Check if in IBD.
181+
virtual bool isInitialBlockDownload() = 0;
182+
183+
//! Get adjusted time.
184+
virtual int64_t getAdjustedTime() = 0;
185+
186+
//! Send init message.
187+
virtual void initMessage(const std::string& message) = 0;
188+
189+
//! Send init warning.
190+
virtual void initWarning(const std::string& message) = 0;
191+
192+
//! Send init error.
193+
virtual void initError(const std::string& message) = 0;
194+
195+
//! Send wallet load notification to the GUI.
196+
virtual void loadWallet(std::unique_ptr<Wallet> wallet) = 0;
130197
};
131198

132199
//! Interface to let node manage chain clients (wallets, or maybe tools for

src/interfaces/node.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -419,7 +419,7 @@ class NodeImpl : public Node
419419
}
420420
std::unique_ptr<Handler> handleLoadWallet(LoadWalletFn fn) override
421421
{
422-
return MakeHandler(::uiInterface.LoadWallet_connect([fn](std::shared_ptr<CWallet> wallet) { fn(MakeWallet(wallet)); }));
422+
return MakeHandler(::uiInterface.LoadWallet_connect([fn](std::unique_ptr<Wallet>& wallet) { fn(std::move(wallet)); }));
423423
}
424424
std::unique_ptr<Handler> handleNotifyNumConnectionsChanged(NotifyNumConnectionsChangedFn fn) override
425425
{

src/interfaces/wallet.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ class PendingWalletTxImpl : public PendingWalletTx
5555
auto locked_chain = m_wallet.chain().lock();
5656
LOCK2(mempool.cs, m_wallet.cs_wallet);
5757
CValidationState state;
58-
if (!m_wallet.CommitTransaction(m_tx, std::move(value_map), std::move(order_form), m_key, g_connman.get(), state)) {
58+
if (!m_wallet.CommitTransaction(m_tx, std::move(value_map), std::move(order_form), m_key, state)) {
5959
reject_reason = state.GetRejectReason();
6060
return false;
6161
}
@@ -110,7 +110,7 @@ WalletTx MakeWalletTx(interfaces::Chain::Lock& locked_chain, CWallet& wallet, co
110110
//! Construct wallet tx status struct.
111111
WalletTxStatus MakeWalletTxStatus(interfaces::Chain::Lock& locked_chain, const CWalletTx& wtx)
112112
{
113-
LockAnnotation lock(::cs_main); // Temporary, for CheckFinalTx below. Removed in upcoming commit.
113+
LockAnnotation lock(::cs_main); // Temporary, for mapBlockIndex below. Removed in upcoming commit.
114114

115115
WalletTxStatus result;
116116
auto mi = ::BlockIndex().find(wtx.hashBlock);
@@ -120,7 +120,7 @@ WalletTxStatus MakeWalletTxStatus(interfaces::Chain::Lock& locked_chain, const C
120120
result.depth_in_main_chain = wtx.GetDepthInMainChain(locked_chain);
121121
result.time_received = wtx.nTimeReceived;
122122
result.lock_time = wtx.tx->nLockTime;
123-
result.is_final = CheckFinalTx(*wtx.tx);
123+
result.is_final = locked_chain.checkFinalTx(*wtx.tx);
124124
result.is_trusted = wtx.IsTrusted(locked_chain);
125125
result.is_abandoned = wtx.isAbandoned();
126126
result.is_coinbase = wtx.IsCoinBase();
@@ -539,7 +539,7 @@ class WalletImpl : public Wallet
539539
{
540540
FeeCalculation fee_calc;
541541
CAmount result;
542-
result = GetMinimumFee(*m_wallet, tx_bytes, coin_control, ::mempool, ::feeEstimator, &fee_calc);
542+
result = GetMinimumFee(*m_wallet, tx_bytes, coin_control, &fee_calc);
543543
if (returned_target) *returned_target = fee_calc.returnedTarget;
544544
if (reason) *reason = fee_calc.reason;
545545
return result;

src/rpc/governance.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -236,7 +236,7 @@ static UniValue gobject_prepare(const JSONRPCRequest& request)
236236
CReserveKey reservekey(pwallet);
237237
// -- send the tx to the network
238238
CValidationState state;
239-
if (!pwallet->CommitTransaction(tx, {}, {}, reservekey, g_connman.get(), state)) {
239+
if (!pwallet->CommitTransaction(tx, {}, {}, reservekey, state)) {
240240
throw JSONRPCError(RPC_INTERNAL_ERROR, "CommitTransaction failed! Reason given: " + state.GetRejectReason());
241241
}
242242

src/rpc/mining.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -860,7 +860,8 @@ static UniValue estimatesmartfee(const JSONRPCRequest& request)
860860

861861
RPCTypeCheck(request.params, {UniValue::VNUM, UniValue::VSTR});
862862
RPCTypeCheckArgument(request.params[0], UniValue::VNUM);
863-
unsigned int conf_target = ParseConfirmTarget(request.params[0]);
863+
unsigned int max_target = ::feeEstimator.HighestTargetTracked(FeeEstimateHorizon::LONG_HALFLIFE);
864+
unsigned int conf_target = ParseConfirmTarget(request.params[0], max_target);
864865
bool conservative = true;
865866
if (!request.params[1].isNull()) {
866867
FeeEstimateMode fee_mode;
@@ -930,7 +931,8 @@ static UniValue estimaterawfee(const JSONRPCRequest& request)
930931

931932
RPCTypeCheck(request.params, {UniValue::VNUM, UniValue::VNUM}, true);
932933
RPCTypeCheckArgument(request.params[0], UniValue::VNUM);
933-
unsigned int conf_target = ParseConfirmTarget(request.params[0]);
934+
unsigned int max_target = ::feeEstimator.HighestTargetTracked(FeeEstimateHorizon::LONG_HALFLIFE);
935+
unsigned int conf_target = ParseConfirmTarget(request.params[0], max_target);
934936
double threshold = 0.95;
935937
if (!request.params[1].isNull()) {
936938
threshold = request.params[1].get_real();

src/rpc/util.cpp

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,10 @@
44

55
#include <key_io.h>
66
#include <keystore.h>
7-
#include <policy/fees.h>
87
#include <pubkey.h>
98
#include <rpc/util.h>
109
#include <tinyformat.h>
1110
#include <util/strencodings.h>
12-
#include <validation.h>
1311

1412
InitInterfaces* g_rpc_interfaces = nullptr;
1513

@@ -96,10 +94,9 @@ UniValue DescribeAddress(const CTxDestination& dest)
9694
return boost::apply_visitor(DescribeAddressVisitor(), dest);
9795
}
9896

99-
unsigned int ParseConfirmTarget(const UniValue& value)
97+
unsigned int ParseConfirmTarget(const UniValue& value, unsigned int max_target)
10098
{
10199
int target = value.get_int();
102-
unsigned int max_target = ::feeEstimator.HighestTargetTracked(FeeEstimateHorizon::LONG_HALFLIFE);
103100
if (target < 1 || (unsigned int)target > max_target) {
104101
throw JSONRPCError(RPC_INVALID_PARAMETER, strprintf("Invalid conf_target, must be between %u - %u", 1, max_target));
105102
}

0 commit comments

Comments
 (0)