-
Notifications
You must be signed in to change notification settings - Fork 182
/
genesis.go
131 lines (114 loc) · 4.5 KB
/
genesis.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
package types
import (
"fmt"
"time"
sdk "github.com/okex/exchain/libs/cosmos-sdk/types"
)
// GenesisState - all staking state that must be provided at genesis
type GenesisState struct {
Params Params `json:"params" yaml:"params"`
LastTotalPower sdk.Int `json:"last_total_power" yaml:"last_total_power"`
LastValidatorPowers []LastValidatorPower `json:"last_validator_powers" yaml:"last_validator_powers"`
Validators []ValidatorExported `json:"validators" yaml:"validators"`
Delegators []Delegator `json:"delegators" yaml:"delegators"`
UnbondingDelegations []UndelegationInfo `json:"unbonding_delegations" yaml:"unbonding_delegations"`
AllShares []SharesExported `json:"all_shares" yaml:"all_shares"`
ProxyDelegatorKeys []ProxyDelegatorKeyExported `json:"proxy_delegator_keys" yaml:"proxy_delegator_keys"`
Exported bool `json:"exported" yaml:"exported"`
}
// LastValidatorPower is needed for validator set update logic
type LastValidatorPower struct {
Address sdk.ValAddress
Power int64
}
// NewLastValidatorPower creates a new instance of LastValidatorPower
func NewLastValidatorPower(valAddr sdk.ValAddress, power int64) LastValidatorPower {
return LastValidatorPower{
valAddr,
power,
}
}
// NewGenesisState creates a new object of GenesisState
func NewGenesisState(params Params, validators Validators, delegators []Delegator) GenesisState {
return GenesisState{
Params: params,
Validators: validators.Export(),
Delegators: delegators,
}
}
// DefaultGenesisState gets the default genesis state
func DefaultGenesisState() GenesisState {
return GenesisState{
Params: DefaultParams(),
}
}
// ValidatorExported is designed for Validator export
type ValidatorExported struct {
OperatorAddress sdk.ValAddress `json:"operator_address"`
ConsPubKey string `json:"consensus_pubkey"`
Jailed bool `json:"jailed"`
Status sdk.BondStatus `json:"status" yaml:"status"`
DelegatorShares sdk.Dec `json:"delegator_shares"`
Description Description `json:"description"`
UnbondingHeight int64 `json:"unbonding_height"`
UnbondingCompletionTime time.Time `json:"unbonding_time"`
MinSelfDelegation sdk.Dec `json:"min_self_delegation"`
}
// Import converts validator exported format to inner one by filling the zero-value of Tokens and Commission
func (ve ValidatorExported) Import() Validator {
consPk, err := GetConsPubKeyBech32(ve.ConsPubKey)
if err != nil {
panic(fmt.Sprintf("failed. consensus pubkey is parsed error: %s", err.Error()))
}
return Validator{
ve.OperatorAddress,
consPk,
ve.Jailed,
ve.Status,
sdk.NewInt(0),
ve.DelegatorShares,
ve.Description,
ve.UnbondingHeight,
ve.UnbondingCompletionTime,
NewCommission(sdk.NewDec(1), sdk.NewDec(1), sdk.NewDec(0)),
ve.MinSelfDelegation,
}
}
// ConsAddress returns the TM validator address of exported validator
func (ve ValidatorExported) ConsAddress() sdk.ConsAddress {
consPk, err := GetConsPubKeyBech32(ve.ConsPubKey)
if err != nil {
panic(fmt.Sprintf("failed. consensus pubkey is parsed error: %s", err.Error()))
}
return sdk.ConsAddress(consPk.Address())
}
// IsBonded checks if the exported validator status equals Bonded
func (ve ValidatorExported) IsBonded() bool {
return ve.Status.Equal(sdk.Bonded)
}
// ProxyDelegatorKeyExported is designed for ProxyDelegatorKey export
type ProxyDelegatorKeyExported struct {
DelAddr sdk.AccAddress `json:"delegator_address" yaml:"delegator_address"`
ProxyAddr sdk.AccAddress `json:"proxy_address" yaml:"proxy_address"`
}
// NewProxyDelegatorKeyExported creates a new object of ProxyDelegatorKeyExported
func NewProxyDelegatorKeyExported(delAddr, proxyAddr sdk.AccAddress) ProxyDelegatorKeyExported {
return ProxyDelegatorKeyExported{
delAddr,
proxyAddr,
}
}
// SharesExported is designed for types.Shares export
type SharesExported struct {
DelAddress sdk.AccAddress `json:"delegator_address" yaml:"delegator_address"`
ValidatorAddress sdk.ValAddress `json:"validator_address" yaml:"validator_address"`
Shares Shares `json:"shares" yaml:"shares"`
}
// NewSharesExported creates a new object of SharesExported
func NewSharesExported(delAddr sdk.AccAddress, valAddr sdk.ValAddress, shares Shares) SharesExported {
return SharesExported{
delAddr,
valAddr,
shares,
}
}