From 84964112be9ff72f67692580302939b8f65b49f2 Mon Sep 17 00:00:00 2001 From: Scott Sadler Date: Sat, 16 Jun 2018 12:05:22 -0300 Subject: [PATCH] move disconnect notarisations from DisconnectBlock to DisconnectTip --- src/crosschain.cpp | 4 ++-- src/main.cpp | 54 ++++++++++++++++++++++++++++------------------ 2 files changed, 35 insertions(+), 23 deletions(-) diff --git a/src/crosschain.cpp b/src/crosschain.cpp index 10910a48a..ddceb1b9b 100644 --- a/src/crosschain.cpp +++ b/src/crosschain.cpp @@ -75,8 +75,8 @@ int ScanNotarisationsFromHeight(int nHeight, const IsTarget f, Notarisation &fou for (int h=nHeight; hphashBlock; - if (!GetBlockNotarisations(blockHash, notarisations)) + + if (!GetBlockNotarisations(*chainActive[h]->phashBlock, notarisations)) continue; BOOST_FOREACH(found, notarisations) { diff --git a/src/main.cpp b/src/main.cpp index b67b1c478..563608cbf 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -2349,6 +2349,37 @@ static bool ApplyTxInUndo(const CTxInUndo& undo, CCoinsViewCache& view, const CO return fClean; } + +void ConnectNotarisations(const CBlock &block, int height) +{ + // Record Notarisations + NotarisationsInBlock notarisations = ScanBlockNotarisations(block, height); + if (notarisations.size() > 0) { + CLevelDBBatch batch; + batch.Write(block.GetHash(), notarisations); + WriteBackNotarisations(notarisations, batch); + pnotarisations->WriteBatch(batch, true); + LogPrintf("ConnectBlock: wrote %i block notarisations in block: %s\n", + notarisations.size(), block.GetHash().GetHex().data()); + } +} + + +void DisconnectNotarisations(const CBlock &block) +{ + // Delete from notarisations cache + NotarisationsInBlock nibs; + if (GetBlockNotarisations(block.GetHash(), nibs)) { + CLevelDBBatch batch; + batch.Erase(block.GetHash()); + EraseBackNotarisations(nibs, batch); + pnotarisations->WriteBatch(batch, true); + LogPrintf("DisconnectTip: deleted %i block notarisations in block: %s\n", + nibs.size(), block.GetHash().GetHex().data()); + } +} + + bool DisconnectBlock(CBlock& block, CValidationState& state, CBlockIndex* pindex, CCoinsViewCache& view, bool* pfClean) { assert(pindex->GetBlockHash() == view.GetBestBlock()); @@ -2483,17 +2514,6 @@ bool DisconnectBlock(CBlock& block, CValidationState& state, CBlockIndex* pindex } } - // Delete from notarisations cache - NotarisationsInBlock nibs; - if (GetBlockNotarisations(block.GetHash(), nibs)) { - CLevelDBBatch batch; - batch.Erase(block.GetHash()); - EraseBackNotarisations(nibs, batch); - pnotarisations->WriteBatch(batch, true); - LogPrintf("ConnectBlock: Deleted %i block notarisations in block: %s\n", - nibs.size(), block.GetHash().GetHex().data()); - } - // set the old best anchor back view.PopAnchor(blockUndo.old_tree_root); @@ -2918,16 +2938,7 @@ bool ConnectBlock(const CBlock& block, CValidationState& state, CBlockIndex* pin setDirtyBlockIndex.insert(pindex); } - // Record Notarisations - NotarisationsInBlock notarisations = ScanBlockNotarisations(block, pindex->nHeight); - if (notarisations.size() > 0) { - CLevelDBBatch batch; - batch.Write(block.GetHash(), notarisations); - WriteBackNotarisations(notarisations, batch); - pnotarisations->WriteBatch(batch, true); - LogPrintf("ConnectBlock: wrote %i block notarisations in block: %s\n", - notarisations.size(), block.GetHash().GetHex().data()); - } + ConnectNotarisations(block, pindex->nHeight); if (fTxIndex) if (!pblocktree->WriteTxIndex(vPos)) @@ -3165,6 +3176,7 @@ bool static DisconnectTip(CValidationState &state, bool fBare = false) { if (!DisconnectBlock(block, state, pindexDelete, view)) return error("DisconnectTip(): DisconnectBlock %s failed", pindexDelete->GetBlockHash().ToString()); assert(view.Flush()); + DisconnectNotarisations(block); } LogPrint("bench", "- Disconnect block: %.2fms\n", (GetTimeMicros() - nStart) * 0.001); uint256 anchorAfterDisconnect = pcoinsTip->GetBestAnchor();