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

feat(delayedack): finalize rollapp packet by packet base64 key #1297

Merged
merged 2 commits into from
Oct 10, 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
14 changes: 14 additions & 0 deletions proto/dymensionxyz/dymension/delayedack/tx.proto
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ service Msg {
// FinalizePacket finalizes a singe packet.
rpc FinalizePacket(MsgFinalizePacket) returns (MsgFinalizePacketResponse);

rpc FinalizePacketByPacketKey(MsgFinalizePacketByPacketKey) returns (MsgFinalizePacketByPacketKeyResponse);

// FinalizePacketsUntilHeight finalizes the packets for the given rollapp until the given height inclusively.
rpc FinalizePacketsUntilHeight(MsgFinalizePacketsUntilHeight) returns (MsgFinalizePacketsUntilHeightResponse);

Expand Down Expand Up @@ -41,6 +43,18 @@ message MsgFinalizePacket {

message MsgFinalizePacketResponse {}

// MsgFinalizePacketByPacketKey finalizes a single packet by the packet key.
message MsgFinalizePacketByPacketKey {
option (cosmos.msg.v1.signer) = "sender";

// Sender is the signer of the message.
string sender = 1;
// PacketKey is a key of the packet.
string packet_key = 2;
}

message MsgFinalizePacketByPacketKeyResponse {}

// MsgFinalizePacketsUntilHeight finalizes packets for the given rollapp until the given height inclusively.
message MsgFinalizePacketsUntilHeight {
option (cosmos.msg.v1.signer) = "sender";
Expand Down
4 changes: 3 additions & 1 deletion proto/dymensionxyz/dymension/eibc/events.proto
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,16 @@ message EventDemandOrderCreated {
bool is_fulfilled = 4;
// packet_status is the status of the packet.
string packet_status = 5;
// packet_key is the key of the packet.
// packet_key is the base64 encoded key of the packet.
string packet_key = 6;
// rollapp_id is the id of the rollapp.
string rollapp_id = 7;
// recipient is the address of the recipient.
string recipient = 8;
// packet_type is the type of the packet.
string packet_type = 9;
// proof_height is the height of the block when order was created.
uint64 proof_height = 10;
}

// EventDemandOrderPacketStatusUpdate is emitted when the status of the related packet is updated.
Expand Down
14 changes: 7 additions & 7 deletions x/delayedack/keeper/finalize.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,26 +75,26 @@ func (k Keeper) FinalizeRollappPacketsByReceiver(ctx sdk.Context, ibc porttypes.
}

// FinalizeRollappPacket finalizes a singe packet by its rollapp packet key.
func (k Keeper) FinalizeRollappPacket(ctx sdk.Context, ibc porttypes.IBCModule, rollappID string, rollappPacketKey string) error {
func (k Keeper) FinalizeRollappPacket(ctx sdk.Context, ibc porttypes.IBCModule, rollappPacketKey string) (*commontypes.RollappPacket, error) {
// Get a rollapp packet
packet, err := k.GetRollappPacket(ctx, rollappPacketKey)
if err != nil {
return fmt.Errorf("get rollapp packet: %w", err)
return nil, fmt.Errorf("get rollapp packet: %w", err)
}

// Verify the height is finalized
err = k.VerifyHeightFinalized(ctx, rollappID, packet.ProofHeight)
err = k.VerifyHeightFinalized(ctx, packet.RollappId, packet.ProofHeight)
if err != nil {
return fmt.Errorf("verify height is finalized: rollapp '%s': %w", rollappID, err)
return packet, fmt.Errorf("verify height is finalized: rollapp '%s': %w", packet.RollappId, err)
}

// Finalize the packet
err = k.finalizeRollappPacket(ctx, ibc, rollappID, *packet)
err = k.finalizeRollappPacket(ctx, ibc, packet.RollappId, *packet)
if err != nil {
return fmt.Errorf("finalize rollapp packet: %w", err)
return packet, fmt.Errorf("finalize rollapp packet: %w", err)
}

return nil
return packet, nil
}

type wrappedFunc func(ctx sdk.Context) error
Expand Down
41 changes: 40 additions & 1 deletion x/delayedack/keeper/msg_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ func (m MsgServer) FinalizePacket(goCtx context.Context, msg *types.MsgFinalizeP

ctx := sdk.UnwrapSDKContext(goCtx)

err = m.k.FinalizeRollappPacket(ctx, m.ibc.NextIBCMiddleware(), msg.RollappId, string(msg.PendingPacketKey()))
_, err = m.k.FinalizeRollappPacket(ctx, m.ibc.NextIBCMiddleware(), string(msg.PendingPacketKey()))
if err != nil {
return nil, err
}
Expand All @@ -56,6 +56,45 @@ func (m MsgServer) FinalizePacket(goCtx context.Context, msg *types.MsgFinalizeP
return &types.MsgFinalizePacketResponse{}, nil
}

func (m MsgServer) FinalizePacketByPacketKey(goCtx context.Context, msg *types.MsgFinalizePacketByPacketKey) (*types.MsgFinalizePacketByPacketKeyResponse, error) {
err := msg.ValidateBasic()
if err != nil {
return nil, err
}

ctx := sdk.UnwrapSDKContext(goCtx)

packetKey := string(msg.MustDecodePacketKey())
packet, err := m.k.FinalizeRollappPacket(ctx, m.ibc.NextIBCMiddleware(), packetKey)
if err != nil {
return nil, err
}

var (
sourceChannel string
sequence uint64
)

if packet.Packet != nil {
sourceChannel = packet.Packet.SourceChannel
sequence = packet.Packet.Sequence
}

err = uevent.EmitTypedEvent(ctx, &types.EventFinalizePacket{
Sender: msg.Sender,
RollappId: packet.RollappId,
PacketProofHeight: packet.ProofHeight,
PacketType: packet.Type,
PacketSrcChannel: sourceChannel,
PacketSequence: sequence,
})
if err != nil {
return nil, fmt.Errorf("emit event: %w", err)
}

return &types.MsgFinalizePacketByPacketKeyResponse{}, nil
}

func (m MsgServer) FinalizePacketsUntilHeight(goCtx context.Context, msg *types.MsgFinalizePacketsUntilHeight) (*types.MsgFinalizePacketsUntilHeightResponse, error) {
err := msg.ValidateBasic()
if err != nil {
Expand Down
47 changes: 47 additions & 0 deletions x/delayedack/types/msgs.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
package types

import (
"bytes"
"encoding/base64"
"errors"
"fmt"

errorsmod "cosmossdk.io/errors"
sdk "github.com/cosmos/cosmos-sdk/types"
Expand Down Expand Up @@ -44,6 +47,50 @@ func (m MsgFinalizePacket) PendingPacketKey() []byte {
)
}

func (m MsgFinalizePacketByPacketKey) ValidateBasic() error {
_, err := sdk.AccAddressFromBech32(m.Sender)
if err != nil {
return errors.Join(
sdkerrors.ErrInvalidAddress,
errorsmod.Wrapf(err, "sender must be a valid bech32 address: %s", m.Sender),
)
}
if len(m.PacketKey) == 0 {
return gerrc.ErrInvalidArgument.Wrap("rollappId must be non-empty")
}

if _, err := m.DecodePacketKey(); err != nil {
return gerrc.ErrInvalidArgument.Wrap("packet key must be a valid base64 encoded string")
}
return nil
}

func (m MsgFinalizePacketByPacketKey) GetSigners() []sdk.AccAddress {
signer, _ := sdk.AccAddressFromBech32(m.Sender)
return []sdk.AccAddress{signer}
}

func (m MsgFinalizePacketByPacketKey) DecodePacketKey() ([]byte, error) {
// decode base64
rollappPacketKeyBytes := make([]byte, base64.StdEncoding.DecodedLen(len(m.PacketKey)))
_, err := base64.StdEncoding.Decode(rollappPacketKeyBytes, []byte(m.PacketKey))
if err != nil {
return nil, err
}

rollappPacketKeyBytes = bytes.TrimRight(rollappPacketKeyBytes, "\x00") // remove padding
return rollappPacketKeyBytes, nil
}

func (m MsgFinalizePacketByPacketKey) MustDecodePacketKey() []byte {
packetKey, err := m.DecodePacketKey()
if err != nil {
panic(fmt.Errorf("failed to decode base64 packet key: %w", err))
}

return packetKey
}

func (m MsgFinalizePacketsUntilHeight) ValidateBasic() error {
_, err := sdk.AccAddressFromBech32(m.Sender)
if err != nil {
Expand Down
Loading
Loading