diff --git a/src/blockencodings.cpp b/src/blockencodings.cpp index cce380848..a641fbdd6 100644 --- a/src/blockencodings.cpp +++ b/src/blockencodings.cpp @@ -1,268 +1,268 @@ // Copyright (c) 2016 The Bitcoin Core developers // Distributed under the MIT software license, see the accompanying // file COPYING or http://www.opensource.org/licenses/mit-license.php. #include "blockencodings.h" #include "chainparams.h" #include "config.h" #include "consensus/consensus.h" #include "consensus/validation.h" #include "hash.h" #include "random.h" #include "streams.h" #include "txmempool.h" #include "util.h" #include "validation.h" #include CBlockHeaderAndShortTxIDs::CBlockHeaderAndShortTxIDs(const CBlock &block) : nonce(GetRand(std::numeric_limits::max())), shorttxids(block.vtx.size() - 1), prefilledtxn(1), header(block) { FillShortTxIDSelector(); // TODO: Use our mempool prior to block acceptance to predictively fill more // than just the coinbase. prefilledtxn[0] = {0, block.vtx[0]}; for (size_t i = 1; i < block.vtx.size(); i++) { const CTransaction &tx = *block.vtx[i]; shorttxids[i - 1] = GetShortID(tx.GetHash()); } } void CBlockHeaderAndShortTxIDs::FillShortTxIDSelector() const { CDataStream stream(SER_NETWORK, PROTOCOL_VERSION); stream << header << nonce; CSHA256 hasher; hasher.Write((uint8_t *)&(*stream.begin()), stream.end() - stream.begin()); uint256 shorttxidhash; hasher.Finalize(shorttxidhash.begin()); shorttxidk0 = shorttxidhash.GetUint64(0); shorttxidk1 = shorttxidhash.GetUint64(1); } uint64_t CBlockHeaderAndShortTxIDs::GetShortID(const uint256 &txhash) const { static_assert(SHORTTXIDS_LENGTH == 6, "shorttxids calculation assumes 6-byte shorttxids"); return SipHashUint256(shorttxidk0, shorttxidk1, txhash) & 0xffffffffffffL; } ReadStatus PartiallyDownloadedBlock::InitData( const CBlockHeaderAndShortTxIDs &cmpctblock, const std::vector> &extra_txns) { if (cmpctblock.header.IsNull() || (cmpctblock.shorttxids.empty() && cmpctblock.prefilledtxn.empty())) { return READ_STATUS_INVALID; } if (cmpctblock.shorttxids.size() + cmpctblock.prefilledtxn.size() > config->GetMaxBlockSize() / MIN_TRANSACTION_SIZE) { return READ_STATUS_INVALID; } assert(header.IsNull() && txns_available.empty()); header = cmpctblock.header; txns_available.resize(cmpctblock.BlockTxCount()); - int32_t lastprefilledindex = -1; + int64_t lastprefilledindex = -1; for (size_t i = 0; i < cmpctblock.prefilledtxn.size(); i++) { auto &prefilledtxn = cmpctblock.prefilledtxn[i]; if (prefilledtxn.tx->IsNull()) { return READ_STATUS_INVALID; } - // index is a uint16_t, so can't overflow here. + // index is a uint32_t, so can't overflow here. lastprefilledindex += prefilledtxn.index + 1; - if (lastprefilledindex > std::numeric_limits::max()) { + if (lastprefilledindex > std::numeric_limits::max()) { return READ_STATUS_INVALID; } if (uint32_t(lastprefilledindex) > cmpctblock.shorttxids.size() + i) { // If we are inserting a tx at an index greater than our full list // of shorttxids plus the number of prefilled txn we've inserted, // then we have txn for which we have neither a prefilled txn or a // shorttxid! return READ_STATUS_INVALID; } txns_available[lastprefilledindex] = prefilledtxn.tx; } prefilled_count = cmpctblock.prefilledtxn.size(); // Calculate map of txids -> positions and check mempool to see what we have // (or don't). Because well-formed cmpctblock messages will have a // (relatively) uniform distribution of short IDs, any highly-uneven // distribution of elements can be safely treated as a READ_STATUS_FAILED. - std::unordered_map shorttxids( + std::unordered_map shorttxids( cmpctblock.shorttxids.size()); - uint16_t index_offset = 0; + uint32_t index_offset = 0; for (size_t i = 0; i < cmpctblock.shorttxids.size(); i++) { while (txns_available[i + index_offset]) { index_offset++; } shorttxids[cmpctblock.shorttxids[i]] = i + index_offset; // To determine the chance that the number of entries in a bucket // exceeds N, we use the fact that the number of elements in a single // bucket is binomially distributed (with n = the number of shorttxids // S, and p = 1 / the number of buckets), that in the worst case the // number of buckets is equal to S (due to std::unordered_map having a // default load factor of 1.0), and that the chance for any bucket to // exceed N elements is at most buckets * (the chance that any given // bucket is above N elements). Thus: P(max_elements_per_bucket > N) <= // S * (1 - cdf(binomial(n=S,p=1/S), N)). If we assume blocks of up to // 16000, allowing 12 elements per bucket should only fail once per ~1 // million block transfers (per peer and connection). if (shorttxids.bucket_size( shorttxids.bucket(cmpctblock.shorttxids[i])) > 12) { return READ_STATUS_FAILED; } } // TODO: in the shortid-collision case, we should instead request both // transactions which collided. Falling back to full-block-request here is // overkill. if (shorttxids.size() != cmpctblock.shorttxids.size()) { // Short ID collision return READ_STATUS_FAILED; } std::vector have_txn(txns_available.size()); { LOCK(pool->cs); const std::vector> &vTxHashes = pool->vTxHashes; for (auto txHash : vTxHashes) { uint64_t shortid = cmpctblock.GetShortID(txHash.first); - std::unordered_map::iterator idit = + std::unordered_map::iterator idit = shorttxids.find(shortid); if (idit != shorttxids.end()) { if (!have_txn[idit->second]) { txns_available[idit->second] = txHash.second->GetSharedTx(); have_txn[idit->second] = true; mempool_count++; } else { // If we find two mempool txn that match the short id, just // request it. This should be rare enough that the extra // bandwidth doesn't matter, but eating a round-trip due to // FillBlock failure would be annoying. if (txns_available[idit->second]) { txns_available[idit->second].reset(); mempool_count--; } } } // Though ideally we'd continue scanning for the // two-txn-match-shortid case, the performance win of an early exit // here is too good to pass up and worth the extra risk. if (mempool_count == shorttxids.size()) { break; } } } for (auto &extra_txn : extra_txns) { uint64_t shortid = cmpctblock.GetShortID(extra_txn.first); - std::unordered_map::iterator idit = + std::unordered_map::iterator idit = shorttxids.find(shortid); if (idit != shorttxids.end()) { if (!have_txn[idit->second]) { txns_available[idit->second] = extra_txn.second; have_txn[idit->second] = true; mempool_count++; extra_count++; } else { // If we find two mempool/extra txn that match the short id, // just request it. This should be rare enough that the extra // bandwidth doesn't matter, but eating a round-trip due to // FillBlock failure would be annoying. Note that we dont want // duplication between extra_txns and mempool to trigger this // case, so we compare hashes first. if (txns_available[idit->second] && txns_available[idit->second]->GetHash() != extra_txn.second->GetHash()) { txns_available[idit->second].reset(); mempool_count--; extra_count--; } } } // Though ideally we'd continue scanning for the two-txn-match-shortid // case, the performance win of an early exit here is too good to pass // up and worth the extra risk. if (mempool_count == shorttxids.size()) { break; } } LogPrint(BCLog::CMPCTBLOCK, "Initialized PartiallyDownloadedBlock for " "block %s using a cmpctblock of size %lu\n", cmpctblock.header.GetHash().ToString(), GetSerializeSize(cmpctblock, SER_NETWORK, PROTOCOL_VERSION)); return READ_STATUS_OK; } bool PartiallyDownloadedBlock::IsTxAvailable(size_t index) const { assert(!header.IsNull()); assert(index < txns_available.size()); return txns_available[index] ? true : false; } ReadStatus PartiallyDownloadedBlock::FillBlock( CBlock &block, const std::vector &vtx_missing) { assert(!header.IsNull()); uint256 hash = header.GetHash(); block = header; block.vtx.resize(txns_available.size()); size_t tx_missing_offset = 0; for (size_t i = 0; i < txns_available.size(); i++) { auto &txn_available = txns_available[i]; if (!txn_available) { if (vtx_missing.size() <= tx_missing_offset) { return READ_STATUS_INVALID; } block.vtx[i] = vtx_missing[tx_missing_offset++]; } else { block.vtx[i] = std::move(txn_available); } } // Make sure we can't call FillBlock again. header.SetNull(); txns_available.clear(); if (vtx_missing.size() != tx_missing_offset) { return READ_STATUS_INVALID; } CValidationState state; if (!CheckBlock(*config, block, state)) { // TODO: We really want to just check merkle tree manually here, but // that is expensive, and CheckBlock caches a block's "checked-status" // (in the CBlock?). CBlock should be able to check its own merkle root // and cache that check. if (state.CorruptionPossible()) { // Possible Short ID collision. return READ_STATUS_FAILED; } return READ_STATUS_CHECKBLOCK_FAILED; } 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()); if (vtx_missing.size() < 5) { for (const auto &tx : vtx_missing) { LogPrint(BCLog::CMPCTBLOCK, "Reconstructed block %s required tx %s\n", hash.ToString(), tx->GetId().ToString()); } } return READ_STATUS_OK; } diff --git a/src/blockencodings.h b/src/blockencodings.h index 641f4ea85..373fdb46f 100644 --- a/src/blockencodings.h +++ b/src/blockencodings.h @@ -1,239 +1,239 @@ // Copyright (c) 2016 The Bitcoin Core developers // Distributed under the MIT software license, see the accompanying // file COPYING or http://www.opensource.org/licenses/mit-license.php. #ifndef BITCOIN_BLOCK_ENCODINGS_H #define BITCOIN_BLOCK_ENCODINGS_H #include "primitives/block.h" #include class Config; class CTxMemPool; // Dumb helper to handle CTransaction compression at serialize-time struct TransactionCompressor { private: CTransactionRef &tx; public: TransactionCompressor(CTransactionRef &txIn) : tx(txIn) {} ADD_SERIALIZE_METHODS; template inline void SerializationOp(Stream &s, Operation ser_action) { // TODO: Compress tx encoding READWRITE(tx); } }; class BlockTransactionsRequest { public: // A BlockTransactionsRequest message uint256 blockhash; - std::vector indices; + std::vector indices; ADD_SERIALIZE_METHODS; template inline void SerializationOp(Stream &s, Operation ser_action) { READWRITE(blockhash); uint64_t indices_size = uint64_t(indices.size()); READWRITE(COMPACTSIZE(indices_size)); if (ser_action.ForRead()) { size_t i = 0; while (indices.size() < indices_size) { indices.resize( std::min(uint64_t(1000 + indices.size()), indices_size)); for (; i < indices.size(); i++) { uint64_t n = 0; READWRITE(COMPACTSIZE(n)); - if (indices[i] > std::numeric_limits::max()) { + if (n > std::numeric_limits::max()) { throw std::ios_base::failure( - "index overflowed 16 bits"); + "index overflowed 32 bits"); } indices[i] = n; } } - uint16_t offset = 0; + uint32_t offset = 0; for (auto &index : indices) { if (uint64_t(index) + uint64_t(offset) > - std::numeric_limits::max()) { - throw std::ios_base::failure("indices overflowed 16 bits"); + std::numeric_limits::max()) { + throw std::ios_base::failure("indices overflowed 32 bits"); } index = index + offset; offset = index + 1; } } else { for (size_t i = 0; i < indices.size(); i++) { uint64_t index = indices[i] - (i == 0 ? 0 : (indices[i - 1] + 1)); READWRITE(COMPACTSIZE(index)); } } } }; class BlockTransactions { public: // A BlockTransactions message uint256 blockhash; std::vector txn; BlockTransactions() {} BlockTransactions(const BlockTransactionsRequest &req) : blockhash(req.blockhash), txn(req.indices.size()) {} ADD_SERIALIZE_METHODS; template inline void SerializationOp(Stream &s, Operation ser_action) { READWRITE(blockhash); uint64_t txn_size = (uint64_t)txn.size(); READWRITE(COMPACTSIZE(txn_size)); if (ser_action.ForRead()) { size_t i = 0; while (txn.size() < txn_size) { - txn.resize(std::min((uint64_t)(1000 + txn.size()), txn_size)); + txn.resize(std::min(uint64_t(1000 + txn.size()), txn_size)); for (; i < txn.size(); i++) { READWRITE(REF(TransactionCompressor(txn[i]))); } } } else { for (size_t i = 0; i < txn.size(); i++) { READWRITE(REF(TransactionCompressor(txn[i]))); } } } }; // Dumb serialization/storage-helper for CBlockHeaderAndShortTxIDs and // PartiallyDownloadedBlock struct PrefilledTransaction { // Used as an offset since last prefilled tx in CBlockHeaderAndShortTxIDs, // as a proper transaction-in-block-index in PartiallyDownloadedBlock - uint16_t index; + uint32_t index; CTransactionRef tx; ADD_SERIALIZE_METHODS; template inline void SerializationOp(Stream &s, Operation ser_action) { - uint64_t idx = index; - READWRITE(COMPACTSIZE(idx)); - if (idx > std::numeric_limits::max()) { - throw std::ios_base::failure("index overflowed 16-bits"); + uint64_t n = index; + READWRITE(COMPACTSIZE(n)); + if (n > std::numeric_limits::max()) { + throw std::ios_base::failure("index overflowed 32-bits"); } - index = idx; + index = n; READWRITE(REF(TransactionCompressor(tx))); } }; typedef enum ReadStatus_t { READ_STATUS_OK, // Invalid object, peer is sending bogus crap. // FIXME: differenciate bogus crap from crap that do not fit our policy. READ_STATUS_INVALID, // Failed to process object. READ_STATUS_FAILED, // Used only by FillBlock to indicate a failure in CheckBlock. READ_STATUS_CHECKBLOCK_FAILED, } ReadStatus; class CBlockHeaderAndShortTxIDs { private: mutable uint64_t shorttxidk0, shorttxidk1; uint64_t nonce; void FillShortTxIDSelector() const; friend class PartiallyDownloadedBlock; static const int SHORTTXIDS_LENGTH = 6; protected: std::vector shorttxids; std::vector prefilledtxn; public: CBlockHeader header; // Dummy for deserialization CBlockHeaderAndShortTxIDs() {} CBlockHeaderAndShortTxIDs(const CBlock &block); uint64_t GetShortID(const uint256 &txhash) const; size_t BlockTxCount() const { return shorttxids.size() + prefilledtxn.size(); } ADD_SERIALIZE_METHODS; template inline void SerializationOp(Stream &s, Operation ser_action) { READWRITE(header); READWRITE(nonce); uint64_t shorttxids_size = (uint64_t)shorttxids.size(); READWRITE(COMPACTSIZE(shorttxids_size)); if (ser_action.ForRead()) { size_t i = 0; while (shorttxids.size() < shorttxids_size) { shorttxids.resize(std::min(uint64_t(1000 + shorttxids.size()), shorttxids_size)); for (; i < shorttxids.size(); i++) { uint32_t lsb = 0; uint16_t msb = 0; READWRITE(lsb); READWRITE(msb); shorttxids[i] = (uint64_t(msb) << 32) | uint64_t(lsb); static_assert( SHORTTXIDS_LENGTH == 6, "shorttxids serialization assumes 6-byte shorttxids"); } } } else { for (uint64_t shortid : shorttxids) { uint32_t lsb = shortid & 0xffffffff; uint16_t msb = (shortid >> 32) & 0xffff; READWRITE(lsb); READWRITE(msb); } } READWRITE(prefilledtxn); if (ser_action.ForRead()) { FillShortTxIDSelector(); } } }; class PartiallyDownloadedBlock { protected: std::vector txns_available; size_t prefilled_count = 0, mempool_count = 0, extra_count = 0; CTxMemPool *pool; const Config *config; public: CBlockHeader header; PartiallyDownloadedBlock(const Config &configIn, CTxMemPool *poolIn) : pool(poolIn), config(&configIn) {} // extra_txn is a list of extra transactions to look at, in form. ReadStatus InitData(const CBlockHeaderAndShortTxIDs &cmpctblock, const std::vector> &extra_txn); bool IsTxAvailable(size_t index) const; ReadStatus FillBlock(CBlock &block, const std::vector &vtx_missing); }; #endif diff --git a/test/functional/abc-p2p-compactblocks.py b/test/functional/abc-p2p-compactblocks.py index d0ee12849..c30ba9194 100755 --- a/test/functional/abc-p2p-compactblocks.py +++ b/test/functional/abc-p2p-compactblocks.py @@ -1,353 +1,365 @@ #!/usr/bin/env python3 # Copyright (c) 2015-2016 The Bitcoin Core developers # Copyright (c) 2017 The Bitcoin developers # Distributed under the MIT software license, see the accompanying # file COPYING or http://www.opensource.org/licenses/mit-license.php. """ This test checks simple acceptance of bigger blocks via p2p. It is derived from the much more complex p2p-fullblocktest. The intention is that small tests can be derived from this one, or this one can be extended, to cover the checks done for bigger blocks (e.g. sigops limits). """ from test_framework.test_framework import ComparisonTestFramework from test_framework.util import * from test_framework.comptool import TestManager, TestInstance, RejectResult from test_framework.blocktools import * import time from test_framework.script import * from test_framework.cdefs import (ONE_MEGABYTE, LEGACY_MAX_BLOCK_SIZE, MAX_BLOCK_SIGOPS_PER_MB, MAX_TX_SIGOPS_COUNT) from collections import deque class PreviousSpendableOutput(): def __init__(self, tx=CTransaction(), n=-1): self.tx = tx self.n = n # the output we're spending # TestNode: A peer we use to send messages to bitcoind, and store responses. class TestNode(NodeConnCB): def __init__(self): self.last_sendcmpct = None self.last_cmpctblock = None self.last_getheaders = None self.last_headers = None super().__init__() def on_sendcmpct(self, conn, message): self.last_sendcmpct = message def on_cmpctblock(self, conn, message): self.last_cmpctblock = message self.last_cmpctblock.header_and_shortids.header.calc_sha256() def on_getheaders(self, conn, message): self.last_getheaders = message def on_headers(self, conn, message): self.last_headers = message for x in self.last_headers.headers: x.calc_sha256() def clear_block_data(self): with mininode_lock: self.last_sendcmpct = None self.last_cmpctblock = None class FullBlockTest(ComparisonTestFramework): # Can either run this test as 1 node with expected answers, or two and compare them. # Change the "outcome" variable from each TestInstance object to only do # the comparison. def set_test_params(self): self.num_nodes = 1 self.setup_clean_chain = True self.block_heights = {} self.tip = None self.blocks = {} - self.excessive_block_size = 100 * ONE_MEGABYTE + self.excessive_block_size = 16 * ONE_MEGABYTE self.extra_args = [['-norelaypriority', '-whitelist=127.0.0.1', - '-limitancestorcount=9999', - '-limitancestorsize=9999', - '-limitdescendantcount=9999', - '-limitdescendantsize=9999', - '-maxmempool=999', + '-limitancestorcount=999999', + '-limitancestorsize=999999', + '-limitdescendantcount=999999', + '-limitdescendantsize=999999', + '-maxmempool=99999', "-excessiveblocksize=%d" % self.excessive_block_size]] def add_options(self, parser): super().add_options(parser) parser.add_option( "--runbarelyexpensive", dest="runbarelyexpensive", default=True) def run_test(self): self.test = TestManager(self, self.options.tmpdir) self.test.add_all_connections(self.nodes) # Start up network handling in another thread NetworkThread().start() # Set the blocksize to 2MB as initial condition self.nodes[0].setexcessiveblock(self.excessive_block_size) self.test.run() def add_transactions_to_block(self, block, tx_list): [tx.rehash() for tx in tx_list] block.vtx.extend(tx_list) # this is a little handier to use than the version in blocktools.py def create_tx(self, spend_tx, n, value, script=CScript([OP_TRUE])): tx = create_transaction(spend_tx, n, b"", value, script) return tx - def next_block(self, number, spend=None, script=CScript([OP_TRUE]), block_size=0): + def next_block(self, number, spend=None, script=CScript([OP_TRUE]), block_size=0, extra_txns=0): if self.tip == None: base_block_hash = self.genesis_hash block_time = int(time.time()) + 1 else: base_block_hash = self.tip.sha256 block_time = self.tip.nTime + 1 # First create the coinbase height = self.block_heights[base_block_hash] + 1 coinbase = create_coinbase(height) coinbase.rehash() if spend == None: # We need to have something to spend to fill the block. assert_equal(block_size, 0) block = create_block(base_block_hash, coinbase, block_time) else: # all but one satoshi to fees coinbase.vout[0].nValue += spend.tx.vout[spend.n].nValue - 1 coinbase.rehash() block = create_block(base_block_hash, coinbase, block_time) # Make sure we have plenty engough to spend going forward. spendable_outputs = deque([spend]) def get_base_transaction(): # Create the new transaction tx = CTransaction() # Spend from one of the spendable outputs spend = spendable_outputs.popleft() tx.vin.append(CTxIn(COutPoint(spend.tx.sha256, spend.n))) # Add spendable outputs for i in range(4): tx.vout.append(CTxOut(0, CScript([OP_TRUE]))) spendable_outputs.append(PreviousSpendableOutput(tx, i)) return tx tx = get_base_transaction() # Make it the same format as transaction added for padding and save the size. # It's missing the padding output, so we add a constant to account for it. tx.rehash() base_tx_size = len(tx.serialize()) + 18 # If a specific script is required, add it. if script != None: tx.vout.append(CTxOut(1, script)) # Put some random data into the first transaction of the chain to randomize ids. tx.vout.append( CTxOut(0, CScript([random.randint(0, 256), OP_RETURN]))) # Add the transaction to the block self.add_transactions_to_block(block, [tx]) + # Add transaction until we reach the expected transaction count + for _ in range(extra_txns): + self.add_transactions_to_block(block, [get_base_transaction()]) + # If we have a block size requirement, just fill # the block until we get there current_block_size = len(block.serialize()) while current_block_size < block_size: # We will add a new transaction. That means the size of # the field enumerating how many transaction go in the block # may change. current_block_size -= len(ser_compact_size(len(block.vtx))) current_block_size += len(ser_compact_size(len(block.vtx) + 1)) # Create the new transaction tx = get_base_transaction() # Add padding to fill the block. script_length = block_size - current_block_size - base_tx_size if script_length > 510000: if script_length < 1000000: # Make sure we don't find ourselves in a position where we # need to generate a transaction smaller than what we expected. script_length = script_length // 2 else: script_length = 500000 script_output = CScript([b'\x00' * script_length]) tx.vout.append(CTxOut(0, script_output)) # Add the tx to the list of transactions to be included # in the block. self.add_transactions_to_block(block, [tx]) current_block_size += len(tx.serialize()) # Now that we added a bunch of transaction, we need to recompute # the merkle root. block.hashMerkleRoot = block.calc_merkle_root() # Check that the block size is what's expected if block_size > 0: assert_equal(len(block.serialize()), block_size) # Do PoW, which is cheap on regnet block.solve() self.tip = block self.block_heights[block.sha256] = height assert number not in self.blocks self.blocks[number] = block return block def get_tests(self): self.genesis_hash = int(self.nodes[0].getbestblockhash(), 16) self.block_heights[self.genesis_hash] = 0 spendable_outputs = [] # save the current tip so it can be spent by a later block def save_spendable_output(): spendable_outputs.append(self.tip) # get an output that we previously marked as spendable def get_spendable_output(): return PreviousSpendableOutput(spendable_outputs.pop(0).vtx[0], 0) # returns a test case that asserts that the current tip was accepted def accepted(): return TestInstance([[self.tip, True]]) # returns a test case that asserts that the current tip was rejected def rejected(reject=None): if reject is None: return TestInstance([[self.tip, False]]) else: return TestInstance([[self.tip, reject]]) # move the tip back to a previous block def tip(number): self.tip = self.blocks[number] # adds transactions to the block and updates state def update_block(block_number, new_transactions): block = self.blocks[block_number] self.add_transactions_to_block(block, new_transactions) old_sha256 = block.sha256 block.hashMerkleRoot = block.calc_merkle_root() block.solve() # Update the internal state just like in next_block self.tip = block if block.sha256 != old_sha256: self.block_heights[ block.sha256] = self.block_heights[old_sha256] del self.block_heights[old_sha256] self.blocks[block_number] = block return block # shorthand for functions block = self.next_block # Create a new block block(0) save_spendable_output() yield accepted() # Now we need that block to mature so we can spend the coinbase. test = TestInstance(sync_every_block=False) for i in range(99): block(5000 + i) test.blocks_and_transactions.append([self.tip, True]) save_spendable_output() yield test # collect spendable outputs now to avoid cluttering the code later on out = [] for i in range(100): out.append(get_spendable_output()) # Check that compact block also work for big blocks node = self.nodes[0] peer = TestNode() peer.add_connection(NodeConn('127.0.0.1', p2p_port(0), node, peer)) # Start up network handling in another thread and wait for connection # to be etablished NetworkThread().start() peer.wait_for_verack() # Wait for SENDCMPCT def received_sendcmpct(): return (peer.last_sendcmpct != None) wait_until(received_sendcmpct, timeout=30) sendcmpct = msg_sendcmpct() sendcmpct.version = 1 sendcmpct.announce = True peer.send_and_ping(sendcmpct) # Exchange headers def received_getheaders(): return (peer.last_getheaders != None) wait_until(received_getheaders, timeout=30) # Return the favor peer.send_message(peer.last_getheaders) # Wait for the header list def received_headers(): return (peer.last_headers != None) wait_until(received_headers, timeout=30) # It's like we know about the same headers ! peer.send_message(peer.last_headers) # Send a block b1 = block(1, spend=out[0], block_size=ONE_MEGABYTE + 1) yield accepted() # Checks the node to forward it via compact block def received_block(): return (peer.last_cmpctblock != None) wait_until(received_block, timeout=30) # Was it our block ? cmpctblk_header = peer.last_cmpctblock.header_and_shortids.header cmpctblk_header.calc_sha256() assert(cmpctblk_header.sha256 == b1.sha256) - # Send a bigger block + # Send a large block with numerous transactions. peer.clear_block_data() - b2 = block(2, spend=out[1], block_size=self.excessive_block_size) + b2 = block(2, spend=out[1], extra_txns=70000, + block_size=self.excessive_block_size - 1000) yield accepted() # Checks the node forwards it via compact block wait_until(received_block, timeout=30) # Was it our block ? cmpctblk_header = peer.last_cmpctblock.header_and_shortids.header cmpctblk_header.calc_sha256() assert(cmpctblk_header.sha256 == b2.sha256) + # In order to avoid having to resend a ton of transactions, we invalidate + # b2, which will send all its transactions in the mempool. + node.invalidateblock(node.getbestblockhash()) + # Let's send a compact block and see if the node accepts it. - # First, we generate the block and send all transaction to the mempool - b3 = block(3, spend=out[2], block_size=8 * ONE_MEGABYTE) - for i in range(1, len(b3.vtx)): - node.sendrawtransaction(ToHex(b3.vtx[i]), True) + # Let's modify b2 and use it so that we can reuse the mempool. + tx = b2.vtx[0] + tx.vout.append(CTxOut(0, CScript([random.randint(0, 256), OP_RETURN]))) + tx.rehash() + b2.vtx[0] = tx + b2.hashMerkleRoot = b2.calc_merkle_root() + b2.solve() # Now we create the compact block and send it comp_block = HeaderAndShortIDs() - comp_block.initialize_from_block(b3) + comp_block.initialize_from_block(b2) peer.send_and_ping(msg_cmpctblock(comp_block.to_p2p())) # Check that compact block is received properly - assert(int(node.getbestblockhash(), 16) == b3.sha256) + assert(int(node.getbestblockhash(), 16) == b2.sha256) if __name__ == '__main__': FullBlockTest().main()