Skip to content

Commit

Permalink
Merge pull request #120 from ava-labs/add-subnet-validator-example
Browse files Browse the repository at this point in the history
Add subnet validator example
  • Loading branch information
sukantoraymond authored Aug 23, 2024
2 parents 424a6f3 + a3da517 commit 2aecb5a
Show file tree
Hide file tree
Showing 14 changed files with 401 additions and 118 deletions.
4 changes: 0 additions & 4 deletions avalanche/log.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,6 @@ import (
"os"
)

//
// Public constants
//

const (
// LevelNull sets a logger to show no messages at all.
LevelNull Level = 0
Expand Down
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.Println("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
78 changes: 78 additions & 0 deletions examples/node_validate_primary.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
// Copyright (C) 2024, Ava Labs, Inc. All rights reserved.
// See the file LICENSE for licensing terms.

package examples

import (
"context"
"fmt"
"time"

"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"

"github.com/ava-labs/avalanche-tooling-sdk-go/avalanche"
"github.com/ava-labs/avalanche-tooling-sdk-go/node"
)

func ValidatePrimaryNetwork() {
// We are using existing host
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())
}
8 changes: 4 additions & 4 deletions examples/subnet.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ func DeploySubnet() {
// can be committed on chain
subnetAuthKeys := keychain.Addresses().List()
threshold := 1
newSubnet.SetSubnetCreateParams(controlKeys, uint32(threshold))
newSubnet.SetSubnetControlParams(controlKeys, uint32(threshold))

wallet, _ := wallet.New(
context.Background(),
Expand All @@ -86,7 +86,7 @@ func DeploySubnet() {
// we need to wait to allow the transaction to reach other nodes in Fuji
time.Sleep(2 * time.Second)

newSubnet.SetBlockchainCreateParams(subnetAuthKeys)
newSubnet.SetSubnetAuthKeys(subnetAuthKeys)
deployChainTx, _ := newSubnet.CreateBlockchainTx(wallet)
// since we are using the fee paying key as control key too, we can commit the transaction
// on chain immediately since the number of signatures has been reached
Expand Down Expand Up @@ -139,7 +139,7 @@ func DeploySubnetWithLedger() {
controlKeys := addressesIDs
subnetAuthKeys := addressesIDs
threshold := 1
newSubnet.SetSubnetCreateParams(controlKeys, uint32(threshold))
newSubnet.SetSubnetControlParams(controlKeys, uint32(threshold))

// Pay and Sign CreateSubnet Tx with fee paying key A using Ledger
deploySubnetTx, _ := newSubnet.CreateSubnetTx(walletA)
Expand All @@ -149,7 +149,7 @@ func DeploySubnetWithLedger() {
// we need to wait to allow the transaction to reach other nodes in Fuji
time.Sleep(2 * time.Second)

newSubnet.SetBlockchainCreateParams(subnetAuthKeys)
newSubnet.SetSubnetAuthKeys(subnetAuthKeys)

// Pay and sign CreateChain Tx with fee paying key A using Ledger
deployChainTx, _ := newSubnet.CreateBlockchainTx(walletA)
Expand Down
125 changes: 125 additions & 0 deletions examples/subnet_ add_validator.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
// 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/avalanche"
"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/node"
"github.com/ava-labs/avalanche-tooling-sdk-go/subnet"
"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/set"
"github.com/ava-labs/avalanchego/vms/secp256k1fx"
"github.com/ava-labs/avalanchego/wallet/subnet/primary"
"time"
)

func AddSubnetValidator() {
// We are using existing Subnet that we have already deployed on Fuji
subnetParams := subnet.SubnetParams{
GenesisFilePath: "GENESIS_FILE_PATH",
Name: "SUBNET_NAME",
}

newSubnet, err := subnet.New(&subnetParams)
if err != nil {
panic(err)
}

subnetID, err := ids.FromString("SUBNET_ID")
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",
// 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 is the role that we expect the host to be (Validator, API, AWMRelayer, Loadtest or
// Monitor)
Roles: []node.SupportedRole{node.Validator},
}

// Here we are assuming that the node is currently validating the Primary Network, which is
// a requirement before the node can start validating a Subnet.
// To have a node validate the Primary Network, call node.ValidatePrimaryNetwork
// Now we are calling the node to start tracking the Subnet
subnetIDsToValidate := []string{newSubnet.SubnetID.String()}
if err := node.SyncSubnets(subnetIDsToValidate); err != nil {
panic(err)
}

// Node is now tracking the Subnet

// Key that will be used for paying the transaction fees of Subnet AddValidator Tx
//
// In our example, this Key is also the control Key to the Subnet, so we are going to use
// this key to also sign the Subnet 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: set.Of(subnetID),
},
)
if err != nil {
panic(err)
}

nodeID, err := ids.NodeIDFromString(node.NodeID)
if err != nil {
panic(err)
}

validatorParams := validator.SubnetValidatorParams{
NodeID: nodeID,
// Validate Subnet for 48 hours
Duration: 48 * time.Hour,
Weight: 20,
}

// We need to set Subnet Auth Keys for this transaction since Subnet AddValidator is
// a Subnet-changing transaction
//
// In this example, the example Subnet was created with only 1 key as control key with a threshold of 1
// and the control key is the key contained in the keychain object, so we are going to use the
// key contained in the keychain object as the Subnet Auth Key for Subnet AddValidator tx
subnetAuthKeys := keychain.Addresses().List()
newSubnet.SetSubnetAuthKeys(subnetAuthKeys)

addValidatorTx, err := newSubnet.AddValidator(wallet, validatorParams)
if err != nil {
panic(err)
}

// Since it has the required signatures, we will now commit the transaction on chain
txID, err := newSubnet.Commit(*addValidatorTx, wallet, true)
if err != nil {
panic(err)
}
fmt.Printf("obtained tx id %s", txID.String())
}
10 changes: 5 additions & 5 deletions examples/subnet_ multisig.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,9 @@ func DeploySubnetMultiSig() {
// Create three keys that will be used as control keys of the subnet
// NewKeychain will generate a new key pair in the provided path if no .pk file currently
// exists in the provided path
keychainA, _ := keychain.NewKeychain(network, "KEY_PATH_A")
keychainB, _ := keychain.NewKeychain(network, "KEY_PATH_B")
keychainC, _ := keychain.NewKeychain(network, "KEY_PATH_C")
keychainA, _ := keychain.NewKeychain(network, "KEY_PATH_A", nil)
keychainB, _ := keychain.NewKeychain(network, "KEY_PATH_B", nil)
keychainC, _ := keychain.NewKeychain(network, "KEY_PATH_C", nil)

// In this example, we are using the fee-paying key generated above also as control key
// and subnet auth key
Expand All @@ -55,7 +55,7 @@ func DeploySubnetMultiSig() {
// at least two signatures are required to be able to send the CreateChain transaction on-chain
// note that threshold does not apply to CreateSubnet transaction
threshold := 2
newSubnet.SetSubnetCreateParams(controlKeys, uint32(threshold))
newSubnet.SetSubnetControlParams(controlKeys, uint32(threshold))

// Key A will be used for paying the transaction fees of CreateSubnetTx and CreateChainTx
walletA, _ := wallet.New(
Expand All @@ -75,7 +75,7 @@ func DeploySubnetMultiSig() {
// we need to wait to allow the transaction to reach other nodes in Fuji
time.Sleep(2 * time.Second)

newSubnet.SetBlockchainCreateParams(subnetAuthKeys)
newSubnet.SetSubnetAuthKeys(subnetAuthKeys)
deployChainTx, err := newSubnet.CreateBlockchainTx(walletA)
if err != nil {
fmt.Errorf("error signing tx walletA: %w", err)
Expand Down
24 changes: 3 additions & 21 deletions node/add_validator_primary.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import (
"fmt"
"time"

"github.com/ava-labs/avalanche-tooling-sdk-go/validator"

remoteconfig "github.com/ava-labs/avalanche-tooling-sdk-go/node/config"

"github.com/ava-labs/avalanche-tooling-sdk-go/constants"
Expand All @@ -25,32 +27,12 @@ import (
"golang.org/x/net/context"
)

type PrimaryNetworkValidatorParams struct {
// NodeID is the unique identifier of the node to be added as a validator on the Primary Network.
NodeID ids.NodeID

// Duration is how long the node will be staking the Primary Network
// Duration has to be greater than or equal to minimum duration for the specified network
// (Fuji / Mainnet)
Duration time.Duration

// StakeAmount is the amount of Avalanche tokens (AVAX) to stake in this validator
// StakeAmount is in the amount of nAVAX
// StakeAmount has to be greater than or equal to minimum stake required for the specified network
StakeAmount uint64

// DelegationFee is the percent fee this validator will charge when others delegate stake to it
// When DelegationFee is not set, the minimum delegation fee for the specified network will be set
// For more information on delegation fee, please head to https://docs.avax.network/nodes/validate/node-validator#delegation-fee-rate
DelegationFee uint32
}

// ValidatePrimaryNetwork adds node as primary network validator.
// It adds the node in the specified network (Fuji / Mainnet / Devnet)
// and uses the wallet provided in the argument to pay for the transaction fee
func (h *Node) ValidatePrimaryNetwork(
network avalanche.Network,
validatorParams PrimaryNetworkValidatorParams,
validatorParams validator.PrimaryNetworkValidatorParams,
wallet wallet.Wallet,
) (ids.ID, error) {
if validatorParams.NodeID == ids.EmptyNodeID {
Expand Down
Loading

0 comments on commit 2aecb5a

Please sign in to comment.