Skip to content

Commit

Permalink
update example
Browse files Browse the repository at this point in the history
  • Loading branch information
sukantoraymond committed Aug 22, 2024
1 parent 2041101 commit 424a6f3
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 39 deletions.
14 changes: 14 additions & 0 deletions avalanche/network.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,11 @@
package avalanche

import (
"github.com/ava-labs/avalanche-tooling-sdk-go/utils"
"github.com/ava-labs/avalanchego/genesis"
"github.com/ava-labs/avalanchego/ids"
"github.com/ava-labs/avalanchego/utils/constants"
"github.com/ava-labs/avalanchego/vms/platformvm"
)

type NetworkKind int64
Expand Down Expand Up @@ -90,3 +93,14 @@ func (n Network) GenesisParams() *genesis.Params {
}
return nil
}

func (n Network) GetMinStakingAmount() (uint64, error) {
pClient := platformvm.NewClient(n.Endpoint)
ctx, cancel := utils.GetAPIContext()
defer cancel()
minValStake, _, err := pClient.GetMinStake(ctx, ids.Empty)
if err != nil {
return 0, err
}
return minValStake, nil
}
50 changes: 12 additions & 38 deletions node/add_validator_primary.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ package node

import (
"fmt"
"os"
"time"

remoteconfig "github.com/ava-labs/avalanche-tooling-sdk-go/node/config"
Expand All @@ -16,7 +15,6 @@ import (
"github.com/ava-labs/avalanche-tooling-sdk-go/avalanche"
"github.com/ava-labs/avalanche-tooling-sdk-go/utils"
"github.com/ava-labs/avalanchego/utils/crypto/bls"
"github.com/ava-labs/avalanchego/vms/platformvm"
"github.com/ava-labs/avalanchego/vms/platformvm/signer"
"github.com/ava-labs/avalanchego/vms/secp256k1fx"
"github.com/ava-labs/avalanchego/wallet/subnet/primary/common"
Expand Down Expand Up @@ -52,32 +50,32 @@ type PrimaryNetworkValidatorParams struct {
// and uses the wallet provided in the argument to pay for the transaction fee
func (h *Node) ValidatePrimaryNetwork(
network avalanche.Network,
validator PrimaryNetworkValidatorParams,
validatorParams PrimaryNetworkValidatorParams,
wallet wallet.Wallet,
) (ids.ID, error) {
if validator.NodeID == ids.EmptyNodeID {
if validatorParams.NodeID == ids.EmptyNodeID {
return ids.Empty, subnet.ErrEmptyValidatorNodeID
}

if validator.Duration == 0 {
if validatorParams.Duration == 0 {
return ids.Empty, subnet.ErrEmptyValidatorDuration
}

minValStake, err := GetMinStakingAmount(network)
minValStake, err := network.GetMinStakingAmount()
if err != nil {
return ids.Empty, err
}

if validator.StakeAmount < minValStake {
return ids.Empty, fmt.Errorf("invalid weight, must be greater than or equal to %d: %d", minValStake, validator.StakeAmount)
if validatorParams.StakeAmount < minValStake {
return ids.Empty, fmt.Errorf("invalid weight, must be greater than or equal to %d: %d", minValStake, validatorParams.StakeAmount)
}

if validator.DelegationFee == 0 {
validator.DelegationFee = network.GenesisParams().MinDelegationFee
if validatorParams.DelegationFee == 0 {
validatorParams.DelegationFee = network.GenesisParams().MinDelegationFee
}

if err = h.GetBLSKeyFromRemoteHost(); err != nil {
return ids.Empty, fmt.Errorf("unable to set BLS key of node due to %w", err)
return ids.Empty, fmt.Errorf("unable to set BLS key of node from remote host due to %w", err)
}

wallet.SetSubnetAuthMultisig([]ids.ShortID{})
Expand All @@ -99,16 +97,16 @@ func (h *Node) ValidatePrimaryNetwork(
&txs.SubnetValidator{
Validator: txs.Validator{
NodeID: nodeID,
End: uint64(time.Now().Add(validator.Duration).Unix()),
Wght: validator.StakeAmount,
End: uint64(time.Now().Add(validatorParams.Duration).Unix()),
Wght: validatorParams.StakeAmount,
},
Subnet: ids.Empty,
},
proofOfPossession,
wallet.P().Builder().Context().AVAXAssetID,
owner,
owner,
validator.DelegationFee,
validatorParams.DelegationFee,
)
if err != nil {
return ids.Empty, fmt.Errorf("error building tx: %w", err)
Expand Down Expand Up @@ -137,30 +135,6 @@ func (h *Node) ValidatePrimaryNetwork(
return tx.ID(), nil
}

func GetMinStakingAmount(network avalanche.Network) (uint64, error) {
pClient := platformvm.NewClient(network.Endpoint)
ctx, cancel := utils.GetAPIContext()
defer cancel()
minValStake, _, err := pClient.GetMinStake(ctx, ids.Empty)
if err != nil {
return 0, err
}
return minValStake, nil
}

func (h *Node) SetNodeBLSKey(signingKeyPath string) error {
blsKeyBytes, err := os.ReadFile(signingKeyPath)
if err != nil {
return err
}
blsSk, err := bls.SecretKeyFromBytes(blsKeyBytes)
if err != nil {
return err
}
h.BlsSecretKey = blsSk
return nil
}

// GetBLSKeyFromRemoteHost gets BLS information from remote host and sets the BlsSecretKey value in Node object
func (h *Node) GetBLSKeyFromRemoteHost() error {
blsKeyBytes, err := h.ReadFileBytes(remoteconfig.GetRemoteBLSKeyFile(), constants.SSHFileOpsTimeout)
Expand Down
1 change: 0 additions & 1 deletion node/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,6 @@ type Node struct {
Logger avalanche.LeveledLogger

// BLS provides a way to aggregate signatures off chain into a single signature that can be efficiently verified on chain.
// To set BlsSecretKey of a node, use SetNodeBLSKey
// For more information about how BLS is used on the P-Chain, please head to https://docs.avax.network/cross-chain/avalanche-warp-messaging/deep-dive#bls-multi-signatures-with-public-key-aggregation
BlsSecretKey *bls.SecretKey
}
Expand Down

0 comments on commit 424a6f3

Please sign in to comment.