Convert blockencodings.h to new serialization framework
This commit is contained in:
1 parent
e574fff53e
commit
353f376277
1 file changed
+16
-110
+16
-110
@@ -10,20 +10,9 @@
|
|||||||
|
|
||||||
class CTxMemPool;
|
class CTxMemPool;
|
||||||
|
|
||||||
// Dumb helper to handle CTransaction compression at serialize-time
|
// Transaction compression schemes for compact block relay can be introduced by writing
|
||||||
struct TransactionCompressor {
|
// an actual formatter here.
|
||||||
private:
|
using TransactionCompression = DefaultFormatter;
|
||||||
CTransactionRef& tx;
|
|
||||||
public:
|
|
||||||
explicit TransactionCompressor(CTransactionRef& txIn) : tx(txIn) {}
|
|
||||||
|
|
||||||
ADD_SERIALIZE_METHODS;
|
|
||||||
|
|
||||||
template <typename Stream, typename Operation>
|
|
||||||
inline void SerializationOp(Stream& s, Operation ser_action) {
|
|
||||||
READWRITE(tx); //TODO: Compress tx encoding
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
class DifferenceFormatter
|
class DifferenceFormatter
|
||||||
{
|
{
|
||||||
@@ -53,39 +42,9 @@ public:
|
|||||||
uint256 blockhash;
|
uint256 blockhash;
|
||||||
std::vector<uint16_t> indexes;
|
std::vector<uint16_t> indexes;
|
||||||
|
|
||||||
ADD_SERIALIZE_METHODS;
|
SERIALIZE_METHODS(BlockTransactionsRequest, obj)
|
||||||
|
{
|
||||||
template <typename Stream, typename Operation>
|
READWRITE(obj.blockhash, Using<VectorFormatter<DifferenceFormatter>>(obj.indexes));
|
||||||
inline void SerializationOp(Stream& s, Operation ser_action) {
|
|
||||||
READWRITE(blockhash);
|
|
||||||
uint64_t indexes_size = (uint64_t)indexes.size();
|
|
||||||
READWRITE(COMPACTSIZE(indexes_size));
|
|
||||||
if (ser_action.ForRead()) {
|
|
||||||
size_t i = 0;
|
|
||||||
while (indexes.size() < indexes_size) {
|
|
||||||
indexes.resize(std::min((uint64_t)(1000 + indexes.size()), indexes_size));
|
|
||||||
for (; i < indexes.size(); i++) {
|
|
||||||
uint64_t index = 0;
|
|
||||||
READWRITE(COMPACTSIZE(index));
|
|
||||||
if (index > std::numeric_limits<uint16_t>::max())
|
|
||||||
throw std::ios_base::failure("index overflowed 16 bits");
|
|
||||||
indexes[i] = index;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
int32_t offset = 0;
|
|
||||||
for (size_t j = 0; j < indexes.size(); j++) {
|
|
||||||
if (int32_t(indexes[j]) + offset > std::numeric_limits<uint16_t>::max())
|
|
||||||
throw std::ios_base::failure("indexes overflowed 16 bits");
|
|
||||||
indexes[j] = indexes[j] + offset;
|
|
||||||
offset = int32_t(indexes[j]) + 1;
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
for (size_t i = 0; i < indexes.size(); i++) {
|
|
||||||
uint64_t index = indexes[i] - (i == 0 ? 0 : (indexes[i - 1] + 1));
|
|
||||||
READWRITE(COMPACTSIZE(index));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -99,24 +58,9 @@ public:
|
|||||||
explicit BlockTransactions(const BlockTransactionsRequest& req) :
|
explicit BlockTransactions(const BlockTransactionsRequest& req) :
|
||||||
blockhash(req.blockhash), txn(req.indexes.size()) {}
|
blockhash(req.blockhash), txn(req.indexes.size()) {}
|
||||||
|
|
||||||
ADD_SERIALIZE_METHODS;
|
SERIALIZE_METHODS(BlockTransactions, obj)
|
||||||
|
{
|
||||||
template <typename Stream, typename Operation>
|
READWRITE(obj.blockhash, Using<VectorFormatter<TransactionCompression>>(obj.txn));
|
||||||
inline void SerializationOp(Stream& s, Operation ser_action) {
|
|
||||||
READWRITE(blockhash);
|
|
||||||
uint64_t txn_size = (uint64_t)txn.size();
|
|
||||||
READWRITE(COMPACTSIZE(txn_size));
|
|
||||||
if (ser_action.ForRead()) {
|
|
||||||
size_t i = 0;
|
|
||||||
while (txn.size() < txn_size) {
|
|
||||||
txn.resize(std::min((uint64_t)(1000 + txn.size()), txn_size));
|
|
||||||
for (; i < txn.size(); i++)
|
|
||||||
READWRITE(TransactionCompressor(txn[i]));
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
for (size_t i = 0; i < txn.size(); i++)
|
|
||||||
READWRITE(TransactionCompressor(txn[i]));
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -127,17 +71,7 @@ struct PrefilledTransaction {
|
|||||||
uint16_t index;
|
uint16_t index;
|
||||||
CTransactionRef tx;
|
CTransactionRef tx;
|
||||||
|
|
||||||
ADD_SERIALIZE_METHODS;
|
SERIALIZE_METHODS(PrefilledTransaction, obj) { READWRITE(COMPACTSIZE(obj.index), Using<TransactionCompression>(obj.tx)); }
|
||||||
|
|
||||||
template <typename Stream, typename Operation>
|
|
||||||
inline void SerializationOp(Stream& s, Operation ser_action) {
|
|
||||||
uint64_t idx = index;
|
|
||||||
READWRITE(COMPACTSIZE(idx));
|
|
||||||
if (idx > std::numeric_limits<uint16_t>::max())
|
|
||||||
throw std::ios_base::failure("index overflowed 16-bits");
|
|
||||||
index = idx;
|
|
||||||
READWRITE(TransactionCompressor(tx));
|
|
||||||
}
|
|
||||||
};
|
};
|
||||||
|
|
||||||
typedef enum ReadStatus_t
|
typedef enum ReadStatus_t
|
||||||
@@ -175,43 +109,15 @@ public:
|
|||||||
|
|
||||||
size_t BlockTxCount() const { return shorttxids.size() + prefilledtxn.size(); }
|
size_t BlockTxCount() const { return shorttxids.size() + prefilledtxn.size(); }
|
||||||
|
|
||||||
ADD_SERIALIZE_METHODS;
|
SERIALIZE_METHODS(CBlockHeaderAndShortTxIDs, obj)
|
||||||
|
{
|
||||||
template <typename Stream, typename Operation>
|
READWRITE(obj.header, obj.nonce, Using<VectorFormatter<CustomUintFormatter<SHORTTXIDS_LENGTH>>>(obj.shorttxids), obj.prefilledtxn);
|
||||||
inline void SerializationOp(Stream& s, Operation ser_action) {
|
|
||||||
READWRITE(header);
|
|
||||||
READWRITE(nonce);
|
|
||||||
|
|
||||||
uint64_t shorttxids_size = (uint64_t)shorttxids.size();
|
|
||||||
READWRITE(COMPACTSIZE(shorttxids_size));
|
|
||||||
if (ser_action.ForRead()) {
|
if (ser_action.ForRead()) {
|
||||||
size_t i = 0;
|
if (obj.BlockTxCount() > std::numeric_limits<uint16_t>::max()) {
|
||||||
while (shorttxids.size() < shorttxids_size) {
|
throw std::ios_base::failure("indexes overflowed 16 bits");
|
||||||
shorttxids.resize(std::min((uint64_t)(1000 + shorttxids.size()), shorttxids_size));
|
|
||||||
for (; i < shorttxids.size(); i++) {
|
|
||||||
uint32_t lsb = 0; uint16_t msb = 0;
|
|
||||||
READWRITE(lsb);
|
|
||||||
READWRITE(msb);
|
|
||||||
shorttxids[i] = (uint64_t(msb) << 32) | uint64_t(lsb);
|
|
||||||
static_assert(SHORTTXIDS_LENGTH == 6, "shorttxids serialization assumes 6-byte shorttxids");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
for (size_t i = 0; i < shorttxids.size(); i++) {
|
|
||||||
uint32_t lsb = shorttxids[i] & 0xffffffff;
|
|
||||||
uint16_t msb = (shorttxids[i] >> 32) & 0xffff;
|
|
||||||
READWRITE(lsb);
|
|
||||||
READWRITE(msb);
|
|
||||||
}
|
}
|
||||||
|
obj.FillShortTxIDSelector();
|
||||||
}
|
}
|
||||||
|
|
||||||
READWRITE(prefilledtxn);
|
|
||||||
|
|
||||||
if (BlockTxCount() > std::numeric_limits<uint16_t>::max())
|
|
||||||
throw std::ios_base::failure("indexes overflowed 16 bits");
|
|
||||||
|
|
||||||
if (ser_action.ForRead())
|
|
||||||
FillShortTxIDSelector();
|
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
Reference in new issue
Block a user