Skip to content

Commit

Permalink
fix lint
Browse files Browse the repository at this point in the history
  • Loading branch information
sukantoraymond committed Aug 23, 2024
1 parent a3da517 commit de74680
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 9 deletions.
10 changes: 5 additions & 5 deletions keychain/keychain.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import (

type Keychain struct {
keychain.Keychain
network avalanche.Network
Network avalanche.Network
Ledger *Ledger
}

Expand Down Expand Up @@ -69,7 +69,7 @@ func NewKeychain(
Ledger: &Ledger{
LedgerDevice: dev,
},
network: network,
Network: network,
}
if ledgerInfo.RequiredFunds > 0 {
if err := kc.AddLedgerFunds(ledgerInfo.RequiredFunds); err != nil {
Expand All @@ -92,14 +92,14 @@ func NewKeychain(
}
kc := Keychain{
Keychain: sf.KeyChain(),
network: network,
Network: network,
}
return &kc, nil
}

// P returns string formatted addresses in the keychain
func (kc *Keychain) P() ([]string, error) {
return utils.P(kc.network.HRP(), kc.Addresses().List())
return utils.P(kc.Network.HRP(), kc.Addresses().List())
}

func (kc *Keychain) LedgerEnabled() bool {
Expand Down Expand Up @@ -133,7 +133,7 @@ func (kc *Keychain) AddLedgerAddresses(addresses []string) error {

func (kc *Keychain) AddLedgerFunds(amount uint64) error {
if kc.LedgerEnabled() {
indices, err := kc.Ledger.LedgerDevice.FindFunds(kc.network, amount, 0)
indices, err := kc.Ledger.LedgerDevice.FindFunds(kc.Network, amount, 0)
if err != nil {
return err
}
Expand Down
34 changes: 30 additions & 4 deletions subnet/add_validator_subnet.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ import (
"fmt"
"time"

"github.com/ava-labs/avalanche-tooling-sdk-go/avalanche"
"github.com/ava-labs/avalanche-tooling-sdk-go/utils"
"github.com/ava-labs/avalanchego/vms/platformvm"

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

"golang.org/x/net/context"
Expand All @@ -19,10 +23,11 @@ import (
)

var (
ErrEmptyValidatorNodeID = errors.New("validator node id is not provided")
ErrEmptyValidatorDuration = errors.New("validator duration is not provided")
ErrEmptySubnetID = errors.New("subnet ID is not provided")
ErrEmptySubnetAuth = errors.New("no subnet auth keys is provided")
ErrEmptyValidatorNodeID = errors.New("validator node id is not provided")
ErrEmptyValidatorDuration = errors.New("validator duration is not provided")
ErrEmptySubnetID = errors.New("subnet ID is not provided")
ErrEmptySubnetAuth = errors.New("no subnet auth keys is provided")
ErrNodeNotValidatingPrimaryNetwork = errors.New("node is currently not a Primary Network validator")
)

// AddValidator adds validator to subnet
Expand Down Expand Up @@ -56,6 +61,14 @@ func (c *Subnet) AddValidator(wallet wallet.Wallet, validatorInput validator.Sub
Subnet: c.SubnetID,
}

validatingPrimaryNetwork, err := CheckNodeIsPrimaryNetworkValidator(validatorInput.NodeID, wallet.Keychain.Network)
if err != nil {
return nil, fmt.Errorf("error checking if node is validating Primary Network due to %w", err)
}
if !validatingPrimaryNetwork {
return nil, ErrNodeNotValidatingPrimaryNetwork
}

unsignedTx, err := wallet.P().Builder().NewAddSubnetValidatorTx(validator)
if err != nil {
return nil, fmt.Errorf("error building tx: %w", err)
Expand All @@ -66,3 +79,16 @@ func (c *Subnet) AddValidator(wallet wallet.Wallet, validatorInput validator.Sub
}
return multisig.New(&tx), nil
}

func CheckNodeIsPrimaryNetworkValidator(nodeID ids.NodeID, network avalanche.Network) (bool, error) {
pClient := platformvm.NewClient(network.Endpoint)
ctx, cancel := utils.GetAPIContext()
defer cancel()

vals, err := pClient.GetCurrentValidators(ctx, ids.Empty, []ids.NodeID{nodeID})
if err != nil {
return false, fmt.Errorf("failed to get current validators")
}

return !(len(vals) == 0), nil
}

0 comments on commit de74680

Please sign in to comment.