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

Add edge case handling and tests for initConsistentUtreexoState #230

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

1amhesus
Copy link

@1amhesus 1amhesus commented Jan 2, 2025

Why this change is needed

The initConsistentUtreexoState function lacked explicit handling for some edge cases, such as tipHeight < 0 or tipHash == nil. This could lead to unexpected behaviors or errors during state reconstruction.

What was changed

  1. Explicit checks were added to initConsistentUtreexoState for:

    • tipHeight < 0: The function now returns early when this condition is met.
    • tipHash == nil: The function now returns an error for this invalid state.
  2. New test cases were added to validate these edge conditions:

    • Ensures the function behaves correctly when tipHeight < 0.
    • Validates error handling when tipHash is nil.
  3. Wrapper functions were introduced in indexers_test.go to support reusable test chain setups.

…dge condition

This commit improves initConsistentUtreexoState by:
1. Adding test cases for edge condition (e.g., tipHeight < 0, tipHash == nil).
2. Implementing explicit checks for invalid states:
   - Return early if tipHeight < 0.
   - Return an error if tipHash is nil.
3. Adding wrapper functions in indexers_test.go for reusable test chain setups.

These changes improve robustness, edge case handling, and test environment reusability.
@@ -586,6 +586,15 @@ func upgradeUtreexoState(cfg *UtreexoConfig, p *utreexo.MapPollard,
func (us *UtreexoState) initConsistentUtreexoState(chain *blockchain.BlockChain,
savedHash, tipHash *chainhash.Hash, tipHeight int32) error {

// Handle nil tipHash
Copy link
Contributor

Choose a reason for hiding this comment

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

We handle both of the cases literally just below at line 600.

Copy link
Author

Choose a reason for hiding this comment

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

My intention was to handle cases where either tipHash == nil or tipHeight < 0 happens on its own, which the logic at line 600 doesn’t cover.

@kcalvinalvin
Copy link
Contributor

kcalvinalvin commented Jan 2, 2025

This isn't needed since both of the said reasons why this change is needed is handled here

// This is a new accumulator state that we're working with.
var empty chainhash.Hash
if tipHeight == -1 && tipHash.IsEqual(&empty) {
return nil
}

The tests aren't relevant either because we rely on the fact that the height is negative and the savedHash is nil to see if the user is running a fresh node.

Any other reasons? If not, I'll be closing this PR.

Comment on lines +589 to +597
// Handle nil tipHash
if tipHash == nil {
return fmt.Errorf("tipHash is nil, cannot initialize Utreexo state")
}

// Handle negative tipHeight
if tipHeight < 0 {
return nil
}
Copy link
Author

Choose a reason for hiding this comment

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

tipHeight == -1 && tipHash.IsEqual(&empty) handles the initialization state when both tipHeight == -1 and tipHash == nil are true. However, I intended to handle tipHash == nil or tipHeight < 0 individually. While focusing solely on the "fresh node" scenario makes sense, explicitly handling tipHash == nil in other cases could improve stability and robustness.

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.

2 participants