-
Notifications
You must be signed in to change notification settings - Fork 330
/
distr_info.go
65 lines (55 loc) · 1.61 KB
/
distr_info.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
package keeper
import (
"github.com/dymensionxyz/dymension/v3/x/streamer/types"
sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
)
func (k Keeper) NewDistrInfo(ctx sdk.Context, records []types.DistrRecord) (*types.DistrInfo, error) {
err := k.validateGauges(ctx, records)
if err != nil {
return nil, err
}
distrInfo, err := types.NewDistrInfo(records)
if err != nil {
return nil, err
}
return distrInfo, nil
}
// validateGauges validates a list of records to ensure that:
// 1) there are no duplicates,
// 2) the records are in sorted order.
// 3) the records only pay to gauges that exist.
func (k Keeper) validateGauges(ctx sdk.Context, records []types.DistrRecord) error {
lastGaugeID := uint64(0)
gaugeIdFlags := make(map[uint64]bool)
for _, record := range records {
if gaugeIdFlags[record.GaugeId] {
return sdkerrors.Wrapf(
types.ErrDistrRecordRegisteredGauge,
"Gauge ID #%d has duplications.",
record.GaugeId,
)
}
// Ensure records are sorted because ~AESTHETIC~
if record.GaugeId < lastGaugeID {
return sdkerrors.Wrapf(
types.ErrDistrRecordNotSorted,
"Gauge ID #%d came after Gauge ID #%d.",
record.GaugeId, lastGaugeID,
)
}
lastGaugeID = record.GaugeId
// don't allow distribution records for gauges that don't exist
gauge, err := k.ik.GetGaugeByID(ctx, record.GaugeId)
if err != nil {
return err
}
if !gauge.IsPerpetual {
return sdkerrors.Wrapf(types.ErrDistrRecordRegisteredGauge,
"Gauge ID #%d is not perpetual.",
record.GaugeId)
}
gaugeIdFlags[record.GaugeId] = true
}
return nil
}