Skip to content

Commit 9d60602

Browse files
committed
Merge pull request #5875
aa8c827 P2P regression test for new AcceptBlock behavior (Suhas Daftuar) 9be0e68 Be stricter in processing unrequested blocks (Suhas Daftuar)
2 parents 9d67b10 + aa8c827 commit 9d60602

File tree

7 files changed

+260
-22
lines changed

7 files changed

+260
-22
lines changed

qa/pull-tester/rpc-tests.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ testScriptsExt=(
5151
'invalidblockrequest.py'
5252
'rawtransactions.py'
5353
# 'forknotify.py'
54+
'p2p-acceptblock.py'
5455
);
5556

5657
extArg="-extended"

qa/rpc-tests/p2p-acceptblock.py

Lines changed: 226 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,226 @@
1+
#!/usr/bin/env python2
2+
#
3+
# Distributed under the MIT/X11 software license, see the accompanying
4+
# file COPYING or http://www.opensource.org/licenses/mit-license.php.
5+
#
6+
7+
from test_framework.mininode import *
8+
from test_framework.test_framework import BitcoinTestFramework
9+
from test_framework.util import *
10+
import time
11+
from test_framework.blocktools import create_block, create_coinbase
12+
13+
'''
14+
AcceptBlockTest -- test processing of unrequested blocks.
15+
16+
Since behavior differs when receiving unrequested blocks from whitelisted peers
17+
versus non-whitelisted peers, this tests the behavior of both (effectively two
18+
separate tests running in parallel).
19+
20+
Setup: two nodes, node0 and node1, not connected to each other. Node0 does not
21+
whitelist localhost, but node1 does. They will each be on their own chain for
22+
this test.
23+
24+
We have one NodeConn connection to each, test_node and white_node respectively.
25+
26+
The test:
27+
1. Generate one block on each node, to leave IBD.
28+
29+
2. Mine a new block on each tip, and deliver to each node from node's peer.
30+
The tip should advance.
31+
32+
3. Mine a block that forks the previous block, and deliver to each node from
33+
corresponding peer.
34+
Node0 should not process this block (just accept the header), because it is
35+
unrequested and doesn't have more work than the tip.
36+
Node1 should process because this is coming from a whitelisted peer.
37+
38+
4. Send another block that builds on the forking block.
39+
Node0 should process this block but be stuck on the shorter chain, because
40+
it's missing an intermediate block.
41+
Node1 should reorg to this longer chain.
42+
43+
5. Send a duplicate of the block in #3 to Node0.
44+
Node0 should not process the block because it is unrequested, and stay on
45+
the shorter chain.
46+
47+
6. Send Node0 an inv for the height 3 block produced in #4 above.
48+
Node0 should figure out that Node0 has the missing height 2 block and send a
49+
getdata.
50+
51+
7. Send Node0 the missing block again.
52+
Node0 should process and the tip should advance.
53+
'''
54+
55+
# TestNode: bare-bones "peer". Used mostly as a conduit for a test to sending
56+
# p2p messages to a node, generating the messages in the main testing logic.
57+
class TestNode(NodeConnCB):
58+
def __init__(self):
59+
NodeConnCB.__init__(self)
60+
self.create_callback_map()
61+
self.connection = None
62+
63+
def add_connection(self, conn):
64+
self.connection = conn
65+
66+
# Track the last getdata message we receive (used in the test)
67+
def on_getdata(self, conn, message):
68+
self.last_getdata = message
69+
70+
# Spin until verack message is received from the node.
71+
# We use this to signal that our test can begin. This
72+
# is called from the testing thread, so it needs to acquire
73+
# the global lock.
74+
def wait_for_verack(self):
75+
while True:
76+
with mininode_lock:
77+
if self.verack_received:
78+
return
79+
time.sleep(0.05)
80+
81+
# Wrapper for the NodeConn's send_message function
82+
def send_message(self, message):
83+
self.connection.send_message(message)
84+
85+
class AcceptBlockTest(BitcoinTestFramework):
86+
def add_options(self, parser):
87+
parser.add_option("--testbinary", dest="testbinary",
88+
default=os.getenv("BITCOIND", "bitcoind"),
89+
help="bitcoind binary to test")
90+
91+
def setup_chain(self):
92+
initialize_chain_clean(self.options.tmpdir, 2)
93+
94+
def setup_network(self):
95+
# Node0 will be used to test behavior of processing unrequested blocks
96+
# from peers which are not whitelisted, while Node1 will be used for
97+
# the whitelisted case.
98+
self.nodes = []
99+
self.nodes.append(start_node(0, self.options.tmpdir, ["-debug"],
100+
binary=self.options.testbinary))
101+
self.nodes.append(start_node(1, self.options.tmpdir,
102+
["-debug", "-whitelist=127.0.0.1"],
103+
binary=self.options.testbinary))
104+
105+
def run_test(self):
106+
# Setup the p2p connections and start up the network thread.
107+
test_node = TestNode() # connects to node0 (not whitelisted)
108+
white_node = TestNode() # connects to node1 (whitelisted)
109+
110+
connections = []
111+
connections.append(NodeConn('127.0.0.1', p2p_port(0), self.nodes[0], test_node))
112+
connections.append(NodeConn('127.0.0.1', p2p_port(1), self.nodes[1], white_node))
113+
test_node.add_connection(connections[0])
114+
white_node.add_connection(connections[1])
115+
116+
NetworkThread().start() # Start up network handling in another thread
117+
118+
# Test logic begins here
119+
test_node.wait_for_verack()
120+
white_node.wait_for_verack()
121+
122+
# 1. Have both nodes mine a block (leave IBD)
123+
[ n.generate(1) for n in self.nodes ]
124+
tips = [ int ("0x" + n.getbestblockhash() + "L", 0) for n in self.nodes ]
125+
126+
# 2. Send one block that builds on each tip.
127+
# This should be accepted.
128+
blocks_h2 = [] # the height 2 blocks on each node's chain
129+
for i in xrange(2):
130+
blocks_h2.append(create_block(tips[i], create_coinbase(), time.time()+1))
131+
blocks_h2[i].solve()
132+
test_node.send_message(msg_block(blocks_h2[0]))
133+
white_node.send_message(msg_block(blocks_h2[1]))
134+
135+
time.sleep(1)
136+
assert_equal(self.nodes[0].getblockcount(), 2)
137+
assert_equal(self.nodes[1].getblockcount(), 2)
138+
print "First height 2 block accepted by both nodes"
139+
140+
# 3. Send another block that builds on the original tip.
141+
blocks_h2f = [] # Blocks at height 2 that fork off the main chain
142+
for i in xrange(2):
143+
blocks_h2f.append(create_block(tips[i], create_coinbase(), blocks_h2[i].nTime+1))
144+
blocks_h2f[i].solve()
145+
test_node.send_message(msg_block(blocks_h2f[0]))
146+
white_node.send_message(msg_block(blocks_h2f[1]))
147+
148+
time.sleep(1) # Give time to process the block
149+
for x in self.nodes[0].getchaintips():
150+
if x['hash'] == blocks_h2f[0].hash:
151+
assert_equal(x['status'], "headers-only")
152+
153+
for x in self.nodes[1].getchaintips():
154+
if x['hash'] == blocks_h2f[1].hash:
155+
assert_equal(x['status'], "valid-headers")
156+
157+
print "Second height 2 block accepted only from whitelisted peer"
158+
159+
# 4. Now send another block that builds on the forking chain.
160+
blocks_h3 = []
161+
for i in xrange(2):
162+
blocks_h3.append(create_block(blocks_h2f[i].sha256, create_coinbase(), blocks_h2f[i].nTime+1))
163+
blocks_h3[i].solve()
164+
test_node.send_message(msg_block(blocks_h3[0]))
165+
white_node.send_message(msg_block(blocks_h3[1]))
166+
167+
time.sleep(1)
168+
# Since the earlier block was not processed by node0, the new block
169+
# can't be fully validated.
170+
for x in self.nodes[0].getchaintips():
171+
if x['hash'] == blocks_h3[0].hash:
172+
assert_equal(x['status'], "headers-only")
173+
174+
# But this block should be accepted by node0 since it has more work.
175+
try:
176+
self.nodes[0].getblock(blocks_h3[0].hash)
177+
print "Unrequested more-work block accepted from non-whitelisted peer"
178+
except:
179+
raise AssertionError("Unrequested more work block was not processed")
180+
181+
# Node1 should have accepted and reorged.
182+
assert_equal(self.nodes[1].getblockcount(), 3)
183+
print "Successfully reorged to length 3 chain from whitelisted peer"
184+
185+
# 5. Test handling of unrequested block on the node that didn't process
186+
# Should still not be processed (even though it has a child that has more
187+
# work).
188+
test_node.send_message(msg_block(blocks_h2f[0]))
189+
190+
# Here, if the sleep is too short, the test could falsely succeed (if the
191+
# node hasn't processed the block by the time the sleep returns, and then
192+
# the node processes it and incorrectly advances the tip).
193+
# But this would be caught later on, when we verify that an inv triggers
194+
# a getdata request for this block.
195+
time.sleep(1)
196+
assert_equal(self.nodes[0].getblockcount(), 2)
197+
print "Unrequested block that would complete more-work chain was ignored"
198+
199+
# 6. Try to get node to request the missing block.
200+
# Poke the node with an inv for block at height 3 and see if that
201+
# triggers a getdata on block 2 (it should if block 2 is missing).
202+
with mininode_lock:
203+
# Clear state so we can check the getdata request
204+
test_node.last_getdata = None
205+
test_node.send_message(msg_inv([CInv(2, blocks_h3[0].sha256)]))
206+
207+
time.sleep(1)
208+
with mininode_lock:
209+
getdata = test_node.last_getdata
210+
211+
# Check that the getdata is for the right block
212+
assert_equal(len(getdata.inv), 1)
213+
assert_equal(getdata.inv[0].hash, blocks_h2f[0].sha256)
214+
print "Inv at tip triggered getdata for unprocessed block"
215+
216+
# 7. Send the missing block for the third time (now it is requested)
217+
test_node.send_message(msg_block(blocks_h2f[0]))
218+
219+
time.sleep(1)
220+
assert_equal(self.nodes[0].getblockcount(), 3)
221+
print "Successfully reorged to length 3 chain from non-whitelisted peer"
222+
223+
[ c.disconnect_node() for c in connections ]
224+
225+
if __name__ == '__main__':
226+
AcceptBlockTest().main()

