From 8925697f15f716da94c510c63b2fe7dac852cb0b Mon Sep 17 00:00:00 2001 From: Hector Chu Date: Sat, 24 Feb 2024 00:59:54 +0000 Subject: [PATCH] remove block height from netutxo & changed segment.leaves to vector --- src/libmw/include/mw/mmr/Segment.h | 6 ++--- src/libmw/include/mw/models/tx/UTXO.h | 1 - src/libmw/src/mmr/Segment.cpp | 2 +- src/libmw/test/tests/mmr/Test_Segment.cpp | 31 ++++++++++++++--------- src/net_processing.cpp | 4 +-- 5 files changed, 25 insertions(+), 19 deletions(-) diff --git a/src/libmw/include/mw/mmr/Segment.h b/src/libmw/include/mw/mmr/Segment.h index 1c7897a0b9..4ee1bf3925 100644 --- a/src/libmw/include/mw/mmr/Segment.h +++ b/src/libmw/include/mw/mmr/Segment.h @@ -2,7 +2,7 @@ #include #include -#include +#include #include // Forward Declarations @@ -17,8 +17,8 @@ MMR_NAMESPACE /// struct Segment { - // The hashes of the requested unspent leaves, in ascending order - std::vector leaves; + // The requested unspent leaves, in ascending order + std::vector leaves; // The MMR node hashes needed to verify the integrity of the MMR & the provided leaves std::vector hashes; diff --git a/src/libmw/include/mw/models/tx/UTXO.h b/src/libmw/include/mw/models/tx/UTXO.h index 3aa6728520..61f6e59a46 100644 --- a/src/libmw/include/mw/models/tx/UTXO.h +++ b/src/libmw/include/mw/models/tx/UTXO.h @@ -56,7 +56,6 @@ public: template inline void Serialize(Stream& s) const { - s << m_utxo->GetBlockHeight(); s << COMPACTSIZE(m_utxo->GetLeafIndex().Get()); if (m_format == FULL_UTXO) { diff --git a/src/libmw/src/mmr/Segment.cpp b/src/libmw/src/mmr/Segment.cpp index 998a0fba16..9fdbbdfcf7 100644 --- a/src/libmw/src/mmr/Segment.cpp +++ b/src/libmw/src/mmr/Segment.cpp @@ -19,7 +19,7 @@ Segment SegmentFactory::Assemble(const IMMR& mmr, const ILeafSet& leafset, const while (segment.leaves.size() < num_leaves && leaf_idx < leafset.GetNextLeafIdx()) { if (leafset.Contains(leaf_idx)) { last_leaf_idx = leaf_idx; - segment.leaves.push_back(mmr.GetLeaf(leaf_idx).vec()); + segment.leaves.push_back(mmr.GetLeaf(leaf_idx)); } ++leaf_idx; diff --git a/src/libmw/test/tests/mmr/Test_Segment.cpp b/src/libmw/test/tests/mmr/Test_Segment.cpp index 5eb91afc55..466b6afbe3 100644 --- a/src/libmw/test/tests/mmr/Test_Segment.cpp +++ b/src/libmw/test/tests/mmr/Test_Segment.cpp @@ -16,6 +16,12 @@ ostream& operator<<(ostream& os, const mw::Hash& hash) os << hash.ToHex(); return os; } + +ostream& operator<<(ostream& os, const mmr::Leaf& leaf) +{ + os << leaf.GetHash().ToHex(); + return os; +} } // namespace std using namespace mmr; @@ -27,7 +33,12 @@ struct MMRWithLeafset { static mmr::Leaf DeterministicLeaf(const uint64_t i) { - return mmr::Leaf::Create(mmr::LeafIndex::At(i), Hasher().Append(i).hash().vec()); + std::vector serialized{ + uint8_t(i >> 24), + uint8_t(i >> 16), + uint8_t(i >> 8), + uint8_t(i)}; + return mmr::Leaf::Create(mmr::LeafIndex::At(i), serialized); } static MMRWithLeafset BuildDetermininisticMMR(const uint64_t num_leaves) @@ -56,11 +67,11 @@ BOOST_AUTO_TEST_CASE(AssembleSegment) 4 ); - std::vector expected_leaves{ - Hasher().Append(0).hash(), - Hasher().Append(1).hash(), - Hasher().Append(2).hash(), - Hasher().Append(3).hash(), + std::vector expected_leaves{ + DeterministicLeaf(0), + DeterministicLeaf(1), + DeterministicLeaf(2), + DeterministicLeaf(3) }; BOOST_REQUIRE_EQUAL_COLLECTIONS(segment.leaves.begin(), segment.leaves.end(), expected_leaves.begin(), expected_leaves.end()); @@ -73,12 +84,8 @@ BOOST_AUTO_TEST_CASE(AssembleSegment) BOOST_REQUIRE_EQUAL(expected_lower_peak, segment.lower_peak); // Verify PMMR root can be fully recomputed - mw::Hash n0 = mmr::Leaf::CalcHash(mmr::LeafIndex::At(0), segment.leaves[0].vec()); - mw::Hash n1 = mmr::Leaf::CalcHash(mmr::LeafIndex::At(1), segment.leaves[1].vec()); - mw::Hash n2 = MMRUtil::CalcParentHash(mmr::Index::At(2), n0, n1); - mw::Hash n3 = mmr::Leaf::CalcHash(mmr::LeafIndex::At(2), segment.leaves[2].vec()); - mw::Hash n4 = mmr::Leaf::CalcHash(mmr::LeafIndex::At(3), segment.leaves[3].vec()); - mw::Hash n5 = MMRUtil::CalcParentHash(mmr::Index::At(5), n3, n4); + mw::Hash n2 = MMRUtil::CalcParentHash(mmr::Index::At(2), segment.leaves[0].GetHash(), segment.leaves[1].GetHash()); + mw::Hash n5 = MMRUtil::CalcParentHash(mmr::Index::At(5), segment.leaves[2].GetHash(), segment.leaves[3].GetHash()); mw::Hash n6 = MMRUtil::CalcParentHash(mmr::Index::At(6), n2, n5); mw::Hash n14 = MMRUtil::CalcParentHash(mmr::Index::At(14), n6, segment.hashes[0]); mw::Hash root = MMRUtil::CalcParentHash(Index::At(26), n14, *segment.lower_peak); diff --git a/src/net_processing.cpp b/src/net_processing.cpp index 546ad726f3..63e45aea03 100644 --- a/src/net_processing.cpp +++ b/src/net_processing.cpp @@ -1899,8 +1899,8 @@ static void ProcessGetMWEBUTXOs(CNode& pfrom, const ChainstateManager& chainman, std::vector utxos; utxos.reserve(segment.leaves.size()); - for (const mw::Hash& hash : segment.leaves) { - UTXO::CPtr utxo = mweb_cache->GetUTXO(hash); + for (const mmr::Leaf& leaf : segment.leaves) { + UTXO::CPtr utxo = mweb_cache->GetUTXO(leaf.vec()); if (!utxo) { LogPrint(BCLog::NET, "Could not build segment requested by getmwebutxos from peer=%d\n", pfrom.GetId()); pfrom.fDisconnect = true;