Skip to content

Commit

Permalink
Auto merge of #4690 - LarryRuane:CopyPreviousWitnessesPart2, r=str4d
Browse files Browse the repository at this point in the history
Flush witness data when consistent (part 2)

Closes #4680. After CWallet::ChainTipAdded() updates the witness data, it may flush it to disk (SetBestChain()); make sure the locator part is consistent with the witnesses (height).
  • Loading branch information
zkbot committed Sep 24, 2020
2 parents ad56e9c + 81db0a2 commit 31f48ca
Show file tree
Hide file tree
Showing 7 changed files with 20 additions and 17 deletions.
2 changes: 0 additions & 2 deletions src/chainparams.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -278,7 +278,6 @@ class CMainParams : public CChainParams {
(525000, uint256S("0x0000000001a36c500378be8862d9bf1bea8f1616da6e155971b608139cc7e39b")),
1556722044, // * UNIX timestamp of last checkpoint block
4653556, // * total number of transactions between genesis and last checkpoint
// (the tx=... number in the SetBestChain debug.log lines)
5106 // * estimated number of transactions per day after checkpoint
// total number of tx / (checkpoint block height / (24 * 24))
};
Expand Down Expand Up @@ -566,7 +565,6 @@ class CTestNetParams : public CChainParams {
(38000, uint256S("0x001e9a2d2e2892b88e9998cf7b079b41d59dd085423a921fe8386cecc42287b8")),
1486897419, // * UNIX timestamp of last checkpoint block
47163, // * total number of transactions between genesis and last checkpoint
// (the tx=... number in the SetBestChain debug.log lines)
715 // total number of tx / (checkpoint block height / (24 * 24))
};

Expand Down
3 changes: 0 additions & 3 deletions src/init.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -223,9 +223,6 @@ void Shutdown()
if (pcoinsTip != NULL) {
FlushStateToDisk();
}
// Flush the wallet witness cache to disk (no longer done by FlushStateToDisk())
GetMainSignals().SetBestChain(chainActive.GetLocator());

