[wallet] Keep track of the best block time in the wallet

Move nTimeBestReceived (which is only used for wallet
rebroadcasts) into the wallet.
This commit is contained in:
John Newbery 2019-03-28 13:15:47 -04:00
parent f3ecf3025f
commit f463cd1073
7 changed files with 26 additions and 18 deletions

View file

@ -202,8 +202,12 @@ public:
{ {
m_notifications->BlockDisconnected(*block); m_notifications->BlockDisconnected(*block);
} }
void UpdatedBlockTip(const CBlockIndex* index, const CBlockIndex* fork_index, bool is_ibd) override
{
m_notifications->UpdatedBlockTip();
}
void ChainStateFlushed(const CBlockLocator& locator) override { m_notifications->ChainStateFlushed(locator); } void ChainStateFlushed(const CBlockLocator& locator) override { m_notifications->ChainStateFlushed(locator); }
void ResendWalletTransactions(int64_t best_block_time, CConnman*) override void ResendWalletTransactions(CConnman*) override
{ {
// `cs_main` is always held when this method is called, so it is safe to // `cs_main` is always held when this method is called, so it is safe to
// call `assumeLocked`. This is awkward, and the `assumeLocked` method // call `assumeLocked`. This is awkward, and the `assumeLocked` method
@ -211,7 +215,7 @@ public:
// is replaced by a wallet timer as suggested in // is replaced by a wallet timer as suggested in
// https://github.com/bitcoin/bitcoin/issues/15619 // https://github.com/bitcoin/bitcoin/issues/15619
auto locked_chain = m_chain.assumeLocked(); auto locked_chain = m_chain.assumeLocked();
m_notifications->ResendWalletTransactions(*locked_chain, best_block_time); m_notifications->ResendWalletTransactions(*locked_chain);
} }
Chain& m_chain; Chain& m_chain;
Chain::Notifications* m_notifications; Chain::Notifications* m_notifications;

View file

@ -256,8 +256,9 @@ public:
virtual void TransactionRemovedFromMempool(const CTransactionRef& ptx) {} virtual void TransactionRemovedFromMempool(const CTransactionRef& ptx) {}
virtual void BlockConnected(const CBlock& block, const std::vector<CTransactionRef>& tx_conflicted) {} virtual void BlockConnected(const CBlock& block, const std::vector<CTransactionRef>& tx_conflicted) {}
virtual void BlockDisconnected(const CBlock& block) {} virtual void BlockDisconnected(const CBlock& block) {}
virtual void UpdatedBlockTip() {}
virtual void ChainStateFlushed(const CBlockLocator& locator) {} virtual void ChainStateFlushed(const CBlockLocator& locator) {}
virtual void ResendWalletTransactions(Lock& locked_chain, int64_t best_block_time) {} virtual void ResendWalletTransactions(Lock& locked_chain) {}
}; };
//! Register handler for notifications. //! Register handler for notifications.

View file

