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