Raven Core  3.0.0
P2P Digital Currency
blockencodings.h
Go to the documentation of this file.
1 // Copyright (c) 2016 The Bitcoin Core developers
2 // Copyright (c) 2017-2019 The Raven Core developers
3 // Distributed under the MIT software license, see the accompanying
4 // file COPYING or http://www.opensource.org/licenses/mit-license.php.
5 
6 #ifndef RAVEN_BLOCK_ENCODINGS_H
7 #define RAVEN_BLOCK_ENCODINGS_H
8 
9 #include "primitives/block.h"
10 
11 #include <memory>
12 
13 class CTxMemPool;
15 
16 // Dumb helper to handle CTransaction compression at serialize-time
18 private:
20 public:
21  explicit TransactionCompressor(CTransactionRef& txIn) : tx(txIn) {}
22 
24 
25  template <typename Stream, typename Operation>
26  inline void SerializationOp(Stream& s, Operation ser_action) {
27  READWRITE(tx); //TODO: Compress tx encoding
28  }
29 };
30 
32 public:
33  // A BlockTransactionsRequest message
35  std::vector<uint16_t> indexes;
36 
38 
39  template <typename Stream, typename Operation>
40  inline void SerializationOp(Stream& s, Operation ser_action) {
41  READWRITE(blockhash);
42  uint64_t indexes_size = (uint64_t)indexes.size();
43  READWRITE(COMPACTSIZE(indexes_size));
44  if (ser_action.ForRead()) {
45  size_t i = 0;
46  while (indexes.size() < indexes_size) {
47  indexes.resize(std::min((uint64_t)(1000 + indexes.size()), indexes_size));
48  for (; i < indexes.size(); i++) {
49  uint64_t index = 0;
50  READWRITE(COMPACTSIZE(index));
51  if (index > std::numeric_limits<uint16_t>::max())
52  throw std::ios_base::failure("index overflowed 16 bits");
53  indexes[i] = index;
54  }
55  }
56 
57  uint16_t offset = 0;
58  for (size_t j = 0; j < indexes.size(); j++) {
59  if (uint64_t(indexes[j]) + uint64_t(offset) > std::numeric_limits<uint16_t>::max())
60  throw std::ios_base::failure("indexes overflowed 16 bits");
61  indexes[j] = indexes[j] + offset;
62  offset = indexes[j] + 1;
63  }
64  } else {
65  for (size_t i = 0; i < indexes.size(); i++) {
66  uint64_t index = indexes[i] - (i == 0 ? 0 : (indexes[i - 1] + 1));
67  READWRITE(COMPACTSIZE(index));
68  }
69  }
70  }
71 };
72 
74 public:
75  // A BlockTransactions message
77  std::vector<CTransactionRef> txn;
78 
81  blockhash(req.blockhash), txn(req.indexes.size()) {}
82 
84 
85  template <typename Stream, typename Operation>
86  inline void SerializationOp(Stream& s, Operation ser_action) {
87  READWRITE(blockhash);
88  uint64_t txn_size = (uint64_t)txn.size();
89  READWRITE(COMPACTSIZE(txn_size));
90  if (ser_action.ForRead()) {
91  size_t i = 0;
92  while (txn.size() < txn_size) {
93  txn.resize(std::min((uint64_t)(1000 + txn.size()), txn_size));
94  for (; i < txn.size(); i++)
96  }
97  } else {
98  for (size_t i = 0; i < txn.size(); i++)
100  }
101  }
102 };
103 
104 // Dumb serialization/storage-helper for CBlockHeaderAndShortTxIDs and PartiallyDownloadedBlock
106  // Used as an offset since last prefilled tx in CBlockHeaderAndShortTxIDs,
107  // as a proper transaction-in-block-index in PartiallyDownloadedBlock
108  uint16_t index;
110 
112 
113  template <typename Stream, typename Operation>
114  inline void SerializationOp(Stream& s, Operation ser_action) {
115  uint64_t idx = index;
116  READWRITE(COMPACTSIZE(idx));
117  if (idx > std::numeric_limits<uint16_t>::max())
118  throw std::ios_base::failure("index overflowed 16-bits");
119  index = idx;
121  }
122 };
123 
124 typedef enum ReadStatus_t
125 {
127  READ_STATUS_INVALID, // Invalid object, peer is sending bogus crap
128  READ_STATUS_FAILED, // Failed to process object
129  READ_STATUS_CHECKBLOCK_FAILED, // Used only by FillBlock to indicate a
130  // failure in CheckBlock.
131 } ReadStatus;
132 
134 private:
135  mutable uint64_t shorttxidk0, shorttxidk1;
136  uint64_t nonce;
137 
138  void FillShortTxIDSelector() const;
139 
141 
142  static const int SHORTTXIDS_LENGTH = 6;
143 protected:
144  std::vector<uint64_t> shorttxids;
145  std::vector<PrefilledTransaction> prefilledtxn;
146 
147 public:
149 
150  // Dummy for deserialization
152 
153  CBlockHeaderAndShortTxIDs(const CBlock& block, bool fUseWTXID);
154 
155  uint64_t GetShortID(const uint256& txhash) const;
156 
157  size_t BlockTxCount() const { return shorttxids.size() + prefilledtxn.size(); }
158 
160 
161  template <typename Stream, typename Operation>
162  inline void SerializationOp(Stream& s, Operation ser_action) {
163  READWRITE(header);
164  READWRITE(nonce);
165 
166  uint64_t shorttxids_size = (uint64_t)shorttxids.size();
167  READWRITE(COMPACTSIZE(shorttxids_size));
168  if (ser_action.ForRead()) {
169  size_t i = 0;
170  while (shorttxids.size() < shorttxids_size) {
171  shorttxids.resize(std::min((uint64_t)(1000 + shorttxids.size()), shorttxids_size));
172  for (; i < shorttxids.size(); i++) {
173  uint32_t lsb = 0; uint16_t msb = 0;
174  READWRITE(lsb);
175  READWRITE(msb);
176  shorttxids[i] = (uint64_t(msb) << 32) | uint64_t(lsb);
177  static_assert(SHORTTXIDS_LENGTH == 6, "shorttxids serialization assumes 6-byte shorttxids");
178  }
179  }
180  } else {
181  for (size_t i = 0; i < shorttxids.size(); i++) {
182  uint32_t lsb = shorttxids[i] & 0xffffffff;
183  uint16_t msb = (shorttxids[i] >> 32) & 0xffff;
184  READWRITE(lsb);
185  READWRITE(msb);
186  }
187  }
188 
189  READWRITE(prefilledtxn);
190 
191  if (ser_action.ForRead())
192  FillShortTxIDSelector();
193  }
194 };
195 
197 protected:
198  std::vector<CTransactionRef> txn_available;
199  size_t prefilled_count = 0, mempool_count = 0, extra_count = 0;
201 public:
203  explicit PartiallyDownloadedBlock(CTxMemPool* poolIn) : pool(poolIn) {}
204 
205  // extra_txn is a list of extra transactions to look at, in <witness hash, reference> form
206  ReadStatus InitData(const CBlockHeaderAndShortTxIDs& cmpctblock, const std::vector<std::pair<uint256, CTransactionRef>>& extra_txn);
207  bool IsTxAvailable(size_t index) const;
208  ReadStatus FillBlock(CBlock& block, const std::vector<CTransactionRef>& vtx_missing);
209 };
210 
212 public:
213  std::string name;
214  int8_t units;
216  int8_t reissuable;
217  int8_t hasIPFS;
218  std::string ipfs;
219  int32_t nHeight;
220 
221  SerializedAssetData(const CDatabasedAssetData &assetData);
222 
224 
225  template <typename Stream, typename Operation>
226  inline void SerializationOp(Stream& s, Operation ser_action) {
227  READWRITE(name);
228  READWRITE(amount);
229  READWRITE(units);
230  READWRITE(reissuable);
231  READWRITE(hasIPFS);
232  READWRITE(ipfs);
233  READWRITE(nHeight);
234  }
235 
236 
237 };
238 #endif
enum ReadStatus_t ReadStatus
#define READWRITE(obj)
Definition: serialize.h:163
Definition: block.h:73
std::vector< uint16_t > indexes
std::vector< CTransactionRef > txn_available
#define COMPACTSIZE(obj)
Definition: serialize.h:368
BlockTransactions(const BlockTransactionsRequest &req)
PartiallyDownloadedBlock(CTxMemPool *poolIn)
std::shared_ptr< const CTransaction > CTransactionRef
Definition: transaction.h:436
ReadStatus_t
int64_t CAmount
Amount in corbies (Can be negative)
Definition: amount.h:13
void SerializationOp(Stream &s, Operation ser_action)
TransactionCompressor(CTransactionRef &txIn)
std::vector< CTransactionRef > txn
void SerializationOp(Stream &s, Operation ser_action)
void SerializationOp(Stream &s, Operation ser_action)
void SerializationOp(Stream &s, Operation ser_action)
void SerializationOp(Stream &s, Operation ser_action)
CTransactionRef & tx
void SerializationOp(Stream &s, Operation ser_action)
256-bit opaque blob.
Definition: uint256.h:123
std::vector< uint64_t > shorttxids
CTxMemPool stores valid-according-to-the-current-best-chain transactions that may be included in the ...
Definition: txmempool.h:416
std::vector< PrefilledTransaction > prefilledtxn
CTransactionRef tx
T & REF(const T &val)
Used to bypass the rule against non-const reference to temporary where it makes sense with wrappers s...
Definition: serialize.h:48
Nodes collect new transactions into a block, hash them into a hash tree, and scan through nonce value...
Definition: block.h:21