fix(rpc): report syncing status when starting block header is pruned#3797
fix(rpc): report syncing status when starting block header is pruned#3797Ehsan-saradar wants to merge 2 commits into
Conversation
Syncing() read the starting block's header only to obtain its hash. With history pruning enabled that header can be pruned once the starting block falls outside the retained window, so the read failed and the handler fell back to "not syncing" while the node was still syncing. Decide the sync status from head vs highest block first, then read the starting block hash best-effort. Applies to rpc v8, v9 and v10. Closes NethermindEth#3791
There was a problem hiding this comment.
Pull request overview
This PR fixes incorrect starknet_syncing reporting in history-pruning mode by ensuring the syncing decision is made without depending on the starting block header (which may be pruned), and by fetching starting_block_hash only on a best-effort basis afterward.
Changes:
- Reorders
Syncing()logic in rpc v8/v9/v10 to decide syncing fromheadvshighestfirst. - Makes
starting_block_hashoptional (best-effort DB lookup) while always returningstarting_block_numfromStartingBlockNumber(). - Updates and extends tests to reflect the new call order and pruned-header behavior.
Reviewed changes
Copilot reviewed 6 out of 6 changed files in this pull request and generated no comments.
Show a summary per file
| File | Description |
|---|---|
| rpc/v8/sync.go | Makes starting header lookup best-effort so pruning doesn’t cause false “not syncing”. |
| rpc/v8/sync_test.go | Reworks expectations for new call order; adds pruned-header syncing case. |
| rpc/v9/sync.go | Same best-effort starting header lookup and corrected sync decision order. |
| rpc/v9/sync_test.go | Updates expectations; adds pruned-header syncing case. |
| rpc/v10/sync.go | Same logic update as v8/v9. |
| rpc/v10/sync_test.go | Updates expectations; adds pruned-header syncing case. |
Comments suppressed due to low confidence (3)
rpc/v9/sync_test.go:55
- This subtest is named as if it exercises the
highestBlockHeader.Number <= head.Numberbranch, butHighestBlockHeader()is currently mocked to return nil (via the.Times(2)expectation above), so the handler returns early athighestBlockHeader == nil. This means the head>highest condition isn’t covered and the test name is misleading. Mock a non-nil highest header with a lower number (and adjust the nil expectation count) so the intended branch is actually tested.
t.Run("block height is greater than highest block", func(t *testing.T) {
mockReader.EXPECT().HeadsHeader().Return(&core.Header{Number: 1}, nil)
syncing, err := handler.Syncing()
assert.Nil(t, err)
assert.Equal(t, &rpc.Sync{Syncing: &defaultSyncState}, syncing)
})
rpc/v8/sync_test.go:55
- This subtest is named as if it exercises the
highestBlockHeader.Number <= head.Numberbranch, butHighestBlockHeader()is currently mocked to return nil (via the.Times(2)expectation above), so the handler returns early athighestBlockHeader == nil. This means the head>highest condition isn’t covered and the test name is misleading. Mock a non-nil highest header with a lower number (and adjust the nil expectation count) so the intended branch is actually tested.
t.Run("block height is greater than highest block", func(t *testing.T) {
mockReader.EXPECT().HeadsHeader().Return(&core.Header{Number: 1}, nil)
syncing, err := handler.Syncing()
assert.Nil(t, err)
assert.Equal(t, &rpc.Sync{Syncing: &defaultSyncState}, syncing)
})
rpc/v10/sync_test.go:55
- This subtest is named as if it exercises the
highestBlockHeader.Number <= head.Numberbranch, butHighestBlockHeader()is currently mocked to return nil (via the.Times(2)expectation above), so the handler returns early athighestBlockHeader == nil. This means the head>highest condition isn’t covered and the test name is misleading. Mock a non-nil highest header with a lower number (and adjust the nil expectation count) so the intended branch is actually tested.
t.Run("block height is greater than highest block", func(t *testing.T) {
mockReader.EXPECT().HeadsHeader().Return(&core.Header{Number: 1}, nil)
syncing, err := handler.Syncing()
assert.Nil(t, err)
assert.Equal(t, &rpc.Sync{Syncing: &defaultSyncState}, syncing)
})
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #3797 +/- ##
==========================================
+ Coverage 74.91% 75.38% +0.46%
==========================================
Files 433 438 +5
Lines 38815 39489 +674
==========================================
+ Hits 29079 29769 +690
+ Misses 7733 7650 -83
- Partials 2003 2070 +67 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
EgeCaner
left a comment
There was a problem hiding this comment.
We cannot let StartingBlockHash to be nil since field is required by the spec. Lets either return zero as default, or somehow cache the starting block hash to read from memory later
| // The starting block's header may have been pruned in history-pruning mode. | ||
| // Its hash is only metadata, so read it best-effort instead of reporting "not syncing". | ||
| if startingBlockHeader, err := h.bcReader.BlockHeaderByNumber(startingBlockNumber); err == nil { | ||
| sync.StartingBlockHash = startingBlockHeader.Hash |
There was a problem hiding this comment.
StartingBlockHash is a required field of starknet_syncing response, see the spec ref.
When we leave empty (nil), field will be omitted. I guess we should either return zero when best effort fails, or we somehow cache the starting block hash then directly read the value from memory. Second approach sounds better to me since it will keep API functioning as is, but not sure if it would be easy to implement
The spec marks starting_block_hash as required, so omitting it is not an option. Fall back to the zero felt when the starting block header has been pruned instead of dropping the field.
|
Good catch on the spec, you're right that About caching: we can't cache the hash when sync starts, because the starting block is |
|
@EgeCaner following up on the caching idea — sketched out what it would take, in case you think it's worth doing:
It fully covers the pruned case (in a long-running session the starting block always passes through |
Summary
Fixes #3791.
In pruned mode,
Handler.Syncing(rpcv8/v9/v10) could returnfalse("not syncing") while the node was in fact still syncing.It read the starting block's header only to obtain that block's hash:
With history pruning enabled, once the starting block falls outside the retained window its header no longer exists in the DB. The lookup errors, and the handler falls through to the default "not syncing" response — so a node that is actively syncing reports itself as fully synced.
Fix
The sync decision only depends on
headvshighestblock, so make that decision first. The starting block hash is purely informational metadata (starting_block_hash), so it is now read best-effort afterwards:starting_block_hashis set as before;starting_block_numnow comes fromStartingBlockNumber()(already in memory) rather than the header, so it no longer depends on a DB read that pruning can invalidate.Applied identically to
rpc/v8,rpc/v9andrpc/v10.Testing
TestSyncingin all three versions to match the new call order.syncing with pruned starting block headercase asserting the node still reports syncing whenBlockHeaderByNumberfails.make lintand therpc/v8,rpc/v9,rpc/v10test suites pass locally.