@ -175,8 +175,6 @@ namespace {
/** Expiration-time ordered list of (expire time, relay map entry) pairs. */ /** Expiration-time ordered list of (expire time, relay map entry) pairs. */
std::deque<std::pair<int64_t, MapRelay::iterator>> vRelayExpiration GUARDED_BY(cs_main); std::deque<std::pair<int64_t, MapRelay::iterator>> vRelayExpiration GUARDED_BY(cs_main);
std::atomic<int64_t> nTimeBestReceived(0); // Used only to inform the wallet of when we last received a block
struct IteratorComparator struct IteratorComparator
{ {
template<typename I> template<typename I>
@ -1121,8 +1119,6 @@ void PeerLogicValidation::UpdatedBlockTip(const CBlockIndex *pindexNew, const CB
}); });
connman->WakeMessageHandler(); connman->WakeMessageHandler();
} }
nTimeBestReceived = GetTime();
} }
/** /**
@ -3555,7 +3551,7 @@ bool PeerLogicValidation::SendMessages(CNode* pto)
// transactions become unconfirmed and spams other nodes. // transactions become unconfirmed and spams other nodes.
if (!fReindex && !fImporting && !IsInitialBlockDownload()) if (!fReindex && !fImporting && !IsInitialBlockDownload())
{ {
GetMainSignals().Broadcast(nTimeBestReceived, connman); GetMainSignals().Broadcast(connman);
} }
// //

View file

@ -37,7 +37,7 @@ struct MainSignalsInstance {
boost::signals2::signal<void (const std::shared_ptr<const CBlock> &)> BlockDisconnected; boost::signals2::signal<void (const std::shared_ptr<const CBlock> &)> BlockDisconnected;
boost::signals2::signal<void (const CTransactionRef &)> TransactionRemovedFromMempool; boost::signals2::signal<void (const CTransactionRef &)> TransactionRemovedFromMempool;
boost::signals2::signal<void (const CBlockLocator &)> ChainStateFlushed; boost::signals2::signal<void (const CBlockLocator &)> ChainStateFlushed;
boost::signals2::signal<void (int64_t nBestBlockTime, CConnman* connman)> Broadcast; boost::signals2::signal<void (CConnman* connman)> Broadcast;
boost::signals2::signal<void (const CBlock&, const CValidationState&)> BlockChecked; boost::signals2::signal<void (const CBlock&, const CValidationState&)> BlockChecked;
boost::signals2::signal<void (const CBlockIndex *, const std::shared_ptr<const CBlock>&)> NewPoWValidBlock; boost::signals2::signal<void (const CBlockIndex *, const std::shared_ptr<const CBlock>&)> NewPoWValidBlock;
@ -101,7 +101,7 @@ void RegisterValidationInterface(CValidationInterface* pwalletIn) {
conns.BlockDisconnected = g_signals.m_internals->BlockDisconnected.connect(std::bind(&CValidationInterface::BlockDisconnected, pwalletIn, std::placeholders::_1)); conns.BlockDisconnected = g_signals.m_internals->BlockDisconnected.connect(std::bind(&CValidationInterface::BlockDisconnected, pwalletIn, std::placeholders::_1));
conns.TransactionRemovedFromMempool = g_signals.m_internals->TransactionRemovedFromMempool.connect(std::bind(&CValidationInterface::TransactionRemovedFromMempool, pwalletIn, std::placeholders::_1)); conns.TransactionRemovedFromMempool = g_signals.m_internals->TransactionRemovedFromMempool.connect(std::bind(&CValidationInterface::TransactionRemovedFromMempool, pwalletIn, std::placeholders::_1));
conns.ChainStateFlushed = g_signals.m_internals->ChainStateFlushed.connect(std::bind(&CValidationInterface::ChainStateFlushed, pwalletIn, std::placeholders::_1)); conns.ChainStateFlushed = g_signals.m_internals->ChainStateFlushed.connect(std::bind(&CValidationInterface::ChainStateFlushed, pwalletIn, std::placeholders::_1));
conns.Broadcast = g_signals.m_internals->Broadcast.connect(std::bind(&CValidationInterface::ResendWalletTransactions, pwalletIn, std::placeholders::_1, std::placeholders::_2)); conns.Broadcast = g_signals.m_internals->Broadcast.connect(std::bind(&CValidationInterface::ResendWalletTransactions, pwalletIn, std::placeholders::_1));
conns.BlockChecked = g_signals.m_internals->BlockChecked.connect(std::bind(&CValidationInterface::BlockChecked, pwalletIn, std::placeholders::_1, std::placeholders::_2)); conns.BlockChecked = g_signals.m_internals->BlockChecked.connect(std::bind(&CValidationInterface::BlockChecked, pwalletIn, std::placeholders::_1, std::placeholders::_2));
conns.NewPoWValidBlock = g_signals.m_internals->NewPoWValidBlock.connect(std::bind(&CValidationInterface::NewPoWValidBlock, pwalletIn, std::placeholders::_1, std::placeholders::_2)); conns.NewPoWValidBlock = g_signals.m_internals->NewPoWValidBlock.connect(std::bind(&CValidationInterface::NewPoWValidBlock, pwalletIn, std::placeholders::_1, std::placeholders::_2));
} }
@ -175,8 +175,8 @@ void CMainSignals::ChainStateFlushed(const CBlockLocator &locator) {
}); });
} }
void CMainSignals::Broadcast(int64_t nBestBlockTime, CConnman* connman) { void CMainSignals::Broadcast(CConnman* connman) {
m_internals->Broadcast(nBestBlockTime, connman); m_internals->Broadcast(connman);
} }
void CMainSignals::BlockChecked(const CBlock& block, const CValidationState& state) { void CMainSignals::BlockChecked(const CBlock& block, const CValidationState& state) {

View file

@ -135,7 +135,7 @@ protected:
*/ */
virtual void ChainStateFlushed(const CBlockLocator &locator) {} virtual void ChainStateFlushed(const CBlockLocator &locator) {}
/** Tells listeners to broadcast their data. */ /** Tells listeners to broadcast their data. */
virtual void ResendWalletTransactions(int64_t nBestBlockTime, CConnman* connman) {} virtual void ResendWalletTransactions(CConnman* connman) {}
/** /**
* Notifies listeners of a block validation result. * Notifies listeners of a block validation result.
* If the provided CValidationState IsValid, the provided block * If the provided CValidationState IsValid, the provided block
@ -184,7 +184,7 @@ public:
void BlockConnected(const std::shared_ptr<const CBlock> &, const CBlockIndex *pindex, const std::shared_ptr<const std::vector<CTransactionRef>> &); void BlockConnected(const std::shared_ptr<const CBlock> &, const CBlockIndex *pindex, const std::shared_ptr<const std::vector<CTransactionRef>> &);
void BlockDisconnected(const std::shared_ptr<const CBlock> &); void BlockDisconnected(const std::shared_ptr<const CBlock> &);
void ChainStateFlushed(const CBlockLocator &); void ChainStateFlushed(const CBlockLocator &);
void Broadcast(int64_t nBestBlockTime, CConnman* connman); void Broadcast(CConnman* connman);
void BlockChecked(const CBlock&, const CValidationState&); void BlockChecked(const CBlock&, const CValidationState&);
void NewPoWValidBlock(const CBlockIndex *, const std::shared_ptr<const CBlock>&); void NewPoWValidBlock(const CBlockIndex *, const std::shared_ptr<const CBlock>&);
}; };

View file

@ -1276,6 +1276,10 @@ void CWallet::BlockDisconnected(const CBlock& block) {
} }
} }
void CWallet::UpdatedBlockTip()
{
m_best_block_time = GetTime();
}
void CWallet::BlockUntilSyncedToCurrentChain() { void CWallet::BlockUntilSyncedToCurrentChain() {
@ -2110,7 +2114,7 @@ bool CWalletTx::IsEquivalentTo(const CWalletTx& _tx) const
return CTransaction(tx1) == CTransaction(tx2); return CTransaction(tx1) == CTransaction(tx2);
} }
void CWallet::ResendWalletTransactions(interfaces::Chain::Lock& locked_chain, int64_t nBestBlockTime) void CWallet::ResendWalletTransactions(interfaces::Chain::Lock& locked_chain)
{ {
// Do this infrequently and randomly to avoid giving away // Do this infrequently and randomly to avoid giving away
// that these are our transactions. // that these are our transactions.
@ -2120,7 +2124,7 @@ void CWallet::ResendWalletTransactions(interfaces::Chain::Lock& locked_chain, in
if (fFirst) return; if (fFirst) return;
// Only do it if there's been a new block since last time // Only do it if there's been a new block since last time
if (nBestBlockTime < nLastResend) return; if (m_best_block_time < nLastResend) return;
nLastResend = GetTime(); nLastResend = GetTime();
int relayed_tx_count = 0; int relayed_tx_count = 0;
@ -2133,7 +2137,7 @@ void CWallet::ResendWalletTransactions(interfaces::Chain::Lock& locked_chain, in
CWalletTx& wtx = item.second; CWalletTx& wtx = item.second;
// only rebroadcast unconfirmed txes older than 5 minutes before the // only rebroadcast unconfirmed txes older than 5 minutes before the
// last block was found // last block was found
if (wtx.nTimeReceived > nBestBlockTime - 5 * 60) continue; if (wtx.nTimeReceived > m_best_block_time - 5 * 60) continue;
relayed_tx_count += wtx.RelayWalletTransaction(locked_chain) ? 1 : 0; relayed_tx_count += wtx.RelayWalletTransaction(locked_chain) ? 1 : 0;
} }
} // cs_wallet } // cs_wallet

View file

@ -657,6 +657,8 @@ private:
int64_t nNextResend = 0; int64_t nNextResend = 0;
int64_t nLastResend = 0; int64_t nLastResend = 0;
bool fBroadcastTransactions = false; bool fBroadcastTransactions = false;
// Local time that the tip block was received. Used to schedule wallet rebroadcasts.
std::atomic<int64_t> m_best_block_time {0};
/** /**
* Used to keep track of spent outpoints, and * Used to keep track of spent outpoints, and
@ -926,6 +928,7 @@ public:
void TransactionAddedToMempool(const CTransactionRef& tx) override; void TransactionAddedToMempool(const CTransactionRef& tx) override;
void BlockConnected(const CBlock& block, const std::vector<CTransactionRef>& vtxConflicted) override; void BlockConnected(const CBlock& block, const std::vector<CTransactionRef>& vtxConflicted) override;
void BlockDisconnected(const CBlock& block) override; void BlockDisconnected(const CBlock& block) override;
void UpdatedBlockTip() override;
int64_t RescanFromTime(int64_t startTime, const WalletRescanReserver& reserver, bool update); int64_t RescanFromTime(int64_t startTime, const WalletRescanReserver& reserver, bool update);
struct ScanResult { struct ScanResult {
@ -946,7 +949,7 @@ public:
ScanResult ScanForWalletTransactions(const uint256& first_block, const uint256& last_block, const WalletRescanReserver& reserver, bool fUpdate); ScanResult ScanForWalletTransactions(const uint256& first_block, const uint256& last_block, const WalletRescanReserver& reserver, bool fUpdate);
void TransactionRemovedFromMempool(const CTransactionRef &ptx) override; void TransactionRemovedFromMempool(const CTransactionRef &ptx) override;
void ReacceptWalletTransactions(interfaces::Chain::Lock& locked_chain) EXCLUSIVE_LOCKS_REQUIRED(cs_wallet); void ReacceptWalletTransactions(interfaces::Chain::Lock& locked_chain) EXCLUSIVE_LOCKS_REQUIRED(cs_wallet);
void ResendWalletTransactions(interfaces::Chain::Lock& locked_chain, int64_t nBestBlockTime) override; void ResendWalletTransactions(interfaces::Chain::Lock& locked_chain) override;
struct Balance { struct Balance {
CAmount m_mine_trusted{0}; //!< Trusted, at depth=GetBalance.min_depth or more CAmount m_mine_trusted{0}; //!< Trusted, at depth=GetBalance.min_depth or more
CAmount m_mine_untrusted_pending{0}; //!< Untrusted, but in mempool (pending) CAmount m_mine_untrusted_pending{0}; //!< Untrusted, but in mempool (pending)