From 6e7a25b97ae954fd87f2ddf5431d6e0ccf8aafdf Mon Sep 17 00:00:00 2001 From: Bryce Neal Date: Fri, 3 Feb 2023 11:57:42 -0500 Subject: [PATCH] Add tests for Commiter change (#5) --- baseapp/abci_test.go | 49 ++++++++++++ baseapp/baseapp_test.go | 3 + testutil/mock/types_module_module.go | 113 +++++++++++++++++++++++++++ types/module/module_test.go | 27 ++++++- 4 files changed, 189 insertions(+), 3 deletions(-) diff --git a/baseapp/abci_test.go b/baseapp/abci_test.go index 61f8a7d551f..e3793506866 100644 --- a/baseapp/abci_test.go +++ b/baseapp/abci_test.go @@ -596,6 +596,33 @@ func TestABCI_EndBlock(t *testing.T) { require.Equal(t, cp.Block.MaxGas, res.ConsensusParamUpdates.Block.MaxGas) } +func TestBaseApp_Commit(t *testing.T) { + db := dbm.NewMemDB() + name := t.Name() + logger := defaultLogger() + + cp := &tmproto.ConsensusParams{ + Block: &tmproto.BlockParams{ + MaxGas: 5000000, + }, + } + + app := baseapp.NewBaseApp(name, logger, db, nil) + app.SetParamStore(¶mStore{db: dbm.NewMemDB()}) + app.InitChain(abci.RequestInitChain{ + ConsensusParams: cp, + }) + + wasCommiterCalled := false + app.SetCommiter(func(ctx sdk.Context) { + wasCommiterCalled = true + }) + app.Seal() + + app.Commit() + require.Equal(t, true, wasCommiterCalled) +} + func TestABCI_CheckTx(t *testing.T) { // This ante handler reads the key and checks that the value matches the // current counter. This ensures changes to the KVStore persist across @@ -1320,6 +1347,28 @@ func TestABCI_GetBlockRetentionHeight(t *testing.T) { } } +// Verifies that the Commiter is called with the checkState. +func TestCommiterCalledWithCheckState(t *testing.T) { + t.Parallel() + + logger := defaultLogger() + db := dbm.NewMemDB() + name := t.Name() + app := baseapp.NewBaseApp(name, logger, db, nil) + + wasCommiterCalled := false + app.SetCommiter(func(ctx sdk.Context) { + // Make sure context is for next block + require.Equal(t, true, ctx.IsCheckTx()) + wasCommiterCalled = true + }) + + app.BeginBlock(abci.RequestBeginBlock{Header: tmproto.Header{Height: 1}}) + app.Commit() + + require.Equal(t, true, wasCommiterCalled) +} + func TestABCI_Proposal_HappyPath(t *testing.T) { anteKey := []byte("ante-key") pool := mempool.NewSenderNonceMempool() diff --git a/baseapp/baseapp_test.go b/baseapp/baseapp_test.go index a821ca1e445..7cba6f1753d 100644 --- a/baseapp/baseapp_test.go +++ b/baseapp/baseapp_test.go @@ -390,6 +390,9 @@ func TestBaseAppOptionSeal(t *testing.T) { require.Panics(t, func() { suite.baseApp.SetEndBlocker(nil) }) + require.Panics(t, func() { + suite.baseApp.SetCommiter(nil) + }) require.Panics(t, func() { suite.baseApp.SetAnteHandler(nil) }) diff --git a/testutil/mock/types_module_module.go b/testutil/mock/types_module_module.go index 8b3a79c80fa..ca77ba57f18 100644 --- a/testutil/mock/types_module_module.go +++ b/testutil/mock/types_module_module.go @@ -879,3 +879,116 @@ func (mr *MockEndBlockAppModuleMockRecorder) RegisterLegacyAminoCodec(arg0 inter mr.mock.ctrl.T.Helper() return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "RegisterLegacyAminoCodec", reflect.TypeOf((*MockEndBlockAppModule)(nil).RegisterLegacyAminoCodec), arg0) } + +// MockCommitAppModule is a mock of CommitAppModule interface. +type MockCommitAppModule struct { + ctrl *gomock.Controller + recorder *MockCommitAppModuleMockRecorder +} + +// MockCommitAppModuleMockRecorder is the mock recorder for MockCommitAppModule. +type MockCommitAppModuleMockRecorder struct { + mock *MockCommitAppModule +} + +// NewMockCommitAppModule creates a new mock instance. +func NewMockCommitAppModule(ctrl *gomock.Controller) *MockCommitAppModule { + mock := &MockCommitAppModule{ctrl: ctrl} + mock.recorder = &MockCommitAppModuleMockRecorder{mock} + return mock +} + +// EXPECT returns an object that allows the caller to indicate expected use. +func (m *MockCommitAppModule) EXPECT() *MockCommitAppModuleMockRecorder { + return m.recorder +} + +// Commit mocks base method. +func (m *MockCommitAppModule) Commit(arg0 types1.Context) { + m.ctrl.T.Helper() + m.ctrl.Call(m, "Commit", arg0) +} + +// Commit indicates an expected call of Commit. +func (mr *MockCommitAppModuleMockRecorder) Commit(arg0 interface{}) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Commit", reflect.TypeOf((*MockCommitAppModule)(nil).Commit), arg0) +} + +// GetQueryCmd mocks base method. +func (m *MockCommitAppModule) GetQueryCmd() *cobra.Command { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "GetQueryCmd") + ret0, _ := ret[0].(*cobra.Command) + return ret0 +} + +// GetQueryCmd indicates an expected call of GetQueryCmd. +func (mr *MockCommitAppModuleMockRecorder) GetQueryCmd() *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "GetQueryCmd", reflect.TypeOf((*MockCommitAppModule)(nil).GetQueryCmd)) +} + +// GetTxCmd mocks base method. +func (m *MockCommitAppModule) GetTxCmd() *cobra.Command { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "GetTxCmd") + ret0, _ := ret[0].(*cobra.Command) + return ret0 +} + +// GetTxCmd indicates an expected call of GetTxCmd. +func (mr *MockCommitAppModuleMockRecorder) GetTxCmd() *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "GetTxCmd", reflect.TypeOf((*MockCommitAppModule)(nil).GetTxCmd)) +} + +// Name mocks base method. +func (m *MockCommitAppModule) Name() string { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "Name") + ret0, _ := ret[0].(string) + return ret0 +} + +// Name indicates an expected call of Name. +func (mr *MockCommitAppModuleMockRecorder) Name() *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Name", reflect.TypeOf((*MockCommitAppModule)(nil).Name)) +} + +// RegisterGRPCGatewayRoutes mocks base method. +func (m *MockCommitAppModule) RegisterGRPCGatewayRoutes(arg0 client.Context, arg1 *runtime.ServeMux) { + m.ctrl.T.Helper() + m.ctrl.Call(m, "RegisterGRPCGatewayRoutes", arg0, arg1) +} + +// RegisterGRPCGatewayRoutes indicates an expected call of RegisterGRPCGatewayRoutes. +func (mr *MockCommitAppModuleMockRecorder) RegisterGRPCGatewayRoutes(arg0, arg1 interface{}) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "RegisterGRPCGatewayRoutes", reflect.TypeOf((*MockCommitAppModule)(nil).RegisterGRPCGatewayRoutes), arg0, arg1) +} + +// RegisterInterfaces mocks base method. +func (m *MockCommitAppModule) RegisterInterfaces(arg0 types0.InterfaceRegistry) { + m.ctrl.T.Helper() + m.ctrl.Call(m, "RegisterInterfaces", arg0) +} + +// RegisterInterfaces indicates an expected call of RegisterInterfaces. +func (mr *MockCommitAppModuleMockRecorder) RegisterInterfaces(arg0 interface{}) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "RegisterInterfaces", reflect.TypeOf((*MockCommitAppModule)(nil).RegisterInterfaces), arg0) +} + +// RegisterLegacyAminoCodec mocks base method. +func (m *MockCommitAppModule) RegisterLegacyAminoCodec(arg0 *codec.LegacyAmino) { + m.ctrl.T.Helper() + m.ctrl.Call(m, "RegisterLegacyAminoCodec", arg0) +} + +// RegisterLegacyAminoCodec indicates an expected call of RegisterLegacyAminoCodec. +func (mr *MockCommitAppModuleMockRecorder) RegisterLegacyAminoCodec(arg0 interface{}) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "RegisterLegacyAminoCodec", reflect.TypeOf((*MockCommitAppModule)(nil).RegisterLegacyAminoCodec), arg0) +} diff --git a/types/module/module_test.go b/types/module/module_test.go index 56bfbe7dd79..52c458c73f1 100644 --- a/types/module/module_test.go +++ b/types/module/module_test.go @@ -98,9 +98,13 @@ func TestManagerOrderSetters(t *testing.T) { mm.SetOrderBeginBlockers("module2", "module1") require.Equal(t, []string{"module2", "module1"}, mm.OrderBeginBlockers) - require.Equal(t, []string{"module1", "module2"}, mm.OrderEndBlockers) - mm.SetOrderEndBlockers("module2", "module1") - require.Equal(t, []string{"module2", "module1"}, mm.OrderEndBlockers) + require.Equal(t, []string{"module1", "module2", "module3"}, mm.OrderEndBlockers) + mm.SetOrderEndBlockers("module2", "module1", "module3") + require.Equal(t, []string{"module2", "module1", "module3"}, mm.OrderEndBlockers) + + require.Equal(t, []string{"module1", "module2", "module3"}, mm.OrderCommiters) + mm.SetOrderCommiters("module2", "module1", "module3") + require.Equal(t, []string{"module2", "module1", "module3"}, mm.OrderCommiters) } func TestManager_RegisterInvariants(t *testing.T) { @@ -251,3 +255,20 @@ func TestManager_EndBlock(t *testing.T) { mockAppModule2.EXPECT().EndBlock(gomock.Any(), gomock.Eq(req)).Times(1).Return([]abci.ValidatorUpdate{{}}) require.Panics(t, func() { mm.EndBlock(sdk.Context{}, req) }) } + +func TestManager_Commit(t *testing.T) { + mockCtrl := gomock.NewController(t) + t.Cleanup(mockCtrl.Finish) + + mockAppModule1 := mock.NewMockCommitAppModule(mockCtrl) + mockAppModule2 := mock.NewMockCommitAppModule(mockCtrl) + mockAppModule1.EXPECT().Name().Times(2).Return("module1") + mockAppModule2.EXPECT().Name().Times(2).Return("module2") + mm := module.NewManager(mockAppModule1, mockAppModule2) + require.NotNil(t, mm) + require.Equal(t, 2, len(mm.Modules)) + + mockAppModule1.EXPECT().Commit(gomock.Any()).Times(1) + mockAppModule2.EXPECT().Commit(gomock.Any()).Times(1) + mm.Commit(sdk.Context{}) +}