Skip to content

Commit

Permalink
Convert merkleblock to new serialization
Browse files Browse the repository at this point in the history
  • Loading branch information
sipa authored and furszy committed Jul 3, 2021
1 parent 13577fb commit 3d3ee64
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 27 deletions.
18 changes: 18 additions & 0 deletions src/merkleblock.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,24 @@
#include "utilstrencodings.h"


std::vector<unsigned char> BitsToBytes(const std::vector<bool>& bits)
{
std::vector<unsigned char> ret((bits.size() + 7) / 8);
for (unsigned int p = 0; p < bits.size(); p++) {
ret[p / 8] |= bits[p] << (p % 8);
}
return ret;
}

std::vector<bool> BytesToBits(const std::vector<unsigned char>& bytes)
{
std::vector<bool> ret(bytes.size() * 8);
for (unsigned int p = 0; p < ret.size(); p++) {
ret[p] = (bytes[p / 8] & (1 << (p % 8))) != 0;
}
return ret;
}

CMerkleBlock::CMerkleBlock(const CBlock& block, CBloomFilter& filter)
{
header = block.GetBlockHeader();
Expand Down
39 changes: 12 additions & 27 deletions src/merkleblock.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@

#include <vector>

// Helper functions for serialization.
std::vector<unsigned char> BitsToBytes(const std::vector<bool>& bits);
std::vector<bool> BytesToBits(const std::vector<unsigned char>& bytes);

/** Data structure that represents a partial merkle tree.
*
* It represents a subset of the txid's of a known block, in a way that
Expand Down Expand Up @@ -82,28 +86,15 @@ class CPartialMerkleTree
uint256 TraverseAndExtract(int height, unsigned int pos, unsigned int& nBitsUsed, unsigned int& nHashUsed, std::vector<uint256>& vMatch);

public:
/** serialization implementation */
ADD_SERIALIZE_METHODS;

template <typename Stream, typename Operation>
inline void SerializationOp(Stream& s, Operation ser_action)
SERIALIZE_METHODS(CPartialMerkleTree, obj)
{
READWRITE(nTransactions);
READWRITE(vHash);
std::vector<unsigned char> vBytes;
if (ser_action.ForRead()) {
READWRITE(vBytes);
CPartialMerkleTree& us = *(const_cast<CPartialMerkleTree*>(this));
us.vBits.resize(vBytes.size() * 8);
for (unsigned int p = 0; p < us.vBits.size(); p++)
us.vBits[p] = (vBytes[p / 8] & (1 << (p % 8))) != 0;
us.fBad = false;
} else {
vBytes.resize((vBits.size() + 7) / 8);
for (unsigned int p = 0; p < vBits.size(); p++)
vBytes[p / 8] |= vBits[p] << (p % 8);
READWRITE(vBytes);
}
READWRITE(obj.nTransactions, obj.vHash);
std::vector<unsigned char> bytes;
SER_WRITE(obj, bytes = BitsToBytes(obj.vBits));
READWRITE(bytes);
SER_READ(obj, obj.vBits = BytesToBits(bytes));
SER_READ(obj, obj.fBad = false);
}

/** Construct a partial merkle tree from a list of transaction id's, and a mask that selects a subset of them */
Expand Down Expand Up @@ -143,14 +134,8 @@ class CMerkleBlock

CMerkleBlock() {}

ADD_SERIALIZE_METHODS;
SERIALIZE_METHODS(CMerkleBlock, obj) { READWRITE(obj.header, obj.txn); }

template <typename Stream, typename Operation>
inline void SerializationOp(Stream& s, Operation ser_action)
{
READWRITE(header);
READWRITE(txn);
}
};

#endif // BITCOIN_MERKLEBLOCK_H

0 comments on commit 3d3ee64

Please sign in to comment.