-
Notifications
You must be signed in to change notification settings - Fork 13
/
oracle.go
243 lines (207 loc) · 7.04 KB
/
oracle.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
237
238
239
240
241
242
243
package keeper
import (
"sort"
"time"
sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types"
"github.com/functionx/fx-core/v2/x/crosschain/types"
)
/////////////////////////////
// PROPOSAL ORACLE //
/////////////////////////////
func (k Keeper) SetProposalOracle(ctx sdk.Context, proposalOracle *types.ProposalOracle) {
store := ctx.KVStore(k.storeKey)
store.Set(types.ProposalOracleKey, k.cdc.MustMarshal(proposalOracle))
}
func (k Keeper) GetProposalOracle(ctx sdk.Context) (proposalOracle types.ProposalOracle, found bool) {
store := ctx.KVStore(k.storeKey)
bz := store.Get(types.ProposalOracleKey)
if bz == nil {
return proposalOracle, false
}
k.cdc.MustUnmarshal(bz, &proposalOracle)
return proposalOracle, true
}
func (k Keeper) IsProposalOracle(ctx sdk.Context, oracleAddr string) bool {
proposalOracle, found := k.GetProposalOracle(ctx)
if !found {
return false
}
for _, oracle := range proposalOracle.Oracles {
if oracle == oracleAddr {
return true
}
}
return false
}
/////////////////////////////
// ADDRESS Bridger //
/////////////////////////////
// SetOracleByBridger sets the bridger key for a given oracle
func (k Keeper) SetOracleByBridger(ctx sdk.Context, bridgerAddr, oracleAddr sdk.AccAddress) {
store := ctx.KVStore(k.storeKey)
store.Set(types.GetOracleAddressByBridgerKey(bridgerAddr), oracleAddr.Bytes())
}
// GetOracleAddressByBridgerKey returns the oracle key associated with an bridger key
func (k Keeper) GetOracleAddressByBridgerKey(ctx sdk.Context, bridgerAddr sdk.AccAddress) (sdk.AccAddress, bool) {
store := ctx.KVStore(k.storeKey)
oracle := store.Get(types.GetOracleAddressByBridgerKey(bridgerAddr))
return oracle, oracle != nil
}
// DelOracleByBridger delete the bridger key for a given oracle
func (k Keeper) DelOracleByBridger(ctx sdk.Context, bridgerAddr sdk.AccAddress) {
store := ctx.KVStore(k.storeKey)
key := types.GetOracleAddressByBridgerKey(bridgerAddr)
if !store.Has(key) {
return
}
store.Delete(key)
}
/////////////////////////////
// External ADDRESS //
/////////////////////////////
// SetOracleByExternalAddress sets the external address for a given oracle
func (k Keeper) SetOracleByExternalAddress(ctx sdk.Context, externalAddress string, oracleAddr sdk.AccAddress) {
store := ctx.KVStore(k.storeKey)
store.Set(types.GetOracleAddressByExternalKey(externalAddress), oracleAddr.Bytes())
}
// GetOracleByExternalAddress returns the external address for a given gravity oracle
func (k Keeper) GetOracleByExternalAddress(ctx sdk.Context, externalAddress string) (sdk.AccAddress, bool) {
store := ctx.KVStore(k.storeKey)
bz := store.Get(types.GetOracleAddressByExternalKey(externalAddress))
return bz, bz != nil
}
// DelOracleByExternalAddress delete the external address for a give oracle
func (k Keeper) DelOracleByExternalAddress(ctk sdk.Context, externalAddress string) {
store := ctk.KVStore(k.storeKey)
oracleAddr := types.GetOracleAddressByExternalKey(externalAddress)
if !store.Has(oracleAddr) {
return
}
store.Delete(oracleAddr)
}
/////////////////////////////
// ORACLE TOTAL POWER //
/////////////////////////////
// GetLastTotalPower Load the last total oracle power.
func (k Keeper) GetLastTotalPower(ctx sdk.Context) sdk.Int {
store := ctx.KVStore(k.storeKey)
bz := store.Get(types.LastTotalPowerKey)
if bz == nil {
return sdk.ZeroInt()
}
ip := sdk.IntProto{}
k.cdc.MustUnmarshal(bz, &ip)
return ip.Int
}
// SetLastTotalPower Set the last total validator power.
func (k Keeper) SetLastTotalPower(ctx sdk.Context, power sdk.Int) {
store := ctx.KVStore(k.storeKey)
bz := k.cdc.MustMarshal(&sdk.IntProto{Int: power})
store.Set(types.LastTotalPowerKey, bz)
}
func (k Keeper) CommonSetOracleTotalPower(ctx sdk.Context) {
oracles := k.GetAllOracles(ctx, true)
totalPower := sdk.ZeroInt()
for _, oracle := range oracles {
totalPower = totalPower.Add(oracle.GetPower())
}
k.SetLastTotalPower(ctx, totalPower)
}
/////////////////////////////
// ORACLES //
/////////////////////////////
func (k Keeper) IterateOracle(ctx sdk.Context, cb func(oracle types.Oracle) bool) {
store := ctx.KVStore(k.storeKey)
iterator := sdk.KVStorePrefixIterator(store, types.OracleKey)
defer iterator.Close()
for ; iterator.Valid(); iterator.Next() {
oracle := types.Oracle{}
k.cdc.MustUnmarshal(iterator.Value(), &oracle)
if cb(oracle) {
break
}
}
}
// SetOracle save Oracle data
func (k Keeper) SetOracle(ctx sdk.Context, oracle types.Oracle) {
store := ctx.KVStore(k.storeKey)
bz := k.cdc.MustMarshal(&oracle)
store.Set(types.GetOracleKey(oracle.GetOracle()), bz)
}
func (k Keeper) HasOracle(ctx sdk.Context, addr sdk.AccAddress) (found bool) {
store := ctx.KVStore(k.storeKey)
return store.Has(types.GetOracleKey(addr))
}
// GetOracle get Oracle data
func (k Keeper) GetOracle(ctx sdk.Context, addr sdk.AccAddress) (oracle types.Oracle, found bool) {
store := ctx.KVStore(k.storeKey)
value := store.Get(types.GetOracleKey(addr))
if value == nil {
return oracle, false
}
k.cdc.MustUnmarshal(value, &oracle)
return oracle, true
}
func (k Keeper) DelOracle(ctx sdk.Context, oracle sdk.AccAddress) {
store := ctx.KVStore(k.storeKey)
key := types.GetOracleKey(oracle)
if !store.Has(key) {
return
}
store.Delete(key)
}
func (k Keeper) GetAllOracles(ctx sdk.Context, isOnline bool) (oracles types.Oracles) {
store := ctx.KVStore(k.storeKey)
iterator := sdk.KVStorePrefixIterator(store, types.OracleKey)
defer iterator.Close()
for ; iterator.Valid(); iterator.Next() {
var oracle types.Oracle
k.cdc.MustUnmarshal(iterator.Value(), &oracle)
if isOnline && !oracle.Online {
continue
}
oracles = append(oracles, oracle)
}
sort.Sort(oracles)
return oracles
}
func (k Keeper) UnbondedOracle(ctx sdk.Context, oracle types.Oracle) error {
delegateAddr := oracle.GetDelegateAddress(k.moduleName)
valAddr := oracle.GetValidator()
delegation, found := k.stakingKeeper.GetDelegation(ctx, delegateAddr, valAddr)
if !found {
panic(sdkerrors.Wrap(types.ErrInvalid, "no delegation for (address, validator) tuple"))
}
completionTime, err := k.stakingKeeper.Undelegate(ctx, delegateAddr, valAddr, delegation.Shares)
if err != nil {
return err
}
oracle.Online = false
k.SetOracle(ctx, oracle)
ctx.EventManager().EmitEvent(sdk.NewEvent(
stakingtypes.EventTypeUnbond,
sdk.NewAttribute(stakingtypes.AttributeKeyValidator, oracle.DelegateValidator),
sdk.NewAttribute(sdk.AttributeKeyAmount, oracle.DelegateAmount.String()),
sdk.NewAttribute(stakingtypes.AttributeKeyCompletionTime, completionTime.Format(time.RFC3339)),
))
return nil
}
func (k Keeper) SlashOracle(ctx sdk.Context, oracleAddrStr string) {
oracleAddr, err := sdk.AccAddressFromBech32(oracleAddrStr)
if err != nil {
panic(err)
}
oracle, found := k.GetOracle(ctx, oracleAddr)
if !found {
panic(types.ErrNoFoundOracle)
}
if !oracle.Online {
return
}
oracle.Online = false
oracle.SlashTimes += 1
k.SetOracle(ctx, oracle)
k.SetLastOracleSlashBlockHeight(ctx, uint64(ctx.BlockHeight()))
}