|
| 1 | +package cmd |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "math/big" |
| 6 | + "os" |
| 7 | + |
| 8 | + "github.com/AvaProtocol/EigenLayer-AVS/core/chainio/aa" |
| 9 | + avsconfig "github.com/AvaProtocol/EigenLayer-AVS/core/config" |
| 10 | + "github.com/AvaProtocol/EigenLayer-AVS/core/taskengine" |
| 11 | + "github.com/AvaProtocol/EigenLayer-AVS/storage" |
| 12 | + "github.com/ethereum/go-ethereum/common" |
| 13 | + "github.com/ethereum/go-ethereum/ethclient" |
| 14 | + "github.com/spf13/cobra" |
| 15 | +) |
| 16 | + |
| 17 | +var ( |
| 18 | + backfillWalletSaltIndexDryRun bool |
| 19 | + backfillWalletSaltIndexVerbose bool |
| 20 | + |
| 21 | + backfillWalletSaltIndexCmd = &cobra.Command{ |
| 22 | + Use: "backfill-wallet-salt-index", |
| 23 | + Short: "Backfill the (owner, factory, salt) → wallet address secondary index", |
| 24 | + Long: `Walks every persisted wallet record and either: |
| 25 | +
|
| 26 | + 1. Writes a (owner, factory, salt) → address secondary index entry, when |
| 27 | + the live derivation against the smart-wallet RPC still matches the |
| 28 | + stored address (canonical row); or |
| 29 | + 2. Marks the row stale (StaleDerivation = true, IsHidden = true) when the |
| 30 | + derivation differs — i.e. the factory's account implementation has been |
| 31 | + upgraded since the row was first persisted. |
| 32 | +
|
| 33 | +This subcommand reads db_path and smart_wallet.eth_rpc_url from the same |
| 34 | +aggregator YAML the running aggregator uses, so it must be invoked with |
| 35 | +--config pointing at that file. |
| 36 | +
|
| 37 | +OPERATIONAL CONSTRAINT: BadgerDB is single-writer. The aggregator must be |
| 38 | +stopped before this command runs, otherwise opening the database will fail |
| 39 | +with a lock error. Always run with --dry-run first against a fresh backup. |
| 40 | +
|
| 41 | +Production usage (Docker): |
| 42 | +
|
| 43 | + docker stop aggregator-base |
| 44 | + docker run --rm --volumes-from aggregator-base avaprotocol/ap-avs:latest \ |
| 45 | + backfill-wallet-salt-index --config /app/config/aggregator-base.yaml --dry-run |
| 46 | + # inspect summary, then re-run without --dry-run |
| 47 | + docker start aggregator-base |
| 48 | +`, |
| 49 | + Run: func(cmd *cobra.Command, args []string) { |
| 50 | + if err := runBackfillWalletSaltIndex(config); err != nil { |
| 51 | + fmt.Fprintln(os.Stderr, err) |
| 52 | + os.Exit(1) |
| 53 | + } |
| 54 | + }, |
| 55 | + } |
| 56 | +) |
| 57 | + |
| 58 | +func init() { |
| 59 | + backfillWalletSaltIndexCmd.Flags().BoolVar(&backfillWalletSaltIndexDryRun, "dry-run", false, "Print what would change without writing to BadgerDB") |
| 60 | + backfillWalletSaltIndexCmd.Flags().BoolVar(&backfillWalletSaltIndexVerbose, "verbose", false, "Print one line per wallet processed") |
| 61 | + rootCmd.AddCommand(backfillWalletSaltIndexCmd) |
| 62 | +} |
| 63 | + |
| 64 | +func runBackfillWalletSaltIndex(configPath string) error { |
| 65 | + nodeConfig, err := avsconfig.NewConfig(configPath) |
| 66 | + if err != nil { |
| 67 | + return fmt.Errorf("parse config %s: %w", configPath, err) |
| 68 | + } |
| 69 | + if nodeConfig.DbPath == "" { |
| 70 | + return fmt.Errorf("config %s has no db_path", configPath) |
| 71 | + } |
| 72 | + if nodeConfig.SmartWallet == nil || nodeConfig.SmartWallet.EthRpcUrl == "" { |
| 73 | + return fmt.Errorf("config %s has no smart_wallet.eth_rpc_url", configPath) |
| 74 | + } |
| 75 | + |
| 76 | + fmt.Printf("Config: %s\n", configPath) |
| 77 | + fmt.Printf("DB path: %s\n", nodeConfig.DbPath) |
| 78 | + // Redact: RPC URLs from Tenderly/Alchemy/Infura embed API keys in |
| 79 | + // the path or query — printing the raw URL leaks secrets to terminal |
| 80 | + // scrollback and CI logs. |
| 81 | + fmt.Printf("RPC URL: %s\n", taskengine.RedactRPCURL(nodeConfig.SmartWallet.EthRpcUrl)) |
| 82 | + fmt.Printf("Mode: %s\n", backfillModeLabel(backfillWalletSaltIndexDryRun)) |
| 83 | + fmt.Println() |
| 84 | + |
| 85 | + rpcClient, err := ethclient.Dial(nodeConfig.SmartWallet.EthRpcUrl) |
| 86 | + if err != nil { |
| 87 | + return fmt.Errorf("dial smart-wallet RPC %s: %w", nodeConfig.SmartWallet.EthRpcUrl, err) |
| 88 | + } |
| 89 | + defer rpcClient.Close() |
| 90 | + |
| 91 | + db, err := storage.NewWithPath(nodeConfig.DbPath) |
| 92 | + if err != nil { |
| 93 | + return fmt.Errorf("open BadgerDB at %s: %w\n\nIs the aggregator still running? It must be stopped first.", nodeConfig.DbPath, err) |
| 94 | + } |
| 95 | + defer db.Close() |
| 96 | + |
| 97 | + deriver := func(owner common.Address, factory common.Address, salt *big.Int) (common.Address, error) { |
| 98 | + addr, derr := aa.GetSenderAddressForFactory(rpcClient, owner, factory, salt) |
| 99 | + if derr != nil { |
| 100 | + return common.Address{}, derr |
| 101 | + } |
| 102 | + if addr == nil { |
| 103 | + return common.Address{}, fmt.Errorf("derived nil address") |
| 104 | + } |
| 105 | + return *addr, nil |
| 106 | + } |
| 107 | + |
| 108 | + stats, err := taskengine.BackfillWalletSaltIndex(db, deriver, taskengine.WalletSaltIndexBackfillOptions{ |
| 109 | + DryRun: backfillWalletSaltIndexDryRun, |
| 110 | + Verbose: backfillWalletSaltIndexVerbose, |
| 111 | + Logf: func(format string, args ...any) { |
| 112 | + fmt.Printf(format+"\n", args...) |
| 113 | + }, |
| 114 | + }) |
| 115 | + if err != nil { |
| 116 | + return fmt.Errorf("backfill: %w", err) |
| 117 | + } |
| 118 | + |
| 119 | + fmt.Println() |
| 120 | + fmt.Println("Summary") |
| 121 | + fmt.Println("-------") |
| 122 | + fmt.Printf("Total wallet rows scanned: %d\n", stats.Total) |
| 123 | + fmt.Printf(" Canonical (live derive matches): %d\n", stats.CanonicalConfirmed) |
| 124 | + fmt.Printf(" Secondary index newly written: %d\n", stats.SecondaryIndexWritten) |
| 125 | + fmt.Printf(" Secondary index already correct: %d\n", stats.SecondaryIndexExisting) |
| 126 | + fmt.Printf(" Stale (live derive differs): %d\n", stats.NewlyMarkedStale+stats.AlreadyStale) |
| 127 | + fmt.Printf(" Newly marked stale: %d\n", stats.NewlyMarkedStale) |
| 128 | + fmt.Printf(" Already marked stale: %d\n", stats.AlreadyStale) |
| 129 | + fmt.Printf(" Skipped — missing factory: %d\n", stats.SkippedMissingFactory) |
| 130 | + fmt.Printf(" Skipped — missing salt: %d\n", stats.SkippedMissingSalt) |
| 131 | + fmt.Printf(" Skipped — RPC derive error: %d\n", stats.SkippedDeriveError) |
| 132 | + if backfillWalletSaltIndexDryRun { |
| 133 | + fmt.Println() |
| 134 | + fmt.Println("DRY RUN — no changes written. Re-run without --dry-run to apply.") |
| 135 | + } |
| 136 | + return nil |
| 137 | +} |
| 138 | + |
| 139 | +func backfillModeLabel(dryRun bool) string { |
| 140 | + if dryRun { |
| 141 | + return "DRY RUN" |
| 142 | + } |
| 143 | + return "APPLY" |
| 144 | +} |
0 commit comments