Skip to content

Commit 17d3487

Browse files
authored
Add new keys and getter / setter functions (#7378)
* chore: adding keys and keeper getter and setter functions * chore: use BigEndian for sequence in keys
1 parent d82e1cd commit 17d3487

File tree

2 files changed

+125
-0
lines changed

2 files changed

+125
-0
lines changed

modules/core/04-channel/v2/keeper/keeper.go

+101
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import (
1515

1616
"github.com/cosmos/ibc-go/v9/modules/core/04-channel/v2/types"
1717
host "github.com/cosmos/ibc-go/v9/modules/core/24-host"
18+
hostv2 "github.com/cosmos/ibc-go/v9/modules/core/24-host/v2"
1819
"github.com/cosmos/ibc-go/v9/modules/core/exported"
1920
)
2021

@@ -61,3 +62,103 @@ func (k *Keeper) GetCounterparty(ctx context.Context, clientID string) (types.Co
6162
k.cdc.MustUnmarshal(bz, &counterparty)
6263
return counterparty, true
6364
}
65+
66+
// GetPacketReceipt returns the packet receipt from the packet receipt path based on the sourceID and sequence.
67+
func (k *Keeper) GetPacketReceipt(ctx context.Context, sourceID string, sequence uint64) (string, bool) {
68+
store := k.storeService.OpenKVStore(ctx)
69+
bigEndianBz := sdk.Uint64ToBigEndian(sequence)
70+
bz, err := store.Get(hostv2.PacketReceiptKey(sourceID, bigEndianBz))
71+
if err != nil {
72+
panic(err)
73+
}
74+
if len(bz) == 0 {
75+
return "", false
76+
}
77+
return string(bz), true
78+
}
79+
80+
// SetPacketReceipt writes the packet receipt under the receipt path
81+
// This is a public path that is standardized by the IBC V2 specification.
82+
func (k *Keeper) SetPacketReceipt(ctx context.Context, sourceID string, sequence uint64) {
83+
store := k.storeService.OpenKVStore(ctx)
84+
bigEndianBz := sdk.Uint64ToBigEndian(sequence)
85+
if err := store.Set(hostv2.PacketReceiptKey(sourceID, bigEndianBz), []byte{byte(1)}); err != nil {
86+
panic(err)
87+
}
88+
}
89+
90+
// SetPacketAcknowledgement writes the acknowledgement hash under the acknowledgement path
91+
// This is a public path that is standardized by the IBC V2 specification.
92+
func (k *Keeper) SetPacketAcknowledgement(ctx context.Context, sourceID string, sequence uint64, ackHash []byte) {
93+
store := k.storeService.OpenKVStore(ctx)
94+
bigEndianBz := sdk.Uint64ToBigEndian(sequence)
95+
if err := store.Set(hostv2.PacketAcknowledgementKey(sourceID, bigEndianBz), ackHash); err != nil {
96+
panic(err)
97+
}
98+
}
99+
100+
// HasPacketAcknowledgement check if the packet ack hash is already on the store.
101+
func (k *Keeper) HasPacketAcknowledgement(ctx context.Context, sourceID string, sequence uint64) bool {
102+
store := k.storeService.OpenKVStore(ctx)
103+
bigEndianBz := sdk.Uint64ToBigEndian(sequence)
104+
found, err := store.Has(hostv2.PacketAcknowledgementKey(sourceID, bigEndianBz))
105+
if err != nil {
106+
panic(err)
107+
}
108+
109+
return found
110+
}
111+
112+
// GetPacketCommitment returns the packet commitment hash under the commitment path.
113+
func (k *Keeper) GetPacketCommitment(ctx context.Context, sourceID string, sequence uint64) (string, bool) {
114+
store := k.storeService.OpenKVStore(ctx)
115+
bigEndianBz := sdk.Uint64ToBigEndian(sequence)
116+
bz, err := store.Get(hostv2.PacketCommitmentKey(sourceID, bigEndianBz))
117+
if err != nil {
118+
panic(err)
119+
}
120+
if len(bz) == 0 {
121+
return "", false
122+
}
123+
return string(bz), true
124+
}
125+
126+
// SetPacketCommitment writes the commitment hash under the commitment path.
127+
func (k *Keeper) SetPacketCommitment(ctx context.Context, sourceID string, sequence uint64, commitment []byte) {
128+
store := k.storeService.OpenKVStore(ctx)
129+
bigEndianBz := sdk.Uint64ToBigEndian(sequence)
130+
if err := store.Set(hostv2.PacketCommitmentKey(sourceID, bigEndianBz), commitment); err != nil {
131+
panic(err)
132+
}
133+
}
134+
135+
// DeletePacketCommitment deletes the packet commitment hash under the commitment path.
136+
func (k *Keeper) DeletePacketCommitment(ctx context.Context, sourceID string, sequence uint64) {
137+
store := k.storeService.OpenKVStore(ctx)
138+
bigEndianBz := sdk.Uint64ToBigEndian(sequence)
139+
if err := store.Delete(hostv2.PacketCommitmentKey(sourceID, bigEndianBz)); err != nil {
140+
panic(err)
141+
}
142+
}
143+
144+
// GetNextSequenceSend returns the next send sequence from the sequence path
145+
func (k *Keeper) GetNextSequenceSend(ctx context.Context, sourceID string) (uint64, bool) {
146+
store := k.storeService.OpenKVStore(ctx)
147+
bz, err := store.Get(hostv2.NextSequenceSendKey(sourceID))
148+
if err != nil {
149+
panic(err)
150+
}
151+
if len(bz) == 0 {
152+
return 0, false
153+
}
154+
return sdk.BigEndianToUint64(bz), true
155+
}
156+
157+
// SetNextSequenceSend writes the next send sequence under the sequence path
158+
func (k *Keeper) SetNextSequenceSend(ctx context.Context, sourceID string, sequence uint64) {
159+
store := k.storeService.OpenKVStore(ctx)
160+
bigEndianBz := sdk.Uint64ToBigEndian(sequence)
161+
if err := store.Set(hostv2.NextSequenceSendKey(sourceID), bigEndianBz); err != nil {
162+
panic(err)
163+
}
164+
}
+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package v2
2+
3+
import "fmt"
4+
5+
// PacketReceiptKey returns the store key of under which a packet
6+
// receipt is stored
7+
func PacketReceiptKey(sourceID string, bigEndianSequence []byte) []byte {
8+
return []byte(fmt.Sprintf("receipts/channels/%s/sequences/%s", sourceID, string(bigEndianSequence)))
9+
}
10+
11+
// PacketAcknowledgementKey returns the store key of under which a packet acknowledgement is stored.
12+
func PacketAcknowledgementKey(sourceID string, bigEndianSequence []byte) []byte {
13+
return []byte(fmt.Sprintf("acks/channels/%s/sequences/%s", sourceID, string(bigEndianSequence)))
14+
}
15+
16+
// PacketCommitmentKey returns the store key of under which a packet commitment is stored.
17+
func PacketCommitmentKey(sourceID string, bigEndianSequence []byte) []byte {
18+
return []byte(fmt.Sprintf("commitments/channels/%s/sequences/%s", sourceID, string(bigEndianSequence)))
19+
}
20+
21+
// NextSequenceSendKey returns the store key for the next sequence send of a given sourceID.
22+
func NextSequenceSendKey(sourceID string) []byte {
23+
return []byte(fmt.Sprintf("nextSequenceSend/%s", sourceID))
24+
}

0 commit comments

Comments
 (0)