Merge #9039: Various serialization simplifcations and optimizations
d59a518
Use fixed preallocation instead of costly GetSerializeSize (Pieter Wuille)25a211a
Add optimized CSizeComputer serializers (Pieter Wuille)a2929a2
Make CSerAction's ForRead() constexpr (Pieter Wuille)a603925
Avoid -Wshadow errors (Pieter Wuille)5284721
Get rid of nType and nVersion (Pieter Wuille)657e05a
Make GetSerializeSize a wrapper on top of CSizeComputer (Pieter Wuille)fad9b66
Make nType and nVersion private and sometimes const (Pieter Wuille)c2c5d42
Make streams' read and write return void (Pieter Wuille)50e8a9c
Remove unused ReadVersion and WriteVersion (Pieter Wuille)
This commit is contained in:
commit
e81df49644
39 changed files with 409 additions and 543 deletions
|
@ -46,7 +46,7 @@ public:
|
||||||
ADD_SERIALIZE_METHODS;
|
ADD_SERIALIZE_METHODS;
|
||||||
|
|
||||||
template <typename Stream, typename Operation>
|
template <typename Stream, typename Operation>
|
||||||
inline void SerializationOp(Stream& s, Operation ser_action, int nType, int nVersion) {
|
inline void SerializationOp(Stream& s, Operation ser_action) {
|
||||||
READWRITE(this->nVersion);
|
READWRITE(this->nVersion);
|
||||||
READWRITE(nCreateTime);
|
READWRITE(nCreateTime);
|
||||||
READWRITE(nBanUntil);
|
READWRITE(nBanUntil);
|
||||||
|
|
|
@ -58,7 +58,7 @@ public:
|
||||||
ADD_SERIALIZE_METHODS;
|
ADD_SERIALIZE_METHODS;
|
||||||
|
|
||||||
template <typename Stream, typename Operation>
|
template <typename Stream, typename Operation>
|
||||||
inline void SerializationOp(Stream& s, Operation ser_action, int nType, int nVersion) {
|
inline void SerializationOp(Stream& s, Operation ser_action) {
|
||||||
READWRITE(*(CAddress*)this);
|
READWRITE(*(CAddress*)this);
|
||||||
READWRITE(source);
|
READWRITE(source);
|
||||||
READWRITE(nLastSuccess);
|
READWRITE(nLastSuccess);
|
||||||
|
@ -293,7 +293,7 @@ public:
|
||||||
* very little in common.
|
* very little in common.
|
||||||
*/
|
*/
|
||||||
template<typename Stream>
|
template<typename Stream>
|
||||||
void Serialize(Stream &s, int nType, int nVersionDummy) const
|
void Serialize(Stream &s) const
|
||||||
{
|
{
|
||||||
LOCK(cs);
|
LOCK(cs);
|
||||||
|
|
||||||
|
@ -343,7 +343,7 @@ public:
|
||||||
}
|
}
|
||||||
|
|
||||||
template<typename Stream>
|
template<typename Stream>
|
||||||
void Unserialize(Stream& s, int nType, int nVersionDummy)
|
void Unserialize(Stream& s)
|
||||||
{
|
{
|
||||||
LOCK(cs);
|
LOCK(cs);
|
||||||
|
|
||||||
|
@ -448,11 +448,6 @@ public:
|
||||||
Check();
|
Check();
|
||||||
}
|
}
|
||||||
|
|
||||||
unsigned int GetSerializeSize(int nType, int nVersion) const
|
|
||||||
{
|
|
||||||
return (CSizeComputer(nType, nVersion) << *this).size();
|
|
||||||
}
|
|
||||||
|
|
||||||
void Clear()
|
void Clear()
|
||||||
{
|
{
|
||||||
std::vector<int>().swap(vRandom);
|
std::vector<int>().swap(vRandom);
|
||||||
|
|
|
@ -64,7 +64,7 @@ public:
|
||||||
ADD_SERIALIZE_METHODS;
|
ADD_SERIALIZE_METHODS;
|
||||||
|
|
||||||
template <typename Stream, typename Operation>
|
template <typename Stream, typename Operation>
|
||||||
inline void SerializationOp(Stream& s, Operation ser_action, int nType, int nVersion) {
|
inline void SerializationOp(Stream& s, Operation ser_action) {
|
||||||
READWRITE(nSatoshisPerK);
|
READWRITE(nSatoshisPerK);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
|
@ -131,7 +131,7 @@ ReadStatus PartiallyDownloadedBlock::InitData(const CBlockHeaderAndShortTxIDs& c
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
LogPrint("cmpctblock", "Initialized PartiallyDownloadedBlock for block %s using a cmpctblock of size %lu\n", cmpctblock.header.GetHash().ToString(), cmpctblock.GetSerializeSize(SER_NETWORK, PROTOCOL_VERSION));
|
LogPrint("cmpctblock", "Initialized PartiallyDownloadedBlock for block %s using a cmpctblock of size %lu\n", cmpctblock.header.GetHash().ToString(), GetSerializeSize(cmpctblock, SER_NETWORK, PROTOCOL_VERSION));
|
||||||
|
|
||||||
return READ_STATUS_OK;
|
return READ_STATUS_OK;
|
||||||
}
|
}
|
||||||
|
|
|
@ -21,7 +21,7 @@ public:
|
||||||
ADD_SERIALIZE_METHODS;
|
ADD_SERIALIZE_METHODS;
|
||||||
|
|
||||||
template <typename Stream, typename Operation>
|
template <typename Stream, typename Operation>
|
||||||
inline void SerializationOp(Stream& s, Operation ser_action, int nType, int nVersion) {
|
inline void SerializationOp(Stream& s, Operation ser_action) {
|
||||||
READWRITE(tx); //TODO: Compress tx encoding
|
READWRITE(tx); //TODO: Compress tx encoding
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
@ -35,7 +35,7 @@ public:
|
||||||
ADD_SERIALIZE_METHODS;
|
ADD_SERIALIZE_METHODS;
|
||||||
|
|
||||||
template <typename Stream, typename Operation>
|
template <typename Stream, typename Operation>
|
||||||
inline void SerializationOp(Stream& s, Operation ser_action, int nType, int nVersion) {
|
inline void SerializationOp(Stream& s, Operation ser_action) {
|
||||||
READWRITE(blockhash);
|
READWRITE(blockhash);
|
||||||
uint64_t indexes_size = (uint64_t)indexes.size();
|
uint64_t indexes_size = (uint64_t)indexes.size();
|
||||||
READWRITE(COMPACTSIZE(indexes_size));
|
READWRITE(COMPACTSIZE(indexes_size));
|
||||||
|
@ -81,7 +81,7 @@ public:
|
||||||
ADD_SERIALIZE_METHODS;
|
ADD_SERIALIZE_METHODS;
|
||||||
|
|
||||||
template <typename Stream, typename Operation>
|
template <typename Stream, typename Operation>
|
||||||
inline void SerializationOp(Stream& s, Operation ser_action, int nType, int nVersion) {
|
inline void SerializationOp(Stream& s, Operation ser_action) {
|
||||||
READWRITE(blockhash);
|
READWRITE(blockhash);
|
||||||
uint64_t txn_size = (uint64_t)txn.size();
|
uint64_t txn_size = (uint64_t)txn.size();
|
||||||
READWRITE(COMPACTSIZE(txn_size));
|
READWRITE(COMPACTSIZE(txn_size));
|
||||||
|
@ -109,7 +109,7 @@ struct PrefilledTransaction {
|
||||||
ADD_SERIALIZE_METHODS;
|
ADD_SERIALIZE_METHODS;
|
||||||
|
|
||||||
template <typename Stream, typename Operation>
|
template <typename Stream, typename Operation>
|
||||||
inline void SerializationOp(Stream& s, Operation ser_action, int nType, int nVersion) {
|
inline void SerializationOp(Stream& s, Operation ser_action) {
|
||||||
uint64_t idx = index;
|
uint64_t idx = index;
|
||||||
READWRITE(COMPACTSIZE(idx));
|
READWRITE(COMPACTSIZE(idx));
|
||||||
if (idx > std::numeric_limits<uint16_t>::max())
|
if (idx > std::numeric_limits<uint16_t>::max())
|
||||||
|
@ -157,7 +157,7 @@ public:
|
||||||
ADD_SERIALIZE_METHODS;
|
ADD_SERIALIZE_METHODS;
|
||||||
|
|
||||||
template <typename Stream, typename Operation>
|
template <typename Stream, typename Operation>
|
||||||
inline void SerializationOp(Stream& s, Operation ser_action, int nType, int nVersion) {
|
inline void SerializationOp(Stream& s, Operation ser_action) {
|
||||||
READWRITE(header);
|
READWRITE(header);
|
||||||
READWRITE(nonce);
|
READWRITE(nonce);
|
||||||
|
|
||||||
|
|
|
@ -73,7 +73,7 @@ public:
|
||||||
ADD_SERIALIZE_METHODS;
|
ADD_SERIALIZE_METHODS;
|
||||||
|
|
||||||
template <typename Stream, typename Operation>
|
template <typename Stream, typename Operation>
|
||||||
inline void SerializationOp(Stream& s, Operation ser_action, int nType, int nVersion) {
|
inline void SerializationOp(Stream& s, Operation ser_action) {
|
||||||
READWRITE(vData);
|
READWRITE(vData);
|
||||||
READWRITE(nHashFuncs);
|
READWRITE(nHashFuncs);
|
||||||
READWRITE(nTweak);
|
READWRITE(nTweak);
|
||||||
|
|
|
@ -28,7 +28,7 @@ public:
|
||||||
ADD_SERIALIZE_METHODS;
|
ADD_SERIALIZE_METHODS;
|
||||||
|
|
||||||
template <typename Stream, typename Operation>
|
template <typename Stream, typename Operation>
|
||||||
inline void SerializationOp(Stream& s, Operation ser_action, int nType, int nVersion) {
|
inline void SerializationOp(Stream& s, Operation ser_action) {
|
||||||
READWRITE(VARINT(nBlocks));
|
READWRITE(VARINT(nBlocks));
|
||||||
READWRITE(VARINT(nSize));
|
READWRITE(VARINT(nSize));
|
||||||
READWRITE(VARINT(nUndoSize));
|
READWRITE(VARINT(nUndoSize));
|
||||||
|
@ -76,7 +76,7 @@ struct CDiskBlockPos
|
||||||
ADD_SERIALIZE_METHODS;
|
ADD_SERIALIZE_METHODS;
|
||||||
|
|
||||||
template <typename Stream, typename Operation>
|
template <typename Stream, typename Operation>
|
||||||
inline void SerializationOp(Stream& s, Operation ser_action, int nType, int nVersion) {
|
inline void SerializationOp(Stream& s, Operation ser_action) {
|
||||||
READWRITE(VARINT(nFile));
|
READWRITE(VARINT(nFile));
|
||||||
READWRITE(VARINT(nPos));
|
READWRITE(VARINT(nPos));
|
||||||
}
|
}
|
||||||
|
@ -357,8 +357,9 @@ public:
|
||||||
ADD_SERIALIZE_METHODS;
|
ADD_SERIALIZE_METHODS;
|
||||||
|
|
||||||
template <typename Stream, typename Operation>
|
template <typename Stream, typename Operation>
|
||||||
inline void SerializationOp(Stream& s, Operation ser_action, int nType, int nVersion) {
|
inline void SerializationOp(Stream& s, Operation ser_action) {
|
||||||
if (!(nType & SER_GETHASH))
|
int nVersion = s.GetVersion();
|
||||||
|
if (!(s.GetType() & SER_GETHASH))
|
||||||
READWRITE(VARINT(nVersion));
|
READWRITE(VARINT(nVersion));
|
||||||
|
|
||||||
READWRITE(VARINT(nHeight));
|
READWRITE(VARINT(nHeight));
|
||||||
|
|
47
src/coins.h
47
src/coins.h
|
@ -153,31 +153,8 @@ public:
|
||||||
return fCoinBase;
|
return fCoinBase;
|
||||||
}
|
}
|
||||||
|
|
||||||
unsigned int GetSerializeSize(int nType, int nVersion) const {
|
|
||||||
unsigned int nSize = 0;
|
|
||||||
unsigned int nMaskSize = 0, nMaskCode = 0;
|
|
||||||
CalcMaskSize(nMaskSize, nMaskCode);
|
|
||||||
bool fFirst = vout.size() > 0 && !vout[0].IsNull();
|
|
||||||
bool fSecond = vout.size() > 1 && !vout[1].IsNull();
|
|
||||||
assert(fFirst || fSecond || nMaskCode);
|
|
||||||
unsigned int nCode = 8*(nMaskCode - (fFirst || fSecond ? 0 : 1)) + (fCoinBase ? 1 : 0) + (fFirst ? 2 : 0) + (fSecond ? 4 : 0);
|
|
||||||
// version
|
|
||||||
nSize += ::GetSerializeSize(VARINT(this->nVersion), nType, nVersion);
|
|
||||||
// size of header code
|
|
||||||
nSize += ::GetSerializeSize(VARINT(nCode), nType, nVersion);
|
|
||||||
// spentness bitmask
|
|
||||||
nSize += nMaskSize;
|
|
||||||
// txouts themself
|
|
||||||
for (unsigned int i = 0; i < vout.size(); i++)
|
|
||||||
if (!vout[i].IsNull())
|
|
||||||
nSize += ::GetSerializeSize(CTxOutCompressor(REF(vout[i])), nType, nVersion);
|
|
||||||
// height
|
|
||||||
nSize += ::GetSerializeSize(VARINT(nHeight), nType, nVersion);
|
|
||||||
return nSize;
|
|
||||||
}
|
|
||||||
|
|
||||||
template<typename Stream>
|
template<typename Stream>
|
||||||
void Serialize(Stream &s, int nType, int nVersion) const {
|
void Serialize(Stream &s) const {
|
||||||
unsigned int nMaskSize = 0, nMaskCode = 0;
|
unsigned int nMaskSize = 0, nMaskCode = 0;
|
||||||
CalcMaskSize(nMaskSize, nMaskCode);
|
CalcMaskSize(nMaskSize, nMaskCode);
|
||||||
bool fFirst = vout.size() > 0 && !vout[0].IsNull();
|
bool fFirst = vout.size() > 0 && !vout[0].IsNull();
|
||||||
|
@ -185,33 +162,33 @@ public:
|
||||||
assert(fFirst || fSecond || nMaskCode);
|
assert(fFirst || fSecond || nMaskCode);
|
||||||
unsigned int nCode = 8*(nMaskCode - (fFirst || fSecond ? 0 : 1)) + (fCoinBase ? 1 : 0) + (fFirst ? 2 : 0) + (fSecond ? 4 : 0);
|
unsigned int nCode = 8*(nMaskCode - (fFirst || fSecond ? 0 : 1)) + (fCoinBase ? 1 : 0) + (fFirst ? 2 : 0) + (fSecond ? 4 : 0);
|
||||||
// version
|
// version
|
||||||
::Serialize(s, VARINT(this->nVersion), nType, nVersion);
|
::Serialize(s, VARINT(this->nVersion));
|
||||||
// header code
|
// header code
|
||||||
::Serialize(s, VARINT(nCode), nType, nVersion);
|
::Serialize(s, VARINT(nCode));
|
||||||
// spentness bitmask
|
// spentness bitmask
|
||||||
for (unsigned int b = 0; b<nMaskSize; b++) {
|
for (unsigned int b = 0; b<nMaskSize; b++) {
|
||||||
unsigned char chAvail = 0;
|
unsigned char chAvail = 0;
|
||||||
for (unsigned int i = 0; i < 8 && 2+b*8+i < vout.size(); i++)
|
for (unsigned int i = 0; i < 8 && 2+b*8+i < vout.size(); i++)
|
||||||
if (!vout[2+b*8+i].IsNull())
|
if (!vout[2+b*8+i].IsNull())
|
||||||
chAvail |= (1 << i);
|
chAvail |= (1 << i);
|
||||||
::Serialize(s, chAvail, nType, nVersion);
|
::Serialize(s, chAvail);
|
||||||
}
|
}
|
||||||
// txouts themself
|
// txouts themself
|
||||||
for (unsigned int i = 0; i < vout.size(); i++) {
|
for (unsigned int i = 0; i < vout.size(); i++) {
|
||||||
if (!vout[i].IsNull())
|
if (!vout[i].IsNull())
|
||||||
::Serialize(s, CTxOutCompressor(REF(vout[i])), nType, nVersion);
|
::Serialize(s, CTxOutCompressor(REF(vout[i])));
|
||||||
}
|
}
|
||||||
// coinbase height
|
// coinbase height
|
||||||
::Serialize(s, VARINT(nHeight), nType, nVersion);
|
::Serialize(s, VARINT(nHeight));
|
||||||
}
|
}
|
||||||
|
|
||||||
template<typename Stream>
|
template<typename Stream>
|
||||||
void Unserialize(Stream &s, int nType, int nVersion) {
|
void Unserialize(Stream &s) {
|
||||||
unsigned int nCode = 0;
|
unsigned int nCode = 0;
|
||||||
// version
|
// version
|
||||||
::Unserialize(s, VARINT(this->nVersion), nType, nVersion);
|
::Unserialize(s, VARINT(this->nVersion));
|
||||||
// header code
|
// header code
|
||||||
::Unserialize(s, VARINT(nCode), nType, nVersion);
|
::Unserialize(s, VARINT(nCode));
|
||||||
fCoinBase = nCode & 1;
|
fCoinBase = nCode & 1;
|
||||||
std::vector<bool> vAvail(2, false);
|
std::vector<bool> vAvail(2, false);
|
||||||
vAvail[0] = (nCode & 2) != 0;
|
vAvail[0] = (nCode & 2) != 0;
|
||||||
|
@ -220,7 +197,7 @@ public:
|
||||||
// spentness bitmask
|
// spentness bitmask
|
||||||
while (nMaskCode > 0) {
|
while (nMaskCode > 0) {
|
||||||
unsigned char chAvail = 0;
|
unsigned char chAvail = 0;
|
||||||
::Unserialize(s, chAvail, nType, nVersion);
|
::Unserialize(s, chAvail);
|
||||||
for (unsigned int p = 0; p < 8; p++) {
|
for (unsigned int p = 0; p < 8; p++) {
|
||||||
bool f = (chAvail & (1 << p)) != 0;
|
bool f = (chAvail & (1 << p)) != 0;
|
||||||
vAvail.push_back(f);
|
vAvail.push_back(f);
|
||||||
|
@ -232,10 +209,10 @@ public:
|
||||||
vout.assign(vAvail.size(), CTxOut());
|
vout.assign(vAvail.size(), CTxOut());
|
||||||
for (unsigned int i = 0; i < vAvail.size(); i++) {
|
for (unsigned int i = 0; i < vAvail.size(); i++) {
|
||||||
if (vAvail[i])
|
if (vAvail[i])
|
||||||
::Unserialize(s, REF(CTxOutCompressor(vout[i])), nType, nVersion);
|
::Unserialize(s, REF(CTxOutCompressor(vout[i])));
|
||||||
}
|
}
|
||||||
// coinbase height
|
// coinbase height
|
||||||
::Unserialize(s, VARINT(nHeight), nType, nVersion);
|
::Unserialize(s, VARINT(nHeight));
|
||||||
Cleanup();
|
Cleanup();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -55,16 +55,8 @@ protected:
|
||||||
public:
|
public:
|
||||||
CScriptCompressor(CScript &scriptIn) : script(scriptIn) { }
|
CScriptCompressor(CScript &scriptIn) : script(scriptIn) { }
|
||||||
|
|
||||||
unsigned int GetSerializeSize(int nType, int nVersion) const {
|
|
||||||
std::vector<unsigned char> compr;
|
|
||||||
if (Compress(compr))
|
|
||||||
return compr.size();
|
|
||||||
unsigned int nSize = script.size() + nSpecialScripts;
|
|
||||||
return script.size() + VARINT(nSize).GetSerializeSize(nType, nVersion);
|
|
||||||
}
|
|
||||||
|
|
||||||
template<typename Stream>
|
template<typename Stream>
|
||||||
void Serialize(Stream &s, int nType, int nVersion) const {
|
void Serialize(Stream &s) const {
|
||||||
std::vector<unsigned char> compr;
|
std::vector<unsigned char> compr;
|
||||||
if (Compress(compr)) {
|
if (Compress(compr)) {
|
||||||
s << CFlatData(compr);
|
s << CFlatData(compr);
|
||||||
|
@ -76,7 +68,7 @@ public:
|
||||||
}
|
}
|
||||||
|
|
||||||
template<typename Stream>
|
template<typename Stream>
|
||||||
void Unserialize(Stream &s, int nType, int nVersion) {
|
void Unserialize(Stream &s) {
|
||||||
unsigned int nSize = 0;
|
unsigned int nSize = 0;
|
||||||
s >> VARINT(nSize);
|
s >> VARINT(nSize);
|
||||||
if (nSize < nSpecialScripts) {
|
if (nSize < nSpecialScripts) {
|
||||||
|
@ -112,7 +104,7 @@ public:
|
||||||
ADD_SERIALIZE_METHODS;
|
ADD_SERIALIZE_METHODS;
|
||||||
|
|
||||||
template <typename Stream, typename Operation>
|
template <typename Stream, typename Operation>
|
||||||
inline void SerializationOp(Stream& s, Operation ser_action, int nType, int nVersion) {
|
inline void SerializationOp(Stream& s, Operation ser_action) {
|
||||||
if (!ser_action.ForRead()) {
|
if (!ser_action.ForRead()) {
|
||||||
uint64_t nVal = CompressAmount(txout.nValue);
|
uint64_t nVal = CompressAmount(txout.nValue);
|
||||||
READWRITE(VARINT(nVal));
|
READWRITE(VARINT(nVal));
|
||||||
|
|
|
@ -17,6 +17,9 @@
|
||||||
#include <leveldb/db.h>
|
#include <leveldb/db.h>
|
||||||
#include <leveldb/write_batch.h>
|
#include <leveldb/write_batch.h>
|
||||||
|
|
||||||
|
static const size_t DBWRAPPER_PREALLOC_KEY_SIZE = 64;
|
||||||
|
static const size_t DBWRAPPER_PREALLOC_VALUE_SIZE = 1024;
|
||||||
|
|
||||||
class dbwrapper_error : public std::runtime_error
|
class dbwrapper_error : public std::runtime_error
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
|
@ -60,12 +63,12 @@ public:
|
||||||
void Write(const K& key, const V& value)
|
void Write(const K& key, const V& value)
|
||||||
{
|
{
|
||||||
CDataStream ssKey(SER_DISK, CLIENT_VERSION);
|
CDataStream ssKey(SER_DISK, CLIENT_VERSION);
|
||||||
ssKey.reserve(ssKey.GetSerializeSize(key));
|
ssKey.reserve(DBWRAPPER_PREALLOC_KEY_SIZE);
|
||||||
ssKey << key;
|
ssKey << key;
|
||||||
leveldb::Slice slKey(&ssKey[0], ssKey.size());
|
leveldb::Slice slKey(&ssKey[0], ssKey.size());
|
||||||
|
|
||||||
CDataStream ssValue(SER_DISK, CLIENT_VERSION);
|
CDataStream ssValue(SER_DISK, CLIENT_VERSION);
|
||||||
ssValue.reserve(ssValue.GetSerializeSize(value));
|
ssValue.reserve(DBWRAPPER_PREALLOC_VALUE_SIZE);
|
||||||
ssValue << value;
|
ssValue << value;
|
||||||
ssValue.Xor(dbwrapper_private::GetObfuscateKey(parent));
|
ssValue.Xor(dbwrapper_private::GetObfuscateKey(parent));
|
||||||
leveldb::Slice slValue(&ssValue[0], ssValue.size());
|
leveldb::Slice slValue(&ssValue[0], ssValue.size());
|
||||||
|
@ -77,7 +80,7 @@ public:
|
||||||
void Erase(const K& key)
|
void Erase(const K& key)
|
||||||
{
|
{
|
||||||
CDataStream ssKey(SER_DISK, CLIENT_VERSION);
|
CDataStream ssKey(SER_DISK, CLIENT_VERSION);
|
||||||
ssKey.reserve(ssKey.GetSerializeSize(key));
|
ssKey.reserve(DBWRAPPER_PREALLOC_KEY_SIZE);
|
||||||
ssKey << key;
|
ssKey << key;
|
||||||
leveldb::Slice slKey(&ssKey[0], ssKey.size());
|
leveldb::Slice slKey(&ssKey[0], ssKey.size());
|
||||||
|
|
||||||
|
@ -107,7 +110,7 @@ public:
|
||||||
|
|
||||||
template<typename K> void Seek(const K& key) {
|
template<typename K> void Seek(const K& key) {
|
||||||
CDataStream ssKey(SER_DISK, CLIENT_VERSION);
|
CDataStream ssKey(SER_DISK, CLIENT_VERSION);
|
||||||
ssKey.reserve(ssKey.GetSerializeSize(key));
|
ssKey.reserve(DBWRAPPER_PREALLOC_KEY_SIZE);
|
||||||
ssKey << key;
|
ssKey << key;
|
||||||
leveldb::Slice slKey(&ssKey[0], ssKey.size());
|
leveldb::Slice slKey(&ssKey[0], ssKey.size());
|
||||||
piter->Seek(slKey);
|
piter->Seek(slKey);
|
||||||
|
@ -200,7 +203,7 @@ public:
|
||||||
bool Read(const K& key, V& value) const
|
bool Read(const K& key, V& value) const
|
||||||
{
|
{
|
||||||
CDataStream ssKey(SER_DISK, CLIENT_VERSION);
|
CDataStream ssKey(SER_DISK, CLIENT_VERSION);
|
||||||
ssKey.reserve(ssKey.GetSerializeSize(key));
|
ssKey.reserve(DBWRAPPER_PREALLOC_KEY_SIZE);
|
||||||
ssKey << key;
|
ssKey << key;
|
||||||
leveldb::Slice slKey(&ssKey[0], ssKey.size());
|
leveldb::Slice slKey(&ssKey[0], ssKey.size());
|
||||||
|
|
||||||
|
@ -234,7 +237,7 @@ public:
|
||||||
bool Exists(const K& key) const
|
bool Exists(const K& key) const
|
||||||
{
|
{
|
||||||
CDataStream ssKey(SER_DISK, CLIENT_VERSION);
|
CDataStream ssKey(SER_DISK, CLIENT_VERSION);
|
||||||
ssKey.reserve(ssKey.GetSerializeSize(key));
|
ssKey.reserve(DBWRAPPER_PREALLOC_KEY_SIZE);
|
||||||
ssKey << key;
|
ssKey << key;
|
||||||
leveldb::Slice slKey(&ssKey[0], ssKey.size());
|
leveldb::Slice slKey(&ssKey[0], ssKey.size());
|
||||||
|
|
||||||
|
|
12
src/hash.h
12
src/hash.h
|
@ -132,15 +132,17 @@ class CHashWriter
|
||||||
private:
|
private:
|
||||||
CHash256 ctx;
|
CHash256 ctx;
|
||||||
|
|
||||||
|
const int nType;
|
||||||
|
const int nVersion;
|
||||||
public:
|
public:
|
||||||
int nType;
|
|
||||||
int nVersion;
|
|
||||||
|
|
||||||
CHashWriter(int nTypeIn, int nVersionIn) : nType(nTypeIn), nVersion(nVersionIn) {}
|
CHashWriter(int nTypeIn, int nVersionIn) : nType(nTypeIn), nVersion(nVersionIn) {}
|
||||||
|
|
||||||
CHashWriter& write(const char *pch, size_t size) {
|
int GetType() const { return nType; }
|
||||||
|
int GetVersion() const { return nVersion; }
|
||||||
|
|
||||||
|
void write(const char *pch, size_t size) {
|
||||||
ctx.Write((const unsigned char*)pch, size);
|
ctx.Write((const unsigned char*)pch, size);
|
||||||
return (*this);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// invalidates the object
|
// invalidates the object
|
||||||
|
@ -153,7 +155,7 @@ public:
|
||||||
template<typename T>
|
template<typename T>
|
||||||
CHashWriter& operator<<(const T& obj) {
|
CHashWriter& operator<<(const T& obj) {
|
||||||
// Serialize to this stream
|
// Serialize to this stream
|
||||||
::Serialize(*this, obj, nType, nVersion);
|
::Serialize(*this, obj);
|
||||||
return (*this);
|
return (*this);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
|
@ -162,7 +162,7 @@ struct CExtKey {
|
||||||
CExtPubKey Neuter() const;
|
CExtPubKey Neuter() const;
|
||||||
void SetMaster(const unsigned char* seed, unsigned int nSeedLen);
|
void SetMaster(const unsigned char* seed, unsigned int nSeedLen);
|
||||||
template <typename Stream>
|
template <typename Stream>
|
||||||
void Serialize(Stream& s, int nType, int nVersion) const
|
void Serialize(Stream& s) const
|
||||||
{
|
{
|
||||||
unsigned int len = BIP32_EXTKEY_SIZE;
|
unsigned int len = BIP32_EXTKEY_SIZE;
|
||||||
::WriteCompactSize(s, len);
|
::WriteCompactSize(s, len);
|
||||||
|
@ -171,7 +171,7 @@ struct CExtKey {
|
||||||
s.write((const char *)&code[0], len);
|
s.write((const char *)&code[0], len);
|
||||||
}
|
}
|
||||||
template <typename Stream>
|
template <typename Stream>
|
||||||
void Unserialize(Stream& s, int nType, int nVersion)
|
void Unserialize(Stream& s)
|
||||||
{
|
{
|
||||||
unsigned int len = ::ReadCompactSize(s);
|
unsigned int len = ::ReadCompactSize(s);
|
||||||
unsigned char code[BIP32_EXTKEY_SIZE];
|
unsigned char code[BIP32_EXTKEY_SIZE];
|
||||||
|
|
|
@ -1712,7 +1712,7 @@ bool WriteBlockToDisk(const CBlock& block, CDiskBlockPos& pos, const CMessageHea
|
||||||
return error("WriteBlockToDisk: OpenBlockFile failed");
|
return error("WriteBlockToDisk: OpenBlockFile failed");
|
||||||
|
|
||||||
// Write index header
|
// Write index header
|
||||||
unsigned int nSize = fileout.GetSerializeSize(block);
|
unsigned int nSize = GetSerializeSize(fileout, block);
|
||||||
fileout << FLATDATA(messageStart) << nSize;
|
fileout << FLATDATA(messageStart) << nSize;
|
||||||
|
|
||||||
// Write block
|
// Write block
|
||||||
|
@ -2102,7 +2102,7 @@ bool UndoWriteToDisk(const CBlockUndo& blockundo, CDiskBlockPos& pos, const uint
|
||||||
return error("%s: OpenUndoFile failed", __func__);
|
return error("%s: OpenUndoFile failed", __func__);
|
||||||
|
|
||||||
// Write index header
|
// Write index header
|
||||||
unsigned int nSize = fileout.GetSerializeSize(blockundo);
|
unsigned int nSize = GetSerializeSize(fileout, blockundo);
|
||||||
fileout << FLATDATA(messageStart) << nSize;
|
fileout << FLATDATA(messageStart) << nSize;
|
||||||
|
|
||||||
// Write undo data
|
// Write undo data
|
||||||
|
|
|
@ -85,7 +85,7 @@ public:
|
||||||
ADD_SERIALIZE_METHODS;
|
ADD_SERIALIZE_METHODS;
|
||||||
|
|
||||||
template <typename Stream, typename Operation>
|
template <typename Stream, typename Operation>
|
||||||
inline void SerializationOp(Stream& s, Operation ser_action, int nType, int nVersion) {
|
inline void SerializationOp(Stream& s, Operation ser_action) {
|
||||||
READWRITE(nTransactions);
|
READWRITE(nTransactions);
|
||||||
READWRITE(vHash);
|
READWRITE(vHash);
|
||||||
std::vector<unsigned char> vBytes;
|
std::vector<unsigned char> vBytes;
|
||||||
|
@ -148,7 +148,7 @@ public:
|
||||||
ADD_SERIALIZE_METHODS;
|
ADD_SERIALIZE_METHODS;
|
||||||
|
|
||||||
template <typename Stream, typename Operation>
|
template <typename Stream, typename Operation>
|
||||||
inline void SerializationOp(Stream& s, Operation ser_action, int nType, int nVersion) {
|
inline void SerializationOp(Stream& s, Operation ser_action) {
|
||||||
READWRITE(header);
|
READWRITE(header);
|
||||||
READWRITE(txn);
|
READWRITE(txn);
|
||||||
}
|
}
|
||||||
|
|
|
@ -140,7 +140,7 @@ public:
|
||||||
void PushMessageWithVersionAndFlag(CNode* pnode, int nVersion, int flag, const std::string& sCommand, Args&&... args)
|
void PushMessageWithVersionAndFlag(CNode* pnode, int nVersion, int flag, const std::string& sCommand, Args&&... args)
|
||||||
{
|
{
|
||||||
auto msg(BeginMessage(pnode, nVersion, flag, sCommand));
|
auto msg(BeginMessage(pnode, nVersion, flag, sCommand));
|
||||||
::SerializeMany(msg, msg.nType, msg.nVersion, std::forward<Args>(args)...);
|
::SerializeMany(msg, std::forward<Args>(args)...);
|
||||||
EndMessage(msg);
|
EndMessage(msg);
|
||||||
PushMessage(pnode, msg, sCommand);
|
PushMessage(pnode, msg, sCommand);
|
||||||
}
|
}
|
||||||
|
|
|
@ -85,7 +85,7 @@ class CNetAddr
|
||||||
ADD_SERIALIZE_METHODS;
|
ADD_SERIALIZE_METHODS;
|
||||||
|
|
||||||
template <typename Stream, typename Operation>
|
template <typename Stream, typename Operation>
|
||||||
inline void SerializationOp(Stream& s, Operation ser_action, int nType, int nVersion) {
|
inline void SerializationOp(Stream& s, Operation ser_action) {
|
||||||
READWRITE(FLATDATA(ip));
|
READWRITE(FLATDATA(ip));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -122,7 +122,7 @@ class CSubNet
|
||||||
ADD_SERIALIZE_METHODS;
|
ADD_SERIALIZE_METHODS;
|
||||||
|
|
||||||
template <typename Stream, typename Operation>
|
template <typename Stream, typename Operation>
|
||||||
inline void SerializationOp(Stream& s, Operation ser_action, int nType, int nVersion) {
|
inline void SerializationOp(Stream& s, Operation ser_action) {
|
||||||
READWRITE(network);
|
READWRITE(network);
|
||||||
READWRITE(FLATDATA(netmask));
|
READWRITE(FLATDATA(netmask));
|
||||||
READWRITE(FLATDATA(valid));
|
READWRITE(FLATDATA(valid));
|
||||||
|
@ -159,7 +159,7 @@ class CService : public CNetAddr
|
||||||
ADD_SERIALIZE_METHODS;
|
ADD_SERIALIZE_METHODS;
|
||||||
|
|
||||||
template <typename Stream, typename Operation>
|
template <typename Stream, typename Operation>
|
||||||
inline void SerializationOp(Stream& s, Operation ser_action, int nType, int nVersion) {
|
inline void SerializationOp(Stream& s, Operation ser_action) {
|
||||||
READWRITE(FLATDATA(ip));
|
READWRITE(FLATDATA(ip));
|
||||||
unsigned short portN = htons(port);
|
unsigned short portN = htons(port);
|
||||||
READWRITE(FLATDATA(portN));
|
READWRITE(FLATDATA(portN));
|
||||||
|
|
|
@ -36,7 +36,7 @@ public:
|
||||||
ADD_SERIALIZE_METHODS;
|
ADD_SERIALIZE_METHODS;
|
||||||
|
|
||||||
template <typename Stream, typename Operation>
|
template <typename Stream, typename Operation>
|
||||||
inline void SerializationOp(Stream& s, Operation ser_action, int nType, int nVersion) {
|
inline void SerializationOp(Stream& s, Operation ser_action) {
|
||||||
READWRITE(this->nVersion);
|
READWRITE(this->nVersion);
|
||||||
READWRITE(hashPrevBlock);
|
READWRITE(hashPrevBlock);
|
||||||
READWRITE(hashMerkleRoot);
|
READWRITE(hashMerkleRoot);
|
||||||
|
@ -92,7 +92,7 @@ public:
|
||||||
ADD_SERIALIZE_METHODS;
|
ADD_SERIALIZE_METHODS;
|
||||||
|
|
||||||
template <typename Stream, typename Operation>
|
template <typename Stream, typename Operation>
|
||||||
inline void SerializationOp(Stream& s, Operation ser_action, int nType, int nVersion) {
|
inline void SerializationOp(Stream& s, Operation ser_action) {
|
||||||
READWRITE(*(CBlockHeader*)this);
|
READWRITE(*(CBlockHeader*)this);
|
||||||
READWRITE(vtx);
|
READWRITE(vtx);
|
||||||
}
|
}
|
||||||
|
@ -137,8 +137,9 @@ struct CBlockLocator
|
||||||
ADD_SERIALIZE_METHODS;
|
ADD_SERIALIZE_METHODS;
|
||||||
|
|
||||||
template <typename Stream, typename Operation>
|
template <typename Stream, typename Operation>
|
||||||
inline void SerializationOp(Stream& s, Operation ser_action, int nType, int nVersion) {
|
inline void SerializationOp(Stream& s, Operation ser_action) {
|
||||||
if (!(nType & SER_GETHASH))
|
int nVersion = s.GetVersion();
|
||||||
|
if (!(s.GetType() & SER_GETHASH))
|
||||||
READWRITE(nVersion);
|
READWRITE(nVersion);
|
||||||
READWRITE(vHave);
|
READWRITE(vHave);
|
||||||
}
|
}
|
||||||
|
|
|
@ -28,7 +28,7 @@ public:
|
||||||
ADD_SERIALIZE_METHODS;
|
ADD_SERIALIZE_METHODS;
|
||||||
|
|
||||||
template <typename Stream, typename Operation>
|
template <typename Stream, typename Operation>
|
||||||
inline void SerializationOp(Stream& s, Operation ser_action, int nType, int nVersion) {
|
inline void SerializationOp(Stream& s, Operation ser_action) {
|
||||||
READWRITE(hash);
|
READWRITE(hash);
|
||||||
READWRITE(n);
|
READWRITE(n);
|
||||||
}
|
}
|
||||||
|
@ -104,7 +104,7 @@ public:
|
||||||
ADD_SERIALIZE_METHODS;
|
ADD_SERIALIZE_METHODS;
|
||||||
|
|
||||||
template <typename Stream, typename Operation>
|
template <typename Stream, typename Operation>
|
||||||
inline void SerializationOp(Stream& s, Operation ser_action, int nType, int nVersion) {
|
inline void SerializationOp(Stream& s, Operation ser_action) {
|
||||||
READWRITE(prevout);
|
READWRITE(prevout);
|
||||||
READWRITE(*(CScriptBase*)(&scriptSig));
|
READWRITE(*(CScriptBase*)(&scriptSig));
|
||||||
READWRITE(nSequence);
|
READWRITE(nSequence);
|
||||||
|
@ -144,7 +144,7 @@ public:
|
||||||
ADD_SERIALIZE_METHODS;
|
ADD_SERIALIZE_METHODS;
|
||||||
|
|
||||||
template <typename Stream, typename Operation>
|
template <typename Stream, typename Operation>
|
||||||
inline void SerializationOp(Stream& s, Operation ser_action, int nType, int nVersion) {
|
inline void SerializationOp(Stream& s, Operation ser_action) {
|
||||||
READWRITE(nValue);
|
READWRITE(nValue);
|
||||||
READWRITE(*(CScriptBase*)(&scriptPubKey));
|
READWRITE(*(CScriptBase*)(&scriptPubKey));
|
||||||
}
|
}
|
||||||
|
@ -177,7 +177,7 @@ public:
|
||||||
if (scriptPubKey.IsUnspendable())
|
if (scriptPubKey.IsUnspendable())
|
||||||
return 0;
|
return 0;
|
||||||
|
|
||||||
size_t nSize = GetSerializeSize(SER_DISK, 0);
|
size_t nSize = GetSerializeSize(*this, SER_DISK, 0);
|
||||||
int witnessversion = 0;
|
int witnessversion = 0;
|
||||||
std::vector<unsigned char> witnessprogram;
|
std::vector<unsigned char> witnessprogram;
|
||||||
|
|
||||||
|
@ -219,7 +219,7 @@ public:
|
||||||
ADD_SERIALIZE_METHODS;
|
ADD_SERIALIZE_METHODS;
|
||||||
|
|
||||||
template <typename Stream, typename Operation>
|
template <typename Stream, typename Operation>
|
||||||
inline void SerializationOp(Stream& s, Operation ser_action, int nType, int nVersion)
|
inline void SerializationOp(Stream& s, Operation ser_action)
|
||||||
{
|
{
|
||||||
READWRITE(scriptWitness.stack);
|
READWRITE(scriptWitness.stack);
|
||||||
}
|
}
|
||||||
|
@ -255,7 +255,7 @@ public:
|
||||||
}
|
}
|
||||||
|
|
||||||
template <typename Stream, typename Operation>
|
template <typename Stream, typename Operation>
|
||||||
inline void SerializationOp(Stream& s, Operation ser_action, int nType, int nVersion)
|
inline void SerializationOp(Stream& s, Operation ser_action)
|
||||||
{
|
{
|
||||||
for (size_t n = 0; n < vtxinwit.size(); n++) {
|
for (size_t n = 0; n < vtxinwit.size(); n++) {
|
||||||
READWRITE(vtxinwit[n]);
|
READWRITE(vtxinwit[n]);
|
||||||
|
@ -287,8 +287,8 @@ struct CMutableTransaction;
|
||||||
* - uint32_t nLockTime
|
* - uint32_t nLockTime
|
||||||
*/
|
*/
|
||||||
template<typename Stream, typename Operation, typename TxType>
|
template<typename Stream, typename Operation, typename TxType>
|
||||||
inline void SerializeTransaction(TxType& tx, Stream& s, Operation ser_action, int nType, int nVersion) {
|
inline void SerializeTransaction(TxType& tx, Stream& s, Operation ser_action) {
|
||||||
const bool fAllowWitness = !(nVersion & SERIALIZE_TRANSACTION_NO_WITNESS);
|
const bool fAllowWitness = !(s.GetVersion() & SERIALIZE_TRANSACTION_NO_WITNESS);
|
||||||
|
|
||||||
READWRITE(*const_cast<int32_t*>(&tx.nVersion));
|
READWRITE(*const_cast<int32_t*>(&tx.nVersion));
|
||||||
unsigned char flags = 0;
|
unsigned char flags = 0;
|
||||||
|
@ -385,8 +385,8 @@ public:
|
||||||
ADD_SERIALIZE_METHODS;
|
ADD_SERIALIZE_METHODS;
|
||||||
|
|
||||||
template <typename Stream, typename Operation>
|
template <typename Stream, typename Operation>
|
||||||
inline void SerializationOp(Stream& s, Operation ser_action, int nType, int nVersion) {
|
inline void SerializationOp(Stream& s, Operation ser_action) {
|
||||||
SerializeTransaction(*this, s, ser_action, nType, nVersion);
|
SerializeTransaction(*this, s, ser_action);
|
||||||
if (ser_action.ForRead()) {
|
if (ser_action.ForRead()) {
|
||||||
UpdateHash();
|
UpdateHash();
|
||||||
}
|
}
|
||||||
|
@ -456,8 +456,8 @@ struct CMutableTransaction
|
||||||
ADD_SERIALIZE_METHODS;
|
ADD_SERIALIZE_METHODS;
|
||||||
|
|
||||||
template <typename Stream, typename Operation>
|
template <typename Stream, typename Operation>
|
||||||
inline void SerializationOp(Stream& s, Operation ser_action, int nType, int nVersion) {
|
inline void SerializationOp(Stream& s, Operation ser_action) {
|
||||||
SerializeTransaction(*this, s, ser_action, nType, nVersion);
|
SerializeTransaction(*this, s, ser_action);
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Compute the hash of this CMutableTransaction. This is computed on the
|
/** Compute the hash of this CMutableTransaction. This is computed on the
|
||||||
|
|
|
@ -48,7 +48,7 @@ public:
|
||||||
ADD_SERIALIZE_METHODS;
|
ADD_SERIALIZE_METHODS;
|
||||||
|
|
||||||
template <typename Stream, typename Operation>
|
template <typename Stream, typename Operation>
|
||||||
inline void SerializationOp(Stream& s, Operation ser_action, int nType, int nVersion)
|
inline void SerializationOp(Stream& s, Operation ser_action)
|
||||||
{
|
{
|
||||||
READWRITE(FLATDATA(pchMessageStart));
|
READWRITE(FLATDATA(pchMessageStart));
|
||||||
READWRITE(FLATDATA(pchCommand));
|
READWRITE(FLATDATA(pchCommand));
|
||||||
|
@ -289,14 +289,15 @@ public:
|
||||||
ADD_SERIALIZE_METHODS;
|
ADD_SERIALIZE_METHODS;
|
||||||
|
|
||||||
template <typename Stream, typename Operation>
|
template <typename Stream, typename Operation>
|
||||||
inline void SerializationOp(Stream& s, Operation ser_action, int nType, int nVersion)
|
inline void SerializationOp(Stream& s, Operation ser_action)
|
||||||
{
|
{
|
||||||
if (ser_action.ForRead())
|
if (ser_action.ForRead())
|
||||||
Init();
|
Init();
|
||||||
if (nType & SER_DISK)
|
int nVersion = s.GetVersion();
|
||||||
|
if (s.GetType() & SER_DISK)
|
||||||
READWRITE(nVersion);
|
READWRITE(nVersion);
|
||||||
if ((nType & SER_DISK) ||
|
if ((s.GetType() & SER_DISK) ||
|
||||||
(nVersion >= CADDR_TIME_VERSION && !(nType & SER_GETHASH)))
|
(nVersion >= CADDR_TIME_VERSION && !(s.GetType() & SER_GETHASH)))
|
||||||
READWRITE(nTime);
|
READWRITE(nTime);
|
||||||
uint64_t nServicesInt = nServices;
|
uint64_t nServicesInt = nServices;
|
||||||
READWRITE(nServicesInt);
|
READWRITE(nServicesInt);
|
||||||
|
@ -343,7 +344,7 @@ public:
|
||||||
ADD_SERIALIZE_METHODS;
|
ADD_SERIALIZE_METHODS;
|
||||||
|
|
||||||
template <typename Stream, typename Operation>
|
template <typename Stream, typename Operation>
|
||||||
inline void SerializationOp(Stream& s, Operation ser_action, int nType, int nVersion)
|
inline void SerializationOp(Stream& s, Operation ser_action)
|
||||||
{
|
{
|
||||||
READWRITE(type);
|
READWRITE(type);
|
||||||
READWRITE(hash);
|
READWRITE(hash);
|
||||||
|
|
17
src/pubkey.h
17
src/pubkey.h
|
@ -116,19 +116,15 @@ public:
|
||||||
}
|
}
|
||||||
|
|
||||||
//! Implement serialization, as if this was a byte vector.
|
//! Implement serialization, as if this was a byte vector.
|
||||||
unsigned int GetSerializeSize(int nType, int nVersion) const
|
|
||||||
{
|
|
||||||
return size() + 1;
|
|
||||||
}
|
|
||||||
template <typename Stream>
|
template <typename Stream>
|
||||||
void Serialize(Stream& s, int nType, int nVersion) const
|
void Serialize(Stream& s) const
|
||||||
{
|
{
|
||||||
unsigned int len = size();
|
unsigned int len = size();
|
||||||
::WriteCompactSize(s, len);
|
::WriteCompactSize(s, len);
|
||||||
s.write((char*)vch, len);
|
s.write((char*)vch, len);
|
||||||
}
|
}
|
||||||
template <typename Stream>
|
template <typename Stream>
|
||||||
void Unserialize(Stream& s, int nType, int nVersion)
|
void Unserialize(Stream& s)
|
||||||
{
|
{
|
||||||
unsigned int len = ::ReadCompactSize(s);
|
unsigned int len = ::ReadCompactSize(s);
|
||||||
if (len <= 65) {
|
if (len <= 65) {
|
||||||
|
@ -214,12 +210,13 @@ struct CExtPubKey {
|
||||||
void Decode(const unsigned char code[BIP32_EXTKEY_SIZE]);
|
void Decode(const unsigned char code[BIP32_EXTKEY_SIZE]);
|
||||||
bool Derive(CExtPubKey& out, unsigned int nChild) const;
|
bool Derive(CExtPubKey& out, unsigned int nChild) const;
|
||||||
|
|
||||||
unsigned int GetSerializeSize(int nType, int nVersion) const
|
void Serialize(CSizeComputer& s) const
|
||||||
{
|
{
|
||||||
return BIP32_EXTKEY_SIZE+1; //add one byte for the size (compact int)
|
// Optimized implementation for ::GetSerializeSize that avoids copying.
|
||||||
|
s.seek(BIP32_EXTKEY_SIZE + 1); // add one byte for the size (compact int)
|
||||||
}
|
}
|
||||||
template <typename Stream>
|
template <typename Stream>
|
||||||
void Serialize(Stream& s, int nType, int nVersion) const
|
void Serialize(Stream& s) const
|
||||||
{
|
{
|
||||||
unsigned int len = BIP32_EXTKEY_SIZE;
|
unsigned int len = BIP32_EXTKEY_SIZE;
|
||||||
::WriteCompactSize(s, len);
|
::WriteCompactSize(s, len);
|
||||||
|
@ -228,7 +225,7 @@ struct CExtPubKey {
|
||||||
s.write((const char *)&code[0], len);
|
s.write((const char *)&code[0], len);
|
||||||
}
|
}
|
||||||
template <typename Stream>
|
template <typename Stream>
|
||||||
void Unserialize(Stream& s, int nType, int nVersion)
|
void Unserialize(Stream& s)
|
||||||
{
|
{
|
||||||
unsigned int len = ::ReadCompactSize(s);
|
unsigned int len = ::ReadCompactSize(s);
|
||||||
unsigned char code[BIP32_EXTKEY_SIZE];
|
unsigned char code[BIP32_EXTKEY_SIZE];
|
||||||
|
|
|
@ -27,7 +27,7 @@ public:
|
||||||
ADD_SERIALIZE_METHODS;
|
ADD_SERIALIZE_METHODS;
|
||||||
|
|
||||||
template <typename Stream, typename Operation>
|
template <typename Stream, typename Operation>
|
||||||
inline void SerializationOp(Stream& s, Operation ser_action, int nType, int nVersion) {
|
inline void SerializationOp(Stream& s, Operation ser_action) {
|
||||||
unsigned int nDate = date.toTime_t();
|
unsigned int nDate = date.toTime_t();
|
||||||
|
|
||||||
READWRITE(this->nVersion);
|
READWRITE(this->nVersion);
|
||||||
|
|
|
@ -65,7 +65,7 @@ public:
|
||||||
ADD_SERIALIZE_METHODS;
|
ADD_SERIALIZE_METHODS;
|
||||||
|
|
||||||
template <typename Stream, typename Operation>
|
template <typename Stream, typename Operation>
|
||||||
inline void SerializationOp(Stream& s, Operation ser_action, int nType, int nVersion) {
|
inline void SerializationOp(Stream& s, Operation ser_action) {
|
||||||
std::string sAddress = address.toStdString();
|
std::string sAddress = address.toStdString();
|
||||||
std::string sLabel = label.toStdString();
|
std::string sLabel = label.toStdString();
|
||||||
std::string sMessage = message.toStdString();
|
std::string sMessage = message.toStdString();
|
||||||
|
|
|
@ -50,7 +50,7 @@ struct CCoin {
|
||||||
ADD_SERIALIZE_METHODS;
|
ADD_SERIALIZE_METHODS;
|
||||||
|
|
||||||
template <typename Stream, typename Operation>
|
template <typename Stream, typename Operation>
|
||||||
inline void SerializationOp(Stream& s, Operation ser_action, int nType, int nVersion)
|
inline void SerializationOp(Stream& s, Operation ser_action)
|
||||||
{
|
{
|
||||||
READWRITE(nTxVer);
|
READWRITE(nTxVer);
|
||||||
READWRITE(nHeight);
|
READWRITE(nHeight);
|
||||||
|
|
|
@ -23,7 +23,7 @@ public:
|
||||||
m_remaining(txToLen)
|
m_remaining(txToLen)
|
||||||
{}
|
{}
|
||||||
|
|
||||||
TxInputStream& read(char* pch, size_t nSize)
|
void read(char* pch, size_t nSize)
|
||||||
{
|
{
|
||||||
if (nSize > m_remaining)
|
if (nSize > m_remaining)
|
||||||
throw std::ios_base::failure(std::string(__func__) + ": end of data");
|
throw std::ios_base::failure(std::string(__func__) + ": end of data");
|
||||||
|
@ -37,16 +37,17 @@ public:
|
||||||
memcpy(pch, m_data, nSize);
|
memcpy(pch, m_data, nSize);
|
||||||
m_remaining -= nSize;
|
m_remaining -= nSize;
|
||||||
m_data += nSize;
|
m_data += nSize;
|
||||||
return *this;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
template<typename T>
|
template<typename T>
|
||||||
TxInputStream& operator>>(T& obj)
|
TxInputStream& operator>>(T& obj)
|
||||||
{
|
{
|
||||||
::Unserialize(*this, obj, m_type, m_version);
|
::Unserialize(*this, obj);
|
||||||
return *this;
|
return *this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
int GetVersion() const { return m_version; }
|
||||||
|
int GetType() const { return m_type; }
|
||||||
private:
|
private:
|
||||||
const int m_type;
|
const int m_type;
|
||||||
const int m_version;
|
const int m_version;
|
||||||
|
@ -88,7 +89,7 @@ static int verify_script(const unsigned char *scriptPubKey, unsigned int scriptP
|
||||||
stream >> tx;
|
stream >> tx;
|
||||||
if (nIn >= tx.vin.size())
|
if (nIn >= tx.vin.size())
|
||||||
return set_error(err, bitcoinconsensus_ERR_TX_INDEX);
|
return set_error(err, bitcoinconsensus_ERR_TX_INDEX);
|
||||||
if (tx.GetSerializeSize(SER_NETWORK, PROTOCOL_VERSION) != txToLen)
|
if (GetSerializeSize(tx, SER_NETWORK, PROTOCOL_VERSION) != txToLen)
|
||||||
return set_error(err, bitcoinconsensus_ERR_TX_SIZE_MISMATCH);
|
return set_error(err, bitcoinconsensus_ERR_TX_SIZE_MISMATCH);
|
||||||
|
|
||||||
// Regardless of the verification result, the tx did not error.
|
// Regardless of the verification result, the tx did not error.
|
||||||
|
|
|
@ -1069,7 +1069,7 @@ public:
|
||||||
|
|
||||||
/** Serialize the passed scriptCode, skipping OP_CODESEPARATORs */
|
/** Serialize the passed scriptCode, skipping OP_CODESEPARATORs */
|
||||||
template<typename S>
|
template<typename S>
|
||||||
void SerializeScriptCode(S &s, int nType, int nVersion) const {
|
void SerializeScriptCode(S &s) const {
|
||||||
CScript::const_iterator it = scriptCode.begin();
|
CScript::const_iterator it = scriptCode.begin();
|
||||||
CScript::const_iterator itBegin = it;
|
CScript::const_iterator itBegin = it;
|
||||||
opcodetype opcode;
|
opcodetype opcode;
|
||||||
|
@ -1092,53 +1092,53 @@ public:
|
||||||
|
|
||||||
/** Serialize an input of txTo */
|
/** Serialize an input of txTo */
|
||||||
template<typename S>
|
template<typename S>
|
||||||
void SerializeInput(S &s, unsigned int nInput, int nType, int nVersion) const {
|
void SerializeInput(S &s, unsigned int nInput) const {
|
||||||
// In case of SIGHASH_ANYONECANPAY, only the input being signed is serialized
|
// In case of SIGHASH_ANYONECANPAY, only the input being signed is serialized
|
||||||
if (fAnyoneCanPay)
|
if (fAnyoneCanPay)
|
||||||
nInput = nIn;
|
nInput = nIn;
|
||||||
// Serialize the prevout
|
// Serialize the prevout
|
||||||
::Serialize(s, txTo.vin[nInput].prevout, nType, nVersion);
|
::Serialize(s, txTo.vin[nInput].prevout);
|
||||||
// Serialize the script
|
// Serialize the script
|
||||||
if (nInput != nIn)
|
if (nInput != nIn)
|
||||||
// Blank out other inputs' signatures
|
// Blank out other inputs' signatures
|
||||||
::Serialize(s, CScriptBase(), nType, nVersion);
|
::Serialize(s, CScriptBase());
|
||||||
else
|
else
|
||||||
SerializeScriptCode(s, nType, nVersion);
|
SerializeScriptCode(s);
|
||||||
// Serialize the nSequence
|
// Serialize the nSequence
|
||||||
if (nInput != nIn && (fHashSingle || fHashNone))
|
if (nInput != nIn && (fHashSingle || fHashNone))
|
||||||
// let the others update at will
|
// let the others update at will
|
||||||
::Serialize(s, (int)0, nType, nVersion);
|
::Serialize(s, (int)0);
|
||||||
else
|
else
|
||||||
::Serialize(s, txTo.vin[nInput].nSequence, nType, nVersion);
|
::Serialize(s, txTo.vin[nInput].nSequence);
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Serialize an output of txTo */
|
/** Serialize an output of txTo */
|
||||||
template<typename S>
|
template<typename S>
|
||||||
void SerializeOutput(S &s, unsigned int nOutput, int nType, int nVersion) const {
|
void SerializeOutput(S &s, unsigned int nOutput) const {
|
||||||
if (fHashSingle && nOutput != nIn)
|
if (fHashSingle && nOutput != nIn)
|
||||||
// Do not lock-in the txout payee at other indices as txin
|
// Do not lock-in the txout payee at other indices as txin
|
||||||
::Serialize(s, CTxOut(), nType, nVersion);
|
::Serialize(s, CTxOut());
|
||||||
else
|
else
|
||||||
::Serialize(s, txTo.vout[nOutput], nType, nVersion);
|
::Serialize(s, txTo.vout[nOutput]);
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Serialize txTo */
|
/** Serialize txTo */
|
||||||
template<typename S>
|
template<typename S>
|
||||||
void Serialize(S &s, int nType, int nVersion) const {
|
void Serialize(S &s) const {
|
||||||
// Serialize nVersion
|
// Serialize nVersion
|
||||||
::Serialize(s, txTo.nVersion, nType, nVersion);
|
::Serialize(s, txTo.nVersion);
|
||||||
// Serialize vin
|
// Serialize vin
|
||||||
unsigned int nInputs = fAnyoneCanPay ? 1 : txTo.vin.size();
|
unsigned int nInputs = fAnyoneCanPay ? 1 : txTo.vin.size();
|
||||||
::WriteCompactSize(s, nInputs);
|
::WriteCompactSize(s, nInputs);
|
||||||
for (unsigned int nInput = 0; nInput < nInputs; nInput++)
|
for (unsigned int nInput = 0; nInput < nInputs; nInput++)
|
||||||
SerializeInput(s, nInput, nType, nVersion);
|
SerializeInput(s, nInput);
|
||||||
// Serialize vout
|
// Serialize vout
|
||||||
unsigned int nOutputs = fHashNone ? 0 : (fHashSingle ? nIn+1 : txTo.vout.size());
|
unsigned int nOutputs = fHashNone ? 0 : (fHashSingle ? nIn+1 : txTo.vout.size());
|
||||||
::WriteCompactSize(s, nOutputs);
|
::WriteCompactSize(s, nOutputs);
|
||||||
for (unsigned int nOutput = 0; nOutput < nOutputs; nOutput++)
|
for (unsigned int nOutput = 0; nOutput < nOutputs; nOutput++)
|
||||||
SerializeOutput(s, nOutput, nType, nVersion);
|
SerializeOutput(s, nOutput);
|
||||||
// Serialize nLockTime
|
// Serialize nLockTime
|
||||||
::Serialize(s, txTo.nLockTime, nType, nVersion);
|
::Serialize(s, txTo.nLockTime);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
448
src/serialize.h
448
src/serialize.h
|
@ -151,6 +151,8 @@ inline float ser_uint32_to_float(uint32_t y)
|
||||||
// i.e. anything that supports .read(char*, size_t) and .write(char*, size_t)
|
// i.e. anything that supports .read(char*, size_t) and .write(char*, size_t)
|
||||||
//
|
//
|
||||||
|
|
||||||
|
class CSizeComputer;
|
||||||
|
|
||||||
enum
|
enum
|
||||||
{
|
{
|
||||||
// primary actions
|
// primary actions
|
||||||
|
@ -159,8 +161,8 @@ enum
|
||||||
SER_GETHASH = (1 << 2),
|
SER_GETHASH = (1 << 2),
|
||||||
};
|
};
|
||||||
|
|
||||||
#define READWRITE(obj) (::SerReadWrite(s, (obj), nType, nVersion, ser_action))
|
#define READWRITE(obj) (::SerReadWrite(s, (obj), ser_action))
|
||||||
#define READWRITEMANY(...) (::SerReadWriteMany(s, nType, nVersion, ser_action, __VA_ARGS__))
|
#define READWRITEMANY(...) (::SerReadWriteMany(s, ser_action, __VA_ARGS__))
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Implement three methods for serializable objects. These are actually wrappers over
|
* Implement three methods for serializable objects. These are actually wrappers over
|
||||||
|
@ -168,63 +170,42 @@ enum
|
||||||
* code. Adding "ADD_SERIALIZE_METHODS" in the body of the class causes these wrappers to be
|
* code. Adding "ADD_SERIALIZE_METHODS" in the body of the class causes these wrappers to be
|
||||||
* added as members.
|
* added as members.
|
||||||
*/
|
*/
|
||||||
#define ADD_SERIALIZE_METHODS \
|
#define ADD_SERIALIZE_METHODS \
|
||||||
size_t GetSerializeSize(int nType, int nVersion) const { \
|
template<typename Stream> \
|
||||||
CSizeComputer s(nType, nVersion); \
|
void Serialize(Stream& s) const { \
|
||||||
NCONST_PTR(this)->SerializationOp(s, CSerActionSerialize(), nType, nVersion);\
|
NCONST_PTR(this)->SerializationOp(s, CSerActionSerialize()); \
|
||||||
return s.size(); \
|
} \
|
||||||
} \
|
template<typename Stream> \
|
||||||
template<typename Stream> \
|
void Unserialize(Stream& s) { \
|
||||||
void Serialize(Stream& s, int nType, int nVersion) const { \
|
SerializationOp(s, CSerActionUnserialize()); \
|
||||||
NCONST_PTR(this)->SerializationOp(s, CSerActionSerialize(), nType, nVersion);\
|
|
||||||
} \
|
|
||||||
template<typename Stream> \
|
|
||||||
void Unserialize(Stream& s, int nType, int nVersion) { \
|
|
||||||
SerializationOp(s, CSerActionUnserialize(), nType, nVersion); \
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
template<typename Stream> inline void Serialize(Stream& s, char a ) { ser_writedata8(s, a); } // TODO Get rid of bare char
|
||||||
* Basic Types
|
template<typename Stream> inline void Serialize(Stream& s, int8_t a ) { ser_writedata8(s, a); }
|
||||||
*/
|
template<typename Stream> inline void Serialize(Stream& s, uint8_t a ) { ser_writedata8(s, a); }
|
||||||
inline unsigned int GetSerializeSize(char a, int, int=0) { return 1; }
|
template<typename Stream> inline void Serialize(Stream& s, int16_t a ) { ser_writedata16(s, a); }
|
||||||
inline unsigned int GetSerializeSize(int8_t a, int, int=0) { return 1; }
|
template<typename Stream> inline void Serialize(Stream& s, uint16_t a) { ser_writedata16(s, a); }
|
||||||
inline unsigned int GetSerializeSize(uint8_t a, int, int=0) { return 1; }
|
template<typename Stream> inline void Serialize(Stream& s, int32_t a ) { ser_writedata32(s, a); }
|
||||||
inline unsigned int GetSerializeSize(int16_t a, int, int=0) { return 2; }
|
template<typename Stream> inline void Serialize(Stream& s, uint32_t a) { ser_writedata32(s, a); }
|
||||||
inline unsigned int GetSerializeSize(uint16_t a, int, int=0) { return 2; }
|
template<typename Stream> inline void Serialize(Stream& s, int64_t a ) { ser_writedata64(s, a); }
|
||||||
inline unsigned int GetSerializeSize(int32_t a, int, int=0) { return 4; }
|
template<typename Stream> inline void Serialize(Stream& s, uint64_t a) { ser_writedata64(s, a); }
|
||||||
inline unsigned int GetSerializeSize(uint32_t a, int, int=0) { return 4; }
|
template<typename Stream> inline void Serialize(Stream& s, float a ) { ser_writedata32(s, ser_float_to_uint32(a)); }
|
||||||
inline unsigned int GetSerializeSize(int64_t a, int, int=0) { return 8; }
|
template<typename Stream> inline void Serialize(Stream& s, double a ) { ser_writedata64(s, ser_double_to_uint64(a)); }
|
||||||
inline unsigned int GetSerializeSize(uint64_t a, int, int=0) { return 8; }
|
|
||||||
inline unsigned int GetSerializeSize(float a, int, int=0) { return 4; }
|
|
||||||
inline unsigned int GetSerializeSize(double a, int, int=0) { return 8; }
|
|
||||||
|
|
||||||
template<typename Stream> inline void Serialize(Stream& s, char a, int, int=0) { ser_writedata8(s, a); } // TODO Get rid of bare char
|
template<typename Stream> inline void Unserialize(Stream& s, char& a ) { a = ser_readdata8(s); } // TODO Get rid of bare char
|
||||||
template<typename Stream> inline void Serialize(Stream& s, int8_t a, int, int=0) { ser_writedata8(s, a); }
|
template<typename Stream> inline void Unserialize(Stream& s, int8_t& a ) { a = ser_readdata8(s); }
|
||||||
template<typename Stream> inline void Serialize(Stream& s, uint8_t a, int, int=0) { ser_writedata8(s, a); }
|
template<typename Stream> inline void Unserialize(Stream& s, uint8_t& a ) { a = ser_readdata8(s); }
|
||||||
template<typename Stream> inline void Serialize(Stream& s, int16_t a, int, int=0) { ser_writedata16(s, a); }
|
template<typename Stream> inline void Unserialize(Stream& s, int16_t& a ) { a = ser_readdata16(s); }
|
||||||
template<typename Stream> inline void Serialize(Stream& s, uint16_t a, int, int=0) { ser_writedata16(s, a); }
|
template<typename Stream> inline void Unserialize(Stream& s, uint16_t& a) { a = ser_readdata16(s); }
|
||||||
template<typename Stream> inline void Serialize(Stream& s, int32_t a, int, int=0) { ser_writedata32(s, a); }
|
template<typename Stream> inline void Unserialize(Stream& s, int32_t& a ) { a = ser_readdata32(s); }
|
||||||
template<typename Stream> inline void Serialize(Stream& s, uint32_t a, int, int=0) { ser_writedata32(s, a); }
|
template<typename Stream> inline void Unserialize(Stream& s, uint32_t& a) { a = ser_readdata32(s); }
|
||||||
template<typename Stream> inline void Serialize(Stream& s, int64_t a, int, int=0) { ser_writedata64(s, a); }
|
template<typename Stream> inline void Unserialize(Stream& s, int64_t& a ) { a = ser_readdata64(s); }
|
||||||
template<typename Stream> inline void Serialize(Stream& s, uint64_t a, int, int=0) { ser_writedata64(s, a); }
|
template<typename Stream> inline void Unserialize(Stream& s, uint64_t& a) { a = ser_readdata64(s); }
|
||||||
template<typename Stream> inline void Serialize(Stream& s, float a, int, int=0) { ser_writedata32(s, ser_float_to_uint32(a)); }
|
template<typename Stream> inline void Unserialize(Stream& s, float& a ) { a = ser_uint32_to_float(ser_readdata32(s)); }
|
||||||
template<typename Stream> inline void Serialize(Stream& s, double a, int, int=0) { ser_writedata64(s, ser_double_to_uint64(a)); }
|
template<typename Stream> inline void Unserialize(Stream& s, double& a ) { a = ser_uint64_to_double(ser_readdata64(s)); }
|
||||||
|
|
||||||
template<typename Stream> inline void Unserialize(Stream& s, char& a, int, int=0) { a = ser_readdata8(s); } // TODO Get rid of bare char
|
template<typename Stream> inline void Serialize(Stream& s, bool a) { char f=a; ser_writedata8(s, f); }
|
||||||
template<typename Stream> inline void Unserialize(Stream& s, int8_t& a, int, int=0) { a = ser_readdata8(s); }
|
template<typename Stream> inline void Unserialize(Stream& s, bool& a) { char f=ser_readdata8(s); a=f; }
|
||||||
template<typename Stream> inline void Unserialize(Stream& s, uint8_t& a, int, int=0) { a = ser_readdata8(s); }
|
|
||||||
template<typename Stream> inline void Unserialize(Stream& s, int16_t& a, int, int=0) { a = ser_readdata16(s); }
|
|
||||||
template<typename Stream> inline void Unserialize(Stream& s, uint16_t& a, int, int=0) { a = ser_readdata16(s); }
|
|
||||||
template<typename Stream> inline void Unserialize(Stream& s, int32_t& a, int, int=0) { a = ser_readdata32(s); }
|
|
||||||
template<typename Stream> inline void Unserialize(Stream& s, uint32_t& a, int, int=0) { a = ser_readdata32(s); }
|
|
||||||
template<typename Stream> inline void Unserialize(Stream& s, int64_t& a, int, int=0) { a = ser_readdata64(s); }
|
|
||||||
template<typename Stream> inline void Unserialize(Stream& s, uint64_t& a, int, int=0) { a = ser_readdata64(s); }
|
|
||||||
template<typename Stream> inline void Unserialize(Stream& s, float& a, int, int=0) { a = ser_uint32_to_float(ser_readdata32(s)); }
|
|
||||||
template<typename Stream> inline void Unserialize(Stream& s, double& a, int, int=0) { a = ser_uint64_to_double(ser_readdata64(s)); }
|
|
||||||
|
|
||||||
inline unsigned int GetSerializeSize(bool a, int, int=0) { return sizeof(char); }
|
|
||||||
template<typename Stream> inline void Serialize(Stream& s, bool a, int, int=0) { char f=a; ser_writedata8(s, f); }
|
|
||||||
template<typename Stream> inline void Unserialize(Stream& s, bool& a, int, int=0) { char f=ser_readdata8(s); a=f; }
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
@ -246,6 +227,8 @@ inline unsigned int GetSizeOfCompactSize(uint64_t nSize)
|
||||||
else return sizeof(unsigned char) + sizeof(uint64_t);
|
else return sizeof(unsigned char) + sizeof(uint64_t);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
inline void WriteCompactSize(CSizeComputer& os, uint64_t nSize);
|
||||||
|
|
||||||
template<typename Stream>
|
template<typename Stream>
|
||||||
void WriteCompactSize(Stream& os, uint64_t nSize)
|
void WriteCompactSize(Stream& os, uint64_t nSize)
|
||||||
{
|
{
|
||||||
|
@ -340,6 +323,9 @@ inline unsigned int GetSizeOfVarInt(I n)
|
||||||
return nRet;
|
return nRet;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
template<typename I>
|
||||||
|
inline void WriteVarInt(CSizeComputer& os, I n);
|
||||||
|
|
||||||
template<typename Stream, typename I>
|
template<typename Stream, typename I>
|
||||||
void WriteVarInt(Stream& os, I n)
|
void WriteVarInt(Stream& os, I n)
|
||||||
{
|
{
|
||||||
|
@ -403,19 +389,14 @@ public:
|
||||||
char* end() { return pend; }
|
char* end() { return pend; }
|
||||||
const char* end() const { return pend; }
|
const char* end() const { return pend; }
|
||||||
|
|
||||||
unsigned int GetSerializeSize(int, int=0) const
|
|
||||||
{
|
|
||||||
return pend - pbegin;
|
|
||||||
}
|
|
||||||
|
|
||||||
template<typename Stream>
|
template<typename Stream>
|
||||||
void Serialize(Stream& s, int, int=0) const
|
void Serialize(Stream& s) const
|
||||||
{
|
{
|
||||||
s.write(pbegin, pend - pbegin);
|
s.write(pbegin, pend - pbegin);
|
||||||
}
|
}
|
||||||
|
|
||||||
template<typename Stream>
|
template<typename Stream>
|
||||||
void Unserialize(Stream& s, int, int=0)
|
void Unserialize(Stream& s)
|
||||||
{
|
{
|
||||||
s.read(pbegin, pend - pbegin);
|
s.read(pbegin, pend - pbegin);
|
||||||
}
|
}
|
||||||
|
@ -429,17 +410,13 @@ protected:
|
||||||
public:
|
public:
|
||||||
CVarInt(I& nIn) : n(nIn) { }
|
CVarInt(I& nIn) : n(nIn) { }
|
||||||
|
|
||||||
unsigned int GetSerializeSize(int, int) const {
|
|
||||||
return GetSizeOfVarInt<I>(n);
|
|
||||||
}
|
|
||||||
|
|
||||||
template<typename Stream>
|
template<typename Stream>
|
||||||
void Serialize(Stream &s, int, int) const {
|
void Serialize(Stream &s) const {
|
||||||
WriteVarInt<Stream,I>(s, n);
|
WriteVarInt<Stream,I>(s, n);
|
||||||
}
|
}
|
||||||
|
|
||||||
template<typename Stream>
|
template<typename Stream>
|
||||||
void Unserialize(Stream& s, int, int) {
|
void Unserialize(Stream& s) {
|
||||||
n = ReadVarInt<Stream,I>(s);
|
n = ReadVarInt<Stream,I>(s);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
@ -451,17 +428,13 @@ protected:
|
||||||
public:
|
public:
|
||||||
CCompactSize(uint64_t& nIn) : n(nIn) { }
|
CCompactSize(uint64_t& nIn) : n(nIn) { }
|
||||||
|
|
||||||
unsigned int GetSerializeSize(int, int) const {
|
|
||||||
return GetSizeOfCompactSize(n);
|
|
||||||
}
|
|
||||||
|
|
||||||
template<typename Stream>
|
template<typename Stream>
|
||||||
void Serialize(Stream &s, int, int) const {
|
void Serialize(Stream &s) const {
|
||||||
WriteCompactSize<Stream>(s, n);
|
WriteCompactSize<Stream>(s, n);
|
||||||
}
|
}
|
||||||
|
|
||||||
template<typename Stream>
|
template<typename Stream>
|
||||||
void Unserialize(Stream& s, int, int) {
|
void Unserialize(Stream& s) {
|
||||||
n = ReadCompactSize<Stream>(s);
|
n = ReadCompactSize<Stream>(s);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
@ -472,10 +445,10 @@ class LimitedString
|
||||||
protected:
|
protected:
|
||||||
std::string& string;
|
std::string& string;
|
||||||
public:
|
public:
|
||||||
LimitedString(std::string& string) : string(string) {}
|
LimitedString(std::string& _string) : string(_string) {}
|
||||||
|
|
||||||
template<typename Stream>
|
template<typename Stream>
|
||||||
void Unserialize(Stream& s, int, int=0)
|
void Unserialize(Stream& s)
|
||||||
{
|
{
|
||||||
size_t size = ReadCompactSize(s);
|
size_t size = ReadCompactSize(s);
|
||||||
if (size > Limit) {
|
if (size > Limit) {
|
||||||
|
@ -487,17 +460,12 @@ public:
|
||||||
}
|
}
|
||||||
|
|
||||||
template<typename Stream>
|
template<typename Stream>
|
||||||
void Serialize(Stream& s, int, int=0) const
|
void Serialize(Stream& s) const
|
||||||
{
|
{
|
||||||
WriteCompactSize(s, string.size());
|
WriteCompactSize(s, string.size());
|
||||||
if (!string.empty())
|
if (!string.empty())
|
||||||
s.write((char*)&string[0], string.size());
|
s.write((char*)&string[0], string.size());
|
||||||
}
|
}
|
||||||
|
|
||||||
unsigned int GetSerializeSize(int, int=0) const
|
|
||||||
{
|
|
||||||
return GetSizeOfCompactSize(string.size()) + string.size();
|
|
||||||
}
|
|
||||||
};
|
};
|
||||||
|
|
||||||
template<typename I>
|
template<typename I>
|
||||||
|
@ -510,58 +478,48 @@ CVarInt<I> WrapVarInt(I& n) { return CVarInt<I>(n); }
|
||||||
/**
|
/**
|
||||||
* string
|
* string
|
||||||
*/
|
*/
|
||||||
template<typename C> unsigned int GetSerializeSize(const std::basic_string<C>& str, int, int=0);
|
template<typename Stream, typename C> void Serialize(Stream& os, const std::basic_string<C>& str);
|
||||||
template<typename Stream, typename C> void Serialize(Stream& os, const std::basic_string<C>& str, int, int=0);
|
template<typename Stream, typename C> void Unserialize(Stream& is, std::basic_string<C>& str);
|
||||||
template<typename Stream, typename C> void Unserialize(Stream& is, std::basic_string<C>& str, int, int=0);
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* prevector
|
* prevector
|
||||||
* prevectors of unsigned char are a special case and are intended to be serialized as a single opaque blob.
|
* prevectors of unsigned char are a special case and are intended to be serialized as a single opaque blob.
|
||||||
*/
|
*/
|
||||||
template<unsigned int N, typename T> unsigned int GetSerializeSize_impl(const prevector<N, T>& v, int nType, int nVersion, const unsigned char&);
|
template<typename Stream, unsigned int N, typename T> void Serialize_impl(Stream& os, const prevector<N, T>& v, const unsigned char&);
|
||||||
template<unsigned int N, typename T, typename V> unsigned int GetSerializeSize_impl(const prevector<N, T>& v, int nType, int nVersion, const V&);
|
template<typename Stream, unsigned int N, typename T, typename V> void Serialize_impl(Stream& os, const prevector<N, T>& v, const V&);
|
||||||
template<unsigned int N, typename T> inline unsigned int GetSerializeSize(const prevector<N, T>& v, int nType, int nVersion);
|
template<typename Stream, unsigned int N, typename T> inline void Serialize(Stream& os, const prevector<N, T>& v);
|
||||||
template<typename Stream, unsigned int N, typename T> void Serialize_impl(Stream& os, const prevector<N, T>& v, int nType, int nVersion, const unsigned char&);
|
template<typename Stream, unsigned int N, typename T> void Unserialize_impl(Stream& is, prevector<N, T>& v, const unsigned char&);
|
||||||
template<typename Stream, unsigned int N, typename T, typename V> void Serialize_impl(Stream& os, const prevector<N, T>& v, int nType, int nVersion, const V&);
|
template<typename Stream, unsigned int N, typename T, typename V> void Unserialize_impl(Stream& is, prevector<N, T>& v, const V&);
|
||||||
template<typename Stream, unsigned int N, typename T> inline void Serialize(Stream& os, const prevector<N, T>& v, int nType, int nVersion);
|
template<typename Stream, unsigned int N, typename T> inline void Unserialize(Stream& is, prevector<N, T>& v);
|
||||||
template<typename Stream, unsigned int N, typename T> void Unserialize_impl(Stream& is, prevector<N, T>& v, int nType, int nVersion, const unsigned char&);
|
|
||||||
template<typename Stream, unsigned int N, typename T, typename V> void Unserialize_impl(Stream& is, prevector<N, T>& v, int nType, int nVersion, const V&);
|
|
||||||
template<typename Stream, unsigned int N, typename T> inline void Unserialize(Stream& is, prevector<N, T>& v, int nType, int nVersion);
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* vector
|
* vector
|
||||||
* vectors of unsigned char are a special case and are intended to be serialized as a single opaque blob.
|
* vectors of unsigned char are a special case and are intended to be serialized as a single opaque blob.
|
||||||
*/
|
*/
|
||||||
template<typename T, typename A> unsigned int GetSerializeSize_impl(const std::vector<T, A>& v, int nType, int nVersion, const unsigned char&);
|
template<typename Stream, typename T, typename A> void Serialize_impl(Stream& os, const std::vector<T, A>& v, const unsigned char&);
|
||||||
template<typename T, typename A, typename V> unsigned int GetSerializeSize_impl(const std::vector<T, A>& v, int nType, int nVersion, const V&);
|
template<typename Stream, typename T, typename A, typename V> void Serialize_impl(Stream& os, const std::vector<T, A>& v, const V&);
|
||||||
template<typename T, typename A> inline unsigned int GetSerializeSize(const std::vector<T, A>& v, int nType, int nVersion);
|
template<typename Stream, typename T, typename A> inline void Serialize(Stream& os, const std::vector<T, A>& v);
|
||||||
template<typename Stream, typename T, typename A> void Serialize_impl(Stream& os, const std::vector<T, A>& v, int nType, int nVersion, const unsigned char&);
|
template<typename Stream, typename T, typename A> void Unserialize_impl(Stream& is, std::vector<T, A>& v, const unsigned char&);
|
||||||
template<typename Stream, typename T, typename A, typename V> void Serialize_impl(Stream& os, const std::vector<T, A>& v, int nType, int nVersion, const V&);
|
template<typename Stream, typename T, typename A, typename V> void Unserialize_impl(Stream& is, std::vector<T, A>& v, const V&);
|
||||||
template<typename Stream, typename T, typename A> inline void Serialize(Stream& os, const std::vector<T, A>& v, int nType, int nVersion);
|
template<typename Stream, typename T, typename A> inline void Unserialize(Stream& is, std::vector<T, A>& v);
|
||||||
template<typename Stream, typename T, typename A> void Unserialize_impl(Stream& is, std::vector<T, A>& v, int nType, int nVersion, const unsigned char&);
|
|
||||||
template<typename Stream, typename T, typename A, typename V> void Unserialize_impl(Stream& is, std::vector<T, A>& v, int nType, int nVersion, const V&);
|
|
||||||
template<typename Stream, typename T, typename A> inline void Unserialize(Stream& is, std::vector<T, A>& v, int nType, int nVersion);
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* pair
|
* pair
|
||||||
*/
|
*/
|
||||||
template<typename K, typename T> unsigned int GetSerializeSize(const std::pair<K, T>& item, int nType, int nVersion);
|
template<typename Stream, typename K, typename T> void Serialize(Stream& os, const std::pair<K, T>& item);
|
||||||
template<typename Stream, typename K, typename T> void Serialize(Stream& os, const std::pair<K, T>& item, int nType, int nVersion);
|
template<typename Stream, typename K, typename T> void Unserialize(Stream& is, std::pair<K, T>& item);
|
||||||
template<typename Stream, typename K, typename T> void Unserialize(Stream& is, std::pair<K, T>& item, int nType, int nVersion);
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* map
|
* map
|
||||||
*/
|
*/
|
||||||
template<typename K, typename T, typename Pred, typename A> unsigned int GetSerializeSize(const std::map<K, T, Pred, A>& m, int nType, int nVersion);
|
template<typename Stream, typename K, typename T, typename Pred, typename A> void Serialize(Stream& os, const std::map<K, T, Pred, A>& m);
|
||||||
template<typename Stream, typename K, typename T, typename Pred, typename A> void Serialize(Stream& os, const std::map<K, T, Pred, A>& m, int nType, int nVersion);
|
template<typename Stream, typename K, typename T, typename Pred, typename A> void Unserialize(Stream& is, std::map<K, T, Pred, A>& m);
|
||||||
template<typename Stream, typename K, typename T, typename Pred, typename A> void Unserialize(Stream& is, std::map<K, T, Pred, A>& m, int nType, int nVersion);
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* set
|
* set
|
||||||
*/
|
*/
|
||||||
template<typename K, typename Pred, typename A> unsigned int GetSerializeSize(const std::set<K, Pred, A>& m, int nType, int nVersion);
|
template<typename Stream, typename K, typename Pred, typename A> void Serialize(Stream& os, const std::set<K, Pred, A>& m);
|
||||||
template<typename Stream, typename K, typename Pred, typename A> void Serialize(Stream& os, const std::set<K, Pred, A>& m, int nType, int nVersion);
|
template<typename Stream, typename K, typename Pred, typename A> void Unserialize(Stream& is, std::set<K, Pred, A>& m);
|
||||||
template<typename Stream, typename K, typename Pred, typename A> void Unserialize(Stream& is, std::set<K, Pred, A>& m, int nType, int nVersion);
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
@ -569,26 +527,17 @@ template<typename Stream, typename K, typename Pred, typename A> void Unserializ
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* If none of the specialized versions above matched, default to calling member function.
|
* If none of the specialized versions above matched, default to calling member function.
|
||||||
* "int nType" is changed to "long nType" to keep from getting an ambiguous overload error.
|
|
||||||
* The compiler will only cast int to long if none of the other templates matched.
|
|
||||||
* Thanks to Boost serialization for this idea.
|
|
||||||
*/
|
*/
|
||||||
template<typename T>
|
template<typename Stream, typename T>
|
||||||
inline unsigned int GetSerializeSize(const T& a, long nType, int nVersion)
|
inline void Serialize(Stream& os, const T& a)
|
||||||
{
|
{
|
||||||
return a.GetSerializeSize((int)nType, nVersion);
|
a.Serialize(os);
|
||||||
}
|
}
|
||||||
|
|
||||||
template<typename Stream, typename T>
|
template<typename Stream, typename T>
|
||||||
inline void Serialize(Stream& os, const T& a, long nType, int nVersion)
|
inline void Unserialize(Stream& is, T& a)
|
||||||
{
|
{
|
||||||
a.Serialize(os, (int)nType, nVersion);
|
a.Unserialize(is);
|
||||||
}
|
|
||||||
|
|
||||||
template<typename Stream, typename T>
|
|
||||||
inline void Unserialize(Stream& is, T& a, long nType, int nVersion)
|
|
||||||
{
|
|
||||||
a.Unserialize(is, (int)nType, nVersion);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -598,14 +547,8 @@ inline void Unserialize(Stream& is, T& a, long nType, int nVersion)
|
||||||
/**
|
/**
|
||||||
* string
|
* string
|
||||||
*/
|
*/
|
||||||
template<typename C>
|
|
||||||
unsigned int GetSerializeSize(const std::basic_string<C>& str, int, int)
|
|
||||||
{
|
|
||||||
return GetSizeOfCompactSize(str.size()) + str.size() * sizeof(str[0]);
|
|
||||||
}
|
|
||||||
|
|
||||||
template<typename Stream, typename C>
|
template<typename Stream, typename C>
|
||||||
void Serialize(Stream& os, const std::basic_string<C>& str, int, int)
|
void Serialize(Stream& os, const std::basic_string<C>& str)
|
||||||
{
|
{
|
||||||
WriteCompactSize(os, str.size());
|
WriteCompactSize(os, str.size());
|
||||||
if (!str.empty())
|
if (!str.empty())
|
||||||
|
@ -613,7 +556,7 @@ void Serialize(Stream& os, const std::basic_string<C>& str, int, int)
|
||||||
}
|
}
|
||||||
|
|
||||||
template<typename Stream, typename C>
|
template<typename Stream, typename C>
|
||||||
void Unserialize(Stream& is, std::basic_string<C>& str, int, int)
|
void Unserialize(Stream& is, std::basic_string<C>& str)
|
||||||
{
|
{
|
||||||
unsigned int nSize = ReadCompactSize(is);
|
unsigned int nSize = ReadCompactSize(is);
|
||||||
str.resize(nSize);
|
str.resize(nSize);
|
||||||
|
@ -626,30 +569,8 @@ void Unserialize(Stream& is, std::basic_string<C>& str, int, int)
|
||||||
/**
|
/**
|
||||||
* prevector
|
* prevector
|
||||||
*/
|
*/
|
||||||
template<unsigned int N, typename T>
|
|
||||||
unsigned int GetSerializeSize_impl(const prevector<N, T>& v, int nType, int nVersion, const unsigned char&)
|
|
||||||
{
|
|
||||||
return (GetSizeOfCompactSize(v.size()) + v.size() * sizeof(T));
|
|
||||||
}
|
|
||||||
|
|
||||||
template<unsigned int N, typename T, typename V>
|
|
||||||
unsigned int GetSerializeSize_impl(const prevector<N, T>& v, int nType, int nVersion, const V&)
|
|
||||||
{
|
|
||||||
unsigned int nSize = GetSizeOfCompactSize(v.size());
|
|
||||||
for (typename prevector<N, T>::const_iterator vi = v.begin(); vi != v.end(); ++vi)
|
|
||||||
nSize += GetSerializeSize((*vi), nType, nVersion);
|
|
||||||
return nSize;
|
|
||||||
}
|
|
||||||
|
|
||||||
template<unsigned int N, typename T>
|
|
||||||
inline unsigned int GetSerializeSize(const prevector<N, T>& v, int nType, int nVersion)
|
|
||||||
{
|
|
||||||
return GetSerializeSize_impl(v, nType, nVersion, T());
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
template<typename Stream, unsigned int N, typename T>
|
template<typename Stream, unsigned int N, typename T>
|
||||||
void Serialize_impl(Stream& os, const prevector<N, T>& v, int nType, int nVersion, const unsigned char&)
|
void Serialize_impl(Stream& os, const prevector<N, T>& v, const unsigned char&)
|
||||||
{
|
{
|
||||||
WriteCompactSize(os, v.size());
|
WriteCompactSize(os, v.size());
|
||||||
if (!v.empty())
|
if (!v.empty())
|
||||||
|
@ -657,22 +578,22 @@ void Serialize_impl(Stream& os, const prevector<N, T>& v, int nType, int nVersio
|
||||||
}
|
}
|
||||||
|
|
||||||
template<typename Stream, unsigned int N, typename T, typename V>
|
template<typename Stream, unsigned int N, typename T, typename V>
|
||||||
void Serialize_impl(Stream& os, const prevector<N, T>& v, int nType, int nVersion, const V&)
|
void Serialize_impl(Stream& os, const prevector<N, T>& v, const V&)
|
||||||
{
|
{
|
||||||
WriteCompactSize(os, v.size());
|
WriteCompactSize(os, v.size());
|
||||||
for (typename prevector<N, T>::const_iterator vi = v.begin(); vi != v.end(); ++vi)
|
for (typename prevector<N, T>::const_iterator vi = v.begin(); vi != v.end(); ++vi)
|
||||||
::Serialize(os, (*vi), nType, nVersion);
|
::Serialize(os, (*vi));
|
||||||
}
|
}
|
||||||
|
|
||||||
template<typename Stream, unsigned int N, typename T>
|
template<typename Stream, unsigned int N, typename T>
|
||||||
inline void Serialize(Stream& os, const prevector<N, T>& v, int nType, int nVersion)
|
inline void Serialize(Stream& os, const prevector<N, T>& v)
|
||||||
{
|
{
|
||||||
Serialize_impl(os, v, nType, nVersion, T());
|
Serialize_impl(os, v, T());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
template<typename Stream, unsigned int N, typename T>
|
template<typename Stream, unsigned int N, typename T>
|
||||||
void Unserialize_impl(Stream& is, prevector<N, T>& v, int nType, int nVersion, const unsigned char&)
|
void Unserialize_impl(Stream& is, prevector<N, T>& v, const unsigned char&)
|
||||||
{
|
{
|
||||||
// Limit size per read so bogus size value won't cause out of memory
|
// Limit size per read so bogus size value won't cause out of memory
|
||||||
v.clear();
|
v.clear();
|
||||||
|
@ -688,7 +609,7 @@ void Unserialize_impl(Stream& is, prevector<N, T>& v, int nType, int nVersion, c
|
||||||
}
|
}
|
||||||
|
|
||||||
template<typename Stream, unsigned int N, typename T, typename V>
|
template<typename Stream, unsigned int N, typename T, typename V>
|
||||||
void Unserialize_impl(Stream& is, prevector<N, T>& v, int nType, int nVersion, const V&)
|
void Unserialize_impl(Stream& is, prevector<N, T>& v, const V&)
|
||||||
{
|
{
|
||||||
v.clear();
|
v.clear();
|
||||||
unsigned int nSize = ReadCompactSize(is);
|
unsigned int nSize = ReadCompactSize(is);
|
||||||
|
@ -701,14 +622,14 @@ void Unserialize_impl(Stream& is, prevector<N, T>& v, int nType, int nVersion, c
|
||||||
nMid = nSize;
|
nMid = nSize;
|
||||||
v.resize(nMid);
|
v.resize(nMid);
|
||||||
for (; i < nMid; i++)
|
for (; i < nMid; i++)
|
||||||
Unserialize(is, v[i], nType, nVersion);
|
Unserialize(is, v[i]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
template<typename Stream, unsigned int N, typename T>
|
template<typename Stream, unsigned int N, typename T>
|
||||||
inline void Unserialize(Stream& is, prevector<N, T>& v, int nType, int nVersion)
|
inline void Unserialize(Stream& is, prevector<N, T>& v)
|
||||||
{
|
{
|
||||||
Unserialize_impl(is, v, nType, nVersion, T());
|
Unserialize_impl(is, v, T());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -716,30 +637,8 @@ inline void Unserialize(Stream& is, prevector<N, T>& v, int nType, int nVersion)
|
||||||
/**
|
/**
|
||||||
* vector
|
* vector
|
||||||
*/
|
*/
|
||||||
template<typename T, typename A>
|
|
||||||
unsigned int GetSerializeSize_impl(const std::vector<T, A>& v, int nType, int nVersion, const unsigned char&)
|
|
||||||
{
|
|
||||||
return (GetSizeOfCompactSize(v.size()) + v.size() * sizeof(T));
|
|
||||||
}
|
|
||||||
|
|
||||||
template<typename T, typename A, typename V>
|
|
||||||
unsigned int GetSerializeSize_impl(const std::vector<T, A>& v, int nType, int nVersion, const V&)
|
|
||||||
{
|
|
||||||
unsigned int nSize = GetSizeOfCompactSize(v.size());
|
|
||||||
for (typename std::vector<T, A>::const_iterator vi = v.begin(); vi != v.end(); ++vi)
|
|
||||||
nSize += GetSerializeSize((*vi), nType, nVersion);
|
|
||||||
return nSize;
|
|
||||||
}
|
|
||||||
|
|
||||||
template<typename T, typename A>
|
|
||||||
inline unsigned int GetSerializeSize(const std::vector<T, A>& v, int nType, int nVersion)
|
|
||||||
{
|
|
||||||
return GetSerializeSize_impl(v, nType, nVersion, T());
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
template<typename Stream, typename T, typename A>
|
template<typename Stream, typename T, typename A>
|
||||||
void Serialize_impl(Stream& os, const std::vector<T, A>& v, int nType, int nVersion, const unsigned char&)
|
void Serialize_impl(Stream& os, const std::vector<T, A>& v, const unsigned char&)
|
||||||
{
|
{
|
||||||
WriteCompactSize(os, v.size());
|
WriteCompactSize(os, v.size());
|
||||||
if (!v.empty())
|
if (!v.empty())
|
||||||
|
@ -747,22 +646,22 @@ void Serialize_impl(Stream& os, const std::vector<T, A>& v, int nType, int nVers
|
||||||
}
|
}
|
||||||
|
|
||||||
template<typename Stream, typename T, typename A, typename V>
|
template<typename Stream, typename T, typename A, typename V>
|
||||||
void Serialize_impl(Stream& os, const std::vector<T, A>& v, int nType, int nVersion, const V&)
|
void Serialize_impl(Stream& os, const std::vector<T, A>& v, const V&)
|
||||||
{
|
{
|
||||||
WriteCompactSize(os, v.size());
|
WriteCompactSize(os, v.size());
|
||||||
for (typename std::vector<T, A>::const_iterator vi = v.begin(); vi != v.end(); ++vi)
|
for (typename std::vector<T, A>::const_iterator vi = v.begin(); vi != v.end(); ++vi)
|
||||||
::Serialize(os, (*vi), nType, nVersion);
|
::Serialize(os, (*vi));
|
||||||
}
|
}
|
||||||
|
|
||||||
template<typename Stream, typename T, typename A>
|
template<typename Stream, typename T, typename A>
|
||||||
inline void Serialize(Stream& os, const std::vector<T, A>& v, int nType, int nVersion)
|
inline void Serialize(Stream& os, const std::vector<T, A>& v)
|
||||||
{
|
{
|
||||||
Serialize_impl(os, v, nType, nVersion, T());
|
Serialize_impl(os, v, T());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
template<typename Stream, typename T, typename A>
|
template<typename Stream, typename T, typename A>
|
||||||
void Unserialize_impl(Stream& is, std::vector<T, A>& v, int nType, int nVersion, const unsigned char&)
|
void Unserialize_impl(Stream& is, std::vector<T, A>& v, const unsigned char&)
|
||||||
{
|
{
|
||||||
// Limit size per read so bogus size value won't cause out of memory
|
// Limit size per read so bogus size value won't cause out of memory
|
||||||
v.clear();
|
v.clear();
|
||||||
|
@ -778,7 +677,7 @@ void Unserialize_impl(Stream& is, std::vector<T, A>& v, int nType, int nVersion,
|
||||||
}
|
}
|
||||||
|
|
||||||
template<typename Stream, typename T, typename A, typename V>
|
template<typename Stream, typename T, typename A, typename V>
|
||||||
void Unserialize_impl(Stream& is, std::vector<T, A>& v, int nType, int nVersion, const V&)
|
void Unserialize_impl(Stream& is, std::vector<T, A>& v, const V&)
|
||||||
{
|
{
|
||||||
v.clear();
|
v.clear();
|
||||||
unsigned int nSize = ReadCompactSize(is);
|
unsigned int nSize = ReadCompactSize(is);
|
||||||
|
@ -791,14 +690,14 @@ void Unserialize_impl(Stream& is, std::vector<T, A>& v, int nType, int nVersion,
|
||||||
nMid = nSize;
|
nMid = nSize;
|
||||||
v.resize(nMid);
|
v.resize(nMid);
|
||||||
for (; i < nMid; i++)
|
for (; i < nMid; i++)
|
||||||
Unserialize(is, v[i], nType, nVersion);
|
Unserialize(is, v[i]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
template<typename Stream, typename T, typename A>
|
template<typename Stream, typename T, typename A>
|
||||||
inline void Unserialize(Stream& is, std::vector<T, A>& v, int nType, int nVersion)
|
inline void Unserialize(Stream& is, std::vector<T, A>& v)
|
||||||
{
|
{
|
||||||
Unserialize_impl(is, v, nType, nVersion, T());
|
Unserialize_impl(is, v, T());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -806,24 +705,18 @@ inline void Unserialize(Stream& is, std::vector<T, A>& v, int nType, int nVersio
|
||||||
/**
|
/**
|
||||||
* pair
|
* pair
|
||||||
*/
|
*/
|
||||||
template<typename K, typename T>
|
template<typename Stream, typename K, typename T>
|
||||||
unsigned int GetSerializeSize(const std::pair<K, T>& item, int nType, int nVersion)
|
void Serialize(Stream& os, const std::pair<K, T>& item)
|
||||||
{
|
{
|
||||||
return GetSerializeSize(item.first, nType, nVersion) + GetSerializeSize(item.second, nType, nVersion);
|
Serialize(os, item.first);
|
||||||
|
Serialize(os, item.second);
|
||||||
}
|
}
|
||||||
|
|
||||||
template<typename Stream, typename K, typename T>
|
template<typename Stream, typename K, typename T>
|
||||||
void Serialize(Stream& os, const std::pair<K, T>& item, int nType, int nVersion)
|
void Unserialize(Stream& is, std::pair<K, T>& item)
|
||||||
{
|
{
|
||||||
Serialize(os, item.first, nType, nVersion);
|
Unserialize(is, item.first);
|
||||||
Serialize(os, item.second, nType, nVersion);
|
Unserialize(is, item.second);
|
||||||
}
|
|
||||||
|
|
||||||
template<typename Stream, typename K, typename T>
|
|
||||||
void Unserialize(Stream& is, std::pair<K, T>& item, int nType, int nVersion)
|
|
||||||
{
|
|
||||||
Unserialize(is, item.first, nType, nVersion);
|
|
||||||
Unserialize(is, item.second, nType, nVersion);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -831,25 +724,16 @@ void Unserialize(Stream& is, std::pair<K, T>& item, int nType, int nVersion)
|
||||||
/**
|
/**
|
||||||
* map
|
* map
|
||||||
*/
|
*/
|
||||||
template<typename K, typename T, typename Pred, typename A>
|
|
||||||
unsigned int GetSerializeSize(const std::map<K, T, Pred, A>& m, int nType, int nVersion)
|
|
||||||
{
|
|
||||||
unsigned int nSize = GetSizeOfCompactSize(m.size());
|
|
||||||
for (typename std::map<K, T, Pred, A>::const_iterator mi = m.begin(); mi != m.end(); ++mi)
|
|
||||||
nSize += GetSerializeSize((*mi), nType, nVersion);
|
|
||||||
return nSize;
|
|
||||||
}
|
|
||||||
|
|
||||||
template<typename Stream, typename K, typename T, typename Pred, typename A>
|
template<typename Stream, typename K, typename T, typename Pred, typename A>
|
||||||
void Serialize(Stream& os, const std::map<K, T, Pred, A>& m, int nType, int nVersion)
|
void Serialize(Stream& os, const std::map<K, T, Pred, A>& m)
|
||||||
{
|
{
|
||||||
WriteCompactSize(os, m.size());
|
WriteCompactSize(os, m.size());
|
||||||
for (typename std::map<K, T, Pred, A>::const_iterator mi = m.begin(); mi != m.end(); ++mi)
|
for (typename std::map<K, T, Pred, A>::const_iterator mi = m.begin(); mi != m.end(); ++mi)
|
||||||
Serialize(os, (*mi), nType, nVersion);
|
Serialize(os, (*mi));
|
||||||
}
|
}
|
||||||
|
|
||||||
template<typename Stream, typename K, typename T, typename Pred, typename A>
|
template<typename Stream, typename K, typename T, typename Pred, typename A>
|
||||||
void Unserialize(Stream& is, std::map<K, T, Pred, A>& m, int nType, int nVersion)
|
void Unserialize(Stream& is, std::map<K, T, Pred, A>& m)
|
||||||
{
|
{
|
||||||
m.clear();
|
m.clear();
|
||||||
unsigned int nSize = ReadCompactSize(is);
|
unsigned int nSize = ReadCompactSize(is);
|
||||||
|
@ -857,7 +741,7 @@ void Unserialize(Stream& is, std::map<K, T, Pred, A>& m, int nType, int nVersion
|
||||||
for (unsigned int i = 0; i < nSize; i++)
|
for (unsigned int i = 0; i < nSize; i++)
|
||||||
{
|
{
|
||||||
std::pair<K, T> item;
|
std::pair<K, T> item;
|
||||||
Unserialize(is, item, nType, nVersion);
|
Unserialize(is, item);
|
||||||
mi = m.insert(mi, item);
|
mi = m.insert(mi, item);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -867,25 +751,16 @@ void Unserialize(Stream& is, std::map<K, T, Pred, A>& m, int nType, int nVersion
|
||||||
/**
|
/**
|
||||||
* set
|
* set
|
||||||
*/
|
*/
|
||||||
template<typename K, typename Pred, typename A>
|
|
||||||
unsigned int GetSerializeSize(const std::set<K, Pred, A>& m, int nType, int nVersion)
|
|
||||||
{
|
|
||||||
unsigned int nSize = GetSizeOfCompactSize(m.size());
|
|
||||||
for (typename std::set<K, Pred, A>::const_iterator it = m.begin(); it != m.end(); ++it)
|
|
||||||
nSize += GetSerializeSize((*it), nType, nVersion);
|
|
||||||
return nSize;
|
|
||||||
}
|
|
||||||
|
|
||||||
template<typename Stream, typename K, typename Pred, typename A>
|
template<typename Stream, typename K, typename Pred, typename A>
|
||||||
void Serialize(Stream& os, const std::set<K, Pred, A>& m, int nType, int nVersion)
|
void Serialize(Stream& os, const std::set<K, Pred, A>& m)
|
||||||
{
|
{
|
||||||
WriteCompactSize(os, m.size());
|
WriteCompactSize(os, m.size());
|
||||||
for (typename std::set<K, Pred, A>::const_iterator it = m.begin(); it != m.end(); ++it)
|
for (typename std::set<K, Pred, A>::const_iterator it = m.begin(); it != m.end(); ++it)
|
||||||
Serialize(os, (*it), nType, nVersion);
|
Serialize(os, (*it));
|
||||||
}
|
}
|
||||||
|
|
||||||
template<typename Stream, typename K, typename Pred, typename A>
|
template<typename Stream, typename K, typename Pred, typename A>
|
||||||
void Unserialize(Stream& is, std::set<K, Pred, A>& m, int nType, int nVersion)
|
void Unserialize(Stream& is, std::set<K, Pred, A>& m)
|
||||||
{
|
{
|
||||||
m.clear();
|
m.clear();
|
||||||
unsigned int nSize = ReadCompactSize(is);
|
unsigned int nSize = ReadCompactSize(is);
|
||||||
|
@ -893,7 +768,7 @@ void Unserialize(Stream& is, std::set<K, Pred, A>& m, int nType, int nVersion)
|
||||||
for (unsigned int i = 0; i < nSize; i++)
|
for (unsigned int i = 0; i < nSize; i++)
|
||||||
{
|
{
|
||||||
K key;
|
K key;
|
||||||
Unserialize(is, key, nType, nVersion);
|
Unserialize(is, key);
|
||||||
it = m.insert(it, key);
|
it = m.insert(it, key);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -905,23 +780,23 @@ void Unserialize(Stream& is, std::set<K, Pred, A>& m, int nType, int nVersion)
|
||||||
*/
|
*/
|
||||||
struct CSerActionSerialize
|
struct CSerActionSerialize
|
||||||
{
|
{
|
||||||
bool ForRead() const { return false; }
|
constexpr bool ForRead() const { return false; }
|
||||||
};
|
};
|
||||||
struct CSerActionUnserialize
|
struct CSerActionUnserialize
|
||||||
{
|
{
|
||||||
bool ForRead() const { return true; }
|
constexpr bool ForRead() const { return true; }
|
||||||
};
|
};
|
||||||
|
|
||||||
template<typename Stream, typename T>
|
template<typename Stream, typename T>
|
||||||
inline void SerReadWrite(Stream& s, const T& obj, int nType, int nVersion, CSerActionSerialize ser_action)
|
inline void SerReadWrite(Stream& s, const T& obj, CSerActionSerialize ser_action)
|
||||||
{
|
{
|
||||||
::Serialize(s, obj, nType, nVersion);
|
::Serialize(s, obj);
|
||||||
}
|
}
|
||||||
|
|
||||||
template<typename Stream, typename T>
|
template<typename Stream, typename T>
|
||||||
inline void SerReadWrite(Stream& s, T& obj, int nType, int nVersion, CSerActionUnserialize ser_action)
|
inline void SerReadWrite(Stream& s, T& obj, CSerActionUnserialize ser_action)
|
||||||
{
|
{
|
||||||
::Unserialize(s, obj, nType, nVersion);
|
::Unserialize(s, obj);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -932,81 +807,122 @@ inline void SerReadWrite(Stream& s, T& obj, int nType, int nVersion, CSerActionU
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
/* ::GetSerializeSize implementations
|
||||||
|
*
|
||||||
|
* Computing the serialized size of objects is done through a special stream
|
||||||
|
* object of type CSizeComputer, which only records the number of bytes written
|
||||||
|
* to it.
|
||||||
|
*
|
||||||
|
* If your Serialize or SerializationOp method has non-trivial overhead for
|
||||||
|
* serialization, it may be worthwhile to implement a specialized version for
|
||||||
|
* CSizeComputer, which uses the s.seek() method to record bytes that would
|
||||||
|
* be written instead.
|
||||||
|
*/
|
||||||
class CSizeComputer
|
class CSizeComputer
|
||||||
{
|
{
|
||||||
protected:
|
protected:
|
||||||
size_t nSize;
|
size_t nSize;
|
||||||
|
|
||||||
|
const int nType;
|
||||||
|
const int nVersion;
|
||||||
public:
|
public:
|
||||||
int nType;
|
|
||||||
int nVersion;
|
|
||||||
|
|
||||||
CSizeComputer(int nTypeIn, int nVersionIn) : nSize(0), nType(nTypeIn), nVersion(nVersionIn) {}
|
CSizeComputer(int nTypeIn, int nVersionIn) : nSize(0), nType(nTypeIn), nVersion(nVersionIn) {}
|
||||||
|
|
||||||
CSizeComputer& write(const char *psz, size_t nSize)
|
void write(const char *psz, size_t _nSize)
|
||||||
{
|
{
|
||||||
this->nSize += nSize;
|
this->nSize += _nSize;
|
||||||
return *this;
|
}
|
||||||
|
|
||||||
|
/** Pretend _nSize bytes are written, without specifying them. */
|
||||||
|
void seek(size_t _nSize)
|
||||||
|
{
|
||||||
|
this->nSize += _nSize;
|
||||||
}
|
}
|
||||||
|
|
||||||
template<typename T>
|
template<typename T>
|
||||||
CSizeComputer& operator<<(const T& obj)
|
CSizeComputer& operator<<(const T& obj)
|
||||||
{
|
{
|
||||||
::Serialize(*this, obj, nType, nVersion);
|
::Serialize(*this, obj);
|
||||||
return (*this);
|
return (*this);
|
||||||
}
|
}
|
||||||
|
|
||||||
size_t size() const {
|
size_t size() const {
|
||||||
return nSize;
|
return nSize;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
int GetVersion() const { return nVersion; }
|
||||||
|
int GetType() const { return nType; }
|
||||||
};
|
};
|
||||||
|
|
||||||
template<typename Stream>
|
template<typename Stream>
|
||||||
void SerializeMany(Stream& s, int nType, int nVersion)
|
void SerializeMany(Stream& s)
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
template<typename Stream, typename Arg>
|
template<typename Stream, typename Arg>
|
||||||
void SerializeMany(Stream& s, int nType, int nVersion, Arg&& arg)
|
void SerializeMany(Stream& s, Arg&& arg)
|
||||||
{
|
{
|
||||||
::Serialize(s, std::forward<Arg>(arg), nType, nVersion);
|
::Serialize(s, std::forward<Arg>(arg));
|
||||||
}
|
}
|
||||||
|
|
||||||
template<typename Stream, typename Arg, typename... Args>
|
template<typename Stream, typename Arg, typename... Args>
|
||||||
void SerializeMany(Stream& s, int nType, int nVersion, Arg&& arg, Args&&... args)
|
void SerializeMany(Stream& s, Arg&& arg, Args&&... args)
|
||||||
{
|
{
|
||||||
::Serialize(s, std::forward<Arg>(arg), nType, nVersion);
|
::Serialize(s, std::forward<Arg>(arg));
|
||||||
::SerializeMany(s, nType, nVersion, std::forward<Args>(args)...);
|
::SerializeMany(s, std::forward<Args>(args)...);
|
||||||
}
|
}
|
||||||
|
|
||||||
template<typename Stream>
|
template<typename Stream>
|
||||||
inline void UnserializeMany(Stream& s, int nType, int nVersion)
|
inline void UnserializeMany(Stream& s)
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
template<typename Stream, typename Arg>
|
template<typename Stream, typename Arg>
|
||||||
inline void UnserializeMany(Stream& s, int nType, int nVersion, Arg& arg)
|
inline void UnserializeMany(Stream& s, Arg& arg)
|
||||||
{
|
{
|
||||||
::Unserialize(s, arg, nType, nVersion);
|
::Unserialize(s, arg);
|
||||||
}
|
}
|
||||||
|
|
||||||
template<typename Stream, typename Arg, typename... Args>
|
template<typename Stream, typename Arg, typename... Args>
|
||||||
inline void UnserializeMany(Stream& s, int nType, int nVersion, Arg& arg, Args&... args)
|
inline void UnserializeMany(Stream& s, Arg& arg, Args&... args)
|
||||||
{
|
{
|
||||||
::Unserialize(s, arg, nType, nVersion);
|
::Unserialize(s, arg);
|
||||||
::UnserializeMany(s, nType, nVersion, args...);
|
::UnserializeMany(s, args...);
|
||||||
}
|
}
|
||||||
|
|
||||||
template<typename Stream, typename... Args>
|
template<typename Stream, typename... Args>
|
||||||
inline void SerReadWriteMany(Stream& s, int nType, int nVersion, CSerActionSerialize ser_action, Args&&... args)
|
inline void SerReadWriteMany(Stream& s, CSerActionSerialize ser_action, Args&&... args)
|
||||||
{
|
{
|
||||||
::SerializeMany(s, nType, nVersion, std::forward<Args>(args)...);
|
::SerializeMany(s, std::forward<Args>(args)...);
|
||||||
}
|
}
|
||||||
|
|
||||||
template<typename Stream, typename... Args>
|
template<typename Stream, typename... Args>
|
||||||
inline void SerReadWriteMany(Stream& s, int nType, int nVersion, CSerActionUnserialize ser_action, Args&... args)
|
inline void SerReadWriteMany(Stream& s, CSerActionUnserialize ser_action, Args&... args)
|
||||||
{
|
{
|
||||||
::UnserializeMany(s, nType, nVersion, args...);
|
::UnserializeMany(s, args...);
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename I>
|
||||||
|
inline void WriteVarInt(CSizeComputer &s, I n)
|
||||||
|
{
|
||||||
|
s.seek(GetSizeOfVarInt<I>(n));
|
||||||
|
}
|
||||||
|
|
||||||
|
inline void WriteCompactSize(CSizeComputer &s, uint64_t nSize)
|
||||||
|
{
|
||||||
|
s.seek(GetSizeOfCompactSize(nSize));
|
||||||
|
}
|
||||||
|
|
||||||
|
template <typename T>
|
||||||
|
size_t GetSerializeSize(const T& t, int nType, int nVersion = 0)
|
||||||
|
{
|
||||||
|
return (CSizeComputer(nType, nVersion) << t).size();
|
||||||
|
}
|
||||||
|
|
||||||
|
template <typename S, typename T>
|
||||||
|
size_t GetSerializeSize(const S& s, const T& t)
|
||||||
|
{
|
||||||
|
return (CSizeComputer(s.GetType(), s.GetVersion()) << t).size();
|
||||||
}
|
}
|
||||||
|
|
||||||
#endif // BITCOIN_SERIALIZE_H
|
#endif // BITCOIN_SERIALIZE_H
|
||||||
|
|
111
src/streams.h
111
src/streams.h
|
@ -26,17 +26,18 @@ template<typename Stream>
|
||||||
class OverrideStream
|
class OverrideStream
|
||||||
{
|
{
|
||||||
Stream* stream;
|
Stream* stream;
|
||||||
public:
|
|
||||||
const int nType;
|
const int nType;
|
||||||
const int nVersion;
|
const int nVersion;
|
||||||
|
|
||||||
|
public:
|
||||||
OverrideStream(Stream* stream_, int nType_, int nVersion_) : stream(stream_), nType(nType_), nVersion(nVersion_) {}
|
OverrideStream(Stream* stream_, int nType_, int nVersion_) : stream(stream_), nType(nType_), nVersion(nVersion_) {}
|
||||||
|
|
||||||
template<typename T>
|
template<typename T>
|
||||||
OverrideStream<Stream>& operator<<(const T& obj)
|
OverrideStream<Stream>& operator<<(const T& obj)
|
||||||
{
|
{
|
||||||
// Serialize to this stream
|
// Serialize to this stream
|
||||||
::Serialize(*this->stream, obj, nType, nVersion);
|
::Serialize(*this, obj);
|
||||||
return (*this);
|
return (*this);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -44,9 +45,22 @@ public:
|
||||||
OverrideStream<Stream>& operator>>(T& obj)
|
OverrideStream<Stream>& operator>>(T& obj)
|
||||||
{
|
{
|
||||||
// Unserialize from this stream
|
// Unserialize from this stream
|
||||||
::Unserialize(*this->stream, obj, nType, nVersion);
|
::Unserialize(*this, obj);
|
||||||
return (*this);
|
return (*this);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void write(const char* pch, size_t nSize)
|
||||||
|
{
|
||||||
|
stream->write(pch, nSize);
|
||||||
|
}
|
||||||
|
|
||||||
|
void read(char* pch, size_t nSize)
|
||||||
|
{
|
||||||
|
stream->read(pch, nSize);
|
||||||
|
}
|
||||||
|
|
||||||
|
int GetVersion() const { return nVersion; }
|
||||||
|
int GetType() const { return nType; }
|
||||||
};
|
};
|
||||||
|
|
||||||
template<typename S>
|
template<typename S>
|
||||||
|
@ -66,9 +80,10 @@ protected:
|
||||||
typedef CSerializeData vector_type;
|
typedef CSerializeData vector_type;
|
||||||
vector_type vch;
|
vector_type vch;
|
||||||
unsigned int nReadPos;
|
unsigned int nReadPos;
|
||||||
public:
|
|
||||||
int nType;
|
int nType;
|
||||||
int nVersion;
|
int nVersion;
|
||||||
|
public:
|
||||||
|
|
||||||
typedef vector_type::allocator_type allocator_type;
|
typedef vector_type::allocator_type allocator_type;
|
||||||
typedef vector_type::size_type size_type;
|
typedef vector_type::size_type size_type;
|
||||||
|
@ -116,7 +131,7 @@ public:
|
||||||
CDataStream(int nTypeIn, int nVersionIn, Args&&... args)
|
CDataStream(int nTypeIn, int nVersionIn, Args&&... args)
|
||||||
{
|
{
|
||||||
Init(nTypeIn, nVersionIn);
|
Init(nTypeIn, nVersionIn);
|
||||||
::SerializeMany(*this, nType, nVersion, std::forward<Args>(args)...);
|
::SerializeMany(*this, std::forward<Args>(args)...);
|
||||||
}
|
}
|
||||||
|
|
||||||
void Init(int nTypeIn, int nVersionIn)
|
void Init(int nTypeIn, int nVersionIn)
|
||||||
|
@ -251,13 +266,11 @@ public:
|
||||||
int in_avail() { return size(); }
|
int in_avail() { return size(); }
|
||||||
|
|
||||||
void SetType(int n) { nType = n; }
|
void SetType(int n) { nType = n; }
|
||||||
int GetType() { return nType; }
|
int GetType() const { return nType; }
|
||||||
void SetVersion(int n) { nVersion = n; }
|
void SetVersion(int n) { nVersion = n; }
|
||||||
int GetVersion() { return nVersion; }
|
int GetVersion() const { return nVersion; }
|
||||||
void ReadVersion() { *this >> nVersion; }
|
|
||||||
void WriteVersion() { *this << nVersion; }
|
|
||||||
|
|
||||||
CDataStream& read(char* pch, size_t nSize)
|
void read(char* pch, size_t nSize)
|
||||||
{
|
{
|
||||||
// Read from the beginning of the buffer
|
// Read from the beginning of the buffer
|
||||||
unsigned int nReadPosNext = nReadPos + nSize;
|
unsigned int nReadPosNext = nReadPos + nSize;
|
||||||
|
@ -270,14 +283,13 @@ public:
|
||||||
memcpy(pch, &vch[nReadPos], nSize);
|
memcpy(pch, &vch[nReadPos], nSize);
|
||||||
nReadPos = 0;
|
nReadPos = 0;
|
||||||
vch.clear();
|
vch.clear();
|
||||||
return (*this);
|
return;
|
||||||
}
|
}
|
||||||
memcpy(pch, &vch[nReadPos], nSize);
|
memcpy(pch, &vch[nReadPos], nSize);
|
||||||
nReadPos = nReadPosNext;
|
nReadPos = nReadPosNext;
|
||||||
return (*this);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
CDataStream& ignore(int nSize)
|
void ignore(int nSize)
|
||||||
{
|
{
|
||||||
// Ignore from the beginning of the buffer
|
// Ignore from the beginning of the buffer
|
||||||
if (nSize < 0) {
|
if (nSize < 0) {
|
||||||
|
@ -290,39 +302,30 @@ public:
|
||||||
throw std::ios_base::failure("CDataStream::ignore(): end of data");
|
throw std::ios_base::failure("CDataStream::ignore(): end of data");
|
||||||
nReadPos = 0;
|
nReadPos = 0;
|
||||||
vch.clear();
|
vch.clear();
|
||||||
return (*this);
|
return;
|
||||||
}
|
}
|
||||||
nReadPos = nReadPosNext;
|
nReadPos = nReadPosNext;
|
||||||
return (*this);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
CDataStream& write(const char* pch, size_t nSize)
|
void write(const char* pch, size_t nSize)
|
||||||
{
|
{
|
||||||
// Write to the end of the buffer
|
// Write to the end of the buffer
|
||||||
vch.insert(vch.end(), pch, pch + nSize);
|
vch.insert(vch.end(), pch, pch + nSize);
|
||||||
return (*this);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
template<typename Stream>
|
template<typename Stream>
|
||||||
void Serialize(Stream& s, int nType, int nVersion) const
|
void Serialize(Stream& s) const
|
||||||
{
|
{
|
||||||
// Special case: stream << stream concatenates like stream += stream
|
// Special case: stream << stream concatenates like stream += stream
|
||||||
if (!vch.empty())
|
if (!vch.empty())
|
||||||
s.write((char*)&vch[0], vch.size() * sizeof(vch[0]));
|
s.write((char*)&vch[0], vch.size() * sizeof(vch[0]));
|
||||||
}
|
}
|
||||||
|
|
||||||
template<typename T>
|
|
||||||
unsigned int GetSerializeSize(const T& obj)
|
|
||||||
{
|
|
||||||
// Tells the size of the object if serialized to this stream
|
|
||||||
return ::GetSerializeSize(obj, nType, nVersion);
|
|
||||||
}
|
|
||||||
|
|
||||||
template<typename T>
|
template<typename T>
|
||||||
CDataStream& operator<<(const T& obj)
|
CDataStream& operator<<(const T& obj)
|
||||||
{
|
{
|
||||||
// Serialize to this stream
|
// Serialize to this stream
|
||||||
::Serialize(*this, obj, nType, nVersion);
|
::Serialize(*this, obj);
|
||||||
return (*this);
|
return (*this);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -330,7 +333,7 @@ public:
|
||||||
CDataStream& operator>>(T& obj)
|
CDataStream& operator>>(T& obj)
|
||||||
{
|
{
|
||||||
// Unserialize from this stream
|
// Unserialize from this stream
|
||||||
::Unserialize(*this, obj, nType, nVersion);
|
::Unserialize(*this, obj);
|
||||||
return (*this);
|
return (*this);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -385,17 +388,15 @@ private:
|
||||||
CAutoFile(const CAutoFile&);
|
CAutoFile(const CAutoFile&);
|
||||||
CAutoFile& operator=(const CAutoFile&);
|
CAutoFile& operator=(const CAutoFile&);
|
||||||
|
|
||||||
int nType;
|
const int nType;
|
||||||
int nVersion;
|
const int nVersion;
|
||||||
|
|
||||||
FILE* file;
|
FILE* file;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
CAutoFile(FILE* filenew, int nTypeIn, int nVersionIn)
|
CAutoFile(FILE* filenew, int nTypeIn, int nVersionIn) : nType(nTypeIn), nVersion(nVersionIn)
|
||||||
{
|
{
|
||||||
file = filenew;
|
file = filenew;
|
||||||
nType = nTypeIn;
|
|
||||||
nVersion = nVersionIn;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
~CAutoFile()
|
~CAutoFile()
|
||||||
|
@ -430,23 +431,18 @@ public:
|
||||||
//
|
//
|
||||||
// Stream subset
|
// Stream subset
|
||||||
//
|
//
|
||||||
void SetType(int n) { nType = n; }
|
int GetType() const { return nType; }
|
||||||
int GetType() { return nType; }
|
int GetVersion() const { return nVersion; }
|
||||||
void SetVersion(int n) { nVersion = n; }
|
|
||||||
int GetVersion() { return nVersion; }
|
|
||||||
void ReadVersion() { *this >> nVersion; }
|
|
||||||
void WriteVersion() { *this << nVersion; }
|
|
||||||
|
|
||||||
CAutoFile& read(char* pch, size_t nSize)
|
void read(char* pch, size_t nSize)
|
||||||
{
|
{
|
||||||
if (!file)
|
if (!file)
|
||||||
throw std::ios_base::failure("CAutoFile::read: file handle is NULL");
|
throw std::ios_base::failure("CAutoFile::read: file handle is NULL");
|
||||||
if (fread(pch, 1, nSize, file) != nSize)
|
if (fread(pch, 1, nSize, file) != nSize)
|
||||||
throw std::ios_base::failure(feof(file) ? "CAutoFile::read: end of file" : "CAutoFile::read: fread failed");
|
throw std::ios_base::failure(feof(file) ? "CAutoFile::read: end of file" : "CAutoFile::read: fread failed");
|
||||||
return (*this);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
CAutoFile& ignore(size_t nSize)
|
void ignore(size_t nSize)
|
||||||
{
|
{
|
||||||
if (!file)
|
if (!file)
|
||||||
throw std::ios_base::failure("CAutoFile::ignore: file handle is NULL");
|
throw std::ios_base::failure("CAutoFile::ignore: file handle is NULL");
|
||||||
|
@ -457,23 +453,14 @@ public:
|
||||||
throw std::ios_base::failure(feof(file) ? "CAutoFile::ignore: end of file" : "CAutoFile::read: fread failed");
|
throw std::ios_base::failure(feof(file) ? "CAutoFile::ignore: end of file" : "CAutoFile::read: fread failed");
|
||||||
nSize -= nNow;
|
nSize -= nNow;
|
||||||
}
|
}
|
||||||
return (*this);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
CAutoFile& write(const char* pch, size_t nSize)
|
void write(const char* pch, size_t nSize)
|
||||||
{
|
{
|
||||||
if (!file)
|
if (!file)
|
||||||
throw std::ios_base::failure("CAutoFile::write: file handle is NULL");
|
throw std::ios_base::failure("CAutoFile::write: file handle is NULL");
|
||||||
if (fwrite(pch, 1, nSize, file) != nSize)
|
if (fwrite(pch, 1, nSize, file) != nSize)
|
||||||
throw std::ios_base::failure("CAutoFile::write: write failed");
|
throw std::ios_base::failure("CAutoFile::write: write failed");
|
||||||
return (*this);
|
|
||||||
}
|
|
||||||
|
|
||||||
template<typename T>
|
|
||||||
unsigned int GetSerializeSize(const T& obj)
|
|
||||||
{
|
|
||||||
// Tells the size of the object if serialized to this stream
|
|
||||||
return ::GetSerializeSize(obj, nType, nVersion);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
template<typename T>
|
template<typename T>
|
||||||
|
@ -482,7 +469,7 @@ public:
|
||||||
// Serialize to this stream
|
// Serialize to this stream
|
||||||
if (!file)
|
if (!file)
|
||||||
throw std::ios_base::failure("CAutoFile::operator<<: file handle is NULL");
|
throw std::ios_base::failure("CAutoFile::operator<<: file handle is NULL");
|
||||||
::Serialize(*this, obj, nType, nVersion);
|
::Serialize(*this, obj);
|
||||||
return (*this);
|
return (*this);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -492,7 +479,7 @@ public:
|
||||||
// Unserialize from this stream
|
// Unserialize from this stream
|
||||||
if (!file)
|
if (!file)
|
||||||
throw std::ios_base::failure("CAutoFile::operator>>: file handle is NULL");
|
throw std::ios_base::failure("CAutoFile::operator>>: file handle is NULL");
|
||||||
::Unserialize(*this, obj, nType, nVersion);
|
::Unserialize(*this, obj);
|
||||||
return (*this);
|
return (*this);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
@ -510,8 +497,8 @@ private:
|
||||||
CBufferedFile(const CBufferedFile&);
|
CBufferedFile(const CBufferedFile&);
|
||||||
CBufferedFile& operator=(const CBufferedFile&);
|
CBufferedFile& operator=(const CBufferedFile&);
|
||||||
|
|
||||||
int nType;
|
const int nType;
|
||||||
int nVersion;
|
const int nVersion;
|
||||||
|
|
||||||
FILE *src; // source file
|
FILE *src; // source file
|
||||||
uint64_t nSrcPos; // how many bytes have been read from source
|
uint64_t nSrcPos; // how many bytes have been read from source
|
||||||
|
@ -541,11 +528,9 @@ protected:
|
||||||
|
|
||||||
public:
|
public:
|
||||||
CBufferedFile(FILE *fileIn, uint64_t nBufSize, uint64_t nRewindIn, int nTypeIn, int nVersionIn) :
|
CBufferedFile(FILE *fileIn, uint64_t nBufSize, uint64_t nRewindIn, int nTypeIn, int nVersionIn) :
|
||||||
nSrcPos(0), nReadPos(0), nReadLimit((uint64_t)(-1)), nRewind(nRewindIn), vchBuf(nBufSize, 0)
|
nType(nTypeIn), nVersion(nVersionIn), nSrcPos(0), nReadPos(0), nReadLimit((uint64_t)(-1)), nRewind(nRewindIn), vchBuf(nBufSize, 0)
|
||||||
{
|
{
|
||||||
src = fileIn;
|
src = fileIn;
|
||||||
nType = nTypeIn;
|
|
||||||
nVersion = nVersionIn;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
~CBufferedFile()
|
~CBufferedFile()
|
||||||
|
@ -553,6 +538,9 @@ public:
|
||||||
fclose();
|
fclose();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
int GetVersion() const { return nVersion; }
|
||||||
|
int GetType() const { return nType; }
|
||||||
|
|
||||||
void fclose()
|
void fclose()
|
||||||
{
|
{
|
||||||
if (src) {
|
if (src) {
|
||||||
|
@ -567,7 +555,7 @@ public:
|
||||||
}
|
}
|
||||||
|
|
||||||
// read a number of bytes
|
// read a number of bytes
|
||||||
CBufferedFile& read(char *pch, size_t nSize) {
|
void read(char *pch, size_t nSize) {
|
||||||
if (nSize + nReadPos > nReadLimit)
|
if (nSize + nReadPos > nReadLimit)
|
||||||
throw std::ios_base::failure("Read attempted past buffer limit");
|
throw std::ios_base::failure("Read attempted past buffer limit");
|
||||||
if (nSize + nRewind > vchBuf.size())
|
if (nSize + nRewind > vchBuf.size())
|
||||||
|
@ -586,7 +574,6 @@ public:
|
||||||
pch += nNow;
|
pch += nNow;
|
||||||
nSize -= nNow;
|
nSize -= nNow;
|
||||||
}
|
}
|
||||||
return (*this);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// return the current reading position
|
// return the current reading position
|
||||||
|
@ -632,7 +619,7 @@ public:
|
||||||
template<typename T>
|
template<typename T>
|
||||||
CBufferedFile& operator>>(T& obj) {
|
CBufferedFile& operator>>(T& obj) {
|
||||||
// Unserialize from this stream
|
// Unserialize from this stream
|
||||||
::Unserialize(*this, obj, nType, nVersion);
|
::Unserialize(*this, obj);
|
||||||
return (*this);
|
return (*this);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -129,7 +129,7 @@ public:
|
||||||
ADD_SERIALIZE_METHODS;
|
ADD_SERIALIZE_METHODS;
|
||||||
|
|
||||||
template <typename Stream, typename Operation>
|
template <typename Stream, typename Operation>
|
||||||
inline void SerializationOp(Stream& s, Operation ser_action, int nType, int nVersion) {
|
inline void SerializationOp(Stream& s, Operation ser_action) {
|
||||||
READWRITE(header);
|
READWRITE(header);
|
||||||
READWRITE(nonce);
|
READWRITE(nonce);
|
||||||
size_t shorttxids_size = shorttxids.size();
|
size_t shorttxids_size = shorttxids.size();
|
||||||
|
|
|
@ -41,7 +41,7 @@ BOOST_AUTO_TEST_CASE(bloom_create_insert_serialize)
|
||||||
BOOST_CHECK_MESSAGE(filter.contains(ParseHex("b9300670b4c5366e95b2699e8b18bc75e5f729c5")), "BloomFilter doesn't contain just-inserted object (3)!");
|
BOOST_CHECK_MESSAGE(filter.contains(ParseHex("b9300670b4c5366e95b2699e8b18bc75e5f729c5")), "BloomFilter doesn't contain just-inserted object (3)!");
|
||||||
|
|
||||||
CDataStream stream(SER_NETWORK, PROTOCOL_VERSION);
|
CDataStream stream(SER_NETWORK, PROTOCOL_VERSION);
|
||||||
filter.Serialize(stream, SER_NETWORK, PROTOCOL_VERSION);
|
stream << filter;
|
||||||
|
|
||||||
vector<unsigned char> vch = ParseHex("03614e9b050000000000000001");
|
vector<unsigned char> vch = ParseHex("03614e9b050000000000000001");
|
||||||
vector<char> expected(vch.size());
|
vector<char> expected(vch.size());
|
||||||
|
@ -73,7 +73,7 @@ BOOST_AUTO_TEST_CASE(bloom_create_insert_serialize_with_tweak)
|
||||||
BOOST_CHECK_MESSAGE(filter.contains(ParseHex("b9300670b4c5366e95b2699e8b18bc75e5f729c5")), "BloomFilter doesn't contain just-inserted object (3)!");
|
BOOST_CHECK_MESSAGE(filter.contains(ParseHex("b9300670b4c5366e95b2699e8b18bc75e5f729c5")), "BloomFilter doesn't contain just-inserted object (3)!");
|
||||||
|
|
||||||
CDataStream stream(SER_NETWORK, PROTOCOL_VERSION);
|
CDataStream stream(SER_NETWORK, PROTOCOL_VERSION);
|
||||||
filter.Serialize(stream, SER_NETWORK, PROTOCOL_VERSION);
|
stream << filter;
|
||||||
|
|
||||||
vector<unsigned char> vch = ParseHex("03ce4299050000000100008001");
|
vector<unsigned char> vch = ParseHex("03ce4299050000000100008001");
|
||||||
vector<char> expected(vch.size());
|
vector<char> expected(vch.size());
|
||||||
|
@ -100,7 +100,7 @@ BOOST_AUTO_TEST_CASE(bloom_create_insert_key)
|
||||||
filter.insert(vector<unsigned char>(hash.begin(), hash.end()));
|
filter.insert(vector<unsigned char>(hash.begin(), hash.end()));
|
||||||
|
|
||||||
CDataStream stream(SER_NETWORK, PROTOCOL_VERSION);
|
CDataStream stream(SER_NETWORK, PROTOCOL_VERSION);
|
||||||
filter.Serialize(stream, SER_NETWORK, PROTOCOL_VERSION);
|
stream << filter;
|
||||||
|
|
||||||
vector<unsigned char> vch = ParseHex("038fc16b080000000000000001");
|
vector<unsigned char> vch = ParseHex("038fc16b080000000000000001");
|
||||||
vector<char> expected(vch.size());
|
vector<char> expected(vch.size());
|
||||||
|
|
|
@ -254,7 +254,7 @@ struct StringContentsSerializer {
|
||||||
ADD_SERIALIZE_METHODS;
|
ADD_SERIALIZE_METHODS;
|
||||||
|
|
||||||
template <typename Stream, typename Operation>
|
template <typename Stream, typename Operation>
|
||||||
inline void SerializationOp(Stream& s, Operation ser_action, int nType, int nVersion) {
|
inline void SerializationOp(Stream& s, Operation ser_action) {
|
||||||
if (ser_action.ForRead()) {
|
if (ser_action.ForRead()) {
|
||||||
str.clear();
|
str.clear();
|
||||||
char c = 0;
|
char c = 0;
|
||||||
|
|
|
@ -17,7 +17,7 @@ using namespace std;
|
||||||
class CAddrManSerializationMock : public CAddrMan
|
class CAddrManSerializationMock : public CAddrMan
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
virtual void Serialize(CDataStream& s, int nType, int nVersionDummy) const = 0;
|
virtual void Serialize(CDataStream& s) const = 0;
|
||||||
|
|
||||||
//! Ensure that bucket placement is always the same for testing purposes.
|
//! Ensure that bucket placement is always the same for testing purposes.
|
||||||
void MakeDeterministic()
|
void MakeDeterministic()
|
||||||
|
@ -30,16 +30,16 @@ public:
|
||||||
class CAddrManUncorrupted : public CAddrManSerializationMock
|
class CAddrManUncorrupted : public CAddrManSerializationMock
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
void Serialize(CDataStream& s, int nType, int nVersionDummy) const
|
void Serialize(CDataStream& s) const
|
||||||
{
|
{
|
||||||
CAddrMan::Serialize(s, nType, nVersionDummy);
|
CAddrMan::Serialize(s);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
class CAddrManCorrupted : public CAddrManSerializationMock
|
class CAddrManCorrupted : public CAddrManSerializationMock
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
void Serialize(CDataStream& s, int nType, int nVersionDummy) const
|
void Serialize(CDataStream& s) const
|
||||||
{
|
{
|
||||||
// Produces corrupt output that claims addrman has 20 addrs when it only has one addr.
|
// Produces corrupt output that claims addrman has 20 addrs when it only has one addr.
|
||||||
unsigned char nVersion = 1;
|
unsigned char nVersion = 1;
|
||||||
|
|
|
@ -28,7 +28,7 @@ public:
|
||||||
ADD_SERIALIZE_METHODS;
|
ADD_SERIALIZE_METHODS;
|
||||||
|
|
||||||
template <typename Stream, typename Operation>
|
template <typename Stream, typename Operation>
|
||||||
inline void SerializationOp(Stream& s, Operation ser_action, int nType, int nVersion) {
|
inline void SerializationOp(Stream& s, Operation ser_action) {
|
||||||
READWRITE(intval);
|
READWRITE(intval);
|
||||||
READWRITE(boolval);
|
READWRITE(boolval);
|
||||||
READWRITE(stringval);
|
READWRITE(stringval);
|
||||||
|
@ -53,7 +53,7 @@ public:
|
||||||
ADD_SERIALIZE_METHODS;
|
ADD_SERIALIZE_METHODS;
|
||||||
|
|
||||||
template <typename Stream, typename Operation>
|
template <typename Stream, typename Operation>
|
||||||
inline void SerializationOp(Stream& s, Operation ser_action, int nType, int nVersion) {
|
inline void SerializationOp(Stream& s, Operation ser_action) {
|
||||||
READWRITEMANY(intval, boolval, stringval, FLATDATA(charstrval), txval);
|
READWRITEMANY(intval, boolval, stringval, FLATDATA(charstrval), txval);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
|
@ -184,25 +184,25 @@ BOOST_AUTO_TEST_CASE( methods ) // GetHex SetHex begin() end() size() GetLow64 G
|
||||||
BOOST_CHECK(OneL.begin() + 32 == OneL.end());
|
BOOST_CHECK(OneL.begin() + 32 == OneL.end());
|
||||||
BOOST_CHECK(MaxL.begin() + 32 == MaxL.end());
|
BOOST_CHECK(MaxL.begin() + 32 == MaxL.end());
|
||||||
BOOST_CHECK(TmpL.begin() + 32 == TmpL.end());
|
BOOST_CHECK(TmpL.begin() + 32 == TmpL.end());
|
||||||
BOOST_CHECK(R1L.GetSerializeSize(0,PROTOCOL_VERSION) == 32);
|
BOOST_CHECK(GetSerializeSize(R1L, 0, PROTOCOL_VERSION) == 32);
|
||||||
BOOST_CHECK(ZeroL.GetSerializeSize(0,PROTOCOL_VERSION) == 32);
|
BOOST_CHECK(GetSerializeSize(ZeroL, 0, PROTOCOL_VERSION) == 32);
|
||||||
|
|
||||||
std::stringstream ss;
|
CDataStream ss(0, PROTOCOL_VERSION);
|
||||||
R1L.Serialize(ss,0,PROTOCOL_VERSION);
|
ss << R1L;
|
||||||
BOOST_CHECK(ss.str() == std::string(R1Array,R1Array+32));
|
BOOST_CHECK(ss.str() == std::string(R1Array,R1Array+32));
|
||||||
TmpL.Unserialize(ss,0,PROTOCOL_VERSION);
|
ss >> TmpL;
|
||||||
BOOST_CHECK(R1L == TmpL);
|
BOOST_CHECK(R1L == TmpL);
|
||||||
ss.str("");
|
ss.clear();
|
||||||
ZeroL.Serialize(ss,0,PROTOCOL_VERSION);
|
ss << ZeroL;
|
||||||
BOOST_CHECK(ss.str() == std::string(ZeroArray,ZeroArray+32));
|
BOOST_CHECK(ss.str() == std::string(ZeroArray,ZeroArray+32));
|
||||||
TmpL.Unserialize(ss,0,PROTOCOL_VERSION);
|
ss >> TmpL;
|
||||||
BOOST_CHECK(ZeroL == TmpL);
|
BOOST_CHECK(ZeroL == TmpL);
|
||||||
ss.str("");
|
ss.clear();
|
||||||
MaxL.Serialize(ss,0,PROTOCOL_VERSION);
|
ss << MaxL;
|
||||||
BOOST_CHECK(ss.str() == std::string(MaxArray,MaxArray+32));
|
BOOST_CHECK(ss.str() == std::string(MaxArray,MaxArray+32));
|
||||||
TmpL.Unserialize(ss,0,PROTOCOL_VERSION);
|
ss >> TmpL;
|
||||||
BOOST_CHECK(MaxL == TmpL);
|
BOOST_CHECK(MaxL == TmpL);
|
||||||
ss.str("");
|
ss.clear();
|
||||||
|
|
||||||
BOOST_CHECK(R1S.GetHex() == R1S.ToString());
|
BOOST_CHECK(R1S.GetHex() == R1S.ToString());
|
||||||
BOOST_CHECK(R2S.GetHex() == R2S.ToString());
|
BOOST_CHECK(R2S.GetHex() == R2S.ToString());
|
||||||
|
@ -230,24 +230,24 @@ BOOST_AUTO_TEST_CASE( methods ) // GetHex SetHex begin() end() size() GetLow64 G
|
||||||
BOOST_CHECK(OneS.begin() + 20 == OneS.end());
|
BOOST_CHECK(OneS.begin() + 20 == OneS.end());
|
||||||
BOOST_CHECK(MaxS.begin() + 20 == MaxS.end());
|
BOOST_CHECK(MaxS.begin() + 20 == MaxS.end());
|
||||||
BOOST_CHECK(TmpS.begin() + 20 == TmpS.end());
|
BOOST_CHECK(TmpS.begin() + 20 == TmpS.end());
|
||||||
BOOST_CHECK(R1S.GetSerializeSize(0,PROTOCOL_VERSION) == 20);
|
BOOST_CHECK(GetSerializeSize(R1S, 0, PROTOCOL_VERSION) == 20);
|
||||||
BOOST_CHECK(ZeroS.GetSerializeSize(0,PROTOCOL_VERSION) == 20);
|
BOOST_CHECK(GetSerializeSize(ZeroS, 0, PROTOCOL_VERSION) == 20);
|
||||||
|
|
||||||
R1S.Serialize(ss,0,PROTOCOL_VERSION);
|
ss << R1S;
|
||||||
BOOST_CHECK(ss.str() == std::string(R1Array,R1Array+20));
|
BOOST_CHECK(ss.str() == std::string(R1Array,R1Array+20));
|
||||||
TmpS.Unserialize(ss,0,PROTOCOL_VERSION);
|
ss >> TmpS;
|
||||||
BOOST_CHECK(R1S == TmpS);
|
BOOST_CHECK(R1S == TmpS);
|
||||||
ss.str("");
|
ss.clear();
|
||||||
ZeroS.Serialize(ss,0,PROTOCOL_VERSION);
|
ss << ZeroS;
|
||||||
BOOST_CHECK(ss.str() == std::string(ZeroArray,ZeroArray+20));
|
BOOST_CHECK(ss.str() == std::string(ZeroArray,ZeroArray+20));
|
||||||
TmpS.Unserialize(ss,0,PROTOCOL_VERSION);
|
ss >> TmpS;
|
||||||
BOOST_CHECK(ZeroS == TmpS);
|
BOOST_CHECK(ZeroS == TmpS);
|
||||||
ss.str("");
|
ss.clear();
|
||||||
MaxS.Serialize(ss,0,PROTOCOL_VERSION);
|
ss << MaxS;
|
||||||
BOOST_CHECK(ss.str() == std::string(MaxArray,MaxArray+20));
|
BOOST_CHECK(ss.str() == std::string(MaxArray,MaxArray+20));
|
||||||
TmpS.Unserialize(ss,0,PROTOCOL_VERSION);
|
ss >> TmpS;
|
||||||
BOOST_CHECK(MaxS == TmpS);
|
BOOST_CHECK(MaxS == TmpS);
|
||||||
ss.str("");
|
ss.clear();
|
||||||
}
|
}
|
||||||
|
|
||||||
BOOST_AUTO_TEST_CASE( conversion )
|
BOOST_AUTO_TEST_CASE( conversion )
|
||||||
|
|
|
@ -43,7 +43,7 @@ struct CDiskTxPos : public CDiskBlockPos
|
||||||
ADD_SERIALIZE_METHODS;
|
ADD_SERIALIZE_METHODS;
|
||||||
|
|
||||||
template <typename Stream, typename Operation>
|
template <typename Stream, typename Operation>
|
||||||
inline void SerializationOp(Stream& s, Operation ser_action, int nType, int nVersion) {
|
inline void SerializationOp(Stream& s, Operation ser_action) {
|
||||||
READWRITE(*(CDiskBlockPos*)this);
|
READWRITE(*(CDiskBlockPos*)this);
|
||||||
READWRITE(VARINT(nTxOffset));
|
READWRITE(VARINT(nTxOffset));
|
||||||
}
|
}
|
||||||
|
|
|
@ -78,11 +78,6 @@ public:
|
||||||
return sizeof(data);
|
return sizeof(data);
|
||||||
}
|
}
|
||||||
|
|
||||||
unsigned int GetSerializeSize(int nType, int nVersion) const
|
|
||||||
{
|
|
||||||
return sizeof(data);
|
|
||||||
}
|
|
||||||
|
|
||||||
uint64_t GetUint64(int pos) const
|
uint64_t GetUint64(int pos) const
|
||||||
{
|
{
|
||||||
const uint8_t* ptr = data + pos * 8;
|
const uint8_t* ptr = data + pos * 8;
|
||||||
|
@ -97,13 +92,13 @@ public:
|
||||||
}
|
}
|
||||||
|
|
||||||
template<typename Stream>
|
template<typename Stream>
|
||||||
void Serialize(Stream& s, int nType, int nVersion) const
|
void Serialize(Stream& s) const
|
||||||
{
|
{
|
||||||
s.write((char*)data, sizeof(data));
|
s.write((char*)data, sizeof(data));
|
||||||
}
|
}
|
||||||
|
|
||||||
template<typename Stream>
|
template<typename Stream>
|
||||||
void Unserialize(Stream& s, int nType, int nVersion)
|
void Unserialize(Stream& s)
|
||||||
{
|
{
|
||||||
s.read((char*)data, sizeof(data));
|
s.read((char*)data, sizeof(data));
|
||||||
}
|
}
|
||||||
|
|
26
src/undo.h
26
src/undo.h
|
@ -27,29 +27,23 @@ public:
|
||||||
CTxInUndo() : txout(), fCoinBase(false), nHeight(0), nVersion(0) {}
|
CTxInUndo() : txout(), fCoinBase(false), nHeight(0), nVersion(0) {}
|
||||||
CTxInUndo(const CTxOut &txoutIn, bool fCoinBaseIn = false, unsigned int nHeightIn = 0, int nVersionIn = 0) : txout(txoutIn), fCoinBase(fCoinBaseIn), nHeight(nHeightIn), nVersion(nVersionIn) { }
|
CTxInUndo(const CTxOut &txoutIn, bool fCoinBaseIn = false, unsigned int nHeightIn = 0, int nVersionIn = 0) : txout(txoutIn), fCoinBase(fCoinBaseIn), nHeight(nHeightIn), nVersion(nVersionIn) { }
|
||||||
|
|
||||||
unsigned int GetSerializeSize(int nType, int nVersion) const {
|
|
||||||
return ::GetSerializeSize(VARINT(nHeight*2+(fCoinBase ? 1 : 0)), nType, nVersion) +
|
|
||||||
(nHeight > 0 ? ::GetSerializeSize(VARINT(this->nVersion), nType, nVersion) : 0) +
|
|
||||||
::GetSerializeSize(CTxOutCompressor(REF(txout)), nType, nVersion);
|
|
||||||
}
|
|
||||||
|
|
||||||
template<typename Stream>
|
template<typename Stream>
|
||||||
void Serialize(Stream &s, int nType, int nVersion) const {
|
void Serialize(Stream &s) const {
|
||||||
::Serialize(s, VARINT(nHeight*2+(fCoinBase ? 1 : 0)), nType, nVersion);
|
::Serialize(s, VARINT(nHeight*2+(fCoinBase ? 1 : 0)));
|
||||||
if (nHeight > 0)
|
if (nHeight > 0)
|
||||||
::Serialize(s, VARINT(this->nVersion), nType, nVersion);
|
::Serialize(s, VARINT(this->nVersion));
|
||||||
::Serialize(s, CTxOutCompressor(REF(txout)), nType, nVersion);
|
::Serialize(s, CTxOutCompressor(REF(txout)));
|
||||||
}
|
}
|
||||||
|
|
||||||
template<typename Stream>
|
template<typename Stream>
|
||||||
void Unserialize(Stream &s, int nType, int nVersion) {
|
void Unserialize(Stream &s) {
|
||||||
unsigned int nCode = 0;
|
unsigned int nCode = 0;
|
||||||
::Unserialize(s, VARINT(nCode), nType, nVersion);
|
::Unserialize(s, VARINT(nCode));
|
||||||
nHeight = nCode / 2;
|
nHeight = nCode / 2;
|
||||||
fCoinBase = nCode & 1;
|
fCoinBase = nCode & 1;
|
||||||
if (nHeight > 0)
|
if (nHeight > 0)
|
||||||
::Unserialize(s, VARINT(this->nVersion), nType, nVersion);
|
::Unserialize(s, VARINT(this->nVersion));
|
||||||
::Unserialize(s, REF(CTxOutCompressor(REF(txout))), nType, nVersion);
|
::Unserialize(s, REF(CTxOutCompressor(REF(txout))));
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -63,7 +57,7 @@ public:
|
||||||
ADD_SERIALIZE_METHODS;
|
ADD_SERIALIZE_METHODS;
|
||||||
|
|
||||||
template <typename Stream, typename Operation>
|
template <typename Stream, typename Operation>
|
||||||
inline void SerializationOp(Stream& s, Operation ser_action, int nType, int nVersion) {
|
inline void SerializationOp(Stream& s, Operation ser_action) {
|
||||||
READWRITE(vprevout);
|
READWRITE(vprevout);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
@ -77,7 +71,7 @@ public:
|
||||||
ADD_SERIALIZE_METHODS;
|
ADD_SERIALIZE_METHODS;
|
||||||
|
|
||||||
template <typename Stream, typename Operation>
|
template <typename Stream, typename Operation>
|
||||||
inline void SerializationOp(Stream& s, Operation ser_action, int nType, int nVersion) {
|
inline void SerializationOp(Stream& s, Operation ser_action) {
|
||||||
READWRITE(vtxundo);
|
READWRITE(vtxundo);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
|
@ -47,7 +47,7 @@ public:
|
||||||
ADD_SERIALIZE_METHODS;
|
ADD_SERIALIZE_METHODS;
|
||||||
|
|
||||||
template <typename Stream, typename Operation>
|
template <typename Stream, typename Operation>
|
||||||
inline void SerializationOp(Stream& s, Operation ser_action, int nType, int nVersion) {
|
inline void SerializationOp(Stream& s, Operation ser_action) {
|
||||||
READWRITE(vchCryptedKey);
|
READWRITE(vchCryptedKey);
|
||||||
READWRITE(vchSalt);
|
READWRITE(vchSalt);
|
||||||
READWRITE(nDerivationMethod);
|
READWRITE(nDerivationMethod);
|
||||||
|
|
|
@ -100,8 +100,9 @@ public:
|
||||||
ADD_SERIALIZE_METHODS;
|
ADD_SERIALIZE_METHODS;
|
||||||
|
|
||||||
template <typename Stream, typename Operation>
|
template <typename Stream, typename Operation>
|
||||||
inline void SerializationOp(Stream& s, Operation ser_action, int nType, int nVersion) {
|
inline void SerializationOp(Stream& s, Operation ser_action) {
|
||||||
if (!(nType & SER_GETHASH))
|
int nVersion = s.GetVersion();
|
||||||
|
if (!(s.GetType() & SER_GETHASH))
|
||||||
READWRITE(nVersion);
|
READWRITE(nVersion);
|
||||||
READWRITE(nTime);
|
READWRITE(nTime);
|
||||||
READWRITE(vchPubKey);
|
READWRITE(vchPubKey);
|
||||||
|
@ -195,7 +196,7 @@ public:
|
||||||
ADD_SERIALIZE_METHODS;
|
ADD_SERIALIZE_METHODS;
|
||||||
|
|
||||||
template <typename Stream, typename Operation>
|
template <typename Stream, typename Operation>
|
||||||
inline void SerializationOp(Stream& s, Operation ser_action, int nType, int nVersion) {
|
inline void SerializationOp(Stream& s, Operation ser_action) {
|
||||||
std::vector<uint256> vMerkleBranch; // For compatibility with older versions.
|
std::vector<uint256> vMerkleBranch; // For compatibility with older versions.
|
||||||
READWRITE(*(CTransaction*)this);
|
READWRITE(*(CTransaction*)this);
|
||||||
READWRITE(hashBlock);
|
READWRITE(hashBlock);
|
||||||
|
@ -315,7 +316,7 @@ public:
|
||||||
ADD_SERIALIZE_METHODS;
|
ADD_SERIALIZE_METHODS;
|
||||||
|
|
||||||
template <typename Stream, typename Operation>
|
template <typename Stream, typename Operation>
|
||||||
inline void SerializationOp(Stream& s, Operation ser_action, int nType, int nVersion) {
|
inline void SerializationOp(Stream& s, Operation ser_action) {
|
||||||
if (ser_action.ForRead())
|
if (ser_action.ForRead())
|
||||||
Init(NULL);
|
Init(NULL);
|
||||||
char fSpent = false;
|
char fSpent = false;
|
||||||
|
@ -448,8 +449,9 @@ public:
|
||||||
ADD_SERIALIZE_METHODS;
|
ADD_SERIALIZE_METHODS;
|
||||||
|
|
||||||
template <typename Stream, typename Operation>
|
template <typename Stream, typename Operation>
|
||||||
inline void SerializationOp(Stream& s, Operation ser_action, int nType, int nVersion) {
|
inline void SerializationOp(Stream& s, Operation ser_action) {
|
||||||
if (!(nType & SER_GETHASH))
|
int nVersion = s.GetVersion();
|
||||||
|
if (!(s.GetType() & SER_GETHASH))
|
||||||
READWRITE(nVersion);
|
READWRITE(nVersion);
|
||||||
READWRITE(vchPrivKey);
|
READWRITE(vchPrivKey);
|
||||||
READWRITE(nTimeCreated);
|
READWRITE(nTimeCreated);
|
||||||
|
@ -493,8 +495,9 @@ public:
|
||||||
ADD_SERIALIZE_METHODS;
|
ADD_SERIALIZE_METHODS;
|
||||||
|
|
||||||
template <typename Stream, typename Operation>
|
template <typename Stream, typename Operation>
|
||||||
inline void SerializationOp(Stream& s, Operation ser_action, int nType, int nVersion) {
|
inline void SerializationOp(Stream& s, Operation ser_action) {
|
||||||
if (!(nType & SER_GETHASH))
|
int nVersion = s.GetVersion();
|
||||||
|
if (!(s.GetType() & SER_GETHASH))
|
||||||
READWRITE(nVersion);
|
READWRITE(nVersion);
|
||||||
//! Note: strAccount is serialized as part of the key, not here.
|
//! Note: strAccount is serialized as part of the key, not here.
|
||||||
READWRITE(nCreditDebit);
|
READWRITE(nCreditDebit);
|
||||||
|
@ -507,7 +510,7 @@ public:
|
||||||
|
|
||||||
if (!(mapValue.empty() && _ssExtra.empty()))
|
if (!(mapValue.empty() && _ssExtra.empty()))
|
||||||
{
|
{
|
||||||
CDataStream ss(nType, nVersion);
|
CDataStream ss(s.GetType(), s.GetVersion());
|
||||||
ss.insert(ss.begin(), '\0');
|
ss.insert(ss.begin(), '\0');
|
||||||
ss << mapValue;
|
ss << mapValue;
|
||||||
ss.insert(ss.end(), _ssExtra.begin(), _ssExtra.end());
|
ss.insert(ss.end(), _ssExtra.begin(), _ssExtra.end());
|
||||||
|
@ -523,7 +526,7 @@ public:
|
||||||
mapValue.clear();
|
mapValue.clear();
|
||||||
if (std::string::npos != nSepPos)
|
if (std::string::npos != nSepPos)
|
||||||
{
|
{
|
||||||
CDataStream ss(std::vector<char>(strComment.begin() + nSepPos + 1, strComment.end()), nType, nVersion);
|
CDataStream ss(std::vector<char>(strComment.begin() + nSepPos + 1, strComment.end()), s.GetType(), s.GetVersion());
|
||||||
ss >> mapValue;
|
ss >> mapValue;
|
||||||
_ssExtra = std::vector<char>(ss.begin(), ss.end());
|
_ssExtra = std::vector<char>(ss.begin(), ss.end());
|
||||||
}
|
}
|
||||||
|
@ -986,8 +989,9 @@ public:
|
||||||
ADD_SERIALIZE_METHODS;
|
ADD_SERIALIZE_METHODS;
|
||||||
|
|
||||||
template <typename Stream, typename Operation>
|
template <typename Stream, typename Operation>
|
||||||
inline void SerializationOp(Stream& s, Operation ser_action, int nType, int nVersion) {
|
inline void SerializationOp(Stream& s, Operation ser_action) {
|
||||||
if (!(nType & SER_GETHASH))
|
int nVersion = s.GetVersion();
|
||||||
|
if (!(s.GetType() & SER_GETHASH))
|
||||||
READWRITE(nVersion);
|
READWRITE(nVersion);
|
||||||
READWRITE(vchPubKey);
|
READWRITE(vchPubKey);
|
||||||
}
|
}
|
||||||
|
|
|
@ -54,7 +54,7 @@ public:
|
||||||
CHDChain() { SetNull(); }
|
CHDChain() { SetNull(); }
|
||||||
ADD_SERIALIZE_METHODS;
|
ADD_SERIALIZE_METHODS;
|
||||||
template <typename Stream, typename Operation>
|
template <typename Stream, typename Operation>
|
||||||
inline void SerializationOp(Stream& s, Operation ser_action, int nType, int nVersion)
|
inline void SerializationOp(Stream& s, Operation ser_action)
|
||||||
{
|
{
|
||||||
READWRITE(this->nVersion);
|
READWRITE(this->nVersion);
|
||||||
READWRITE(nExternalChainCounter);
|
READWRITE(nExternalChainCounter);
|
||||||
|
@ -93,7 +93,7 @@ public:
|
||||||
ADD_SERIALIZE_METHODS;
|
ADD_SERIALIZE_METHODS;
|
||||||
|
|
||||||
template <typename Stream, typename Operation>
|
template <typename Stream, typename Operation>
|
||||||
inline void SerializationOp(Stream& s, Operation ser_action, int nType, int nVersion) {
|
inline void SerializationOp(Stream& s, Operation ser_action) {
|
||||||
READWRITE(this->nVersion);
|
READWRITE(this->nVersion);
|
||||||
READWRITE(nCreateTime);
|
READWRITE(nCreateTime);
|
||||||
if (this->nVersion >= VERSION_WITH_HDDATA)
|
if (this->nVersion >= VERSION_WITH_HDDATA)
|
||||||
|
|
Loading…
Add table
Reference in a new issue