Bitcoin ABC 0.34.3
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 <functional>
14#include <memory>
15#include <vector>
16
17class Config;
18class CTxMemPool;
21namespace Consensus {
22struct Params;
23};
24
25// Transaction compression schemes for compact block relay can be introduced by
26// writing an actual formatter here.
28
30public:
31 // A BlockTransactionsRequest message
33 std::vector<uint32_t> indices;
34
36 READWRITE(obj.blockhash,
38 }
39};
40
42public:
43 // A BlockTransactions message
45 std::vector<CTransactionRef> txn;
46
47 BlockTransactions() = default;
49 : blockhash(req.blockhash), txn(req.indices.size()) {}
50
52 READWRITE(obj.blockhash,
54 }
55};
56
57// Dumb serialization/storage-helper for CBlockHeaderAndShortTxIDs and
58// PartiallyDownloadedBlock
60 // Used as an offset since last prefilled tx in CBlockHeaderAndShortTxIDs
61 uint32_t index;
63
64 template <typename Stream> void SerData(Stream &s) { s << tx; }
65 template <typename Stream> void UnserData(Stream &s) { s >> tx; }
66};
67
69 uint32_t getIndex(const PrefilledTransaction &pt) const { return pt.index; }
71 return pt.tx;
72 }
73};
74
75typedef enum ReadStatus_t {
77 // Invalid object, peer is sending bogus crap.
78 // FIXME: differenciate bogus crap from crap that do not fit our policy.
80 // Failed to process object.
83
85private:
86 mutable uint64_t shorttxidk0{0}, shorttxidk1{0};
87 uint64_t nonce;
88
89 void FillShortTxIDSelector() const;
90
92
93protected:
94 std::vector<uint64_t> shorttxids;
95 std::vector<PrefilledTransaction> prefilledtxn;
96
97public:
98 static constexpr int SHORTTXIDS_LENGTH = 6;
99
101
106
111 explicit CBlockHeaderAndShortTxIDs(const CBlock &block,
112 const uint64_t nonce);
113
114 uint64_t GetShortID(const TxHash &txhash) const;
115
116 size_t BlockTxCount() const {
117 return shorttxids.size() + prefilledtxn.size();
118 }
119
121 READWRITE(
122 obj.header, obj.nonce,
124 obj.shorttxids),
126 obj.prefilledtxn));
127
128 if (ser_action.ForRead()) {
129 if (obj.prefilledtxn.size() > 0) {
130 // Thanks to the DifferenceFormatter, the index values in the
131 // deserialized prefilled txs are absolute and sorted, so the
132 // last vector item has the highest index value.
133 uint64_t highestPrefilledIndex = obj.prefilledtxn.back().index;
134
135 // Make sure the indexes do not overflow 32 bits.
136 if (highestPrefilledIndex + obj.shorttxids.size() >
137 std::numeric_limits<uint32_t>::max()) {
138 throw std::ios_base::failure("indexes overflowed 32 bits");
139 }
140
141 // Make sure the indexes are contiguous. E.g. if there is no
142 // shortid but 2 prefilled txs with absolute indexes 0 and 2,
143 // then the tx at index 1 cannot be recovered.
144 if (highestPrefilledIndex >= obj.BlockTxCount()) {
145 throw std::ios_base::failure("non contiguous indexes");
146 }
147 }
148
149 // Always derive SipHash keys on the read path, including when
150 // prefilledtxn is empty (shorttxids-only compact blocks).
151 obj.FillShortTxIDSelector();
152 }
153 }
154};
155
159 const CTransactionRef &rhs) const {
160 return lhs->GetHash() == rhs->GetHash();
161 }
162 };
163
168
169 // FIXME This better fits a unique_ptr, but the unit tests needs a copy
170 // operator for this class. It can be trivially changed when the unit tests
171 // are refactored.
172 std::shared_ptr<TransactionShortIdProcessor> shortidProcessor;
173
174protected:
178
179public:
181
182 // Can be overridden for testing
183 using IsBlockMutatedFn = std::function<bool(const CBlock &)>;
185
187 : pool(poolIn), config(&configIn) {}
188
189 // extra_txn is a list of extra orphan/conflicted/etc transactions to look
190 // at
192 const std::vector<CTransactionRef> &extra_txn);
193 bool IsTxAvailable(size_t index) const;
195 const std::vector<CTransactionRef> &vtx_missing);
196};
197
198#endif // BITCOIN_BLOCKENCODINGS_H
ReadStatus_t
@ READ_STATUS_OK
@ READ_STATUS_INVALID
@ READ_STATUS_FAILED
enum ReadStatus_t ReadStatus
std::vector< CTransactionRef > txn
BlockTransactions(const BlockTransactionsRequest &req)
BlockTransactions()=default
SERIALIZE_METHODS(BlockTransactions, obj)
std::vector< uint32_t > indices
SERIALIZE_METHODS(BlockTransactionsRequest, obj)
uint64_t GetShortID(const TxHash &txhash) const
CBlockHeaderAndShortTxIDs()=default
Dummy for deserialization.
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:222
Definition: config.h:19
const CTxMemPool * pool
std::function< bool(const CBlock &)> IsBlockMutatedFn
IsBlockMutatedFn m_check_block_mutated_mock
ReadStatus InitData(const CBlockHeaderAndShortTxIDs &cmpctblock, const std::vector< 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:630
#define READWRITE(...)
Definition: serialize.h:176
A BlockHash is a unqiue identifier for a block.
Definition: blockhash.h:13
Parameters that influence chain consensus.
Definition: params.h:34
Serialization wrapper class for custom integers and enums.
Definition: serialize.h:662
Default formatter.
Definition: serialize.h:964
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:26
Formatter to serialize/deserialize vector elements using another formatter.
Definition: serialize.h:774