Skip to content

Commit

Permalink
Port serialize.h from Bitcoin
Browse files Browse the repository at this point in the history
Bitcoin moved its stream classes out from serialize.h into streams.h.
These changes update the application serialization and stream code to
match Bitcoin's newer implementation with the following exceptions:

- CSizeComputer retains nType for legacy code with type-dependent
  serialization (SER_NETWORK, SER_DISK, SER_GETHASH, ...)
- The retention of serialization implementations for std::tuple
  for the deprecated accounting API

The backport includes some new stream classes from Bitcoin not used in
Gridcoin. We may refactor code to use these new classes in the future.
  • Loading branch information
cyrossignol committed Sep 24, 2019
1 parent 0c3d9e7 commit 6a6389f
Show file tree
Hide file tree
Showing 27 changed files with 1,391 additions and 853 deletions.
22 changes: 10 additions & 12 deletions src/addrman.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,13 +45,16 @@ class CAddrInfo : public CAddress

public:

IMPLEMENT_SERIALIZE(
CAddress* pthis = (CAddress*)(this);
READWRITE(*pthis);
ADD_SERIALIZE_METHODS;

template <typename Stream, typename Operation>
inline void SerializationOp(Stream& s, Operation ser_action)
{
READWRITEAS(CAddress, *this);
READWRITE(source);
READWRITE(nLastSuccess);
READWRITE(nAttempts);
)
}

void Init()
{
Expand Down Expand Up @@ -265,10 +268,10 @@ class CAddrMan
// This format is more complex, but significantly smaller (at most 1.5 MiB), and supports
// changes to the ADDRMAN_ parameters without breaking the on-disk structure.
//
// We don't use IMPLEMENT_SERIALIZE since the serialization and deserialization code has
// We don't use ADD_SERIALIZE_METHODS since the serialization and deserialization code has
// very little in common.
template<typename Stream>
void Serialize(Stream &s, int nType, int nVersionDummy) const
void Serialize(Stream &s) const
{
LOCK(cs);

Expand Down Expand Up @@ -312,7 +315,7 @@ class CAddrMan
}

template<typename Stream>
void Unserialize(Stream& s, int nType, int nVersionDummy)
void Unserialize(Stream& s)
{
LOCK(cs);

Expand Down Expand Up @@ -376,11 +379,6 @@ class CAddrMan
}
}

unsigned int GetSerializeSize(int nType, int nVersion) const
{
return (CSizeComputer(nType, nVersion) << *this).size();
}

CAddrMan() : vRandom(0), vvTried(ADDRMAN_TRIED_BUCKET_COUNT, std::vector<int>(0)), vvNew(ADDRMAN_NEW_BUCKET_COUNT, std::set<int>())
{
nKey.resize(32);
Expand Down
23 changes: 15 additions & 8 deletions src/alert.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,14 @@ class CUnsignedAlert
std::string strStatusBar;
std::string strReserved;

IMPLEMENT_SERIALIZE
(
READWRITE(this->nVersion);
nVersion = this->nVersion;
ADD_SERIALIZE_METHODS;

template <typename Stream, typename Operation>
inline void SerializationOp(Stream& s, Operation ser_action)
{
READWRITE(nVersion);
s.SetVersion(nVersion);

READWRITE(nRelayUntil);
READWRITE(nExpiration);
READWRITE(nID);
Expand All @@ -56,7 +60,7 @@ class CUnsignedAlert
READWRITE(strComment);
READWRITE(strStatusBar);
READWRITE(strReserved);
)
}

void SetNull();

Expand All @@ -76,11 +80,14 @@ class CAlert : public CUnsignedAlert
SetNull();
}

IMPLEMENT_SERIALIZE
(
ADD_SERIALIZE_METHODS;

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

void SetNull();
bool IsNull() const;
Expand Down
13 changes: 4 additions & 9 deletions src/bignum.h
Original file line number Diff line number Diff line change
Expand Up @@ -411,22 +411,17 @@ class CBigNum : public CBigNumBase
return ToString(16);
}

