diff --git a/src/avalanche/processor.h b/src/avalanche/processor.h --- a/src/avalanche/processor.h +++ b/src/avalanche/processor.h @@ -61,35 +61,22 @@ Finalized, }; -class BlockUpdate { - union { - CBlockIndex *pindex; - uintptr_t raw; - }; - - static const size_t STATUS_BITS = 2; - static const uintptr_t MASK = (1 << STATUS_BITS) - 1; - - static_assert( - alignof(CBlockIndex) >= (1 << STATUS_BITS), - "CBlockIndex alignement doesn't allow for Status to be stored."); +template class VoteItemUpdate { + VoteItem item; + VoteStatus status; public: - BlockUpdate(CBlockIndex *pindexIn, VoteStatus statusIn) : pindex(pindexIn) { - raw |= static_cast(statusIn); - } + VoteItemUpdate(const VoteItem itemIn, VoteStatus statusIn) + : item(itemIn), status(statusIn) {} - VoteStatus getStatus() const { return VoteStatus(raw & MASK); } + const VoteStatus &getStatus() const { return status; } - CBlockIndex *getBlockIndex() { - return reinterpret_cast(raw & ~MASK); - } - - const CBlockIndex *getBlockIndex() const { - return const_cast(this)->getBlockIndex(); - } + VoteItem getVoteItem() { return item; } + const VoteItem getVoteItem() const { return item; } }; +using BlockUpdate = VoteItemUpdate; + using BlockVoteMap = std::map; using ProofVoteMap = std::map, VoteRecord, diff --git a/src/avalanche/test/processor_tests.cpp b/src/avalanche/test/processor_tests.cpp --- a/src/avalanche/test/processor_tests.cpp +++ b/src/avalanche/test/processor_tests.cpp @@ -303,7 +303,7 @@ BlockUpdate abu(pindex, s); // The use of BOOST_CHECK instead of BOOST_CHECK_EQUAL prevents from // having to define operator<<() for each argument type. - BOOST_CHECK(abu.getBlockIndex() == pindex); + BOOST_CHECK(abu.getVoteItem() == pindex); BOOST_CHECK(abu.getStatus() == s); } } @@ -425,7 +425,7 @@ // Now finalize the decision. registerNewVote(next(resp)); BOOST_CHECK_EQUAL(updates.size(), 1); - BOOST_CHECK(updates[0].getBlockIndex() == pindex); + BOOST_CHECK(updates[0].getVoteItem() == pindex); BOOST_CHECK(updates[0].getStatus() == VoteStatus::Finalized); updates = {}; @@ -451,7 +451,7 @@ registerNewVote(next(resp)); BOOST_CHECK(!m_processor->isAccepted(pindex)); BOOST_CHECK_EQUAL(updates.size(), 1); - BOOST_CHECK(updates[0].getBlockIndex() == pindex); + BOOST_CHECK(updates[0].getVoteItem() == pindex); BOOST_CHECK(updates[0].getStatus() == VoteStatus::Rejected); updates = {}; @@ -472,7 +472,7 @@ registerNewVote(next(resp)); BOOST_CHECK(!m_processor->isAccepted(pindex)); BOOST_CHECK_EQUAL(updates.size(), 1); - BOOST_CHECK(updates[0].getBlockIndex() == pindex); + BOOST_CHECK(updates[0].getVoteItem() == pindex); BOOST_CHECK(updates[0].getStatus() == VoteStatus::Invalid); updates = {}; @@ -560,7 +560,7 @@ // Next vote will finalize block A. BOOST_CHECK(registerVotes(firstNodeid, next(resp), updates)); BOOST_CHECK_EQUAL(updates.size(), 1); - BOOST_CHECK(updates[0].getBlockIndex() == pindexA); + BOOST_CHECK(updates[0].getVoteItem() == pindexA); BOOST_CHECK(updates[0].getStatus() == VoteStatus::Finalized); updates = {}; @@ -573,7 +573,7 @@ // Next vote will finalize block B. BOOST_CHECK(registerVotes(secondNodeid, resp, updates)); BOOST_CHECK_EQUAL(updates.size(), 1); - BOOST_CHECK(updates[0].getBlockIndex() == pindexB); + BOOST_CHECK(updates[0].getVoteItem() == pindexB); BOOST_CHECK(updates[0].getStatus() == VoteStatus::Finalized); updates = {}; diff --git a/src/net_processing.cpp b/src/net_processing.cpp --- a/src/net_processing.cpp +++ b/src/net_processing.cpp @@ -4392,7 +4392,7 @@ if (updates.size()) { for (avalanche::BlockUpdate &u : updates) { - CBlockIndex *pindex = u.getBlockIndex(); + CBlockIndex *pindex = u.getVoteItem(); switch (u.getStatus()) { case avalanche::VoteStatus::Invalid: case avalanche::VoteStatus::Rejected: {