-
Notifications
You must be signed in to change notification settings - Fork 3.6k
/
types.go
190 lines (165 loc) · 7.03 KB
/
types.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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
// Package v034 is used for legacy migration scripts. Actual migration scripts
// for v034 have been removed, but the v039->v042 migration script still
// references types from this file, so we're keeping it for now.
// DONTCOVER
package v034
import (
"time"
"github.com/cosmos/cosmos-sdk/codec/legacy"
cryptotypes "github.com/cosmos/cosmos-sdk/crypto/types"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/types/bech32/legacybech32" //nolint:staticcheck
)
const (
ModuleName = "staking"
)
// staking constants
const (
Unbonded BondStatus = 0x00
Unbonding BondStatus = 0x01
Bonded BondStatus = 0x02
BondStatusUnbonded = "Unbonded"
BondStatusUnbonding = "Unbonding"
BondStatusBonded = "Bonded"
)
type (
// BondStatus is the status of a validator
BondStatus byte
Pool struct {
NotBondedTokens sdk.Int `json:"not_bonded_tokens"`
BondedTokens sdk.Int `json:"bonded_tokens"`
}
Params struct {
UnbondingTime time.Duration `json:"unbonding_time"`
MaxValidators uint16 `json:"max_validators"`
MaxEntries uint16 `json:"max_entries"`
BondDenom string `json:"bond_denom"`
}
LastValidatorPower struct {
Address sdk.ValAddress
Power int64
}
Description struct {
Moniker string `json:"moniker"`
Identity string `json:"identity"`
Website string `json:"website"`
Details string `json:"details"`
}
Commission struct {
Rate sdk.Dec `json:"rate"`
MaxRate sdk.Dec `json:"max_rate"`
MaxChangeRate sdk.Dec `json:"max_change_rate"`
UpdateTime time.Time `json:"update_time"`
}
bechValidator struct {
OperatorAddress sdk.ValAddress `json:"operator_address"` // the bech32 address of the validator's operator
ConsPubKey string `json:"consensus_pubkey"` // the bech32 consensus public key of the validator
Jailed bool `json:"jailed"` // has the validator been jailed from bonded status?
Status BondStatus `json:"status"` // validator status (bonded/unbonding/unbonded)
Tokens sdk.Int `json:"tokens"` // delegated tokens (incl. self-delegation)
DelegatorShares sdk.Dec `json:"delegator_shares"` // total shares issued to a validator's delegators
Description Description `json:"description"` // description terms for the validator
UnbondingHeight int64 `json:"unbonding_height"` // if unbonding, height at which this validator has begun unbonding
UnbondingCompletionTime time.Time `json:"unbonding_time"` // if unbonding, min time for the validator to complete unbonding
Commission Commission `json:"commission"` // commission parameters
MinSelfDelegation sdk.Int `json:"min_self_delegation"` // minimum self delegation
}
Validator struct {
OperatorAddress sdk.ValAddress `json:"operator_address"`
ConsPubKey cryptotypes.PubKey `json:"consensus_pubkey"`
Jailed bool `json:"jailed"`
Status BondStatus `json:"status"`
Tokens sdk.Int `json:"tokens"`
DelegatorShares sdk.Dec `json:"delegator_shares"`
Description Description `json:"description"`
UnbondingHeight int64 `json:"unbonding_height"`
UnbondingCompletionTime time.Time `json:"unbonding_time"`
Commission Commission `json:"commission"`
MinSelfDelegation sdk.Int `json:"min_self_delegation"`
}
Validators []Validator
Delegation struct {
DelegatorAddress sdk.AccAddress `json:"delegator_address"`
ValidatorAddress sdk.ValAddress `json:"validator_address"`
Shares sdk.Dec `json:"shares"`
}
Delegations []Delegation
UnbondingDelegationEntry struct {
CreationHeight int64 `json:"creation_height"`
CompletionTime time.Time `json:"completion_time"`
InitialBalance sdk.Int `json:"initial_balance"`
Balance sdk.Int `json:"balance"`
}
UnbondingDelegation struct {
DelegatorAddress sdk.AccAddress `json:"delegator_address"`
ValidatorAddress sdk.ValAddress `json:"validator_address"`
Entries []UnbondingDelegationEntry `json:"entries"`
}
RedelegationEntry struct {
CreationHeight int64 `json:"creation_height"`
CompletionTime time.Time `json:"completion_time"`
InitialBalance sdk.Int `json:"initial_balance"`
SharesDst sdk.Dec `json:"shares_dst"`
}
Redelegation struct {
DelegatorAddress sdk.AccAddress `json:"delegator_address"`
ValidatorSrcAddress sdk.ValAddress `json:"validator_src_address"`
ValidatorDstAddress sdk.ValAddress `json:"validator_dst_address"`
Entries []RedelegationEntry `json:"entries"`
}
GenesisState struct {
Pool Pool `json:"pool"`
Params Params `json:"params"`
LastTotalPower sdk.Int `json:"last_total_power"`
LastValidatorPowers []LastValidatorPower `json:"last_validator_powers"`
Validators Validators `json:"validators"`
Delegations Delegations `json:"delegations"`
UnbondingDelegations []UnbondingDelegation `json:"unbonding_delegations"`
Redelegations []Redelegation `json:"redelegations"`
Exported bool `json:"exported"`
}
)
func (v Validator) MarshalJSON() ([]byte, error) {
bechConsPubKey, err := legacybech32.MarshalPubKey(legacybech32.ConsPK, v.ConsPubKey) //nolint:staticcheck
if err != nil {
return nil, err
}
return legacy.Cdc.MarshalJSON(bechValidator{
OperatorAddress: v.OperatorAddress,
ConsPubKey: bechConsPubKey,
Jailed: v.Jailed,
Status: v.Status,
Tokens: v.Tokens,
DelegatorShares: v.DelegatorShares,
Description: v.Description,
UnbondingHeight: v.UnbondingHeight,
UnbondingCompletionTime: v.UnbondingCompletionTime,
MinSelfDelegation: v.MinSelfDelegation,
Commission: v.Commission,
})
}
// UnmarshalJSON unmarshals the validator from JSON using Bech32
func (v *Validator) UnmarshalJSON(data []byte) error {
bv := &bechValidator{}
if err := legacy.Cdc.UnmarshalJSON(data, bv); err != nil {
return err
}
consPubKey, err := legacybech32.UnmarshalPubKey(legacybech32.ConsPK, bv.ConsPubKey) //nolint:staticcheck
if err != nil {
return err
}
*v = Validator{
OperatorAddress: bv.OperatorAddress,
ConsPubKey: consPubKey,
Jailed: bv.Jailed,
Tokens: bv.Tokens,
Status: bv.Status,
DelegatorShares: bv.DelegatorShares,
Description: bv.Description,
UnbondingHeight: bv.UnbondingHeight,
UnbondingCompletionTime: bv.UnbondingCompletionTime,
Commission: bv.Commission,
MinSelfDelegation: bv.MinSelfDelegation,
}
return nil
}