|
| 1 | +package commands |
| 2 | + |
| 3 | +import ( |
| 4 | + "encoding/json" |
| 5 | + "fmt" |
| 6 | + "os" |
| 7 | + "path/filepath" |
| 8 | + "strings" |
| 9 | + "time" |
| 10 | + |
| 11 | + "github.com/spf13/cobra" |
| 12 | + |
| 13 | + "github.com/sei-protocol/sei-chain/sei-tendermint/config" |
| 14 | + atypes "github.com/sei-protocol/sei-chain/sei-tendermint/internal/autobahn/types" |
| 15 | + "github.com/sei-protocol/sei-chain/sei-tendermint/internal/p2p" |
| 16 | + "github.com/sei-protocol/sei-chain/sei-tendermint/libs/utils" |
| 17 | + "github.com/sei-protocol/sei-chain/sei-tendermint/libs/utils/tcp" |
| 18 | +) |
| 19 | + |
| 20 | +// MakeGenAutobahnConfigCommand creates a cobra command that generates an autobahn JSON config file. |
| 21 | +// Each node directory must contain validator_pubkey.txt, node_pubkey.txt, and autobahn_address.txt. |
| 22 | +func MakeGenAutobahnConfigCommand() *cobra.Command { |
| 23 | + cmd := &cobra.Command{ |
| 24 | + Use: "gen-autobahn-config [node-dirs...]", |
| 25 | + Short: "Generate autobahn JSON config from node pubkey files", |
| 26 | + Long: `Generate an autobahn JSON config file by reading validator_pubkey.txt, |
| 27 | +node_pubkey.txt, and autobahn_address.txt from each node directory. |
| 28 | +Output is written to the file specified by --output.`, |
| 29 | + Args: cobra.MinimumNArgs(1), |
| 30 | + RunE: func(cmd *cobra.Command, args []string) error { |
| 31 | + output, _ := cmd.Flags().GetString("output") |
| 32 | + if output == "" { |
| 33 | + return fmt.Errorf("--output flag is required") |
| 34 | + } |
| 35 | + |
| 36 | + var validators []config.AutobahnValidator |
| 37 | + for _, dir := range args { |
| 38 | + valKeyRaw, err := os.ReadFile(filepath.Clean(filepath.Join(dir, "validator_pubkey.txt"))) |
| 39 | + if err != nil { |
| 40 | + return fmt.Errorf("reading validator_pubkey.txt from %s: %w", dir, err) |
| 41 | + } |
| 42 | + var valKey atypes.PublicKey |
| 43 | + if err := valKey.UnmarshalText([]byte(strings.TrimSpace(string(valKeyRaw)))); err != nil { |
| 44 | + return fmt.Errorf("parsing validator key from %s: %w", dir, err) |
| 45 | + } |
| 46 | + |
| 47 | + nodeKeyRaw, err := os.ReadFile(filepath.Clean(filepath.Join(dir, "node_pubkey.txt"))) |
| 48 | + if err != nil { |
| 49 | + return fmt.Errorf("reading node_pubkey.txt from %s: %w", dir, err) |
| 50 | + } |
| 51 | + var nodeKey p2p.NodePublicKey |
| 52 | + if err := nodeKey.UnmarshalText([]byte(strings.TrimSpace(string(nodeKeyRaw)))); err != nil { |
| 53 | + return fmt.Errorf("parsing node key from %s: %w", dir, err) |
| 54 | + } |
| 55 | + |
| 56 | + addrRaw, err := os.ReadFile(filepath.Clean(filepath.Join(dir, "autobahn_address.txt"))) |
| 57 | + if err != nil { |
| 58 | + return fmt.Errorf("reading autobahn_address.txt from %s: %w", dir, err) |
| 59 | + } |
| 60 | + addr, err := tcp.ParseHostPort(strings.TrimSpace(string(addrRaw))) |
| 61 | + if err != nil { |
| 62 | + return fmt.Errorf("parsing address from %s: %w", dir, err) |
| 63 | + } |
| 64 | + |
| 65 | + validators = append(validators, config.AutobahnValidator{ |
| 66 | + ValidatorKey: valKey, |
| 67 | + NodeKey: nodeKey, |
| 68 | + Address: addr, |
| 69 | + }) |
| 70 | + } |
| 71 | + |
| 72 | + cfg := config.AutobahnFileConfig{ |
| 73 | + Validators: validators, |
| 74 | + MaxGasPerBlock: 50_000_000, |
| 75 | + MaxTxsPerBlock: 5_000, |
| 76 | + MempoolSize: 5_000, |
| 77 | + BlockInterval: utils.Duration(400 * time.Millisecond), |
| 78 | + AllowEmptyBlocks: false, |
| 79 | + ViewTimeout: utils.Duration(1500 * time.Millisecond), |
| 80 | + DialInterval: utils.Duration(10 * time.Second), |
| 81 | + } |
| 82 | + |
| 83 | + data, err := json.MarshalIndent(cfg, "", " ") |
| 84 | + if err != nil { |
| 85 | + return fmt.Errorf("marshaling config: %w", err) |
| 86 | + } |
| 87 | + if err := os.WriteFile(output, data, 0600); err != nil { |
| 88 | + return fmt.Errorf("writing config to %s: %w", output, err) |
| 89 | + } |
| 90 | + fmt.Printf("Autobahn config written to %s\n", output) |
| 91 | + return nil |
| 92 | + }, |
| 93 | + } |
| 94 | + cmd.Flags().StringP("output", "o", "", "output file path for the autobahn config") |
| 95 | + return cmd |
| 96 | +} |
0 commit comments