github.com/ava-labs/avalanchego@v1.11.11/vms/platformvm/client_permissionless_validator.go (about)

     1  // Copyright (C) 2019-2024, Ava Labs, Inc. All rights reserved.
     2  // See the file LICENSE for licensing terms.
     3  
     4  package platformvm
     5  
     6  import (
     7  	"encoding/json"
     8  
     9  	"github.com/ava-labs/avalanchego/ids"
    10  	"github.com/ava-labs/avalanchego/utils/formatting/address"
    11  	"github.com/ava-labs/avalanchego/vms/platformvm/api"
    12  	"github.com/ava-labs/avalanchego/vms/platformvm/signer"
    13  )
    14  
    15  // ClientStaker is the representation of a staker sent via client.
    16  type ClientStaker struct {
    17  	// the txID of the transaction that added this staker.
    18  	TxID ids.ID
    19  	// the Unix time when they start staking
    20  	StartTime uint64
    21  	// the Unix time when they are done staking
    22  	EndTime uint64
    23  	// the validator weight when sampling validators
    24  	Weight uint64
    25  	// the amount of tokens being staked.
    26  	StakeAmount *uint64
    27  	// the node ID of the staker
    28  	NodeID ids.NodeID
    29  }
    30  
    31  // ClientOwner is the repr. of a reward owner sent over client
    32  type ClientOwner struct {
    33  	Locktime  uint64
    34  	Threshold uint32
    35  	Addresses []ids.ShortID
    36  }
    37  
    38  // ClientPermissionlessValidator is the repr. of a permissionless validator sent
    39  // over client
    40  type ClientPermissionlessValidator struct {
    41  	ClientStaker
    42  	ValidationRewardOwner  *ClientOwner
    43  	DelegationRewardOwner  *ClientOwner
    44  	PotentialReward        *uint64
    45  	AccruedDelegateeReward *uint64
    46  	DelegationFee          float32
    47  	// Uptime is deprecated for Subnet Validators.
    48  	// It will be available only for Primary Network Validators.
    49  	Uptime *float32
    50  	// Connected is deprecated for Subnet Validators.
    51  	// It will be available only for Primary Network Validators.
    52  	Connected *bool
    53  	Signer    *signer.ProofOfPossession
    54  	// The delegators delegating to this validator
    55  	DelegatorCount  *uint64
    56  	DelegatorWeight *uint64
    57  	Delegators      []ClientDelegator
    58  }
    59  
    60  // ClientDelegator is the repr. of a delegator sent over client
    61  type ClientDelegator struct {
    62  	ClientStaker
    63  	RewardOwner     *ClientOwner
    64  	PotentialReward *uint64
    65  }
    66  
    67  func apiStakerToClientStaker(validator api.Staker) ClientStaker {
    68  	return ClientStaker{
    69  		TxID:        validator.TxID,
    70  		StartTime:   uint64(validator.StartTime),
    71  		EndTime:     uint64(validator.EndTime),
    72  		Weight:      uint64(validator.Weight),
    73  		StakeAmount: (*uint64)(validator.StakeAmount),
    74  		NodeID:      validator.NodeID,
    75  	}
    76  }
    77  
    78  func apiOwnerToClientOwner(rewardOwner *api.Owner) (*ClientOwner, error) {
    79  	if rewardOwner == nil {
    80  		return nil, nil
    81  	}
    82  
    83  	addrs, err := address.ParseToIDs(rewardOwner.Addresses)
    84  	return &ClientOwner{
    85  		Locktime:  uint64(rewardOwner.Locktime),
    86  		Threshold: uint32(rewardOwner.Threshold),
    87  		Addresses: addrs,
    88  	}, err
    89  }
    90  
    91  func getClientPermissionlessValidators(validatorsSliceIntf []interface{}) ([]ClientPermissionlessValidator, error) {
    92  	clientValidators := make([]ClientPermissionlessValidator, len(validatorsSliceIntf))
    93  	for i, validatorMapIntf := range validatorsSliceIntf {
    94  		validatorMapJSON, err := json.Marshal(validatorMapIntf)
    95  		if err != nil {
    96  			return nil, err
    97  		}
    98  
    99  		var apiValidator api.PermissionlessValidator
   100  		err = json.Unmarshal(validatorMapJSON, &apiValidator)
   101  		if err != nil {
   102  			return nil, err
   103  		}
   104  
   105  		validationRewardOwner, err := apiOwnerToClientOwner(apiValidator.ValidationRewardOwner)
   106  		if err != nil {
   107  			return nil, err
   108  		}
   109  
   110  		delegationRewardOwner, err := apiOwnerToClientOwner(apiValidator.DelegationRewardOwner)
   111  		if err != nil {
   112  			return nil, err
   113  		}
   114  
   115  		var clientDelegators []ClientDelegator
   116  		if apiValidator.Delegators != nil {
   117  			clientDelegators = make([]ClientDelegator, len(*apiValidator.Delegators))
   118  			for j, apiDelegator := range *apiValidator.Delegators {
   119  				rewardOwner, err := apiOwnerToClientOwner(apiDelegator.RewardOwner)
   120  				if err != nil {
   121  					return nil, err
   122  				}
   123  
   124  				clientDelegators[j] = ClientDelegator{
   125  					ClientStaker:    apiStakerToClientStaker(apiDelegator.Staker),
   126  					RewardOwner:     rewardOwner,
   127  					PotentialReward: (*uint64)(apiDelegator.PotentialReward),
   128  				}
   129  			}
   130  		}
   131  
   132  		clientValidators[i] = ClientPermissionlessValidator{
   133  			ClientStaker:           apiStakerToClientStaker(apiValidator.Staker),
   134  			ValidationRewardOwner:  validationRewardOwner,
   135  			DelegationRewardOwner:  delegationRewardOwner,
   136  			PotentialReward:        (*uint64)(apiValidator.PotentialReward),
   137  			AccruedDelegateeReward: (*uint64)(apiValidator.AccruedDelegateeReward),
   138  			DelegationFee:          float32(apiValidator.DelegationFee),
   139  			Uptime:                 (*float32)(apiValidator.Uptime),
   140  			Connected:              &apiValidator.Connected,
   141  			Signer:                 apiValidator.Signer,
   142  			DelegatorCount:         (*uint64)(apiValidator.DelegatorCount),
   143  			DelegatorWeight:        (*uint64)(apiValidator.DelegatorWeight),
   144  			Delegators:             clientDelegators,
   145  		}
   146  	}
   147  	return clientValidators, nil
   148  }