Bitcoin Core 32.99.0
P2P Digital Currency
Loading...
Searching...
No Matches
blockencodings.h
Go to the documentation of this file.
1// Copyright (c) 2016-present 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 <crypto/siphash.h>
9#include <primitives/block.h>
10
11#include <functional>
12
13class CTxMemPool;
15namespace Consensus {
16struct Params;
17};
18
19// Transaction compression schemes for compact block relay can be introduced by writing
20// an actual formatter here.
22
24{
26
27public:
28 template<typename Stream, typename I>
29 void Ser(Stream& s, I v)
30 {
31 if (v < m_shift || v >= std::numeric_limits<uint64_t>::max()) throw std::ios_base::failure("differential value overflow");
33 m_shift = uint64_t(v) + 1;
34 }
35 template<typename Stream, typename I>
36 void Unser(Stream& s, I& v)
37 {
39 m_shift += n;
40 if (m_shift < n || m_shift >= std::numeric_limits<uint64_t>::max() || m_shift < std::numeric_limits<I>::min() || m_shift > std::numeric_limits<I>::max()) throw std::ios_base::failure("differential value overflow");
41 v = I(m_shift++);
42 }
43};
44
46public:
47 // A BlockTransactionsRequest message
49 std::vector<uint16_t> indexes;
50
55};
56
58public:
59 // A BlockTransactions message
61 std::vector<CTransactionRef> txn;
62
63 BlockTransactions() = default;
65 blockhash(req.blockhash), txn(req.indexes.size()) {}
66
71};
72
73// Dumb serialization/storage-helper for CBlockHeaderAndShortTxIDs and PartiallyDownloadedBlock
75 // Used as an offset since last prefilled tx in CBlockHeaderAndShortTxIDs,
76 // as a proper transaction-in-block-index in PartiallyDownloadedBlock
79
81};
82
83typedef enum ReadStatus_t
84{
86 READ_STATUS_INVALID, // Invalid object, peer is sending bogus crap
87 READ_STATUS_FAILED, // Failed to process object
89
91 mutable std::optional<PresaltedSipHasher> m_hasher;
93
94 void FillShortTxIDSelector() const;
95
97
98protected:
99 std::vector<uint64_t> shorttxids;
100 std::vector<PrefilledTransaction> prefilledtxn;
101
102public:
103 static constexpr int SHORTTXIDS_LENGTH = 6;
104
106
111
116
117 uint64_t GetShortID(const Wtxid& wtxid) const;
118
119 size_t BlockTxCount() const { return shorttxids.size() + prefilledtxn.size(); }
120
122 {
123 READWRITE(obj.header, obj.nonce, Using<VectorFormatter<CustomUintFormatter<SHORTTXIDS_LENGTH>>>(obj.shorttxids), obj.prefilledtxn);
124 if (ser_action.ForRead()) {
125 if (obj.BlockTxCount() > std::numeric_limits<uint16_t>::max()) {
126 throw std::ios_base::failure("indexes overflowed 16 bits");
127 }
128 obj.FillShortTxIDSelector();
129 }
130 }
131};
132
134protected:
135 std::vector<CTransactionRef> txn_available;
136
137 // These count and size values should ONLY be used for logging output, they
138 // are NOT correctly set if compact block logging is off.
141
142 // For tracking redundant prefills, DO NOT use for anything other than
143 // logging, these are not correctly set if compact block logging is off.
144 //
145 // Either it was already present in our mempool...
147 // or maybe it was present in our extrapool...
149
151public:
153
154 // Can be overridden for testing
155 using IsBlockMutatedFn = std::function<bool(const CBlock&, bool)>;
157
159
160 // extra_txn is a list of extra transactions to look at, in <witness hash, reference> form
161 ReadStatus InitData(const CBlockHeaderAndShortTxIDs& cmpctblock, const std::vector<std::pair<Wtxid, CTransactionRef>>& extra_txn);
162 bool IsTxAvailable(size_t index) const;
163 // segwit_active enforces witness mutation checks just before reporting a healthy status
164 ReadStatus FillBlock(CBlock& block, const std::vector<CTransactionRef>& vtx_missing, bool segwit_active);
165};
166
167#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)
SERIALIZE_METHODS(BlockTransactionsRequest, obj)
std::vector< uint16_t > indexes
CBlockHeaderAndShortTxIDs()=default
Dummy for deserialization.
SERIALIZE_METHODS(CBlockHeaderAndShortTxIDs, obj)
uint64_t GetShortID(const Wtxid &wtxid) const
std::vector< PrefilledTransaction > prefilledtxn
static constexpr int SHORTTXIDS_LENGTH
std::vector< uint64_t > shorttxids
std::optional< PresaltedSipHasher > m_hasher
Nodes collect new transactions into a block, hash them into a hash tree, and scan through nonce value...
Definition block.h:27
Definition block.h:74
CTxMemPool stores valid-according-to-the-current-best-chain transactions that may be included in the ...
Definition txmempool.h:188
void Unser(Stream &s, I &v)
void Ser(Stream &s, I v)
PartiallyDownloadedBlock(CTxMemPool *poolIn)
const CTxMemPool * pool
IsBlockMutatedFn m_check_block_mutated_mock
std::vector< CTransactionRef > txn_available
ReadStatus FillBlock(CBlock &block, const std::vector< CTransactionRef > &vtx_missing, bool segwit_active)
std::function< bool(const CBlock &, bool)> IsBlockMutatedFn
bool IsTxAvailable(size_t index) const
ReadStatus InitData(const CBlockHeaderAndShortTxIDs &cmpctblock, const std::vector< std::pair< Wtxid, CTransactionRef > > &extra_txn)
256-bit opaque blob.
Definition uint256.h:196
Transaction validation functions.
constexpr TransactionSerParams TX_WITH_WITNESS
std::shared_ptr< const CTransaction > CTransactionRef
void WriteCompactSize(SizeComputer &os, uint64_t nSize)
Definition serialize.h:1151
uint64_t ReadCompactSize(Stream &is, bool range_check=true)
Decode a CompactSize-encoded variable-length integer.
Definition serialize.h:333
static Wrapper< Formatter, T & > Using(T &&t)
Cause serialization/deserialization of an object to be done using a specified formatter class.
Definition serialize.h:491
#define COMPACTSIZE(obj)
Definition serialize.h:495
#define READWRITE(...)
Definition serialize.h:148
Parameters that influence chain consensus.
Definition params.h:88
Serialization wrapper class for custom integers and enums.
Definition serialize.h:525
Default formatter.
Definition serialize.h:786
SERIALIZE_METHODS(PrefilledTransaction, obj)
CTransactionRef tx
Formatter to serialize/deserialize vector elements using another formatter.
Definition serialize.h:667
constexpr auto Ticks(Dur2 d)
Helper to count the seconds of a duration/time_point.
Definition time.h:82