Unverified Commit d3e00da9 authored by antstalepresh's avatar antstalepresh Committed by GitHub
Browse files

Distribution end epoch error handling (#322)

* distribution end epoch error handling

* error handling consistency
parent 5d55c4d1
Showing with 45 additions and 22 deletions
+45 -22
......@@ -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 {
......
......@@ -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)
......@@ -163,14 +169,17 @@ 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
}
......@@ -178,9 +187,15 @@ func (k Keeper) BeginDistribution(ctx sdk.Context, gauge types.Gauge) error {
// 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
}
......
......@@ -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)
}
}
}
}
......
......@@ -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`
......@@ -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`
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment