Skip to content

Commit b2182e7

Browse files
authored
feat(config): add --clear-cache flag (#2701)
<!-- 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 Note, the flag is added in the config, like RootDir, but this will actually never be written in the yaml. This is expected. <!-- 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 dd98789 commit b2182e7

File tree

5 files changed

+46
-14
lines changed

5 files changed

+46
-14
lines changed

apps/evm/single/cmd/rollback.go

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -70,8 +70,7 @@ func NewRollbackCmd() *cobra.Command {
7070
}
7171

7272
if skipP2PStores {
73-
fmt.Printf("Rolled back ev-node state to height %d\n", height)
74-
return nil
73+
return printSuccess(height)
7574
}
7675

7776
// rollback ev-node goheader state
@@ -111,8 +110,7 @@ func NewRollbackCmd() *cobra.Command {
111110
return fmt.Errorf("failed to rollback data sync service state: %w", err)
112111
}
113112

114-
fmt.Printf("Rolled back state to height %d\n", height)
115-
return nil
113+
return printSuccess(height)
116114
},
117115
}
118116

@@ -122,3 +120,9 @@ func NewRollbackCmd() *cobra.Command {
122120

123121
return cmd
124122
}
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/testapp/cmd/rollback.go

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -76,8 +76,7 @@ func NewRollbackCmd() *cobra.Command {
7676
}
7777

7878
if skipP2PStores {
79-
fmt.Printf("Rolled back ev-node state to height %d\n", height)
80-
return nil
79+
return printSuccess(height)
8180
}
8281

8382
// rollback ev-node goheader state
@@ -122,8 +121,7 @@ func NewRollbackCmd() *cobra.Command {
122121
return fmt.Errorf("rollback failed: %w", err)
123122
}
124123

125-
fmt.Printf("Rolled back state to height %d\n", height)
126-
return nil
124+
return printSuccess(height)
127125
},
128126
}
129127

@@ -132,3 +130,9 @@ func NewRollbackCmd() *cobra.Command {
132130
cmd.Flags().BoolVar(&skipP2PStores, "skip-p2p-stores", false, "skip rollback p2p stores (goheaderstore)")
133131
return cmd
134132
}
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+
}

block/internal/cache/manager.go

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"context"
55
"encoding/gob"
66
"fmt"
7+
"os"
78
"path/filepath"
89
"sync"
910

@@ -64,8 +65,11 @@ type Manager interface {
6465
// Cleanup operations
6566
SaveToDisk() error
6667
LoadFromDisk() error
68+
ClearFromDisk() error
6769
}
6870

71+
var _ Manager = (*implementation)(nil)
72+
6973
// implementation provides the concrete implementation of cache Manager
7074
type implementation struct {
7175
headerCache *Cache[types.SignedHeader]
@@ -105,9 +109,16 @@ func NewManager(cfg config.Config, store store.Store, logger zerolog.Logger) (Ma
105109
logger: logger,
106110
}
107111

108-
// Load existing cache from disk
109-
if err := impl.LoadFromDisk(); err != nil {
110-
logger.Warn().Err(err).Msg("failed to load cache from disk, starting with empty cache")
112+
if cfg.ClearCache {
113+
// Clear the cache from disk
114+
if err := impl.ClearFromDisk(); err != nil {
115+
logger.Warn().Err(err).Msg("failed to clear cache from disk, starting with empty cache")
116+
}
117+
} else {
118+
// Load existing cache from disk
119+
if err := impl.LoadFromDisk(); err != nil {
120+
logger.Warn().Err(err).Msg("failed to load cache from disk, starting with empty cache")
121+
}
111122
}
112123

113124
return impl, nil
@@ -245,3 +256,11 @@ func (m *implementation) LoadFromDisk() error {
245256

246257
return nil
247258
}
259+
260+
func (m *implementation) ClearFromDisk() error {
261+
cachePath := filepath.Join(m.config.RootDir, "data", cacheDir)
262+
if err := os.RemoveAll(cachePath); err != nil {
263+
return fmt.Errorf("failed to clear cache from disk: %w", err)
264+
}
265+
return nil
266+
}

pkg/config/config.go

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,8 @@ const (
4747
FlagLazyBlockTime = FlagPrefixEvnode + "node.lazy_block_interval"
4848
// FlagReadinessMaxBlocksBehind configures how many blocks behind best-known head is still considered ready
4949
FlagReadinessMaxBlocksBehind = FlagPrefixEvnode + "node.readiness_max_blocks_behind"
50+
// FlagClearCache is a flag for clearing the cache
51+
FlagClearCache = FlagPrefixEvnode + "clear_cache"
5052

5153
// Data Availability configuration flags
5254

@@ -127,9 +129,11 @@ const (
127129

128130
// Config stores Rollkit configuration.
129131
type Config struct {
132+
RootDir string `mapstructure:"-" yaml:"-" comment:"Root directory where rollkit files are located"`
133+
ClearCache bool `mapstructure:"-" yaml:"-" comment:"Clear the cache"`
134+
130135
// Base configuration
131-
RootDir string `mapstructure:"-" yaml:"-" comment:"Root directory where rollkit files are located"`
132-
DBPath string `mapstructure:"db_path" yaml:"db_path" comment:"Path inside the root directory where the database is located"`
136+
DBPath string `mapstructure:"db_path" yaml:"db_path" comment:"Path inside the root directory where the database is located"`
133137
// P2P configuration
134138
P2P P2PConfig `mapstructure:"p2p" yaml:"p2p"`
135139

@@ -303,6 +307,7 @@ func AddFlags(cmd *cobra.Command) {
303307

304308
// Add base flags
305309
cmd.Flags().String(FlagDBPath, def.DBPath, "path for the node database")
310+
cmd.Flags().Bool(FlagClearCache, def.ClearCache, "clear the cache")
306311

307312
// Node configuration flags
308313
cmd.Flags().Bool(FlagAggregator, def.Node.Aggregator, "run node in aggregator mode")

pkg/config/config_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ func TestAddFlags(t *testing.T) {
105105
assertFlagValue(t, flags, FlagRPCAddress, DefaultConfig().RPC.Address)
106106

107107
// Count the number of flags we're explicitly checking
108-
expectedFlagCount := 38 // Update this number if you add more flag checks above
108+
expectedFlagCount := 39 // Update this number if you add more flag checks above
109109

110110
// Get the actual number of flags (both regular and persistent)
111111
actualFlagCount := 0

0 commit comments

Comments
 (0)