Skip to content

test: p2p: check that headers message with invalid proof-of-work disconnects peer - #26184

Merged
achow101 merged 1 commit into
bitcoin:masterfrom
theStack:202209-test-check_for_invalid_pow_headers_msg
Feb 14, 2023
Merged

achow101 merged 1 commit into
bitcoin:masterfrom
theStack:202209-test-check_for_invalid_pow_headers_msg

Conversation

@theStack

@theStack theStack commented Sep 26, 2022 •

Copy link
Copy Markdown
Contributor

One of the earliest anti-DoS checks done after receiving and deserializing a headers message from a peer is verifying whether the proof-of-work is valid (called in method PeerManagerImpl::ProcessHeadersMessage):

bitcoin/src/net_processing.cpp

Lines 2752 to 2762 in f227e15

// Before we do any processing, make sure these pass basic sanity checks.
// We'll rely on headers having valid proof-of-work further down, as an
// anti-DoS criteria (note: this check is required before passing any
// headers into HeadersSyncState).
if (!CheckHeadersPoW(headers, m_chainparams.GetConsensus(), peer)) {
// Misbehaving() calls are handled within CheckHeadersPoW(), so we can
// just return. (Note that even if a header is announced via compact
// block, the header itself should be valid, so this type of error can
// always be punished.)
return;
}

The called method PeerManagerImpl::CheckHeadersPoW calls Misbehaving with a score of 100, i.e. leading to an immediate disconnect of the peer:

bitcoin/src/net_processing.cpp

Lines 2368 to 2372 in f227e15

// Do these headers have proof-of-work matching what's claimed?
if (!HasValidProofOfWork(headers, consensusParams)) {
Misbehaving(peer, 100, "header with invalid proof of work");
return false;
}

This PR adds a simple test for both the misbehaving log and the resulting disconnect. For creating a block header with invalid proof-of-work, we first create one that is accepted by the node (the difficulty field nBits is copied from the genesis block) and based on that the nonce is modified until we have block header hash prefix that is too high to fulfill even the minimum difficulty.

@fanquake fanquake added the Tests label Sep 26, 2022
@jarolrod

Copy link
Copy Markdown
Contributor

concept ack

Comment thread test/functional/p2p_invalid_messages.py Resolved
@theStack
theStack force-pushed the 202209-test-check_for_invalid_pow_headers_msg branch from 07342be to 4bf49eb Compare October 6, 2022 13:10
@theStack

theStack commented Oct 6, 2022

Copy link
Copy Markdown
Contributor Author

Force-pushed taking Marco's review (#26184 (comment)) into account: we first create a valid headers message now and check that it is accepted by the node, and only then we only modify its nNonce until the PoW is invalidated.

Comment thread test/functional/p2p_invalid_messages.py Resolved
@DrahtBot

DrahtBot commented Feb 9, 2023 •

Copy link
Copy Markdown
Contributor

The following sections might be updated with supplementary metadata relevant to reviewers and maintainers.

Reviews

See the guideline for information on the review process.

Type Reviewers
ACK brunoerg, Sjors, furszy, achow101

If your review is incorrectly listed, please react with 👎 to this comment and the bot will ignore it on the next update.

@furszy furszy left a comment •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Concept ACK

Seems that this only add coverage for the target < chain_pow_limit rule but not for the other validation checks inside CheckProofOfWork. What if we extend it to cover all of them?

Comment thread test/functional/p2p_invalid_messages.py Outdated
Comment on lines 265 to 270

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: Could also check the connected header hash

assert any(x["hash"] == blockheader.hash and x["status"] == "headers-only" for x in self.nodes[0].getchaintips())

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good idea, done. Decided to access the getchaintips() result at index 0 (works since the result is always returned in decreasing order of height) in order to take use of assert_equal, so if this ever fails, we see the expected/actual values immediately.

@theStack

theStack commented Feb 9, 2023

Copy link
Copy Markdown
Contributor Author

@brunoerg: @furszy: Thanks for your reviews, will include both your nits in a bit.

Seems that this only add coverage for the target < chain_pow_limit rule but not for the other validation checks inside CheckProofOfWork. What if we extend it to cover all of them?

Considering that we have quite detailled CheckProofOfWork unit tests that check all those invalid causes already, I think triggering one of them for the purpose of increasing p2p disconnection coverage should be sufficient.

@furszy

furszy commented Feb 9, 2023

Copy link
Copy Markdown
Member

Considering that we have quite detailled CheckProofOfWork unit tests that check all those invalid causes already, I think triggering one of them for the purpose of increasing p2p disconnection coverage should be sufficient.

Ah, wasn't aware of its existence. Great.
Then yeah, no need to re-implement the same tests. Only verify the disconnection here.
All good.

@theStack
theStack force-pushed the 202209-test-check_for_invalid_pow_headers_msg branch from 4bf49eb to 7726712 Compare February 9, 2023 23:02

@brunoerg brunoerg left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

crACK 7726712

@Sjors

Sjors commented Feb 11, 2023

Copy link
Copy Markdown
Member

ACK 7726712

@furszy furszy left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code review ACK 7726712 with a non-blocking speedup.

To make the test faster, instead of increase the nonce + rehash the block, could just set the target bits to 0 (or set it above the pow limit). The same "header with invalid proof of work" error will be thrown.

e.g.

# Invalidate header
blockheader.nBits = 0
blockheader.rehash()
with self.nodes[0].assert_debug_log(['Misbehaving', 'header with invalid proof of work']):
    peer.send_message(msg_headers([blockheader]))
    peer.wait_for_disconnect()

@achow101

Copy link
Copy Markdown
Member

ACK 7726712

@achow101
achow101 merged commit 576e16e into bitcoin:master Feb 14, 2023
@theStack
theStack deleted the 202209-test-check_for_invalid_pow_headers_msg branch February 15, 2023 07:25
@bitcoin bitcoin locked and limited conversation to collaborators Feb 15, 2024
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.

9 participants