Skip to content

Commit

Permalink
Move ChainTip sapling update witnesses and nullifiers to BlockConnect…
Browse files Browse the repository at this point in the history
…ed/BlockDisconnected.
  • Loading branch information
furszy committed Jan 28, 2021
1 parent b799070 commit bcdd3e9
Show file tree
Hide file tree
Showing 9 changed files with 40 additions and 39 deletions.
4 changes: 2 additions & 2 deletions src/sapling/saplingscriptpubkeyman.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -300,11 +300,11 @@ void DecrementNoteWitnesses(NoteDataMap& noteDataMap, int indexHeight, int64_t n
}
}

void SaplingScriptPubKeyMan::DecrementNoteWitnesses(const CBlockIndex* pindex)
void SaplingScriptPubKeyMan::DecrementNoteWitnesses(int nChainHeight)
{
LOCK(wallet->cs_wallet);
for (std::pair<const uint256, CWalletTx>& wtxItem : wallet->mapWallet) {
::DecrementNoteWitnesses(wtxItem.second.mapSaplingNoteData, pindex->nHeight, nWitnessCacheSize);
::DecrementNoteWitnesses(wtxItem.second.mapSaplingNoteData, nChainHeight, nWitnessCacheSize);
}
nWitnessCacheSize -= 1;
nWitnessCacheNeedsUpdate = true;
Expand Down
4 changes: 2 additions & 2 deletions src/sapling/saplingscriptpubkeyman.h
Original file line number Diff line number Diff line change
Expand Up @@ -163,9 +163,9 @@ class SaplingScriptPubKeyMan {
const CBlock* pblock,
SaplingMerkleTree& saplingTree);
/**
* pindex is the old tip being disconnected.
* nChainHeight is the old tip height being disconnected.
*/
void DecrementNoteWitnesses(const CBlockIndex* pindex);
void DecrementNoteWitnesses(int nChainHeight);

/**
* Update mapSaplingNullifiersToNotes
Expand Down
23 changes: 1 addition & 22 deletions src/validation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2004,12 +2004,7 @@ bool static DisconnectTip(CValidationState& state, const CChainParams& chainpara
UpdateTip(pindexDelete->pprev);
// Let wallets know transactions went from 1-confirmed to
// 0-confirmed or conflicted:
GetMainSignals().BlockDisconnected(pblock);

if (chainparams.GetConsensus().NetworkUpgradeActive(pindexDelete->nHeight, Consensus::UPGRADE_V5_0)) {
// Update Sapling cached incremental witnesses
GetMainSignals().ChainTip(pindexDelete, &block, nullopt);
}
GetMainSignals().BlockDisconnected(pblock, pindexDelete->nHeight);

return true;
}
Expand Down Expand Up @@ -2361,22 +2356,6 @@ bool ActivateBestChain(CValidationState& state, std::shared_ptr<const CBlock> pb
for (const PerBlockConnectTrace& trace : connectTrace.GetBlocksConnected()) {
assert(trace.pblock && trace.pindex);
GetMainSignals().BlockConnected(trace.pblock, trace.pindex, *trace.conflictedTxs);

// Sapling: notify wallet about the connected blocks ordered
// Get prev block tree anchor
CBlockIndex* pprev = trace.pindex->pprev;
SaplingMerkleTree oldSaplingTree;
bool isSaplingActive = (pprev) != nullptr &&
Params().GetConsensus().NetworkUpgradeActive(pprev->nHeight,
Consensus::UPGRADE_V5_0);
if (isSaplingActive) {
assert(pcoinsTip->GetSaplingAnchorAt(pprev->hashFinalSaplingRoot, oldSaplingTree));
} else {
assert(pcoinsTip->GetSaplingAnchorAt(SaplingMerkleTree::empty_root(), oldSaplingTree));
}

// Sapling: Update cached incremental witnesses
GetMainSignals().ChainTip(trace.pindex, trace.pblock.get(), oldSaplingTree);
}

break;
Expand Down
8 changes: 4 additions & 4 deletions src/validationinterface.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ struct MainSignalsInstance {
*/
boost::signals2::signal<void (const std::shared_ptr<const CBlock> &, const CBlockIndex *pindex, const std::vector<CTransactionRef> &)> BlockConnected;
/** Notifies listeners of a block being disconnected */
boost::signals2::signal<void (const std::shared_ptr<const CBlock> &)> BlockDisconnected;
boost::signals2::signal<void (const std::shared_ptr<const CBlock> &, int nBlockHeight)> BlockDisconnected;
/** Notifies listeners of an updated transaction lock without new data. */
boost::signals2::signal<void (const CTransaction &)> NotifyTransactionLock;
/** Notifies listeners of an updated transaction without new data (for now: a coinbase potentially becoming visible). */
Expand Down Expand Up @@ -69,7 +69,7 @@ void RegisterValidationInterface(CValidationInterface* pwalletIn)
conns.UpdatedBlockTip = g_signals.m_internals->UpdatedBlockTip.connect(std::bind(&CValidationInterface::UpdatedBlockTip, pwalletIn, std::placeholders::_1, std::placeholders::_2, std::placeholders::_3));
conns.TransactionAddedToMempool = g_signals.m_internals->TransactionAddedToMempool.connect(std::bind(&CValidationInterface::TransactionAddedToMempool, pwalletIn, std::placeholders::_1));
conns.BlockConnected = g_signals.m_internals->BlockConnected.connect(std::bind(&CValidationInterface::BlockConnected, pwalletIn, std::placeholders::_1, std::placeholders::_2, std::placeholders::_3));
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, std::placeholders::_2));
conns.ChainTip = g_signals.m_internals->ChainTip.connect(std::bind(&CValidationInterface::ChainTip, pwalletIn, std::placeholders::_1, std::placeholders::_2, std::placeholders::_3));
conns.NotifyTransactionLock = g_signals.m_internals->NotifyTransactionLock.connect(std::bind(&CValidationInterface::NotifyTransactionLock, pwalletIn, std::placeholders::_1));
conns.UpdatedTransaction = g_signals.m_internals->UpdatedTransaction.connect(std::bind(&CValidationInterface::UpdatedTransaction, pwalletIn, std::placeholders::_1));
Expand Down Expand Up @@ -105,8 +105,8 @@ void CMainSignals::BlockConnected(const std::shared_ptr<const CBlock> &block, co
m_internals->BlockConnected(block, pindex, txnConflicted);
}

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

