github.com/MetalBlockchain/metalgo@v1.11.9/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/MetalBlockchain/metalgo/ids"
    10  	"github.com/MetalBlockchain/metalgo/utils/formatting/address"
    11  	"github.com/MetalBlockchain/metalgo/vms/platformvm/api"
    12  	"github.com/MetalBlockchain/metalgo/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                 *float32
    48  	Connected              *bool
    49  	Signer                 *signer.ProofOfPossession
    50  	// The delegators delegating to this validator
    51  	DelegatorCount  *uint64
    52  	DelegatorWeight *uint64
    53  	Delegators      []ClientDelegator
    54  }
    55  
    56  // ClientDelegator is the repr. of a delegator sent over client
    57  type ClientDelegator struct {
    58  	ClientStaker
    59  	RewardOwner     *ClientOwner
    60  	PotentialReward *uint64
    61  }
    62  
    63  func apiStakerToClientStaker(validator api.Staker) ClientStaker {
    64  	return ClientStaker{
    65  		TxID:        validator.TxID,
    66  		StartTime:   uint64(validator.StartTime),
    67  		EndTime:     uint64(validator.EndTime),
    68  		Weight:      uint64(validator.Weight),
    69  		StakeAmount: (*uint64)(validator.StakeAmount),
    70  		NodeID:      validator.NodeID,
    71  	}
    72  }
    73  
    74  func apiOwnerToClientOwner(rewardOwner *api.Owner) (*ClientOwner, error) {
    75  	if rewardOwner == nil {
    76  		return nil, nil
    77  	}
    78  
    79  	addrs, err := address.ParseToIDs(rewardOwner.Addresses)
    80  	return &ClientOwner{
    81  		Locktime:  uint64(rewardOwner.Locktime),
    82  		Threshold: uint32(rewardOwner.Threshold),
    83  		Addresses: addrs,
    84  	}, err
    85  }
    86  
    87  func getClientPermissionlessValidators(validatorsSliceIntf []interface{}) ([]ClientPermissionlessValidator, error) {
    88  	clientValidators := make([]ClientPermissionlessValidator, len(validatorsSliceIntf))
    89  	for i, validatorMapIntf := range validatorsSliceIntf {
    90  		validatorMapJSON, err := json.Marshal(validatorMapIntf)
    91  		if err != nil {
    92  			return nil, err
    93  		}
    94  
    95  		var apiValidator api.PermissionlessValidator
    96  		err = json.Unmarshal(validatorMapJSON, &apiValidator)
    97  		if err != nil {
    98  			return nil, err
    99  		}
   100  
   101  		validationRewardOwner, err := apiOwnerToClientOwner(apiValidator.ValidationRewardOwner)
   102  		if err != nil {
   103  			return nil, err
   104  		}
   105  
   106  		delegationRewardOwner, err := apiOwnerToClientOwner(apiValidator.DelegationRewardOwner)
   107  		if err != nil {
   108  			return nil, err
   109  		}
   110  
   111  		var clientDelegators []ClientDelegator
   112  		if apiValidator.Delegators != nil {
   113  			clientDelegators = make([]ClientDelegator, len(*apiValidator.Delegators))
   114  			for j, apiDelegator := range *apiValidator.Delegators {
   115  				rewardOwner, err := apiOwnerToClientOwner(apiDelegator.RewardOwner)
   116  				if err != nil {
   117  					return nil, err
   118  				}
   119  
   120  				clientDelegators[j] = ClientDelegator{
   121  					ClientStaker:    apiStakerToClientStaker(apiDelegator.Staker),
   122  					RewardOwner:     rewardOwner,
   123  					PotentialReward: (*uint64)(apiDelegator.PotentialReward),
   124  				}
   125  			}
   126  		}
   127  
   128  		clientValidators[i] = ClientPermissionlessValidator{
   129  			ClientStaker:           apiStakerToClientStaker(apiValidator.Staker),
   130  			ValidationRewardOwner:  validationRewardOwner,
   131  			DelegationRewardOwner:  delegationRewardOwner,
   132  			PotentialReward:        (*uint64)(apiValidator.PotentialReward),
   133  			AccruedDelegateeReward: (*uint64)(apiValidator.AccruedDelegateeReward),
   134  			DelegationFee:          float32(apiValidator.DelegationFee),
   135  			Uptime:                 (*float32)(apiValidator.Uptime),
   136  			Connected:              &apiValidator.Connected,
   137  			Signer:                 apiValidator.Signer,
   138  			DelegatorCount:         (*uint64)(apiValidator.DelegatorCount),
   139  			DelegatorWeight:        (*uint64)(apiValidator.DelegatorWeight),
   140  			Delegators:             clientDelegators,
   141  		}
   142  	}
   143  	return clientValidators, nil
   144  }