Skip to content

Commit

Permalink
Merge branch 'main' into dependabot/go_modules/cosmossdk.io/math-1.1.1
Browse files Browse the repository at this point in the history
  • Loading branch information
damiannolan committed Aug 21, 2023
2 parents 4ccb915 + 48a7baf commit 83bfc43
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -261,10 +261,10 @@ func (im IBCMiddleware) GetAppVersion(ctx sdk.Context, portID, channelID string)
// into an InterchainAccountPacketData. This function implements the optional
// PacketDataUnmarshaler interface required for ADR 008 support.
func (IBCMiddleware) UnmarshalPacketData(bz []byte) (interface{}, error) {
var packetData icatypes.InterchainAccountPacketData
if err := icatypes.ModuleCdc.UnmarshalJSON(bz, &packetData); err != nil {
var data icatypes.InterchainAccountPacketData
err := data.UnmarshalJSON(bz)
if err != nil {
return nil, err
}

return packetData, nil
return data, nil
}
4 changes: 2 additions & 2 deletions modules/apps/27-interchain-accounts/host/keeper/relay.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ import (
// If the transaction is successfully executed, the transaction response bytes will be returned.
func (k Keeper) OnRecvPacket(ctx sdk.Context, packet channeltypes.Packet) ([]byte, error) {
var data icatypes.InterchainAccountPacketData

if err := icatypes.ModuleCdc.UnmarshalJSON(packet.GetData(), &data); err != nil {
err := data.UnmarshalJSON(packet.GetData())
if err != nil {
// UnmarshalJSON errors are indeterminate and therefore are not wrapped and included in failed acks
return nil, errorsmod.Wrapf(icatypes.ErrUnknownDataType, "cannot unmarshal ICS-27 interchain account packet data")
}
Expand Down
5 changes: 5 additions & 0 deletions modules/apps/27-interchain-accounts/types/packet.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,11 @@ func (iapd InterchainAccountPacketData) GetBytes() []byte {
return sdk.MustSortJSON(ModuleCdc.MustMarshalJSON(&iapd))
}

// UnmarshalJSON unmarshals raw JSON bytes into an InterchainAccountPacketData.
func (iapd *InterchainAccountPacketData) UnmarshalJSON(bz []byte) error {
return ModuleCdc.UnmarshalJSON(bz, iapd)
}

// GetBytes returns the JSON marshalled interchain account CosmosTx.
func (ct CosmosTx) GetBytes() []byte {
return sdk.MustSortJSON(ModuleCdc.MustMarshalJSON(&ct))
Expand Down
21 changes: 21 additions & 0 deletions modules/apps/27-interchain-accounts/types/packet_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -180,3 +180,24 @@ func (suite *TypesTestSuite) TestPacketDataProvider() {
suite.Require().Equal(tc.expCustomData, customData)
}
}

func (suite *TypesTestSuite) TestPacketDataUnmarshalerInterface() {
expPacketData := types.InterchainAccountPacketData{
Type: types.EXECUTE_TX,
Data: []byte("data"),
Memo: "some memo",
}

var packetData types.InterchainAccountPacketData
err := packetData.UnmarshalJSON(expPacketData.GetBytes())
suite.Require().NoError(err)
suite.Require().Equal(expPacketData, packetData)

// test invalid packet data
invalidPacketDataBytes := []byte("invalid packet data")

var invalidPacketData types.InterchainAccountPacketData
err = packetData.UnmarshalJSON(invalidPacketDataBytes)
suite.Require().Error(err)
suite.Require().Equal(types.InterchainAccountPacketData{}, invalidPacketData)
}
20 changes: 16 additions & 4 deletions modules/apps/callbacks/ibc_middleware_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -741,6 +741,7 @@ func (s *CallbacksTestSuite) TestProcessCallback() {
callbackData types.CallbackData
ctx sdk.Context
callbackExecutor func(sdk.Context) error
expGasConsumed uint64
)

callbackError := fmt.Errorf("callbackExecutor error")
Expand All @@ -761,6 +762,7 @@ func (s *CallbacksTestSuite) TestProcessCallback() {
"success: callbackExecutor panic, but not out of gas",
func() {
callbackExecutor = func(cachedCtx sdk.Context) error {
cachedCtx.GasMeter().ConsumeGas(expGasConsumed, "callbackExecutor gas consumption")
panic("callbackExecutor panic")
}
},
Expand All @@ -771,8 +773,9 @@ func (s *CallbacksTestSuite) TestProcessCallback() {
"success: callbackExecutor oog panic, but retry is not allowed",
func() {
executionGas := callbackData.ExecutionGasLimit
expGasConsumed = executionGas
callbackExecutor = func(cachedCtx sdk.Context) error {
cachedCtx.GasMeter().ConsumeGas(executionGas+1, "callbackExecutor oog panic")
cachedCtx.GasMeter().ConsumeGas(expGasConsumed+1, "callbackExecutor gas consumption")
return nil
}
},
Expand All @@ -783,6 +786,7 @@ func (s *CallbacksTestSuite) TestProcessCallback() {
"failure: callbackExecutor error",
func() {
callbackExecutor = func(cachedCtx sdk.Context) error {
cachedCtx.GasMeter().ConsumeGas(expGasConsumed, "callbackExecutor gas consumption")
return callbackError
}
},
Expand All @@ -794,6 +798,7 @@ func (s *CallbacksTestSuite) TestProcessCallback() {
func() {
callbackType = types.CallbackTypeSendPacket
callbackExecutor = func(cachedCtx sdk.Context) error {
cachedCtx.GasMeter().ConsumeGas(expGasConsumed, "callbackExecutor gas consumption")
panic("callbackExecutor panic")
}
},
Expand All @@ -805,6 +810,7 @@ func (s *CallbacksTestSuite) TestProcessCallback() {
func() {
executionGas := callbackData.ExecutionGasLimit
callbackData.CommitGasLimit = executionGas + 1
expGasConsumed = executionGas
callbackExecutor = func(cachedCtx sdk.Context) error {
cachedCtx.GasMeter().ConsumeGas(executionGas+1, "callbackExecutor oog panic")
return nil
Expand All @@ -823,18 +829,22 @@ func (s *CallbacksTestSuite) TestProcessCallback() {
// set a callback data that does not allow retry
callbackData = types.CallbackData{
CallbackAddress: s.chainB.SenderAccount.GetAddress().String(),
ExecutionGasLimit: 1000000,
ExecutionGasLimit: 1_000_000,
SenderAddress: s.chainB.SenderAccount.GetAddress().String(),
CommitGasLimit: 600000,
CommitGasLimit: 600_000,
}

// this only makes a difference if it is SendPacket
callbackType = types.CallbackTypeReceivePacket

// expGasConsumed can be overwritten in malleate
expGasConsumed = 300_000

ctx = s.chainB.GetContext()

// set a callback executor that will always succeed
// set a callback executor that will always succeed after consuming expGasConsumed
callbackExecutor = func(cachedCtx sdk.Context) error {
cachedCtx.GasMeter().ConsumeGas(expGasConsumed, "callbackExecutor gas consumption")
return nil
}

Expand Down Expand Up @@ -862,6 +872,8 @@ func (s *CallbacksTestSuite) TestProcessCallback() {
processCallback()
s.Require().ErrorIs(tc.expValue.(error), err)
}

s.Require().Equal(expGasConsumed, ctx.GasMeter().GasConsumed())
})
}
}
Expand Down

0 comments on commit 83bfc43

Please sign in to comment.