From ec77e3d48055d71f716d3e111199f510505e1e05 Mon Sep 17 00:00:00 2001 From: Peter Bushnell Date: Mon, 12 Apr 2021 14:04:56 +0100 Subject: [PATCH] Make PendingOrder free function and use it in BestOfTwo --- src/masternodes/anchors.cpp | 70 ++++++++++++++++--------------------- src/masternodes/anchors.h | 12 ++++--- src/test/anchor_tests.cpp | 32 ++++++++--------- 3 files changed, 54 insertions(+), 60 deletions(-) diff --git a/src/masternodes/anchors.cpp b/src/masternodes/anchors.cpp index fd26497f5bf..22f82970e45 100644 --- a/src/masternodes/anchors.cpp +++ b/src/masternodes/anchors.cpp @@ -355,30 +355,7 @@ void CAnchorAuthIndex::PruneOlderThan(THeight height) } CAnchorIndex::CAnchorIndex(size_t nCacheSize, bool fMemory, bool fWipe) - : db(new CDBWrapper(GetDataDir() / "anchors", nCacheSize, fMemory, fWipe)), - PendingOrder([](const CAnchorIndex::AnchorRec& a, const CAnchorIndex::AnchorRec& b) - { - if (a.btcHeight == b.btcHeight) - { - if (a.anchor.height == b.anchor.height) - { - if (a.anchor.height >= static_cast(Params().GetConsensus().EunosHeight)) - { - const auto blockHash = panchors->ReadBlockHash(a.btcHeight); - auto aHash = Hash(a.txHash.begin(), a.txHash.end(), blockHash.begin(), blockHash.end()); - auto bHash = Hash(b.txHash.begin(), b.txHash.end(), blockHash.begin(), blockHash.end()); - return aHash < bHash; - } - - return a.txHash < b.txHash; - } - - // Higher DeFi comes first - return a.anchor.height > b.anchor.height; - } - - return a.btcHeight < b.btcHeight; - }) + : db(new CDBWrapper(GetDataDir() / "anchors", nCacheSize, fMemory, fWipe)) { } @@ -550,7 +527,7 @@ void CAnchorIndex::CheckPendingAnchors() { AssertLockHeld(cs_main); - CAnchorIndex::PendingSet anchorsPending(panchors->PendingOrder); + spv::PendingSet anchorsPending(spv::PendingOrder); ForEachPending([&anchorsPending](uint256 const &, AnchorRec & rec) { anchorsPending.insert(rec); }); @@ -677,21 +654,7 @@ CAnchorIndex::AnchorRec const * BestOfTwo(CAnchorIndex::AnchorRec const * a1, CA if (a2 == nullptr) return a1; - if (a1->anchor.height > a2->anchor.height) { - return a1; - } else if (a1->anchor.height < a2->anchor.height) { - return a2; - } else if (a1->anchor.height >= static_cast(Params().GetConsensus().EunosHeight)) { - const auto blockHash = panchors->ReadBlockHash(a1->btcHeight); - auto a1Hash = Hash(a1->txHash.begin(), a1->txHash.end(), blockHash.begin(), blockHash.end()); - auto a2Hash = Hash(a2->txHash.begin(), a2->txHash.end(), blockHash.begin(), blockHash.end()); - if (a1Hash < a2Hash) { - return a1; - } - } else if (a1->txHash < a2->txHash) { - return a1; - } - return a2; + return spv::PendingOrder(*a1, *a2) ? a1 : a2; } /// @returns true if top active anchor has been changed @@ -1197,3 +1160,30 @@ bool GetAnchorEmbeddedData(const CKeyID& data, uint64_t& anchorCreationHeight, s return true; } + +namespace spv +{ +const PendingOrderType PendingOrder = PendingOrderType([](const CAnchorIndex::AnchorRec& a, const CAnchorIndex::AnchorRec& b) +{ + if (a.btcHeight == b.btcHeight) + { + if (a.anchor.height == b.anchor.height) + { + if (a.anchor.height >= static_cast(Params().GetConsensus().EunosHeight)) + { + const auto blockHash = panchors->ReadBlockHash(a.btcHeight); + auto aHash = Hash(a.txHash.begin(), a.txHash.end(), blockHash.begin(), blockHash.end()); + auto bHash = Hash(b.txHash.begin(), b.txHash.end(), blockHash.begin(), blockHash.end()); + return aHash < bHash; + } + + return a.txHash < b.txHash; + } + + // Higher DeFi comes first + return a.anchor.height > b.anchor.height; + } + + return a.btcHeight < b.btcHeight; +}); +} diff --git a/src/masternodes/anchors.h b/src/masternodes/anchors.h index dfa69c4e532..f2313341d49 100644 --- a/src/masternodes/anchors.h +++ b/src/masternodes/anchors.h @@ -221,10 +221,6 @@ class CAnchorIndex > > AnchorIndexImpl; - // Define comparator and set to hold pending anchors - std::function PendingOrder; - using PendingSet = std::set; - CAnchorIndex(size_t nCacheSize, bool fMemory = false, bool fWipe = false); bool Load(); @@ -517,4 +513,12 @@ extern std::unique_ptr panchorauths; extern std::unique_ptr panchors; extern std::unique_ptr panchorAwaitingConfirms; +namespace spv +{ +// Define comparator and set to hold pending anchors +using PendingOrderType = std::function; +using PendingSet = std::set; +extern const PendingOrderType PendingOrder; +} + #endif // DEFI_MASTERNODES_ANCHORS_H diff --git a/src/test/anchor_tests.cpp b/src/test/anchor_tests.cpp index 331f3309999..8d1152a82bc 100644 --- a/src/test/anchor_tests.cpp +++ b/src/test/anchor_tests.cpp @@ -33,16 +33,16 @@ BOOST_AUTO_TEST_CASE(anchor_order_logic) anchorTwo.btcHeight = 200; // Lowest Bitcoin height wins - BOOST_CHECK(panchors->PendingOrder(anchorOne, anchorTwo) == true); - BOOST_CHECK(panchors->PendingOrder(anchorTwo, anchorOne) == false); + BOOST_CHECK(spv::PendingOrder(anchorOne, anchorTwo) == true); + BOOST_CHECK(spv::PendingOrder(anchorTwo, anchorOne) == false); anchorOne.btcHeight = anchorTwo.btcHeight; anchorOne.anchor.height = 100; anchorTwo.anchor.height = 200; // Heighest DeFi height wins - BOOST_CHECK(panchors->PendingOrder(anchorOne, anchorTwo) == false); - BOOST_CHECK(panchors->PendingOrder(anchorTwo, anchorOne) == true); + BOOST_CHECK(spv::PendingOrder(anchorOne, anchorTwo) == false); + BOOST_CHECK(spv::PendingOrder(anchorTwo, anchorOne) == true); BOOST_CHECK(BestOfTwo(&anchorOne, &anchorTwo)->anchor.height == 200); BOOST_CHECK(BestOfTwo(&anchorTwo, &anchorOne)->anchor.height == 200); @@ -51,8 +51,8 @@ BOOST_AUTO_TEST_CASE(anchor_order_logic) anchorTwo.txHash = uint256S("852bb89808af5a5487d4afed23b4ec3c4186ec8101ff9e7c73a038c9a2c436d9"); // Lowest hash wins - BOOST_CHECK(panchors->PendingOrder(anchorOne, anchorTwo) == true); - BOOST_CHECK(panchors->PendingOrder(anchorTwo, anchorOne) == false); + BOOST_CHECK(spv::PendingOrder(anchorOne, anchorTwo) == true); + BOOST_CHECK(spv::PendingOrder(anchorTwo, anchorOne) == false); BOOST_CHECK(BestOfTwo(&anchorOne, &anchorTwo)->txHash == uint256S("12ca5ac2b666478bbbdfc0e0b328552a8cd83aa1b3fbb822560ab8cbf72be893")); BOOST_CHECK(BestOfTwo(&anchorTwo, &anchorOne)->txHash == uint256S("12ca5ac2b666478bbbdfc0e0b328552a8cd83aa1b3fbb822560ab8cbf72be893")); @@ -63,40 +63,40 @@ BOOST_AUTO_TEST_CASE(anchor_order_logic) anchorOne.txHash = uint256S("12ca5ac2b666478bbbdfc0e0b328552a8cd83aa1b3fbb822560ab8cbf72be893"); // 5cfe6594dad4efe238e5c7903ba5afa4c3f92ee81282a43e7ba5919f4cebd210 anchorTwo.txHash = uint256S("852bb89808af5a5487d4afed23b4ec3c4186ec8101ff9e7c73a038c9a2c436d9"); // 1af2609c24bcbe59af8ffb921129454e12f7aef07da3c3c0fead97711469045a - BOOST_CHECK(panchors->PendingOrder(anchorOne, anchorTwo) == true); - BOOST_CHECK(panchors->PendingOrder(anchorTwo, anchorOne) == false); + BOOST_CHECK(spv::PendingOrder(anchorOne, anchorTwo) == true); + BOOST_CHECK(spv::PendingOrder(anchorTwo, anchorOne) == false); BOOST_CHECK(BestOfTwo(&anchorOne, &anchorTwo)->txHash == uint256S("12ca5ac2b666478bbbdfc0e0b328552a8cd83aa1b3fbb822560ab8cbf72be893")); BOOST_CHECK(BestOfTwo(&anchorTwo, &anchorOne)->txHash == uint256S("12ca5ac2b666478bbbdfc0e0b328552a8cd83aa1b3fbb822560ab8cbf72be893")); anchorOne.txHash = uint256S("e48106cf7254b73be5d550f2054495b32c4e98f2c2c251697c267ab0a6cb87cf"); // ff8c5aa31428aa787513d1e3451914ed7f8a1b6174e3a572dc5f2a449201240d anchorTwo.txHash = uint256S("a5c974e6eca14593bdfd53eaf49c777e4615342370e79705d96b5afd2a016278"); // a06399a5ed47c65452f71174174e9c2696dd3fae83a9d0e89796e195c01b670d - BOOST_CHECK(panchors->PendingOrder(anchorOne, anchorTwo) == true); - BOOST_CHECK(panchors->PendingOrder(anchorTwo, anchorOne) == false); + BOOST_CHECK(spv::PendingOrder(anchorOne, anchorTwo) == true); + BOOST_CHECK(spv::PendingOrder(anchorTwo, anchorOne) == false); BOOST_CHECK(BestOfTwo(&anchorOne, &anchorTwo)->txHash == uint256S("e48106cf7254b73be5d550f2054495b32c4e98f2c2c251697c267ab0a6cb87cf")); BOOST_CHECK(BestOfTwo(&anchorTwo, &anchorOne)->txHash == uint256S("e48106cf7254b73be5d550f2054495b32c4e98f2c2c251697c267ab0a6cb87cf")); anchorOne.txHash = uint256S("7398ddf9bdabb2c1271b918d3f516fd4573bbead448b4e8a611b7ffd5451777b"); // 9a15e4a213dcd75e035141012e3636c4a46f15ef2ba13ff52357f6121d46901b anchorTwo.txHash = uint256S("b2f2ed1fc0b6192b9398b0aef2e79e57d4a473c3e9b2be45e556f7c85e269cbc"); // 699b691491d27aad44fe58d897af97a0e631e6ad27f83408a0d64d933639fd03 - BOOST_CHECK(panchors->PendingOrder(anchorOne, anchorTwo) == false); - BOOST_CHECK(panchors->PendingOrder(anchorTwo, anchorOne) == true); + BOOST_CHECK(spv::PendingOrder(anchorOne, anchorTwo) == false); + BOOST_CHECK(spv::PendingOrder(anchorTwo, anchorOne) == true); BOOST_CHECK(BestOfTwo(&anchorOne, &anchorTwo)->txHash == uint256S("b2f2ed1fc0b6192b9398b0aef2e79e57d4a473c3e9b2be45e556f7c85e269cbc")); BOOST_CHECK(BestOfTwo(&anchorTwo, &anchorOne)->txHash == uint256S("b2f2ed1fc0b6192b9398b0aef2e79e57d4a473c3e9b2be45e556f7c85e269cbc")); anchorOne.txHash = uint256S("3264bb76dc2cdff731733fa33dd530b0058da45606af9824b49b61e1f5ac9d9d"); // f4b83366e8d5650ec7714962b3c6619d737ae43f8b6641b35b4e39ab9605b88f anchorTwo.txHash = uint256S("851d8697118d6688b6552cb142a95f461b45e61b9accafa1ef3386b1be0cc2bb"); // c9f467a9e6233f9614d88111b333336a29be9f6d54ac7171fce0f5f58eed04e9 - BOOST_CHECK(panchors->PendingOrder(anchorOne, anchorTwo) == true); - BOOST_CHECK(panchors->PendingOrder(anchorTwo, anchorOne) == false); + BOOST_CHECK(spv::PendingOrder(anchorOne, anchorTwo) == true); + BOOST_CHECK(spv::PendingOrder(anchorTwo, anchorOne) == false); BOOST_CHECK(BestOfTwo(&anchorOne, &anchorTwo)->txHash == uint256S("3264bb76dc2cdff731733fa33dd530b0058da45606af9824b49b61e1f5ac9d9d")); BOOST_CHECK(BestOfTwo(&anchorTwo, &anchorOne)->txHash == uint256S("3264bb76dc2cdff731733fa33dd530b0058da45606af9824b49b61e1f5ac9d9d")); anchorOne.txHash = uint256S("87c638cfe4efa94d8e259978c55a85de101cafaac68c9f6c03b3dc0335016b55"); // af431c6193434919a996fb87aba8790c65f49116f489a7a72e08af02276804ca anchorTwo.txHash = uint256S("390a8b3b581e75e13e8eec4fc7fe0b35a382e9fba29d9b42c547e1b6c6785a51"); // 1ecf49be7a49c081245b6df1ba9f7a0463f7da3a33c505145c963be8f741b086 - BOOST_CHECK(panchors->PendingOrder(anchorOne, anchorTwo) == false); - BOOST_CHECK(panchors->PendingOrder(anchorTwo, anchorOne) == true); + BOOST_CHECK(spv::PendingOrder(anchorOne, anchorTwo) == false); + BOOST_CHECK(spv::PendingOrder(anchorTwo, anchorOne) == true); BOOST_CHECK(BestOfTwo(&anchorOne, &anchorTwo)->txHash == uint256S("390a8b3b581e75e13e8eec4fc7fe0b35a382e9fba29d9b42c547e1b6c6785a51")); BOOST_CHECK(BestOfTwo(&anchorTwo, &anchorOne)->txHash == uint256S("390a8b3b581e75e13e8eec4fc7fe0b35a382e9fba29d9b42c547e1b6c6785a51")); }