Skip to content

Commit bcdd3e9

Browse files
committed
Move ChainTip sapling update witnesses and nullifiers to BlockConnected/BlockDisconnected.
1 parent b799070 commit bcdd3e9

File tree

9 files changed

+40
-39
lines changed

9 files changed

+40
-39
lines changed

src/sapling/saplingscriptpubkeyman.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -300,11 +300,11 @@ void DecrementNoteWitnesses(NoteDataMap& noteDataMap, int indexHeight, int64_t n
300300
}
301301
}
302302

303-
void SaplingScriptPubKeyMan::DecrementNoteWitnesses(const CBlockIndex* pindex)
303+
void SaplingScriptPubKeyMan::DecrementNoteWitnesses(int nChainHeight)
304304
{
305305
LOCK(wallet->cs_wallet);
306306
for (std::pair<const uint256, CWalletTx>& wtxItem : wallet->mapWallet) {
307-
::DecrementNoteWitnesses(wtxItem.second.mapSaplingNoteData, pindex->nHeight, nWitnessCacheSize);
307+
::DecrementNoteWitnesses(wtxItem.second.mapSaplingNoteData, nChainHeight, nWitnessCacheSize);
308308
}
309309
nWitnessCacheSize -= 1;
310310
nWitnessCacheNeedsUpdate = true;

src/sapling/saplingscriptpubkeyman.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -163,9 +163,9 @@ class SaplingScriptPubKeyMan {
163163
const CBlock* pblock,
164164
SaplingMerkleTree& saplingTree);
165165
/**
166-
* pindex is the old tip being disconnected.
166+
* nChainHeight is the old tip height being disconnected.
167167
*/
168-
void DecrementNoteWitnesses(const CBlockIndex* pindex);
168+
void DecrementNoteWitnesses(int nChainHeight);
169169

170170
/**
171171
* Update mapSaplingNullifiersToNotes

src/validation.cpp

Lines changed: 1 addition & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -2004,12 +2004,7 @@ bool static DisconnectTip(CValidationState& state, const CChainParams& chainpara
20042004
UpdateTip(pindexDelete->pprev);
20052005
// Let wallets know transactions went from 1-confirmed to
20062006
// 0-confirmed or conflicted:
2007-
GetMainSignals().BlockDisconnected(pblock);
2008-
2009-
if (chainparams.GetConsensus().NetworkUpgradeActive(pindexDelete->nHeight, Consensus::UPGRADE_V5_0)) {
2010-
// Update Sapling cached incremental witnesses
2011-
GetMainSignals().ChainTip(pindexDelete, &block, nullopt);
2012-
}
2007+
GetMainSignals().BlockDisconnected(pblock, pindexDelete->nHeight);
20132008

20142009
return true;
20152010
}
@@ -2361,22 +2356,6 @@ bool ActivateBestChain(CValidationState& state, std::shared_ptr<const CBlock> pb
23612356
for (const PerBlockConnectTrace& trace : connectTrace.GetBlocksConnected()) {
23622357
assert(trace.pblock && trace.pindex);
23632358
GetMainSignals().BlockConnected(trace.pblock, trace.pindex, *trace.conflictedTxs);
2364-
2365-
// Sapling: notify wallet about the connected blocks ordered
2366-
// Get prev block tree anchor
2367-
CBlockIndex* pprev = trace.pindex->pprev;
2368-
SaplingMerkleTree oldSaplingTree;
2369-
bool isSaplingActive = (pprev) != nullptr &&
2370-
Params().GetConsensus().NetworkUpgradeActive(pprev->nHeight,
2371-
Consensus::UPGRADE_V5_0);
2372-
if (isSaplingActive) {
2373-
assert(pcoinsTip->GetSaplingAnchorAt(pprev->hashFinalSaplingRoot, oldSaplingTree));
2374-
} else {
2375-
assert(pcoinsTip->GetSaplingAnchorAt(SaplingMerkleTree::empty_root(), oldSaplingTree));
2376-
}
2377-
2378-
// Sapling: Update cached incremental witnesses
2379-
GetMainSignals().ChainTip(trace.pindex, trace.pblock.get(), oldSaplingTree);
23802359
}
23812360

23822361
break;

src/validationinterface.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ struct MainSignalsInstance {
3434
*/
3535
boost::signals2::signal<void (const std::shared_ptr<const CBlock> &, const CBlockIndex *pindex, const std::vector<CTransactionRef> &)> BlockConnected;
3636
/** Notifies listeners of a block being disconnected */
37-
boost::signals2::signal<void (const std::shared_ptr<const CBlock> &)> BlockDisconnected;
37+
boost::signals2::signal<void (const std::shared_ptr<const CBlock> &, int nBlockHeight)> BlockDisconnected;
3838
/** Notifies listeners of an updated transaction lock without new data. */
3939
boost::signals2::signal<void (const CTransaction &)> NotifyTransactionLock;
4040
/** Notifies listeners of an updated transaction without new data (for now: a coinbase potentially becoming visible). */
@@ -69,7 +69,7 @@ void RegisterValidationInterface(CValidationInterface* pwalletIn)
6969
conns.UpdatedBlockTip = g_signals.m_internals->UpdatedBlockTip.connect(std::bind(&CValidationInterface::UpdatedBlockTip, pwalletIn, std::placeholders::_1, std::placeholders::_2, std::placeholders::_3));
7070
conns.TransactionAddedToMempool = g_signals.m_internals->TransactionAddedToMempool.connect(std::bind(&CValidationInterface::TransactionAddedToMempool, pwalletIn, std::placeholders::_1));
7171
conns.BlockConnected = g_signals.m_internals->BlockConnected.connect(std::bind(&CValidationInterface::BlockConnected, pwalletIn, std::placeholders::_1, std::placeholders::_2, std::placeholders::_3));
72-
conns.BlockDisconnected = g_signals.m_internals->BlockDisconnected.connect(std::bind(&CValidationInterface::BlockDisconnected, pwalletIn, std::placeholders::_1));
72+
conns.BlockDisconnected = g_signals.m_internals->BlockDisconnected.connect(std::bind(&CValidationInterface::BlockDisconnected, pwalletIn, std::placeholders::_1, std::placeholders::_2));
7373
conns.ChainTip = g_signals.m_internals->ChainTip.connect(std::bind(&CValidationInterface::ChainTip, pwalletIn, std::placeholders::_1, std::placeholders::_2, std::placeholders::_3));
7474
conns.NotifyTransactionLock = g_signals.m_internals->NotifyTransactionLock.connect(std::bind(&CValidationInterface::NotifyTransactionLock, pwalletIn, std::placeholders::_1));
7575
conns.UpdatedTransaction = g_signals.m_internals->UpdatedTransaction.connect(std::bind(&CValidationInterface::UpdatedTransaction, pwalletIn, std::placeholders::_1));
@@ -105,8 +105,8 @@ void CMainSignals::BlockConnected(const std::shared_ptr<const CBlock> &block, co
105105
m_internals->BlockConnected(block, pindex, txnConflicted);
106106
}
107107

108-
void CMainSignals::BlockDisconnected(const std::shared_ptr<const CBlock> &block) {
109-
m_internals->BlockDisconnected(block);
108+
void CMainSignals::BlockDisconnected(const std::shared_ptr<const CBlock> &block, int nBlockHeight) {
109+
m_internals->BlockDisconnected(block, nBlockHeight);
110110
}
111111

112112
void CMainSignals::NotifyTransactionLock(const CTransaction& tx) {

src/validationinterface.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ class CValidationInterface {
3434
virtual void UpdatedBlockTip(const CBlockIndex *pindexNew, const CBlockIndex *pindexFork, bool fInitialDownload) {}
3535
virtual void TransactionAddedToMempool(const CTransactionRef &ptxn) {}
3636
virtual void BlockConnected(const std::shared_ptr<const CBlock> &block, const CBlockIndex *pindex, const std::vector<CTransactionRef> &txnConflicted) {}
37-
virtual void BlockDisconnected(const std::shared_ptr<const CBlock> &block) {}
37+
virtual void BlockDisconnected(const std::shared_ptr<const CBlock> &block, int nBlockHeight) {}
3838
virtual void ChainTip(const CBlockIndex *pindex, const CBlock *pblock, Optional<SaplingMerkleTree> added) {}
3939
virtual void NotifyTransactionLock(const CTransaction &tx) {}
4040
/** Notifies listeners of the new active block chain on-disk. */
@@ -62,7 +62,7 @@ class CMainSignals {
6262
void UpdatedBlockTip(const CBlockIndex *, const CBlockIndex *, bool fInitialDownload);
6363
void TransactionAddedToMempool(const CTransactionRef &ptxn);
6464
void BlockConnected(const std::shared_ptr<const CBlock> &block, const CBlockIndex *pindex, const std::vector<CTransactionRef> &txnConflicted);
65-
void BlockDisconnected(const std::shared_ptr<const CBlock> &block);
65+
void BlockDisconnected(const std::shared_ptr<const CBlock> &block, int nBlockHeight);
6666
void NotifyTransactionLock(const CTransaction&);
6767
void UpdatedTransaction(const uint256 &);
6868
void SetBestChain(const CBlockLocator &);

src/wallet/wallet.cpp

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1267,23 +1267,45 @@ void CWallet::BlockConnected(const std::shared_ptr<const CBlock>& pblock, const
12671267
// state of transactions in our wallet is currently cleared when we
12681268
// receive another notification and there is a race condition where
12691269
// notification of a connected conflict might cause an outside process
1270-
// to abandon a transaction and then have it inadvertantly cleared by
1270+
// to abandon a transaction and then have it inadvertently cleared by
12711271
// the notification that the conflicted transaction was evicted.
12721272

12731273
for (const CTransactionRef& ptx : vtxConflicted) {
1274-
SyncTransaction(ptx, NULL, -1);
1274+
SyncTransaction(ptx, nullptr, -1);
12751275
}
12761276
for (size_t i = 0; i < pblock->vtx.size(); i++) {
12771277
SyncTransaction(pblock->vtx[i], pindex, i);
12781278
}
1279+
1280+
// Sapling: notify about the connected block
1281+
// Get prev block tree anchor
1282+
CBlockIndex* pprev = pindex->pprev;
1283+
SaplingMerkleTree oldSaplingTree;
1284+
bool isSaplingActive = (pprev) != nullptr &&
1285+
Params().GetConsensus().NetworkUpgradeActive(pprev->nHeight,
1286+
Consensus::UPGRADE_V5_0);
1287+
if (isSaplingActive) {
1288+
assert(pcoinsTip->GetSaplingAnchorAt(pprev->hashFinalSaplingRoot, oldSaplingTree));
1289+
} else {
1290+
assert(pcoinsTip->GetSaplingAnchorAt(SaplingMerkleTree::empty_root(), oldSaplingTree));
1291+
}
1292+
1293+
// Sapling: Update cached incremental witnesses
1294+
ChainTipAdded(pindex, pblock.get(), oldSaplingTree);
12791295
}
12801296

1281-
void CWallet::BlockDisconnected(const std::shared_ptr<const CBlock>& pblock)
1297+
void CWallet::BlockDisconnected(const std::shared_ptr<const CBlock>& pblock, int nBlockHeight)
12821298
{
12831299
LOCK2(cs_main, cs_wallet);
12841300
for (const CTransactionRef& ptx : pblock->vtx) {
12851301
SyncTransaction(ptx, NULL, -1);
12861302
}
1303+
1304+
if (Params().GetConsensus().NetworkUpgradeActive(nBlockHeight, Consensus::UPGRADE_V5_0)) {
1305+
// Update Sapling cached incremental witnesses
1306+
m_sspk_man->DecrementNoteWitnesses(nBlockHeight);
1307+
m_sspk_man->UpdateSaplingNullifierNoteMapForBlock(pblock.get());
1308+
}
12871309
}
12881310

12891311
void CWallet::MarkAffectedTransactionsDirty(const CTransaction& tx)
@@ -4490,7 +4512,7 @@ void CWallet::IncrementNoteWitnesses(const CBlockIndex* pindex,
44904512
const CBlock* pblock,
44914513
SaplingMerkleTree& saplingTree) { m_sspk_man->IncrementNoteWitnesses(pindex, pblock, saplingTree); }
44924514

4493-
void CWallet::DecrementNoteWitnesses(const CBlockIndex* pindex) { m_sspk_man->DecrementNoteWitnesses(pindex); }
4515+
void CWallet::DecrementNoteWitnesses(const CBlockIndex* pindex) { m_sspk_man->DecrementNoteWitnesses(pindex->nHeight); }
44944516

44954517
bool CWallet::AddSaplingZKey(const libzcash::SaplingExtendedSpendingKey &key) { return m_sspk_man->AddSaplingZKey(key); }
44964518

src/wallet/wallet.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -603,7 +603,7 @@ class CWallet : public CCryptoKeyStore, public CValidationInterface
603603
bool LoadToWallet(const CWalletTx& wtxIn);
604604
void TransactionAddedToMempool(const CTransactionRef& tx) override;
605605
void BlockConnected(const std::shared_ptr<const CBlock>& pblock, const CBlockIndex *pindex, const std::vector<CTransactionRef>& vtxConflicted) override;
606-
void BlockDisconnected(const std::shared_ptr<const CBlock>& pblock) override;
606+
void BlockDisconnected(const std::shared_ptr<const CBlock>& pblock, int nBlockHeight) override;
607607
bool AddToWalletIfInvolvingMe(const CTransactionRef& tx, const uint256& blockHash, int posInBlock, bool fUpdate);
608608
void EraseFromWallet(const uint256& hash);
609609

src/zmq/zmqnotificationinterface.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,7 @@ void CZMQNotificationInterface::BlockConnected(const std::shared_ptr<const CBloc
174174
}
175175
}
176176

177-
void CZMQNotificationInterface::BlockDisconnected(const std::shared_ptr<const CBlock>& pblock)
177+
void CZMQNotificationInterface::BlockDisconnected(const std::shared_ptr<const CBlock>& pblock, int nBlockHeight)
178178
{
179179
for (const CTransactionRef& ptx : pblock->vtx) {
180180
// Do a normal notify for each transaction removed in block disconnection

src/zmq/zmqnotificationinterface.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ class CZMQNotificationInterface : public CValidationInterface
2727
// CValidationInterface
2828
void TransactionAddedToMempool(const CTransactionRef& tx) override;
2929
void BlockConnected(const std::shared_ptr<const CBlock>& pblock, const CBlockIndex* pindexConnected, const std::vector<CTransactionRef>& vtxConflicted) override;
30-
void BlockDisconnected(const std::shared_ptr<const CBlock>& pblock) override;
30+
void BlockDisconnected(const std::shared_ptr<const CBlock>& pblock, int nBlockHeight) override;
3131
void NotifyTransactionLock(const CTransaction &tx) override;
3232
void UpdatedBlockTip(const CBlockIndex *pindexNew, const CBlockIndex *pindexFork, bool fInitialDownload) override;
3333

0 commit comments

Comments
 (0)