remove block height from netutxo & changed segment.leaves to vector<Leaf>

pull/944/head
Hector Chu 9 months ago committed by Loshan T
parent 4c3d4f278a
commit 8925697f15

@ -2,7 +2,7 @@
#include <mw/common/Macros.h>
#include <mw/models/crypto/Hash.h>
#include <mw/mmr/LeafIndex.h>
#include <mw/mmr/Leaf.h>
#include <set>
// Forward Declarations
@ -17,8 +17,8 @@ MMR_NAMESPACE
/// </summary>
struct Segment
{
// The hashes of the requested unspent leaves, in ascending order
std::vector<mw::Hash> leaves;
// The requested unspent leaves, in ascending order
std::vector<mmr::Leaf> leaves;
// The MMR node hashes needed to verify the integrity of the MMR & the provided leaves
std::vector<mw::Hash> hashes;

@ -56,7 +56,6 @@ public:
template <typename Stream>
inline void Serialize(Stream& s) const
{
s << m_utxo->GetBlockHeight();
s << COMPACTSIZE(m_utxo->GetLeafIndex().Get());
if (m_format == FULL_UTXO) {

@ -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;

@ -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<uint8_t> 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<mw::Hash> expected_leaves{
Hasher().Append<uint64_t>(0).hash(),
Hasher().Append<uint64_t>(1).hash(),
Hasher().Append<uint64_t>(2).hash(),
Hasher().Append<uint64_t>(3).hash(),
std::vector<mmr::Leaf> 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);

@ -1899,8 +1899,8 @@ static void ProcessGetMWEBUTXOs(CNode& pfrom, const ChainstateManager& chainman,
std::vector<NetUTXO> 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;

Loading…
Cancel
Save