Skip to content

Commit

Permalink
Implement RemoveConfirmedInstantSendLocks to prune confirmed IS locks…
Browse files Browse the repository at this point in the history
… from DB
  • Loading branch information
codablock committed Apr 5, 2019
1 parent d6e7758 commit 4577438
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 2 deletions.
46 changes: 44 additions & 2 deletions src/llmq/quorums_instantsend.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,13 @@ void CInstantSendDb::WriteNewInstantSendLock(const uint256& hash, const CInstant
}

void CInstantSendDb::RemoveInstantSendLock(const uint256& hash, CInstantSendLockPtr islock)
{
CDBBatch batch(db);
RemoveInstantSendLock(batch, hash, islock);
db.WriteBatch(batch);
}

void CInstantSendDb::RemoveInstantSendLock(CDBBatch& batch, const uint256& hash, CInstantSendLockPtr islock)
{
if (!islock) {
islock = GetInstantSendLockByHash(hash);
Expand All @@ -70,13 +77,11 @@ void CInstantSendDb::RemoveInstantSendLock(const uint256& hash, CInstantSendLock
}
}

CDBBatch batch(db);
batch.Erase(std::make_tuple(std::string("is_i"), hash));
batch.Erase(std::make_tuple(std::string("is_tx"), islock->txid));
for (auto& in : islock->inputs) {
batch.Erase(std::make_tuple(std::string("is_in"), in));
}
db.WriteBatch(batch);

islockCache.erase(hash);
txidCache.erase(islock->txid);
Expand All @@ -95,6 +100,43 @@ void CInstantSendDb::RemoveInstantSendLockMined(const uint256& hash, int nHeight
db.Erase(std::make_tuple(std::string("is_m"), std::numeric_limits<int>::max() - nHeight, hash));
}

std::unordered_map<uint256, CInstantSendLockPtr> CInstantSendDb::RemoveConfirmedInstantSendLocks(int nUntilHeight)
{
auto it = std::unique_ptr<CDBIterator>(db.NewIterator());

auto firstKey = std::make_tuple(std::string("is_m"), std::numeric_limits<int>::max() - nUntilHeight, uint256());

it->Seek(firstKey);

CDBBatch deleteBatch(db);
std::unordered_map<uint256, CInstantSendLockPtr> ret;
while (it->Valid()) {
decltype(firstKey) curKey;
if (!it->GetKey(curKey) || std::get<0>(curKey) != "is_m") {
break;
}
int nHeight = std::numeric_limits<int>::max() - std::get<1>(curKey);
if (nHeight > nUntilHeight) {
break;
}

auto& islockHash = std::get<2>(curKey);
auto islock = GetInstantSendLockByHash(islockHash);
if (islock) {
RemoveInstantSendLock(deleteBatch, islockHash, islock);
ret.emplace(islockHash, islock);
}

deleteBatch.Erase(curKey);

it->Next();
}

db.WriteBatch(deleteBatch);

return ret;
}

CInstantSendLockPtr CInstantSendDb::GetInstantSendLockByHash(const uint256& hash)
{
CInstantSendLockPtr ret;
Expand Down
2 changes: 2 additions & 0 deletions src/llmq/quorums_instantsend.h
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,11 @@ class CInstantSendDb

void WriteNewInstantSendLock(const uint256& hash, const CInstantSendLock& islock);
void RemoveInstantSendLock(const uint256& hash, CInstantSendLockPtr islock);
void RemoveInstantSendLock(CDBBatch& batch, const uint256& hash, CInstantSendLockPtr islock);

void WriteInstantSendLockMined(const uint256& hash, int nHeight);
void RemoveInstantSendLockMined(const uint256& hash, int nHeight);
std::unordered_map<uint256, CInstantSendLockPtr> RemoveConfirmedInstantSendLocks(int nUntilHeight);

CInstantSendLockPtr GetInstantSendLockByHash(const uint256& hash);
uint256 GetInstantSendLockHashByTxid(const uint256& txid);
Expand Down

0 comments on commit 4577438

Please sign in to comment.