Skip to content

Commit

Permalink
Check pending anchors in same ordrer as top anchor selection (#275)
Browse files Browse the repository at this point in the history
Co-authored-by: Ahmed Hilali <[email protected]>
  • Loading branch information
Bushstar and monstrobishi authored Mar 9, 2021
1 parent 3aca3d7 commit f240849
Showing 1 changed file with 34 additions and 9 deletions.
43 changes: 34 additions & 9 deletions src/masternodes/anchors.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -526,9 +526,26 @@ void CAnchorIndex::CheckPendingAnchors()
{
AssertLockHeld(cs_main);

std::set<uint256> deletePending;
// Get pending anchors and organise by Bitcoin height, anchor height or TX hash
auto ByBtcHeight = [](const AnchorRec& a, const AnchorRec& b) {
if (a.btcHeight == b.btcHeight) {
if (a.anchor.height == b.anchor.height) {
return a.txHash < b.txHash;
}

// Higher DeFi height wins
return a.anchor.height > b.anchor.height;
}

return a.btcHeight < b.btcHeight;
};
std::set<AnchorRec, decltype(ByBtcHeight)> anchorsPending(ByBtcHeight);
ForEachPending([&anchorsPending](uint256 const &, AnchorRec & rec) {
anchorsPending.insert(rec);
});

ForEachPending([this, &deletePending](uint256 const &, AnchorRec & rec) {
std::set<uint256> deletePending;
for (const auto& rec : anchorsPending) {

CBlockIndex anchorBlock;
uint64_t anchorCreationHeight;
Expand All @@ -539,13 +556,21 @@ void CAnchorIndex::CheckPendingAnchors()
deletePending.insert(rec.txHash);
}

return;
continue;
}

// Here we can check new rule that Bitcoin blocktime is three hours more than DeFi anchored block
uint32_t timestamp = spv::pspv->ReadTxTimestamp(rec.txHash);
if (timestamp == 0 || anchorBlock.nTime > timestamp - Params().GetConsensus().mn.anchoringTimeDepth) {
return; // Do not delete, TX time may still be updated on SPV TX status update.

// Do not delete, TX time still pending.
if (timestamp == 0 || timestamp == std::numeric_limits<int32_t>::max()) {
continue;
}

// Here we can check new rule that Bitcoin blocktime is three hours more than DeFi anchored block
if (anchorBlock.nTime > timestamp - Params().GetConsensus().mn.anchoringTimeDepth) {
LogPrint(BCLog::ANCHORING, "DeFi anchor time not deep enough. DeFi: %d Bitcoin: %d\n", anchorBlock.nTime, timestamp);
deletePending.insert(rec.txHash);
continue;
}

// Let's try and get the team that set this anchor
Expand All @@ -554,15 +579,15 @@ void CAnchorIndex::CheckPendingAnchors()
if (!anchorTeam || anchorTeam->empty()) {
LogPrint(BCLog::ANCHORING, "No team found at height %ld. Deleting anchor txHash %s\n", anchorCreationHeight, rec.txHash.ToString());
deletePending.insert(rec.txHash);
return;
continue;
}

// Validate the anchor sigs
CPubKey pubKey;
if (!rec.anchor.CheckAuthSigs(*anchorTeam)) {
LogPrint(BCLog::ANCHORING, "Signature validation fails. Deleting anchor txHash %s\n", rec.txHash.ToString());
deletePending.insert(rec.txHash);
return;
continue;
}

// Finally add to anchors and delete from pending
Expand All @@ -573,7 +598,7 @@ void CAnchorIndex::CheckPendingAnchors()
// Recheck anchors
possibleReActivation = true;
CheckActiveAnchor();
});
}

for (const auto& hash : deletePending) {
DeletePendingByBtcTx(hash);
Expand Down

0 comments on commit f240849

Please sign in to comment.