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 441498 : CBlockHeaderAndShortTxIDs::CBlockHeaderAndShortTxIDs(const CBlock& block) : 22 220749 : nonce(GetRand<uint64_t>()), 23 441498 : shorttxids(block.vtx.size() - 1), prefilledtxn(1), header(block) { 24 220749 : FillShortTxIDSelector(); 25 : //TODO: Use our mempool prior to block acceptance to predictively fill more than just the coinbase 26 220749 : prefilledtxn[0] = {0, block.vtx[0]}; 27 489435 : for (size_t i = 1; i < block.vtx.size(); i++) { 28 268686 : const CTransaction& tx = *block.vtx[i]; 29 268686 : shorttxids[i - 1] = GetShortID(tx.GetHash()); 30 268686 : } 31 441498 : } 32 : 33 341457 : void CBlockHeaderAndShortTxIDs::FillShortTxIDSelector() const { 34 341457 : CDataStream stream(SER_NETWORK, PROTOCOL_VERSION); 35 341457 : stream << header << nonce; 36 341457 : CSHA256 hasher; 37 341457 : hasher.Write((unsigned char*)&(*stream.begin()), stream.end() - stream.begin()); 38 341457 : uint256 shorttxidhash; 39 341457 : hasher.Finalize(shorttxidhash.begin()); 40 341457 : shorttxidk0 = shorttxidhash.GetUint64(0); 41 341457 : shorttxidk1 = shorttxidhash.GetUint64(1); 42 341457 : } 43 : 44 362868 : uint64_t CBlockHeaderAndShortTxIDs::GetShortID(const uint256& txhash) const { 45 : static_assert(SHORTTXIDS_LENGTH == 6, "shorttxids calculation assumes 6-byte shorttxids"); 46 362868 : return SipHashUint256(shorttxidk0, shorttxidk1, txhash) & 0xffffffffffffL; 47 : } 48 : 49 : 50 : 51 97887 : ReadStatus PartiallyDownloadedBlock::InitData(const CBlockHeaderAndShortTxIDs& cmpctblock, const std::vector<std::pair<uint256, CTransactionRef>>& extra_txn) { 52 97887 : if (cmpctblock.header.IsNull() || (cmpctblock.shorttxids.empty() && cmpctblock.prefilledtxn.empty())) 53 0 : return READ_STATUS_INVALID; 54 97887 : if (cmpctblock.shorttxids.size() + cmpctblock.prefilledtxn.size() > MaxBlockSize() / MIN_TRANSACTION_SIZE) 55 0 : return READ_STATUS_INVALID; 56 : 57 97887 : assert(header.IsNull() && txn_available.empty()); 58 97887 : header = cmpctblock.header; 59 97887 : txn_available.resize(cmpctblock.BlockTxCount()); 60 : 61 97887 : int32_t lastprefilledindex = -1; 62 195789 : for (size_t i = 0; i < cmpctblock.prefilledtxn.size(); i++) { 63 97906 : if (cmpctblock.prefilledtxn[i].tx->IsNull()) 64 2 : return READ_STATUS_INVALID; 65 : 66 97904 : lastprefilledindex += cmpctblock.prefilledtxn[i].index + 1; //index is a uint16_t, so can't overflow here 67 97904 : if (lastprefilledindex > std::numeric_limits<uint16_t>::max()) 68 0 : return READ_STATUS_INVALID; 69 97904 : 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 2 : return READ_STATUS_INVALID; 74 : } 75 97902 : txn_available[lastprefilledindex] = cmpctblock.prefilledtxn[i].tx; 76 97902 : } 77 97883 : 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 97883 : std::unordered_map<uint64_t, uint16_t> shorttxids(cmpctblock.shorttxids.size()); 84 97883 : uint16_t index_offset = 0; 85 200280 : for (size_t i = 0; i < cmpctblock.shorttxids.size(); i++) { 86 133073 : while (txn_available[i + index_offset]) 87 30676 : index_offset++; 88 102397 : 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 102397 : if (shorttxids.bucket_size(shorttxids.bucket(cmpctblock.shorttxids[i])) > 12) 100 0 : return READ_STATUS_FAILED; 101 102397 : } 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 97883 : if (shorttxids.size() != cmpctblock.shorttxids.size()) 105 0 : return READ_STATUS_FAILED; // Short ID collision 106 : 107 97883 : std::vector<bool> have_txn(txn_available.size()); 108 : { 109 97883 : LOCK(pool->cs); 110 165770 : for (size_t i = 0; i < pool->vTxHashes.size(); i++) { 111 73166 : uint64_t shortid = cmpctblock.GetShortID(pool->vTxHashes[i].first); 112 73166 : std::unordered_map<uint64_t, uint16_t>::iterator idit = shorttxids.find(shortid); 113 73166 : if (idit != shorttxids.end()) { 114 18340 : if (!have_txn[idit->second]) { 115 18340 : txn_available[idit->second] = pool->vTxHashes[i].second->GetSharedTx(); 116 18340 : have_txn[idit->second] = true; 117 18340 : mempool_count++; 118 18340 : } 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 18340 : } 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 73166 : if (mempool_count == shorttxids.size()) 132 5279 : break; 133 67887 : } 134 97883 : } 135 : 136 118526 : for (size_t i = 0; i < extra_txn.size(); i++) { 137 21013 : uint64_t shortid = cmpctblock.GetShortID(extra_txn[i].first); 138 21013 : std::unordered_map<uint64_t, uint16_t>::iterator idit = shorttxids.find(shortid); 139 21013 : if (idit != shorttxids.end()) { 140 43 : if (!have_txn[idit->second]) { 141 37 : txn_available[idit->second] = extra_txn[i].second; 142 37 : have_txn[idit->second] = true; 143 37 : mempool_count++; 144 37 : extra_count++; 145 37 : } 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 12 : if (txn_available[idit->second] && 153 6 : 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 43 : } 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 21013 : if (mempool_count == shorttxids.size()) 164 370 : break; 165 20643 : } 166 : 167 97883 : 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 97883 : return READ_STATUS_OK; 170 97887 : } 171 : 172 199327 : bool PartiallyDownloadedBlock::IsTxAvailable(size_t index) const { 173 199327 : assert(!header.IsNull()); 174 199327 : assert(index < txn_available.size()); 175 199327 : return txn_available[index] != nullptr; 176 : } 177 : 178 97869 : ReadStatus PartiallyDownloadedBlock::FillBlock(CBlock& block, const std::vector<CTransactionRef>& vtx_missing) { 179 97869 : assert(!header.IsNull()); 180 97869 : uint256 hash = header.GetHash(); 181 97869 : block = header; 182 97869 : block.vtx.resize(txn_available.size()); 183 : 184 97869 : size_t tx_missing_offset = 0; 185 297654 : for (size_t i = 0; i < txn_available.size(); i++) { 186 199952 : if (!txn_available[i]) { 187 83692 : if (vtx_missing.size() <= tx_missing_offset) 188 167 : return READ_STATUS_INVALID; 189 83525 : block.vtx[i] = vtx_missing[tx_missing_offset++]; 190 83525 : } else 191 116260 : block.vtx[i] = std::move(txn_available[i]); 192 199785 : } 193 : 194 : // Make sure we can't call FillBlock again. 195 97702 : header.SetNull(); 196 97702 : txn_available.clear(); 197 : 198 97702 : if (vtx_missing.size() != tx_missing_offset) 199 0 : return READ_STATUS_INVALID; 200 : 201 97702 : BlockValidationState state; 202 97702 : 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 4 : if (state.GetResult() == BlockValidationResult::BLOCK_MUTATED) 208 4 : return READ_STATUS_FAILED; // Possible Short ID collision 209 0 : return READ_STATUS_CHECKBLOCK_FAILED; 210 : } 211 : 212 97698 : 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 97698 : if (vtx_missing.size() < 5) { 214 158798 : for (const auto& tx : vtx_missing) { 215 64228 : LogPrint(BCLog::CMPCTBLOCK, "Reconstructed block %s required tx %s\n", hash.ToString(), tx->GetHash().ToString()); 216 : } 217 94570 : } 218 : 219 97698 : return READ_STATUS_OK; 220 97869 : }