diff --git a/examples/node.go b/examples/node.go index 01d13fa..ed1a457 100644 --- a/examples/node.go +++ b/examples/node.go @@ -102,15 +102,6 @@ func CreateNodes() { } } - // examle of how to reconfigure the created nodes to track a subnet - subnetIDsToValidate := []string{"xxxxxxxxxxxxxxxxxxxyyyyyyyyyyyyyyyzzzzzzzzzzzzzzz"} - for _, h := range hosts { - fmt.Printf("Reconfiguring node %s to track subnet %s", h.NodeID, subnetIDsToValidate) - if err := h.SyncSubnets(subnetIDsToValidate); err != nil { - panic(err) - } - } - // Create a monitoring node. // Monitoring node enables you to have a centralized Grafana Dashboard where you can view // metrics relevant to any Validator & API nodes that the monitoring node is linked to as well diff --git a/examples/node_validate_primary.go b/examples/node_validate_primary.go new file mode 100644 index 0000000..ea108d9 --- /dev/null +++ b/examples/node_validate_primary.go @@ -0,0 +1,76 @@ +// Copyright (C) 2024, Ava Labs, Inc. All rights reserved. +// See the file LICENSE for licensing terms. + +package examples + +import ( + "context" + "fmt" + "github.com/ava-labs/avalanche-tooling-sdk-go/constants" + "github.com/ava-labs/avalanche-tooling-sdk-go/keychain" + "github.com/ava-labs/avalanche-tooling-sdk-go/validator" + "github.com/ava-labs/avalanche-tooling-sdk-go/wallet" + "github.com/ava-labs/avalanchego/ids" + "github.com/ava-labs/avalanchego/utils/units" + "github.com/ava-labs/avalanchego/vms/secp256k1fx" + "github.com/ava-labs/avalanchego/wallet/subnet/primary" + "time" + + "github.com/ava-labs/avalanche-tooling-sdk-go/avalanche" + "github.com/ava-labs/avalanche-tooling-sdk-go/node" +) + +func ValidatePrimaryNetwork() { + node := node.Node{ + // NodeID is Avalanche Node ID of the node + NodeID: "NODE_ID", + // IP address of the node + IP: "NODE_IP_ADDRESS", + // SSH configuration for the node + SSHConfig: node.SSHConfig{ + User: constants.RemoteHostUser, + PrivateKeyPath: "NODE_KEYPAIR_PRIVATE_KEY_PATH", + }, + // Role of the node can be Validator, API, AWMRelayer, Loadtest, or Monitor + Roles: []node.SupportedRole{node.Validator}, + } + + nodeID, err := ids.NodeIDFromString(node.NodeID) + if err != nil { + panic(err) + } + + validatorParams := validator.PrimaryNetworkValidatorParams{ + NodeID: nodeID, + // Validate Primary Network for 48 hours + Duration: 48 * time.Hour, + // Stake 2 AVAX + StakeAmount: 2 * units.Avax, + } + + // Key that will be used for paying the transaction fee of AddValidator Tx + network := avalanche.FujiNetwork() + keychain, err := keychain.NewKeychain(network, "PRIVATE_KEY_FILEPATH", nil) + if err != nil { + panic(err) + } + + wallet, err := wallet.New( + context.Background(), + &primary.WalletConfig{ + URI: network.Endpoint, + AVAXKeychain: keychain.Keychain, + EthKeychain: secp256k1fx.NewKeychain(), + PChainTxsToFetch: nil, + }, + ) + if err != nil { + panic(err) + } + + txID, err := node.ValidatePrimaryNetwork(avalanche.FujiNetwork(), validatorParams, wallet) + if err != nil { + panic(err) + } + fmt.Printf("obtained tx id %s", txID.String()) +} diff --git a/examples/subnet_ addValidator.go b/examples/subnet_ add_validator.go similarity index 92% rename from examples/subnet_ addValidator.go rename to examples/subnet_ add_validator.go index 07e45a0..1cfba70 100644 --- a/examples/subnet_ addValidator.go +++ b/examples/subnet_ add_validator.go @@ -21,6 +21,7 @@ import ( ) func AddSubnetValidator() { + // We are using existing Subnet that we have already deployed on Fuji subnetParams := subnet.SubnetParams{ GenesisFilePath: "GENESIS_FILE_PATH", Name: "SUBNET_NAME", @@ -35,8 +36,11 @@ func AddSubnetValidator() { if err != nil { panic(err) } + + // Genesis doesn't contain the deployed Subnet's SubnetID, we need to first set the Subnet ID newSubnet.SetSubnetID(subnetID) + // We are using existing host node := node.Node{ // NodeID is Avalanche Node ID of the node NodeID: "NODE_ID", @@ -47,9 +51,8 @@ func AddSubnetValidator() { User: constants.RemoteHostUser, PrivateKeyPath: "NODE_KEYPAIR_PRIVATE_KEY_PATH", }, - // Cloud is the cloud service that the node is on - Cloud: node.AWSCloud, - + // Role is the role that we expect the host to be (Validator, API, AWMRelayer, Loadtest or + // Monitor) Roles: []node.SupportedRole{node.Validator}, } diff --git a/node/add_validator_primary_test.go b/node/add_validator_primary_test.go index f4889f9..c070232 100644 --- a/node/add_validator_primary_test.go +++ b/node/add_validator_primary_test.go @@ -6,6 +6,7 @@ package node import ( "context" "fmt" + "github.com/stretchr/testify/require" "testing" "time" @@ -21,7 +22,9 @@ import ( "github.com/ava-labs/avalanchego/wallet/subnet/primary" ) -func TestNodesValidatePrimaryNetwork(_ *testing.T) { +func TestNodesValidatePrimaryNetwork(t *testing.T) { + require := require.New(t) + // We are using an existing host node := Node{ // NodeID is Avalanche Node ID of the node NodeID: "NODE_ID", @@ -32,16 +35,12 @@ func TestNodesValidatePrimaryNetwork(_ *testing.T) { User: constants.RemoteHostUser, PrivateKeyPath: "NODE_KEYPAIR_PRIVATE_KEY_PATH", }, - // Cloud is the cloud service that the node is on - Cloud: AWSCloud, // Role of the node can be Validator, API, AWMRelayer, Loadtest, or Monitor Roles: []SupportedRole{Validator}, } nodeID, err := ids.NodeIDFromString(node.NodeID) - if err != nil { - panic(err) - } + require.NoError(err) validator := validator.PrimaryNetworkValidatorParams{ NodeID: nodeID, @@ -52,11 +51,8 @@ func TestNodesValidatePrimaryNetwork(_ *testing.T) { } network := avalanche.FujiNetwork() - keychain, err := keychain.NewKeychain(network, "PRIVATE_KEY_FILEPATH", nil) - if err != nil { - panic(err) - } + require.NoError(err) wallet, err := wallet.New( context.Background(), @@ -67,13 +63,10 @@ func TestNodesValidatePrimaryNetwork(_ *testing.T) { PChainTxsToFetch: nil, }, ) - if err != nil { - panic(err) - } + require.NoError(err) txID, err := node.ValidatePrimaryNetwork(avalanche.FujiNetwork(), validator, wallet) - if err != nil { - panic(err) - } + require.NoError(err) + fmt.Printf("obtained tx id %s", txID.String()) }