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

nico/store virtual packet #9

Merged
merged 2 commits into from
May 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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
28 changes: 28 additions & 0 deletions modules/core/04-channel/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,34 @@ func (k Keeper) deletePacketCommitment(ctx sdk.Context, portID, channelID string
store.Delete(host.PacketCommitmentKey(portID, channelID, sequence))
}

func (k Keeper) SetVirtualPacket(ctx sdk.Context, portID, channelID string, sequence uint64, packet exported.PacketI) {
timeoutHeight := packet.GetTimeoutHeight()
p := types.NewPacket(
packet.GetData(),
packet.GetSequence(),
packet.GetSourcePort(),
packet.GetSourceChannel(),
packet.GetDestPort(),
packet.GetDestChannel(),
clienttypes.NewHeight(timeoutHeight.GetRevisionNumber(), timeoutHeight.GetRevisionHeight()),
packet.GetTimeoutTimestamp(),
)
store := ctx.KVStore(k.storeKey)
bz := k.cdc.MustMarshal(&p)
store.Set(host.VirtualPacketKey(portID, channelID, sequence), bz)
}

func (k Keeper) GetVirtualPacket(ctx sdk.Context, portID, channelID string, sequence uint64) (types.Packet, bool) {
store := ctx.KVStore(k.storeKey)
bz := store.Get(host.VirtualPacketKey(portID, channelID, sequence))
if len(bz) == 0 {
return types.Packet{}, false
}
var packet types.Packet
k.cdc.MustUnmarshal(bz, &packet)
return packet, true
}

// SetPacketAcknowledgement sets the packet ack hash to the store
func (k Keeper) SetPacketAcknowledgement(ctx sdk.Context, portID, channelID string, sequence uint64, ackHash []byte) {
store := ctx.KVStore(k.storeKey)
Expand Down
1 change: 1 addition & 0 deletions modules/core/04-channel/keeper/packet.go
Original file line number Diff line number Diff line change
Expand Up @@ -283,6 +283,7 @@ func (k Keeper) RecvPacket(

// emit an event that the relayer can query for
EmitRecvPacketEvent(ctx, packet, channel)
k.SetVirtualPacket(ctx, packet.GetDestPort(), packet.GetDestChannel(), packet.GetSequence(), packet)

return nil
}
Expand Down
16 changes: 16 additions & 0 deletions modules/core/24-host/keys.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ const (
KeyNextSeqRecvPrefix = "nextSequenceRecv"
KeyNextSeqAckPrefix = "nextSequenceAck"
KeyPacketCommitmentPrefix = "commitments"
KeyVirtualPacketPrefix = "virtualPacket"
KeyPacketAckPrefix = "acks"
KeyPacketReceiptPrefix = "receipts"
)
Expand Down Expand Up @@ -186,6 +187,21 @@ func PacketCommitmentKey(portID, channelID string, sequence uint64) []byte {
return []byte(PacketCommitmentPath(portID, channelID, sequence))
}

// Defines the prefix for virtual packet store path.
func VirtualPacketPrefixPath(portID, channelID string) string {
return fmt.Sprintf("%s/%s/%s", KeyVirtualPacketPrefix, channelPath(portID, channelID), KeySequencePrefix)
}

// Defines the virtual packet store path
func VirtualPacketPath(portID, channelID string, sequence uint64) string {
return fmt.Sprintf("%s/%d", VirtualPacketPrefixPath(portID, channelID), sequence)
}

// Returns the store key of under which a virtual packet is stored
func VirtualPacketKey(portID, channelID string, sequence uint64) []byte {
return []byte(VirtualPacketPath(portID, channelID, sequence))
}

// PacketCommitmentPrefixPath defines the prefix for commitments to packet data fields store path.
func PacketCommitmentPrefixPath(portID, channelID string) string {
return fmt.Sprintf("%s/%s/%s", KeyPacketCommitmentPrefix, channelPath(portID, channelID), KeySequencePrefix)
Expand Down
Loading