Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 12 additions & 17 deletions apps/evm/single/cmd/rollback.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package cmd

import (
"context"
"errors"
"fmt"

ds "github.com/ipfs/go-datastore"
Expand All @@ -18,9 +19,8 @@ import (
// NewRollbackCmd creates a command to rollback ev-node state by one height.
func NewRollbackCmd() *cobra.Command {
var (
height uint64
skipP2PStores bool
syncNode bool
height uint64
syncNode bool
)

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

if skipP2PStores {
return printSuccess(height)
}

// rollback ev-node goheader state
headerStore, err := goheaderstore.NewStore[*types.SignedHeader](
evolveDB,
Expand Down Expand Up @@ -102,27 +98,26 @@ func NewRollbackCmd() *cobra.Command {
}
defer dataStore.Stop(goCtx)

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

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

fmt.Printf("Rolled back ev-node state to height %d\n", height)
if syncNode {
fmt.Println("Restart the node with the `--clear-cache` flag")
}

return printSuccess(height)
return errs
},
}

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

return cmd
}

func printSuccess(height uint64) error {
fmt.Printf("Rolled back ev-node state to height %d\n", height)
fmt.Println("Restart the node with the `--clear-cache` flag")
return nil
}
2 changes: 1 addition & 1 deletion apps/evm/single/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ module github.com/evstack/ev-node/apps/evm/single

go 1.24.6

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
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

replace (
github.com/evstack/ev-node => ../../../
Expand Down
4 changes: 2 additions & 2 deletions apps/evm/single/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -242,8 +242,8 @@ github.com/json-iterator/go v1.1.6/go.mod h1:+SdeFBvtyEkXs7REEP0seUULqWtbJapLOCV
github.com/jstemmer/go-junit-report v0.0.0-20190106144839-af01ea7f8024/go.mod h1:6v2b51hI/fHJwM22ozAgKL4VKDeJcHhJFhtBdhmNjmU=
github.com/jtolds/gls v4.20.0+incompatible h1:xdiiI2gbIgH/gLH7ADydsJ1uDOEzR8yvV7C0MuV77Wo=
github.com/jtolds/gls v4.20.0+incompatible/go.mod h1:QJZ7F/aHp+rZTRtaJ1ow/lLfFfVYBRgL+9YlvaHOwJU=
github.com/julienrbrt/go-header v0.0.0-20250909151551-cb11b091bf58 h1:FBGLvgA4hfRjjYHUiiya7KkGxcHQWSpdn2dRKUEDa5k=
github.com/julienrbrt/go-header v0.0.0-20250909151551-cb11b091bf58/go.mod h1:eX9iTSPthVEAlEDLux40ZT/olXPGhpxHd+mEzJeDhd0=
github.com/julienrbrt/go-header v0.0.0-20251008134330-747c8c192fa8 h1:F+gOiipBxG43s+Ho+ri9T8IwumjWjp1XUon4DLWjxfQ=
github.com/julienrbrt/go-header v0.0.0-20251008134330-747c8c192fa8/go.mod h1:eX9iTSPthVEAlEDLux40ZT/olXPGhpxHd+mEzJeDhd0=
github.com/kisielk/errcheck v1.5.0/go.mod h1:pFxgyoBC7bSaBwPgfKdkLd5X25qrDl4LWUI2bnpBCr8=
github.com/kisielk/gotool v1.0.0/go.mod h1:XhKaO+MFFWcvkIS/tQcRk01m1F5IRFswLeQ+oQHNcck=
github.com/klauspost/compress v1.18.0 h1:c/Cqfb0r+Yi+JtIEq73FWXVkRonBlf0CRNYc8Zttxdo=
Expand Down
31 changes: 13 additions & 18 deletions apps/testapp/cmd/rollback.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package cmd

import (
"context"
"errors"
"fmt"

kvexecutor "github.com/evstack/ev-node/apps/testapp/kv"
Expand All @@ -19,9 +20,8 @@ import (
// NewRollbackCmd creates a command to rollback ev-node state by one height.
func NewRollbackCmd() *cobra.Command {
var (
height uint64
skipP2PStores bool
syncNode bool
height uint64
syncNode bool
)

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

if skipP2PStores {
return printSuccess(height)
}

// rollback ev-node goheader state
headerStore, err := goheaderstore.NewStore[*types.SignedHeader](
evolveDB,
Expand Down Expand Up @@ -108,31 +104,30 @@ func NewRollbackCmd() *cobra.Command {
}
defer dataStore.Stop(goCtx)

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

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

// rollback execution store
if err := executor.Rollback(goCtx, height); err != nil {
return fmt.Errorf("rollback failed: %w", err)
errs = errors.Join(errs, fmt.Errorf("rollback failed: %w", err))
}

fmt.Printf("Rolled back ev-node state to height %d\n", height)
if syncNode {
fmt.Println("Restart the node with the `--clear-cache` flag")
}

return printSuccess(height)
return errs
},
}

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

func printSuccess(height uint64) error {
fmt.Printf("Rolled back ev-node state to height %d\n", height)
fmt.Println("Restart the node with the `--clear-cache` flag")
return nil
}
2 changes: 1 addition & 1 deletion apps/testapp/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ module github.com/evstack/ev-node/apps/testapp

go 1.24.6

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
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

replace (
github.com/evstack/ev-node => ../../.
Expand Down
4 changes: 2 additions & 2 deletions apps/testapp/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -176,8 +176,8 @@ github.com/json-iterator/go v1.1.6/go.mod h1:+SdeFBvtyEkXs7REEP0seUULqWtbJapLOCV
github.com/jstemmer/go-junit-report v0.0.0-20190106144839-af01ea7f8024/go.mod h1:6v2b51hI/fHJwM22ozAgKL4VKDeJcHhJFhtBdhmNjmU=
github.com/jtolds/gls v4.20.0+incompatible h1:xdiiI2gbIgH/gLH7ADydsJ1uDOEzR8yvV7C0MuV77Wo=
github.com/jtolds/gls v4.20.0+incompatible/go.mod h1:QJZ7F/aHp+rZTRtaJ1ow/lLfFfVYBRgL+9YlvaHOwJU=
github.com/julienrbrt/go-header v0.0.0-20250909151551-cb11b091bf58 h1:FBGLvgA4hfRjjYHUiiya7KkGxcHQWSpdn2dRKUEDa5k=
github.com/julienrbrt/go-header v0.0.0-20250909151551-cb11b091bf58/go.mod h1:eX9iTSPthVEAlEDLux40ZT/olXPGhpxHd+mEzJeDhd0=
github.com/julienrbrt/go-header v0.0.0-20251008134330-747c8c192fa8 h1:F+gOiipBxG43s+Ho+ri9T8IwumjWjp1XUon4DLWjxfQ=
github.com/julienrbrt/go-header v0.0.0-20251008134330-747c8c192fa8/go.mod h1:eX9iTSPthVEAlEDLux40ZT/olXPGhpxHd+mEzJeDhd0=
github.com/kisielk/errcheck v1.5.0/go.mod h1:pFxgyoBC7bSaBwPgfKdkLd5X25qrDl4LWUI2bnpBCr8=
github.com/kisielk/gotool v1.0.0/go.mod h1:XhKaO+MFFWcvkIS/tQcRk01m1F5IRFswLeQ+oQHNcck=
github.com/klauspost/compress v1.18.0 h1:c/Cqfb0r+Yi+JtIEq73FWXVkRonBlf0CRNYc8Zttxdo=
Expand Down
26 changes: 5 additions & 21 deletions pkg/store/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -277,7 +277,7 @@ func (s *DefaultStore) Rollback(ctx context.Context, height uint64, aggregator b
}

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

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

if err := batch.Delete(ctx, ds.NewKey(getStateAtHeightKey(currentHeight))); err != nil {
return fmt.Errorf("failed to delete state in batch: %w", err)
}

currentHeight--
}

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

targetState, err := s.GetStateAtHeight(ctx, height)
if err != nil {
return fmt.Errorf("failed to get state at height %d: %w", height, err)
}

// update state manually to keep using the batch
pbState, err := targetState.ToProto()
if err != nil {
return fmt.Errorf("failed to convert type state to protobuf type: %w", err)
}

data, err := proto.Marshal(pbState)
if err != nil {
return fmt.Errorf("failed to marshal state to protobuf: %w", err)
}

if err := batch.Put(ctx, ds.NewKey(getStateAtHeightKey(height)), data); err != nil {
return fmt.Errorf("failed to set state at height %d: %w", height, err)
}

if err := batch.Commit(ctx); err != nil {
return fmt.Errorf("failed to commit batch: %w", err)
}
Expand Down
4 changes: 2 additions & 2 deletions pkg/store/store_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -719,7 +719,7 @@ func TestRollbackToSameHeight(t *testing.T) {

// Execute rollback to same height
err = store.Rollback(ctx, height, true)
require.NoError(err)
require.Error(err)

// Verify height unchanged
newHeight, err := store.Height(ctx)
Expand Down Expand Up @@ -754,7 +754,7 @@ func TestRollbackToHigherHeight(t *testing.T) {
// Execute rollback to higher height
rollbackToHeight := uint64(10)
err = store.Rollback(ctx, rollbackToHeight, true)
require.NoError(err)
require.Error(err)

// Verify height unchanged
newHeight, err := store.Height(ctx)
Expand Down
Loading