Skip to content

Commit f463cd1

Browse files
committed
[wallet] Keep track of the best block time in the wallet
Move nTimeBestReceived (which is only used for wallet rebroadcasts) into the wallet.
1 parent f3ecf30 commit f463cd1

File tree

7 files changed

+26
-18
lines changed

7 files changed

+26
-18
lines changed

src/interfaces/chain.cpp

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -202,16 +202,20 @@ class NotificationsHandlerImpl : public Handler, CValidationInterface
202202
{
203203
m_notifications->BlockDisconnected(*block);
204204
}
205+
void UpdatedBlockTip(const CBlockIndex* index, const CBlockIndex* fork_index, bool is_ibd) override
206+
{
207+
m_notifications->UpdatedBlockTip();
208+
}
205209
void ChainStateFlushed(const CBlockLocator& locator) override { m_notifications->ChainStateFlushed(locator); }
206-
void ResendWalletTransactions(int64_t best_block_time, CConnman*) override
210+
void ResendWalletTransactions(CConnman*) override
207211
{
208212
// `cs_main` is always held when this method is called, so it is safe to
209213
// call `assumeLocked`. This is awkward, and the `assumeLocked` method
210214
// should be able to be removed entirely if `ResendWalletTransactions`
211215
// is replaced by a wallet timer as suggested in
212216
// https://github.com/bitcoin/bitcoin/issues/15619
213217
auto locked_chain = m_chain.assumeLocked();
214-
m_notifications->ResendWalletTransactions(*locked_chain, best_block_time);
218+
m_notifications->ResendWalletTransactions(*locked_chain);
215219
}
216220
Chain& m_chain;
217221
Chain::Notifications* m_notifications;

src/interfaces/chain.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -256,8 +256,9 @@ class Chain
256256
virtual void TransactionRemovedFromMempool(const CTransactionRef& ptx) {}
257257
virtual void BlockConnected(const CBlock& block, const std::vector<CTransactionRef>& tx_conflicted) {}
258258
virtual void BlockDisconnected(const CBlock& block) {}
259+
virtual void UpdatedBlockTip() {}
259260
virtual void ChainStateFlushed(const CBlockLocator& locator) {}
260-
virtual void ResendWalletTransactions(Lock& locked_chain, int64_t best_block_time) {}
261+
virtual void ResendWalletTransactions(Lock& locked_chain) {}
261262
};
262263

263264
//! Register handler for notifications.

src/net_processing.cpp

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -175,8 +175,6 @@ namespace {
175175
/** Expiration-time ordered list of (expire time, relay map entry) pairs. */
176176
std::deque<std::pair<int64_t, MapRelay::iterator>> vRelayExpiration GUARDED_BY(cs_main);
177177

178-
std::atomic<int64_t> nTimeBestReceived(0); // Used only to inform the wallet of when we last received a block
179-
180178
struct IteratorComparator
181179
{
182180
template<typename I>
@@ -1121,8 +1119,6 @@ void PeerLogicValidation::UpdatedBlockTip(const CBlockIndex *pindexNew, const CB
11211119
});
11221120
connman->WakeMessageHandler();
11231121
}
1124-
1125-
nTimeBestReceived = GetTime();
11261122
}
11271123

