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

36 lines
927 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
2018-09-02 02:57:42 +00:00
// TODO optimise so it doesn't pull every channel update from DB every block
2018-09-01 23:29:51 +00:00
q := k.getSubmittedUpdatesQueue(ctx)
2018-08-27 21:58:58 +00:00
var sUpdate SubmittedUpdate
2018-09-01 23:29:51 +00:00
var found bool
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
}