Skip to content

Commit

Permalink
Merge branch 'main' into acp-77-local-network
Browse files Browse the repository at this point in the history
  • Loading branch information
felipemadero committed Nov 23, 2024
2 parents 8d97a04 + 6debe41 commit ba6cf0c
Show file tree
Hide file tree
Showing 6 changed files with 146 additions and 18 deletions.
25 changes: 20 additions & 5 deletions cmd/blockchaincmd/deploy.go
Original file line number Diff line number Diff line change
Expand Up @@ -595,13 +595,28 @@ func deployBlockchain(cmd *cobra.Command, args []string) error {
avagoVersionSettings := node.AvalancheGoVersionSettings{}
useEtnaDevnet := network.Kind == models.EtnaDevnet
if avagoBinaryPath == "" {
ux.Logger.PrintToUser("Local build of Avalanche Go is required to create an Avalanche node using local machine")
ux.Logger.PrintToUser("Please download Avalanche Go repo at https://github.com/ava-labs/avalanchego and build from source through ./scripts/build.sh")
ux.Logger.PrintToUser("Please provide the full path to Avalanche Go binary in the build directory (e.g, xxx/build/avalanchego)")
avagoBinaryPath, err = app.Prompt.CaptureString("Path to Avalanche Go build")
useLatestAvalanchegoPreReleaseVersion := true
useLatestAvalanchegoReleaseVersion := false
if userProvidedAvagoVersion != "latest" {
useLatestAvalanchegoReleaseVersion = false
useLatestAvalanchegoPreReleaseVersion = false
} else {
userProvidedAvagoVersion = ""
}
avaGoVersionSetting := node.AvalancheGoVersionSettings{
UseCustomAvalanchegoVersion: userProvidedAvagoVersion,
UseLatestAvalanchegoPreReleaseVersion: useLatestAvalanchegoPreReleaseVersion,
UseLatestAvalanchegoReleaseVersion: useLatestAvalanchegoReleaseVersion,
}
avalancheGoVersion, err := node.GetAvalancheGoVersion(app, avaGoVersionSetting)
if err != nil {
return err
}
_, avagoDir, err := binutils.SetupAvalanchego(app, avalancheGoVersion)
if err != nil {
return fmt.Errorf("failed installing Avalanche Go version %s: %w", avalancheGoVersion, err)
}
avagoBinaryPath = filepath.Join(avagoDir, "avalanchego")
}
nodeConfig := map[string]interface{}{}
if app.AvagoNodeConfigExists(blockchainName) {
Expand Down Expand Up @@ -1309,7 +1324,7 @@ func CheckForInvalidDeployAndGetAvagoVersion(
}

if networkRunning {
if userProvidedAvagoVersion == "latest" {
if userProvidedAvagoVersion == constants.LatestAvalancheGoVersion {
if runningRPCVersion != configuredRPCVersion && !skipRPCCheck {
return "", fmt.Errorf(
"the current avalanchego deployment uses rpc version %d but your subnet has version %d and is not compatible",
Expand Down
11 changes: 9 additions & 2 deletions cmd/nodecmd/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,8 +137,15 @@ will apply to all nodes in the cluster`,
func handlePostRun(_ *cobra.Command, _ []string) {}

func preCreateChecks(clusterName string) error {
if !flags.EnsureMutuallyExclusive([]bool{useLatestAvalanchegoReleaseVersion, useLatestAvalanchegoPreReleaseVersion, useAvalanchegoVersionFromSubnet != "", useCustomAvalanchegoVersion != ""}) {
return fmt.Errorf("latest avalanchego released version, latest avalanchego pre-released version, custom avalanchego version and avalanchego version based on given subnet, are mutually exclusive options")
if useCustomAvalanchegoVersion != "" || useAvalanchegoVersionFromSubnet != "" {
useLatestAvalanchegoReleaseVersion = false
useLatestAvalanchegoPreReleaseVersion = false
}
if !flags.EnsureMutuallyExclusive([]bool{useLatestAvalanchegoReleaseVersion, useLatestAvalanchegoPreReleaseVersion}) {
return fmt.Errorf("latest avalanchego released version, latest avalanchego pre-released version are mutually exclusive options")
}
if !flags.EnsureMutuallyExclusive([]bool{useAvalanchegoVersionFromSubnet != "", useCustomAvalanchegoVersion != ""}) {
return fmt.Errorf("custom avalanchego version and avalanchego version based on given subnet, are mutually exclusive options")
}
if useAWS && useGCP {
return fmt.Errorf("could not use both AWS and GCP cloud options")
Expand Down
33 changes: 31 additions & 2 deletions cmd/nodecmd/local.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ package nodecmd
import (
"fmt"

"path/filepath"

"github.com/ava-labs/avalanche-cli/pkg/binutils"
"github.com/ava-labs/avalanche-cli/pkg/cobrautils"
"github.com/ava-labs/avalanche-cli/pkg/networkoptions"
"github.com/ava-labs/avalanche-cli/pkg/node"
Expand Down Expand Up @@ -73,7 +76,7 @@ status by running avalanche node status local
}
networkoptions.AddNetworkFlagsToCmd(cmd, &globalNetworkFlags, false, createSupportedNetworkOptions)
cmd.Flags().BoolVar(&useLatestAvalanchegoReleaseVersion, "latest-avalanchego-version", false, "install latest avalanchego release version on node/s")
cmd.Flags().BoolVar(&useLatestAvalanchegoPreReleaseVersion, "latest-avalanchego-pre-release-version", false, "install latest avalanchego pre-release version on node/s")
cmd.Flags().BoolVar(&useLatestAvalanchegoPreReleaseVersion, "latest-avalanchego-pre-release-version", true, "install latest avalanchego pre-release version on node/s")
cmd.Flags().StringVar(&useCustomAvalanchegoVersion, "custom-avalanchego-version", "", "install given avalanchego version on node/s")
cmd.Flags().StringVar(&avalanchegoBinaryPath, "avalanchego-path", "", "use this avalanchego binary path")
cmd.Flags().StringArrayVar(&bootstrapIDs, "bootstrap-id", []string{}, "nodeIDs of bootstrap nodes")
Expand Down Expand Up @@ -108,6 +111,9 @@ func newLocalTrackCmd() *cobra.Command {
RunE: localTrack,
}
cmd.Flags().StringVar(&avalanchegoBinaryPath, "avalanchego-path", "", "use this avalanchego binary path")
cmd.Flags().BoolVar(&useLatestAvalanchegoReleaseVersion, "latest-avalanchego-version", false, "install latest avalanchego release version on node/s")
cmd.Flags().BoolVar(&useLatestAvalanchegoPreReleaseVersion, "latest-avalanchego-pre-release-version", true, "install latest avalanchego pre-release version on node/s")
cmd.Flags().StringVar(&useCustomAvalanchegoVersion, "custom-avalanchego-version", "", "install given avalanchego version on node/s")
return cmd
}

Expand Down Expand Up @@ -147,11 +153,14 @@ func localStartNode(_ *cobra.Command, args []string) error {
StakingCertKeyPath: stakingCertKeyPath,
StakingTLSKeyPath: stakingTLSKeyPath,
}
if useCustomAvalanchegoVersion != "" {
useLatestAvalanchegoReleaseVersion = false
useLatestAvalanchegoPreReleaseVersion = false
}
avaGoVersionSetting := node.AvalancheGoVersionSettings{
UseCustomAvalanchegoVersion: useCustomAvalanchegoVersion,
UseLatestAvalanchegoPreReleaseVersion: useLatestAvalanchegoPreReleaseVersion,
UseLatestAvalanchegoReleaseVersion: useLatestAvalanchegoReleaseVersion,
UseAvalanchegoVersionFromSubnet: useAvalanchegoVersionFromSubnet,
}
var (
err error
Expand Down Expand Up @@ -189,6 +198,26 @@ func localDestroyNode(_ *cobra.Command, args []string) error {
}

func localTrack(_ *cobra.Command, args []string) error {
if avalanchegoBinaryPath == "" {
if useCustomAvalanchegoVersion != "" {
useLatestAvalanchegoReleaseVersion = false
useLatestAvalanchegoPreReleaseVersion = false
}
avaGoVersionSetting := node.AvalancheGoVersionSettings{
UseCustomAvalanchegoVersion: useCustomAvalanchegoVersion,
UseLatestAvalanchegoPreReleaseVersion: useLatestAvalanchegoPreReleaseVersion,
UseLatestAvalanchegoReleaseVersion: useLatestAvalanchegoReleaseVersion,
}
avalancheGoVersion, err := node.GetAvalancheGoVersion(app, avaGoVersionSetting)
if err != nil {
return err
}
_, avagoDir, err := binutils.SetupAvalanchego(app, avalancheGoVersion)
if err != nil {
return fmt.Errorf("failed installing Avalanche Go version %s: %w", avalancheGoVersion, err)
}
avalanchegoBinaryPath = filepath.Join(avagoDir, "avalanchego")
}
return node.TrackSubnetWithLocalMachine(app, args[0], args[1], avalanchegoBinaryPath)
}

Expand Down
4 changes: 2 additions & 2 deletions pkg/constants/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,8 @@ const (
AWSGP3DefaultIOPS = 3000
AWSGP3DefaultThroughput = 125
SimulatePublicNetwork = "SIMULATE_PUBLIC_NETWORK"

OperateOfflineEnvVarName = "CLIOFFLINE"
LatestAvalancheGoVersion = "latest"
OperateOfflineEnvVarName = "CLIOFFLINE"

PublicAccess HTTPAccess = true
PrivateAccess HTTPAccess = false
Expand Down
15 changes: 8 additions & 7 deletions pkg/node/local.go
Original file line number Diff line number Diff line change
Expand Up @@ -208,9 +208,13 @@ func StartLocalNode(
avalancheGoVersion, err = GetAvalancheGoVersion(app, avaGoVersionSetting)
if err != nil {
return err
} else {
ux.Logger.PrintToUser("Using AvalancheGo version: %s", avalancheGoVersion)
}
_, avagoDir, err := binutils.SetupAvalanchego(app, avalancheGoVersion)
if err != nil {
return fmt.Errorf("failed installing Avalanche Go version %s: %w", avalancheGoVersion, err)
}
avalanchegoBinaryPath = filepath.Join(avagoDir, "avalanchego")
ux.Logger.PrintToUser("Using AvalancheGo version: %s", avalancheGoVersion)
}
serverLogPath := filepath.Join(rootDir, "server.log")
sd := subnet.NewLocalDeployer(app, avalancheGoVersion, avalanchegoBinaryPath, "", true)
Expand Down Expand Up @@ -304,7 +308,7 @@ func StartLocalNode(
return err
}
}
if err := preLocalChecks(anrSettings, avaGoVersionSetting, avalanchegoBinaryPath, useEtnaDevnet, globalNetworkFlags); err != nil {
if err := preLocalChecks(anrSettings, avaGoVersionSetting, useEtnaDevnet, globalNetworkFlags); err != nil {
return err
}
if useEtnaDevnet {
Expand Down Expand Up @@ -422,7 +426,7 @@ func localClusterDataExists(app *application.Avalanche, clusterName string) bool
}

// stub for now
func preLocalChecks(anrSettings ANRSettings, avaGoVersionSettings AvalancheGoVersionSettings, avalanchegoBinaryPath string, useEtnaDevnet bool, globalNetworkFlags networkoptions.NetworkFlags) error {
func preLocalChecks(anrSettings ANRSettings, avaGoVersionSettings AvalancheGoVersionSettings, useEtnaDevnet bool, globalNetworkFlags networkoptions.NetworkFlags) error {
// expand passed paths
if anrSettings.GenesisPath != "" {
anrSettings.GenesisPath = utils.ExpandHome(anrSettings.GenesisPath)
Expand All @@ -434,9 +438,6 @@ func preLocalChecks(anrSettings ANRSettings, avaGoVersionSettings AvalancheGoVer
if avaGoVersionSettings.UseCustomAvalanchegoVersion != "" && (avaGoVersionSettings.UseLatestAvalanchegoReleaseVersion || avaGoVersionSettings.UseLatestAvalanchegoPreReleaseVersion) {
return fmt.Errorf("specify either --custom-avalanchego-version or --latest-avalanchego-version")
}
if avalanchegoBinaryPath != "" && (avaGoVersionSettings.UseLatestAvalanchegoReleaseVersion || avaGoVersionSettings.UseLatestAvalanchegoPreReleaseVersion || avaGoVersionSettings.UseCustomAvalanchegoVersion != "") {
return fmt.Errorf("specify either --avalanchego-path or --latest-avalanchego-version or --custom-avalanchego-version")
}
if useEtnaDevnet && (globalNetworkFlags.UseDevnet || globalNetworkFlags.UseFuji) {
return fmt.Errorf("etna devnet can only be used with devnet")
}
Expand Down
76 changes: 76 additions & 0 deletions pkg/subnet/local_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,82 @@ func setupTest(t *testing.T) *require.Assertions {
return require.New(t)
}

func TestDeployToLocal(t *testing.T) {
require := setupTest(t)
avagoVersion := "v1.18.0"

// fake-return true simulating the process is running
procChecker := &mocks.ProcessChecker{}
procChecker.On("IsServerProcessRunning", mock.Anything, mock.Anything).Return(true, nil)

tmpDir := os.TempDir()
testDir, err := os.MkdirTemp(tmpDir, "local-test")
require.NoError(err)
defer func() {
err = os.RemoveAll(testDir)
require.NoError(err)
}()

app := &application.Avalanche{}
app.Setup(testDir, logging.NoLog{}, config.New(), prompts.NewPrompter(), application.NewDownloader())

binDir := filepath.Join(app.GetAvalanchegoBinDir(), "avalanchego-"+avagoVersion)

// create a dummy plugins dir, deploy will check it exists
binChecker := &mocks.BinaryChecker{}
err = os.MkdirAll(filepath.Join(binDir, "plugins"), perms.ReadWriteExecute)
require.NoError(err)

// create a dummy avalanchego file, deploy will check it exists
f, err := os.Create(filepath.Join(binDir, "avalanchego"))
require.NoError(err)
defer func() {
_ = f.Close()
}()

binChecker.On("ExistsWithLatestVersion", mock.AnythingOfType("string"), mock.AnythingOfType("string")).Return(true, tmpDir, nil)

binDownloader := &mocks.PluginBinaryDownloader{}
binDownloader.On("Download", mock.Anything, mock.AnythingOfType("string"), mock.AnythingOfType("string")).Return(nil)
binDownloader.On("InstallVM", mock.Anything, mock.Anything, mock.Anything).Return(nil)

testDeployer := &LocalDeployer{
procChecker: procChecker,
binChecker: binChecker,
getClientFunc: getTestClientFunc,
binaryDownloader: binDownloader,
app: app,
setDefaultSnapshot: fakeSetDefaultSnapshot,
avagoVersion: avagoVersion,
}

// create a simple genesis for the test
genesis := `{"config":{"chainId":9999},"gasLimit":"0x0","difficulty":"0x0","alloc":{}}`
// create a dummy genesis file, deploy will check it exists
testGenesis, err := os.CreateTemp(tmpDir, "test-genesis.json")
require.NoError(err)
err = os.WriteFile(testGenesis.Name(), []byte(genesis), constants.DefaultPerms755)
require.NoError(err)
// create dummy sidecar file, also checked by deploy
sidecar := `{"VM": "SubnetEVM"}`
testSubnetDir := filepath.Join(testDir, constants.SubnetDir, testChainName)
err = os.MkdirAll(testSubnetDir, constants.DefaultPerms755)
require.NoError(err)
testSidecar, err := os.Create(filepath.Join(testSubnetDir, constants.SidecarFileName))
require.NoError(err)
err = os.WriteFile(testSidecar.Name(), []byte(sidecar), constants.DefaultPerms755)
require.NoError(err)
// test actual deploy
icmSpec := ICMSpec{
SkipICMDeploy: true,
}
deployInfo, err := testDeployer.DeployToLocalNetwork(testChainName, testGenesis.Name(), icmSpec, "", "")
require.NoError(err)
require.Equal(testSubnetID2, deployInfo.SubnetID.String())
require.Equal(testBlockChainID2, deployInfo.BlockchainID.String())
}

>>>>>>> main

Check failure on line 99 in pkg/subnet/local_test.go

View workflow job for this annotation

GitHub Actions / Lint

syntax error: non-declaration statement outside function body (typecheck)

Check failure on line 99 in pkg/subnet/local_test.go

View workflow job for this annotation

GitHub Actions / Lint

expected declaration, found '>>' (typecheck)

Check failure on line 99 in pkg/subnet/local_test.go

View workflow job for this annotation

GitHub Actions / Golang Unit Tests (ubuntu-22.04)

expected declaration, found '>>'

Check failure on line 99 in pkg/subnet/local_test.go

View workflow job for this annotation

GitHub Actions / Golang Unit Tests (macos-14)

expected declaration, found '>>'
func TestGetLatestAvagoVersion(t *testing.T) {
require := setupTest(t)

Expand Down

0 comments on commit ba6cf0c

Please sign in to comment.