Skip to content

Commit

Permalink
Convert the last, non-trivial, serialization functions to the new form
Browse files Browse the repository at this point in the history
  • Loading branch information
furszy committed Jul 3, 2021
1 parent 8c74c09 commit 060d62b
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 81 deletions.
45 changes: 15 additions & 30 deletions src/evo/deterministicmns.h
Original file line number Diff line number Diff line change
Expand Up @@ -143,13 +143,10 @@ class CDeterministicMNStateDiff
#undef DMN_STATE_DIFF_LINE
}

ADD_SERIALIZE_METHODS;

template <typename Stream, typename Operation>
inline void SerializationOp(Stream& s, Operation ser_action)
SERIALIZE_METHODS(CDeterministicMNStateDiff, obj)
{
READWRITE(VARINT(fields));
#define DMN_STATE_DIFF_LINE(f) if (fields & Field_##f) READWRITE(state.f);
READWRITE(VARINT(obj.fields));
#define DMN_STATE_DIFF_LINE(f) if (obj.fields & Field_##f) READWRITE(obj.state.f);
DMN_STATE_DIFF_ALL_FIELDS
#undef DMN_STATE_DIFF_LINE
}
Expand Down Expand Up @@ -193,22 +190,15 @@ class CDeterministicMN
CDeterministicMNStateCPtr pdmnState;

public:
template <typename Stream, typename Operation>
inline void SerializationOp(Stream& s, Operation ser_action)
SERIALIZE_METHODS(CDeterministicMN, obj)
{
READWRITE(proTxHash);
READWRITE(VARINT(internalId));
READWRITE(collateralOutpoint);
READWRITE(nOperatorReward);
READWRITE(pdmnState);
READWRITE(obj.proTxHash);
READWRITE(VARINT(obj.internalId));
READWRITE(obj.collateralOutpoint);
READWRITE(obj.nOperatorReward);
READWRITE(obj.pdmnState);
}

template<typename Stream>
void Serialize(Stream& s) const { NCONST_PTR(this)->SerializationOp(s, CSerActionSerialize()); }

template<typename Stream>
void Unserialize(Stream& s) { SerializationOp(s, CSerActionUnserialize()); }

uint64_t GetInternalId() const;

std::string ToString() const;
Expand Down Expand Up @@ -280,18 +270,12 @@ class CDeterministicMNList
{
}

template <typename Stream, typename Operation>
inline void SerializationOpBase(Stream& s, Operation ser_action)
{
READWRITE(blockHash);
READWRITE(nHeight);
READWRITE(nTotalRegisteredCount);
}

