/
keeper.go
216 lines (182 loc) · 6.36 KB
/
keeper.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
package keeper
import (
"github.com/aximchain/axc-cosmos-sdk/baseapp"
"github.com/aximchain/axc-cosmos-sdk/codec"
"github.com/aximchain/axc-cosmos-sdk/pubsub"
sdk "github.com/aximchain/axc-cosmos-sdk/types"
"github.com/aximchain/axc-cosmos-sdk/x/bank"
"github.com/aximchain/axc-cosmos-sdk/x/ibc"
"github.com/aximchain/axc-cosmos-sdk/x/oracle/metrics"
"github.com/aximchain/axc-cosmos-sdk/x/oracle/types"
pTypes "github.com/aximchain/axc-cosmos-sdk/x/paramHub/types"
param "github.com/aximchain/axc-cosmos-sdk/x/params"
"github.com/aximchain/axc-cosmos-sdk/x/sidechain"
)
// Keeper maintains the link to data storage and
// exposes getter/setter methods for the various parts of the state machine
type Keeper struct {
cdc *codec.Codec
storeKey sdk.StoreKey
// The reference to the Paramstore to get and set gov specific params
paramSpace param.Subspace
Pool *sdk.Pool
stakeKeeper types.StakingKeeper
ScKeeper sidechain.Keeper
IbcKeeper ibc.Keeper
BkKeeper bank.Keeper
Metrics *metrics.Metrics
pubServer *pubsub.Server
}
// Parameter store
const (
DefaultParamSpace = "oracle"
)
func ParamTypeTable() param.TypeTable {
return param.NewTypeTable().RegisterParamSet(&types.Params{})
}
// NewKeeper creates new instances of the oracle Keeper
func NewKeeper(cdc *codec.Codec, storeKey sdk.StoreKey, paramSpace param.Subspace, stakeKeeper types.StakingKeeper,
scKeeper sidechain.Keeper, ibcKeeper ibc.Keeper, bkKeeper bank.Keeper, pool *sdk.Pool,
) Keeper {
return Keeper{
cdc: cdc,
storeKey: storeKey,
paramSpace: paramSpace.WithTypeTable(ParamTypeTable()),
stakeKeeper: stakeKeeper,
ScKeeper: scKeeper,
IbcKeeper: ibcKeeper,
BkKeeper: bkKeeper,
Metrics: metrics.NopMetrics(),
Pool: pool,
}
}
func (k Keeper) GetConsensusNeeded(ctx sdk.Context) (consensusNeeded sdk.Dec) {
k.paramSpace.Get(ctx, types.ParamStoreKeyProphecyParams, &consensusNeeded)
return
}
func (k *Keeper) EnablePrometheusMetrics() {
k.Metrics = metrics.PrometheusMetrics()
}
func (k *Keeper) SetParams(ctx sdk.Context, params types.Params) {
k.paramSpace.SetParamSet(ctx, ¶ms)
}
func (k *Keeper) SetPbsbServer(p *pubsub.Server) {
k.pubServer = p
}
// GetProphecy gets the entire prophecy data struct for a given id
func (k Keeper) GetProphecy(ctx sdk.Context, id string) (types.Prophecy, bool) {
store := ctx.KVStore(k.storeKey)
bz := store.Get([]byte(id))
if bz == nil {
return types.Prophecy{}, false
}
var dbProphecy types.DBProphecy
k.cdc.MustUnmarshalBinaryBare(bz, &dbProphecy)
deSerializedProphecy, err := dbProphecy.DeserializeFromDB()
if err != nil {
return types.Prophecy{}, false
}
return deSerializedProphecy, true
}
// DeleteProphecy delete prophecy for a given id
func (k Keeper) DeleteProphecy(ctx sdk.Context, id string) {
store := ctx.KVStore(k.storeKey)
store.Delete([]byte(id))
}
// setProphecy saves a prophecy with an initial claim
func (k Keeper) setProphecy(ctx sdk.Context, prophecy types.Prophecy) {
store := ctx.KVStore(k.storeKey)
serializedProphecy, err := prophecy.SerializeForDB()
if err != nil {
panic(err)
}
store.Set([]byte(prophecy.ID), k.cdc.MustMarshalBinaryBare(serializedProphecy))
}
// ProcessClaim ...
func (k Keeper) ProcessClaim(ctx sdk.Context, claim types.Claim) (types.Prophecy, sdk.Error) {
activeValidator := k.stakeKeeper.CheckIsValidOracleRelayer(ctx, claim.ValidatorAddress)
if !activeValidator {
return types.Prophecy{}, types.ErrInvalidValidator()
}
if claim.ID == "" {
return types.Prophecy{}, types.ErrInvalidIdentifier()
}
if len(claim.Payload) == 0 {
return types.Prophecy{}, types.ErrInvalidClaim()
}
prophecy, found := k.GetProphecy(ctx, claim.ID)
if !found {
prophecy = types.NewProphecy(claim.ID)
}
switch prophecy.Status.Text {
case types.PendingStatusText:
// continue processing
default:
return types.Prophecy{}, types.ErrProphecyFinalized()
}
prophecy.AddClaim(claim.ValidatorAddress, claim.Payload)
prophecy = k.processCompletion(ctx, prophecy)
k.setProphecy(ctx, prophecy)
return prophecy, nil
}
// processCompletion looks at a given prophecy
// and assesses whether the claim with the highest power on that prophecy has enough
// power to be considered successful, or alternatively,
// will never be able to become successful due to not enough validation power being
// left to push it over the threshold required for consensus.
func (k Keeper) processCompletion(ctx sdk.Context, prophecy types.Prophecy) types.Prophecy {
highestClaim, highestClaimPower, totalClaimsPower, totalPower := prophecy.FindHighestClaim(ctx, k.stakeKeeper)
highestConsensusRatio := sdk.NewDec(highestClaimPower).Quo(sdk.NewDec(totalPower))
remainingPossibleClaimPower := totalPower - totalClaimsPower
highestPossibleClaimPower := highestClaimPower + remainingPossibleClaimPower
highestPossibleConsensusRatio := sdk.NewDec(highestPossibleClaimPower).Quo(sdk.NewDec(totalPower))
consensusNeeded := k.GetConsensusNeeded(ctx)
if highestConsensusRatio.GTE(consensusNeeded) {
prophecy.Status.Text = types.SuccessStatusText
prophecy.Status.FinalClaim = highestClaim
} else if highestPossibleConsensusRatio.LT(consensusNeeded) {
prophecy.Status.Text = types.FailedStatusText
}
return prophecy
}
func (k *Keeper) SubscribeParamChange(hub pTypes.ParamChangePublisher) {
hub.SubscribeParamChange(
func(context sdk.Context, iChange interface{}) {
switch change := iChange.(type) {
case *types.Params:
// do double check
err := change.UpdateCheck()
if err != nil {
context.Logger().Error("skip invalid param change", "err", err, "param", change)
} else {
newCtx := context.DepriveSideChainKeyPrefix()
k.SetParams(newCtx, *change)
break
}
default:
context.Logger().Debug("skip unknown param change")
}
},
&pTypes.ParamSpaceProto{ParamSpace: k.paramSpace, Proto: func() pTypes.SCParam {
return new(types.Params)
}},
nil,
nil,
)
}
func (k *Keeper) PublishCrossAppFailEvent(ctx sdk.Context, from string, relayerFee int64, chainId string) {
if k.pubServer != nil {
txHash := ctx.Value(baseapp.TxHashKey)
if txHashStr, ok := txHash.(string); ok {
event := types.CrossAppFailEvent{
TxHash: txHashStr,
ChainId: chainId,
RelayerFee: relayerFee,
From: from,
}
k.pubServer.Publish(event)
} else {
ctx.Logger().With("module", "oracle").Error("failed to get txhash, will not publish oracle event ")
}
}
}