Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add new keys and getter / setter functions #7378

Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
94 changes: 94 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"
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

new keys have been added under a host/v2

"github.com/cosmos/ibc-go/v9/modules/core/exported"
)

Expand Down Expand Up @@ -61,3 +62,96 @@ 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) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oh, weren't all sequences in keys now to be stored using big endian?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fine if issue for later too (might already exist and I missed it)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sequences should be in big endian as specified by @DimitrisJim. Is there a way to specify that in the go code? Which is the default option in go?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

store := k.storeService.OpenKVStore(ctx)
bz, err := store.Get(hostv2.PacketReceiptKey(sourceID, sequence))
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)
if err := store.Set(hostv2.PacketReceiptKey(sourceID, sequence), []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)
if err := store.Set(hostv2.PacketAcknowledgementKey(sourceID, sequence), 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)
found, err := store.Has(hostv2.PacketAcknowledgementKey(sourceID, sequence))
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)
bz, err := store.Get(hostv2.PacketCommitmentKey(sourceID, sequence))
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)
if err := store.Set(hostv2.PacketCommitmentKey(sourceID, sequence), 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)
if err := store.Delete(hostv2.PacketCommitmentKey(sourceID, sequence)); 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) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

unblocks #7327

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)
bz := sdk.Uint64ToBigEndian(sequence)
if err := store.Set(hostv2.NextSequenceSendKey(sourceID), bz); err != nil {
panic(err)
}
}
24 changes: 24 additions & 0 deletions modules/core/24-host/v2/packet_keys.go
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

With new keys we should probably consider how we format the sequence in order to support effecient iteration, right?
I think this was an issue before, as formatting ended up in lexographical ordering rather than numerical(?)

E.g. format sequence using big endian bytes rather than decimal string

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes! Was just chatting to @DimitrisJim about it, seems like we do indeed want to use BigEndian, pushing changes now.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should've refreshed my browser before publishing review... 😆

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, sequence uint64) []byte {
return []byte(fmt.Sprintf("receipts/channels/%s/sequences/%d", sourceID, sequence))
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we can introduce utility functions if needed, but I didn't think it was necessary here

}

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

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

// 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))
}
Loading