Bitcoin Core  29.1.0
P2P Digital Currency
blockencodings.h
Go to the documentation of this file.
1 // Copyright (c) 2016-2022 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 #include <functional>
11 
12 class CTxMemPool;
14 namespace Consensus {
15 struct Params;
16 };
17 
18 // Transaction compression schemes for compact block relay can be introduced by writing
19 // an actual formatter here.
21 
23 {
24  uint64_t m_shift = 0;
25 
26 public:
27  template<typename Stream, typename I>
28  void Ser(Stream& s, I v)
29  {
30  if (v < m_shift || v >= std::numeric_limits<uint64_t>::max()) throw std::ios_base::failure("differential value overflow");
32  m_shift = uint64_t(v) + 1;
33  }
34  template<typename Stream, typename I>
35  void Unser(Stream& s, I& v)
36  {
37  uint64_t n = ReadCompactSize(s);
38  m_shift += n;
39  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");
40  v = I(m_shift++);
41  }
42 };
43 
45 public:
46  // A BlockTransactionsRequest message
48  std::vector<uint16_t> indexes;
49 
51  {
52  READWRITE(obj.blockhash, Using<VectorFormatter<DifferenceFormatter>>(obj.indexes));
53  }
54 };
55 
57 public:
58  // A BlockTransactions message
60  std::vector<CTransactionRef> txn;
61 
62  BlockTransactions() = default;
64  blockhash(req.blockhash), txn(req.indexes.size()) {}
65 
67  {
69  }
70 };
71 
72 // Dumb serialization/storage-helper for CBlockHeaderAndShortTxIDs and PartiallyDownloadedBlock
74  // Used as an offset since last prefilled tx in CBlockHeaderAndShortTxIDs,
75  // as a proper transaction-in-block-index in PartiallyDownloadedBlock
76  uint16_t index;
78 
79  SERIALIZE_METHODS(PrefilledTransaction, obj) { READWRITE(COMPACTSIZE(obj.index), TX_WITH_WITNESS(Using<TransactionCompression>(obj.tx))); }
80 };
81 
82 typedef enum ReadStatus_t
83 {
85  READ_STATUS_INVALID, // Invalid object, peer is sending bogus crap
86  READ_STATUS_FAILED, // Failed to process object
87  READ_STATUS_CHECKBLOCK_FAILED, // Used only by FillBlock to indicate a
88  // failure in CheckBlock.
89 } ReadStatus;
90 
92 private:
93  mutable uint64_t shorttxidk0, shorttxidk1;
94  uint64_t nonce;
95 
96  void FillShortTxIDSelector() const;
97 
99 
100 protected:
101  std::vector<uint64_t> shorttxids;
102  std::vector<PrefilledTransaction> prefilledtxn;
103 
104 public:
105  static constexpr int SHORTTXIDS_LENGTH = 6;
106 
108 
112  CBlockHeaderAndShortTxIDs() = default;
113 
117  CBlockHeaderAndShortTxIDs(const CBlock& block, const uint64_t nonce);
118 
119  uint64_t GetShortID(const Wtxid& wtxid) const;
120 
121  size_t BlockTxCount() const { return shorttxids.size() + prefilledtxn.size(); }
122 
124  {
125  READWRITE(obj.header, obj.nonce, Using<VectorFormatter<CustomUintFormatter<SHORTTXIDS_LENGTH>>>(obj.shorttxids), obj.prefilledtxn);
126  if (ser_action.ForRead()) {
127  if (obj.BlockTxCount() > std::numeric_limits<uint16_t>::max()) {
128  throw std::ios_base::failure("indexes overflowed 16 bits");
129  }
130  obj.FillShortTxIDSelector();
131  }
132  }
133 };
134 
136 protected:
137  std::vector<CTransactionRef> txn_available;
139  const CTxMemPool* pool;
140 public:
142 
143  // Can be overridden for testing
144  using CheckBlockFn = std::function<bool(const CBlock&, BlockValidationState&, const Consensus::Params&, bool, bool)>;
146 
147  explicit PartiallyDownloadedBlock(CTxMemPool* poolIn) : pool(poolIn) {}
148 
149  // extra_txn is a list of extra orphan/conflicted/etc transactions to look at
150  ReadStatus InitData(const CBlockHeaderAndShortTxIDs& cmpctblock, const std::vector<CTransactionRef>& extra_txn);
151  bool IsTxAvailable(size_t index) const;
152  ReadStatus FillBlock(CBlock& block, const std::vector<CTransactionRef>& vtx_missing);
153 };
154 
155 #endif // BITCOIN_BLOCKENCODINGS_H
std::shared_ptr< const CTransaction > CTransactionRef
Definition: transaction.h:423
enum ReadStatus_t ReadStatus
ReadStatus FillBlock(CBlock &block, const std::vector< CTransactionRef > &vtx_missing)
Definition: block.h:68
uint64_t ReadCompactSize(Stream &is, bool range_check=true)
Decode a CompactSize-encoded variable-length integer.
Definition: serialize.h:339
std::vector< uint16_t > indexes
std::vector< CTransactionRef > txn_available
#define COMPACTSIZE(obj)
Definition: serialize.h:501
BlockTransactions(const BlockTransactionsRequest &req)
CheckBlockFn m_check_block_mock
void FillShortTxIDSelector() const
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:497
ReadStatus_t
Transaction validation functions.
SERIALIZE_METHODS(PrefilledTransaction, obj)
CBlockHeaderAndShortTxIDs()=default
Dummy for deserialization.
std::function< bool(const CBlock &, BlockValidationState &, const Consensus::Params &, bool, bool)> CheckBlockFn
BlockTransactions()=default
std::vector< CTransactionRef > txn
void WriteCompactSize(SizeComputer &os, uint64_t nSize)
Definition: serialize.h:1097
SERIALIZE_METHODS(BlockTransactions, obj)
static constexpr int SHORTTXIDS_LENGTH
void Unser(Stream &s, I &v)
Parameters that influence chain consensus.
Definition: params.h:74
ReadStatus InitData(const CBlockHeaderAndShortTxIDs &cmpctblock, const std::vector< CTransactionRef > &extra_txn)
Default formatter.
Definition: serialize.h:771
256-bit opaque blob.
Definition: uint256.h:201
std::vector< uint64_t > shorttxids
const CTxMemPool * pool
CTxMemPool stores valid-according-to-the-current-best-chain transactions that may be included in the ...
Definition: txmempool.h:303
std::vector< PrefilledTransaction > prefilledtxn
bool IsTxAvailable(size_t index) const
Serialization wrapper class for custom integers and enums.
Definition: serialize.h:529
void Ser(Stream &s, I v)
SERIALIZE_METHODS(CBlockHeaderAndShortTxIDs, obj)
Formatter to serialize/deserialize vector elements using another formatter.
Definition: serialize.h:658
#define READWRITE(...)
Definition: serialize.h:156
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:21
static constexpr TransactionSerParams TX_WITH_WITNESS
Definition: transaction.h:195
uint64_t GetShortID(const Wtxid &wtxid) const
transaction_identifier represents the two canonical transaction identifier types (txid, wtxid).