11281124
/**
@@ -3555,7 +3551,7 @@ bool PeerLogicValidation::SendMessages(CNode* pto)
35553551
// transactions become unconfirmed and spams other nodes.
35563552
if (!fReindex && !fImporting && !IsInitialBlockDownload())
35573553
{
3558-
GetMainSignals().Broadcast(nTimeBestReceived, connman);
3554+
GetMainSignals().Broadcast(connman);
35593555
}
35603556

35613557
//

src/validationinterface.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ struct MainSignalsInstance {
3737
boost::signals2::signal<void (const std::shared_ptr<const CBlock> &)> BlockDisconnected;
3838
boost::signals2::signal<void (const CTransactionRef &)> TransactionRemovedFromMempool;
3939
boost::signals2::signal<void (const CBlockLocator &)> ChainStateFlushed;
40-
boost::signals2::signal<void (int64_t nBestBlockTime, CConnman* connman)> Broadcast;
40+
boost::signals2::signal<void (CConnman* connman)> Broadcast;
4141
boost::signals2::signal<void (const CBlock&, const CValidationState&)> BlockChecked;
4242
boost::signals2::signal<void (const CBlockIndex *, const std::shared_ptr<const CBlock>&)> NewPoWValidBlock;
4343

@@ -101,7 +101,7 @@ void RegisterValidationInterface(CValidationInterface* pwalletIn) {
101101
conns.BlockDisconnected = g_signals.m_internals->BlockDisconnected.connect(std::bind(&CValidationInterface::BlockDisconnected, pwalletIn, std::placeholders::_1));
102102
conns.TransactionRemovedFromMempool = g_signals.m_internals->TransactionRemovedFromMempool.connect(std::bind(&CValidationInterface::TransactionRemovedFromMempool, pwalletIn, std::placeholders::_1));
103103
conns.ChainStateFlushed = g_signals.m_internals->ChainStateFlushed.connect(std::bind(&CValidationInterface::ChainStateFlushed, pwalletIn, std::placeholders::_1));
104-
conns.Broadcast = g_signals.m_internals->Broadcast.connect(std::bind(&CValidationInterface::ResendWalletTransactions, pwalletIn, std::placeholders::_1, std::placeholders::_2));
104+
conns.Broadcast = g_signals.m_internals->Broadcast.connect(std::bind(&CValidationInterface::ResendWalletTransactions, pwalletIn, std::placeholders::_1));
105105
conns.BlockChecked = g_signals.m_internals->BlockChecked.connect(std::bind(&CValidationInterface::BlockChecked, pwalletIn, std::placeholders::_1, std::placeholders::_2));
106106
conns.NewPoWValidBlock = g_signals.m_internals->NewPoWValidBlock.connect(std::bind(&CValidationInterface::NewPoWValidBlock, pwalletIn, std::placeholders::_1, std::placeholders::_2));
107107
}
@@ -175,8 +175,8 @@ void CMainSignals::ChainStateFlushed(const CBlockLocator &locator) {
175175
});
176176
}
177177

178-
void CMainSignals::Broadcast(int64_t nBestBlockTime, CConnman* connman) {
179-
m_internals->Broadcast(nBestBlockTime, connman);
178+
void CMainSignals::Broadcast(CConnman* connman) {
179+
m_internals->Broadcast(connman);
180180
}
181181

182182
void CMainSignals::BlockChecked(const CBlock& block, const CValidationState& state) {

src/validationinterface.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ class CValidationInterface {
135135
*/
136136
virtual void ChainStateFlushed(const CBlockLocator &locator) {}
137137
/** Tells listeners to broadcast their data. */
138-
virtual void ResendWalletTransactions(int64_t nBestBlockTime, CConnman* connman) {}
138+
virtual void ResendWalletTransactions(CConnman* connman) {}
139139
/**
140140
* Notifies listeners of a block validation result.
141141
* If the provided CValidationState IsValid, the provided block
@@ -184,7 +184,7 @@ class CMainSignals {
184184
void BlockConnected(const std::shared_ptr<const CBlock> &, const CBlockIndex *pindex, const std::shared_ptr<const std::vector<CTransactionRef>> &);
185185
void BlockDisconnected(const std::shared_ptr<const CBlock> &);
186186
void ChainStateFlushed(const CBlockLocator &);
187-
void Broadcast(int64_t nBestBlockTime, CConnman* connman);
187+
void Broadcast(CConnman* connman);
188188
void BlockChecked(const CBlock&, const CValidationState&);
189189
void NewPoWValidBlock(const CBlockIndex *, const std::shared_ptr<const CBlock>&);
190190
};

src/wallet/wallet.cpp

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1276,6 +1276,10 @@ void CWallet::BlockDisconnected(const CBlock& block) {
12761276
}
12771277
}
12781278

1279+
void CWallet::UpdatedBlockTip()
1280+
{
1281+
m_best_block_time = GetTime();
1282+
}
12791283

12801284

12811285
void CWallet::BlockUntilSyncedToCurrentChain() {
@@ -2110,7 +2114,7 @@ bool CWalletTx::IsEquivalentTo(const CWalletTx& _tx) const
21102114
return CTransaction(tx1) == CTransaction(tx2);
21112115
}
21122116

2113-
void CWallet::ResendWalletTransactions(interfaces::Chain::Lock& locked_chain, int64_t nBestBlockTime)
2117+
void CWallet::ResendWalletTransactions(interfaces::Chain::Lock& locked_chain)
21142118
{
21152119
// Do this infrequently and randomly to avoid giving away
21162120
// that these are our transactions.
@@ -2120,7 +2124,7 @@ void CWallet::ResendWalletTransactions(interfaces::Chain::Lock& locked_chain, in
21202124
if (fFirst) return;
21212125

21222126
// Only do it if there's been a new block since last time
2123-
if (nBestBlockTime < nLastResend) return;
2127+
if (m_best_block_time < nLastResend) return;
21242128
nLastResend = GetTime();
21252129

21262130
int relayed_tx_count = 0;
@@ -2133,7 +2137,7 @@ void CWallet::ResendWalletTransactions(interfaces::Chain::Lock& locked_chain, in
21332137
CWalletTx& wtx = item.second;
21342138
// only rebroadcast unconfirmed txes older than 5 minutes before the
21352139
// last block was found
2136-
if (wtx.nTimeReceived > nBestBlockTime - 5 * 60) continue;
2140+
if (wtx.nTimeReceived > m_best_block_time - 5 * 60) continue;
21372141
relayed_tx_count += wtx.RelayWalletTransaction(locked_chain) ? 1 : 0;
21382142
}
21392143
} // cs_wallet

src/wallet/wallet.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -657,6 +657,8 @@ class CWallet final : public CCryptoKeyStore, private interfaces::Chain::Notific
657657
int64_t nNextResend = 0;
658658
int64_t nLastResend = 0;
659659
bool fBroadcastTransactions = false;
660+
// Local time that the tip block was received. Used to schedule wallet rebroadcasts.
661+
std::atomic<int64_t> m_best_block_time {0};
660662

661663
/**
662664
* Used to keep track of spent outpoints, and
@@ -926,6 +928,7 @@ class CWallet final : public CCryptoKeyStore, private interfaces::Chain::Notific
926928
void TransactionAddedToMempool(const CTransactionRef& tx) override;
927929
void BlockConnected(const CBlock& block, const std::vector<CTransactionRef>& vtxConflicted) override;
928930
void BlockDisconnected(const CBlock& block) override;
931+
void UpdatedBlockTip() override;
929932
int64_t RescanFromTime(int64_t startTime, const WalletRescanReserver& reserver, bool update);
930933

931934
struct ScanResult {
@@ -946,7 +949,7 @@ class CWallet final : public CCryptoKeyStore, private interfaces::Chain::Notific
946949
ScanResult ScanForWalletTransactions(const uint256& first_block, const uint256& last_block, const WalletRescanReserver& reserver, bool fUpdate);
947950
void TransactionRemovedFromMempool(const CTransactionRef &ptx) override;
948951
void ReacceptWalletTransactions(interfaces::Chain::Lock& locked_chain) EXCLUSIVE_LOCKS_REQUIRED(cs_wallet);
949-
void ResendWalletTransactions(interfaces::Chain::Lock& locked_chain, int64_t nBestBlockTime) override;
952+
void ResendWalletTransactions(interfaces::Chain::Lock& locked_chain) override;
950953
struct Balance {
951954
CAmount m_mine_trusted{0}; //!< Trusted, at depth=GetBalance.min_depth or more
952955
CAmount m_mine_untrusted_pending{0}; //!< Untrusted, but in mempool (pending)

0 commit comments

Comments
 (0)