unsigned int GetSerializeSize(int nType=0, int nVersion=PROTOCOL_VERSION) const
{
return ::GetSerializeSize(getvch(), nType, nVersion);
}

template<typename Stream>
void Serialize(Stream& s, int nType=0, int nVersion=PROTOCOL_VERSION) const
void Serialize(Stream& s) const
{
::Serialize(s, getvch(), nType, nVersion);
::Serialize(s, getvch());
}

template<typename Stream>
void Unserialize(Stream& s, int nType=0, int nVersion=PROTOCOL_VERSION)
void Unserialize(Stream& s)
{
std::vector<unsigned char> vch;
::Unserialize(s, vch, nType, nVersion);
::Unserialize(s, vch);
setvch(vch);
}

Expand Down
10 changes: 7 additions & 3 deletions src/crypter.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,14 +39,18 @@ class CMasterKey
// such as the various parameters to scrypt
std::vector<unsigned char> vchOtherDerivationParameters;

IMPLEMENT_SERIALIZE
(
ADD_SERIALIZE_METHODS;

template <typename Stream, typename Operation>
inline void SerializationOp(Stream& s, Operation ser_action)
{
READWRITE(vchCryptedKey);
READWRITE(vchSalt);
READWRITE(nDerivationMethod);
READWRITE(nDeriveIterations);
READWRITE(vchOtherDerivationParameters);
)
}

CMasterKey()
{
// 25000 rounds is just under 0.1 seconds on a 1.86 GHz Pentium M
Expand Down
16 changes: 8 additions & 8 deletions src/db.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,7 @@ void CDBEnv::CheckpointLSN(std::string strFile)
void CDBEnv::lsn_reset(const std::string& strFile)
{
dbenv.lsn_reset(strFile.c_str(),0);
}
}

CDB::CDB(const char *pszFile, const char* pszMode) :
pdb(NULL), activeTxn(NULL)
Expand Down Expand Up @@ -506,16 +506,16 @@ bool CAddrDB::Write(const CAddrMan& addr)

// serialize addresses, checksum data up to that point, then append csum
CDataStream ssPeers(SER_DISK, CLIENT_VERSION);
ssPeers << FLATDATA(pchMessageStart);
ssPeers << pchMessageStart;
ssPeers << addr;
uint256 hash = Hash(ssPeers.begin(), ssPeers.end());
ssPeers << hash;

// open temp output file, and associate with CAutoFile
boost::filesystem::path pathTmp = GetDataDir() / tmpfn;
FILE *file = fopen(pathTmp.string().c_str(), "wb");
CAutoFile fileout = CAutoFile(file, SER_DISK, CLIENT_VERSION);
if (!fileout)
CAutoFile fileout(file, SER_DISK, CLIENT_VERSION);
if (fileout.IsNull())
return error("CAddrman::Write() : open failed");

// Write and commit header, data
Expand All @@ -525,7 +525,7 @@ bool CAddrDB::Write(const CAddrMan& addr)
catch (std::exception &e) {
return error("CAddrman::Write() : I/O error");
}
FileCommit(fileout);
FileCommit(fileout.Get());
fileout.fclose();

