-
Notifications
You must be signed in to change notification settings - Fork 4
/
keeper_delegate_key.go
236 lines (220 loc) · 7.27 KB
/
keeper_delegate_key.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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
package keeper
import (
"time"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
codectypes "github.com/cosmos/cosmos-sdk/codec/types"
sdk "github.com/cosmos/cosmos-sdk/types"
stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types"
"github.com/treasurenetprotocol/treasurenet/x/gravity/types"
)
///////////////////////////
//// ADDRESS DELEGATION ///
///////////////////////////
// SetOrchestratorValidator sets the Orchestrator key for a given validator
func (k Keeper) SetOrchestratorValidator(ctx sdk.Context, val sdk.ValAddress, orch sdk.AccAddress) {
if err := sdk.VerifyAddressFormat(val); err != nil {
panic(sdkerrors.Wrap(err, "invalid val address"))
}
if err := sdk.VerifyAddressFormat(orch); err != nil {
panic(sdkerrors.Wrap(err, "invalid orch address"))
}
store := ctx.KVStore(k.storeKey)
store.Set(types.GetOrchestratorAddressKey(orch), val.Bytes())
}
// GetOrchestratorValidator returns the validator key associated with an orchestrator key
func (k Keeper) GetOrchestratorValidator(ctx sdk.Context, orch sdk.AccAddress) (validator stakingtypes.Validator, found bool) {
valAddr, foundValAddr := k.GetOrchestratorValidatorAddr(ctx, orch)
if err := sdk.VerifyAddressFormat(orch); err != nil {
ctx.Logger().Error("invalid orch address")
return validator, false
}
if !foundValAddr && valAddr == nil {
return stakingtypes.Validator{
OperatorAddress: "",
ConsensusPubkey: &codectypes.Any{
TypeUrl: "",
Value: []byte{},
XXX_NoUnkeyedLiteral: struct{}{},
XXX_unrecognized: []byte{},
XXX_sizecache: 0,
},
Jailed: false,
Status: 0,
Tokens: sdk.Int{},
DelegatorShares: sdk.Dec{},
Description: stakingtypes.Description{
Moniker: "",
Identity: "",
Website: "",
SecurityContact: "",
Details: "",
},
UnbondingHeight: 0,
UnbondingTime: time.Time{},
Commission: stakingtypes.Commission{
CommissionRates: stakingtypes.CommissionRates{
Rate: sdk.Dec{},
MaxRate: sdk.Dec{},
MaxChangeRate: sdk.Dec{},
},
UpdateTime: time.Time{},
},
MinSelfDelegation: sdk.Int{},
}, false
}
validator, found = k.StakingKeeper.GetValidator(ctx, valAddr)
if !found {
return stakingtypes.Validator{
OperatorAddress: "",
ConsensusPubkey: &codectypes.Any{
TypeUrl: "",
Value: []byte{},
XXX_NoUnkeyedLiteral: struct{}{},
XXX_unrecognized: []byte{},
XXX_sizecache: 0,
},
Jailed: false,
Status: 0,
Tokens: sdk.Int{},
DelegatorShares: sdk.Dec{},
Description: stakingtypes.Description{
Moniker: "",
Identity: "",
Website: "",
SecurityContact: "",
Details: "",
},
UnbondingHeight: 0,
UnbondingTime: time.Time{},
Commission: stakingtypes.Commission{
CommissionRates: stakingtypes.CommissionRates{
Rate: sdk.Dec{},
MaxRate: sdk.Dec{},
MaxChangeRate: sdk.Dec{},
},
UpdateTime: time.Time{},
},
MinSelfDelegation: sdk.Int{},
}, false
}
return validator, true
}
// GetOrchestratorValidatorAddr returns the validator address associated with an orchestrator key.
// Getting a result from this function means that the validator existed at some point and sent a SetOrchestratorAddress
// message. It does not mean that the validator is in the current validator set, for that use GetOrchestratorValidator.
// This will hold true as long as we never delete any delegate keys.
func (k Keeper) GetOrchestratorValidatorAddr(ctx sdk.Context, orch sdk.AccAddress) (validator sdk.ValAddress, found bool) {
if err := sdk.VerifyAddressFormat(orch); err != nil {
ctx.Logger().Error("invalid orch address")
return validator, false
}
store := ctx.KVStore(k.storeKey)
valAddr := store.Get([]byte(types.GetOrchestratorAddressKey(orch)))
if valAddr == nil {
return sdk.ValAddress{}, false
}
return valAddr, true
}
/////////////////////////////
// ETH ADDRESS //
/////////////////////////////
// SetEthAddress sets the ethereum address for a given validator
func (k Keeper) SetEthAddressForValidator(ctx sdk.Context, validator sdk.ValAddress, ethAddr types.EthAddress) {
if err := sdk.VerifyAddressFormat(validator); err != nil {
panic(sdkerrors.Wrap(err, "invalid validator address"))
}
store := ctx.KVStore(k.storeKey)
store.Set(types.GetEthAddressByValidatorKey(validator), ethAddr.GetAddress().Bytes())
store.Set(types.GetValidatorByEthAddressKey(ethAddr), []byte(validator))
}
// GetEthAddressByValidator returns the eth address for a given gravity validator
func (k Keeper) GetEthAddressByValidator(ctx sdk.Context, validator sdk.ValAddress) (ethAddress *types.EthAddress, found bool) {
if err := sdk.VerifyAddressFormat(validator); err != nil {
panic(sdkerrors.Wrap(err, "invalid validator address"))
}
store := ctx.KVStore(k.storeKey)
ethAddr := store.Get(types.GetEthAddressByValidatorKey(validator))
if ethAddr == nil {
return nil, false
}
addr, err := types.NewEthAddressFromBytes(ethAddr)
if err != nil {
return nil, false
}
return addr, true
}
// GetValidatorByEthAddress returns the validator for a given eth address
func (k Keeper) GetValidatorByEthAddress(ctx sdk.Context, ethAddr types.EthAddress) (validator stakingtypes.Validator, found bool) {
store := ctx.KVStore(k.storeKey)
valAddr := store.Get([]byte(types.GetValidatorByEthAddressKey(ethAddr)))
if valAddr == nil {
return stakingtypes.Validator{
OperatorAddress: "",
ConsensusPubkey: &codectypes.Any{
TypeUrl: "",
Value: []byte{},
XXX_NoUnkeyedLiteral: struct{}{},
XXX_unrecognized: []byte{},
XXX_sizecache: 0,
},
Jailed: false,
Status: 0,
Tokens: sdk.Int{},
DelegatorShares: sdk.Dec{},
Description: stakingtypes.Description{
Moniker: "",
Identity: "",
Website: "",
SecurityContact: "",
Details: "",
},
UnbondingHeight: 0,
UnbondingTime: time.Time{},
Commission: stakingtypes.Commission{
CommissionRates: stakingtypes.CommissionRates{
Rate: sdk.Dec{},
MaxRate: sdk.Dec{},
MaxChangeRate: sdk.Dec{},
},
UpdateTime: time.Time{},
},
MinSelfDelegation: sdk.Int{},
}, false
}
validator, found = k.StakingKeeper.GetValidator(ctx, valAddr)
if !found {
return stakingtypes.Validator{
OperatorAddress: "",
ConsensusPubkey: &codectypes.Any{
TypeUrl: "",
Value: []byte{},
XXX_NoUnkeyedLiteral: struct{}{},
XXX_unrecognized: []byte{},
XXX_sizecache: 0,
},
Jailed: false,
Status: 0,
Tokens: sdk.Int{},
DelegatorShares: sdk.Dec{},
Description: stakingtypes.Description{
Moniker: "",
Identity: "",
Website: "",
SecurityContact: "",
Details: "",
},
UnbondingHeight: 0,
UnbondingTime: time.Time{},
Commission: stakingtypes.Commission{
CommissionRates: stakingtypes.CommissionRates{
Rate: sdk.Dec{},
MaxRate: sdk.Dec{},
MaxChangeRate: sdk.Dec{},
},
UpdateTime: time.Time{},
},
MinSelfDelegation: sdk.Int{},
}, false
}
return validator, true
}