Skip to content

Commit

Permalink
update example
Browse files Browse the repository at this point in the history
  • Loading branch information
sukantoraymond committed Aug 23, 2024
1 parent ce57100 commit e17cde5
Show file tree
Hide file tree
Showing 4 changed files with 91 additions and 28 deletions.
9 changes: 0 additions & 9 deletions examples/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
76 changes: 76 additions & 0 deletions examples/node_validate_primary.go
Original file line number Diff line number Diff line change
@@ -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())
}
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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",
Expand All @@ -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},
}

Expand Down
25 changes: 9 additions & 16 deletions node/add_validator_primary_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ package node
import (
"context"
"fmt"
"github.com/stretchr/testify/require"

Check failure on line 9 in node/add_validator_primary_test.go

View workflow job for this annotation

GitHub Actions / Lint

File is not `gofumpt`-ed (gofumpt)
"testing"
"time"

Check failure on line 11 in node/add_validator_primary_test.go

View workflow job for this annotation

GitHub Actions / Lint

File is not `gofumpt`-ed (gofumpt)

Check failure on line 12 in node/add_validator_primary_test.go

View workflow job for this annotation

GitHub Actions / Lint

File is not `goimports`-ed (goimports)
Expand All @@ -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",
Expand All @@ -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,
Expand All @@ -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(),
Expand All @@ -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())
}

0 comments on commit e17cde5

Please sign in to comment.