src/main.cpp

Lines changed: 25 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -296,7 +296,8 @@ void FinalizeNode(NodeId nodeid) {
296296
}
297297

298298
// Requires cs_main.
299-
void MarkBlockAsReceived(const uint256& hash) {
299+
// Returns a bool indicating whether we requested this block.
300+
bool MarkBlockAsReceived(const uint256& hash) {
300301
map<uint256, pair<NodeId, list<QueuedBlock>::iterator> >::iterator itInFlight = mapBlocksInFlight.find(hash);
301302
if (itInFlight != mapBlocksInFlight.end()) {
302303
CNodeState *state = State(itInFlight->second.first);
@@ -306,7 +307,9 @@ void MarkBlockAsReceived(const uint256& hash) {
306307
state->nBlocksInFlight--;
307308
state->nStallingSince = 0;
308309
mapBlocksInFlight.erase(itInFlight);
310+
return true;
309311
}
312+
return false;
310313
}
311314

312315
// Requires cs_main.
@@ -2826,7 +2829,7 @@ bool AcceptBlockHeader(const CBlockHeader& block, CValidationState& state, CBloc
28262829
return true;
28272830
}
28282831

2829-
bool AcceptBlock(CBlock& block, CValidationState& state, CBlockIndex** ppindex, CDiskBlockPos* dbp)
2832+
bool AcceptBlock(CBlock& block, CValidationState& state, CBlockIndex** ppindex, bool fRequested, CDiskBlockPos* dbp)
28302833
{
28312834
const CChainParams& chainparams = Params();
28322835
AssertLockHeld(cs_main);
@@ -2836,13 +2839,18 @@ bool AcceptBlock(CBlock& block, CValidationState& state, CBlockIndex** ppindex,
28362839
if (!AcceptBlockHeader(block, state, &pindex))
28372840
return false;
28382841

2839-
// If we're pruning, ensure that we don't allow a peer to dump a copy
2840-
// of old blocks. But we might need blocks that are not on the main chain
2841-
// to handle a reorg, even if we've processed once.
2842-
if (pindex->nStatus & BLOCK_HAVE_DATA || chainActive.Contains(pindex)) {
2843-
// TODO: deal better with duplicate blocks.
2844-
// return state.DoS(20, error("AcceptBlock(): already have block %d %s", pindex->nHeight, pindex->GetBlockHash().ToString()), REJECT_DUPLICATE, "duplicate");
2845-
return true;
2842+
// Try to process all requested blocks that we don't have, but only
2843+
// process an unrequested block if it's new and has enough work to
2844+
// advance our tip.
2845+
bool fAlreadyHave = pindex->nStatus & BLOCK_HAVE_DATA;
2846+
bool fHasMoreWork = (chainActive.Tip() ? pindex->nChainWork > chainActive.Tip()->nChainWork : true);
2847+
2848+
// TODO: deal better with return value and error conditions for duplicate
2849+
// and unrequested blocks.
2850+
if (fAlreadyHave) return true;
2851+
if (!fRequested) { // If we didn't ask for it:
2852+
if (pindex->nTx != 0) return true; // This is a previously-processed block that was pruned
2853+
if (!fHasMoreWork) return true; // Don't process less-work chains
28462854
}
28472855

28482856
if ((!CheckBlock(block, state)) || !ContextualCheckBlock(block, state, pindex->pprev)) {
@@ -2891,21 +2899,22 @@ static bool IsSuperMajority(int minVersion, const CBlockIndex* pstart, unsigned
28912899
}
28922900

28932901

2894-
bool ProcessNewBlock(CValidationState &state, CNode* pfrom, CBlock* pblock, CDiskBlockPos *dbp)
2902+
bool ProcessNewBlock(CValidationState &state, CNode* pfrom, CBlock* pblock, bool fForceProcessing, CDiskBlockPos *dbp)
28952903
{
28962904
// Preliminary checks
28972905
bool checked = CheckBlock(*pblock, state);
28982906

28992907
{
29002908
LOCK(cs_main);
2901-
MarkBlockAsReceived(pblock->GetHash());
2909+
bool fRequested = MarkBlockAsReceived(pblock->GetHash());
2910+
fRequested |= fForceProcessing;
29022911
if (!checked) {
29032912
return error("%s: CheckBlock FAILED", __func__);
29042913
}
29052914

29062915
// Store to disk
29072916
CBlockIndex *pindex = NULL;
2908-
bool ret = AcceptBlock(*pblock, state, &pindex, dbp);
2917+
bool ret = AcceptBlock(*pblock, state, &pindex, fRequested, dbp);
29092918
if (pindex && pfrom) {
29102919
mapBlockSource[pindex->GetBlockHash()] = pfrom->GetId();
29112920
}
@@ -3453,7 +3462,7 @@ bool LoadExternalBlockFile(FILE* fileIn, CDiskBlockPos *dbp)
34533462
// process in case the block isn't known yet
34543463
if (mapBlockIndex.count(hash) == 0 || (mapBlockIndex[hash]->nStatus & BLOCK_HAVE_DATA) == 0) {
34553464
CValidationState state;
3456-
if (ProcessNewBlock(state, NULL, &block, dbp))
3465+
if (ProcessNewBlock(state, NULL, &block, true, dbp))
34573466
nLoaded++;
34583467
if (state.IsError())
34593468
break;
@@ -3475,7 +3484,7 @@ bool LoadExternalBlockFile(FILE* fileIn, CDiskBlockPos *dbp)
34753484
LogPrintf("%s: Processing out of order child %s of %s\n", __func__, block.GetHash().ToString(),
34763485
head.ToString());
34773486
CValidationState dummy;
3478-
if (ProcessNewBlock(dummy, NULL, &block, &it->second))
3487+
if (ProcessNewBlock(dummy, NULL, &block, true, &it->second))
34793488
{
34803489
nLoaded++;
34813490
queue.push_back(block.GetHash());
@@ -4462,7 +4471,8 @@ bool static ProcessMessage(CNode* pfrom, string strCommand, CDataStream& vRecv,
44624471
pfrom->AddInventoryKnown(inv);
44634472

44644473
CValidationState state;
4465-
ProcessNewBlock(state, pfrom, &block);
4474+
// Process all blocks from whitelisted peers, even if not requested.
4475+
ProcessNewBlock(state, pfrom, &block, pfrom->fWhitelisted, NULL);
44664476
int nDoS;
44674477
if (state.IsInvalid(nDoS)) {
44684478
pfrom->PushMessage("reject", strCommand, state.GetRejectCode(),

src/main.h

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -153,10 +153,11 @@ void UnregisterNodeSignals(CNodeSignals& nodeSignals);
153153
* @param[out] state This may be set to an Error state if any error occurred processing it, including during validation/connection/etc of otherwise unrelated blocks during reorganisation; or it may be set to an Invalid state if pblock is itself invalid (but this is not guaranteed even when the block is checked). If you want to *possibly* get feedback on whether pblock is valid, you must also install a CValidationInterface (see validationinterface.h) - this will have its BlockChecked method called whenever *any* block completes validation.
154154
* @param[in] pfrom The node which we are receiving the block from; it is added to mapBlockSource and may be penalised if the block is invalid.
155155
* @param[in] pblock The block we want to process.
156+
* @param[in] fForceProcessing Process this block even if unrequested; used for non-network block sources and whitelisted peers.
156157
* @param[out] dbp If pblock is stored to disk (or already there), this will be set to its location.
157158
* @return True if state.IsValid()
158159
*/
159-
bool ProcessNewBlock(CValidationState &state, CNode* pfrom, CBlock* pblock, CDiskBlockPos *dbp = NULL);
160+
bool ProcessNewBlock(CValidationState &state, CNode* pfrom, CBlock* pblock, bool fForceProcessing, CDiskBlockPos *dbp);
160161
/** Check whether enough disk space is available for an incoming block */
161162
bool CheckDiskSpace(uint64_t nAdditionalBytes = 0);
162163
/** Open a block file (blk?????.dat) */
@@ -400,8 +401,8 @@ bool ContextualCheckBlock(const CBlock& block, CValidationState& state, CBlockIn
400401
/** Check a block is completely valid from start to finish (only works on top of our current best block, with cs_main held) */
401402
bool TestBlockValidity(CValidationState &state, const CBlock& block, CBlockIndex *pindexPrev, bool fCheckPOW = true, bool fCheckMerkleRoot = true);
402403

403-
/** Store block on disk. If dbp is provided, the file is known to already reside on disk */
404-
bool AcceptBlock(CBlock& block, CValidationState& state, CBlockIndex **pindex, CDiskBlockPos* dbp = NULL);
404+
/** Store block on disk. If dbp is non-NULL, the file is known to already reside on disk */
405+
bool AcceptBlock(CBlock& block, CValidationState& state, CBlockIndex **pindex, bool fRequested, CDiskBlockPos* dbp);
405406
bool AcceptBlockHeader(const CBlockHeader& block, CValidationState& state, CBlockIndex **ppindex= NULL);
406407

407408

src/miner.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -434,7 +434,7 @@ static bool ProcessBlockFound(CBlock* pblock, CWallet& wallet, CReserveKey& rese
434434

435435
// Process this block the same as if we had received it from another node
436436
CValidationState state;
437-
if (!ProcessNewBlock(state, NULL, pblock))
437+
if (!ProcessNewBlock(state, NULL, pblock, true, NULL))
438438
return error("BitcoinMiner: ProcessNewBlock, block not accepted");
439439

440440
return true;

src/rpcmining.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,7 @@ Value generate(const Array& params, bool fHelp)
164164
++pblock->nNonce;
165165
}
166166
CValidationState state;
167-
if (!ProcessNewBlock(state, NULL, pblock))
167+
if (!ProcessNewBlock(state, NULL, pblock, true, NULL))
168168
throw JSONRPCError(RPC_INTERNAL_ERROR, "ProcessNewBlock, block not accepted");
169169
++nHeight;
170170
blockHashes.push_back(pblock->GetHash().GetHex());
@@ -650,7 +650,7 @@ Value submitblock(const Array& params, bool fHelp)
650650
CValidationState state;
651651
submitblock_StateCatcher sc(block.GetHash());
652652
RegisterValidationInterface(&sc);
653-
bool fAccepted = ProcessNewBlock(state, NULL, &block);
653+
bool fAccepted = ProcessNewBlock(state, NULL, &block, true, NULL);
654654
UnregisterValidationInterface(&sc);
655655
if (fBlockPresent)
656656
{

0 commit comments

Comments
 (0)