Skip to content

Commit 05124cc

Browse files
authored
refactor(apps): rollback cmd updates (#2744)
<!-- Please read and fill out this form before submitting your PR. Please make sure you have reviewed our contributors guide before submitting your first PR. NOTE: PR titles should follow semantic commits: https://www.conventionalcommits.org/en/v1.0.0/ --> ## Overview With the fix from #2736, the `--skip-p2p-stores` flag isn't needed. Additionally, this bump go-header to the latest changes from celestiaorg/go-header#347 <!-- Please provide an explanation of the PR, including the appropriate context, background, goal, and rationale. If there is an issue with this information, please provide a tl;dr and link the issue. Ex: Closes #<issue number> -->
1 parent 2c85e06 commit 05124cc

File tree

8 files changed

+38
-64
lines changed

8 files changed

+38
-64
lines changed

apps/evm/single/cmd/rollback.go

Lines changed: 12 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package cmd
22

33
import (
44
"context"
5+
"errors"
56
"fmt"
67

78
ds "github.com/ipfs/go-datastore"
@@ -18,9 +19,8 @@ import (
1819
// NewRollbackCmd creates a command to rollback ev-node state by one height.
1920
func NewRollbackCmd() *cobra.Command {
2021
var (
21-
height uint64
22-
skipP2PStores bool
23-
syncNode bool
22+
height uint64
23+
syncNode bool
2424
)
2525

2626
cmd := &cobra.Command{
@@ -69,10 +69,6 @@ func NewRollbackCmd() *cobra.Command {
6969
return fmt.Errorf("failed to rollback ev-node state: %w", err)
7070
}
7171

72-
if skipP2PStores {
73-
return printSuccess(height)
74-
}
75-
7672
// rollback ev-node goheader state
7773
headerStore, err := goheaderstore.NewStore[*types.SignedHeader](
7874
evolveDB,
@@ -102,27 +98,26 @@ func NewRollbackCmd() *cobra.Command {
10298
}
10399
defer dataStore.Stop(goCtx)
104100

101+
var errs error
105102
if err := headerStore.DeleteRange(goCtx, height+1, headerStore.Height()); err != nil {
106-
return fmt.Errorf("failed to rollback header sync service state: %w", err)
103+
errs = errors.Join(errs, fmt.Errorf("failed to rollback header sync service state: %w", err))
107104
}
108105

109106
if err := dataStore.DeleteRange(goCtx, height+1, dataStore.Height()); err != nil {
110-
return fmt.Errorf("failed to rollback data sync service state: %w", err)
107+
errs = errors.Join(errs, fmt.Errorf("failed to rollback data sync service state: %w", err))
108+
}
109+
110+
fmt.Printf("Rolled back ev-node state to height %d\n", height)
111+
if syncNode {
112+
fmt.Println("Restart the node with the `--clear-cache` flag")
111113
}
112114

113-
return printSuccess(height)
115+
return errs
114116
},
115117
}
116118

117119
cmd.Flags().Uint64Var(&height, "height", 0, "rollback to a specific height")
118120
cmd.Flags().BoolVar(&syncNode, "sync-node", false, "sync node (no aggregator)")
119-
cmd.Flags().BoolVar(&skipP2PStores, "skip-p2p-stores", false, "skip rollback p2p stores (goheaderstore)")
120121

121122
return cmd
122123
}
123-
124-
func printSuccess(height uint64) error {
125-
fmt.Printf("Rolled back ev-node state to height %d\n", height)
126-
fmt.Println("Restart the node with the `--clear-cache` flag")
127-
return nil
128-
}

apps/evm/single/go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ module github.com/evstack/ev-node/apps/evm/single
22

33
go 1.24.6
44

5-
replace github.com/celestiaorg/go-header => github.com/julienrbrt/go-header v0.0.0-20250909151551-cb11b091bf58 // TODO: to remove after https://github.com/celestiaorg/go-header/pull/347
5+
replace github.com/celestiaorg/go-header => github.com/julienrbrt/go-header v0.0.0-20251008134330-747c8c192fa8 // TODO: to remove after https://github.com/celestiaorg/go-header/pull/347
66

77
replace (
88
github.com/evstack/ev-node => ../../../

apps/evm/single/go.sum

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -242,8 +242,8 @@ github.com/json-iterator/go v1.1.6/go.mod h1:+SdeFBvtyEkXs7REEP0seUULqWtbJapLOCV
242242
github.com/jstemmer/go-junit-report v0.0.0-20190106144839-af01ea7f8024/go.mod h1:6v2b51hI/fHJwM22ozAgKL4VKDeJcHhJFhtBdhmNjmU=
243243
github.com/jtolds/gls v4.20.0+incompatible h1:xdiiI2gbIgH/gLH7ADydsJ1uDOEzR8yvV7C0MuV77Wo=
244244
github.com/jtolds/gls v4.20.0+incompatible/go.mod h1:QJZ7F/aHp+rZTRtaJ1ow/lLfFfVYBRgL+9YlvaHOwJU=
245-
github.com/julienrbrt/go-header v0.0.0-20250909151551-cb11b091bf58 h1:FBGLvgA4hfRjjYHUiiya7KkGxcHQWSpdn2dRKUEDa5k=
246-
github.com/julienrbrt/go-header v0.0.0-20250909151551-cb11b091bf58/go.mod h1:eX9iTSPthVEAlEDLux40ZT/olXPGhpxHd+mEzJeDhd0=
245+
github.com/julienrbrt/go-header v0.0.0-20251008134330-747c8c192fa8 h1:F+gOiipBxG43s+Ho+ri9T8IwumjWjp1XUon4DLWjxfQ=
246+
github.com/julienrbrt/go-header v0.0.0-20251008134330-747c8c192fa8/go.mod h1:eX9iTSPthVEAlEDLux40ZT/olXPGhpxHd+mEzJeDhd0=
247247
github.com/kisielk/errcheck v1.5.0/go.mod h1:pFxgyoBC7bSaBwPgfKdkLd5X25qrDl4LWUI2bnpBCr8=
248248
github.com/kisielk/gotool v1.0.0/go.mod h1:XhKaO+MFFWcvkIS/tQcRk01m1F5IRFswLeQ+oQHNcck=
249249
github.com/klauspost/compress v1.18.0 h1:c/Cqfb0r+Yi+JtIEq73FWXVkRonBlf0CRNYc8Zttxdo=

apps/testapp/cmd/rollback.go

Lines changed: 13 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package cmd
22

33
import (
44
"context"
5+
"errors"
56
"fmt"
67

78
kvexecutor "github.com/evstack/ev-node/apps/testapp/kv"
@@ -19,9 +20,8 @@ import (
1920
// NewRollbackCmd creates a command to rollback ev-node state by one height.
2021
func NewRollbackCmd() *cobra.Command {
2122
var (
22-
height uint64
23-
skipP2PStores bool
24-
syncNode bool
23+
height uint64
24+
syncNode bool
2525
)
2626

2727
cmd := &cobra.Command{
@@ -75,10 +75,6 @@ func NewRollbackCmd() *cobra.Command {
7575
return fmt.Errorf("failed to rollback ev-node state: %w", err)
7676
}
7777

78-
if skipP2PStores {
79-
return printSuccess(height)
80-
}
81-
8278
// rollback ev-node goheader state
8379
headerStore, err := goheaderstore.NewStore[*types.SignedHeader](
8480
evolveDB,
@@ -108,31 +104,30 @@ func NewRollbackCmd() *cobra.Command {
108104
}
109105
defer dataStore.Stop(goCtx)
110106

107+
var errs error
111108
if err := headerStore.DeleteRange(goCtx, height+1, headerStore.Height()); err != nil {
112-
return fmt.Errorf("failed to rollback header sync service state: %w", err)
109+
errs = errors.Join(errs, fmt.Errorf("failed to rollback header sync service state: %w", err))
113110
}
114111

115112
if err := dataStore.DeleteRange(goCtx, height+1, dataStore.Height()); err != nil {
116-
return fmt.Errorf("failed to rollback data sync service state: %w", err)
113+
errs = errors.Join(errs, fmt.Errorf("failed to rollback data sync service state: %w", err))
117114
}
118115

119116
// rollback execution store
120117
if err := executor.Rollback(goCtx, height); err != nil {
121-
return fmt.Errorf("rollback failed: %w", err)
118+
errs = errors.Join(errs, fmt.Errorf("rollback failed: %w", err))
119+
}
120+
121+
fmt.Printf("Rolled back ev-node state to height %d\n", height)
122+
if syncNode {
123+
fmt.Println("Restart the node with the `--clear-cache` flag")
122124
}
123125

124-
return printSuccess(height)
126+
return errs
125127
},
126128
}
127129

128130
cmd.Flags().Uint64Var(&height, "height", 0, "rollback to a specific height")
129131
cmd.Flags().BoolVar(&syncNode, "sync-node", false, "sync node (no aggregator)")
130-
cmd.Flags().BoolVar(&skipP2PStores, "skip-p2p-stores", false, "skip rollback p2p stores (goheaderstore)")
131132
return cmd
132133
}
133-
134-
func printSuccess(height uint64) error {
135-
fmt.Printf("Rolled back ev-node state to height %d\n", height)
136-
fmt.Println("Restart the node with the `--clear-cache` flag")
137-
return nil
138-
}

apps/testapp/go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ module github.com/evstack/ev-node/apps/testapp
22

33
go 1.24.6
44

5-
replace github.com/celestiaorg/go-header => github.com/julienrbrt/go-header v0.0.0-20250909151551-cb11b091bf58 // TODO: to remove after https://github.com/celestiaorg/go-header/pull/347
5+
replace github.com/celestiaorg/go-header => github.com/julienrbrt/go-header v0.0.0-20251008134330-747c8c192fa8 // TODO: to remove after https://github.com/celestiaorg/go-header/pull/347
66

77
replace (
88
github.com/evstack/ev-node => ../../.

apps/testapp/go.sum

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -176,8 +176,8 @@ github.com/json-iterator/go v1.1.6/go.mod h1:+SdeFBvtyEkXs7REEP0seUULqWtbJapLOCV
176176
github.com/jstemmer/go-junit-report v0.0.0-20190106144839-af01ea7f8024/go.mod h1:6v2b51hI/fHJwM22ozAgKL4VKDeJcHhJFhtBdhmNjmU=
177177
github.com/jtolds/gls v4.20.0+incompatible h1:xdiiI2gbIgH/gLH7ADydsJ1uDOEzR8yvV7C0MuV77Wo=
178178
github.com/jtolds/gls v4.20.0+incompatible/go.mod h1:QJZ7F/aHp+rZTRtaJ1ow/lLfFfVYBRgL+9YlvaHOwJU=
179-
github.com/julienrbrt/go-header v0.0.0-20250909151551-cb11b091bf58 h1:FBGLvgA4hfRjjYHUiiya7KkGxcHQWSpdn2dRKUEDa5k=
180-
github.com/julienrbrt/go-header v0.0.0-20250909151551-cb11b091bf58/go.mod h1:eX9iTSPthVEAlEDLux40ZT/olXPGhpxHd+mEzJeDhd0=
179+
github.com/julienrbrt/go-header v0.0.0-20251008134330-747c8c192fa8 h1:F+gOiipBxG43s+Ho+ri9T8IwumjWjp1XUon4DLWjxfQ=
180+
github.com/julienrbrt/go-header v0.0.0-20251008134330-747c8c192fa8/go.mod h1:eX9iTSPthVEAlEDLux40ZT/olXPGhpxHd+mEzJeDhd0=
181181
github.com/kisielk/errcheck v1.5.0/go.mod h1:pFxgyoBC7bSaBwPgfKdkLd5X25qrDl4LWUI2bnpBCr8=
182182
github.com/kisielk/gotool v1.0.0/go.mod h1:XhKaO+MFFWcvkIS/tQcRk01m1F5IRFswLeQ+oQHNcck=
183183
github.com/klauspost/compress v1.18.0 h1:c/Cqfb0r+Yi+JtIEq73FWXVkRonBlf0CRNYc8Zttxdo=

pkg/store/store.go

Lines changed: 5 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -277,7 +277,7 @@ func (s *DefaultStore) Rollback(ctx context.Context, height uint64, aggregator b
277277
}
278278

279279
if currentHeight <= height {
280-
return nil
280+
return fmt.Errorf("current height %d is already less than or equal to rollback height %d", currentHeight, height)
281281
}
282282

283283
daIncludedHeightBz, err := s.GetMetadata(ctx, DAIncludedHeightKey)
@@ -322,6 +322,10 @@ func (s *DefaultStore) Rollback(ctx context.Context, height uint64, aggregator b
322322
return fmt.Errorf("failed to delete index key in batch: %w", err)
323323
}
324324

325+
if err := batch.Delete(ctx, ds.NewKey(getStateAtHeightKey(currentHeight))); err != nil {
326+
return fmt.Errorf("failed to delete state in batch: %w", err)
327+
}
328+
325329
currentHeight--
326330
}
327331

@@ -332,26 +336,6 @@ func (s *DefaultStore) Rollback(ctx context.Context, height uint64, aggregator b
332336
return fmt.Errorf("failed to set height: %w", err)
333337
}
334338

335-
targetState, err := s.GetStateAtHeight(ctx, height)
336-
if err != nil {
337-
return fmt.Errorf("failed to get state at height %d: %w", height, err)
338-
}
339-
340-
// update state manually to keep using the batch
341-
pbState, err := targetState.ToProto()
342-
if err != nil {
343-
return fmt.Errorf("failed to convert type state to protobuf type: %w", err)
344-
}
345-
346-
data, err := proto.Marshal(pbState)
347-
if err != nil {
348-
return fmt.Errorf("failed to marshal state to protobuf: %w", err)
349-
}
350-
351-
if err := batch.Put(ctx, ds.NewKey(getStateAtHeightKey(height)), data); err != nil {
352-
return fmt.Errorf("failed to set state at height %d: %w", height, err)
353-
}
354-
355339
if err := batch.Commit(ctx); err != nil {
356340
return fmt.Errorf("failed to commit batch: %w", err)
357341
}

pkg/store/store_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -719,7 +719,7 @@ func TestRollbackToSameHeight(t *testing.T) {
719719

720720
// Execute rollback to same height
721721
err = store.Rollback(ctx, height, true)
722-
require.NoError(err)
722+
require.Error(err)
723723

724724
// Verify height unchanged
725725
newHeight, err := store.Height(ctx)
@@ -754,7 +754,7 @@ func TestRollbackToHigherHeight(t *testing.T) {
754754
// Execute rollback to higher height
755755
rollbackToHeight := uint64(10)
756756
err = store.Rollback(ctx, rollbackToHeight, true)
757-
require.NoError(err)
757+
require.Error(err)
758758

759759
// Verify height unchanged
760760
newHeight, err := store.Height(ctx)

0 commit comments

Comments
 (0)