mirror of
https://github.com/bitcoin/bitcoin.git
synced 2025-02-22 06:52:36 +01:00
Convert merkleblock to new serialization
This commit is contained in:
parent
d06fedd1bc
commit
73747afbbe
2 changed files with 31 additions and 28 deletions
|
@ -9,6 +9,24 @@
|
||||||
#include <consensus/consensus.h>
|
#include <consensus/consensus.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, const std::set<uint256>* txids)
|
CMerkleBlock::CMerkleBlock(const CBlock& block, CBloomFilter* filter, const std::set<uint256>* txids)
|
||||||
{
|
{
|
||||||
header = block.GetBlockHeader();
|
header = block.GetBlockHeader();
|
||||||
|
|
|
@ -13,6 +13,10 @@
|
||||||
|
|
||||||
#include <vector>
|
#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.
|
/** Data structure that represents a partial merkle tree.
|
||||||
*
|
*
|
||||||
* It represents a subset of the txid's of a known block, in a way that
|
* It represents a subset of the txid's of a known block, in a way that
|
||||||
|
@ -81,27 +85,14 @@ protected:
|
||||||
|
|
||||||
public:
|
public:
|
||||||
|
|
||||||
/** serialization implementation */
|
SERIALIZE_METHODS(CPartialMerkleTree, obj)
|
||||||
ADD_SERIALIZE_METHODS;
|
{
|
||||||
|
READWRITE(obj.nTransactions, obj.vHash);
|
||||||
template <typename Stream, typename Operation>
|
std::vector<unsigned char> bytes;
|
||||||
inline void SerializationOp(Stream& s, Operation ser_action) {
|
SER_WRITE(obj, bytes = BitsToBytes(obj.vBits));
|
||||||
READWRITE(nTransactions);
|
READWRITE(bytes);
|
||||||
READWRITE(vHash);
|
SER_READ(obj, obj.vBits = BytesToBits(bytes));
|
||||||
std::vector<unsigned char> vBytes;
|
SER_READ(obj, obj.fBad = false);
|
||||||
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);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Construct a partial merkle tree from a list of transaction ids, and a mask that selects a subset of them */
|
/** Construct a partial merkle tree from a list of transaction ids, and a mask that selects a subset of them */
|
||||||
|
@ -157,13 +148,7 @@ public:
|
||||||
|
|
||||||
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);
|
|
||||||
}
|
|
||||||
|
|
||||||
private:
|
private:
|
||||||
// Combined constructor to consolidate code
|
// Combined constructor to consolidate code
|
||||||
|
|
Loading…
Add table
Reference in a new issue