Skip to content

test: resyncing to tip of blocks generated after invalidateblock - #19397

Closed
jonatack wants to merge 1 commit into
bitcoin:masterfrom
jonatack:p2p-invalidateblock-tests
Closed

jonatack wants to merge 1 commit into
bitcoin:masterfrom
jonatack:p2p-invalidateblock-tests

Conversation

@jonatack

@jonatack jonatack commented Jun 27, 2020 •

Copy link
Copy Markdown
Member

This commit picks up #17335 to add an updated version of a test by Suhas Daftuar in issue #5806 to demonstrate a block propagation issue for the first coinbase following an invalidateblock. As Suhas reported: "[only] the first 16 [blocks] will make it, because of the call to getdata on processing each inv, which occurs until we've maxed out the number of blocks in flight to that peer."

Here we test that after running invalidateblock, on the first coinbase transaction nodes will resync to a new lower chain tip, but for the non-mining node only reliably up to the number of blocks equal to the value (16) of MAX_BLOCKS_IN_TRANSIT_PER_PEER. We then check that it catches up to the chain tip with the next coinbase transactions.

Reviewers may test this by running the test with different values of BLOCKS in the test and with rebuilding using different values of MAX_BLOCKS_IN_TRANSIT_PER_PEER in net_processing.cpp.

Comment thread test/functional/p2p_invalidateblock.py Resolved
@jnewbery

Copy link
Copy Markdown
Contributor

Concept ACK. Thanks for picking this up, Jon!

@jonatack
jonatack force-pushed the p2p-invalidateblock-tests branch 2 times, most recently from 308cd07 to f23984d Compare June 28, 2020 08:32
@jonatack

Copy link
Copy Markdown
Member Author

Concept ACK. Thanks for picking this up, Jon!

Thanks, John. The interesting thing is, unlike the PR17335 description, the original issue reported by @sdaftuar still appears to be unresolved.

@jonatack
jonatack force-pushed the p2p-invalidateblock-tests branch from f23984d to 6ee65be Compare June 29, 2020 03:06
@jonatack

Copy link
Copy Markdown
Member Author

Rebased to add more tests to verify that the non-mining node does catch up to the chain tip on the next coinbase transactions after the first one.

@jonatack
jonatack force-pushed the p2p-invalidateblock-tests branch from 6ee65be to c48c680 Compare June 29, 2020 03:18
@jnewbery

Copy link
Copy Markdown
Contributor

Thanks for digging into this more, @jonatack . I've run the test with the assert catching removed to reproduce the error. I've attached a combined log (best viewed with less -r). Here's what's happening:

If we invalidate MAX_BLOCKS_IN_TRANSIT_PER_PEER + 2 blocks and then regenerate MAX_BLOCKS_IN_TRANSIT_PER_PEER + 1 blocks on node0, node0 doesn't announce those new blocks to the peer using compact blocks because of this logic in NewPoWValidBlock():

bitcoin/src/net_processing.cpp

Lines 1270 to 1272 in 1269cab

static int nHighestFastAnnounce = 0;
if (pindex->nHeight <= nHighestFastAnnounce)
return;

