Line data Source code
1 : // Copyright (c) 2016-2020 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 <consensus/consensus.h> 7 : #include <consensus/validation.h> 8 : #include <chainparams.h> 9 : #include <crypto/sha256.h> 10 : #include <crypto/siphash.h> 11 : #include <random.h> 12 : #include <streams.h> 13 : #include <txmempool.h> 14 : #include <validation.h> 15 : #include <util/system.h> 16 : 17 : #include <unordered_map> 18 : 19 : #define MIN_TRANSACTION_SIZE (::GetSerializeSize(CMutableTransaction(), PROTOCOL_VERSION)) 20 : 21 8 : CBlockHeaderAndShortTxIDs::CBlockHeaderAndShortTxIDs(const CBlock& block) : 22 4 : nonce(GetRand<uint64_t>()), 23 8 : shorttxids(block.vtx.size() - 1), prefilledtxn(1), header(block) { 24 4 : FillShortTxIDSelector(); 25 : //TODO: Use our mempool prior to block acceptance to predictively fill more than just the coinbase 26 4 : prefilledtxn[0] = {0, block.vtx[0]}; 27 10 : for (size_t i = 1; i < block.vtx.size(); i++) { 28 6 : const CTransaction& tx = *block.vtx[i]; 29 6 : shorttxids[i - 1] = GetShortID(tx.GetHash()); 30 6 : } 31 8 : } 32 : 33 11 : void CBlockHeaderAndShortTxIDs::FillShortTxIDSelector() const { 34 11 : CDataStream stream(SER_NETWORK, PROTOCOL_VERSION); 35 11 : stream << header << nonce; 36 11 : CSHA256 hasher; 37 11 : hasher.Write((unsigned char*)&(*stream.begin()), stream.end() - stream.begin()); 38 11 : uint256 shorttxidhash; 39 11 : hasher.Finalize(shorttxidhash.begin()); 40 11 : shorttxidk0 = shorttxidhash.GetUint64(0); 41 11 : shorttxidk1 = shorttxidhash.GetUint64(1); 42 11 : } 43 : 44 12 : uint64_t CBlockHeaderAndShortTxIDs::GetShortID(const uint256& txhash) const { 45 : static_assert(SHORTTXIDS_LENGTH == 6, "shorttxids calculation assumes 6-byte shorttxids"); 46 12 : return SipHashUint256(shorttxidk0, shorttxidk1, txhash) & 0xffffffffffffL; 47 : } 48 : 49 : 50 : 51 4 : ReadStatus PartiallyDownloadedBlock::InitData(const CBlockHeaderAndShortTxIDs& cmpctblock, const std::vector<std::pair<uint256, CTransactionRef>>& extra_txn) { 52 4 : if (cmpctblock.header.IsNull() || (cmpctblock.shorttxids.empty() && cmpctblock.prefilledtxn.empty())) 53 0 : return READ_STATUS_INVALID; 54 4 : if (cmpctblock.shorttxids.size() + cmpctblock.prefilledtxn.size() > MaxBlockSize() / MIN_TRANSACTION_SIZE) 55 0 : return READ_STATUS_INVALID; 56 : 57 4 : assert(header.IsNull() && txn_available.empty()); 58 4 : header = cmpctblock.header; 59 4 : txn_available.resize(cmpctblock.BlockTxCount()); 60 : 61 4 : int32_t lastprefilledindex = -1; 62 9 : for (size_t i = 0; i < cmpctblock.prefilledtxn.size(); i++) { 63 5 : if (cmpctblock.prefilledtxn[i].tx->IsNull()) 64 0 : return READ_STATUS_INVALID; 65 : 66 5 : lastprefilledindex += cmpctblock.prefilledtxn[i].index + 1; //index is a uint16_t, so can't overflow here 67 5 : if (lastprefilledindex > std::numeric_limits<uint16_t>::max()) 68 0 : return READ_STATUS_INVALID; 69 5 : if ((uint32_t)lastprefilledindex > cmpctblock.shorttxids.size() + i) { 70 : // If we are inserting a tx at an index greater than our full list of shorttxids 71 : // plus the number of prefilled txn we've inserted, then we have txn for which we 72 : // have neither a prefilled txn or a shorttxid! 73 0 : return READ_STATUS_INVALID; 74 : } 75 5 : txn_available[lastprefilledindex] = cmpctblock.prefilledtxn[i].tx; 76 5 : } 77 4 : prefilled_count = cmpctblock.prefilledtxn.size(); 78 : 79 : // Calculate map of txids -> positions and check mempool to see what we have (or don't) 80 : // Because well-formed cmpctblock messages will have a (relatively) uniform distribution 81 : // of short IDs, any highly-uneven distribution of elements can be safely treated as a 82 : // READ_STATUS_FAILED. 83 4 : std::unordered_map<uint64_t, uint16_t> shorttxids(cmpctblock.shorttxids.size()); 84 4 : uint16_t index_offset = 0; 85 9 : for (size_t i = 0; i < cmpctblock.shorttxids.size(); i++) { 86 8 : while (txn_available[i + index_offset]) 87 3 : index_offset++; 88 5 : shorttxids[cmpctblock.shorttxids[i]] = i + index_offset; 89 : // To determine the chance that the number of entries in a bucket exceeds N, 90 : // we use the fact that the number of elements in a single bucket is 91 : // binomially distributed (with n = the number of shorttxids S, and p = 92 : // 1 / the number of buckets), that in the worst case the number of buckets is 93 : // equal to S (due to std::unordered_map having a default load factor of 1.0), 94 : // and that the chance for any bucket to exceed N elements is at most 95 : // buckets * (the chance that any given bucket is above N elements). 96 : // Thus: P(max_elements_per_bucket > N) <= S * (1 - cdf(binomial(n=S,p=1/S), N)). 97 : // If we assume blocks of up to 16000, allowing 12 elements per bucket should 98 : // only fail once per ~1 million block transfers (per peer and connection). 99 5 : if (shorttxids.bucket_size(shorttxids.bucket(cmpctblock.shorttxids[i])) > 12) 100 0 : return READ_STATUS_FAILED; 101 5 : } 102 : // TODO: in the shortid-collision case, we should instead request both transactions 103 : // which collided. Falling back to full-block-request here is overkill. 104 4 : if (shorttxids.size() != cmpctblock.shorttxids.size()) 105 0 : return READ_STATUS_FAILED; // Short ID collision 106 : 107 4 : std::vector<bool> have_txn(txn_available.size()); 108 : { 109 4 : LOCK(pool->cs); 110 6 : for (size_t i = 0; i < pool->vTxHashes.size(); i++) { 111 3 : uint64_t shortid = cmpctblock.GetShortID(pool->vTxHashes[i].first); 112 3 : std::unordered_map<uint64_t, uint16_t>::iterator idit = shorttxids.find(shortid); 113 3 : if (idit != shorttxids.end()) { 114 3 : if (!have_txn[idit->second]) { 115 3 : txn_available[idit->second] = pool->vTxHashes[i].second->GetSharedTx(); 116 3 : have_txn[idit->second] = true; 117 3 : mempool_count++; 118 3 : } else { 119 : // If we find two mempool txn that match the short id, just request it. 120 : // This should be rare enough that the extra bandwidth doesn't matter, 121 : // but eating a round-trip due to FillBlock failure would be annoying 122 0 : if (txn_available[idit->second]) { 123 0 : txn_available[idit->second].reset(); 124 0 : mempool_count--; 125 0 : } 126 : } 127 3 : } 128 : // Though ideally we'd continue scanning for the two-txn-match-shortid case, 129 : // the performance win of an early exit here is too good to pass up and worth 130 : // the extra risk. 131 3 : if (mempool_count == shorttxids.size()) 132 1 : break; 133 2 : } 134 4 : } 135 : 136 4 : for (size_t i = 0; i < extra_txn.size(); i++) { 137 0 : uint64_t shortid = cmpctblock.GetShortID(extra_txn[i].first); 138 0 : std::unordered_map<uint64_t, uint16_t>::iterator idit = shorttxids.find(shortid); 139 0 : if (idit != shorttxids.end()) { 140 0 : if (!have_txn[idit->second]) { 141 0 : txn_available[idit->second] = extra_txn[i].second; 142 0 : have_txn[idit->second] = true; 143 0 : mempool_count++; 144 0 : extra_count++; 145 0 : } else { 146 : // If we find two mempool/extra txn that match the short id, just 147 : // request it. 148 : // This should be rare enough that the extra bandwidth doesn't matter, 149 : // but eating a round-trip due to FillBlock failure would be annoying 150 : // Note that we don't want duplication between extra_txn and mempool to 151 : // trigger this case, so we compare hashes first 152 0 : if (txn_available[idit->second] && 153 0 : txn_available[idit->second]->GetHash() != extra_txn[i].second->GetHash()) { 154 0 : txn_available[idit->second].reset(); 155 0 : mempool_count--; 156 0 : extra_count--; 157 0 : } 158 : } 159 0 : } 160 : // Though ideally we'd continue scanning for the two-txn-match-shortid case, 161 : // the performance win of an early exit here is too good to pass up and worth 162 : // the extra risk. 163 0 : if (mempool_count == shorttxids.size()) 164 0 : break; 165 0 : } 166 : 167 4 : LogPrint(BCLog::CMPCTBLOCK, "Initialized PartiallyDownloadedBlock for block %s using a cmpctblock of size %lu\n", cmpctblock.header.GetHash().ToString(), GetSerializeSize(cmpctblock, PROTOCOL_VERSION)); 168 : 169 4 : return READ_STATUS_OK; 170 4 : } 171 : 172 10 : bool PartiallyDownloadedBlock::IsTxAvailable(size_t index) const { 173 10 : assert(!header.IsNull()); 174 10 : assert(index < txn_available.size()); 175 10 : return txn_available[index] != nullptr; 176 : } 177 : 178 8 : ReadStatus PartiallyDownloadedBlock::FillBlock(CBlock& block, const std::vector<CTransactionRef>& vtx_missing) { 179 8 : assert(!header.IsNull()); 180 8 : uint256 hash = header.GetHash(); 181 8 : block = header; 182 8 : block.vtx.resize(txn_available.size()); 183 : 184 8 : size_t tx_missing_offset = 0; 185 25 : for (size_t i = 0; i < txn_available.size(); i++) { 186 19 : if (!txn_available[i]) { 187 6 : if (vtx_missing.size() <= tx_missing_offset) 188 2 : return READ_STATUS_INVALID; 189 4 : block.vtx[i] = vtx_missing[tx_missing_offset++]; 190 4 : } else 191 13 : block.vtx[i] = std::move(txn_available[i]); 192 17 : } 193 : 194 : // Make sure we can't call FillBlock again. 195 6 : header.SetNull(); 196 6 : txn_available.clear(); 197 : 198 6 : if (vtx_missing.size() != tx_missing_offset) 199 0 : return READ_STATUS_INVALID; 200 : 201 6 : BlockValidationState state; 202 6 : if (!CheckBlock(block, state, Params().GetConsensus())) { 203 : // TODO: We really want to just check merkle tree manually here, 204 : // but that is expensive, and CheckBlock caches a block's 205 : // "checked-status" (in the CBlock?). CBlock should be able to 206 : // check its own merkle root and cache that check. 207 2 : if (state.GetResult() == BlockValidationResult::BLOCK_MUTATED) 208 2 : return READ_STATUS_FAILED; // Possible Short ID collision 209 0 : return READ_STATUS_CHECKBLOCK_FAILED; 210 : } 211 : 212 4 : LogPrint(BCLog::CMPCTBLOCK, "Successfully reconstructed block %s with %lu txn prefilled, %lu txn from mempool (incl at least %lu from extra pool) and %lu txn requested\n", hash.ToString(), prefilled_count, mempool_count, extra_count, vtx_missing.size()); 213 4 : if (vtx_missing.size() < 5) { 214 6 : for (const auto& tx : vtx_missing) { 215 2 : LogPrint(BCLog::CMPCTBLOCK, "Reconstructed block %s required tx %s\n", hash.ToString(), tx->GetHash().ToString()); 216 : } 217 4 : } 218 : 219 4 : return READ_STATUS_OK; 220 8 : }