Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

blockchain: refactor and export header validation checks #1931

Merged
merged 2 commits into from
Jun 29, 2023

Conversation

Crypt-iQ
Copy link
Collaborator

This PR allows an external library like neutrino to be able to perform contextual and context-free header validation. This is achieved by introducing two new interfaces HeaderCtx (which replaces *blockNode in the relevant functions) and ChainCtx (which replaces *BlockChain in the relevant functions). A library calling into these functions needs to only satisfy the interface to be able to validate any received headers.

@coveralls
Copy link

coveralls commented Nov 29, 2022

Pull Request Test Coverage Report for Build 5415613990

  • 73 of 130 (56.15%) changed or added relevant lines in 6 files are covered.
  • 3 unchanged lines in 1 file lost coverage.
  • Overall coverage increased (+0.02%) to 55.235%

Changes Missing Coverage Covered Lines Changed/Added Lines %
blockchain/chainio.go 0 1 0.0%
blockchain/thresholdstate.go 0 1 0.0%
blockchain/blockindex.go 20 29 68.97%
blockchain/validate.go 43 64 67.19%
blockchain/difficulty.go 6 31 19.35%
Files with Coverage Reduction New Missed Lines %
blockchain/difficulty.go 3 48.78%
Totals Coverage Status
Change from base Build 5379381875: 0.02%
Covered Lines: 26688
Relevant Lines: 48317

💛 - Coveralls

Copy link
Collaborator

@guggero guggero left a comment

Choose a reason for hiding this comment

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

Looks good! Only blocking comments are about the nil checks vs. interfaces (need to also read up on the specifics, when a normal nil check works and when not).

blockchain/blockindex.go Show resolved Hide resolved
blockchain/chain.go Outdated Show resolved Hide resolved
blockchain/difficulty.go Show resolved Hide resolved
blockchain/difficulty.go Show resolved Hide resolved
blockchain/validate.go Show resolved Hide resolved
blockchain/validate.go Outdated Show resolved Hide resolved
blockchain/validate.go Outdated Show resolved Hide resolved
@Crypt-iQ
Copy link
Collaborator Author

Crypt-iQ commented Dec 1, 2022

Looks good! Only blocking comments are about the nil checks vs. interfaces (need to also read up on the specifics, when a normal nil check works and when not).

You can nil-check an interface, but it's a bit weird.

See here: https://go.dev/play/p/9NVSUoFo2oI
If foo() returns innerFoo() which returns nil, then the nil is converted to a MyIface with a nil value. This will fail the nil-check.

If foo() just calls return nil, then this will succeed the nil-check. I'll double-check the call-sites you mentioned just to be sure though

@guggero
Copy link
Collaborator

guggero commented Dec 1, 2022

Looks good! Only blocking comments are about the nil checks vs. interfaces (need to also read up on the specifics, when a normal nil check works and when not).

You can nil-check an interface, but it's a bit weird.

See here: https://go.dev/play/p/9NVSUoFo2oI If foo() returns innerFoo() which returns nil, then the nil is converted to a MyIface with a nil value. This will fail the nil-check.

If foo() just calls return nil, then this will succeed the nil-check. I'll double-check the call-sites you mentioned just to be sure though

Aaah, I see. Thanks for the example. I guess that makes sense. That's why you had that explicit if x == nil { return nil in there, gotcha.

@Crypt-iQ
Copy link
Collaborator Author

Crypt-iQ commented Dec 1, 2022

I do wonder if there's not a way to get around nil-checking and to instead use some dummy value to compare against

Copy link
Collaborator Author

@Crypt-iQ Crypt-iQ left a comment

Choose a reason for hiding this comment

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

Left some comments, this pattern is kind of worrying since I feel like I've seen it before in our codebases, but can't exactly grep for it

blockchain/blockindex.go Show resolved Hide resolved
blockchain/difficulty.go Show resolved Hide resolved
blockchain/validate.go Show resolved Hide resolved
blockchain/blockindex.go Show resolved Hide resolved
blockchain/validate.go Outdated Show resolved Hide resolved
blockchain/validate.go Outdated Show resolved Hide resolved
blockchain/blockindex.go Show resolved Hide resolved
blockchain/difficulty.go Show resolved Hide resolved
blockchain/validate.go Outdated Show resolved Hide resolved
@Crypt-iQ Crypt-iQ force-pushed the export_header_funcs branch 3 times, most recently from 7abd38f to 7abdd05 Compare June 29, 2023 11:53
Copy link
Member

@Roasbeef Roasbeef left a comment

Choose a reason for hiding this comment

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

LGTM 🌪️

@Crypt-iQ Crypt-iQ force-pushed the export_header_funcs branch from 7abdd05 to cd583a4 Compare June 29, 2023 18:39
Copy link
Collaborator

@guggero guggero left a comment

Choose a reason for hiding this comment

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

LGTM 🎉

blockchain/validate.go Outdated Show resolved Hide resolved
@Crypt-iQ
Copy link
Collaborator Author

Changed FindPreviousCheckpoint to check and return an error from findPreviousCheckpoint

Crypt-iQ added 2 commits June 29, 2023 14:45
This change will allow an external program to provide its own HeaderCtx
and ChainCtx and be able to perform contextual block header checks.
@Crypt-iQ Crypt-iQ force-pushed the export_header_funcs branch from cd583a4 to ee6c0e1 Compare June 29, 2023 18:45
Copy link
Member

@Roasbeef Roasbeef left a comment

Choose a reason for hiding this comment

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

LGTM 🧱

@Roasbeef Roasbeef merged commit f5eeb10 into btcsuite:master Jun 29, 2023
@Crypt-iQ Crypt-iQ deleted the export_header_funcs branch July 14, 2023 16:04
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants