Bitcoin ABC  0.24.10
P2P Digital Currency
blockencodings.h
Go to the documentation of this file.
1 // Copyright (c) 2016 The Bitcoin Core developers
2 // Distributed under the MIT software license, see the accompanying
3 // file COPYING or http://www.opensource.org/licenses/mit-license.php.
4 
5 #ifndef BITCOIN_BLOCKENCODINGS_H
6 #define BITCOIN_BLOCKENCODINGS_H
7 
8 #include <primitives/block.h>
9 
10 class Config;
11 class CTxMemPool;
12 
13 // Transaction compression schemes for compact block relay can be introduced by
14 // writing an actual formatter here.
16 
18  uint64_t m_shift = 0;
19 
20 public:
21  template <typename Stream, typename I> void Ser(Stream &s, I v) {
22  if (v < m_shift || v >= std::numeric_limits<uint64_t>::max()) {
23  throw std::ios_base::failure("differential value overflow");
24  }
25  WriteCompactSize(s, v - m_shift);
26  m_shift = uint64_t(v) + 1;
27  }
28  template <typename Stream, typename I> void Unser(Stream &s, I &v) {
29  uint64_t n = ReadCompactSize(s);
30  m_shift += n;
31  if (m_shift < n || m_shift >= std::numeric_limits<uint64_t>::max() ||
32  m_shift < std::numeric_limits<I>::min() ||
33  m_shift > std::numeric_limits<I>::max()) {
34  throw std::ios_base::failure("differential value overflow");
35  }
36  v = I(m_shift++);
37  }
38 };
39 
41 public:
42  // A BlockTransactionsRequest message
44  std::vector<uint32_t> indices;
45 
47  READWRITE(obj.blockhash,
49  }
50 };
51 
53 public:
54  // A BlockTransactions message
56  std::vector<CTransactionRef> txn;
57 
60  : blockhash(req.blockhash), txn(req.indices.size()) {}
61 
63  READWRITE(obj.blockhash,
65  }
66 };
67 
68 // Dumb serialization/storage-helper for CBlockHeaderAndShortTxIDs and
69 // PartiallyDownloadedBlock
71  // Used as an offset since last prefilled tx in CBlockHeaderAndShortTxIDs,
72  // as a proper transaction-in-block-index in PartiallyDownloadedBlock
73  uint32_t index;
75 
77  READWRITE(COMPACTSIZE(obj.index),
78  Using<TransactionCompression>(obj.tx));
79  }
80 };
81 
82 typedef enum ReadStatus_t {
84  // Invalid object, peer is sending bogus crap.
85  // FIXME: differenciate bogus crap from crap that do not fit our policy.
87  // Failed to process object.
89  // Used only by FillBlock to indicate a failure in CheckBlock.
91 } ReadStatus;
92 
94 private:
95  mutable uint64_t shorttxidk0, shorttxidk1;
96  uint64_t nonce;
97 
98  void FillShortTxIDSelector() const;
99 
101 
102 protected:
103  std::vector<uint64_t> shorttxids;
104  std::vector<PrefilledTransaction> prefilledtxn;
105 
106 public:
107  static constexpr int SHORTTXIDS_LENGTH = 6;
108 
110 
111  // Dummy for deserialization
113 
114  explicit CBlockHeaderAndShortTxIDs(const CBlock &block);
115 
116  uint64_t GetShortID(const TxHash &txhash) const;
117 
118  size_t BlockTxCount() const {
119  return shorttxids.size() + prefilledtxn.size();
120  }
121 
123  READWRITE(
124  obj.header, obj.nonce,
126  obj.shorttxids),
127  obj.prefilledtxn);
128  if (ser_action.ForRead()) {
129  if (obj.BlockTxCount() > std::numeric_limits<uint32_t>::max()) {
130  throw std::ios_base::failure("indices overflowed 32 bits");
131  }
132  obj.FillShortTxIDSelector();
133  }
134  }
135 };
136 
138 protected:
139  std::vector<CTransactionRef> txns_available;
141  const CTxMemPool *pool;
142  const Config *config;
143 
144 public:
146  PartiallyDownloadedBlock(const Config &configIn, CTxMemPool *poolIn)
147  : pool(poolIn), config(&configIn) {}
148 
149  // extra_txn is a list of extra transactions to look at, in <txhash,
150  // reference> form.
151  ReadStatus
152  InitData(const CBlockHeaderAndShortTxIDs &cmpctblock,
153  const std::vector<std::pair<TxHash, CTransactionRef>> &extra_txn);
154  bool IsTxAvailable(size_t index) const;
155  ReadStatus FillBlock(CBlock &block,
156  const std::vector<CTransactionRef> &vtx_missing);
157 };
158 
159 #endif // BITCOIN_BLOCKENCODINGS_H
DefaultFormatter
Default formatter.
Definition: serialize.h:861
block.h
BlockTransactions::SERIALIZE_METHODS
SERIALIZE_METHODS(BlockTransactions, obj)
Definition: blockencodings.h:62
CBlockHeaderAndShortTxIDs::GetShortID
uint64_t GetShortID(const TxHash &txhash) const
Definition: blockencodings.cpp:45
CBlockHeaderAndShortTxIDs::SHORTTXIDS_LENGTH
static constexpr int SHORTTXIDS_LENGTH
Definition: blockencodings.h:107
BlockTransactions::BlockTransactions
BlockTransactions()
Definition: blockencodings.h:58
PartiallyDownloadedBlock::FillBlock
ReadStatus FillBlock(CBlock &block, const std::vector< CTransactionRef > &vtx_missing)
Definition: blockencodings.cpp:214
READ_STATUS_CHECKBLOCK_FAILED
@ READ_STATUS_CHECKBLOCK_FAILED
Definition: blockencodings.h:90
CBlockHeader
Nodes collect new transactions into a block, hash them into a hash tree, and scan through nonce value...
Definition: block.h:22
DifferenceFormatter::Ser
void Ser(Stream &s, I v)
Definition: blockencodings.h:21
CBlockHeaderAndShortTxIDs::CBlockHeaderAndShortTxIDs
CBlockHeaderAndShortTxIDs()
Definition: blockencodings.h:112
BlockTransactions
Definition: blockencodings.h:52
CTxMemPool
CTxMemPool stores valid-according-to-the-current-best-chain transactions that may be included in the ...
Definition: txmempool.h:495
VectorFormatter
Formatter to serialize/deserialize vector elements using another formatter.
Definition: serialize.h:718
BlockTransactions::BlockTransactions
BlockTransactions(const BlockTransactionsRequest &req)
Definition: blockencodings.h:59
DifferenceFormatter::Unser
void Unser(Stream &s, I &v)
Definition: blockencodings.h:28
CBlockHeaderAndShortTxIDs::shorttxids
std::vector< uint64_t > shorttxids
Definition: blockencodings.h:103
PrefilledTransaction::SERIALIZE_METHODS
SERIALIZE_METHODS(PrefilledTransaction, obj)
Definition: blockencodings.h:76
PrefilledTransaction
Definition: blockencodings.h:70
PartiallyDownloadedBlock::header
CBlockHeader header
Definition: blockencodings.h:145
ReadCompactSize
uint64_t ReadCompactSize(Stream &is, bool range_check=true)
Decode a CompactSize-encoded variable-length integer.
Definition: serialize.h:430
CustomUintFormatter
Serialization wrapper class for custom integers and enums.
Definition: serialize.h:623
PartiallyDownloadedBlock::InitData
ReadStatus InitData(const CBlockHeaderAndShortTxIDs &cmpctblock, const std::vector< std::pair< TxHash, CTransactionRef >> &extra_txn)
Definition: blockencodings.cpp:51
PartiallyDownloadedBlock::IsTxAvailable
bool IsTxAvailable(size_t index) const
Definition: blockencodings.cpp:208
BlockTransactions::txn
std::vector< CTransactionRef > txn
Definition: blockencodings.h:56
PrefilledTransaction::index
uint32_t index
Definition: blockencodings.h:73
CBlockHeaderAndShortTxIDs::nonce
uint64_t nonce
Definition: blockencodings.h:96
CBlockHeaderAndShortTxIDs::prefilledtxn
std::vector< PrefilledTransaction > prefilledtxn
Definition: blockencodings.h:104
DifferenceFormatter
Definition: blockencodings.h:17
Config
Definition: config.h:17
TxHash
A TxHash is the double sha256 hash of the full transaction data.
Definition: txid.h:22
BlockTransactionsRequest
Definition: blockencodings.h:40
BlockTransactions::blockhash
BlockHash blockhash
Definition: blockencodings.h:55
READ_STATUS_OK
@ READ_STATUS_OK
Definition: blockencodings.h:83
WriteCompactSize
void WriteCompactSize(CSizeComputer &os, uint64_t nSize)
Definition: serialize.h:1178
Using
static Wrapper< Formatter, T & > Using(T &&t)
Cause serialization/deserialization of an object to be done using a specified formatter class.
Definition: serialize.h:591
PartiallyDownloadedBlock::config
const Config * config
Definition: blockencodings.h:142
BlockTransactionsRequest::indices
std::vector< uint32_t > indices
Definition: blockencodings.h:44
ReadStatus_t
ReadStatus_t
Definition: blockencodings.h:82
READWRITE
#define READWRITE(...)
Definition: serialize.h:179
CBlockHeaderAndShortTxIDs::header
CBlockHeader header
Definition: blockencodings.h:109
BlockTransactionsRequest::SERIALIZE_METHODS
SERIALIZE_METHODS(BlockTransactionsRequest, obj)
Definition: blockencodings.h:46
PartiallyDownloadedBlock::pool
const CTxMemPool * pool
Definition: blockencodings.h:141
PartiallyDownloadedBlock::PartiallyDownloadedBlock
PartiallyDownloadedBlock(const Config &configIn, CTxMemPool *poolIn)
Definition: blockencodings.h:146
BlockHash
A BlockHash is a unqiue identifier for a block.
Definition: blockhash.h:13
PartiallyDownloadedBlock
Definition: blockencodings.h:137
CBlock
Definition: block.h:55
CBlockHeaderAndShortTxIDs
Definition: blockencodings.h:93
CBlockHeaderAndShortTxIDs::BlockTxCount
size_t BlockTxCount() const
Definition: blockencodings.h:118
PartiallyDownloadedBlock::prefilled_count
size_t prefilled_count
Definition: blockencodings.h:140
CTransactionRef
std::shared_ptr< const CTransaction > CTransactionRef
Definition: transaction.h:319
CBlockHeaderAndShortTxIDs::FillShortTxIDSelector
void FillShortTxIDSelector() const
Definition: blockencodings.cpp:34
PartiallyDownloadedBlock::txns_available
std::vector< CTransactionRef > txns_available
Definition: blockencodings.h:139
ReadStatus
enum ReadStatus_t ReadStatus
COMPACTSIZE
#define COMPACTSIZE(obj)
Definition: serialize.h:597
DifferenceFormatter::m_shift
uint64_t m_shift
Definition: blockencodings.h:18
PrefilledTransaction::tx
CTransactionRef tx
Definition: blockencodings.h:74
BlockTransactionsRequest::blockhash
BlockHash blockhash
Definition: blockencodings.h:43
CBlockHeaderAndShortTxIDs::shorttxidk1
uint64_t shorttxidk1
Definition: blockencodings.h:95
PartiallyDownloadedBlock::mempool_count
size_t mempool_count
Definition: blockencodings.h:140
CBlockHeaderAndShortTxIDs::shorttxidk0
uint64_t shorttxidk0
Definition: blockencodings.h:95
PartiallyDownloadedBlock::extra_count
size_t extra_count
Definition: blockencodings.h:140
READ_STATUS_FAILED
@ READ_STATUS_FAILED
Definition: blockencodings.h:88
CBlockHeaderAndShortTxIDs::SERIALIZE_METHODS
SERIALIZE_METHODS(CBlockHeaderAndShortTxIDs, obj)
Definition: blockencodings.h:122
READ_STATUS_INVALID
@ READ_STATUS_INVALID
Definition: blockencodings.h:86