-
Notifications
You must be signed in to change notification settings - Fork 21
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
base: main
Are you sure you want to change the base?
Add edge case handling and tests for initConsistentUtreexoState #230
Conversation
…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 |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
This isn't needed since both of the said reasons why this change is needed is handled here utreexod/blockchain/indexers/utreexobackend.go Lines 589 to 593 in 03ed304
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. |
// Handle nil tipHash | ||
if tipHash == nil { | ||
return fmt.Errorf("tipHash is nil, cannot initialize Utreexo state") | ||
} | ||
|
||
// Handle negative tipHeight | ||
if tipHeight < 0 { | ||
return nil | ||
} |
There was a problem hiding this comment.
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.
Why this change is needed
The
initConsistentUtreexoState
function lacked explicit handling for some edge cases, such astipHeight < 0
ortipHash == nil
. This could lead to unexpected behaviors or errors during state reconstruction.What was changed
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.New test cases were added to validate these edge conditions:
tipHeight < 0
.tipHash
is nil.Wrapper functions were introduced in
indexers_test.go
to support reusable test chain setups.