Bitcoin ABC  0.22.13
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  static const int SHORTTXIDS_LENGTH = 6;
103 
104 protected:
105  std::vector<uint64_t> shorttxids;
106  std::vector<PrefilledTransaction> prefilledtxn;
107 
108 public:
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;
140  size_t prefilled_count = 0, mempool_count = 0, extra_count = 0;
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
std::shared_ptr< const CTransaction > CTransactionRef
Definition: transaction.h:338
enum ReadStatus_t ReadStatus
std::vector< CTransactionRef > txns_available
Definition: block.h:62
uint64_t ReadCompactSize(Stream &is)
Definition: serialize.h:442
void WriteCompactSize(CSizeComputer &os, uint64_t nSize)
Definition: serialize.h:1189
PartiallyDownloadedBlock(const Config &configIn, CTxMemPool *poolIn)
#define COMPACTSIZE(obj)
Definition: serialize.h:609
BlockTransactions(const BlockTransactionsRequest &req)
ReadStatus_t
SERIALIZE_METHODS(PrefilledTransaction, obj)
Definition: config.h:19
std::vector< CTransactionRef > txn
SERIALIZE_METHODS(BlockTransactions, obj)
A TxHash is the double sha256 hash of the full transaction data.
Definition: txid.h:22
void Unser(Stream &s, I &v)
Default formatter.
Definition: serialize.h:892
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:468
std::vector< PrefilledTransaction > prefilledtxn
A BlockHash is a unqiue identifier for a block.
Definition: blockhash.h:13
void Ser(Stream &s, I v)
SERIALIZE_METHODS(CBlockHeaderAndShortTxIDs, obj)
Formatter to serialize/deserialize vector elements using another formatter.
Definition: serialize.h:749
std::vector< uint32_t > indices
static Wrapper< Formatter, T & > Using(T &&t)
Cause serialization/deserialization of an object to be done using a specified formatter class...
Definition: serialize.h:603
#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:22