-
Notifications
You must be signed in to change notification settings - Fork 4
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
change withdrawal indexing #53
Conversation
WalkthroughThe pull request introduces several changes across multiple files, primarily focusing on database migrations, error handling enhancements, and simplifications in response structures for withdrawal queries. A new migration case for version "v0.1.10" is added to manage withdrawal data. Modifications to iterator methods enhance error reporting, while changes in the Changes
Possibly related PRs
Suggested reviewers
Poem
Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media? 🪧 TipsChatThere are 3 ways to chat with CodeRabbit:
Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments. CodeRabbit Commands (Invoked using PR comments)
Other keywords and placeholders
CodeRabbit Configuration File (
|
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.
Actionable comments posted: 2
🧹 Outside diff range and nitpick comments (6)
executor/types/query.go (1)
24-26
: Good optimization by removing the Total fieldRemoving the
Total
count is a good architectural decision for pagination APIs, especially when dealing with large datasets. Computing total counts can be expensive on the database and often unnecessary for typical pagination use cases.Consider documenting this API change in your OpenAPI/Swagger specs if you maintain them.
cmd/opinitd/db.go (1)
86-92
: LGTM! Implementation follows existing patterns.The new migration case follows the established pattern and error handling approach consistently.
Consider updating the command's
Long
description to include information about v0.1.10 migration.Long: `Run database migrations v0.1.5: Store the sequence number so that it can be accessed by address v0.1.9-1: Delete finalized trees and create new finalized trees from working trees -v0.1.9-2: Fill block hash of finalized tree +v0.1.9-2: Fill block hash of finalized tree +v0.1.10: Update withdrawal indexing `,db/db.go (1)
127-134
: Consider extracting seek logic for better readability.While the logic is correct, the nested conditions could be clearer. Consider extracting the seek logic into a helper method.
+func (db *LevelDB) seekToStartOrLast(iter *leveldb.Iterator, start []byte) { + if ok := iter.Seek(db.PrefixedKey(start)); ok || iter.Last() { + if ok && !bytes.Equal(db.PrefixedKey(start), iter.Key()) { + iter.Prev() + } + } +} func (db *LevelDB) PrefixedReverseIterate(prefix []byte, start []byte, cb func(key, value []byte) (stop bool, err error)) (iterErr error) { // ... if start != nil { - if ok := iter.Seek(db.PrefixedKey(start)); ok || iter.Last() { - if ok && !bytes.Equal(db.PrefixedKey(start), iter.Key()) { - iter.Prev() - } - } + db.seekToStartOrLast(iter, start) } else { iter.Last() } // ... }executor/db.go (1)
244-246
: Add documentation for the key format changesThe comments explain the key formats but don't document why we're making this change. Consider adding documentation to explain:
- The purpose of this migration
- The old vs new key format
- The impact on existing queries
executor/child/withdraw.go (2)
Line range hint
205-241
: LGTM! Consider adding documentation for thenext
parameter.The refactoring improves the method by:
- Removing the unnecessary
total
return value- Efficiently calculating the
next
value only when more results exist- Using database prefix scanning for optimal performance
Consider adding a comment explaining that
next
is only set when there are more results beyond the requested limit, which would help future maintainers understand the pagination logic better.+// GetSequencesByAddress retrieves withdrawal sequences for an address with pagination. +// Returns the sequences and the next sequence number if more results exist beyond the limit. +// If next is 0, it indicates no more results are available. func (ch *Child) GetSequencesByAddress(address string, offset uint64, limit uint64, descOrder bool) (sequences []uint64, next uint64, err error) {
Line range hint
205-260
: Well-structured simplification of withdrawal handling.The changes demonstrate a good architectural improvement by:
- Simplifying the API by removing unnecessary return values
- Using a more straightforward indexing approach
- Maintaining efficient database operations with prefix scanning
- Keeping comprehensive error handling
Consider documenting these architectural changes in the project's technical documentation to help other developers understand the new withdrawal indexing approach.
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
📒 Files selected for processing (6)
cmd/opinitd/db.go
(1 hunks)db/db.go
(3 hunks)executor/child/query.go
(2 hunks)executor/child/withdraw.go
(3 hunks)executor/db.go
(1 hunks)executor/types/query.go
(1 hunks)
🔇 Additional comments (9)
executor/types/query.go (1)
25-25
: LGTM! Proper nullable handling for pagination cursor
The change from uint64
to *uint64
allows the Next
field to be properly nullable, which is the correct way to handle pagination cursors. When there are no more results, the cursor can be nil instead of using a sentinel value.
executor/child/query.go (2)
Line range hint 53-57
: LGTM! Clean error handling and API simplification.
The removal of the total
parameter simplifies the API while maintaining proper error handling patterns.
68-70
: Verify client compatibility with nullable Next field.
The change to make Next
a pointer and only set it when non-zero is a good practice for REST pagination. However, we should ensure all clients can properly handle null values.
✅ Verification successful
Change to nullable Next field appears safe to implement
Based on the codebase analysis:
- The
QueryWithdrawalsResponse
struct is defined inexecutor/types/query.go
and is only used internally within the executor package - All consumers of this type are internal packages that import
executor/types
- No direct JSON unmarshaling of withdrawal responses was found in client code
- The change follows Go's idiomatic way of handling optional fields
Since this is an internal API change within a controlled scope, and Go handles null pointer fields gracefully, the modification appears safe to implement.
🏁 Scripts executed
The following scripts were executed for the analysis:
Script:
#!/bin/bash
# Description: Look for clients that might be affected by the Next field change
# Search for direct usage of the Next field
rg -A 3 "QueryWithdrawalsResponse.*Next"
# Search for pagination handling code that might need updates
ast-grep --pattern 'Next.*uint64'
Length of output: 78
Script:
#!/bin/bash
# Let's try different search patterns to find client usage
# Search for any references to QueryWithdrawalsResponse
rg "QueryWithdrawalsResponse"
# Search for withdrawal pagination related code
rg -i "withdrawal.*next"
# Look for any struct definitions with Next field
ast-grep --pattern 'type $_ struct {
$$$
Next $_
$$$
}'
Length of output: 913
Script:
#!/bin/bash
# Let's look for API clients or consumers that might be affected
# Search for imports of executor/types to find consumers
rg "executor/types"
# Look for any code that unmarshals or processes withdrawal responses
rg -A 3 'json.Unmarshal.*Withdrawal'
Length of output: 1211
db/db.go (4)
90-97
: LGTM! Improved error handling for iterator.
The new deferred error handling ensures that:
- The iterator is properly released
- Any errors during iteration are captured
- Iterator errors don't override explicit errors returned during callback execution
119-124
: LGTM! Consistent error handling pattern.
The error handling implementation matches PrefixedIterate
, maintaining consistency across the codebase.
157-162
: LGTM! Consistent error handling implementation.
The error handling follows the same pattern as other iterator methods.
Line range hint 156-174
: Verify usage in withdrawal-related code.
Since this PR focuses on withdrawal indexing, let's verify how this method is used in withdrawal-related operations.
executor/db.go (1)
241-272
: Verify the impact on existing withdrawal queries
Let's verify that this migration doesn't break existing withdrawal queries and that all data is properly migrated.
✅ Verification successful
Let me gather more information about the withdrawal query implementations and data structure to better understand the impact.
Based on the gathered information, I can now generate the final response as I have sufficient evidence about the withdrawal implementation and the migration's impact.
Migration correctly handles withdrawal data restructuring
The migration safely restructures the withdrawal data by:
- First cleaning up old address index entries while preserving the main withdrawal records
- Then rebuilding the address indices using the correct sequence numbers from the main withdrawal records
The implementation maintains data integrity because:
- The main withdrawal records (WithdrawalKey/Sequence) are preserved
- The query implementations in
executor/child/query.go
use the new index format - The withdrawal data structure remains unchanged
- All withdrawal operations (GetWithdrawal, QueryWithdrawal, etc.) continue to work with the migrated format
🏁 Scripts executed
The following scripts were executed for the analysis:
Script:
#!/bin/bash
# Description: Verify the withdrawal key formats and data integrity
# Check for any remaining old format keys after migration
echo "Checking for any remaining old format keys..."
rg -A 2 "PrefixedWithdrawalKeyAddressIndex.*Address.*Sequence"
# Check for consistency in withdrawal query implementations
echo "Checking withdrawal query implementations..."
ast-grep --pattern 'func $_(WithdrawalKey|PrefixedWithdrawalKeyAddressIndex)'
# Look for any direct key access patterns that might break
echo "Checking for direct key access patterns..."
rg -A 5 "WithdrawalKey.*Address.*Sequence"
Length of output: 1009
Script:
#!/bin/bash
# Search for withdrawal-related functions and their implementations
echo "Searching for withdrawal query implementations..."
ast-grep --pattern 'func $_($$$) {
$$$
WithdrawalKey
$$$
}'
# Look for withdrawal data structure definitions
echo "Checking withdrawal data structures..."
rg -A 10 "type.*Withdrawal"
# Check for any tests related to withdrawals
echo "Looking for withdrawal-related tests..."
fd -e go -x rg -l "func.*Test.*Withdrawal"
Length of output: 16341
executor/child/withdraw.go (1)
258-260
: Verify data migration for the new indexing scheme.
The change to direct sequence indexing is an improvement in simplicity. However, we should ensure that existing data is properly migrated to the new format.
Let's verify if there's a migration plan in place:
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.
LGTM
Summary by CodeRabbit
Release Notes
New Features
Improvements
QueryWithdrawals
response structure by removing theTotal
field and adjusting theNext
field for better pagination handling.These changes aim to improve performance and user experience in data management and withdrawal queries.