Bitcoin ABC 0.30.5
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#include <serialize.h>
10#include <shortidprocessor.h>
11
12#include <cstdint>
13#include <memory>
14#include <vector>
15
16class Config;
17class CTxMemPool;
18
19// Transaction compression schemes for compact block relay can be introduced by
20// writing an actual formatter here.
22
24public:
25 // A BlockTransactionsRequest message
27 std::vector<uint32_t> indices;
28
30 READWRITE(obj.blockhash,
32 }
33};
34
36public:
37 // A BlockTransactions message
39 std::vector<CTransactionRef> txn;
40
43 : blockhash(req.blockhash), txn(req.indices.size()) {}
44
46 READWRITE(obj.blockhash,
48 }
49};
50
51// Dumb serialization/storage-helper for CBlockHeaderAndShortTxIDs and
52// PartiallyDownloadedBlock
54 // Used as an offset since last prefilled tx in CBlockHeaderAndShortTxIDs
55 uint32_t index;
57
58 template <typename Stream> void SerData(Stream &s) { s << tx; }
59 template <typename Stream> void UnserData(Stream &s) { s >> tx; }
60};
61
63 uint32_t getIndex(const PrefilledTransaction &pt) const { return pt.index; }
65 return pt.tx;
66 }
67};
68
69typedef enum ReadStatus_t {
71 // Invalid object, peer is sending bogus crap.
72 // FIXME: differenciate bogus crap from crap that do not fit our policy.
74 // Failed to process object.
76 // Used only by FillBlock to indicate a failure in CheckBlock.
79
81private:
82 mutable uint64_t shorttxidk0, shorttxidk1;
83 uint64_t nonce;
84
85 void FillShortTxIDSelector() const;
86
88
89protected:
90 std::vector<uint64_t> shorttxids;
91 std::vector<PrefilledTransaction> prefilledtxn;
92
93public:
94 static constexpr int SHORTTXIDS_LENGTH = 6;
95
97
98 // Dummy for deserialization
100
101 explicit CBlockHeaderAndShortTxIDs(const CBlock &block);
102
103 uint64_t GetShortID(const TxHash &txhash) const;
104
105 size_t BlockTxCount() const {
106 return shorttxids.size() + prefilledtxn.size();
107 }
108
110 READWRITE(
111 obj.header, obj.nonce,
113 obj.shorttxids),
115 obj.prefilledtxn));
116
117 if (ser_action.ForRead() && obj.prefilledtxn.size() > 0) {
118 // Thanks to the DifferenceFormatter, the index values in the
119 // deserialized prefilled txs are absolute and sorted, so the last
120 // vector item has the highest index value.
121 uint64_t highestPrefilledIndex = obj.prefilledtxn.back().index;
122
123 // Make sure the indexes do not overflow 32 bits.
124 if (highestPrefilledIndex + obj.shorttxids.size() >
125 std::numeric_limits<uint32_t>::max()) {
126 throw std::ios_base::failure("indexes overflowed 32 bits");
127 }
128
129 // Make sure the indexes are contiguous. E.g. if there is no shortid
130 // but 2 prefilled txs with absolute indexes 0 and 2, then the tx at
131 // index 1 cannot be recovered.
132 if (highestPrefilledIndex >= obj.BlockTxCount()) {
133 throw std::ios_base::failure("non contiguous indexes");
134 }
135
136 obj.FillShortTxIDSelector();
137 }
138 }
139};
140
144 const CTransactionRef &rhs) const {
145 return lhs->GetHash() == rhs->GetHash();
146 }
147 };
148
153
154 // FIXME This better fits a unique_ptr, but the unit tests needs a copy
155 // operator for this class. It can be trivially changed when the unit tests
156 // are refactored.
157 std::shared_ptr<TransactionShortIdProcessor> shortidProcessor;
158
159protected:
163
164public:
167 : pool(poolIn), config(&configIn) {}
168
169 // extra_txn is a list of extra transactions to look at, in <txhash,
170 // reference> form.
172 InitData(const CBlockHeaderAndShortTxIDs &cmpctblock,
173 const std::vector<std::pair<TxHash, CTransactionRef>> &extra_txn);
174 bool IsTxAvailable(size_t index) const;
176 const std::vector<CTransactionRef> &vtx_missing);
177};
178
179#endif // BITCOIN_BLOCKENCODINGS_H
ReadStatus_t
@ READ_STATUS_OK
@ READ_STATUS_INVALID
@ READ_STATUS_CHECKBLOCK_FAILED
@ READ_STATUS_FAILED
enum ReadStatus_t ReadStatus
std::vector< CTransactionRef > txn
BlockTransactions(const BlockTransactionsRequest &req)
SERIALIZE_METHODS(BlockTransactions, obj)
std::vector< uint32_t > indices
SERIALIZE_METHODS(BlockTransactionsRequest, obj)
uint64_t GetShortID(const TxHash &txhash) const
void FillShortTxIDSelector() const
SERIALIZE_METHODS(CBlockHeaderAndShortTxIDs, obj)
std::vector< PrefilledTransaction > prefilledtxn
static constexpr int SHORTTXIDS_LENGTH
std::vector< uint64_t > shorttxids
Nodes collect new transactions into a block, hash them into a hash tree, and scan through nonce value...
Definition: block.h:23
Definition: block.h:60
CTxMemPool stores valid-according-to-the-current-best-chain transactions that may be included in the ...
Definition: txmempool.h:212
Definition: config.h:19
const CTxMemPool * pool
ReadStatus InitData(const CBlockHeaderAndShortTxIDs &cmpctblock, const std::vector< std::pair< TxHash, CTransactionRef > > &extra_txn)
bool IsTxAvailable(size_t index) const
ReadStatus FillBlock(CBlock &block, const std::vector< CTransactionRef > &vtx_missing)
PartiallyDownloadedBlock(const Config &configIn, CTxMemPool *poolIn)
std::shared_ptr< TransactionShortIdProcessor > shortidProcessor
std::shared_ptr< const CTransaction > CTransactionRef
Definition: transaction.h:315
static Wrapper< Formatter, T & > Using(T &&t)
Cause serialization/deserialization of an object to be done using a specified formatter class.
Definition: serialize.h:574
#define READWRITE(...)
Definition: serialize.h:166
A BlockHash is a unqiue identifier for a block.
Definition: blockhash.h:13
Serialization wrapper class for custom integers and enums.
Definition: serialize.h:606
Default formatter.
Definition: serialize.h:924
bool operator()(const CTransactionRef &lhs, const CTransactionRef &rhs) const
CTransactionRef tx
void SerData(Stream &s)
void UnserData(Stream &s)
uint32_t getIndex(const PrefilledTransaction &pt) const
CTransactionRef getItem(const PrefilledTransaction &pt) const
A TxHash is the double sha256 hash of the full transaction data.
Definition: txid.h:22
Formatter to serialize/deserialize vector elements using another formatter.
Definition: serialize.h:718