-
Notifications
You must be signed in to change notification settings - Fork 20
Expand file tree
/
Copy pathdispute.go
More file actions
334 lines (307 loc) · 12.2 KB
/
Copy pathdispute.go
File metadata and controls
334 lines (307 loc) · 12.2 KB
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
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
package keeper
import (
"context"
"crypto/sha256"
"encoding/hex"
"fmt"
gomath "math"
"math/big"
"strconv"
layertypes "github.com/tellor-io/layer/types"
"github.com/tellor-io/layer/x/dispute/types"
oracletypes "github.com/tellor-io/layer/x/oracle/types"
"cosmossdk.io/collections"
"cosmossdk.io/math"
sdk "github.com/cosmos/cosmos-sdk/types"
)
// GetDisputeByReporter assembles dispute key for reporter given the params and fetches the current dispute (the last generated one for this specific dispute)
// and returns the dispute object
func (k Keeper) GetDisputeByReporter(ctx sdk.Context, r oracletypes.MicroReport, c types.DisputeCategory) (types.Dispute, error) {
key := []byte(k.ReporterKey(ctx, r, c))
rng := collections.NewPrefixedPairRange[[]byte, uint64](key).Descending()
// returns iterator for all the dispute ids sorted in descending order
iter, err := k.Disputes.Indexes.DisputeByReporter.Iterate(ctx, rng)
if err != nil {
return types.Dispute{}, err
}
defer iter.Close()
if !iter.Valid() {
return types.Dispute{}, collections.ErrNotFound
}
// get the first dispute id
id, err := iter.PrimaryKey()
if err != nil {
return types.Dispute{}, err
}
return k.Disputes.Get(ctx, id)
}
// NextDisputeId fetches an iterator for all disputes in descending order and increments the first returned dispute id by 1
func (k Keeper) NextDisputeId(ctx sdk.Context) uint64 {
rng := new(collections.Range[uint64]).Descending()
iter, err := k.Disputes.Iterate(ctx, rng)
if err != nil {
return 0
}
defer iter.Close()
// for the first dispute id, return 1
if !iter.Valid() {
return 1
}
currentId, err := iter.Key()
if err != nil {
return 0
}
return currentId + 1
}
// Generate hash id
func (k Keeper) HashId(ctx sdk.Context, r oracletypes.MicroReport, c types.DisputeCategory) [32]byte {
params := types.DisputeParams{
Report: &r,
Category: c,
}
return sha256.Sum256(k.cdc.MustMarshal(¶ms))
}
// Make a reporter key by combining reporter address and a hash of dispute params
func (k Keeper) ReporterKey(ctx sdk.Context, r oracletypes.MicroReport, c types.DisputeCategory) string {
return fmt.Sprintf("%s:%x", r.Reporter, k.HashId(ctx, r, c))
}
// Set new dispute
func (k Keeper) SetNewDispute(ctx sdk.Context, sender sdk.AccAddress, msg types.MsgProposeDispute, report *oracletypes.MicroReport) error {
disputeId := k.NextDisputeId(ctx)
hashId := k.HashId(ctx, *report, msg.DisputeCategory)
// slash amount
disputeFee, err := k.GetDisputeFee(ctx, *report, msg.DisputeCategory)
if err != nil {
return err
}
if msg.Fee.Amount.GT(disputeFee) {
msg.Fee.Amount = disputeFee
}
disputeFeeDec := math.LegacyNewDecFromInt(disputeFee)
fivePercentDec := disputeFeeDec.Mul(math.LegacyNewDec(1)).Quo(math.LegacyNewDec(20))
fivePercent := fivePercentDec.TruncateInt()
dispute := types.Dispute{
HashId: hashId[:],
DisputeId: disputeId,
DisputeCategory: msg.DisputeCategory,
DisputeStatus: types.Prevote,
DisputeStartTime: ctx.BlockTime(),
DisputeEndTime: ctx.BlockTime().Add(ONE_DAY), // one day to fully pay fee
DisputeStartBlock: uint64(ctx.BlockHeight()),
DisputeRound: 1,
SlashAmount: disputeFee,
// burn amount is calculated as 5% of dispute fee
BurnAmount: fivePercent,
DisputeFee: disputeFee,
InitialEvidence: *report,
FeeTotal: msg.Fee.Amount,
PrevDisputeIds: []uint64{disputeId},
Open: true,
BlockNumber: uint64(ctx.BlockHeight()),
}
if err := k.DisputeFeePayer.Set(ctx, collections.Join(dispute.DisputeId, sender.Bytes()), types.PayerInfo{
Amount: msg.Fee.Amount,
FromBond: msg.PayFromBond,
}); err != nil {
return err
}
// Pay the dispute fee
if err := k.PayDisputeFee(ctx, sender, msg.Fee, msg.PayFromBond, dispute.HashId, true); err != nil {
return err
}
// if the paid fee is equal to the slash amount, then slash validator and jail
if dispute.FeeTotal.Equal(dispute.SlashAmount) {
if err := k.SlashAndJailReporter(ctx, dispute.InitialEvidence, dispute.DisputeCategory, dispute.HashId); err != nil {
return err
}
// extend dispute end time by 3 days, 2 for voting and 1 to allow for more rounds
dispute.DisputeEndTime = ctx.BlockTime().Add(THREE_DAYS)
dispute.DisputeStatus = types.Voting
if err := k.SetStartVote(ctx, dispute.DisputeId); err != nil { // starting voting immediately
return err
}
}
ctx.EventManager().EmitEvents(sdk.Events{
sdk.NewEvent(
"new_dispute",
sdk.NewAttribute("disputer", msg.Creator),
sdk.NewAttribute("reporter", msg.DisputedReporter),
sdk.NewAttribute("dispute_category", msg.DisputeCategory.String()),
sdk.NewAttribute("total_fee", disputeFee.String()),
sdk.NewAttribute("fee_paid", msg.Fee.Amount.String()),
sdk.NewAttribute("pay_from_bond", strconv.FormatBool(msg.PayFromBond)),
sdk.NewAttribute("dispute_id", strconv.FormatUint(disputeId, 10)),
sdk.NewAttribute("value", report.Value),
sdk.NewAttribute("query_type", report.QueryType),
sdk.NewAttribute("query_id", hex.EncodeToString(report.QueryId)),
sdk.NewAttribute("report_block_number", strconv.FormatUint(report.BlockNumber, 10)),
sdk.NewAttribute("microreport_timestamp", strconv.FormatInt(report.Timestamp.UnixMilli(), 10)),
),
})
return k.Disputes.Set(ctx, dispute.DisputeId, dispute)
}
// Slash and jail reporter
func (k Keeper) SlashAndJailReporter(ctx sdk.Context, report oracletypes.MicroReport, category types.DisputeCategory, hashId []byte) error {
// flag aggregate report if necessary
err := k.oracleKeeper.FlagAggregateReport(ctx, report)
if err != nil {
return err
}
reporterAddr := sdk.MustAccAddressFromBech32(report.Reporter)
slashPercentageFixed6, jailDuration, err := GetSlashPercentageAndJailDuration(category)
if err != nil {
return err
}
reportPowerFixed6 := math.NewInt(int64(report.Power)).Mul(layertypes.PowerReduction)
powerReductionDec := math.LegacyNewDecFromInt(layertypes.PowerReduction)
reportPowerFixed6Dec := math.LegacyNewDecFromInt(reportPowerFixed6)
slashPercentageFixed6Dec := math.LegacyNewDecFromInt(slashPercentageFixed6)
slashAmountFixed6Dec := reportPowerFixed6Dec.Mul(slashPercentageFixed6Dec).Quo(powerReductionDec)
slashAmountFixed6 := slashAmountFixed6Dec.TruncateInt()
err = k.reporterKeeper.EscrowReporterStake(ctx, reporterAddr, report.Power, report.BlockNumber, slashAmountFixed6, report.QueryId, hashId)
if err != nil {
return err
}
return k.reporterKeeper.JailReporter(ctx, reporterAddr, jailDuration, report.BlockNumber, hashId)
}
func (k Keeper) JailReporter(ctx context.Context, repAddr sdk.AccAddress, jailDuration, reportBlockNumber uint64, disputeHashID []byte) error {
return k.reporterKeeper.JailReporter(ctx, repAddr, jailDuration, reportBlockNumber, disputeHashID)
}
// Get percentage of slash amount based on category, returned as fixed6
func GetSlashPercentageAndJailDuration(category types.DisputeCategory) (math.Int, uint64, error) {
switch category {
case types.Warning:
return math.NewInt(layertypes.PowerReduction.Int64()).QuoRaw(100), 0, nil // 1%
case types.Minor:
return math.NewInt(layertypes.PowerReduction.Int64()).QuoRaw(20), 600, nil // 5%
case types.Major:
return layertypes.PowerReduction, gomath.MaxInt64, nil // 100% and jails reporter for a year or 31536000 seconds. Will be deleted or unjailed depending on the results of the dispute
default:
return math.Int{}, 0, types.ErrInvalidDisputeCategory
}
}
// Get dispute fee
func (k Keeper) GetDisputeFee(ctx sdk.Context, rep oracletypes.MicroReport, category types.DisputeCategory) (math.Int, error) {
stake := layertypes.PowerReduction.MulRaw(int64(rep.Power))
switch category {
case types.Warning:
// calculate 1 percent of bond
stakeDec := math.LegacyNewDecFromInt(stake)
feeDec := stakeDec.Mul(math.LegacyNewDec(1)).Quo(math.LegacyNewDec(100))
return feeDec.TruncateInt(), nil
case types.Minor:
// calculate 5 percent of bond
stakeDec := math.LegacyNewDecFromInt(stake)
feeDec := stakeDec.Mul(math.LegacyNewDec(5)).Quo(math.LegacyNewDec(100))
return feeDec.TruncateInt(), nil
case types.Major:
// calculate 100 percent of bond
return stake, nil
default:
return math.Int{}, types.ErrInvalidDisputeCategory
}
}
// Update existing dispute when conditions are met
// if dispute is unresolved then you can ingite another round.
// dispute round will have a new dispute id and the dispute.Round will be incremented.
// previous dispute will be closed.
func (k Keeper) AddDisputeRound(ctx sdk.Context, sender sdk.AccAddress, dispute types.Dispute, msg types.MsgProposeDispute) error {
if dispute.DisputeStatus != types.Unresolved {
return fmt.Errorf("can't start a new round for this dispute %d; dispute status %s", dispute.DisputeId, dispute.DisputeStatus)
}
if !dispute.Open {
return fmt.Errorf("can't start a new round for this dispute %d; dispute closed", dispute.DisputeId)
}
// if dispute is not unresovled and dispute end time is before current block time then we can't update it
if dispute.DisputeEndTime.Before(ctx.BlockTime()) {
return fmt.Errorf("this dispute is expired, can't start new round %d", dispute.DisputeId)
}
if dispute.DisputeRound == 5 {
return fmt.Errorf("can't start a new round for this dispute %d; max dispute rounds has been reached %d", dispute.DisputeId, dispute.DisputeRound)
}
// fee calculates a fee by scaling a base amount (fivePercent) exponentially based on the given round,
// doubling for each successive round.
fee := func(fivePercent math.Int, round int64) math.Int {
base := new(big.Int).Exp(big.NewInt(2), big.NewInt(round), nil)
return fivePercent.Mul(math.NewIntFromBigInt(base))
}
disputeSlashAmountDec := math.LegacyNewDecFromInt(dispute.SlashAmount)
fivePercentDec := disputeSlashAmountDec.Mul(math.LegacyNewDec(1)).Quo(math.LegacyNewDec(20))
fivePercent := fivePercentDec.TruncateInt()
roundFee := fee(fivePercent, int64(dispute.DisputeRound))
if roundFee.GT(dispute.SlashAmount) {
roundFee = dispute.SlashAmount
}
if msg.Fee.Amount.LT(roundFee) {
return fmt.Errorf("fee amount is less than amount required")
} else {
msg.Fee.Amount = roundFee
}
// Pay the dispute fee. Later-round fees are fully consumed (never refunded), so they
// must not be tracked as refundable first-round stake.
if err := k.PayDisputeFee(ctx, sender, msg.Fee, msg.PayFromBond, dispute.HashId, false); err != nil {
return err
}
if err := k.CloseDispute(ctx, dispute.DisputeId); err != nil {
return err
}
prevDisputeId := dispute.DisputeId
dispute.BurnAmount = dispute.BurnAmount.Add(roundFee)
// FeeTotal is informationl only, tracks total fees paid across rounds.
dispute.FeeTotal = dispute.FeeTotal.Add(msg.Fee.Amount)
disputeId := k.NextDisputeId(ctx)
dispute.DisputeId = disputeId
dispute.DisputeStatus = types.Voting // starting voting immediately
dispute.DisputeStartTime = ctx.BlockTime()
// add 3 days to block time
dispute.DisputeEndTime = ctx.BlockTime().Add(THREE_DAYS)
dispute.DisputeStartBlock = uint64(ctx.BlockHeight())
dispute.DisputeRound++
dispute.PrevDisputeIds = append(dispute.PrevDisputeIds, disputeId)
err := k.Disputes.Set(ctx, dispute.DisputeId, dispute)
if err != nil {
return err
}
ctx.EventManager().EmitEvents(sdk.Events{
sdk.NewEvent(
"added_dispute_round",
sdk.NewAttribute("prev_dispute_id", strconv.FormatUint(prevDisputeId, 10)),
sdk.NewAttribute("dispute_id", strconv.FormatUint(disputeId, 10)),
),
})
return k.SetStartVote(ctx, dispute.DisputeId) // starting voting immediately
}
// creates a snapshot of total reporter power and total tips
func (k Keeper) SetBlockInfo(ctx context.Context, hashId []byte) error {
tp, err := k.reporterKeeper.TotalReporterPower(ctx)
if err != nil {
return err
}
tips, err := k.oracleKeeper.GetTotalTips(ctx)
if err != nil {
return err
}
blockInfo := types.BlockInfo{
TotalReporterPower: tp,
TotalUserTips: tips,
}
return k.BlockInfo.Set(ctx, hashId, blockInfo)
}
// close dispute by id
func (k Keeper) CloseDispute(ctx context.Context, id uint64) error {
dispute, err := k.Disputes.Get(ctx, id)
if err != nil {
return err
}
dispute.Open = false
dispute.PendingExecution = false
return k.Disputes.Set(ctx, id, dispute)
}
// gets all open disputes
func (k Keeper) GetOpenDisputes(ctx context.Context) ([]uint64, error) {
iter, err := k.Disputes.Indexes.OpenDisputes.MatchExact(ctx, true)
if err != nil {
return nil, err
}
return iter.PrimaryKeys()
}