0g-chain/internal/x/paychan/endblocker.go

38 lines
970 B
Go
Raw Normal View History

2018-08-24 23:18:41 +00:00
package paychan
import (
sdk "github.com/cosmos/cosmos-sdk/types"
)
2018-08-24 23:18:41 +00:00
2018-08-27 21:58:58 +00:00
func EndBlocker(ctx sdk.Context, k Keeper) sdk.Tags {
var err sdk.Error
var channelTags sdk.Tags
tags := sdk.EmptyTags()
2018-08-24 23:18:41 +00:00
2018-08-27 21:58:58 +00:00
// Iterate through submittedUpdatesQueue
// TODO optimise so it doesn't pull every update from DB every block
var sUpdate SubmittedUpdate
q, found := k.getSubmittedUpdatesQueue(ctx)
if !found {
panic("SubmittedUpdatesQueue not found.")
}
2018-08-27 21:58:58 +00:00
for _, id := range q {
// close the channel if the update has reached its execution time.
// Using >= in case some are somehow missed.
sUpdate, found = k.getSubmittedUpdate(ctx, id)
if !found {
panic("can't find element in queue that should exist")
}
2018-08-27 21:58:58 +00:00
if ctx.BlockHeight() >= sUpdate.ExecutionTime {
2018-08-29 03:45:26 +00:00
k.removeFromSubmittedUpdatesQueue(ctx, sUpdate.ChannelID)
channelTags, err = k.closeChannel(ctx, sUpdate.Update)
if err != nil {
panic(err)
}
tags.AppendTags(channelTags)
2018-08-27 21:58:58 +00:00
}
}
2018-08-24 23:18:41 +00:00
return tags
2018-08-27 21:58:58 +00:00
}