diff --git a/src/main.cpp b/src/main.cpp index 714d8625b..d8dc2ddf1 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -2486,8 +2486,12 @@ bool DisconnectBlock(CBlock& block, CValidationState& state, CBlockIndex* pindex // Delete from notarisations cache NotarisationsInBlock nibs; if (GetBlockNotarisations(block.GetHash(), nibs)) { - pnotarisations->Erase(block.GetHash()); - EraseBackNotarisations(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 @@ -2844,14 +2848,7 @@ bool ConnectBlock(const CBlock& block, CValidationState& state, CBlockIndex* pin vPos.push_back(std::make_pair(tx.GetHash(), pos)); pos.nTxOffset += ::GetSerializeSize(tx, SER_DISK, CLIENT_VERSION); } - - // Record Notarisations - NotarisationsInBlock notarisations = GetNotarisationsInBlock(block, pindex->nHeight); - if (notarisations.size() > 0) { - pnotarisations->Write(block.GetHash(), notarisations); - WriteBackNotarisations(notarisations); - } - + view.PushAnchor(tree); if (!fJustCheck) { pindex->hashAnchorEnd = tree.root(); @@ -2920,6 +2917,17 @@ bool ConnectBlock(const CBlock& block, CValidationState& state, CBlockIndex* pin pindex->RaiseValidity(BLOCK_VALID_SCRIPTS); setDirtyBlockIndex.insert(pindex); } + + // Record Notarisations + NotarisationsInBlock notarisations = GetNotarisationsInBlock(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()); + } if (fTxIndex) if (!pblocktree->WriteTxIndex(vPos)) diff --git a/src/notarisationdb.cpp b/src/notarisationdb.cpp index 023588874..4b2ade793 100644 --- a/src/notarisationdb.cpp +++ b/src/notarisationdb.cpp @@ -61,25 +61,24 @@ bool GetBackNotarisation(uint256 notarisationHash, Notarisation &n) /* * Write an index of KMD notarisation id -> backnotarisation */ -void WriteBackNotarisations(const NotarisationsInBlock notarisations) +void WriteBackNotarisations(const NotarisationsInBlock notarisations, CLevelDBBatch &batch) { - CLevelDBBatch batch; + int wrote = 0; BOOST_FOREACH(const Notarisation &n, notarisations) { - if (!n.second.txHash.IsNull()) + if (!n.second.txHash.IsNull()) { batch.Write(n.second.txHash, n); + wrote++; + } } - pnotarisations->WriteBatch(batch, true); } -void EraseBackNotarisations(const NotarisationsInBlock notarisations) +void EraseBackNotarisations(const NotarisationsInBlock notarisations, CLevelDBBatch &batch) { - CLevelDBBatch batch; BOOST_FOREACH(const Notarisation &n, notarisations) { if (!n.second.txHash.IsNull()) batch.Erase(n.second.txHash); } - pnotarisations->WriteBatch(batch, true); } diff --git a/src/notarisationdb.h b/src/notarisationdb.h index 27f979784..59c500fc4 100644 --- a/src/notarisationdb.h +++ b/src/notarisationdb.h @@ -21,7 +21,7 @@ typedef std::vector NotarisationsInBlock; NotarisationsInBlock GetNotarisationsInBlock(const CBlock &block, int nHeight); bool GetBlockNotarisations(uint256 blockHash, NotarisationsInBlock &nibs); bool GetBackNotarisation(uint256 notarisationHash, Notarisation &n); -void WriteBackNotarisations(const NotarisationsInBlock notarisations); -void EraseBackNotarisations(const NotarisationsInBlock notarisations); +void WriteBackNotarisations(const NotarisationsInBlock notarisations, CLevelDBBatch &batch); +void EraseBackNotarisations(const NotarisationsInBlock notarisations, CLevelDBBatch &batch); #endif /* NOTARISATIONDB_H */