Bitcoin Core  22.99.0
P2P Digital Currency
blockencodings.h
Go to the documentation of this file.
1 // Copyright (c) 2016-2020 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 protected:
96  std::vector<uint64_t> shorttxids;
97  std::vector<PrefilledTransaction> prefilledtxn;
98 
99 public:
100  static constexpr int SHORTTXIDS_LENGTH = 6;
101 
103 
104  // Dummy for deserialization
106 
107  CBlockHeaderAndShortTxIDs(const CBlock& block, bool fUseWTXID);
108 
109  uint64_t GetShortID(const uint256& txhash) const;
110 
111  size_t BlockTxCount() const { return shorttxids.size() + prefilledtxn.size(); }
112 
114  {
115  READWRITE(obj.header, obj.nonce, Using<VectorFormatter<CustomUintFormatter<SHORTTXIDS_LENGTH>>>(obj.shorttxids), obj.prefilledtxn);
116  if (ser_action.ForRead()) {
117  if (obj.BlockTxCount() > std::numeric_limits<uint16_t>::max()) {
118  throw std::ios_base::failure("indexes overflowed 16 bits");
119  }
120  obj.FillShortTxIDSelector();
121  }
122  }
123 };
124 
126 protected:
127  std::vector<CTransactionRef> txn_available;
129  const CTxMemPool* pool;
130 public:
132  explicit PartiallyDownloadedBlock(CTxMemPool* poolIn) : pool(poolIn) {}
133 
134  // extra_txn is a list of extra transactions to look at, in <witness hash, reference> form
135  ReadStatus InitData(const CBlockHeaderAndShortTxIDs& cmpctblock, const std::vector<std::pair<uint256, CTransactionRef>>& extra_txn);
136  bool IsTxAvailable(size_t index) const;
137  ReadStatus FillBlock(CBlock& block, const std::vector<CTransactionRef>& vtx_missing);
138 };
139 
140 #endif // BITCOIN_BLOCKENCODINGS_H
DefaultFormatter
Default formatter.
Definition: serialize.h:684
block.h
BlockTransactions::SERIALIZE_METHODS
SERIALIZE_METHODS(BlockTransactions, obj)
Definition: blockencodings.h:61
CBlockHeaderAndShortTxIDs::SHORTTXIDS_LENGTH
static constexpr int SHORTTXIDS_LENGTH
Definition: blockencodings.h:100
BlockTransactions::BlockTransactions
BlockTransactions()
Definition: blockencodings.h:57
PartiallyDownloadedBlock::FillBlock
ReadStatus FillBlock(CBlock &block, const std::vector< CTransactionRef > &vtx_missing)
Definition: blockencodings.cpp:176
READ_STATUS_CHECKBLOCK_FAILED
@ READ_STATUS_CHECKBLOCK_FAILED
Definition: blockencodings.h:82
CBlockHeader
Nodes collect new transactions into a block, hash them into a hash tree, and scan through nonce value...
Definition: block.h:20
DifferenceFormatter::Ser
void Ser(Stream &s, I v)
Definition: blockencodings.h:23
CBlockHeaderAndShortTxIDs::CBlockHeaderAndShortTxIDs
CBlockHeaderAndShortTxIDs()
Definition: blockencodings.h:105
extra_txn
std::vector< std::pair< uint256, CTransactionRef > > extra_txn
Definition: blockencodings_tests.cpp:15
BlockTransactions
Definition: blockencodings.h:51
CTxMemPool
CTxMemPool stores valid-according-to-the-current-best-chain transactions that may be included in the ...
Definition: txmempool.h:477
VectorFormatter
Formatter to serialize/deserialize vector elements using another formatter.
Definition: serialize.h:565
BlockTransactions::BlockTransactions
BlockTransactions(const BlockTransactionsRequest &req)
Definition: blockencodings.h:58
DifferenceFormatter::Unser
void Unser(Stream &s, I &v)
Definition: blockencodings.h:30
CBlockHeaderAndShortTxIDs::shorttxids
std::vector< uint64_t > shorttxids
Definition: blockencodings.h:96
PrefilledTransaction::SERIALIZE_METHODS
SERIALIZE_METHODS(PrefilledTransaction, obj)
Definition: blockencodings.h:74
PrefilledTransaction
Definition: blockencodings.h:68
PartiallyDownloadedBlock::header
CBlockHeader header
Definition: blockencodings.h:131
PrefilledTransaction::index
uint16_t index
Definition: blockencodings.h:71
ReadCompactSize
uint64_t ReadCompactSize(Stream &is, bool range_check=true)
Decode a CompactSize-encoded variable-length integer.
Definition: serialize.h:282
CustomUintFormatter
Serialization wrapper class for custom integers and enums.
Definition: serialize.h:472
CTransactionRef
std::shared_ptr< const CTransaction > CTransactionRef
Definition: transaction.h:386
PartiallyDownloadedBlock::IsTxAvailable
bool IsTxAvailable(size_t index) const
Definition: blockencodings.cpp:170
BlockTransactions::txn
std::vector< CTransactionRef > txn
Definition: blockencodings.h:55
CBlockHeaderAndShortTxIDs::nonce
uint64_t nonce
Definition: blockencodings.h:89
PartiallyDownloadedBlock::PartiallyDownloadedBlock
PartiallyDownloadedBlock(CTxMemPool *poolIn)
Definition: blockencodings.h:132
CBlockHeaderAndShortTxIDs::prefilledtxn
std::vector< PrefilledTransaction > prefilledtxn
Definition: blockencodings.h:97
DifferenceFormatter
Definition: blockencodings.h:17
PartiallyDownloadedBlock::InitData
ReadStatus InitData(const CBlockHeaderAndShortTxIDs &cmpctblock, const std::vector< std::pair< uint256, CTransactionRef >> &extra_txn)
Definition: blockencodings.cpp:49
BlockTransactionsRequest
Definition: blockencodings.h:39
READ_STATUS_OK
@ READ_STATUS_OK
Definition: blockencodings.h:79
PartiallyDownloadedBlock::txn_available
std::vector< CTransactionRef > txn_available
Definition: blockencodings.h:127
WriteCompactSize
void WriteCompactSize(CSizeComputer &os, uint64_t nSize)
Definition: serialize.h:1074
ReadStatus_t
ReadStatus_t
Definition: blockencodings.h:77
uint256
256-bit opaque blob.
Definition: uint256.h:124
READWRITE
#define READWRITE(...)
Definition: serialize.h:147
CBlockHeaderAndShortTxIDs::header
CBlockHeader header
Definition: blockencodings.h:102
BlockTransactionsRequest::SERIALIZE_METHODS
SERIALIZE_METHODS(BlockTransactionsRequest, obj)
Definition: blockencodings.h:45
PartiallyDownloadedBlock::pool
const CTxMemPool * pool
Definition: blockencodings.h:129
BlockTransactionsRequest::indexes
std::vector< uint16_t > indexes
Definition: blockencodings.h:43
PartiallyDownloadedBlock
Definition: blockencodings.h:125
CBlock
Definition: block.h:62
CBlockHeaderAndShortTxIDs
Definition: blockencodings.h:86
CBlockHeaderAndShortTxIDs::BlockTxCount
size_t BlockTxCount() const
Definition: blockencodings.h:111
CBlockHeaderAndShortTxIDs::GetShortID
uint64_t GetShortID(const uint256 &txhash) const
Definition: blockencodings.cpp:42
PartiallyDownloadedBlock::prefilled_count
size_t prefilled_count
Definition: blockencodings.h:128
CBlockHeaderAndShortTxIDs::FillShortTxIDSelector
void FillShortTxIDSelector() const
Definition: blockencodings.cpp:31
ReadStatus
enum ReadStatus_t ReadStatus
COMPACTSIZE
#define COMPACTSIZE(obj)
Definition: serialize.h:444
BlockTransactionsRequest::blockhash
uint256 blockhash
Definition: blockencodings.h:42
DifferenceFormatter::m_shift
uint64_t m_shift
Definition: blockencodings.h:19
PrefilledTransaction::tx
CTransactionRef tx
Definition: blockencodings.h:72
BlockTransactions::blockhash
uint256 blockhash
Definition: blockencodings.h:54
CBlockHeaderAndShortTxIDs::shorttxidk1
uint64_t shorttxidk1
Definition: blockencodings.h:88
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:440
PartiallyDownloadedBlock::mempool_count
size_t mempool_count
Definition: blockencodings.h:128
CBlockHeaderAndShortTxIDs::shorttxidk0
uint64_t shorttxidk0
Definition: blockencodings.h:88
PartiallyDownloadedBlock::extra_count
size_t extra_count
Definition: blockencodings.h:128
READ_STATUS_FAILED
@ READ_STATUS_FAILED
Definition: blockencodings.h:81
CBlockHeaderAndShortTxIDs::SERIALIZE_METHODS
SERIALIZE_METHODS(CBlockHeaderAndShortTxIDs, obj)
Definition: blockencodings.h:113
READ_STATUS_INVALID
@ READ_STATUS_INVALID
Definition: blockencodings.h:80