Bitcoin ABC 0.34.2
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
41 BlockTransactions() = default;
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{0}, shorttxidk1{0};
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
102
107 explicit CBlockHeaderAndShortTxIDs(const CBlock &block,
108 const uint64_t nonce);
109
110 uint64_t GetShortID(const TxHash &txhash) const;
111
112 size_t BlockTxCount() const {
113 return shorttxids.size() + prefilledtxn.size();
114 }
115
117 READWRITE(
118 obj.header, obj.nonce,
120 obj.shorttxids),
122 obj.prefilledtxn));
123
124 if (ser_action.ForRead()) {
125 if (obj.prefilledtxn.size() > 0) {
126 // Thanks to the DifferenceFormatter, the index values in the
127 // deserialized prefilled txs are absolute and sorted, so the
128 // last vector item has the highest index value.
129 uint64_t highestPrefilledIndex = obj.prefilledtxn.back().index;
130
131 // Make sure the indexes do not overflow 32 bits.
132 if (highestPrefilledIndex + obj.shorttxids.size() >
133 std::numeric_limits<uint32_t>::max()) {
134 throw std::ios_base::failure("indexes overflowed 32 bits");
135 }
136
137 // Make sure the indexes are contiguous. E.g. if there is no
138 // shortid but 2 prefilled txs with absolute indexes 0 and 2,
139 // then the tx at index 1 cannot be recovered.
140 if (highestPrefilledIndex >= obj.BlockTxCount()) {
141 throw std::ios_base::failure("non contiguous indexes");
142 }
143 }
144
145 // Always derive SipHash keys on the read path, including when
146 // prefilledtxn is empty (shorttxids-only compact blocks).
147 obj.FillShortTxIDSelector();
148 }
149 }
150};
151
155 const CTransactionRef &rhs) const {
156 return lhs->GetHash() == rhs->GetHash();
157 }
158 };
159
164
165 // FIXME This better fits a unique_ptr, but the unit tests needs a copy
166 // operator for this class. It can be trivially changed when the unit tests
167 // are refactored.
168 std::shared_ptr<TransactionShortIdProcessor> shortidProcessor;
169
170protected:
174
175public:
178 : pool(poolIn), config(&configIn) {}
179
180 // extra_txn is a list of extra orphan/conflicted/etc transactions to look
181 // at
183 const std::vector<CTransactionRef> &extra_txn);
184 bool IsTxAvailable(size_t index) const;
186 const std::vector<CTransactionRef> &vtx_missing);
187};
188
189#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)
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
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
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