(ie the new block isn't as high as a block node0 has previously announced via compact blocks)

Instead, node0 relies on block inventory sending in SendMessages():

bitcoin/src/net_processing.cpp

Lines 3956 to 3971 in 1269cab

//
// Try sending block announcements via headers
//
{
// If we have less than MAX_BLOCKS_TO_ANNOUNCE in our
// list of block hashes we're relaying, and our peer wants
// headers announcements, then find the first header
// not yet known to our peer but would connect, and send.
// If no header would connect, or if we have too many
// blocks, or if the peer doesn't want headers, just
// add all to the inv queue.
LOCK(pto->cs_inventory);
std::vector<CBlock> vHeaders;
bool fRevertToInv = ((!state.fPreferHeaders &&
(!state.fPreferHeaderAndIDs || pto->vBlockHashesToAnnounce.size() > 1)) ||
pto->vBlockHashesToAnnounce.size() > MAX_BLOCKS_TO_ANNOUNCE);

We're generating the blocks on the RPC thread (httpworker.x), but this block inventory relay happens on the MessageHandler thread. The timing windows for those two threads is the reason for the intermittency of this test failing.

In my failing case, all 17 blocks are generated while the message handler thread is sleeping. Here's the sleep on the message handler thread:

condMsgProc.wait_until(lock, std::chrono::steady_clock::now() + std::chrono::milliseconds(100), [this]() EXCLUSIVE_LOCKS_REQUIRED(mutexMsgProc) { return fMsgProcWake; });

Here it is waking up to send the blocks:

node0 2020-06-29T14:27:10.231122Z [msghand] SendMessages: sending inv peer=0 hash=3dafa6054777a53fd32abcb6f6eefe67591e93c275d23d07696313efed464ebe 
node0 2020-06-29T14:27:10.231172Z [msghand] sending inv (37 bytes) peer=0 

and here is the first of the 17 blocks being generated:

test  2020-06-29T14:27:10.213000Z TestFramework (INFO):  Generate an alternate chain of 17 blocks on node0 
 node0 2020-06-29T14:27:10.214362Z [http] Received a POST request for / from 127.0.0.1:42502 
 node0 2020-06-29T14:27:10.214565Z [httpworker.3] ThreadRPCServer method=generatetoaddress user=__cookie__ 
 node0 2020-06-29T14:27:10.214736Z [httpworker.3] CreateNewBlock(): block weight: 904 txs: 0 fees: 0 sigops 400 

(ie all block generation happens in the 0.1s message handler sleep)

When node0 gets to the logic in SendMessages(), it reverts to sending the first MAX_BLOCKS_IN_TRANSIT_PER_PEER by INV instead of by HEADERS:

bitcoin/src/net_processing.cpp

Lines 3956 to 3971 in 1269cab

//
// Try sending block announcements via headers
//
{
// If we have less than MAX_BLOCKS_TO_ANNOUNCE in our
// list of block hashes we're relaying, and our peer wants
// headers announcements, then find the first header
// not yet known to our peer but would connect, and send.
// If no header would connect, or if we have too many
// blocks, or if the peer doesn't want headers, just
// add all to the inv queue.
LOCK(pto->cs_inventory);
std::vector<CBlock> vHeaders;
bool fRevertToInv = ((!state.fPreferHeaders &&
(!state.fPreferHeaderAndIDs || pto->vBlockHashesToAnnounce.size() > 1)) ||
pto->vBlockHashesToAnnounce.size() > MAX_BLOCKS_TO_ANNOUNCE);

Here it is in the log:

node0 2020-06-29T14:27:10.231122Z [msghand] SendMessages: sending inv peer=0 hash=3dafa6054777a53fd32abcb6f6eefe67591e93c275d23d07696313efed464ebe 
 node0 2020-06-29T14:27:10.231172Z [msghand] sending inv (37 bytes) peer=0 
 node1 2020-06-29T14:27:10.231360Z [msghand] received: inv (37 bytes) peer=0 
 node1 2020-06-29T14:27:10.231392Z [msghand] got inv: block 3dafa6054777a53fd32abcb6f6eefe67591e93c275d23d07696313efed464ebe  new peer=0 
 node1 2020-06-29T14:27:10.231411Z [msghand] sending getheaders (645 bytes) peer=0 

The next time node0 goes through SendMessages(), it sends the final block to announce as a compact block:

 node0 2020-06-29T14:27:10.232052Z [msghand] SendMessages sending header-and-ids 7dd48d289ad0eaaae4d0912686d4af94ebf7de371af02adfa78d2b7538ad9615 to peer=0 
 node0 2020-06-29T14:27:10.232227Z [msghand] sending cmpctblock (264 bytes) peer=0 

which node1 receives:

 node1 2020-06-29T14:27:10.233481Z [msghand] received: cmpctblock (264 bytes) peer=0 

I'd expect that to trigger node1 to send a getheaders for that block:

bitcoin/src/net_processing.cpp

Lines 2956 to 2961 in 1269cab

if (!LookupBlockIndex(cmpctblock.header.hashPrevBlock)) {
// Doesn't connect (or is genesis), instead of DoSing in AcceptBlockHeader, request deeper headers
if (!::ChainstateActive().IsInitialBlockDownload())
connman->PushMessage(&pfrom, msgMaker.Make(NetMsgType::GETHEADERS, ::ChainActive().GetLocator(pindexBestHeader), uint256()));
return;
}

But it doesn't. node1 doesn't do any processing or requesting for the final block, which is why it never syncs to the same tip as node0. I haven't dug into which logic path node1 takes through the CMPCTBLOCK processing.

As the test demonstrates, if node0 then generates and announces another block, then node1 will be able to sync to it.

I'm not sure what to do with this. It seems like a very limited edge-case, so maybe we can just document it and move on. Perhaps @sdaftuar or @TheBlueMatt have an opinion?

pr19397.log

@jonatack

Copy link
Copy Markdown
Member Author

No ACKs. Closing.

@jonatack jonatack closed this Jul 23, 2020
@bitcoin bitcoin locked as resolved and limited conversation to collaborators Feb 15, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants