Skip to content

Commit

Permalink
Add new keys and getter / setter functions (#7378)
Browse files Browse the repository at this point in the history
* chore: adding keys and keeper getter and setter functions

* chore: use BigEndian for sequence in keys
  • Loading branch information
chatton authored Oct 2, 2024
1 parent d82e1cd commit 17d3487
Show file tree
Hide file tree
Showing 2 changed files with 125 additions and 0 deletions.
101 changes: 101 additions & 0 deletions modules/core/04-channel/v2/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (

"github.com/cosmos/ibc-go/v9/modules/core/04-channel/v2/types"
host "github.com/cosmos/ibc-go/v9/modules/core/24-host"
hostv2 "github.com/cosmos/ibc-go/v9/modules/core/24-host/v2"
"github.com/cosmos/ibc-go/v9/modules/core/exported"
)

Expand Down Expand Up @@ -61,3 +62,103 @@ func (k *Keeper) GetCounterparty(ctx context.Context, clientID string) (types.Co
k.cdc.MustUnmarshal(bz, &counterparty)
return counterparty, true
}

// GetPacketReceipt returns the packet receipt from the packet receipt path based on the sourceID and sequence.
func (k *Keeper) GetPacketReceipt(ctx context.Context, sourceID string, sequence uint64) (string, bool) {
store := k.storeService.OpenKVStore(ctx)
bigEndianBz := sdk.Uint64ToBigEndian(sequence)
bz, err := store.Get(hostv2.PacketReceiptKey(sourceID, bigEndianBz))
if err != nil {
panic(err)
}
if len(bz) == 0 {
return "", false
}
return string(bz), true
}

// SetPacketReceipt writes the packet receipt under the receipt path
// This is a public path that is standardized by the IBC V2 specification.
func (k *Keeper) SetPacketReceipt(ctx context.Context, sourceID string, sequence uint64) {
store := k.storeService.OpenKVStore(ctx)
bigEndianBz := sdk.Uint64ToBigEndian(sequence)
if err := store.Set(hostv2.PacketReceiptKey(sourceID, bigEndianBz), []byte{byte(1)}); err != nil {
panic(err)
}
}

// SetPacketAcknowledgement writes the acknowledgement hash under the acknowledgement path
// This is a public path that is standardized by the IBC V2 specification.
func (k *Keeper) SetPacketAcknowledgement(ctx context.Context, sourceID string, sequence uint64, ackHash []byte) {
store := k.storeService.OpenKVStore(ctx)
bigEndianBz := sdk.Uint64ToBigEndian(sequence)
if err := store.Set(hostv2.PacketAcknowledgementKey(sourceID, bigEndianBz), ackHash); err != nil {
panic(err)
}
}

// HasPacketAcknowledgement check if the packet ack hash is already on the store.
func (k *Keeper) HasPacketAcknowledgement(ctx context.Context, sourceID string, sequence uint64) bool {
store := k.storeService.OpenKVStore(ctx)
bigEndianBz := sdk.Uint64ToBigEndian(sequence)
found, err := store.Has(hostv2.PacketAcknowledgementKey(sourceID, bigEndianBz))
if err != nil {
panic(err)
}

return found
}

// GetPacketCommitment returns the packet commitment hash under the commitment path.
func (k *Keeper) GetPacketCommitment(ctx context.Context, sourceID string, sequence uint64) (string, bool) {
store := k.storeService.OpenKVStore(ctx)
bigEndianBz := sdk.Uint64ToBigEndian(sequence)
bz, err := store.Get(hostv2.PacketCommitmentKey(sourceID, bigEndianBz))
if err != nil {
panic(err)
}
if len(bz) == 0 {
return "", false
}
return string(bz), true
}

// SetPacketCommitment writes the commitment hash under the commitment path.
func (k *Keeper) SetPacketCommitment(ctx context.Context, sourceID string, sequence uint64, commitment []byte) {
store := k.storeService.OpenKVStore(ctx)
bigEndianBz := sdk.Uint64ToBigEndian(sequence)
if err := store.Set(hostv2.PacketCommitmentKey(sourceID, bigEndianBz), commitment); err != nil {
panic(err)
}
}

// DeletePacketCommitment deletes the packet commitment hash under the commitment path.
func (k *Keeper) DeletePacketCommitment(ctx context.Context, sourceID string, sequence uint64) {
store := k.storeService.OpenKVStore(ctx)
bigEndianBz := sdk.Uint64ToBigEndian(sequence)
if err := store.Delete(hostv2.PacketCommitmentKey(sourceID, bigEndianBz)); err != nil {
panic(err)
}
}

// GetNextSequenceSend returns the next send sequence from the sequence path
func (k *Keeper) GetNextSequenceSend(ctx context.Context, sourceID string) (uint64, bool) {
store := k.storeService.OpenKVStore(ctx)
bz, err := store.Get(hostv2.NextSequenceSendKey(sourceID))
if err != nil {
panic(err)
}
if len(bz) == 0 {
return 0, false
}
return sdk.BigEndianToUint64(bz), true
}

// SetNextSequenceSend writes the next send sequence under the sequence path
func (k *Keeper) SetNextSequenceSend(ctx context.Context, sourceID string, sequence uint64) {
store := k.storeService.OpenKVStore(ctx)
bigEndianBz := sdk.Uint64ToBigEndian(sequence)
if err := store.Set(hostv2.NextSequenceSendKey(sourceID), bigEndianBz); err != nil {
panic(err)
}
}
24 changes: 24 additions & 0 deletions modules/core/24-host/v2/packet_keys.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package v2

import "fmt"

// PacketReceiptKey returns the store key of under which a packet
// receipt is stored
func PacketReceiptKey(sourceID string, bigEndianSequence []byte) []byte {
return []byte(fmt.Sprintf("receipts/channels/%s/sequences/%s", sourceID, string(bigEndianSequence)))
}

// PacketAcknowledgementKey returns the store key of under which a packet acknowledgement is stored.
func PacketAcknowledgementKey(sourceID string, bigEndianSequence []byte) []byte {
return []byte(fmt.Sprintf("acks/channels/%s/sequences/%s", sourceID, string(bigEndianSequence)))
}

// PacketCommitmentKey returns the store key of under which a packet commitment is stored.
func PacketCommitmentKey(sourceID string, bigEndianSequence []byte) []byte {
return []byte(fmt.Sprintf("commitments/channels/%s/sequences/%s", sourceID, string(bigEndianSequence)))
}

// NextSequenceSendKey returns the store key for the next sequence send of a given sourceID.
func NextSequenceSendKey(sourceID string) []byte {
return []byte(fmt.Sprintf("nextSequenceSend/%s", sourceID))
}

0 comments on commit 17d3487

Please sign in to comment.