Modify getblocktxn handler not to drop requests for old blocks

This commit is contained in:
Russell Yanofsky
2016-10-26 14:53:18 -04:00
committed by lateminer
parent e13c064eec
commit dc8f0bd8de
2 changed files with 22 additions and 4 deletions

View File

@@ -524,8 +524,8 @@ class CompactBlocksTest(BitcoinTestFramework):
def test_getblocktxn_handler(self):
print("Testing getblocktxn handler...")
# bitcoind won't respond for blocks whose height is more than 15 blocks
# deep.
# bitcoind will not send blocktxn responses for blocks whose height is
# more than 10 blocks deep.
MAX_GETBLOCKTXN_DEPTH = 10
chain_height = self.nodes[0].getblockcount()
current_height = chain_height
@@ -552,11 +552,17 @@ class CompactBlocksTest(BitcoinTestFramework):
self.test_node.last_blocktxn = None
current_height -= 1
# Next request should be ignored, as we're past the allowed depth.
# Next request should send a full block response, as we're past the
# allowed depth for a blocktxn response.
block_hash = self.nodes[0].getblockhash(current_height)
msg.block_txn_request = BlockTransactionsRequest(int(block_hash, 16), [0])
with mininode_lock:
self.test_node.last_block = None
self.test_node.last_blocktxn = None
self.test_node.send_and_ping(msg)
with mininode_lock:
self.test_node.last_block.block.calc_sha256()
assert_equal(self.test_node.last_block.block.sha256, int(block_hash, 16))
assert_equal(self.test_node.last_blocktxn, None)
def test_compactblocks_not_at_tip(self):

View File

@@ -5453,7 +5453,19 @@ bool static ProcessMessage(CNode* pfrom, string strCommand, CDataStream& vRecv,
}
if (it->second->nHeight < chainActive.Height() - MAX_BLOCKTXN_DEPTH) {
// If an older block is requested (should never happen in practice,
// but can happen in tests) send a block response instead of a
// blocktxn response. Sending a full block response instead of a
// small blocktxn response is preferable in the case where a peer
// might maliciously send lots of getblocktxn requests to trigger
// expensive disk reads, because it will require the peer to
// actually receive all the data read from disk over the network.
LogPrint("net", "Peer %d sent us a getblocktxn for a block > %i deep", pfrom->id, MAX_BLOCKTXN_DEPTH);
CInv inv;
inv.type = MSG_BLOCK;
inv.hash = req.blockhash;
pfrom->vRecvGetData.push_back(inv);
ProcessGetData(pfrom, chainparams.GetConsensus());
return true;
}
@@ -5768,7 +5780,7 @@ bool static ProcessMessage(CNode* pfrom, string strCommand, CDataStream& vRecv,
if (!fAlreadyInFlight && mapBlocksInFlight.size() == 1 && pindex->pprev->IsValid(BLOCK_VALID_CHAIN)) {
// We seem to be rather well-synced, so it appears pfrom was the first to provide us
// with this block! Let's get them to announce using compact blocks in the future.
MaybeSetPeerAsAnnouncingHeaderAndIDs(nodestate, pfrom, connman);
MaybeSetPeerAsAnnouncingHeaderAndIDs(nodestate, pfrom);
}
BlockTransactionsRequest req;