-
Notifications
You must be signed in to change notification settings - Fork 58
/
pending_send.go
78 lines (64 loc) · 2.79 KB
/
pending_send.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
66
67
68
69
70
71
72
73
74
75
76
77
78
package keeper
import (
"encoding/binary"
"fmt"
"strings"
"cosmossdk.io/store/prefix"
storetypes "cosmossdk.io/store/types"
"github.com/cosmos/cosmos-sdk/runtime"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/ibc-apps/modules/rate-limiting/v8/types"
)
// Sets the sequence number of a packet that was just sent
func (k Keeper) SetPendingSendPacket(ctx sdk.Context, channelId string, sequence uint64) {
adapter := runtime.KVStoreAdapter(k.storeService.OpenKVStore(ctx))
store := prefix.NewStore(adapter, types.PendingSendPacketPrefix)
key := types.GetPendingSendPacketKey(channelId, sequence)
store.Set(key, []byte{1})
}
// Remove a pending packet sequence number from the store
// Used after the ack or timeout for a packet has been received
func (k Keeper) RemovePendingSendPacket(ctx sdk.Context, channelId string, sequence uint64) {
adapter := runtime.KVStoreAdapter(k.storeService.OpenKVStore(ctx))
store := prefix.NewStore(adapter, types.PendingSendPacketPrefix)
key := types.GetPendingSendPacketKey(channelId, sequence)
store.Delete(key)
}
// Checks whether the packet sequence number is in the store - indicating that it was
// sent during the current quota
func (k Keeper) CheckPacketSentDuringCurrentQuota(ctx sdk.Context, channelId string, sequence uint64) bool {
adapter := runtime.KVStoreAdapter(k.storeService.OpenKVStore(ctx))
store := prefix.NewStore(adapter, types.PendingSendPacketPrefix)
key := types.GetPendingSendPacketKey(channelId, sequence)
valueBz := store.Get(key)
found := len(valueBz) != 0
return found
}
// Get all pending packet sequence numbers
func (k Keeper) GetAllPendingSendPackets(ctx sdk.Context) []string {
adapter := runtime.KVStoreAdapter(k.storeService.OpenKVStore(ctx))
store := prefix.NewStore(adapter, types.PendingSendPacketPrefix)
iterator := store.Iterator(nil, nil)
defer iterator.Close()
pendingPackets := []string{}
for ; iterator.Valid(); iterator.Next() {
key := iterator.Key()
channelId := string(key[:types.PendingSendPacketChannelLength])
channelId = strings.TrimRight(channelId, "\x00") // removes null bytes from suffix
sequence := binary.BigEndian.Uint64(key[types.PendingSendPacketChannelLength:])
packetId := fmt.Sprintf("%s/%d", channelId, sequence)
pendingPackets = append(pendingPackets, packetId)
}
return pendingPackets
}
// Remove all pending sequence numbers from the store
// This is executed when the quota resets
func (k Keeper) RemoveAllChannelPendingSendPackets(ctx sdk.Context, channelId string) {
adapter := runtime.KVStoreAdapter(k.storeService.OpenKVStore(ctx))
store := prefix.NewStore(adapter, types.PendingSendPacketPrefix)
iterator := storetypes.KVStorePrefixIterator(store, types.KeyPrefix(channelId))
defer iterator.Close()
for ; iterator.Valid(); iterator.Next() {
store.Delete(iterator.Key())
}
}