|
| 1 | +// Copyright (C) 2025, Ava Labs, Inc. All rights reserved. |
| 2 | +// See the file LICENSE for licensing terms. |
| 3 | +package blockchaincmd |
| 4 | + |
| 5 | +import ( |
| 6 | + "encoding/json" |
| 7 | + "errors" |
| 8 | + "fmt" |
| 9 | + "os" |
| 10 | + |
| 11 | + "github.com/ava-labs/avalanche-cli/pkg/cobrautils" |
| 12 | + "github.com/ava-labs/avalanche-cli/pkg/models" |
| 13 | + "github.com/ava-labs/avalanche-cli/pkg/ux" |
| 14 | + "github.com/ava-labs/avalanche-cli/pkg/vm" |
| 15 | + "github.com/spf13/cobra" |
| 16 | +) |
| 17 | + |
| 18 | +// avalanche blockchain import file |
| 19 | +func newImportFileCmd() *cobra.Command { |
| 20 | + cmd := &cobra.Command{ |
| 21 | + Use: "file [blockchainPath]", |
| 22 | + Short: "Import an existing blockchain config", |
| 23 | + RunE: importFile, |
| 24 | + Args: cobrautils.MaximumNArgs(1), |
| 25 | + Long: `The blockchain import file command will import a blockchain configuration from a file. |
| 26 | +
|
| 27 | +You can optionally provide the path as a command-line argument. |
| 28 | +Alternatively, running the command without any arguments triggers an interactive wizard. |
| 29 | +By default, an imported blockchain doesn't overwrite an existing blockchain with the same name. |
| 30 | +To allow overwrites, provide the --force flag.`, |
| 31 | + } |
| 32 | + cmd.Flags().BoolVarP( |
| 33 | + &overwriteImport, |
| 34 | + "force", |
| 35 | + "f", |
| 36 | + false, |
| 37 | + "overwrite the existing configuration if one exists", |
| 38 | + ) |
| 39 | + return cmd |
| 40 | +} |
| 41 | + |
| 42 | +func importFile(_ *cobra.Command, args []string) error { |
| 43 | + var ( |
| 44 | + importPath string |
| 45 | + err error |
| 46 | + ) |
| 47 | + if len(args) == 1 { |
| 48 | + importPath = args[0] |
| 49 | + } |
| 50 | + |
| 51 | + if importPath == "" { |
| 52 | + promptStr := "Select the file to import your blockchain from" |
| 53 | + importPath, err = app.Prompt.CaptureExistingFilepath(promptStr) |
| 54 | + if err != nil { |
| 55 | + return err |
| 56 | + } |
| 57 | + } |
| 58 | + |
| 59 | + importFileBytes, err := os.ReadFile(importPath) |
| 60 | + if err != nil { |
| 61 | + return err |
| 62 | + } |
| 63 | + |
| 64 | + importable := models.Exportable{} |
| 65 | + err = json.Unmarshal(importFileBytes, &importable) |
| 66 | + if err != nil { |
| 67 | + return err |
| 68 | + } |
| 69 | + |
| 70 | + blockchainName := importable.Sidecar.Name |
| 71 | + if blockchainName == "" { |
| 72 | + return errors.New("export data is malformed: missing blockchain name") |
| 73 | + } |
| 74 | + |
| 75 | + if app.GenesisExists(blockchainName) && !overwriteImport { |
| 76 | + return errors.New("blockchain already exists. Use --" + forceFlag + " parameter to overwrite") |
| 77 | + } |
| 78 | + |
| 79 | + if importable.Sidecar.VM == models.CustomVM { |
| 80 | + if importable.Sidecar.CustomVMRepoURL == "" { |
| 81 | + return fmt.Errorf("repository url must be defined for custom vm import") |
| 82 | + } |
| 83 | + if importable.Sidecar.CustomVMBranch == "" { |
| 84 | + return fmt.Errorf("repository branch must be defined for custom vm import") |
| 85 | + } |
| 86 | + if importable.Sidecar.CustomVMBuildScript == "" { |
| 87 | + return fmt.Errorf("build script must be defined for custom vm import") |
| 88 | + } |
| 89 | + |
| 90 | + if err := vm.BuildCustomVM(app, &importable.Sidecar); err != nil { |
| 91 | + return err |
| 92 | + } |
| 93 | + |
| 94 | + vmPath := app.GetCustomVMPath(blockchainName) |
| 95 | + rpcVersion, err := vm.GetVMBinaryProtocolVersion(vmPath) |
| 96 | + if err != nil { |
| 97 | + return fmt.Errorf("unable to get custom binary RPC version: %w", err) |
| 98 | + } |
| 99 | + if rpcVersion != importable.Sidecar.RPCVersion { |
| 100 | + return fmt.Errorf("RPC version mismatch between sidecar and vm binary (%d vs %d)", importable.Sidecar.RPCVersion, rpcVersion) |
| 101 | + } |
| 102 | + } |
| 103 | + |
| 104 | + if err := app.WriteGenesisFile(blockchainName, importable.Genesis); err != nil { |
| 105 | + return err |
| 106 | + } |
| 107 | + |
| 108 | + if importable.NodeConfig != nil { |
| 109 | + if err := app.WriteAvagoNodeConfigFile(blockchainName, importable.NodeConfig); err != nil { |
| 110 | + return err |
| 111 | + } |
| 112 | + } else { |
| 113 | + _ = os.RemoveAll(app.GetAvagoNodeConfigPath(blockchainName)) |
| 114 | + } |
| 115 | + |
| 116 | + if importable.ChainConfig != nil { |
| 117 | + if err := app.WriteChainConfigFile(blockchainName, importable.ChainConfig); err != nil { |
| 118 | + return err |
| 119 | + } |
| 120 | + } else { |
| 121 | + _ = os.RemoveAll(app.GetChainConfigPath(blockchainName)) |
| 122 | + } |
| 123 | + |
| 124 | + if importable.SubnetConfig != nil { |
| 125 | + if err := app.WriteAvagoSubnetConfigFile(blockchainName, importable.SubnetConfig); err != nil { |
| 126 | + return err |
| 127 | + } |
| 128 | + } else { |
| 129 | + _ = os.RemoveAll(app.GetAvagoSubnetConfigPath(blockchainName)) |
| 130 | + } |
| 131 | + |
| 132 | + if importable.NetworkUpgrades != nil { |
| 133 | + if err := app.WriteNetworkUpgradesFile(blockchainName, importable.NetworkUpgrades); err != nil { |
| 134 | + return err |
| 135 | + } |
| 136 | + } else { |
| 137 | + _ = os.RemoveAll(app.GetUpgradeBytesFilepath(blockchainName)) |
| 138 | + } |
| 139 | + |
| 140 | + if err := app.CreateSidecar(&importable.Sidecar); err != nil { |
| 141 | + return err |
| 142 | + } |
| 143 | + |
| 144 | + ux.Logger.PrintToUser("Blockchain imported successfully") |
| 145 | + |
| 146 | + return nil |
| 147 | +} |
0 commit comments