|
| 1 | +package cmd |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + |
| 7 | + ds "github.com/ipfs/go-datastore" |
| 8 | + dsq "github.com/ipfs/go-datastore/query" |
| 9 | + "github.com/spf13/cobra" |
| 10 | + |
| 11 | + rollcmd "github.com/evstack/ev-node/pkg/cmd" |
| 12 | + "github.com/evstack/ev-node/pkg/store" |
| 13 | +) |
| 14 | + |
| 15 | +const ( |
| 16 | + // go-header store prefixes used prior to the unified store migration |
| 17 | + headerSyncPrefix = "/headerSync" |
| 18 | + dataSyncPrefix = "/dataSync" |
| 19 | +) |
| 20 | + |
| 21 | +// NewCleanupGoHeaderCmd creates a command to delete the legacy go-header store data. |
| 22 | +// This data is no longer needed after the migration to the unified store approach |
| 23 | +// where HeaderStoreAdapter and DataStoreAdapter read directly from the ev-node store. |
| 24 | +func NewCleanupGoHeaderCmd() *cobra.Command { |
| 25 | + var dryRun bool |
| 26 | + |
| 27 | + cmd := &cobra.Command{ |
| 28 | + Use: "cleanup-goheader", |
| 29 | + Short: "Delete legacy go-header store data from disk", |
| 30 | + Long: `Delete the legacy go-header store data (headerSync and dataSync prefixes) from the database. |
| 31 | +
|
| 32 | +This command removes data that was previously duplicated by the go-header library |
| 33 | +for P2P sync operations. After the migration to the unified store approach, |
| 34 | +this data is no longer needed as the HeaderStoreAdapter and DataStoreAdapter |
| 35 | +now read directly from the ev-node store. |
| 36 | +
|
| 37 | +WARNING: Make sure the node is stopped before running this command. |
| 38 | +This operation is irreversible.`, |
| 39 | + RunE: func(cmd *cobra.Command, args []string) error { |
| 40 | + nodeConfig, err := rollcmd.ParseConfig(cmd) |
| 41 | + if err != nil { |
| 42 | + return err |
| 43 | + } |
| 44 | + |
| 45 | + goCtx := cmd.Context() |
| 46 | + if goCtx == nil { |
| 47 | + goCtx = context.Background() |
| 48 | + } |
| 49 | + |
| 50 | + // Open the database |
| 51 | + rawDB, err := store.NewDefaultKVStore(nodeConfig.RootDir, nodeConfig.DBPath, evmDbName) |
| 52 | + if err != nil { |
| 53 | + return fmt.Errorf("failed to open database: %w", err) |
| 54 | + } |
| 55 | + defer func() { |
| 56 | + if closeErr := rawDB.Close(); closeErr != nil { |
| 57 | + cmd.Printf("Warning: failed to close database: %v\n", closeErr) |
| 58 | + } |
| 59 | + }() |
| 60 | + |
| 61 | + // Delete headerSync prefix |
| 62 | + headerCount, err := deletePrefix(goCtx, rawDB, headerSyncPrefix, dryRun) |
| 63 | + if err != nil { |
| 64 | + return fmt.Errorf("failed to delete headerSync data: %w", err) |
| 65 | + } |
| 66 | + |
| 67 | + // Delete dataSync prefix |
| 68 | + dataCount, err := deletePrefix(goCtx, rawDB, dataSyncPrefix, dryRun) |
| 69 | + if err != nil { |
| 70 | + return fmt.Errorf("failed to delete dataSync data: %w", err) |
| 71 | + } |
| 72 | + |
| 73 | + totalCount := headerCount + dataCount |
| 74 | + |
| 75 | + if dryRun { |
| 76 | + cmd.Printf("Dry run: would delete %d keys (%d headerSync, %d dataSync)\n", |
| 77 | + totalCount, headerCount, dataCount) |
| 78 | + } else { |
| 79 | + if totalCount == 0 { |
| 80 | + cmd.Println("No legacy go-header store data found to delete.") |
| 81 | + } else { |
| 82 | + cmd.Printf("Successfully deleted %d keys (%d headerSync, %d dataSync)\n", |
| 83 | + totalCount, headerCount, dataCount) |
| 84 | + } |
| 85 | + } |
| 86 | + |
| 87 | + return nil |
| 88 | + }, |
| 89 | + } |
| 90 | + |
| 91 | + cmd.Flags().BoolVar(&dryRun, "dry-run", false, "show what would be deleted without actually deleting") |
| 92 | + |
| 93 | + return cmd |
| 94 | +} |
| 95 | + |
| 96 | +// deletePrefix deletes all keys with the given prefix from the datastore. |
| 97 | +// Returns the number of keys deleted. |
| 98 | +func deletePrefix(ctx context.Context, db ds.Batching, prefix string, dryRun bool) (int, error) { |
| 99 | + results, err := db.Query(ctx, dsq.Query{ |
| 100 | + Prefix: prefix, |
| 101 | + KeysOnly: true, |
| 102 | + }) |
| 103 | + if err != nil { |
| 104 | + return 0, fmt.Errorf("failed to query keys with prefix %s: %w", prefix, err) |
| 105 | + } |
| 106 | + defer results.Close() |
| 107 | + |
| 108 | + count := 0 |
| 109 | + batch, err := db.Batch(ctx) |
| 110 | + if err != nil { |
| 111 | + return 0, fmt.Errorf("failed to create batch: %w", err) |
| 112 | + } |
| 113 | + |
| 114 | + for result := range results.Next() { |
| 115 | + if result.Error != nil { |
| 116 | + return count, fmt.Errorf("error iterating results: %w", result.Error) |
| 117 | + } |
| 118 | + |
| 119 | + if !dryRun { |
| 120 | + if err := batch.Delete(ctx, ds.NewKey(result.Key)); err != nil { |
| 121 | + return count, fmt.Errorf("failed to delete key %s: %w", result.Key, err) |
| 122 | + } |
| 123 | + } |
| 124 | + count++ |
| 125 | + } |
| 126 | + |
| 127 | + if !dryRun && count > 0 { |
| 128 | + if err := batch.Commit(ctx); err != nil { |
| 129 | + return count, fmt.Errorf("failed to commit batch delete: %w", err) |
| 130 | + } |
| 131 | + } |
| 132 | + |
| 133 | + return count, nil |
| 134 | +} |
0 commit comments