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, fullblocktests, workmath, testhelper: add InvalidateBlock() method to BlockChain #2155

Merged
merged 10 commits into from
May 22, 2024

Conversation

kcalvinalvin
Copy link
Collaborator

Depends on #2153

All of the code except for the last two commits are for refactoring code. The vast majority is just moving code from fullblocktests to testhelper so that the testing code for InvalidateBlock() can call the functions instead of having to copy over and duplicate functions.

The InvalidateBlock() method is operational and is tested but is not revealed through rpc in this PR.

@coveralls
Copy link

coveralls commented Apr 2, 2024

Pull Request Test Coverage Report for Build 8891501246

Details

  • 292 of 315 (92.7%) changed or added relevant lines in 5 files are covered.
  • 10 unchanged lines in 2 files lost coverage.
  • Overall coverage increased (+0.1%) to 56.978%

Changes Missing Coverage Covered Lines Changed/Added Lines %
blockchain/fullblocktests/generate.go 41 42 97.62%
blockchain/internal/testhelper/common.go 105 113 92.92%
blockchain/chain.go 76 90 84.44%
Files with Coverage Reduction New Missed Lines %
connmgr/connmanager.go 1 86.27%
peer/peer.go 9 73.72%
Totals Coverage Status
Change from base Build 8881487645: 0.1%
Covered Lines: 29567
Relevant Lines: 51892

💛 - Coveralls

blockchain/testhelper/README.md Outdated Show resolved Hide resolved
blockchain/common_test.go Outdated Show resolved Hide resolved
blockchain/chain.go Outdated Show resolved Hide resolved
blockchain/chain.go Show resolved Hide resolved
blockchain/chain_test.go Outdated Show resolved Hide resolved
blockchain/chain_test.go Outdated Show resolved Hide resolved
blockchain/chain_test.go Outdated Show resolved Hide resolved
blockchain/chain_test.go Outdated Show resolved Hide resolved
blockchain/chain_test.go Outdated Show resolved Hide resolved
blockchain/chain_test.go Show resolved Hide resolved
Copy link
Collaborator

@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.

looks good, think the tests could be cleaned up a little and have more comments

blockchain/chain_test.go Outdated Show resolved Hide resolved
}{
{
name: "One branch, invalidate once",
chainGen: func() (*BlockChain, []*chainhash.Hash, func()) {
Copy link
Collaborator

Choose a reason for hiding this comment

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

think this should be functions below rather than explicitly in full form here

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Could you clarify what you mean here?

Copy link
Collaborator

Choose a reason for hiding this comment

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

I just mean moving the chainGen function into a function outside of the test so you just call chainGen:funcHere for readability. Just my preference, but it's non blocking

@Roasbeef
Copy link
Member

Dependent PR merged, needs a rebase!

@kcalvinalvin kcalvinalvin force-pushed the 2024-04-02-invalidate-block branch 2 times, most recently from 6b3238e to 7e9b796 Compare April 22, 2024 17:19
Some of the functions in difficulty.go are not dependent on any external
functions and they are needed to introduce testing code for the
invalidateblock and reconsiderblock methods that are to be added on in
later commits. Having the workmath package let's us reuse the code and
avoid dependency cycles.

The existing functions that were exported already (HashToBig,
CompactToBig, BigToCompact, CalcWork) are still kept in difficulty.go
to avoid breaking external code that depends on those exported
functions.
spendableOut and the functions related to it are is moved to package
testhelper and are exported.  This is done to make the code reusable
without introducing an import cycle when the testing code for
invalidateblock and reconsiderblock are added in follow up commits.
solveBlock is moved to testhelper and is exported.  This is done so that
the code can be reused without introducing import cycles.  The testing
code to be added in alter commits for invalidateblock and reconsider
block will use SolveBlock.
The variables are moved to testhelper so that they can be reused in the
blockchain package without introducing an import cycle.  The testing
code for invalidateblock and reconsiderblock that will be added in later
commits will be using these variables.
uniqueOpReturnScript is moved to testhelper and is exported so that the
code and be reused in package blockchain without introducing import
cycles.  The test code for invalidateblock and reconsiderblock that are
gonna be added in later commits uses the functions.
standardCoinbaseScript is moved to testhelper and is exported.  This
allows test code in package blockchain to reuse the code without
introducing an import cycle.  This code is used in the testing code
for invalidateblock and reconsiderblock that's added in the later
commits.
createCoinbaseTx's code is refactored out and placed in testhelper
package and is exported so that callers in package blockchain can reuse
the code without introducing import cycles.  The test code for
invalidateblock and reconsiderblock that'll be added in later commits
make use of this code.
createSpendTx is moved to testhelper so that the function can be used
for callers in package blockchain without introducing import cycles.
The test code for invalidateblock and reconsiderblock that are going to
be added in later commits make use of this code.
The block generating functions here allow for a test to create mock
blocks.  This is useful for testing invalidateblock and reconsiderblock
methods on blockchain that will be added in later commits.
Copy link
Collaborator

@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.

changes look good to me, only thing is whether reconsiderblock should be implemented instead

blockchain/chain_test.go Outdated Show resolved Hide resolved
InvalidateBlock() invalidates a given block and marks all its
descendents as invalid as well. The active chain tip changes if the
invalidated block is part of the best chain.
Copy link
Collaborator

@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.

LGTM 🏑

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 c467725 into btcsuite:master May 22, 2024
3 checks passed
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