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

Commit

Permalink
fix: middleware panic upon receiving amount that is not int64; added …
Browse files Browse the repository at this point in the history
…test (#78)

resolves #77
  • Loading branch information
xlab authored and boojamya committed May 23, 2023
1 parent c31b9ca commit f6ef0a5
Show file tree
Hide file tree
Showing 2 changed files with 107 additions and 7 deletions.
12 changes: 7 additions & 5 deletions router/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -302,11 +302,13 @@ func (k *Keeper) ForwardTransferPacket(
store.Set(key, bz)

defer func() {
telemetry.SetGaugeWithLabels(
[]string{"tx", "msg", "ibc", "transfer"},
float32(token.Amount.Int64()),
[]metrics.Label{telemetry.NewLabel(coretypes.LabelDenom, token.Denom)},
)
if token.Amount.IsInt64() {
telemetry.SetGaugeWithLabels(
[]string{"tx", "msg", "ibc", "transfer"},
float32(token.Amount.Int64()),
[]metrics.Label{telemetry.NewLabel(coretypes.LabelDenom, token.Denom)},
)
}

telemetry.IncrCounterWithLabels(
[]string{"ibc", types.ModuleName, "send"},
Expand Down
102 changes: 100 additions & 2 deletions router/module_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,9 @@ import (
)

var (
testDenom = "uatom"
testAmount = "100"
testDenom = "uatom"
testAmount = "100"
testAmount256 = "100000000000000000000"

testSourcePort = "transfer"
testSourceChannel = "channel-10"
Expand Down Expand Up @@ -64,6 +65,36 @@ func transferPacket(t *testing.T, receiver string, metadata any) channeltypes.Pa
}
}

func transferPacket256(t *testing.T, receiver string, metadata any) channeltypes.Packet {
t.Helper()
transferPacket := transfertypes.FungibleTokenPacketData{
Denom: testDenom,
Amount: testAmount256,
Receiver: receiver,
}

if metadata != nil {
if mStr, ok := metadata.(string); ok {
transferPacket.Memo = mStr
} else {
memo, err := json.Marshal(metadata)
require.NoError(t, err)
transferPacket.Memo = string(memo)
}
}

transferData, err := transfertypes.ModuleCdc.MarshalJSON(&transferPacket)
require.NoError(t, err)

return channeltypes.Packet{
SourcePort: testSourcePort,
SourceChannel: testSourceChannel,
DestinationPort: testDestinationPort,
DestinationChannel: testDestinationChannel,
Data: transferData,
}
}

func TestOnRecvPacket_EmptyPacket(t *testing.T) {
ctl := gomock.NewController(t)
defer ctl.Finish()
Expand Down Expand Up @@ -228,6 +259,73 @@ func TestOnRecvPacket_ForwardNoFee(t *testing.T) {
require.NoError(t, err)
}

func TestOnRecvPacket_ForwardAmountInt256(t *testing.T) {
var err error
ctl := gomock.NewController(t)
defer ctl.Finish()
setup := test.NewTestSetup(t, ctl)
ctx := setup.Initializer.Ctx
cdc := setup.Initializer.Marshaler
forwardMiddleware := setup.ForwardMiddleware

// Test data
const (
hostAddr = "cosmos1vzxkv3lxccnttr9rs0002s93sgw72h7ghukuhs"
destAddr = "cosmos16plylpsgxechajltx9yeseqexzdzut9g8vla4k"
port = "transfer"
channel = "channel-0"
)
denom := makeIBCDenom(testDestinationPort, testDestinationChannel, testDenom)
senderAccAddr := test.AccAddress()

amount256, ok := sdk.NewIntFromString(testAmount256)
require.True(t, ok)

testCoin := sdk.NewCoin(denom, amount256)
packetOrig := transferPacket256(t, hostAddr, &types.PacketMetadata{
Forward: &types.ForwardMetadata{
Receiver: destAddr,
Port: port,
Channel: channel,
},
})
packetFwd := transferPacket256(t, destAddr, nil)

acknowledgement := channeltypes.NewResultAcknowledgement([]byte("test"))
successAck := cdc.MustMarshalJSON(&acknowledgement)

// Expected mocks
gomock.InOrder(
setup.Mocks.IBCModuleMock.EXPECT().OnRecvPacket(ctx, packetOrig, senderAccAddr).
Return(acknowledgement),

setup.Mocks.TransferKeeperMock.EXPECT().Transfer(
sdk.WrapSDKContext(ctx),
transfertypes.NewMsgTransfer(
port,
channel,
testCoin,
hostAddr,
destAddr,
keeper.DefaultTransferPacketTimeoutHeight,
uint64(ctx.BlockTime().UnixNano())+uint64(keeper.DefaultForwardTransferPacketTimeoutTimestamp.Nanoseconds()),
"",
),
).Return(&transfertypes.MsgTransferResponse{Sequence: 0}, nil),

setup.Mocks.IBCModuleMock.EXPECT().OnAcknowledgementPacket(ctx, packetFwd, successAck, senderAccAddr).
Return(nil),
)

// chain B with router module receives packet and forwards. ack should be nil so that it is not written yet.
ack := forwardMiddleware.OnRecvPacket(ctx, packetOrig, senderAccAddr)
require.Nil(t, ack)

// ack returned from chain C
err = forwardMiddleware.OnAcknowledgementPacket(ctx, packetFwd, successAck, senderAccAddr)
require.NoError(t, err)
}

func TestOnRecvPacket_ForwardWithFee(t *testing.T) {
var err error
ctl := gomock.NewController(t)
Expand Down

0 comments on commit f6ef0a5

Please sign in to comment.