From e91179f76bcd8fddc5b5a4029ef7591328eeedd0 Mon Sep 17 00:00:00 2001 From: srdtrk Date: Thu, 17 Aug 2023 19:23:46 +0300 Subject: [PATCH 1/2] imp(callbacks_test): checking that processCallback consumes gas from callback execution --- modules/apps/callbacks/ibc_middleware_test.go | 23 +++++++++++++++---- 1 file changed, 18 insertions(+), 5 deletions(-) diff --git a/modules/apps/callbacks/ibc_middleware_test.go b/modules/apps/callbacks/ibc_middleware_test.go index 83383eee239..e3d8d5245ca 100644 --- a/modules/apps/callbacks/ibc_middleware_test.go +++ b/modules/apps/callbacks/ibc_middleware_test.go @@ -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") @@ -753,7 +754,9 @@ func (s *CallbacksTestSuite) TestProcessCallback() { }{ { "success", - func() {}, + func() { + expGasConsumed = 0 + }, false, nil, }, @@ -761,6 +764,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") } }, @@ -771,8 +775,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 } }, @@ -783,6 +788,7 @@ func (s *CallbacksTestSuite) TestProcessCallback() { "failure: callbackExecutor error", func() { callbackExecutor = func(cachedCtx sdk.Context) error { + cachedCtx.GasMeter().ConsumeGas(expGasConsumed, "callbackExecutor gas consumption") return callbackError } }, @@ -794,6 +800,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") } }, @@ -805,6 +812,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 @@ -823,17 +831,20 @@ 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 without consuming gas callbackExecutor = func(cachedCtx sdk.Context) error { return nil } @@ -862,6 +873,8 @@ func (s *CallbacksTestSuite) TestProcessCallback() { processCallback() s.Require().ErrorIs(tc.expValue.(error), err) } + + s.Require().Equal(expGasConsumed, ctx.GasMeter().GasConsumed()) }) } } From ec1ad8c0dc91a203030076dfb6875ac771d4b2ac Mon Sep 17 00:00:00 2001 From: srdtrk Date: Thu, 17 Aug 2023 19:29:00 +0300 Subject: [PATCH 2/2] imp(callbacks_test): simplified success test case a bit --- modules/apps/callbacks/ibc_middleware_test.go | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/modules/apps/callbacks/ibc_middleware_test.go b/modules/apps/callbacks/ibc_middleware_test.go index e3d8d5245ca..c995450ce50 100644 --- a/modules/apps/callbacks/ibc_middleware_test.go +++ b/modules/apps/callbacks/ibc_middleware_test.go @@ -754,9 +754,7 @@ func (s *CallbacksTestSuite) TestProcessCallback() { }{ { "success", - func() { - expGasConsumed = 0 - }, + func() {}, false, nil, }, @@ -844,8 +842,9 @@ func (s *CallbacksTestSuite) TestProcessCallback() { ctx = s.chainB.GetContext() - // set a callback executor that will always succeed without consuming gas + // 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 }