Skip to content

Commit

Permalink
Distribution end epoch error handling (#322)
Browse files Browse the repository at this point in the history
* distribution end epoch error handling

* error handling consistency
  • Loading branch information
antstalepresh authored Jun 16, 2021
1 parent 5d55c4d commit d3e00da
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 22 deletions.
4 changes: 2 additions & 2 deletions x/incentives/keeper/export_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ func (k Keeper) AddGaugeRefByKey(ctx sdk.Context, key []byte, guageID uint64) er
return k.addGaugeRefByKey(ctx, key, guageID)
}

func (k Keeper) DeleteGaugeRefByKey(ctx sdk.Context, key []byte, guageID uint64) {
k.deleteGaugeRefByKey(ctx, key, guageID)
func (k Keeper) DeleteGaugeRefByKey(ctx sdk.Context, key []byte, guageID uint64) error {
return k.deleteGaugeRefByKey(ctx, key, guageID)
}

func (k Keeper) GetGaugeRefs(ctx sdk.Context, key []byte) []uint64 {
Expand Down
37 changes: 26 additions & 11 deletions x/incentives/keeper/gauge.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,11 +86,17 @@ func (k Keeper) SetGaugeWithRefKey(ctx sdk.Context, gauge *types.Gauge) error {
curTime := ctx.BlockTime()
timeKey := getTimeKey(gauge.StartTime)
if gauge.IsUpcomingGauge(curTime) {
k.addGaugeRefByKey(ctx, combineKeys(types.KeyPrefixUpcomingGauges, timeKey), gauge.Id)
if err := k.addGaugeRefByKey(ctx, combineKeys(types.KeyPrefixUpcomingGauges, timeKey), gauge.Id); err != nil {
return err
}
} else if gauge.IsActiveGauge(curTime) {
k.addGaugeRefByKey(ctx, combineKeys(types.KeyPrefixActiveGauges, timeKey), gauge.Id)
if err := k.addGaugeRefByKey(ctx, combineKeys(types.KeyPrefixActiveGauges, timeKey), gauge.Id); err != nil {
return err
}
} else {
k.addGaugeRefByKey(ctx, combineKeys(types.KeyPrefixFinishedGauges, timeKey), gauge.Id)
if err := k.addGaugeRefByKey(ctx, combineKeys(types.KeyPrefixFinishedGauges, timeKey), gauge.Id); err != nil {
return err
}
}
store := ctx.KVStore(k.storeKey)
bz, err := proto.Marshal(gauge)
Expand Down Expand Up @@ -163,24 +169,33 @@ func (k Keeper) AddToGaugeRewards(ctx sdk.Context, owner sdk.AccAddress, coins s
// BeginDistribution is a utility to begin distribution for a specific gauge
func (k Keeper) BeginDistribution(ctx sdk.Context, gauge types.Gauge) error {
// validation for current time and distribution start time
curTime := ctx.BlockTime()
if curTime.Before(gauge.StartTime) {
return fmt.Errorf("gauge is not able to start distribution yet: %s >= %s", curTime.String(), gauge.StartTime.String())
if ctx.BlockTime().Before(gauge.StartTime) {
return fmt.Errorf("gauge is not able to start distribution yet: %s >= %s", ctx.BlockTime().String(), gauge.StartTime.String())
}

timeKey := getTimeKey(gauge.StartTime)
k.deleteGaugeRefByKey(ctx, combineKeys(types.KeyPrefixUpcomingGauges, timeKey), gauge.Id)
k.addGaugeRefByKey(ctx, combineKeys(types.KeyPrefixActiveGauges, timeKey), gauge.Id)
if err := k.deleteGaugeRefByKey(ctx, combineKeys(types.KeyPrefixUpcomingGauges, timeKey), gauge.Id); err != nil {
return err
}
if err := k.addGaugeRefByKey(ctx, combineKeys(types.KeyPrefixActiveGauges, timeKey), gauge.Id); err != nil {
return err
}
k.hooks.AfterStartDistribution(ctx, gauge.Id)
return nil
}

// FinishDistribution is a utility to finish distribution for a specific gauge
func (k Keeper) FinishDistribution(ctx sdk.Context, gauge types.Gauge) error {
timeKey := getTimeKey(gauge.StartTime)
k.deleteGaugeRefByKey(ctx, combineKeys(types.KeyPrefixActiveGauges, timeKey), gauge.Id)
k.addGaugeRefByKey(ctx, combineKeys(types.KeyPrefixFinishedGauges, timeKey), gauge.Id)
k.deleteGaugeIDForDenom(ctx, gauge.Id, gauge.DistributeTo.Denom)
if err := k.deleteGaugeRefByKey(ctx, combineKeys(types.KeyPrefixActiveGauges, timeKey), gauge.Id); err != nil {
return err
}
if err := k.addGaugeRefByKey(ctx, combineKeys(types.KeyPrefixFinishedGauges, timeKey), gauge.Id); err != nil {
return err
}
if err := k.deleteGaugeIDForDenom(ctx, gauge.Id, gauge.DistributeTo.Denom); err != nil {
return err
}
k.hooks.AfterFinishDistribution(ctx, gauge.Id)
return nil
}
Expand Down
15 changes: 11 additions & 4 deletions x/incentives/keeper/hooks.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +14,25 @@ func (k Keeper) AfterEpochEnd(ctx sdk.Context, epochIdentifier string, epochNumb
// begin distribution if it's start time
gauges := k.GetUpcomingGauges(ctx)
for _, gauge := range gauges {
if gauge.StartTime.Before(ctx.BlockTime()) {
k.BeginDistribution(ctx, gauge)
if !ctx.BlockTime().Before(gauge.StartTime) {
if err := k.BeginDistribution(ctx, gauge); err != nil {
panic(err)
}
}
}

// distribute due to epoch event
gauges = k.GetActiveGauges(ctx)
for _, gauge := range gauges {
k.Distribute(ctx, gauge)
_, err := k.Distribute(ctx, gauge)
if err != nil {
panic(err)
}
// filled epoch is increased in this step and we compare with +1
if !gauge.IsPerpetual && gauge.NumEpochsPaidOver <= gauge.FilledEpochs+1 {
k.FinishDistribution(ctx, gauge)
if err := k.FinishDistribution(ctx, gauge); err != nil {
panic(err)
}
}
}
}
Expand Down
11 changes: 6 additions & 5 deletions x/incentives/keeper/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,23 +66,24 @@ func (k Keeper) addGaugeRefByKey(ctx sdk.Context, key []byte, gaugeID uint64) er
}

// deleteGaugeRefByKey removes gauge ID from an array associated to provided key
func (k Keeper) deleteGaugeRefByKey(ctx sdk.Context, key []byte, gaugeID uint64) {
func (k Keeper) deleteGaugeRefByKey(ctx sdk.Context, key []byte, gaugeID uint64) error {
var index = -1
store := ctx.KVStore(k.storeKey)
gaugeIDs := k.getGaugeRefs(ctx, key)
gaugeIDs, index = removeValue(gaugeIDs, gaugeID)
if index < 0 {
panic(fmt.Sprintf("specific gauge with ID %d not found", gaugeID))
return fmt.Errorf("specific gauge with ID %d not found", gaugeID)
}
if len(gaugeIDs) == 0 {
store.Delete(key)
} else {
bz, err := json.Marshal(gaugeIDs)
if err != nil {
panic(err)
return err
}
store.Set(key, bz)
}
return nil
}

// getAllGaugeIDsByDenom returns all active gauge-IDs associated with lockups of denomination `denom`
Expand All @@ -91,8 +92,8 @@ func (k Keeper) getAllGaugeIDsByDenom(ctx sdk.Context, denom string) []uint64 {
}

// deleteGaugeIDForDenom deletes ID from the list of gauge ID's associated with denomination `denom`
func (k Keeper) deleteGaugeIDForDenom(ctx sdk.Context, ID uint64, denom string) {
k.deleteGaugeRefByKey(ctx, gaugeDenomStoreKey(denom), ID)
func (k Keeper) deleteGaugeIDForDenom(ctx sdk.Context, ID uint64, denom string) error {
return k.deleteGaugeRefByKey(ctx, gaugeDenomStoreKey(denom), ID)
}

// addGaugeIDForDenom adds ID to the list of gauge ID's associated with denomination `denom`
Expand Down

0 comments on commit d3e00da

Please sign in to comment.