Bitcoin Core  0.19.99
P2P Digital Currency
blockencodings.h
Go to the documentation of this file.
1 // Copyright (c) 2016-2019 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 
11 class CTxMemPool;
12 
13 // Transaction compression schemes for compact block relay can be introduced by writing
14 // an actual formatter here.
16 
18 {
19  uint64_t m_shift = 0;
20 
21 public:
22  template<typename Stream, typename I>
23  void Ser(Stream& s, I v)
24  {
25  if (v < m_shift || v >= std::numeric_limits<uint64_t>::max()) throw std::ios_base::failure("differential value overflow");
26  WriteCompactSize(s, v - m_shift);
27  m_shift = uint64_t(v) + 1;
28  }
29  template<typename Stream, typename I>
30  void Unser(Stream& s, I& v)
31  {
32  uint64_t n = ReadCompactSize(s);
33  m_shift += n;
34  if (m_shift < n || m_shift >= std::numeric_limits<uint64_t>::max() || m_shift < std::numeric_limits<I>::min() || m_shift > std::numeric_limits<I>::max()) throw std::ios_base::failure("differential value overflow");
35  v = I(m_shift++);
36  }
37 };
38 
40 public:
41  // A BlockTransactionsRequest message
43  std::vector<uint16_t> indexes;
44 
46  {
47  READWRITE(obj.blockhash, Using<VectorFormatter<DifferenceFormatter>>(obj.indexes));
48  }
49 };
50 
52 public:
53  // A BlockTransactions message
55  std::vector<CTransactionRef> txn;
56 
59  blockhash(req.blockhash), txn(req.indexes.size()) {}
60 
62  {
64  }
65 };
66 
67 // Dumb serialization/storage-helper for CBlockHeaderAndShortTxIDs and PartiallyDownloadedBlock
69  // Used as an offset since last prefilled tx in CBlockHeaderAndShortTxIDs,
70  // as a proper transaction-in-block-index in PartiallyDownloadedBlock
71  uint16_t index;
73 
74  SERIALIZE_METHODS(PrefilledTransaction, obj) { READWRITE(COMPACTSIZE(obj.index), Using<TransactionCompression>(obj.tx)); }
75 };
76 
77 typedef enum ReadStatus_t
78 {
80  READ_STATUS_INVALID, // Invalid object, peer is sending bogus crap
81  READ_STATUS_FAILED, // Failed to process object
82  READ_STATUS_CHECKBLOCK_FAILED, // Used only by FillBlock to indicate a
83  // failure in CheckBlock.
84 } ReadStatus;
85 
87 private:
88  mutable uint64_t shorttxidk0, shorttxidk1;
89  uint64_t nonce;
90 
91  void FillShortTxIDSelector() const;
92 
94 
95  static const int SHORTTXIDS_LENGTH = 6;
96 protected:
97  std::vector<uint64_t> shorttxids;
98  std::vector<PrefilledTransaction> prefilledtxn;
99 
100 public:
102 
103  // Dummy for deserialization
105 
106  CBlockHeaderAndShortTxIDs(const CBlock& block, bool fUseWTXID);
107 
108  uint64_t GetShortID(const uint256& txhash) const;
109 
110  size_t BlockTxCount() const { return shorttxids.size() + prefilledtxn.size(); }
111 
113  {
114  READWRITE(obj.header, obj.nonce, Using<VectorFormatter<CustomUintFormatter<SHORTTXIDS_LENGTH>>>(obj.shorttxids), obj.prefilledtxn);
115  if (ser_action.ForRead()) {
116  if (obj.BlockTxCount() > std::numeric_limits<uint16_t>::max()) {
117  throw std::ios_base::failure("indexes overflowed 16 bits");
118  }
119  obj.FillShortTxIDSelector();
120  }
121  }
122 };
123 
125 protected:
126  std::vector<CTransactionRef> txn_available;
127  size_t prefilled_count = 0, mempool_count = 0, extra_count = 0;
129 public:
131  explicit PartiallyDownloadedBlock(CTxMemPool* poolIn) : pool(poolIn) {}
132 
133  // extra_txn is a list of extra transactions to look at, in <witness hash, reference> form
134  ReadStatus InitData(const CBlockHeaderAndShortTxIDs& cmpctblock, const std::vector<std::pair<uint256, CTransactionRef>>& extra_txn);
135  bool IsTxAvailable(size_t index) const;
136  ReadStatus FillBlock(CBlock& block, const std::vector<CTransactionRef>& vtx_missing);
137 };
138 
139 #endif // BITCOIN_BLOCKENCODINGS_H
std::shared_ptr< const CTransaction > CTransactionRef
Definition: transaction.h:408
enum ReadStatus_t ReadStatus
Definition: block.h:72
uint64_t ReadCompactSize(Stream &is)
Definition: serialize.h:342
void WriteCompactSize(CSizeComputer &os, uint64_t nSize)
Definition: serialize.h:1133
std::vector< uint16_t > indexes
std::vector< CTransactionRef > txn_available
#define COMPACTSIZE(obj)
Definition: serialize.h:503
BlockTransactions(const BlockTransactionsRequest &req)
PartiallyDownloadedBlock(CTxMemPool *poolIn)
static Wrapper< Formatter, T & > Using(T &&t)
Cause serialization/deserialization of an object to be done using a specified formatter class...
Definition: serialize.h:499
ReadStatus_t
SERIALIZE_METHODS(PrefilledTransaction, obj)
std::vector< CTransactionRef > txn
SERIALIZE_METHODS(BlockTransactions, obj)
void Unser(Stream &s, I &v)
Default formatter.
Definition: serialize.h:765
256-bit opaque blob.
Definition: uint256.h:120
std::vector< uint64_t > shorttxids
CTxMemPool stores valid-according-to-the-current-best-chain transactions that may be included in the ...
Definition: txmempool.h:443
std::vector< PrefilledTransaction > prefilledtxn
void Ser(Stream &s, I v)
SERIALIZE_METHODS(CBlockHeaderAndShortTxIDs, obj)
Formatter to serialize/deserialize vector elements using another formatter.
Definition: serialize.h:646
#define READWRITE(...)
Definition: serialize.h:191
CTransactionRef tx
SERIALIZE_METHODS(BlockTransactionsRequest, obj)
Nodes collect new transactions into a block, hash them into a hash tree, and scan through nonce value...
Definition: block.h:20