template<typename Stream>
void Serialize(Stream& s) const
{
NCONST_PTR(this)->SerializationOpBase(s, CSerActionSerialize());
s << blockHash;
s << nHeight;
s << nTotalRegisteredCount;
// Serialize the map as a vector
WriteCompactSize(s, mnMap.size());
for (const auto& p : mnMap) {
Expand All @@ -305,8 +289,9 @@ class CDeterministicMNList
mnUniquePropertyMap = MnUniquePropertyMap();
mnInternalIdMap = MnInternalIdMap();

SerializationOpBase(s, CSerActionUnserialize());

s >> blockHash;
s >> nHeight;
s >> nTotalRegisteredCount;
size_t cnt = ReadCompactSize(s);
for (size_t i = 0; i < cnt; i++) {
AddMN(std::make_shared<CDeterministicMN>(deserialize, s), false);
Expand Down
72 changes: 31 additions & 41 deletions src/sapling/incrementalmerkletree.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,33 +23,35 @@ class MerklePath {
std::vector<std::vector<bool>> authentication_path;
std::vector<bool> index;

ADD_SERIALIZE_METHODS;

template <typename Stream, typename Operation>
inline void SerializationOp(Stream& s, Operation ser_action)
template<typename Stream>
void Serialize(Stream &s) const
{
std::vector<std::vector<unsigned char>> pathBytes;
uint64_t indexInt;
if (ser_action.ForRead()) {
READWRITE(pathBytes);
READWRITE(indexInt);
MerklePath &us = *(const_cast<MerklePath*>(this));
for (size_t i = 0; i < pathBytes.size(); i++) {
us.authentication_path.push_back(convertBytesVectorToVector(pathBytes[i]));
us.index.push_back((indexInt >> ((pathBytes.size() - 1) - i)) & 1);
}
} else {
assert(authentication_path.size() == index.size());
pathBytes.resize(authentication_path.size());
for (size_t i = 0; i < authentication_path.size(); i++) {
pathBytes[i].resize((authentication_path[i].size()+7)/8);
for (unsigned int p = 0; p < authentication_path[i].size(); p++) {
pathBytes[i][p / 8] |= authentication_path[i][p] << (7-(p % 8));
}
assert(authentication_path.size() == index.size());
pathBytes.resize(authentication_path.size());
for (size_t i = 0; i < authentication_path.size(); i++) {
pathBytes[i].resize((authentication_path[i].size()+7)/8);
for (unsigned int p = 0; p < authentication_path[i].size(); p++) {
pathBytes[i][p / 8] |= authentication_path[i][p] << (7-(p % 8));
}
indexInt = convertVectorToInt(index);
READWRITE(pathBytes);
READWRITE(indexInt);
}
indexInt = convertVectorToInt(index);
::Serialize(s, pathBytes);
::Serialize(s, indexInt);
}

template<typename Stream>
void Unserialize(Stream &s)
{
std::vector<std::vector<unsigned char>> pathBytes;
uint64_t indexInt;
::Unserialize(s, pathBytes);
::Unserialize(s, indexInt);
MerklePath &us = *(const_cast<MerklePath*>(this));
for (size_t i = 0; i < pathBytes.size(); i++) {
us.authentication_path.push_back(convertBytesVectorToVector(pathBytes[i]));
us.index.push_back((indexInt >> ((pathBytes.size() - 1) - i)) & 1);
}
}

Expand Down Expand Up @@ -110,16 +112,10 @@ friend class IncrementalWitness<Depth, Hash>;
return IncrementalWitness<Depth, Hash>(*this);
}

ADD_SERIALIZE_METHODS;

template <typename Stream, typename Operation>
inline void SerializationOp(Stream& s, Operation ser_action)
SERIALIZE_METHODS(IncrementalMerkleTree, obj)
{
READWRITE(left);
READWRITE(right);
READWRITE(parents);

wfcheck();
READWRITE(obj.left, obj.right, obj.parents);
obj.wfcheck();
}

static Hash empty_root() {
Expand Down Expand Up @@ -181,16 +177,10 @@ friend class IncrementalMerkleTree<Depth, Hash>;

void append(Hash obj);

ADD_SERIALIZE_METHODS;

template <typename Stream, typename Operation>
inline void SerializationOp(Stream& s, Operation ser_action)
SERIALIZE_METHODS(IncrementalWitness, obj)
{
READWRITE(tree);
READWRITE(filled);
READWRITE(cursor);

cursor_depth = tree.next_depth(filled.size());
READWRITE(obj.tree, obj.filled, obj.cursor);
SER_READ(obj, obj.cursor_depth = obj.tree.next_depth(obj.filled.size()));
}

template <size_t D, typename H>
Expand Down
11 changes: 1 addition & 10 deletions src/sapling/sapling_transaction.h
Original file line number Diff line number Diff line change
Expand Up @@ -117,16 +117,7 @@ class SaplingTxData
std::vector<OutputDescription> vShieldedOutput;
binding_sig_t bindingSig = {{0}};

ADD_SERIALIZE_METHODS;

template <typename Stream, typename Operation>
inline void SerializationOp(Stream& s, Operation ser_action)
{
READWRITE(*const_cast<CAmount*>(&valueBalance));
READWRITE(*const_cast<std::vector<SpendDescription>*>(&vShieldedSpend));
READWRITE(*const_cast<std::vector<OutputDescription>*>(&vShieldedOutput));
READWRITE(*const_cast<binding_sig_t*>(&bindingSig));
}
SERIALIZE_METHODS(SaplingTxData, obj) { READWRITE(obj.valueBalance, obj.vShieldedSpend, obj.vShieldedOutput, obj.bindingSig); }

explicit SaplingTxData() : valueBalance(0), vShieldedSpend(), vShieldedOutput() { }
explicit SaplingTxData(const SaplingTxData& from) : valueBalance(from.valueBalance), vShieldedSpend(from.vShieldedSpend), vShieldedOutput(from.vShieldedOutput), bindingSig(from.bindingSig) {}
Expand Down

0 comments on commit 060d62b

Please sign in to comment.