-
Notifications
You must be signed in to change notification settings - Fork 270
Expand file tree
/
Copy pathrollback.go
More file actions
86 lines (70 loc) · 2.29 KB
/
Copy pathrollback.go
File metadata and controls
86 lines (70 loc) · 2.29 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
package cmd
import (
"context"
"fmt"
"github.com/spf13/cobra"
kvexecutor "github.com/evstack/ev-node/apps/testapp/kv"
rollcmd "github.com/evstack/ev-node/pkg/cmd"
"github.com/evstack/ev-node/pkg/store"
)
// NewRollbackCmd creates a command to rollback ev-node state by one height.
func NewRollbackCmd() *cobra.Command {
var (
height uint64
syncNode bool
)
cmd := &cobra.Command{
Use: "rollback",
Short: "rollback ev-node state by one height. Pass --height to specify another height to rollback to.",
RunE: func(cmd *cobra.Command, args []string) error {
nodeConfig, err := rollcmd.ParseConfig(cmd)
if err != nil {
return err
}
goCtx := cmd.Context()
if goCtx == nil {
goCtx = context.Background()
}
// evolve db
rawEvolveDB, err := store.NewDefaultKVStore(nodeConfig.RootDir, nodeConfig.DBPath, testDbName)
if err != nil {
return err
}
defer func() {
if closeErr := rawEvolveDB.Close(); closeErr != nil {
fmt.Printf("Warning: failed to close evolve database: %v\n", closeErr)
}
}()
// prefixed evolve db
evolveDB := store.NewEvNodeKVStore(rawEvolveDB)
evolveStore := store.New(evolveDB)
if height == 0 {
currentHeight, err := evolveStore.Height(goCtx)
if err != nil {
return err
}
height = currentHeight - 1
}
executor := kvexecutor.NewKVExecutor()
// rollback ev-node main state
// Note: With the unified store approach, the ev-node store is the single source of truth.
// The store adapters (HeaderStoreAdapter/DataStoreAdapter) read from this store,
// so rolling back the ev-node store automatically affects P2P sync operations.
if err := evolveStore.Rollback(goCtx, height, !syncNode); err != nil {
return fmt.Errorf("failed to rollback ev-node state: %w", err)
}
// rollback execution store
if err := executor.Rollback(goCtx, height); err != nil {
return fmt.Errorf("failed to rollback executor state: %w", err)
}
fmt.Printf("Rolled back ev-node state to height %d\n", height)
if syncNode {
fmt.Println("Restart the node with the `--evnode.clear_cache` flag")
}
return nil
},
}
cmd.Flags().Uint64Var(&height, "height", 0, "rollback to a specific height")
cmd.Flags().BoolVar(&syncNode, "sync-node", false, "sync node (no aggregator)")
return cmd
}