void CMainSignals::NotifyTransactionLock(const CTransaction& tx) {
Expand Down
4 changes: 2 additions & 2 deletions src/validationinterface.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ class CValidationInterface {
virtual void UpdatedBlockTip(const CBlockIndex *pindexNew, const CBlockIndex *pindexFork, bool fInitialDownload) {}
virtual void TransactionAddedToMempool(const CTransactionRef &ptxn) {}
virtual void BlockConnected(const std::shared_ptr<const CBlock> &block, const CBlockIndex *pindex, const std::vector<CTransactionRef> &txnConflicted) {}
virtual void BlockDisconnected(const std::shared_ptr<const CBlock> &block) {}
virtual void BlockDisconnected(const std::shared_ptr<const CBlock> &block, int nBlockHeight) {}
virtual void ChainTip(const CBlockIndex *pindex, const CBlock *pblock, Optional<SaplingMerkleTree> added) {}
virtual void NotifyTransactionLock(const CTransaction &tx) {}
/** Notifies listeners of the new active block chain on-disk. */
Expand Down Expand Up @@ -62,7 +62,7 @@ class CMainSignals {
void UpdatedBlockTip(const CBlockIndex *, const CBlockIndex *, bool fInitialDownload);
void TransactionAddedToMempool(const CTransactionRef &ptxn);
void BlockConnected(const std::shared_ptr<const CBlock> &block, const CBlockIndex *pindex, const std::vector<CTransactionRef> &txnConflicted);
void BlockDisconnected(const std::shared_ptr<const CBlock> &block);
void BlockDisconnected(const std::shared_ptr<const CBlock> &block, int nBlockHeight);
void NotifyTransactionLock(const CTransaction&);
void UpdatedTransaction(const uint256 &);
void SetBestChain(const CBlockLocator &);
Expand Down
30 changes: 26 additions & 4 deletions src/wallet/wallet.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1267,23 +1267,45 @@ void CWallet::BlockConnected(const std::shared_ptr<const CBlock>& pblock, const
// state of transactions in our wallet is currently cleared when we
// receive another notification and there is a race condition where
// notification of a connected conflict might cause an outside process
// to abandon a transaction and then have it inadvertantly cleared by
// to abandon a transaction and then have it inadvertently cleared by
// the notification that the conflicted transaction was evicted.

for (const CTransactionRef& ptx : vtxConflicted) {
SyncTransaction(ptx, NULL, -1);
SyncTransaction(ptx, nullptr, -1);
}
for (size_t i = 0; i < pblock->vtx.size(); i++) {
SyncTransaction(pblock->vtx[i], pindex, i);
}

// Sapling: notify about the connected block
// Get prev block tree anchor
CBlockIndex* pprev = pindex->pprev;
SaplingMerkleTree oldSaplingTree;
bool isSaplingActive = (pprev) != nullptr &&
Params().GetConsensus().NetworkUpgradeActive(pprev->nHeight,
Consensus::UPGRADE_V5_0);
if (isSaplingActive) {
assert(pcoinsTip->GetSaplingAnchorAt(pprev->hashFinalSaplingRoot, oldSaplingTree));
} else {
assert(pcoinsTip->GetSaplingAnchorAt(SaplingMerkleTree::empty_root(), oldSaplingTree));
}

// Sapling: Update cached incremental witnesses
ChainTipAdded(pindex, pblock.get(), oldSaplingTree);
}

void CWallet::BlockDisconnected(const std::shared_ptr<const CBlock>& pblock)
void CWallet::BlockDisconnected(const std::shared_ptr<const CBlock>& pblock, int nBlockHeight)
{
LOCK2(cs_main, cs_wallet);
for (const CTransactionRef& ptx : pblock->vtx) {
SyncTransaction(ptx, NULL, -1);
}

if (Params().GetConsensus().NetworkUpgradeActive(nBlockHeight, Consensus::UPGRADE_V5_0)) {
// Update Sapling cached incremental witnesses
m_sspk_man->DecrementNoteWitnesses(nBlockHeight);
m_sspk_man->UpdateSaplingNullifierNoteMapForBlock(pblock.get());
}
}

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

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

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

Expand Down
2 changes: 1 addition & 1 deletion src/wallet/wallet.h
Original file line number Diff line number Diff line change
Expand Up @@ -603,7 +603,7 @@ class CWallet : public CCryptoKeyStore, public CValidationInterface
bool LoadToWallet(const CWalletTx& wtxIn);
void TransactionAddedToMempool(const CTransactionRef& tx) override;
void BlockConnected(const std::shared_ptr<const CBlock>& pblock, const CBlockIndex *pindex, const std::vector<CTransactionRef>& vtxConflicted) override;
void BlockDisconnected(const std::shared_ptr<const CBlock>& pblock) override;
void BlockDisconnected(const std::shared_ptr<const CBlock>& pblock, int nBlockHeight) override;
bool AddToWalletIfInvolvingMe(const CTransactionRef& tx, const uint256& blockHash, int posInBlock, bool fUpdate);
void EraseFromWallet(const uint256& hash);

Expand Down
2 changes: 1 addition & 1 deletion src/zmq/zmqnotificationinterface.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ void CZMQNotificationInterface::BlockConnected(const std::shared_ptr<const CBloc
}
}

void CZMQNotificationInterface::BlockDisconnected(const std::shared_ptr<const CBlock>& pblock)
void CZMQNotificationInterface::BlockDisconnected(const std::shared_ptr<const CBlock>& pblock, int nBlockHeight)
{
for (const CTransactionRef& ptx : pblock->vtx) {
// Do a normal notify for each transaction removed in block disconnection
Expand Down
2 changes: 1 addition & 1 deletion src/zmq/zmqnotificationinterface.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ class CZMQNotificationInterface : public CValidationInterface
// CValidationInterface
void TransactionAddedToMempool(const CTransactionRef& tx) override;
void BlockConnected(const std::shared_ptr<const CBlock>& pblock, const CBlockIndex* pindexConnected, const std::vector<CTransactionRef>& vtxConflicted) override;
void BlockDisconnected(const std::shared_ptr<const CBlock>& pblock) override;
void BlockDisconnected(const std::shared_ptr<const CBlock>& pblock, int nBlockHeight) override;
void NotifyTransactionLock(const CTransaction &tx) override;
void UpdatedBlockTip(const CBlockIndex *pindexNew, const CBlockIndex *pindexFork, bool fInitialDownload) override;

Expand Down

0 comments on commit bcdd3e9

Please sign in to comment.