Add edge case handling and tests for initConsistentUtreexoState#230
Add edge case handling and tests for initConsistentUtreexoState#2301amhesus wants to merge 1 commit into
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.
| func (us *UtreexoState) initConsistentUtreexoState(chain *blockchain.BlockChain, | ||
| savedHash, tipHash *chainhash.Hash, tipHeight int32) error { | ||
|
|
||
| // Handle nil tipHash |
There was a problem hiding this comment.
We handle both of the cases literally just below at line 600.
There was a problem hiding this comment.
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.
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
initConsistentUtreexoStatefunction lacked explicit handling for some edge cases, such astipHeight < 0ortipHash == nil. This could lead to unexpected behaviors or errors during state reconstruction.What was changed
Explicit checks were added to
initConsistentUtreexoStatefor: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.tipHashis nil.Wrapper functions were introduced in
indexers_test.goto support reusable test chain setups.