You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

blockencodings.h 6.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. // Copyright (c) 2016 The Starwels developers
  2. // Distributed under the MIT software license, see the accompanying
  3. // file COPYING or http://www.opensource.org/licenses/mit-license.php.
  4. #ifndef STARWELS_BLOCK_ENCODINGS_H
  5. #define STARWELS_BLOCK_ENCODINGS_H
  6. #include "primitives/block.h"
  7. #include <memory>
  8. class CTxMemPool;
  9. // Dumb helper to handle CTransaction compression at serialize-time
  10. struct TransactionCompressor {
  11. private:
  12. CTransactionRef& tx;
  13. public:
  14. TransactionCompressor(CTransactionRef& txIn) : tx(txIn) {}
  15. ADD_SERIALIZE_METHODS;
  16. template <typename Stream, typename Operation>
  17. inline void SerializationOp(Stream& s, Operation ser_action) {
  18. READWRITE(tx); //TODO: Compress tx encoding
  19. }
  20. };
  21. class BlockTransactionsRequest {
  22. public:
  23. // A BlockTransactionsRequest message
  24. uint256 blockhash;
  25. std::vector<uint16_t> indexes;
  26. ADD_SERIALIZE_METHODS;
  27. template <typename Stream, typename Operation>
  28. inline void SerializationOp(Stream& s, Operation ser_action) {
  29. READWRITE(blockhash);
  30. uint64_t indexes_size = (uint64_t)indexes.size();
  31. READWRITE(COMPACTSIZE(indexes_size));
  32. if (ser_action.ForRead()) {
  33. size_t i = 0;
  34. while (indexes.size() < indexes_size) {
  35. indexes.resize(std::min((uint64_t)(1000 + indexes.size()), indexes_size));
  36. for (; i < indexes.size(); i++) {
  37. uint64_t index = 0;
  38. READWRITE(COMPACTSIZE(index));
  39. if (index > std::numeric_limits<uint16_t>::max())
  40. throw std::ios_base::failure("index overflowed 16 bits");
  41. indexes[i] = index;
  42. }
  43. }
  44. uint16_t offset = 0;
  45. for (size_t j = 0; j < indexes.size(); j++) {
  46. if (uint64_t(indexes[j]) + uint64_t(offset) > std::numeric_limits<uint16_t>::max())
  47. throw std::ios_base::failure("indexes overflowed 16 bits");
  48. indexes[j] = indexes[j] + offset;
  49. offset = indexes[j] + 1;
  50. }
  51. } else {
  52. for (size_t i = 0; i < indexes.size(); i++) {
  53. uint64_t index = indexes[i] - (i == 0 ? 0 : (indexes[i - 1] + 1));
  54. READWRITE(COMPACTSIZE(index));
  55. }
  56. }
  57. }
  58. };
  59. class BlockTransactions {
  60. public:
  61. // A BlockTransactions message
  62. uint256 blockhash;
  63. std::vector<CTransactionRef> txn;
  64. BlockTransactions() {}
  65. BlockTransactions(const BlockTransactionsRequest& req) :
  66. blockhash(req.blockhash), txn(req.indexes.size()) {}
  67. ADD_SERIALIZE_METHODS;
  68. template <typename Stream, typename Operation>
  69. inline void SerializationOp(Stream& s, Operation ser_action) {
  70. READWRITE(blockhash);
  71. uint64_t txn_size = (uint64_t)txn.size();
  72. READWRITE(COMPACTSIZE(txn_size));
  73. if (ser_action.ForRead()) {
  74. size_t i = 0;
  75. while (txn.size() < txn_size) {
  76. txn.resize(std::min((uint64_t)(1000 + txn.size()), txn_size));
  77. for (; i < txn.size(); i++)
  78. READWRITE(REF(TransactionCompressor(txn[i])));
  79. }
  80. } else {
  81. for (size_t i = 0; i < txn.size(); i++)
  82. READWRITE(REF(TransactionCompressor(txn[i])));
  83. }
  84. }
  85. };
  86. // Dumb serialization/storage-helper for CBlockHeaderAndShortTxIDs and PartiallyDownloadedBlock
  87. struct PrefilledTransaction {
  88. // Used as an offset since last prefilled tx in CBlockHeaderAndShortTxIDs,
  89. // as a proper transaction-in-block-index in PartiallyDownloadedBlock
  90. uint16_t index;
  91. CTransactionRef tx;
  92. ADD_SERIALIZE_METHODS;
  93. template <typename Stream, typename Operation>
  94. inline void SerializationOp(Stream& s, Operation ser_action) {
  95. uint64_t idx = index;
  96. READWRITE(COMPACTSIZE(idx));
  97. if (idx > std::numeric_limits<uint16_t>::max())
  98. throw std::ios_base::failure("index overflowed 16-bits");
  99. index = idx;
  100. READWRITE(REF(TransactionCompressor(tx)));
  101. }
  102. };
  103. typedef enum ReadStatus_t
  104. {
  105. READ_STATUS_OK,
  106. READ_STATUS_INVALID, // Invalid object, peer is sending bogus crap
  107. READ_STATUS_FAILED, // Failed to process object
  108. READ_STATUS_CHECKBLOCK_FAILED, // Used only by FillBlock to indicate a
  109. // failure in CheckBlock.
  110. } ReadStatus;
  111. class CBlockHeaderAndShortTxIDs {
  112. private:
  113. mutable uint64_t shorttxidk0, shorttxidk1;
  114. uint64_t nonce;
  115. void FillShortTxIDSelector() const;
  116. friend class PartiallyDownloadedBlock;
  117. static const int SHORTTXIDS_LENGTH = 6;
  118. protected:
  119. std::vector<uint64_t> shorttxids;
  120. std::vector<PrefilledTransaction> prefilledtxn;
  121. public:
  122. CBlockHeader header;
  123. // Dummy for deserialization
  124. CBlockHeaderAndShortTxIDs() {}
  125. CBlockHeaderAndShortTxIDs(const CBlock& block, bool fUseWTXID);
  126. uint64_t GetShortID(const uint256& txhash) const;
  127. size_t BlockTxCount() const { return shorttxids.size() + prefilledtxn.size(); }
  128. ADD_SERIALIZE_METHODS;
  129. template <typename Stream, typename Operation>
  130. inline void SerializationOp(Stream& s, Operation ser_action) {
  131. READWRITE(header);
  132. READWRITE(nonce);
  133. uint64_t shorttxids_size = (uint64_t)shorttxids.size();
  134. READWRITE(COMPACTSIZE(shorttxids_size));
  135. if (ser_action.ForRead()) {
  136. size_t i = 0;
  137. while (shorttxids.size() < shorttxids_size) {
  138. shorttxids.resize(std::min((uint64_t)(1000 + shorttxids.size()), shorttxids_size));
  139. for (; i < shorttxids.size(); i++) {
  140. uint32_t lsb = 0; uint16_t msb = 0;
  141. READWRITE(lsb);
  142. READWRITE(msb);
  143. shorttxids[i] = (uint64_t(msb) << 32) | uint64_t(lsb);
  144. static_assert(SHORTTXIDS_LENGTH == 6, "shorttxids serialization assumes 6-byte shorttxids");
  145. }
  146. }
  147. } else {
  148. for (size_t i = 0; i < shorttxids.size(); i++) {
  149. uint32_t lsb = shorttxids[i] & 0xffffffff;
  150. uint16_t msb = (shorttxids[i] >> 32) & 0xffff;
  151. READWRITE(lsb);
  152. READWRITE(msb);
  153. }
  154. }
  155. READWRITE(prefilledtxn);
  156. if (ser_action.ForRead())
  157. FillShortTxIDSelector();
  158. }
  159. };
  160. class PartiallyDownloadedBlock {
  161. protected:
  162. std::vector<CTransactionRef> txn_available;
  163. size_t prefilled_count = 0, mempool_count = 0, extra_count = 0;
  164. CTxMemPool* pool;
  165. public:
  166. CBlockHeader header;
  167. PartiallyDownloadedBlock(CTxMemPool* poolIn) : pool(poolIn) {}
  168. // extra_txn is a list of extra transactions to look at, in <witness hash, reference> form
  169. ReadStatus InitData(const CBlockHeaderAndShortTxIDs& cmpctblock, const std::vector<std::pair<uint256, CTransactionRef>>& extra_txn);
  170. bool IsTxAvailable(size_t index) const;
  171. ReadStatus FillBlock(CBlock& block, const std::vector<CTransactionRef>& vtx_missing);
  172. };
  173. #endif