delete pcoinsTip;
pcoinsTip = NULL;
delete pcoinscatcher;
Expand Down
4 changes: 4 additions & 0 deletions src/main.h
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,10 @@ static const unsigned int BLOCK_DOWNLOAD_WINDOW = 1024;
static const unsigned int DATABASE_WRITE_INTERVAL = 60 * 60;
/** Time to wait (in seconds) between flushing chainstate to disk. */
static const unsigned int DATABASE_FLUSH_INTERVAL = 24 * 60 * 60;
/** Time to wait (in seconds) between writing wallet witness data to disk. */
static const unsigned int WITNESS_WRITE_INTERVAL = 10 * 60;
/** Number of updates between writing wallet witness data to disk. */
static const unsigned int WITNESS_WRITE_UPDATES = 10000;
/** Maximum length of reject messages. */
static const unsigned int MAX_REJECT_MESSAGE_LENGTH = 111;
static const unsigned int DEFAULT_LIMITFREERELAY = 15;
Expand Down
3 changes: 0 additions & 3 deletions src/validationinterface.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ void RegisterValidationInterface(CValidationInterface* pwalletIn) {
g_signals.EraseTransaction.connect(boost::bind(&CValidationInterface::EraseFromWallet, pwalletIn, _1));
g_signals.UpdatedTransaction.connect(boost::bind(&CValidationInterface::UpdatedTransaction, pwalletIn, _1));
g_signals.ChainTip.connect(boost::bind(&CValidationInterface::ChainTip, pwalletIn, _1, _2, _3));
g_signals.SetBestChain.connect(boost::bind(&CValidationInterface::SetBestChain, pwalletIn, _1));
g_signals.Inventory.connect(boost::bind(&CValidationInterface::Inventory, pwalletIn, _1));
g_signals.Broadcast.connect(boost::bind(&CValidationInterface::ResendWalletTransactions, pwalletIn, _1));
g_signals.BlockChecked.connect(boost::bind(&CValidationInterface::BlockChecked, pwalletIn, _1, _2));
Expand All @@ -44,7 +43,6 @@ void UnregisterValidationInterface(CValidationInterface* pwalletIn) {
g_signals.Broadcast.disconnect(boost::bind(&CValidationInterface::ResendWalletTransactions, pwalletIn, _1));
g_signals.Inventory.disconnect(boost::bind(&CValidationInterface::Inventory, pwalletIn, _1));
g_signals.ChainTip.disconnect(boost::bind(&CValidationInterface::ChainTip, pwalletIn, _1, _2, _3));
g_signals.SetBestChain.disconnect(boost::bind(&CValidationInterface::SetBestChain, pwalletIn, _1));
g_signals.UpdatedTransaction.disconnect(boost::bind(&CValidationInterface::UpdatedTransaction, pwalletIn, _1));
g_signals.EraseTransaction.disconnect(boost::bind(&CValidationInterface::EraseFromWallet, pwalletIn, _1));
g_signals.SyncTransaction.disconnect(boost::bind(&CValidationInterface::SyncTransaction, pwalletIn, _1, _2, _3));
Expand All @@ -58,7 +56,6 @@ void UnregisterAllValidationInterfaces() {
g_signals.Broadcast.disconnect_all_slots();
g_signals.Inventory.disconnect_all_slots();
g_signals.ChainTip.disconnect_all_slots();
g_signals.SetBestChain.disconnect_all_slots();
g_signals.UpdatedTransaction.disconnect_all_slots();
g_signals.EraseTransaction.disconnect_all_slots();
g_signals.SyncTransaction.disconnect_all_slots();
Expand Down
3 changes: 0 additions & 3 deletions src/validationinterface.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ class CValidationInterface {
virtual void SyncTransaction(const CTransaction &tx, const CBlock *pblock, const int nHeight) {}
virtual void EraseFromWallet(const uint256 &hash) {}
virtual void ChainTip(const CBlockIndex *pindex, const CBlock *pblock, boost::optional<std::pair<SproutMerkleTree, SaplingMerkleTree>> added) {}
virtual void SetBestChain(const CBlockLocator &locator) {}
virtual void UpdatedTransaction(const uint256 &hash) {}
virtual void Inventory(const uint256 &hash) {}
virtual void ResendWalletTransactions(int64_t nBestBlockTime) {}
Expand All @@ -59,8 +58,6 @@ struct CMainSignals {
boost::signals2::signal<void (const uint256 &)> UpdatedTransaction;
/** Notifies listeners of a change to the tip of the active block chain. */
boost::signals2::signal<void (const CBlockIndex *, const CBlock *, boost::optional<std::pair<SproutMerkleTree, SaplingMerkleTree>>)> ChainTip;
/** Notifies listeners of a new active block chain. */
boost::signals2::signal<void (const CBlockLocator &)> SetBestChain;
/** Notifies listeners about an inventory item being seen on the network. */
boost::signals2::signal<void (const uint256 &)> Inventory;
/** Tells listeners to broadcast their data. */
Expand Down
20 changes: 14 additions & 6 deletions src/wallet/wallet.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -587,18 +587,26 @@ void CWallet::ChainTipAdded(const CBlockIndex *pindex,
IncrementNoteWitnesses(pindex, pblock, sproutTree, saplingTree);
UpdateSaplingNullifierNoteMapForBlock(pblock);

// SetBestChain() can be expensive for large wallets, so do this
// at most once per hour; the wallet state will be brought up to
// date during rescanning on startup.
// SetBestChain() can be expensive for large wallets, so do only
// this sometimes; the wallet state will be brought up to date
// during rescanning on startup.
int64_t nNow = GetTimeMicros();
if (nLastSetChain == 0) {
// Don't flush during startup.
nLastSetChain = nNow;
}
if (nLastSetChain + (int64_t)DATABASE_WRITE_INTERVAL * 1000000 < nNow) {
if (++nSetChainUpdates >= WITNESS_WRITE_UPDATES ||
nLastSetChain + (int64_t)WITNESS_WRITE_INTERVAL * 1000000 < nNow) {
nLastSetChain = nNow;
LOCK(cs_main);
SetBestChain(chainActive.GetLocator());
nSetChainUpdates = 0;
CBlockLocator loc;
{
// The locator must be derived from the pindex used to increment
// the witnesses above; pindex can be behind chainActive.Tip().
LOCK(cs_main);
loc = chainActive.GetLocator(pindex);
}
SetBestChain(loc);
}
}

Expand Down
2 changes: 2 additions & 0 deletions src/wallet/wallet.h
Original file line number Diff line number Diff line change
Expand Up @@ -767,6 +767,7 @@ class CWallet : public CCryptoKeyStore, public CValidationInterface
int64_t nNextResend;
int64_t nLastResend;
int64_t nLastSetChain;
int nSetChainUpdates;
bool fBroadcastTransactions;

template <class T>
Expand Down Expand Up @@ -928,6 +929,7 @@ class CWallet : public CCryptoKeyStore, public CValidationInterface
nNextResend = 0;
nLastResend = 0;
nLastSetChain = 0;
nSetChainUpdates = 0;
nTimeFirstKey = 0;
fBroadcastTransactions = false;
nWitnessCacheSize = 0;
Expand Down

0 comments on commit 31f48ca

Please sign in to comment.