Bitcoin Core 30.99.0
P2P Digital Currency
blockencodings.cpp
Go to the documentation of this file.
1// Copyright (c) 2016-2022 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#include <blockencodings.h>
6#include <chainparams.h>
7#include <common/system.h>
10#include <crypto/sha256.h>
11#include <crypto/siphash.h>
12#include <logging.h>
13#include <random.h>
14#include <streams.h>
15#include <txmempool.h>
16#include <validation.h>
17
18#include <unordered_map>
19
21 : nonce(nonce),
22 shorttxids(block.vtx.size() - 1),
23 prefilledtxn(1),
24 header(block)
25{
27 // TODO: Use our mempool prior to block acceptance to predictively fill more than just the coinbase
28 prefilledtxn[0] = {0, block.vtx[0]};
29 for (size_t i = 1; i < block.vtx.size(); i++) {
30 const CTransaction& tx = *block.vtx[i];
32 }
33}
34
36{
37 DataStream stream{};
38 stream << header << nonce;
39 CSHA256 hasher;
40 hasher.Write((unsigned char*)&(*stream.begin()), stream.end() - stream.begin());
41 uint256 shorttxidhash;
42 hasher.Finalize(shorttxidhash.begin());
43 m_hasher.emplace(shorttxidhash.GetUint64(0), shorttxidhash.GetUint64(1));
44}
45
46uint64_t CBlockHeaderAndShortTxIDs::GetShortID(const Wtxid& wtxid) const
47{
48 static_assert(SHORTTXIDS_LENGTH == 6, "shorttxids calculation assumes 6-byte shorttxids");
49 return (*Assert(m_hasher))(wtxid.ToUint256()) & 0xffffffffffffL;
50}
51
52/* Reconstructing a compact block is in the hot-path for block relay,
53 * so we want to do it as quickly as possible. Because this often
54 * involves iterating over the entire mempool, we put all the data we
55 * need (ie the wtxid and a reference to the actual transaction data)
56 * in a vector and iterate over the vector directly. This allows optimal
57 * CPU caching behaviour, at a cost of only 40 bytes per transaction.
58 */
59ReadStatus PartiallyDownloadedBlock::InitData(const CBlockHeaderAndShortTxIDs& cmpctblock, const std::vector<std::pair<Wtxid, CTransactionRef>>& extra_txn)
60{
61 LogDebug(BCLog::CMPCTBLOCK, "Initializing PartiallyDownloadedBlock for block %s using a cmpctblock of %u bytes\n", cmpctblock.header.GetHash().ToString(), GetSerializeSize(cmpctblock));
62 if (cmpctblock.header.IsNull() || (cmpctblock.shorttxids.empty() && cmpctblock.prefilledtxn.empty()))
64 if (cmpctblock.shorttxids.size() + cmpctblock.prefilledtxn.size() > MAX_BLOCK_WEIGHT / MIN_SERIALIZABLE_TRANSACTION_WEIGHT)
66
67 if (!header.IsNull() || !txn_available.empty()) return READ_STATUS_INVALID;
68
69 header = cmpctblock.header;
70 txn_available.resize(cmpctblock.BlockTxCount());
71
72 int32_t lastprefilledindex = -1;
73 for (size_t i = 0; i < cmpctblock.prefilledtxn.size(); i++) {
74 if (cmpctblock.prefilledtxn[i].tx->IsNull())
76
77 lastprefilledindex += cmpctblock.prefilledtxn[i].index + 1; //index is a uint16_t, so can't overflow here
78 if (lastprefilledindex > std::numeric_limits<uint16_t>::max())
80 if ((uint32_t)lastprefilledindex > cmpctblock.shorttxids.size() + i) {
81 // If we are inserting a tx at an index greater than our full list of shorttxids
82 // plus the number of prefilled txn we've inserted, then we have txn for which we
83 // have neither a prefilled txn or a shorttxid!
85 }
86 txn_available[lastprefilledindex] = cmpctblock.prefilledtxn[i].tx;
87 }
88 prefilled_count = cmpctblock.prefilledtxn.size();
89
90 // Calculate map of txids -> positions and check mempool to see what we have (or don't)
91 // Because well-formed cmpctblock messages will have a (relatively) uniform distribution
92 // of short IDs, any highly-uneven distribution of elements can be safely treated as a
93 // READ_STATUS_FAILED.
94 std::unordered_map<uint64_t, uint16_t> shorttxids(cmpctblock.shorttxids.size());
95 uint16_t index_offset = 0;
96 for (size_t i = 0; i < cmpctblock.shorttxids.size(); i++) {
97 while (txn_available[i + index_offset])
98 index_offset++;
99 shorttxids[cmpctblock.shorttxids[i]] = i + index_offset;
100 // To determine the chance that the number of entries in a bucket exceeds N,
101 // we use the fact that the number of elements in a single bucket is
102 // binomially distributed (with n = the number of shorttxids S, and p =
103 // 1 / the number of buckets), that in the worst case the number of buckets is
104 // equal to S (due to std::unordered_map having a default load factor of 1.0),
105 // and that the chance for any bucket to exceed N elements is at most
106 // buckets * (the chance that any given bucket is above N elements).
107 // Thus: P(max_elements_per_bucket > N) <= S * (1 - cdf(binomial(n=S,p=1/S), N)).
108 // If we assume blocks of up to 16000, allowing 12 elements per bucket should
109 // only fail once per ~1 million block transfers (per peer and connection).
110 if (shorttxids.bucket_size(shorttxids.bucket(cmpctblock.shorttxids[i])) > 12)
111 return READ_STATUS_FAILED;
112 }
113 // TODO: in the shortid-collision case, we should instead request both transactions
114 // which collided. Falling back to full-block-request here is overkill.
115 if (shorttxids.size() != cmpctblock.shorttxids.size())
116 return READ_STATUS_FAILED; // Short ID collision
117
118 std::vector<bool> have_txn(txn_available.size());
119 {
120 LOCK(pool->cs);
121 for (const auto& [wtxid, txit] : pool->txns_randomized) {
122 uint64_t shortid = cmpctblock.GetShortID(wtxid);
123 std::unordered_map<uint64_t, uint16_t>::iterator idit = shorttxids.find(shortid);
124 if (idit != shorttxids.end()) {
125 if (!have_txn[idit->second]) {
126 txn_available[idit->second] = txit->GetSharedTx();
127 have_txn[idit->second] = true;
129 } else {
130 // If we find two mempool txn that match the short id, just request it.
131 // This should be rare enough that the extra bandwidth doesn't matter,
132 // but eating a round-trip due to FillBlock failure would be annoying
133 if (txn_available[idit->second]) {
134 txn_available[idit->second].reset();
136 }
137 }
138 }
139 // Though ideally we'd continue scanning for the two-txn-match-shortid case,
140 // the performance win of an early exit here is too good to pass up and worth
141 // the extra risk.
142 if (mempool_count == shorttxids.size())
143 break;
144 }
145 }
146
147 for (size_t i = 0; i < extra_txn.size(); i++) {
148 uint64_t shortid = cmpctblock.GetShortID(extra_txn[i].first);
149 std::unordered_map<uint64_t, uint16_t>::iterator idit = shorttxids.find(shortid);
150 if (idit != shorttxids.end()) {
151 if (!have_txn[idit->second]) {
152 txn_available[idit->second] = extra_txn[i].second;
153 have_txn[idit->second] = true;
155 extra_count++;
156 } else {
157 // If we find two mempool/extra txn that match the short id, just
158 // request it.
159 // This should be rare enough that the extra bandwidth doesn't matter,
160 // but eating a round-trip due to FillBlock failure would be annoying
161 // Note that we don't want duplication between extra_txn and mempool to
162 // trigger this case, so we compare witness hashes first
163 if (txn_available[idit->second] &&
164 txn_available[idit->second]->GetWitnessHash() != extra_txn[i].second->GetWitnessHash()) {
165 txn_available[idit->second].reset();
167 extra_count--;
168 }
169 }
170 }
171 // Though ideally we'd continue scanning for the two-txn-match-shortid case,
172 // the performance win of an early exit here is too good to pass up and worth
173 // the extra risk.
174 if (mempool_count == shorttxids.size())
175 break;
176 }
177
178 LogDebug(BCLog::CMPCTBLOCK, "Initialized PartiallyDownloadedBlock for block %s using a cmpctblock of %u bytes\n", cmpctblock.header.GetHash().ToString(), GetSerializeSize(cmpctblock));
179
180 return READ_STATUS_OK;
181}
182
184{
185 if (header.IsNull()) return false;
186
187 assert(index < txn_available.size());
188 return txn_available[index] != nullptr;
189}
190
191ReadStatus PartiallyDownloadedBlock::FillBlock(CBlock& block, const std::vector<CTransactionRef>& vtx_missing, bool segwit_active)
192{
193 if (header.IsNull()) return READ_STATUS_INVALID;
194
195 uint256 hash = header.GetHash();
196 block = header;
197 block.vtx.resize(txn_available.size());
198
199 unsigned int tx_missing_size = 0;
200 size_t tx_missing_offset = 0;
201 for (size_t i = 0; i < txn_available.size(); i++) {
202 if (!txn_available[i]) {
203 if (vtx_missing.size() <= tx_missing_offset)
204 return READ_STATUS_INVALID;
205 block.vtx[i] = vtx_missing[tx_missing_offset++];
206 tx_missing_size += block.vtx[i]->GetTotalSize();
207 } else
208 block.vtx[i] = std::move(txn_available[i]);
209 }
210
211 // Make sure we can't call FillBlock again.
212 header.SetNull();
213 txn_available.clear();
214
215 if (vtx_missing.size() != tx_missing_offset)
216 return READ_STATUS_INVALID;
217
218 // Check for possible mutations early now that we have a seemingly good block
220 if (check_mutated(/*block=*/block,
221 /*check_witness_root=*/segwit_active)) {
222 return READ_STATUS_FAILED; // Possible Short ID collision
223 }
224
225 LogDebug(BCLog::CMPCTBLOCK, "Successfully reconstructed block %s with %u txn prefilled, %u txn from mempool (incl at least %u from extra pool) and %u txn (%u bytes) requested\n", hash.ToString(), prefilled_count, mempool_count, extra_count, vtx_missing.size(), tx_missing_size);
226 if (vtx_missing.size() < 5) {
227 for (const auto& tx : vtx_missing) {
228 LogDebug(BCLog::CMPCTBLOCK, "Reconstructed block %s required tx %s\n", hash.ToString(), tx->GetHash().ToString());
229 }
230 }
231
232 return READ_STATUS_OK;
233}
@ READ_STATUS_OK
@ READ_STATUS_INVALID
@ READ_STATUS_FAILED
enum ReadStatus_t ReadStatus
#define Assert(val)
Identity function.
Definition: check.h:113
CBlockHeaderAndShortTxIDs()=default
Dummy for deserialization.
void FillShortTxIDSelector() const
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
void SetNull()
Definition: block.h:39
uint256 GetHash() const
Definition: block.cpp:11
bool IsNull() const
Definition: block.h:49
Definition: block.h:69
std::vector< CTransactionRef > vtx
Definition: block.h:72
A hasher class for SHA-256.
Definition: sha256.h:14
void Finalize(unsigned char hash[OUTPUT_SIZE])
Definition: sha256.cpp:725
CSHA256 & Write(const unsigned char *data, size_t len)
Definition: sha256.cpp:699
The basic transaction that is broadcasted on the network and contained in blocks.
Definition: transaction.h:281
const Wtxid & GetWitnessHash() const LIFETIMEBOUND
Definition: transaction.h:329
RecursiveMutex cs
This mutex needs to be locked when accessing mapTx or other members that are guarded by it.
Definition: txmempool.h:263
Double ended buffer combining vector and stream-like interfaces.
Definition: streams.h:130
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)
constexpr uint64_t GetUint64(int pos) const
Definition: uint256.h:108
std::string ToString() const
Definition: uint256.cpp:21
constexpr unsigned char * begin()
Definition: uint256.h:100
transaction_identifier represents the two canonical transaction identifier types (txid,...
const uint256 & ToUint256() const LIFETIMEBOUND
256-bit opaque blob.
Definition: uint256.h:195
static const unsigned int MAX_BLOCK_WEIGHT
The maximum allowed weight for a block, see BIP 141 (network rule)
Definition: consensus.h:15
static const size_t MIN_SERIALIZABLE_TRANSACTION_WEIGHT
Definition: consensus.h:24
#define LogDebug(category,...)
Definition: logging.h:390
unsigned int nonce
Definition: miner_tests.cpp:81
@ CMPCTBLOCK
Definition: logging.h:78
uint64_t GetSerializeSize(const T &t)
Definition: serialize.h:1095
#define LOCK(cs)
Definition: sync.h:259
bool IsBlockMutated(const CBlock &block, bool check_witness_root)
Check if a block has been mutated (with respect to its merkle root and witness commitments).
assert(!tx.IsCoinBase())