// replace existing peers.dat, if any, with new peers.dat.XXXX
Expand All @@ -539,8 +539,8 @@ bool CAddrDB::Read(CAddrMan& addr)
{
// open input file, and associate with CAutoFile
FILE *file = fopen(pathAddr.string().c_str(), "rb");
CAutoFile filein = CAutoFile(file, SER_DISK, CLIENT_VERSION);
if (!filein)
CAutoFile filein(file, SER_DISK, CLIENT_VERSION);
if (filein.IsNull())
return error("CAddrman::Read() : open failed");

// use file size to size memory buffer
Expand Down Expand Up @@ -573,7 +573,7 @@ bool CAddrDB::Read(CAddrMan& addr)
unsigned char pchMsgTmp[4];
try {
// de-serialize file header (pchMessageStart magic number) and
ssPeers >> FLATDATA(pchMsgTmp);
ssPeers >> pchMsgTmp;

// verify the network matches ours
if (memcmp(pchMsgTmp, pchMessageStart, sizeof(pchMsgTmp)))
Expand Down
2 changes: 1 addition & 1 deletion src/hash.h
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ class CHashWriter
template<typename T>
CHashWriter& operator<<(const T& obj) {
// Serialize to this stream
::Serialize(*this, obj, nType, nVersion);
::Serialize(*this, obj);
return (*this);
}
};
Expand Down
10 changes: 7 additions & 3 deletions src/key.h
Original file line number Diff line number Diff line change
Expand Up @@ -73,9 +73,13 @@ class CPubKey {
friend bool operator!=(const CPubKey &a, const CPubKey &b) { return a.vchPubKey != b.vchPubKey; }
friend bool operator<(const CPubKey &a, const CPubKey &b) { return a.vchPubKey < b.vchPubKey; }

IMPLEMENT_SERIALIZE(
ADD_SERIALIZE_METHODS;

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

CKeyID GetID() const {
return CKeyID(Hash160(vchPubKey));
Expand Down Expand Up @@ -149,7 +153,7 @@ class CKey
// This is only slightly more CPU intensive than just verifying it.
// If this function succeeds, the recovered public key is guaranteed to be valid
// (the signature is a valid signature of the given data for that key)

// Ensure that signature is DER-encoded
static bool ReserealizeSignature(std::vector<unsigned char>& vchSig);

Expand Down
12 changes: 6 additions & 6 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -821,7 +821,7 @@ bool AddOrphanTx(const CTransaction& tx)
// 10,000 orphans, each of which is at most 5,000 bytes big is
// at most 500 megabytes of orphans:

size_t nSize = tx.GetSerializeSize(SER_NETWORK, CTransaction::CURRENT_VERSION);
size_t nSize = GetSerializeSize(tx, SER_NETWORK, CTransaction::CURRENT_VERSION);

if (nSize > 5000)
{
Expand Down Expand Up @@ -987,7 +987,7 @@ bool IsStandardTx(const CTransaction& tx)
// almost as much to process as they cost the sender in fees, because
// computing signature hashes is O(ninputs*txsize). Limiting transactions
// to MAX_STANDARD_TX_SIZE mitigates CPU exhaustion attacks.
unsigned int sz = tx.GetSerializeSize(SER_NETWORK, CTransaction::CURRENT_VERSION);
unsigned int sz = GetSerializeSize(tx, SER_NETWORK, CTransaction::CURRENT_VERSION);
if (sz >= MAX_STANDARD_TX_SIZE)
return false;

Expand Down Expand Up @@ -5333,12 +5333,12 @@ bool LoadExternalBlockFile(FILE* fileIn)
try {
CAutoFile blkdat(fileIn, SER_DISK, CLIENT_VERSION);
unsigned int nPos = 0;
while (nPos != (unsigned int)-1 && blkdat.good() && !fRequestShutdown)
while (nPos != (unsigned int)-1 && !fRequestShutdown)
{
unsigned char pchData[65536];
do {
fseek(blkdat, nPos, SEEK_SET);
int nRead = fread(pchData, 1, sizeof(pchData), blkdat);
fseek(blkdat.Get(), nPos, SEEK_SET);
int nRead = fread(pchData, 1, sizeof(pchData), blkdat.Get());
if (nRead <= 8)
{
nPos = (unsigned int)-1;
Expand All @@ -5359,7 +5359,7 @@ bool LoadExternalBlockFile(FILE* fileIn)
} while(!fRequestShutdown);
if (nPos == (unsigned int)-1)
break;
fseek(blkdat, nPos, SEEK_SET);
fseek(blkdat.Get(), nPos, SEEK_SET);
unsigned int nSize;
blkdat >> nSize;
if (nSize > 0 && nSize <= MAX_BLOCK_SIZE)
Expand Down
Loading

0 comments on commit 6a6389f

Please sign in to comment.