Skip to content
This repository has been archived by the owner on Feb 23, 2024. It is now read-only.

Commit

Permalink
Better validation of port and channel. Use string for next memo
Browse files Browse the repository at this point in the history
  • Loading branch information
agouin committed Nov 4, 2022
1 parent da2ecc9 commit 26fedfe
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 19 deletions.
31 changes: 13 additions & 18 deletions router/keeper/keeper.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package keeper

import (
"encoding/json"
"fmt"
"time"

Expand Down Expand Up @@ -38,23 +37,24 @@ type PacketMetadata struct {
}

type ForwardMetadata struct {
Receiver string `json:"receiver"`
Port string `json:"port"`
Channel string `json:"channel"`
Timeout time.Duration `json:"timeout"`
Retries *uint8 `json:"retries"`
Next *PacketMetadata `json:"next"`
SourceKey string
Receiver string `json:"receiver"`
Port string `json:"port"`
Channel string `json:"channel"`
Timeout time.Duration `json:"timeout"`
Retries *uint8 `json:"retries"`
Next string `json:"next"`
}

func (m *ForwardMetadata) Validate() error {
if m.Receiver == "" {
return fmt.Errorf("failed to validate forward metadata. receiver cannot be empty")
}
if m.Port == "" {
return fmt.Errorf("failed to validate forward metadata. port cannot be empty")
if err := host.PortIdentifierValidator(m.Port); err != nil {
return fmt.Errorf("failed to validate forward metadata: %w", err)
}
if m.Channel == "" {
return fmt.Errorf("failed to validate forward metadata. channel cannot be empty")
if err := host.ChannelIdentifierValidator(m.Channel); err != nil {
return fmt.Errorf("failed to validate forward metadata: %w", err)
}

return nil
Expand Down Expand Up @@ -142,13 +142,8 @@ func (k Keeper) ForwardTransferPacket(
uint64(ctx.BlockTime().UnixNano())+uint64(timeout.Nanoseconds()),
)

if metadata.Next != nil {
memo, err := json.Marshal(metadata.Next)
if err != nil {
return sdkerrors.Wrapf(sdkerrors.ErrJSONMarshal, err.Error())
}
msgTransfer.Memo = string(memo)
}
// set memo for next transfer with next from this transfer.
msgTransfer.Memo = metadata.Next

// send tokens to destination
res, err := k.transferKeeper.Transfer(
Expand Down
4 changes: 3 additions & 1 deletion router/module_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -304,12 +304,14 @@ func TestOnRecvPacket_ForwardMultihop(t *testing.T) {
Channel: channel2,
},
}
next, err := json.Marshal(nextMetadata)
require.NoError(t, err)
packetOrig := transferPacket(t, hostAddr, &keeper.PacketMetadata{
Forward: &keeper.ForwardMetadata{
Receiver: hostAddr2,
Port: port,
Channel: channel,
Next: nextMetadata,
Next: string(next),
},
})
packet2 := transferPacket(t, hostAddr2, nextMetadata)
Expand Down

0 comments on commit 26fedfe

Please sign in to comment.