From b53627ab502ba173b601480dc2b2ffff48dfcd51 Mon Sep 17 00:00:00 2001 From: Quentin McGaw Date: Tue, 26 Oct 2021 11:03:50 -0400 Subject: [PATCH] chore(mocks): use `//go:generate` to re-generate mocks (#1917) - Fixes #1915 - Unblocks #1785 - Remove unused generated mocks - Mocks in `mocks` directory are not prefixed with `Mock` - Update .github/contributing.md on how to generate mocks - Remove Makefile `mock` - Unexport mocks that did not need exportation - Export mocks that should be exported (exported mock constructors) - Ignore mock files in codecov - Move package local mocks to `./mocks` sub-directory when possible (only for `MockTransactionState`) - Re-generate all mocks with mockery `v2.9.4` --- .github/CONTRIBUTING.md | 19 ++- Makefile | 19 --- codecov.yml | 2 + dot/core/interface.go | 8 + dot/core/messages_test.go | 8 +- dot/core/mocks/Network.go | 8 +- dot/core/mocks/block_producer.go | 34 ---- dot/core/mocks/block_state.go | 51 +++--- dot/core/mocks/digest_handler.go | 8 +- dot/core/mocks/storage_state.go | 24 +-- dot/core/service_test.go | 18 +- dot/core/test_helpers.go | 6 +- dot/network/mock_block_state.go | 18 +- dot/network/mock_syncer.go | 54 +----- dot/network/mock_transaction_handler.go | 2 +- dot/network/notifications.go | 11 +- dot/network/notifications_test.go | 2 +- .../mock_is_block_request__from_block.go | 15 -- dot/network/service.go | 2 +- dot/network/state.go | 6 + dot/network/test_helpers.go | 4 +- dot/network/transaction.go | 10 +- dot/rpc/http_test.go | 8 +- dot/rpc/modules/api.go | 18 ++ dot/rpc/modules/api_mocks.go | 12 +- dot/rpc/modules/author_test.go | 20 +-- dot/rpc/modules/grandpa_test.go | 2 +- dot/rpc/modules/mocks/block_api.go | 38 ++--- dot/rpc/modules/mocks/block_finality_api.go | 26 +-- dot/rpc/modules/mocks/core_api.go | 22 +-- dot/rpc/modules/mocks/network_api.go | 28 ++-- dot/rpc/modules/mocks/rpcapi.go | 10 +- dot/rpc/modules/mocks/runtime_storage_api.go | 26 +-- dot/rpc/modules/mocks/storage_api.go | 24 +-- dot/rpc/modules/mocks/system_api.go | 16 +- ...onStateAPI.go => transaction_state_api.go} | 2 +- dot/rpc/modules/offchain_test.go | 4 +- dot/rpc/modules/payment_test.go | 14 +- dot/rpc/modules/state_test.go | 8 +- dot/rpc/modules/system_test.go | 18 +- dot/rpc/service_test.go | 22 +-- dot/rpc/subscription/listeners_test.go | 14 +- dot/rpc/subscription/websocket_test.go | 4 +- dot/state/mock_observer.go | 2 +- dot/state/storage_notify.go | 2 + dot/sync/bootstrap_syncer_test.go | 2 +- dot/sync/chain_sync_test.go | 46 ++--- dot/sync/interface.go | 10 ++ dot/sync/mocks/Network.go | 12 +- .../mocks/{verifier.go => babe_verifier.go} | 10 +- dot/sync/mocks/block_import_handler.go | 10 +- .../mocks/{BlockState.go => block_state.go} | 54 +++--- .../{FinalityGadget.go => finality_gadget.go} | 4 +- dot/sync/syncer_test.go | 20 +-- dot/sync/tip_syncer_test.go | 4 +- ...portHandler.go => block_import_handler.go} | 2 +- lib/babe/state.go | 2 + lib/grandpa/grandpa_test.go | 6 +- lib/grandpa/mocks/digest_handler.go | 10 +- lib/grandpa/mocks/network.go | 41 +++-- lib/grandpa/state.go | 4 + lib/keystore/mock_keyring.go | 157 ------------------ lib/keystore/mock_keystore.go | 126 -------------- lib/runtime/allocator_test.go | 16 +- lib/runtime/interface.go | 4 + lib/runtime/memory.go | 2 + .../{mock_memory.go => mock_memory_test.go} | 12 +- lib/runtime/mocks/instance.go | 58 +++---- .../transaction_state.go} | 10 +- lib/runtime/mocks/version.go | 2 +- lib/runtime/version.go | 2 + lib/runtime/wasmer/test_helpers.go | 7 +- lib/scale/decode_ptr_test.go | 20 +-- lib/scale/encode_test.go | 22 +-- lib/services/mocks/service.go | 12 +- lib/services/services.go | 2 + lib/services/services_test.go | 10 +- 77 files changed, 524 insertions(+), 844 deletions(-) delete mode 100644 dot/core/mocks/block_producer.go delete mode 100644 dot/network/proto/mock_is_block_request__from_block.go rename dot/rpc/modules/mocks/{TransactionStateAPI.go => transaction_state_api.go} (97%) rename dot/sync/mocks/{verifier.go => babe_verifier.go} (60%) rename dot/sync/mocks/{BlockState.go => block_state.go} (81%) rename dot/sync/mocks/{FinalityGadget.go => finality_gadget.go} (89%) rename lib/babe/mocks/{BlockImportHandler.go => block_import_handler.go} (93%) delete mode 100644 lib/keystore/mock_keyring.go delete mode 100644 lib/keystore/mock_keystore.go rename lib/runtime/{mock_memory.go => mock_memory_test.go} (73%) rename lib/runtime/{mock_transaction_state.go => mocks/transaction_state.go} (63%) diff --git a/.github/CONTRIBUTING.md b/.github/CONTRIBUTING.md index f494b6ff50..f35665d8e3 100644 --- a/.github/CONTRIBUTING.md +++ b/.github/CONTRIBUTING.md @@ -62,19 +62,22 @@ One important distinction is that we are building the Polkadot Runtime Environme go test ``` - Sometimes you may need to create mocks for interfaces, in that case just execute: + Sometimes you may need to create mocks for interfaces, in that case, add a go generate comment. For example, for interface `MyInterface`, the comment would be: - ```sh - make mock path=$(path to the interface) interface=$(interface name) + ```go + //go:generate mockery --name MyInterface --structname MyInterface --case underscore --keeptree ``` - The command above will generate a file with prefix `mock_` inside the interface folder, if you want to generate the same mock but inside the `mocks` folder, you can execute: + This will generate a Go file `./mocks/my_interface.go` with the `MyInterface` Mockery mock. - ```sh - make mock path=$(path to the interface) interface=$(interface name) INMOCKS=1 - ``` + If you want to have your mock in the same package you are working on: + - Replace `--keeptree` with `-inpackage` + - Add `--filename mock_my_interface_test.go` so it's clear it's only used for package local tests + - Prefix the `--structname` value with `mock` to differentiate it with the original interface. + + This will generate a Go file `./mock_my_interface_test.go` with mock `mockMyInterface`. - The command above will generate the mock inside the folder `$(path)/mocks`, and the mocks will be in the same package that your interface is. + Generate the mock code with `go generate -run "mockery" ./...` from your working directory. This will also update existing mocks. You can update all mocks by running `go generate -run "mockery" ./...` from the repository root. 8. **Lint your changes.** diff --git a/Makefile b/Makefile index 8bd963bb92..8cfa4bee33 100644 --- a/Makefile +++ b/Makefile @@ -126,22 +126,3 @@ gossamer: clean ## install: install the gossamer binary in $GOPATH/bin install: GOBIN=$(GOPATH)/bin go run scripts/ci.go install - -MOCKGEN := $(shell command -v $(GOPATH)/bin/mockery 2> /dev/null) -INMOCKS=0 -mock: -ifndef MOCKGEN - @echo "> Installing mockery ..." - @go get github.com/vektra/mockery/v2/.../ -endif - @echo "> Generating mocks at $(path)" - -ifeq ($(INMOCKS),1) - cd $(path); $(GOPATH)/bin/mockery --name $(interface) --structname Mock$(interface) --case underscore --keeptree -else - $(GOPATH)/bin/mockery --srcpkg $(path) --name $(interface) --case underscore --inpackage -endif - - - - diff --git a/codecov.yml b/codecov.yml index b276613c66..a37451ce0d 100644 --- a/codecov.yml +++ b/codecov.yml @@ -14,5 +14,7 @@ ignore: - "lib/runtime/test_data" - "**/errors.go" - "**/*/errors.go" + - "**/*/mocks/*.go" + - "**/*/mock_*.go" - "go.mod" - "go.sum" diff --git a/dot/core/interface.go b/dot/core/interface.go index f410eb5450..6336b50820 100644 --- a/dot/core/interface.go +++ b/dot/core/interface.go @@ -28,6 +28,8 @@ import ( "github.com/ChainSafe/gossamer/lib/transaction" ) +//go:generate mockery --name BlockState --structname BlockState --case underscore --keeptree + // BlockState interface for block state methods type BlockState interface { BestBlockHash() common.Hash @@ -55,6 +57,8 @@ type BlockState interface { StoreRuntime(common.Hash, runtime.Instance) } +//go:generate mockery --name StorageState --structname StorageState --case underscore --keeptree + // StorageState interface for storage state methods type StorageState interface { LoadCode(root *common.Hash) ([]byte, error) @@ -76,6 +80,8 @@ type TransactionState interface { PendingInPool() []*transaction.ValidTransaction } +//go:generate mockery --name Network --structname Network --case underscore --keeptree + // Network is the interface for the network service type Network interface { GossipMessage(network.NotificationsMessage) @@ -95,6 +101,8 @@ type CodeSubstitutedState interface { StoreCodeSubstitutedBlockHash(hash common.Hash) error } +//go:generate mockery --name DigestHandler --structname DigestHandler --case underscore --keeptree + // DigestHandler is the interface for the consensus digest handler type DigestHandler interface { HandleDigests(header *types.Header) diff --git a/dot/core/messages_test.go b/dot/core/messages_test.go index 53354bb989..ef94e5accc 100644 --- a/dot/core/messages_test.go +++ b/dot/core/messages_test.go @@ -21,7 +21,7 @@ import ( "testing" "time" - . "github.com/ChainSafe/gossamer/dot/core/mocks" // nolint + "github.com/ChainSafe/gossamer/dot/core/mocks" "github.com/ChainSafe/gossamer/dot/network" "github.com/ChainSafe/gossamer/dot/state" "github.com/ChainSafe/gossamer/dot/sync" @@ -80,7 +80,7 @@ func createExtrinsic(t *testing.T, rt runtime.Instance, genHash common.Hash, non } func TestService_HandleBlockProduced(t *testing.T) { - net := new(MockNetwork) + net := new(mocks.Network) cfg := &Config{ Network: net, Keystore: keystore.NewGlobalKeystore(), @@ -132,10 +132,6 @@ func TestService_HandleTransactionMessage(t *testing.T) { ks := keystore.NewGlobalKeystore() ks.Acco.Insert(kp) - bp := new(MockBlockProducer) // nolint - blockC := make(chan types.Block) - bp.On("GetBlockChannel", nil).Return(blockC) - cfg := &Config{ Keystore: ks, TransactionState: state.NewTransactionState(), diff --git a/dot/core/mocks/Network.go b/dot/core/mocks/Network.go index 9bc4564527..f7df49c1f6 100644 --- a/dot/core/mocks/Network.go +++ b/dot/core/mocks/Network.go @@ -7,18 +7,18 @@ import ( mock "github.com/stretchr/testify/mock" ) -// MockNetwork is an autogenerated mock type for the Network type -type MockNetwork struct { +// Network is an autogenerated mock type for the Network type +type Network struct { mock.Mock } // GossipMessage provides a mock function with given fields: _a0 -func (_m *MockNetwork) GossipMessage(_a0 network.NotificationsMessage) { +func (_m *Network) GossipMessage(_a0 network.NotificationsMessage) { _m.Called(_a0) } // IsSynced provides a mock function with given fields: -func (_m *MockNetwork) IsSynced() bool { +func (_m *Network) IsSynced() bool { ret := _m.Called() var r0 bool diff --git a/dot/core/mocks/block_producer.go b/dot/core/mocks/block_producer.go deleted file mode 100644 index 16c549ab1a..0000000000 --- a/dot/core/mocks/block_producer.go +++ /dev/null @@ -1,34 +0,0 @@ -// Code generated by mockery v2.8.0. DO NOT EDIT. - -package mocks - -import ( - types "github.com/ChainSafe/gossamer/dot/types" - mock "github.com/stretchr/testify/mock" -) - -// MockBlockProducer is an autogenerated mock type for the BlockProducer type -type MockBlockProducer struct { - mock.Mock -} - -// GetBlockChannel provides a mock function with given fields: -func (_m *MockBlockProducer) GetBlockChannel() <-chan types.Block { - ret := _m.Called() - - var r0 <-chan types.Block - if rf, ok := ret.Get(0).(func() <-chan types.Block); ok { - r0 = rf() - } else { - if ret.Get(0) != nil { - r0 = ret.Get(0).(<-chan types.Block) - } - } - - return r0 -} - -// SetOnDisabled provides a mock function with given fields: authorityIndex -func (_m *MockBlockProducer) SetOnDisabled(authorityIndex uint32) { - _m.Called(authorityIndex) -} diff --git a/dot/core/mocks/block_state.go b/dot/core/mocks/block_state.go index 933e7f4e54..43f779983c 100644 --- a/dot/core/mocks/block_state.go +++ b/dot/core/mocks/block_state.go @@ -6,6 +6,7 @@ import ( big "math/big" common "github.com/ChainSafe/gossamer/lib/common" + mock "github.com/stretchr/testify/mock" runtime "github.com/ChainSafe/gossamer/lib/runtime" @@ -15,13 +16,13 @@ import ( types "github.com/ChainSafe/gossamer/dot/types" ) -// MockBlockState is an autogenerated mock type for the BlockState type -type MockBlockState struct { +// BlockState is an autogenerated mock type for the BlockState type +type BlockState struct { mock.Mock } // AddBlock provides a mock function with given fields: _a0 -func (_m *MockBlockState) AddBlock(_a0 *types.Block) error { +func (_m *BlockState) AddBlock(_a0 *types.Block) error { ret := _m.Called(_a0) var r0 error @@ -35,7 +36,7 @@ func (_m *MockBlockState) AddBlock(_a0 *types.Block) error { } // BestBlock provides a mock function with given fields: -func (_m *MockBlockState) BestBlock() (*types.Block, error) { +func (_m *BlockState) BestBlock() (*types.Block, error) { ret := _m.Called() var r0 *types.Block @@ -58,7 +59,7 @@ func (_m *MockBlockState) BestBlock() (*types.Block, error) { } // BestBlockHash provides a mock function with given fields: -func (_m *MockBlockState) BestBlockHash() common.Hash { +func (_m *BlockState) BestBlockHash() common.Hash { ret := _m.Called() var r0 common.Hash @@ -74,7 +75,7 @@ func (_m *MockBlockState) BestBlockHash() common.Hash { } // BestBlockHeader provides a mock function with given fields: -func (_m *MockBlockState) BestBlockHeader() (*types.Header, error) { +func (_m *BlockState) BestBlockHeader() (*types.Header, error) { ret := _m.Called() var r0 *types.Header @@ -97,7 +98,7 @@ func (_m *MockBlockState) BestBlockHeader() (*types.Header, error) { } // BestBlockNumber provides a mock function with given fields: -func (_m *MockBlockState) BestBlockNumber() (*big.Int, error) { +func (_m *BlockState) BestBlockNumber() (*big.Int, error) { ret := _m.Called() var r0 *big.Int @@ -120,7 +121,7 @@ func (_m *MockBlockState) BestBlockNumber() (*big.Int, error) { } // BestBlockStateRoot provides a mock function with given fields: -func (_m *MockBlockState) BestBlockStateRoot() (common.Hash, error) { +func (_m *BlockState) BestBlockStateRoot() (common.Hash, error) { ret := _m.Called() var r0 common.Hash @@ -143,17 +144,17 @@ func (_m *MockBlockState) BestBlockStateRoot() (common.Hash, error) { } // FreeFinalisedNotifierChannel provides a mock function with given fields: ch -func (_m *MockBlockState) FreeFinalisedNotifierChannel(ch chan *types.FinalisationInfo) { +func (_m *BlockState) FreeFinalisedNotifierChannel(ch chan *types.FinalisationInfo) { _m.Called(ch) } // FreeImportedBlockNotifierChannel provides a mock function with given fields: ch -func (_m *MockBlockState) FreeImportedBlockNotifierChannel(ch chan *types.Block) { +func (_m *BlockState) FreeImportedBlockNotifierChannel(ch chan *types.Block) { _m.Called(ch) } // GenesisHash provides a mock function with given fields: -func (_m *MockBlockState) GenesisHash() common.Hash { +func (_m *BlockState) GenesisHash() common.Hash { ret := _m.Called() var r0 common.Hash @@ -169,7 +170,7 @@ func (_m *MockBlockState) GenesisHash() common.Hash { } // GetAllBlocksAtDepth provides a mock function with given fields: hash -func (_m *MockBlockState) GetAllBlocksAtDepth(hash common.Hash) []common.Hash { +func (_m *BlockState) GetAllBlocksAtDepth(hash common.Hash) []common.Hash { ret := _m.Called(hash) var r0 []common.Hash @@ -185,7 +186,7 @@ func (_m *MockBlockState) GetAllBlocksAtDepth(hash common.Hash) []common.Hash { } // GetBlockBody provides a mock function with given fields: hash -func (_m *MockBlockState) GetBlockBody(hash common.Hash) (*types.Body, error) { +func (_m *BlockState) GetBlockBody(hash common.Hash) (*types.Body, error) { ret := _m.Called(hash) var r0 *types.Body @@ -208,7 +209,7 @@ func (_m *MockBlockState) GetBlockBody(hash common.Hash) (*types.Body, error) { } // GetBlockByHash provides a mock function with given fields: _a0 -func (_m *MockBlockState) GetBlockByHash(_a0 common.Hash) (*types.Block, error) { +func (_m *BlockState) GetBlockByHash(_a0 common.Hash) (*types.Block, error) { ret := _m.Called(_a0) var r0 *types.Block @@ -231,7 +232,7 @@ func (_m *MockBlockState) GetBlockByHash(_a0 common.Hash) (*types.Block, error) } // GetBlockStateRoot provides a mock function with given fields: bhash -func (_m *MockBlockState) GetBlockStateRoot(bhash common.Hash) (common.Hash, error) { +func (_m *BlockState) GetBlockStateRoot(bhash common.Hash) (common.Hash, error) { ret := _m.Called(bhash) var r0 common.Hash @@ -254,7 +255,7 @@ func (_m *MockBlockState) GetBlockStateRoot(bhash common.Hash) (common.Hash, err } // GetFinalisedHash provides a mock function with given fields: _a0, _a1 -func (_m *MockBlockState) GetFinalisedHash(_a0 uint64, _a1 uint64) (common.Hash, error) { +func (_m *BlockState) GetFinalisedHash(_a0 uint64, _a1 uint64) (common.Hash, error) { ret := _m.Called(_a0, _a1) var r0 common.Hash @@ -277,7 +278,7 @@ func (_m *MockBlockState) GetFinalisedHash(_a0 uint64, _a1 uint64) (common.Hash, } // GetFinalisedHeader provides a mock function with given fields: _a0, _a1 -func (_m *MockBlockState) GetFinalisedHeader(_a0 uint64, _a1 uint64) (*types.Header, error) { +func (_m *BlockState) GetFinalisedHeader(_a0 uint64, _a1 uint64) (*types.Header, error) { ret := _m.Called(_a0, _a1) var r0 *types.Header @@ -300,7 +301,7 @@ func (_m *MockBlockState) GetFinalisedHeader(_a0 uint64, _a1 uint64) (*types.Hea } // GetFinalisedNotifierChannel provides a mock function with given fields: -func (_m *MockBlockState) GetFinalisedNotifierChannel() chan *types.FinalisationInfo { +func (_m *BlockState) GetFinalisedNotifierChannel() chan *types.FinalisationInfo { ret := _m.Called() var r0 chan *types.FinalisationInfo @@ -316,7 +317,7 @@ func (_m *MockBlockState) GetFinalisedNotifierChannel() chan *types.Finalisation } // GetImportedBlockNotifierChannel provides a mock function with given fields: -func (_m *MockBlockState) GetImportedBlockNotifierChannel() chan *types.Block { +func (_m *BlockState) GetImportedBlockNotifierChannel() chan *types.Block { ret := _m.Called() var r0 chan *types.Block @@ -332,7 +333,7 @@ func (_m *MockBlockState) GetImportedBlockNotifierChannel() chan *types.Block { } // GetRuntime provides a mock function with given fields: _a0 -func (_m *MockBlockState) GetRuntime(_a0 *common.Hash) (runtime.Instance, error) { +func (_m *BlockState) GetRuntime(_a0 *common.Hash) (runtime.Instance, error) { ret := _m.Called(_a0) var r0 runtime.Instance @@ -355,7 +356,7 @@ func (_m *MockBlockState) GetRuntime(_a0 *common.Hash) (runtime.Instance, error) } // GetSlotForBlock provides a mock function with given fields: _a0 -func (_m *MockBlockState) GetSlotForBlock(_a0 common.Hash) (uint64, error) { +func (_m *BlockState) GetSlotForBlock(_a0 common.Hash) (uint64, error) { ret := _m.Called(_a0) var r0 uint64 @@ -376,7 +377,7 @@ func (_m *MockBlockState) GetSlotForBlock(_a0 common.Hash) (uint64, error) { } // HandleRuntimeChanges provides a mock function with given fields: newState, in, bHash -func (_m *MockBlockState) HandleRuntimeChanges(newState *storage.TrieState, in runtime.Instance, bHash common.Hash) error { +func (_m *BlockState) HandleRuntimeChanges(newState *storage.TrieState, in runtime.Instance, bHash common.Hash) error { ret := _m.Called(newState, in, bHash) var r0 error @@ -390,7 +391,7 @@ func (_m *MockBlockState) HandleRuntimeChanges(newState *storage.TrieState, in r } // HighestCommonAncestor provides a mock function with given fields: a, b -func (_m *MockBlockState) HighestCommonAncestor(a common.Hash, b common.Hash) (common.Hash, error) { +func (_m *BlockState) HighestCommonAncestor(a common.Hash, b common.Hash) (common.Hash, error) { ret := _m.Called(a, b) var r0 common.Hash @@ -413,12 +414,12 @@ func (_m *MockBlockState) HighestCommonAncestor(a common.Hash, b common.Hash) (c } // StoreRuntime provides a mock function with given fields: _a0, _a1 -func (_m *MockBlockState) StoreRuntime(_a0 common.Hash, _a1 runtime.Instance) { +func (_m *BlockState) StoreRuntime(_a0 common.Hash, _a1 runtime.Instance) { _m.Called(_a0, _a1) } // SubChain provides a mock function with given fields: start, end -func (_m *MockBlockState) SubChain(start common.Hash, end common.Hash) ([]common.Hash, error) { +func (_m *BlockState) SubChain(start common.Hash, end common.Hash) ([]common.Hash, error) { ret := _m.Called(start, end) var r0 []common.Hash diff --git a/dot/core/mocks/digest_handler.go b/dot/core/mocks/digest_handler.go index 95dd03c168..c6f23a4c3c 100644 --- a/dot/core/mocks/digest_handler.go +++ b/dot/core/mocks/digest_handler.go @@ -1,4 +1,4 @@ -// Code generated by mockery v2.8.0. DO NOT EDIT. +// Code generated by mockery v2.9.4. DO NOT EDIT. package mocks @@ -7,12 +7,12 @@ import ( mock "github.com/stretchr/testify/mock" ) -// MockDigestHandler is an autogenerated mock type for the DigestHandler type -type MockDigestHandler struct { +// DigestHandler is an autogenerated mock type for the DigestHandler type +type DigestHandler struct { mock.Mock } // HandleDigests provides a mock function with given fields: header -func (_m *MockDigestHandler) HandleDigests(header *types.Header) { +func (_m *DigestHandler) HandleDigests(header *types.Header) { _m.Called(header) } diff --git a/dot/core/mocks/storage_state.go b/dot/core/mocks/storage_state.go index 654e1e8ec9..320a6165d0 100644 --- a/dot/core/mocks/storage_state.go +++ b/dot/core/mocks/storage_state.go @@ -1,4 +1,4 @@ -// Code generated by mockery v0.0.0-dev. DO NOT EDIT. +// Code generated by mockery v2.9.4. DO NOT EDIT. package mocks @@ -12,13 +12,13 @@ import ( types "github.com/ChainSafe/gossamer/dot/types" ) -// MockStorageState is an autogenerated mock type for the StorageState type -type MockStorageState struct { +// StorageState is an autogenerated mock type for the StorageState type +type StorageState struct { mock.Mock } // GenerateTrieProof provides a mock function with given fields: stateRoot, keys -func (_m *MockStorageState) GenerateTrieProof(stateRoot common.Hash, keys [][]byte) ([][]byte, error) { +func (_m *StorageState) GenerateTrieProof(stateRoot common.Hash, keys [][]byte) ([][]byte, error) { ret := _m.Called(stateRoot, keys) var r0 [][]byte @@ -41,7 +41,7 @@ func (_m *MockStorageState) GenerateTrieProof(stateRoot common.Hash, keys [][]by } // GetStateRootFromBlock provides a mock function with given fields: bhash -func (_m *MockStorageState) GetStateRootFromBlock(bhash *common.Hash) (*common.Hash, error) { +func (_m *StorageState) GetStateRootFromBlock(bhash *common.Hash) (*common.Hash, error) { ret := _m.Called(bhash) var r0 *common.Hash @@ -64,7 +64,7 @@ func (_m *MockStorageState) GetStateRootFromBlock(bhash *common.Hash) (*common.H } // GetStorage provides a mock function with given fields: root, key -func (_m *MockStorageState) GetStorage(root *common.Hash, key []byte) ([]byte, error) { +func (_m *StorageState) GetStorage(root *common.Hash, key []byte) ([]byte, error) { ret := _m.Called(root, key) var r0 []byte @@ -87,7 +87,7 @@ func (_m *MockStorageState) GetStorage(root *common.Hash, key []byte) ([]byte, e } // LoadCode provides a mock function with given fields: root -func (_m *MockStorageState) LoadCode(root *common.Hash) ([]byte, error) { +func (_m *StorageState) LoadCode(root *common.Hash) ([]byte, error) { ret := _m.Called(root) var r0 []byte @@ -110,7 +110,7 @@ func (_m *MockStorageState) LoadCode(root *common.Hash) ([]byte, error) { } // LoadCodeHash provides a mock function with given fields: root -func (_m *MockStorageState) LoadCodeHash(root *common.Hash) (common.Hash, error) { +func (_m *StorageState) LoadCodeHash(root *common.Hash) (common.Hash, error) { ret := _m.Called(root) var r0 common.Hash @@ -133,12 +133,12 @@ func (_m *MockStorageState) LoadCodeHash(root *common.Hash) (common.Hash, error) } // Lock provides a mock function with given fields: -func (_m *MockStorageState) Lock() { +func (_m *StorageState) Lock() { _m.Called() } // StoreTrie provides a mock function with given fields: _a0, _a1 -func (_m *MockStorageState) StoreTrie(_a0 *storage.TrieState, _a1 *types.Header) error { +func (_m *StorageState) StoreTrie(_a0 *storage.TrieState, _a1 *types.Header) error { ret := _m.Called(_a0, _a1) var r0 error @@ -152,7 +152,7 @@ func (_m *MockStorageState) StoreTrie(_a0 *storage.TrieState, _a1 *types.Header) } // TrieState provides a mock function with given fields: root -func (_m *MockStorageState) TrieState(root *common.Hash) (*storage.TrieState, error) { +func (_m *StorageState) TrieState(root *common.Hash) (*storage.TrieState, error) { ret := _m.Called(root) var r0 *storage.TrieState @@ -175,6 +175,6 @@ func (_m *MockStorageState) TrieState(root *common.Hash) (*storage.TrieState, er } // Unlock provides a mock function with given fields: -func (_m *MockStorageState) Unlock() { +func (_m *StorageState) Unlock() { _m.Called() } diff --git a/dot/core/service_test.go b/dot/core/service_test.go index c625eba2a7..c55fcf8cb9 100644 --- a/dot/core/service_test.go +++ b/dot/core/service_test.go @@ -70,7 +70,7 @@ func TestStartService(t *testing.T) { } func TestAnnounceBlock(t *testing.T) { - net := new(mocks.MockNetwork) + net := new(mocks.Network) cfg := &Config{ Network: net, } @@ -761,10 +761,10 @@ func createNewBlockAndStoreDataAtBlock(t *testing.T, s *Service, key, value []by } func TestDecodeSessionKeys(t *testing.T) { - mockInstance := new(runtimemocks.MockInstance) + mockInstance := new(runtimemocks.Instance) mockInstance.On("DecodeSessionKeys", mock.AnythingOfType("[]uint8")).Return([]byte{}, nil).Once() - mockBlockState := new(mocks.MockBlockState) + mockBlockState := new(mocks.BlockState) mockBlockState.On("GetRuntime", mock.AnythingOfType("*common.Hash")).Return(mockInstance, nil).Once() coreservice := new(Service) @@ -780,7 +780,7 @@ func TestDecodeSessionKeys(t *testing.T) { } func TestDecodeSessionKeys_WhenGetRuntimeReturnError(t *testing.T) { - mockBlockState := new(mocks.MockBlockState) + mockBlockState := new(mocks.BlockState) mockBlockState.On("GetRuntime", mock.AnythingOfType("*common.Hash")).Return(nil, errors.New("problems")).Once() coreservice := new(Service) @@ -801,12 +801,12 @@ func TestGetReadProofAt(t *testing.T) { mockedStateRootHash := common.NewHash([]byte("state root hash")) expectedBlockHash := common.NewHash([]byte("expected block hash")) - mockBlockState := new(mocks.MockBlockState) + mockBlockState := new(mocks.BlockState) mockBlockState.On("BestBlockHash").Return(expectedBlockHash) mockBlockState.On("GetBlockStateRoot", expectedBlockHash). Return(mockedStateRootHash, nil) - mockStorageStage := new(mocks.MockStorageState) + mockStorageStage := new(mocks.StorageState) mockStorageStage.On("GenerateTrieProof", mockedStateRootHash, keysToProof). Return(mockedProofs, nil) @@ -828,7 +828,7 @@ func TestGetReadProofAt(t *testing.T) { t.Run("When GetStateRoot fails", func(t *testing.T) { mockedBlockHash := common.NewHash([]byte("fake block hash")) - mockBlockState := new(mocks.MockBlockState) + mockBlockState := new(mocks.BlockState) mockBlockState.On("GetBlockStateRoot", mockedBlockHash). Return(common.EmptyHash, errors.New("problems while getting state root")) @@ -846,11 +846,11 @@ func TestGetReadProofAt(t *testing.T) { mockedBlockHash := common.NewHash([]byte("fake block hash")) mockedStateRootHash := common.NewHash([]byte("state root hash")) - mockBlockState := new(mocks.MockBlockState) + mockBlockState := new(mocks.BlockState) mockBlockState.On("GetBlockStateRoot", mockedBlockHash). Return(mockedStateRootHash, nil) - mockStorageStage := new(mocks.MockStorageState) + mockStorageStage := new(mocks.StorageState) mockStorageStage.On("GenerateTrieProof", mockedStateRootHash, keysToProof). Return(nil, errors.New("problems to generate trie proof")) diff --git a/dot/core/test_helpers.go b/dot/core/test_helpers.go index aad7aaa89a..f17f65e113 100644 --- a/dot/core/test_helpers.go +++ b/dot/core/test_helpers.go @@ -43,8 +43,8 @@ func NewTestService(t *testing.T, cfg *Config) *Service { cfg = &Config{} } - cfg.DigestHandler = new(coremocks.MockDigestHandler) - cfg.DigestHandler.(*coremocks.MockDigestHandler).On("HandleDigests", mock.AnythingOfType("*types.Header")) + cfg.DigestHandler = new(coremocks.DigestHandler) + cfg.DigestHandler.(*coremocks.DigestHandler).On("HandleDigests", mock.AnythingOfType("*types.Header")) if cfg.Keystore == nil { cfg.Keystore = keystore.NewGlobalKeystore() @@ -124,7 +124,7 @@ func NewTestService(t *testing.T, cfg *Config) *Service { cfg.BlockState.StoreRuntime(cfg.BlockState.BestBlockHash(), cfg.Runtime) if cfg.Network == nil { - net := new(coremocks.MockNetwork) + net := new(coremocks.Network) net.On("GossipMessage", mock.AnythingOfType("*network.TransactionMessage")) net.On("IsSynced").Return(true) cfg.Network = net diff --git a/dot/network/mock_block_state.go b/dot/network/mock_block_state.go index 54a9af2a6f..f403cf4421 100644 --- a/dot/network/mock_block_state.go +++ b/dot/network/mock_block_state.go @@ -1,4 +1,4 @@ -// Code generated by mockery v2.8.0. DO NOT EDIT. +// Code generated by mockery v2.9.4. DO NOT EDIT. package network @@ -11,13 +11,13 @@ import ( types "github.com/ChainSafe/gossamer/dot/types" ) -// mockBlockState is an autogenerated mock type for the BlockState type -type mockBlockState struct { +// MockBlockState is an autogenerated mock type for the BlockState type +type MockBlockState struct { mock.Mock } // BestBlockHeader provides a mock function with given fields: -func (_m *mockBlockState) BestBlockHeader() (*types.Header, error) { +func (_m *MockBlockState) BestBlockHeader() (*types.Header, error) { ret := _m.Called() var r0 *types.Header @@ -40,7 +40,7 @@ func (_m *mockBlockState) BestBlockHeader() (*types.Header, error) { } // BestBlockNumber provides a mock function with given fields: -func (_m *mockBlockState) BestBlockNumber() (*big.Int, error) { +func (_m *MockBlockState) BestBlockNumber() (*big.Int, error) { ret := _m.Called() var r0 *big.Int @@ -63,7 +63,7 @@ func (_m *mockBlockState) BestBlockNumber() (*big.Int, error) { } // GenesisHash provides a mock function with given fields: -func (_m *mockBlockState) GenesisHash() common.Hash { +func (_m *MockBlockState) GenesisHash() common.Hash { ret := _m.Called() var r0 common.Hash @@ -79,7 +79,7 @@ func (_m *mockBlockState) GenesisHash() common.Hash { } // GetHashByNumber provides a mock function with given fields: num -func (_m *mockBlockState) GetHashByNumber(num *big.Int) (common.Hash, error) { +func (_m *MockBlockState) GetHashByNumber(num *big.Int) (common.Hash, error) { ret := _m.Called(num) var r0 common.Hash @@ -102,7 +102,7 @@ func (_m *mockBlockState) GetHashByNumber(num *big.Int) (common.Hash, error) { } // GetHighestFinalisedHeader provides a mock function with given fields: -func (_m *mockBlockState) GetHighestFinalisedHeader() (*types.Header, error) { +func (_m *MockBlockState) GetHighestFinalisedHeader() (*types.Header, error) { ret := _m.Called() var r0 *types.Header @@ -125,7 +125,7 @@ func (_m *mockBlockState) GetHighestFinalisedHeader() (*types.Header, error) { } // HasBlockBody provides a mock function with given fields: _a0 -func (_m *mockBlockState) HasBlockBody(_a0 common.Hash) (bool, error) { +func (_m *MockBlockState) HasBlockBody(_a0 common.Hash) (bool, error) { ret := _m.Called(_a0) var r0 bool diff --git a/dot/network/mock_syncer.go b/dot/network/mock_syncer.go index a6f0b44216..01cfa86c13 100644 --- a/dot/network/mock_syncer.go +++ b/dot/network/mock_syncer.go @@ -1,13 +1,10 @@ -// Code generated by mockery v2.8.0. DO NOT EDIT. +// Code generated by mockery v2.9.4. DO NOT EDIT. package network import ( - mock "github.com/stretchr/testify/mock" - peer "github.com/libp2p/go-libp2p-core/peer" - - types "github.com/ChainSafe/gossamer/dot/types" + mock "github.com/stretchr/testify/mock" ) // MockSyncer is an autogenerated mock type for the Syncer type @@ -79,50 +76,3 @@ func (_m *MockSyncer) IsSynced() bool { return r0 } - -// ProcessBlockData provides a mock function with given fields: data -func (_m *MockSyncer) ProcessBlockData(data []*types.BlockData) (int, error) { - ret := _m.Called(data) - - var r0 int - if rf, ok := ret.Get(0).(func([]*types.BlockData) int); ok { - r0 = rf(data) - } else { - r0 = ret.Get(0).(int) - } - - var r1 error - if rf, ok := ret.Get(1).(func([]*types.BlockData) error); ok { - r1 = rf(data) - } else { - r1 = ret.Error(1) - } - - return r0, r1 -} - -// ProcessJustification provides a mock function with given fields: data -func (_m *MockSyncer) ProcessJustification(data []*types.BlockData) (int, error) { - ret := _m.Called(data) - - var r0 int - if rf, ok := ret.Get(0).(func([]*types.BlockData) int); ok { - r0 = rf(data) - } else { - r0 = ret.Get(0).(int) - } - - var r1 error - if rf, ok := ret.Get(1).(func([]*types.BlockData) error); ok { - r1 = rf(data) - } else { - r1 = ret.Error(1) - } - - return r0, r1 -} - -// SetSyncing provides a mock function with given fields: _a0 -func (_m *MockSyncer) SetSyncing(_a0 bool) { - _m.Called(_a0) -} diff --git a/dot/network/mock_transaction_handler.go b/dot/network/mock_transaction_handler.go index 5fe1d7e597..1d7461418a 100644 --- a/dot/network/mock_transaction_handler.go +++ b/dot/network/mock_transaction_handler.go @@ -1,4 +1,4 @@ -// Code generated by mockery v1.0.0. DO NOT EDIT. +// Code generated by mockery v2.9.4. DO NOT EDIT. package network diff --git a/dot/network/notifications.go b/dot/network/notifications.go index 818b71dffc..b236157d4e 100644 --- a/dot/network/notifications.go +++ b/dot/network/notifications.go @@ -57,10 +57,13 @@ type ( NotificationsMessageHandler = func(peer peer.ID, msg NotificationsMessage) (propagate bool, err error) // NotificationsMessageBatchHandler is called when a (non-handshake) message is received over a notifications stream in batch processing mode. - NotificationsMessageBatchHandler = func(peer peer.ID, msg NotificationsMessage) (batchMsgs []*batchMessage, err error) + NotificationsMessageBatchHandler = func(peer peer.ID, msg NotificationsMessage) (batchMsgs []*BatchMessage, err error) ) -type batchMessage struct { +// BatchMessage is exported for the mocks of lib/grandpa/mocks/network.go +// to be able to compile. +// TODO: unexport if changing mock library to e.g. github.com/golang/gomock +type BatchMessage struct { msg NotificationsMessage peer peer.ID } @@ -221,7 +224,7 @@ func (s *Service) createNotificationsMessageHandler(info *notificationsProtocol, var ( propagate bool err error - msgs []*batchMessage + msgs []*BatchMessage ) if batchHandler != nil { msgs, err = batchHandler(peer, msg) @@ -235,7 +238,7 @@ func (s *Service) createNotificationsMessageHandler(info *notificationsProtocol, if err != nil { return err } - msgs = append(msgs, &batchMessage{ + msgs = append(msgs, &BatchMessage{ msg: msg, peer: peer, }) diff --git a/dot/network/notifications_test.go b/dot/network/notifications_test.go index b7c4ca6b45..cfb8803bb4 100644 --- a/dot/network/notifications_test.go +++ b/dot/network/notifications_test.go @@ -344,7 +344,7 @@ func TestCreateNotificationsMessageHandler_HandleTransaction(t *testing.T) { b := createTestService(t, configB) - txnBatch := make(chan *batchMessage, s.batchSize) + txnBatch := make(chan *BatchMessage, s.batchSize) txnBatchHandler := s.createBatchMessageHandler(txnBatch) // don't set handshake data ie. this stream has just been opened diff --git a/dot/network/proto/mock_is_block_request__from_block.go b/dot/network/proto/mock_is_block_request__from_block.go deleted file mode 100644 index a76d478776..0000000000 --- a/dot/network/proto/mock_is_block_request__from_block.go +++ /dev/null @@ -1,15 +0,0 @@ -// Code generated by mockery v2.8.0. DO NOT EDIT. - -package api_v1 - -import mock "github.com/stretchr/testify/mock" - -// mockIsBlockRequest_FromBlock is an autogenerated mock type for the isBlockRequest_FromBlock type -type mockIsBlockRequest_FromBlock struct { - mock.Mock -} - -// isBlockRequest_FromBlock provides a mock function with given fields: -func (_m *mockIsBlockRequest_FromBlock) isBlockRequest_FromBlock() { - _m.Called() -} diff --git a/dot/network/service.go b/dot/network/service.go index 5e9fba5d41..e932cc760b 100644 --- a/dot/network/service.go +++ b/dot/network/service.go @@ -224,7 +224,7 @@ func (s *Service) Start() error { logger.Warn("failed to register notifications protocol", "sub-protocol", blockAnnounceID, "error", err) } - txnBatch := make(chan *batchMessage, s.batchSize) + txnBatch := make(chan *BatchMessage, s.batchSize) txnBatchHandler := s.createBatchMessageHandler(txnBatch) // register transactions protocol diff --git a/dot/network/state.go b/dot/network/state.go index 181107914f..0a343d2eb1 100644 --- a/dot/network/state.go +++ b/dot/network/state.go @@ -25,6 +25,8 @@ import ( "github.com/libp2p/go-libp2p-core/peer" ) +//go:generate mockery --name BlockState --structname MockBlockState --case underscore --inpackage + // BlockState interface for block state methods type BlockState interface { BestBlockHeader() (*types.Header, error) @@ -35,6 +37,8 @@ type BlockState interface { GetHashByNumber(num *big.Int) (common.Hash, error) } +//go:generate mockery --name Syncer --structname MockSyncer --case underscore --inpackage + // Syncer is implemented by the syncing service type Syncer interface { HandleBlockAnnounceHandshake(from peer.ID, msg *BlockAnnounceHandshake) error @@ -50,6 +54,8 @@ type Syncer interface { CreateBlockResponse(*BlockRequestMessage) (*BlockResponseMessage, error) } +//go:generate mockery --name TransactionHandler --structname MockTransactionHandler --case underscore --inpackage + // TransactionHandler is the interface used by the transactions sub-protocol type TransactionHandler interface { HandleTransactionMessage(*TransactionMessage) (bool, error) diff --git a/dot/network/test_helpers.go b/dot/network/test_helpers.go index 175ffa23bf..47cb3afb0a 100644 --- a/dot/network/test_helpers.go +++ b/dot/network/test_helpers.go @@ -17,7 +17,7 @@ import ( const blockRequestSize uint32 = 128 // NewMockBlockState create and return a network BlockState interface mock -func NewMockBlockState(n *big.Int) *mockBlockState { +func NewMockBlockState(n *big.Int) *MockBlockState { parentHash, _ := common.HexToHash("0x4545454545454545454545454545454545454545454545454545454545454545") stateRoot, _ := common.HexToHash("0xb3266de137d20a5d0ff3a6401eb57127525fd9b2693701f0bf5a8a853fa3ebe0") extrinsicsRoot, _ := common.HexToHash("0x03170a2e7597b7b7e3d84c05391d139a62b157e78786d8c082f29dcf4c111314") @@ -33,7 +33,7 @@ func NewMockBlockState(n *big.Int) *mockBlockState { Digest: types.NewDigest(), } - m := new(mockBlockState) + m := new(MockBlockState) m.On("BestBlockHeader").Return(header, nil) m.On("GetHighestFinalisedHeader").Return(header, nil) m.On("GenesisHash").Return(common.NewHash([]byte{})) diff --git a/dot/network/transaction.go b/dot/network/transaction.go index 90301e17db..0e4cead9fc 100644 --- a/dot/network/transaction.go +++ b/dot/network/transaction.go @@ -119,9 +119,9 @@ func decodeTransactionHandshake(_ []byte) (Handshake, error) { return &transactionHandshake{}, nil } -func (s *Service) createBatchMessageHandler(txnBatch chan *batchMessage) NotificationsMessageBatchHandler { - return func(peer peer.ID, msg NotificationsMessage) (msgs []*batchMessage, err error) { - data := &batchMessage{ +func (s *Service) createBatchMessageHandler(txnBatch chan *BatchMessage) NotificationsMessageBatchHandler { + return func(peer peer.ID, msg NotificationsMessage) (msgs []*BatchMessage, err error) { + data := &BatchMessage{ msg: msg, peer: peer, } @@ -131,14 +131,14 @@ func (s *Service) createBatchMessageHandler(txnBatch chan *batchMessage) Notific return nil, nil } - var propagateMsgs []*batchMessage + var propagateMsgs []*BatchMessage for txnData := range txnBatch { propagate, err := s.handleTransactionMessage(txnData.peer, txnData.msg) if err != nil { continue } if propagate { - propagateMsgs = append(propagateMsgs, &batchMessage{ + propagateMsgs = append(propagateMsgs, &BatchMessage{ msg: txnData.msg, peer: txnData.peer, }) diff --git a/dot/rpc/http_test.go b/dot/rpc/http_test.go index 2fc7f0d11d..6d78600f55 100644 --- a/dot/rpc/http_test.go +++ b/dot/rpc/http_test.go @@ -39,7 +39,7 @@ import ( ) func TestRegisterModules(t *testing.T) { - rpcapiMocks := new(mocks.MockRPCAPI) + rpcapiMocks := new(mocks.RPCAPI) mods := []string{"system", "author", "chain", "state", "rpc", "grandpa", "offchain", "childstate", "syncstate"} @@ -167,7 +167,7 @@ func TestRPCUnsafeExpose(t *testing.T) { _, err := buf.Write(data) require.NoError(t, err) - netmock := new(mocks.MockNetworkAPI) + netmock := new(mocks.NetworkAPI) netmock.On("AddReservedPeers", mock.AnythingOfType("string")).Return(nil) cfg := &HTTPServerConfig{ @@ -204,7 +204,7 @@ func TestUnsafeRPCJustToLocalhost(t *testing.T) { _, err := buf.Write(data) require.NoError(t, err) - netmock := new(mocks.MockNetworkAPI) + netmock := new(mocks.NetworkAPI) netmock.On("AddReservedPeers", mock.AnythingOfType("string")).Return(nil) cfg := &HTTPServerConfig{ @@ -244,7 +244,7 @@ func TestRPCExternalEnable_UnsafeExternalNotEnabled(t *testing.T) { safebuf := new(bytes.Buffer) safebuf.Write(safeData) - netmock := new(mocks.MockNetworkAPI) + netmock := new(mocks.NetworkAPI) netmock.On("NetworkState").Return(common.NetworkState{ PeerID: "peer id", }) diff --git a/dot/rpc/modules/api.go b/dot/rpc/modules/api.go index ed7000bf71..41556bd1d3 100644 --- a/dot/rpc/modules/api.go +++ b/dot/rpc/modules/api.go @@ -16,6 +16,8 @@ import ( "github.com/ChainSafe/gossamer/lib/trie" ) +//go:generate mockery --name StorageAPI --structname StorageAPI --case underscore --keeptree + // StorageAPI is the interface for the storage state type StorageAPI interface { GetStorage(root *common.Hash, key []byte) ([]byte, error) @@ -29,6 +31,8 @@ type StorageAPI interface { UnregisterStorageObserver(observer state.Observer) } +//go:generate mockery --name BlockAPI --structname BlockAPI --case underscore --keeptree + // BlockAPI is the interface for the block state type BlockAPI interface { GetHeader(hash common.Hash) (*types.Header, error) @@ -49,6 +53,8 @@ type BlockAPI interface { GetRuntime(hash *common.Hash) (runtime.Instance, error) } +//go:generate mockery --name NetworkAPI --structname NetworkAPI --case underscore --keeptree + // NetworkAPI interface for network state methods type NetworkAPI interface { Health() common.Health @@ -72,6 +78,8 @@ type BlockProducerAPI interface { SlotDuration() uint64 } +//go:generate mockery --name TransactionStateAPI --structname TransactionStateAPI --case underscore --keeptree + // TransactionStateAPI ... type TransactionStateAPI interface { AddToPool(*transaction.ValidTransaction) common.Hash @@ -80,6 +88,8 @@ type TransactionStateAPI interface { Pending() []*transaction.ValidTransaction } +//go:generate mockery --name CoreAPI --structname CoreAPI --case underscore --keeptree + // CoreAPI is the interface for the core methods type CoreAPI interface { InsertKey(kp crypto.Keypair) @@ -92,12 +102,16 @@ type CoreAPI interface { GetReadProofAt(block common.Hash, keys [][]byte) (common.Hash, [][]byte, error) } +//go:generate mockery --name RPCAPI --structname RPCAPI --case underscore --keeptree + // RPCAPI is the interface for methods related to RPC service type RPCAPI interface { Methods() []string BuildMethodNames(rcvr interface{}, name string) } +//go:generate mockery --name SystemAPI --structname SystemAPI --case underscore --keeptree + // SystemAPI is the interface for handling system methods type SystemAPI interface { SystemName() string @@ -107,6 +121,8 @@ type SystemAPI interface { ChainName() string } +//go:generate mockery --name BlockFinalityAPI --structname BlockFinalityAPI --case underscore --keeptree + // BlockFinalityAPI is the interface for handling block finalisation methods type BlockFinalityAPI interface { GetSetID() uint64 @@ -116,6 +132,8 @@ type BlockFinalityAPI interface { PreCommits() []ed25519.PublicKeyBytes } +//go:generate mockery --name RuntimeStorageAPI --structname RuntimeStorageAPI --case underscore --keeptree + // RuntimeStorageAPI is the interface to interacts with the node storage type RuntimeStorageAPI interface { SetLocal(k, v []byte) error diff --git a/dot/rpc/modules/api_mocks.go b/dot/rpc/modules/api_mocks.go index 613300810e..1aac332be6 100644 --- a/dot/rpc/modules/api_mocks.go +++ b/dot/rpc/modules/api_mocks.go @@ -9,8 +9,8 @@ import ( ) // NewMockStorageAPI creates and return an rpc StorageAPI interface mock -func NewMockStorageAPI() *modulesmocks.MockStorageAPI { - m := new(modulesmocks.MockStorageAPI) +func NewMockStorageAPI() *modulesmocks.StorageAPI { + m := new(modulesmocks.StorageAPI) m.On("GetStorage", mock.AnythingOfType("*common.Hash"), mock.AnythingOfType("[]uint8")).Return(nil, nil) m.On("GetStorageFromChild", mock.AnythingOfType("*common.Hash"), mock.AnythingOfType("[]uint8"), mock.AnythingOfType("[]uint8")).Return(nil, nil) m.On("Entries", mock.AnythingOfType("*common.Hash")).Return(nil, nil) @@ -23,8 +23,8 @@ func NewMockStorageAPI() *modulesmocks.MockStorageAPI { } // NewMockBlockAPI creates and return an rpc BlockAPI interface mock -func NewMockBlockAPI() *modulesmocks.MockBlockAPI { - m := new(modulesmocks.MockBlockAPI) +func NewMockBlockAPI() *modulesmocks.BlockAPI { + m := new(modulesmocks.BlockAPI) m.On("GetHeader", mock.AnythingOfType("common.Hash")).Return(nil, nil) m.On("BestBlockHash").Return(common.Hash{}) m.On("GetBlockByHash", mock.AnythingOfType("common.Hash")).Return(nil, nil) @@ -44,8 +44,8 @@ func NewMockBlockAPI() *modulesmocks.MockBlockAPI { } // NewMockCoreAPI creates and return an rpc CoreAPI interface mock -func NewMockCoreAPI() *modulesmocks.MockCoreAPI { - m := new(modulesmocks.MockCoreAPI) +func NewMockCoreAPI() *modulesmocks.CoreAPI { + m := new(modulesmocks.CoreAPI) m.On("InsertKey", mock.AnythingOfType("crypto.Keypair")) m.On("HasKey", mock.AnythingOfType("string"), mock.AnythingOfType("string")).Return(false, nil) m.On("GetRuntimeVersion", mock.AnythingOfType("*common.Hash")).Return(NewMockVersion(), nil) diff --git a/dot/rpc/modules/author_test.go b/dot/rpc/modules/author_test.go index 2d84d779ec..e80bed8a68 100644 --- a/dot/rpc/modules/author_test.go +++ b/dot/rpc/modules/author_test.go @@ -24,7 +24,7 @@ func TestAuthorModule_HasSessionKey_WhenScaleDataEmptyOrNil(t *testing.T) { keys := "0x00" runtimeInstance := wasmer.NewTestInstance(t, runtime.NODE_RUNTIME) - coremockapi := new(apimocks.MockCoreAPI) + coremockapi := new(apimocks.CoreAPI) decodeSessionKeysMock := coremockapi.On("DecodeSessionKeys", mock.AnythingOfType("[]uint8")) decodeSessionKeysMock.Run(func(args mock.Arguments) { @@ -51,7 +51,7 @@ func TestAuthorModule_HasSessionKey_WhenScaleDataEmptyOrNil(t *testing.T) { } func TestAuthorModule_HasSessionKey_WhenRuntimeFails(t *testing.T) { - coremockapi := new(apimocks.MockCoreAPI) + coremockapi := new(apimocks.CoreAPI) coremockapi.On("DecodeSessionKeys", mock.AnythingOfType("[]uint8")).Return(nil, errors.New("problems with runtime")) module := &AuthorModule{ @@ -73,7 +73,7 @@ func TestAuthorModule_HasSessionKey_WhenThereIsNoKeys(t *testing.T) { keys := "0xd43593c715fdd31c61141abd04a99fd6822c8558854ccde39a5684e7a56da27d34309a9d2a24213896ff06895db16aade8b6502f3a71cf56374cc3852042602634309a9d2a24213896ff06895db16aade8b6502f3a71cf56374cc3852042602634309a9d2a24213896ff06895db16aade8b6502f3a71cf56374cc38520426026" runtimeInstance := wasmer.NewTestInstance(t, runtime.NODE_RUNTIME) - coremockapi := new(apimocks.MockCoreAPI) + coremockapi := new(apimocks.CoreAPI) coremockapi.On("HasKey", mock.AnythingOfType("string"), mock.AnythingOfType("string")).Return(false, nil) decodeSessionKeysMock := coremockapi.On("DecodeSessionKeys", mock.AnythingOfType("[]uint8")) @@ -105,7 +105,7 @@ func TestAuthorModule_HasSessionKey_WhenThereIsNoKeys(t *testing.T) { func TestAuthorModule_HasSessionKey(t *testing.T) { globalStore := keystore.NewGlobalKeystore() - coremockapi := new(apimocks.MockCoreAPI) + coremockapi := new(apimocks.CoreAPI) mockInsertKey := coremockapi.On("InsertKey", mock.AnythingOfType("*sr25519.Keypair")) mockInsertKey.Run(func(args mock.Arguments) { kp := args.Get(0).(*sr25519.Keypair) @@ -164,10 +164,10 @@ func TestAuthorModule_HasSessionKey(t *testing.T) { } func TestAuthorModule_SubmitExtrinsic(t *testing.T) { - errMockCoreAPI := &apimocks.MockCoreAPI{} + errMockCoreAPI := &apimocks.CoreAPI{} errMockCoreAPI.On("HandleSubmittedExtrinsic", mock.AnythingOfType("types.Extrinsic")).Return(fmt.Errorf("some error")) - mockCoreAPI := &apimocks.MockCoreAPI{} + mockCoreAPI := &apimocks.CoreAPI{} mockCoreAPI.On("HandleSubmittedExtrinsic", mock.AnythingOfType("types.Extrinsic")).Return(nil) // https://github.com/paritytech/substrate/blob/5420de3face1349a97eb954ae71c5b0b940c31de/core/transaction-pool/src/tests.rs#L95 @@ -322,7 +322,7 @@ func TestAuthorModule_PendingExtrinsics(t *testing.T) { } func TestAuthorModule_InsertKey(t *testing.T) { - mockCoreAPI := &apimocks.MockCoreAPI{} + mockCoreAPI := &apimocks.CoreAPI{} mockCoreAPI.On("InsertKey", mock.Anything).Return(nil) type fields struct { @@ -414,13 +414,13 @@ func TestAuthorModule_InsertKey(t *testing.T) { } func TestAuthorModule_HasKey(t *testing.T) { - mockCoreAPITrue := &apimocks.MockCoreAPI{} + mockCoreAPITrue := &apimocks.CoreAPI{} mockCoreAPITrue.On("HasKey", mock.Anything, mock.Anything).Return(true, nil) - mockCoreAPIFalse := &apimocks.MockCoreAPI{} + mockCoreAPIFalse := &apimocks.CoreAPI{} mockCoreAPIFalse.On("HasKey", mock.Anything, mock.Anything).Return(false, nil) - mockCoreAPIErr := &apimocks.MockCoreAPI{} + mockCoreAPIErr := &apimocks.CoreAPI{} mockCoreAPIErr.On("HasKey", mock.Anything, mock.Anything).Return(false, fmt.Errorf("some error")) kr, err := keystore.NewSr25519Keyring() diff --git a/dot/rpc/modules/grandpa_test.go b/dot/rpc/modules/grandpa_test.go index f17d65ad68..471e010c15 100644 --- a/dot/rpc/modules/grandpa_test.go +++ b/dot/rpc/modules/grandpa_test.go @@ -75,7 +75,7 @@ func TestRoundState(t *testing.T) { }) } - grandpamock := new(rpcmocks.MockBlockFinalityAPI) + grandpamock := new(rpcmocks.BlockFinalityAPI) grandpamock.On("GetVoters").Return(voters) grandpamock.On("GetSetID").Return(uint64(0)) grandpamock.On("GetRound").Return(uint64(2)) diff --git a/dot/rpc/modules/mocks/block_api.go b/dot/rpc/modules/mocks/block_api.go index f6d43d496e..c70a2b1bc3 100644 --- a/dot/rpc/modules/mocks/block_api.go +++ b/dot/rpc/modules/mocks/block_api.go @@ -1,4 +1,4 @@ -// Code generated by mockery v0.0.0-dev. DO NOT EDIT. +// Code generated by mockery v2.9.4. DO NOT EDIT. package mocks @@ -13,13 +13,13 @@ import ( types "github.com/ChainSafe/gossamer/dot/types" ) -// MockBlockAPI is an autogenerated mock type for the BlockAPI type -type MockBlockAPI struct { +// BlockAPI is an autogenerated mock type for the BlockAPI type +type BlockAPI struct { mock.Mock } // BestBlockHash provides a mock function with given fields: -func (_m *MockBlockAPI) BestBlockHash() common.Hash { +func (_m *BlockAPI) BestBlockHash() common.Hash { ret := _m.Called() var r0 common.Hash @@ -35,17 +35,17 @@ func (_m *MockBlockAPI) BestBlockHash() common.Hash { } // FreeFinalisedNotifierChannel provides a mock function with given fields: ch -func (_m *MockBlockAPI) FreeFinalisedNotifierChannel(ch chan *types.FinalisationInfo) { +func (_m *BlockAPI) FreeFinalisedNotifierChannel(ch chan *types.FinalisationInfo) { _m.Called(ch) } // FreeImportedBlockNotifierChannel provides a mock function with given fields: ch -func (_m *MockBlockAPI) FreeImportedBlockNotifierChannel(ch chan *types.Block) { +func (_m *BlockAPI) FreeImportedBlockNotifierChannel(ch chan *types.Block) { _m.Called(ch) } // GetBlockByHash provides a mock function with given fields: hash -func (_m *MockBlockAPI) GetBlockByHash(hash common.Hash) (*types.Block, error) { +func (_m *BlockAPI) GetBlockByHash(hash common.Hash) (*types.Block, error) { ret := _m.Called(hash) var r0 *types.Block @@ -68,7 +68,7 @@ func (_m *MockBlockAPI) GetBlockByHash(hash common.Hash) (*types.Block, error) { } // GetBlockHash provides a mock function with given fields: blockNumber -func (_m *MockBlockAPI) GetBlockHash(blockNumber *big.Int) (common.Hash, error) { +func (_m *BlockAPI) GetBlockHash(blockNumber *big.Int) (common.Hash, error) { ret := _m.Called(blockNumber) var r0 common.Hash @@ -91,7 +91,7 @@ func (_m *MockBlockAPI) GetBlockHash(blockNumber *big.Int) (common.Hash, error) } // GetFinalisedHash provides a mock function with given fields: _a0, _a1 -func (_m *MockBlockAPI) GetFinalisedHash(_a0 uint64, _a1 uint64) (common.Hash, error) { +func (_m *BlockAPI) GetFinalisedHash(_a0 uint64, _a1 uint64) (common.Hash, error) { ret := _m.Called(_a0, _a1) var r0 common.Hash @@ -114,7 +114,7 @@ func (_m *MockBlockAPI) GetFinalisedHash(_a0 uint64, _a1 uint64) (common.Hash, e } // GetFinalisedNotifierChannel provides a mock function with given fields: -func (_m *MockBlockAPI) GetFinalisedNotifierChannel() chan *types.FinalisationInfo { +func (_m *BlockAPI) GetFinalisedNotifierChannel() chan *types.FinalisationInfo { ret := _m.Called() var r0 chan *types.FinalisationInfo @@ -130,7 +130,7 @@ func (_m *MockBlockAPI) GetFinalisedNotifierChannel() chan *types.FinalisationIn } // GetHeader provides a mock function with given fields: hash -func (_m *MockBlockAPI) GetHeader(hash common.Hash) (*types.Header, error) { +func (_m *BlockAPI) GetHeader(hash common.Hash) (*types.Header, error) { ret := _m.Called(hash) var r0 *types.Header @@ -153,7 +153,7 @@ func (_m *MockBlockAPI) GetHeader(hash common.Hash) (*types.Header, error) { } // GetHighestFinalisedHash provides a mock function with given fields: -func (_m *MockBlockAPI) GetHighestFinalisedHash() (common.Hash, error) { +func (_m *BlockAPI) GetHighestFinalisedHash() (common.Hash, error) { ret := _m.Called() var r0 common.Hash @@ -176,7 +176,7 @@ func (_m *MockBlockAPI) GetHighestFinalisedHash() (common.Hash, error) { } // GetImportedBlockNotifierChannel provides a mock function with given fields: -func (_m *MockBlockAPI) GetImportedBlockNotifierChannel() chan *types.Block { +func (_m *BlockAPI) GetImportedBlockNotifierChannel() chan *types.Block { ret := _m.Called() var r0 chan *types.Block @@ -192,7 +192,7 @@ func (_m *MockBlockAPI) GetImportedBlockNotifierChannel() chan *types.Block { } // GetJustification provides a mock function with given fields: hash -func (_m *MockBlockAPI) GetJustification(hash common.Hash) ([]byte, error) { +func (_m *BlockAPI) GetJustification(hash common.Hash) ([]byte, error) { ret := _m.Called(hash) var r0 []byte @@ -215,7 +215,7 @@ func (_m *MockBlockAPI) GetJustification(hash common.Hash) ([]byte, error) { } // GetRuntime provides a mock function with given fields: hash -func (_m *MockBlockAPI) GetRuntime(hash *common.Hash) (runtime.Instance, error) { +func (_m *BlockAPI) GetRuntime(hash *common.Hash) (runtime.Instance, error) { ret := _m.Called(hash) var r0 runtime.Instance @@ -238,7 +238,7 @@ func (_m *MockBlockAPI) GetRuntime(hash *common.Hash) (runtime.Instance, error) } // HasJustification provides a mock function with given fields: hash -func (_m *MockBlockAPI) HasJustification(hash common.Hash) (bool, error) { +func (_m *BlockAPI) HasJustification(hash common.Hash) (bool, error) { ret := _m.Called(hash) var r0 bool @@ -259,7 +259,7 @@ func (_m *MockBlockAPI) HasJustification(hash common.Hash) (bool, error) { } // RegisterRuntimeUpdatedChannel provides a mock function with given fields: ch -func (_m *MockBlockAPI) RegisterRuntimeUpdatedChannel(ch chan<- runtime.Version) (uint32, error) { +func (_m *BlockAPI) RegisterRuntimeUpdatedChannel(ch chan<- runtime.Version) (uint32, error) { ret := _m.Called(ch) var r0 uint32 @@ -280,7 +280,7 @@ func (_m *MockBlockAPI) RegisterRuntimeUpdatedChannel(ch chan<- runtime.Version) } // SubChain provides a mock function with given fields: start, end -func (_m *MockBlockAPI) SubChain(start common.Hash, end common.Hash) ([]common.Hash, error) { +func (_m *BlockAPI) SubChain(start common.Hash, end common.Hash) ([]common.Hash, error) { ret := _m.Called(start, end) var r0 []common.Hash @@ -303,7 +303,7 @@ func (_m *MockBlockAPI) SubChain(start common.Hash, end common.Hash) ([]common.H } // UnregisterRuntimeUpdatedChannel provides a mock function with given fields: id -func (_m *MockBlockAPI) UnregisterRuntimeUpdatedChannel(id uint32) bool { +func (_m *BlockAPI) UnregisterRuntimeUpdatedChannel(id uint32) bool { ret := _m.Called(id) var r0 bool diff --git a/dot/rpc/modules/mocks/block_finality_api.go b/dot/rpc/modules/mocks/block_finality_api.go index 5defee8456..e4eed4ed38 100644 --- a/dot/rpc/modules/mocks/block_finality_api.go +++ b/dot/rpc/modules/mocks/block_finality_api.go @@ -1,21 +1,21 @@ -// Code generated by mockery v2.8.0. DO NOT EDIT. +// Code generated by mockery v2.9.4. DO NOT EDIT. package mocks import ( ed25519 "github.com/ChainSafe/gossamer/lib/crypto/ed25519" - "github.com/ChainSafe/gossamer/lib/grandpa" - mock "github.com/stretchr/testify/mock" + + types "github.com/ChainSafe/gossamer/dot/types" ) -// MockBlockFinalityAPI is an autogenerated mock type for the BlockFinalityAPI type -type MockBlockFinalityAPI struct { +// BlockFinalityAPI is an autogenerated mock type for the BlockFinalityAPI type +type BlockFinalityAPI struct { mock.Mock } // GetRound provides a mock function with given fields: -func (_m *MockBlockFinalityAPI) GetRound() uint64 { +func (_m *BlockFinalityAPI) GetRound() uint64 { ret := _m.Called() var r0 uint64 @@ -29,7 +29,7 @@ func (_m *MockBlockFinalityAPI) GetRound() uint64 { } // GetSetID provides a mock function with given fields: -func (_m *MockBlockFinalityAPI) GetSetID() uint64 { +func (_m *BlockFinalityAPI) GetSetID() uint64 { ret := _m.Called() var r0 uint64 @@ -43,15 +43,15 @@ func (_m *MockBlockFinalityAPI) GetSetID() uint64 { } // GetVoters provides a mock function with given fields: -func (_m *MockBlockFinalityAPI) GetVoters() grandpa.Voters { +func (_m *BlockFinalityAPI) GetVoters() types.GrandpaVoters { ret := _m.Called() - var r0 grandpa.Voters - if rf, ok := ret.Get(0).(func() grandpa.Voters); ok { + var r0 types.GrandpaVoters + if rf, ok := ret.Get(0).(func() types.GrandpaVoters); ok { r0 = rf() } else { if ret.Get(0) != nil { - r0 = ret.Get(0).(grandpa.Voters) + r0 = ret.Get(0).(types.GrandpaVoters) } } @@ -59,7 +59,7 @@ func (_m *MockBlockFinalityAPI) GetVoters() grandpa.Voters { } // PreCommits provides a mock function with given fields: -func (_m *MockBlockFinalityAPI) PreCommits() []ed25519.PublicKeyBytes { +func (_m *BlockFinalityAPI) PreCommits() []ed25519.PublicKeyBytes { ret := _m.Called() var r0 []ed25519.PublicKeyBytes @@ -75,7 +75,7 @@ func (_m *MockBlockFinalityAPI) PreCommits() []ed25519.PublicKeyBytes { } // PreVotes provides a mock function with given fields: -func (_m *MockBlockFinalityAPI) PreVotes() []ed25519.PublicKeyBytes { +func (_m *BlockFinalityAPI) PreVotes() []ed25519.PublicKeyBytes { ret := _m.Called() var r0 []ed25519.PublicKeyBytes diff --git a/dot/rpc/modules/mocks/core_api.go b/dot/rpc/modules/mocks/core_api.go index fe20861ab2..61b25518d6 100644 --- a/dot/rpc/modules/mocks/core_api.go +++ b/dot/rpc/modules/mocks/core_api.go @@ -1,4 +1,4 @@ -// Code generated by mockery v2.8.0. DO NOT EDIT. +// Code generated by mockery v2.9.4. DO NOT EDIT. package mocks @@ -15,13 +15,13 @@ import ( types "github.com/ChainSafe/gossamer/dot/types" ) -// MockCoreAPI is an autogenerated mock type for the CoreAPI type -type MockCoreAPI struct { +// CoreAPI is an autogenerated mock type for the CoreAPI type +type CoreAPI struct { mock.Mock } // DecodeSessionKeys provides a mock function with given fields: enc -func (_m *MockCoreAPI) DecodeSessionKeys(enc []byte) ([]byte, error) { +func (_m *CoreAPI) DecodeSessionKeys(enc []byte) ([]byte, error) { ret := _m.Called(enc) var r0 []byte @@ -44,7 +44,7 @@ func (_m *MockCoreAPI) DecodeSessionKeys(enc []byte) ([]byte, error) { } // GetMetadata provides a mock function with given fields: bhash -func (_m *MockCoreAPI) GetMetadata(bhash *common.Hash) ([]byte, error) { +func (_m *CoreAPI) GetMetadata(bhash *common.Hash) ([]byte, error) { ret := _m.Called(bhash) var r0 []byte @@ -67,7 +67,7 @@ func (_m *MockCoreAPI) GetMetadata(bhash *common.Hash) ([]byte, error) { } // GetReadProofAt provides a mock function with given fields: block, keys -func (_m *MockCoreAPI) GetReadProofAt(block common.Hash, keys [][]byte) (common.Hash, [][]byte, error) { +func (_m *CoreAPI) GetReadProofAt(block common.Hash, keys [][]byte) (common.Hash, [][]byte, error) { ret := _m.Called(block, keys) var r0 common.Hash @@ -99,7 +99,7 @@ func (_m *MockCoreAPI) GetReadProofAt(block common.Hash, keys [][]byte) (common. } // GetRuntimeVersion provides a mock function with given fields: bhash -func (_m *MockCoreAPI) GetRuntimeVersion(bhash *common.Hash) (runtime.Version, error) { +func (_m *CoreAPI) GetRuntimeVersion(bhash *common.Hash) (runtime.Version, error) { ret := _m.Called(bhash) var r0 runtime.Version @@ -122,7 +122,7 @@ func (_m *MockCoreAPI) GetRuntimeVersion(bhash *common.Hash) (runtime.Version, e } // HandleSubmittedExtrinsic provides a mock function with given fields: _a0 -func (_m *MockCoreAPI) HandleSubmittedExtrinsic(_a0 types.Extrinsic) error { +func (_m *CoreAPI) HandleSubmittedExtrinsic(_a0 types.Extrinsic) error { ret := _m.Called(_a0) var r0 error @@ -136,7 +136,7 @@ func (_m *MockCoreAPI) HandleSubmittedExtrinsic(_a0 types.Extrinsic) error { } // HasKey provides a mock function with given fields: pubKeyStr, keyType -func (_m *MockCoreAPI) HasKey(pubKeyStr string, keyType string) (bool, error) { +func (_m *CoreAPI) HasKey(pubKeyStr string, keyType string) (bool, error) { ret := _m.Called(pubKeyStr, keyType) var r0 bool @@ -157,12 +157,12 @@ func (_m *MockCoreAPI) HasKey(pubKeyStr string, keyType string) (bool, error) { } // InsertKey provides a mock function with given fields: kp -func (_m *MockCoreAPI) InsertKey(kp crypto.Keypair) { +func (_m *CoreAPI) InsertKey(kp crypto.Keypair) { _m.Called(kp) } // QueryStorage provides a mock function with given fields: from, to, keys -func (_m *MockCoreAPI) QueryStorage(from common.Hash, to common.Hash, keys ...string) (map[common.Hash]core.QueryKeyValueChanges, error) { +func (_m *CoreAPI) QueryStorage(from common.Hash, to common.Hash, keys ...string) (map[common.Hash]core.QueryKeyValueChanges, error) { _va := make([]interface{}, len(keys)) for _i := range keys { _va[_i] = keys[_i] diff --git a/dot/rpc/modules/mocks/network_api.go b/dot/rpc/modules/mocks/network_api.go index 7d8360dc5f..d9dd66275a 100644 --- a/dot/rpc/modules/mocks/network_api.go +++ b/dot/rpc/modules/mocks/network_api.go @@ -1,4 +1,4 @@ -// Code generated by mockery v2.8.0. DO NOT EDIT. +// Code generated by mockery v2.9.4. DO NOT EDIT. package mocks @@ -7,13 +7,13 @@ import ( mock "github.com/stretchr/testify/mock" ) -// MockNetworkAPI is an autogenerated mock type for the NetworkAPI type -type MockNetworkAPI struct { +// NetworkAPI is an autogenerated mock type for the NetworkAPI type +type NetworkAPI struct { mock.Mock } // AddReservedPeers provides a mock function with given fields: addrs -func (_m *MockNetworkAPI) AddReservedPeers(addrs ...string) error { +func (_m *NetworkAPI) AddReservedPeers(addrs ...string) error { _va := make([]interface{}, len(addrs)) for _i := range addrs { _va[_i] = addrs[_i] @@ -33,7 +33,7 @@ func (_m *MockNetworkAPI) AddReservedPeers(addrs ...string) error { } // Health provides a mock function with given fields: -func (_m *MockNetworkAPI) Health() common.Health { +func (_m *NetworkAPI) Health() common.Health { ret := _m.Called() var r0 common.Health @@ -47,7 +47,7 @@ func (_m *MockNetworkAPI) Health() common.Health { } // HighestBlock provides a mock function with given fields: -func (_m *MockNetworkAPI) HighestBlock() int64 { +func (_m *NetworkAPI) HighestBlock() int64 { ret := _m.Called() var r0 int64 @@ -61,7 +61,7 @@ func (_m *MockNetworkAPI) HighestBlock() int64 { } // IsStopped provides a mock function with given fields: -func (_m *MockNetworkAPI) IsStopped() bool { +func (_m *NetworkAPI) IsStopped() bool { ret := _m.Called() var r0 bool @@ -75,7 +75,7 @@ func (_m *MockNetworkAPI) IsStopped() bool { } // NetworkState provides a mock function with given fields: -func (_m *MockNetworkAPI) NetworkState() common.NetworkState { +func (_m *NetworkAPI) NetworkState() common.NetworkState { ret := _m.Called() var r0 common.NetworkState @@ -89,7 +89,7 @@ func (_m *MockNetworkAPI) NetworkState() common.NetworkState { } // NodeRoles provides a mock function with given fields: -func (_m *MockNetworkAPI) NodeRoles() byte { +func (_m *NetworkAPI) NodeRoles() byte { ret := _m.Called() var r0 byte @@ -103,7 +103,7 @@ func (_m *MockNetworkAPI) NodeRoles() byte { } // Peers provides a mock function with given fields: -func (_m *MockNetworkAPI) Peers() []common.PeerInfo { +func (_m *NetworkAPI) Peers() []common.PeerInfo { ret := _m.Called() var r0 []common.PeerInfo @@ -119,7 +119,7 @@ func (_m *MockNetworkAPI) Peers() []common.PeerInfo { } // RemoveReservedPeers provides a mock function with given fields: addrs -func (_m *MockNetworkAPI) RemoveReservedPeers(addrs ...string) error { +func (_m *NetworkAPI) RemoveReservedPeers(addrs ...string) error { _va := make([]interface{}, len(addrs)) for _i := range addrs { _va[_i] = addrs[_i] @@ -139,7 +139,7 @@ func (_m *MockNetworkAPI) RemoveReservedPeers(addrs ...string) error { } // Start provides a mock function with given fields: -func (_m *MockNetworkAPI) Start() error { +func (_m *NetworkAPI) Start() error { ret := _m.Called() var r0 error @@ -153,7 +153,7 @@ func (_m *MockNetworkAPI) Start() error { } // StartingBlock provides a mock function with given fields: -func (_m *MockNetworkAPI) StartingBlock() int64 { +func (_m *NetworkAPI) StartingBlock() int64 { ret := _m.Called() var r0 int64 @@ -167,7 +167,7 @@ func (_m *MockNetworkAPI) StartingBlock() int64 { } // Stop provides a mock function with given fields: -func (_m *MockNetworkAPI) Stop() error { +func (_m *NetworkAPI) Stop() error { ret := _m.Called() var r0 error diff --git a/dot/rpc/modules/mocks/rpcapi.go b/dot/rpc/modules/mocks/rpcapi.go index 6b3640f4df..5073e8b76d 100644 --- a/dot/rpc/modules/mocks/rpcapi.go +++ b/dot/rpc/modules/mocks/rpcapi.go @@ -1,21 +1,21 @@ -// Code generated by mockery v0.0.0-dev. DO NOT EDIT. +// Code generated by mockery v2.9.4. DO NOT EDIT. package mocks import mock "github.com/stretchr/testify/mock" -// MockRPCAPI is an autogenerated mock type for the RPCAPI type -type MockRPCAPI struct { +// RPCAPI is an autogenerated mock type for the RPCAPI type +type RPCAPI struct { mock.Mock } // BuildMethodNames provides a mock function with given fields: rcvr, name -func (_m *MockRPCAPI) BuildMethodNames(rcvr interface{}, name string) { +func (_m *RPCAPI) BuildMethodNames(rcvr interface{}, name string) { _m.Called(rcvr, name) } // Methods provides a mock function with given fields: -func (_m *MockRPCAPI) Methods() []string { +func (_m *RPCAPI) Methods() []string { ret := _m.Called() var r0 []string diff --git a/dot/rpc/modules/mocks/runtime_storage_api.go b/dot/rpc/modules/mocks/runtime_storage_api.go index b53e819d01..054e0c9db8 100644 --- a/dot/rpc/modules/mocks/runtime_storage_api.go +++ b/dot/rpc/modules/mocks/runtime_storage_api.go @@ -1,23 +1,25 @@ -// Code generated by mockery v2.8.0. DO NOT EDIT. +// Code generated by mockery v2.9.4. DO NOT EDIT. package mocks import mock "github.com/stretchr/testify/mock" -// MockRuntimeStorageAPI is an autogenerated mock type for the RuntimeStorageAPI type -type MockRuntimeStorageAPI struct { +// RuntimeStorageAPI is an autogenerated mock type for the RuntimeStorageAPI type +type RuntimeStorageAPI struct { mock.Mock } // GetLocal provides a mock function with given fields: k -func (_m *MockRuntimeStorageAPI) GetLocal(k []byte) ([]byte, error) { +func (_m *RuntimeStorageAPI) GetLocal(k []byte) ([]byte, error) { ret := _m.Called(k) var r0 []byte if rf, ok := ret.Get(0).(func([]byte) []byte); ok { r0 = rf(k) - } else if ret.Get(0) != nil { - r0 = ret.Get(0).([]byte) + } else { + if ret.Get(0) != nil { + r0 = ret.Get(0).([]byte) + } } var r1 error @@ -31,14 +33,16 @@ func (_m *MockRuntimeStorageAPI) GetLocal(k []byte) ([]byte, error) { } // GetPersistent provides a mock function with given fields: k -func (_m *MockRuntimeStorageAPI) GetPersistent(k []byte) ([]byte, error) { +func (_m *RuntimeStorageAPI) GetPersistent(k []byte) ([]byte, error) { ret := _m.Called(k) var r0 []byte if rf, ok := ret.Get(0).(func([]byte) []byte); ok { r0 = rf(k) - } else if ret.Get(0) != nil { - r0 = ret.Get(0).([]byte) + } else { + if ret.Get(0) != nil { + r0 = ret.Get(0).([]byte) + } } var r1 error @@ -52,7 +56,7 @@ func (_m *MockRuntimeStorageAPI) GetPersistent(k []byte) ([]byte, error) { } // SetLocal provides a mock function with given fields: k, v -func (_m *MockRuntimeStorageAPI) SetLocal(k, v []byte) error { +func (_m *RuntimeStorageAPI) SetLocal(k []byte, v []byte) error { ret := _m.Called(k, v) var r0 error @@ -66,7 +70,7 @@ func (_m *MockRuntimeStorageAPI) SetLocal(k, v []byte) error { } // SetPersistent provides a mock function with given fields: k, v -func (_m *MockRuntimeStorageAPI) SetPersistent(k, v []byte) error { +func (_m *RuntimeStorageAPI) SetPersistent(k []byte, v []byte) error { ret := _m.Called(k, v) var r0 error diff --git a/dot/rpc/modules/mocks/storage_api.go b/dot/rpc/modules/mocks/storage_api.go index d46456154d..2a33b3db88 100644 --- a/dot/rpc/modules/mocks/storage_api.go +++ b/dot/rpc/modules/mocks/storage_api.go @@ -1,4 +1,4 @@ -// Code generated by mockery v0.0.0-dev. DO NOT EDIT. +// Code generated by mockery v2.9.4. DO NOT EDIT. package mocks @@ -11,13 +11,13 @@ import ( trie "github.com/ChainSafe/gossamer/lib/trie" ) -// MockStorageAPI is an autogenerated mock type for the StorageAPI type -type MockStorageAPI struct { +// StorageAPI is an autogenerated mock type for the StorageAPI type +type StorageAPI struct { mock.Mock } // Entries provides a mock function with given fields: root -func (_m *MockStorageAPI) Entries(root *common.Hash) (map[string][]byte, error) { +func (_m *StorageAPI) Entries(root *common.Hash) (map[string][]byte, error) { ret := _m.Called(root) var r0 map[string][]byte @@ -40,7 +40,7 @@ func (_m *MockStorageAPI) Entries(root *common.Hash) (map[string][]byte, error) } // GetKeysWithPrefix provides a mock function with given fields: root, prefix -func (_m *MockStorageAPI) GetKeysWithPrefix(root *common.Hash, prefix []byte) ([][]byte, error) { +func (_m *StorageAPI) GetKeysWithPrefix(root *common.Hash, prefix []byte) ([][]byte, error) { ret := _m.Called(root, prefix) var r0 [][]byte @@ -63,7 +63,7 @@ func (_m *MockStorageAPI) GetKeysWithPrefix(root *common.Hash, prefix []byte) ([ } // GetStateRootFromBlock provides a mock function with given fields: bhash -func (_m *MockStorageAPI) GetStateRootFromBlock(bhash *common.Hash) (*common.Hash, error) { +func (_m *StorageAPI) GetStateRootFromBlock(bhash *common.Hash) (*common.Hash, error) { ret := _m.Called(bhash) var r0 *common.Hash @@ -86,7 +86,7 @@ func (_m *MockStorageAPI) GetStateRootFromBlock(bhash *common.Hash) (*common.Has } // GetStorage provides a mock function with given fields: root, key -func (_m *MockStorageAPI) GetStorage(root *common.Hash, key []byte) ([]byte, error) { +func (_m *StorageAPI) GetStorage(root *common.Hash, key []byte) ([]byte, error) { ret := _m.Called(root, key) var r0 []byte @@ -109,7 +109,7 @@ func (_m *MockStorageAPI) GetStorage(root *common.Hash, key []byte) ([]byte, err } // GetStorageByBlockHash provides a mock function with given fields: bhash, key -func (_m *MockStorageAPI) GetStorageByBlockHash(bhash *common.Hash, key []byte) ([]byte, error) { +func (_m *StorageAPI) GetStorageByBlockHash(bhash *common.Hash, key []byte) ([]byte, error) { ret := _m.Called(bhash, key) var r0 []byte @@ -132,7 +132,7 @@ func (_m *MockStorageAPI) GetStorageByBlockHash(bhash *common.Hash, key []byte) } // GetStorageChild provides a mock function with given fields: root, keyToChild -func (_m *MockStorageAPI) GetStorageChild(root *common.Hash, keyToChild []byte) (*trie.Trie, error) { +func (_m *StorageAPI) GetStorageChild(root *common.Hash, keyToChild []byte) (*trie.Trie, error) { ret := _m.Called(root, keyToChild) var r0 *trie.Trie @@ -155,7 +155,7 @@ func (_m *MockStorageAPI) GetStorageChild(root *common.Hash, keyToChild []byte) } // GetStorageFromChild provides a mock function with given fields: root, keyToChild, key -func (_m *MockStorageAPI) GetStorageFromChild(root *common.Hash, keyToChild []byte, key []byte) ([]byte, error) { +func (_m *StorageAPI) GetStorageFromChild(root *common.Hash, keyToChild []byte, key []byte) ([]byte, error) { ret := _m.Called(root, keyToChild, key) var r0 []byte @@ -178,11 +178,11 @@ func (_m *MockStorageAPI) GetStorageFromChild(root *common.Hash, keyToChild []by } // RegisterStorageObserver provides a mock function with given fields: observer -func (_m *MockStorageAPI) RegisterStorageObserver(observer state.Observer) { +func (_m *StorageAPI) RegisterStorageObserver(observer state.Observer) { _m.Called(observer) } // UnregisterStorageObserver provides a mock function with given fields: observer -func (_m *MockStorageAPI) UnregisterStorageObserver(observer state.Observer) { +func (_m *StorageAPI) UnregisterStorageObserver(observer state.Observer) { _m.Called(observer) } diff --git a/dot/rpc/modules/mocks/system_api.go b/dot/rpc/modules/mocks/system_api.go index c4983a93f4..e40825e8ec 100644 --- a/dot/rpc/modules/mocks/system_api.go +++ b/dot/rpc/modules/mocks/system_api.go @@ -1,16 +1,16 @@ -// Code generated by mockery v2.8.0. DO NOT EDIT. +// Code generated by mockery v2.9.4. DO NOT EDIT. package mocks import mock "github.com/stretchr/testify/mock" -// MockSystemAPI is an autogenerated mock type for the SystemAPI type -type MockSystemAPI struct { +// SystemAPI is an autogenerated mock type for the SystemAPI type +type SystemAPI struct { mock.Mock } // ChainName provides a mock function with given fields: -func (_m *MockSystemAPI) ChainName() string { +func (_m *SystemAPI) ChainName() string { ret := _m.Called() var r0 string @@ -24,7 +24,7 @@ func (_m *MockSystemAPI) ChainName() string { } // ChainType provides a mock function with given fields: -func (_m *MockSystemAPI) ChainType() string { +func (_m *SystemAPI) ChainType() string { ret := _m.Called() var r0 string @@ -38,7 +38,7 @@ func (_m *MockSystemAPI) ChainType() string { } // Properties provides a mock function with given fields: -func (_m *MockSystemAPI) Properties() map[string]interface{} { +func (_m *SystemAPI) Properties() map[string]interface{} { ret := _m.Called() var r0 map[string]interface{} @@ -54,7 +54,7 @@ func (_m *MockSystemAPI) Properties() map[string]interface{} { } // SystemName provides a mock function with given fields: -func (_m *MockSystemAPI) SystemName() string { +func (_m *SystemAPI) SystemName() string { ret := _m.Called() var r0 string @@ -68,7 +68,7 @@ func (_m *MockSystemAPI) SystemName() string { } // SystemVersion provides a mock function with given fields: -func (_m *MockSystemAPI) SystemVersion() string { +func (_m *SystemAPI) SystemVersion() string { ret := _m.Called() var r0 string diff --git a/dot/rpc/modules/mocks/TransactionStateAPI.go b/dot/rpc/modules/mocks/transaction_state_api.go similarity index 97% rename from dot/rpc/modules/mocks/TransactionStateAPI.go rename to dot/rpc/modules/mocks/transaction_state_api.go index b241ab8bdb..366941acd1 100644 --- a/dot/rpc/modules/mocks/TransactionStateAPI.go +++ b/dot/rpc/modules/mocks/transaction_state_api.go @@ -1,4 +1,4 @@ -// Code generated by mockery 2.9.0. DO NOT EDIT. +// Code generated by mockery v2.9.4. DO NOT EDIT. package mocks diff --git a/dot/rpc/modules/offchain_test.go b/dot/rpc/modules/offchain_test.go index 09f3535b99..4e1453c451 100644 --- a/dot/rpc/modules/offchain_test.go +++ b/dot/rpc/modules/offchain_test.go @@ -18,7 +18,7 @@ func TestOffchainStorageGet(t *testing.T) { for kind, test := range testFuncs { expectedValue := common.BytesToHex([]byte("some-value")) - st := new(mocks.MockRuntimeStorageAPI) + st := new(mocks.RuntimeStorageAPI) st.On(test, mock.AnythingOfType("[]uint8")).Return([]byte("some-value"), nil).Once() m := new(OffchainModule) @@ -71,7 +71,7 @@ func TestOffchainStorageSet(t *testing.T) { } for kind, test := range testFuncs { - st := new(mocks.MockRuntimeStorageAPI) + st := new(mocks.RuntimeStorageAPI) st.On(test, mock.AnythingOfType("[]uint8"), mock.AnythingOfType("[]uint8")).Return(nil).Once() m := new(OffchainModule) diff --git a/dot/rpc/modules/payment_test.go b/dot/rpc/modules/payment_test.go index b29c790732..a1cc69f429 100644 --- a/dot/rpc/modules/payment_test.go +++ b/dot/rpc/modules/payment_test.go @@ -31,10 +31,10 @@ func TestPaymentQueryInfo(t *testing.T) { PartialFee: scale.MaxUint128.String(), } - runtimeMock := new(mocksruntime.MockInstance) + runtimeMock := new(mocksruntime.Instance) runtimeMock.On("PaymentQueryInfo", mock.AnythingOfType("[]uint8")).Return(mockedQueryInfo, nil) - blockAPIMock := new(mocks.MockBlockAPI) + blockAPIMock := new(mocks.BlockAPI) blockAPIMock.On("BestBlockHash").Return(bestBlockHash) blockAPIMock.On("GetRuntime", mock.AnythingOfType("*common.Hash")).Return(runtimeMock, nil) @@ -60,7 +60,7 @@ func TestPaymentQueryInfo(t *testing.T) { }) t.Run("When could not get runtime", func(t *testing.T) { - blockAPIMock := new(mocks.MockBlockAPI) + blockAPIMock := new(mocks.BlockAPI) blockAPIMock.On("BestBlockHash").Return(bestBlockHash) blockAPIMock.On("GetRuntime", mock.AnythingOfType("*common.Hash")). @@ -85,10 +85,10 @@ func TestPaymentQueryInfo(t *testing.T) { }) t.Run("When PaymentQueryInfo returns error", func(t *testing.T) { - runtimeMock := new(mocksruntime.MockInstance) + runtimeMock := new(mocksruntime.Instance) runtimeMock.On("PaymentQueryInfo", mock.AnythingOfType("[]uint8")).Return(nil, errors.New("mocked error")) - blockAPIMock := new(mocks.MockBlockAPI) + blockAPIMock := new(mocks.BlockAPI) blockAPIMock.On("GetRuntime", mock.AnythingOfType("*common.Hash")).Return(runtimeMock, nil) mod := &PaymentModule{ @@ -113,10 +113,10 @@ func TestPaymentQueryInfo(t *testing.T) { }) t.Run("When PaymentQueryInfo returns a nil info", func(t *testing.T) { - runtimeMock := new(mocksruntime.MockInstance) + runtimeMock := new(mocksruntime.Instance) runtimeMock.On("PaymentQueryInfo", mock.AnythingOfType("[]uint8")).Return(nil, nil) - blockAPIMock := new(mocks.MockBlockAPI) + blockAPIMock := new(mocks.BlockAPI) blockAPIMock.On("GetRuntime", mock.AnythingOfType("*common.Hash")).Return(runtimeMock, nil) mod := &PaymentModule{ diff --git a/dot/rpc/modules/state_test.go b/dot/rpc/modules/state_test.go index cd2c15ca2b..3225c41675 100644 --- a/dot/rpc/modules/state_test.go +++ b/dot/rpc/modules/state_test.go @@ -330,7 +330,7 @@ func TestStateModule_QueryStorage(t *testing.T) { }) t.Run("When coreAPI QueryStorage returns error", func(t *testing.T) { - coreapimock := new(mocks.MockCoreAPI) + coreapimock := new(mocks.CoreAPI) coreapimock.On("QueryStorage", mock.AnythingOfType("common.Hash"), mock.AnythingOfType("common.Hash")).Return(nil, errors.New("problem while querying")) module := new(StateModule) @@ -354,7 +354,7 @@ func TestStateModule_QueryStorage(t *testing.T) { "0x90": "another value", }), } - coreapimock := new(mocks.MockCoreAPI) + coreapimock := new(mocks.CoreAPI) coreapimock.On("QueryStorage", mock.AnythingOfType("common.Hash"), mock.AnythingOfType("common.Hash"), "0x90", "0x80").Return(changes, nil) module := new(StateModule) @@ -477,7 +477,7 @@ func TestStateModule_GetKeysPaged(t *testing.T) { } func TestGetReadProof_WhenCoreAPIReturnsError(t *testing.T) { - coreAPIMock := new(mocks.MockCoreAPI) + coreAPIMock := new(mocks.CoreAPI) coreAPIMock. On("GetReadProofAt", mock.AnythingOfType("common.Hash"), mock.AnythingOfType("[][]uint8")). Return(common.EmptyHash, nil, errors.New("mocked error")) @@ -497,7 +497,7 @@ func TestGetReadProof_WhenReturnsProof(t *testing.T) { expectedBlock := common.BytesToHash([]byte("random hash")) mockedProof := [][]byte{[]byte("proof-1"), []byte("proof-2")} - coreAPIMock := new(mocks.MockCoreAPI) + coreAPIMock := new(mocks.CoreAPI) coreAPIMock. On("GetReadProofAt", mock.AnythingOfType("common.Hash"), mock.AnythingOfType("[][]uint8")). Return(expectedBlock, mockedProof, nil) diff --git a/dot/rpc/modules/system_test.go b/dot/rpc/modules/system_test.go index b7ac869b2e..2561cbc8c8 100644 --- a/dot/rpc/modules/system_test.go +++ b/dot/rpc/modules/system_test.go @@ -154,8 +154,8 @@ var testGenesisData = &genesis.Data{ ChainType: "Local", } -func newMockSystemAPI() *mocks.MockSystemAPI { - sysapimock := new(mocks.MockSystemAPI) +func newMockSystemAPI() *mocks.SystemAPI { + sysapimock := new(mocks.SystemAPI) sysapimock.On("SystemName").Return(testSystemInfo.SystemName) sysapimock.On("SystemVersion").Return(testSystemInfo.SystemVersion) sysapimock.On("ChainName").Return(testGenesisData.Name) @@ -339,7 +339,7 @@ func newCoreService(t *testing.T, srvc *state.Service) *core.Service { srvc = newTestStateService(t) } - mocknet := new(coremocks.MockNetwork) + mocknet := new(coremocks.Network) mocknet.On("GossipMessage", mock.AnythingOfType("network.NotificationsMessage")) cfg := &core.Config{ @@ -362,11 +362,11 @@ func TestSyncState(t *testing.T) { Number: big.NewInt(int64(49)), } - blockapiMock := new(mocks.MockBlockAPI) + blockapiMock := new(mocks.BlockAPI) blockapiMock.On("BestBlockHash").Return(fakeCommonHash) blockapiMock.On("GetHeader", fakeCommonHash).Return(fakeHeader, nil).Once() - netapiMock := new(mocks.MockNetworkAPI) + netapiMock := new(mocks.NetworkAPI) netapiMock.On("HighestBlock").Return(int64(90)) netapiMock.On("StartingBlock").Return(int64(10)) @@ -400,7 +400,7 @@ func TestLocalListenAddresses(t *testing.T) { Multiaddrs: []multiaddr.Multiaddr{ma}, } - mockNetAPI := new(mocks.MockNetworkAPI) + mockNetAPI := new(mocks.NetworkAPI) mockNetAPI.On("NetworkState").Return(mockedNetState).Once() res := make([]string, 0) @@ -427,7 +427,7 @@ func TestLocalPeerId(t *testing.T) { PeerID: peerID, } - mocknetAPI := new(mocks.MockNetworkAPI) + mocknetAPI := new(mocks.NetworkAPI) mocknetAPI.On("NetworkState").Return(state).Once() sysmodules := new(SystemModule) @@ -447,7 +447,7 @@ func TestLocalPeerId(t *testing.T) { func TestAddReservedPeer(t *testing.T) { t.Run("Test Add and Remove reserved peers with success", func(t *testing.T) { - networkMock := new(mocks.MockNetworkAPI) + networkMock := new(mocks.NetworkAPI) networkMock.On("AddReservedPeers", mock.AnythingOfType("string")).Return(nil).Once() networkMock.On("RemoveReservedPeers", mock.AnythingOfType("string")).Return(nil).Once() @@ -468,7 +468,7 @@ func TestAddReservedPeer(t *testing.T) { }) t.Run("Test Add and Remove reserved peers without success", func(t *testing.T) { - networkMock := new(mocks.MockNetworkAPI) + networkMock := new(mocks.NetworkAPI) networkMock.On("AddReservedPeers", mock.AnythingOfType("string")).Return(errors.New("some problems")).Once() networkMock.On("RemoveReservedPeers", mock.AnythingOfType("string")).Return(errors.New("other problems")).Once() diff --git a/dot/rpc/service_test.go b/dot/rpc/service_test.go index 4f943a7443..1a7517cc54 100644 --- a/dot/rpc/service_test.go +++ b/dot/rpc/service_test.go @@ -54,40 +54,42 @@ func TestService_Methods(t *testing.T) { require.Equal(t, qtySystemMethods+qtyRPCMethods+qtyAuthorMethods, len(m)) } -type MockService struct{} +type mockService struct{} +// MockServiceArrayRequest must be exported for ReadArray or tests will fail. type MockServiceArrayRequest struct { Key []string Bhash []common.Hash } +// MockServiceArrayResponse must be exported for ReadArray or tests will fail. type MockServiceArrayResponse struct { Key []string Bhash []common.Hash } -func (t *MockService) ReadArray(r *http.Request, req *MockServiceArrayRequest, res *MockServiceArrayResponse) error { +func (t *mockService) ReadArray(r *http.Request, req *MockServiceArrayRequest, res *MockServiceArrayResponse) error { res.Key = req.Key res.Bhash = req.Bhash return nil } -type MockResponseWriter struct { +type mockResponseWriter struct { header http.Header Status int Body string } -func NewMockResponseWriter() *MockResponseWriter { +func newMockResponseWriter() *mockResponseWriter { header := make(http.Header) - return &MockResponseWriter{header: header} + return &mockResponseWriter{header: header} } -func (w *MockResponseWriter) Header() http.Header { +func (w *mockResponseWriter) Header() http.Header { return w.header } -func (w *MockResponseWriter) Write(p []byte) (int, error) { +func (w *mockResponseWriter) Write(p []byte) (int, error) { w.Body = string(p) if w.Status == 0 { w.Status = 200 @@ -95,13 +97,13 @@ func (w *MockResponseWriter) Write(p []byte) (int, error) { return len(p), nil } -func (w *MockResponseWriter) WriteHeader(status int) { +func (w *mockResponseWriter) WriteHeader(status int) { w.Status = status } func TestJson2ReadRequest(t *testing.T) { s := rpc.NewServer() - s.RegisterService(new(MockService), "mockService") + s.RegisterService(new(mockService), "mockService") s.RegisterCodec(NewDotUpCodec(), "application/json") testCase := []struct { @@ -120,7 +122,7 @@ func TestJson2ReadRequest(t *testing.T) { req, err := http.NewRequest("POST", "", reader) require.NoError(t, err) - resWriter := NewMockResponseWriter() + resWriter := newMockResponseWriter() req.Header.Set("Content-Type", "application/json") s.ServeHTTP(resWriter, req) diff --git a/dot/rpc/subscription/listeners_test.go b/dot/rpc/subscription/listeners_test.go index 506a524b47..24991080e8 100644 --- a/dot/rpc/subscription/listeners_test.go +++ b/dot/rpc/subscription/listeners_test.go @@ -43,11 +43,11 @@ import ( "github.com/stretchr/testify/require" ) -type MockWSConnAPI struct { +type mockWSConnAPI struct { lastMessage BaseResponseJSON } -func (m *MockWSConnAPI) safeSend(msg interface{}) { +func (m *mockWSConnAPI) safeSend(msg interface{}) { m.lastMessage = msg.(BaseResponseJSON) } @@ -97,7 +97,7 @@ func TestBlockListener_Listen(t *testing.T) { wsconn, ws, cancel := setupWSConn(t) defer cancel() - BlockAPI := new(mocks.MockBlockAPI) + BlockAPI := new(mocks.BlockAPI) BlockAPI.On("FreeImportedBlockNotifierChannel", mock.AnythingOfType("chan *types.Block")) wsconn.BlockAPI = BlockAPI @@ -145,7 +145,7 @@ func TestBlockFinalizedListener_Listen(t *testing.T) { wsconn, ws, cancel := setupWSConn(t) defer cancel() - BlockAPI := new(mocks.MockBlockAPI) + BlockAPI := new(mocks.BlockAPI) BlockAPI.On("FreeFinalisedNotifierChannel", mock.AnythingOfType("chan *types.FinalisationInfo")) wsconn.BlockAPI = BlockAPI @@ -196,7 +196,7 @@ func TestExtrinsicSubmitListener_Listen(t *testing.T) { notifyImportedChan := make(chan *types.Block, 100) notifyFinalizedChan := make(chan *types.FinalisationInfo, 100) - BlockAPI := new(mocks.MockBlockAPI) + BlockAPI := new(mocks.BlockAPI) BlockAPI.On("FreeImportedBlockNotifierChannel", mock.AnythingOfType("chan *types.Block")) BlockAPI.On("FreeFinalisedNotifierChannel", mock.AnythingOfType("chan *types.FinalisationInfo")) @@ -270,7 +270,7 @@ func TestGrandpaJustification_Listen(t *testing.T) { mockedJustBytes, err := scale.Marshal(mockedJust) require.NoError(t, err) - blockStateMock := new(mocks.MockBlockAPI) + blockStateMock := new(mocks.BlockAPI) blockStateMock.On("GetJustification", mock.AnythingOfType("common.Hash")).Return(mockedJustBytes, nil) blockStateMock.On("FreeFinalisedNotifierChannel", mock.AnythingOfType("chan *types.FinalisationInfo")) wsconn.BlockAPI = blockStateMock @@ -342,7 +342,7 @@ func setupWSConn(t *testing.T) (*WSConn, *websocket.Conn, func()) { func TestRuntimeChannelListener_Listen(t *testing.T) { notifyChan := make(chan runtime.Version) - mockConnection := &MockWSConnAPI{} + mockConnection := &mockWSConnAPI{} rvl := RuntimeVersionListener{ wsconn: mockConnection, subID: 0, diff --git a/dot/rpc/subscription/websocket_test.go b/dot/rpc/subscription/websocket_test.go index e4eb23effb..5f9abfafd7 100644 --- a/dot/rpc/subscription/websocket_test.go +++ b/dot/rpc/subscription/websocket_test.go @@ -229,7 +229,7 @@ func TestWSConn_HandleComm(t *testing.T) { mockedJustBytes, err := scale.Marshal(mockedJust) require.NoError(t, err) - BlockAPI := new(mocks.MockBlockAPI) + BlockAPI := new(mocks.BlockAPI) fCh := make(chan *types.FinalisationInfo, 5) BlockAPI.On("GetFinalisedNotifierChannel").Return(fCh) @@ -282,7 +282,7 @@ func TestSubscribeAllHeads(t *testing.T) { require.NoError(t, err) require.Equal(t, []byte(`{"jsonrpc":"2.0","error":{"code":null,"message":"error BlockAPI not set"},"id":1}`+"\n"), msg) - mockBlockAPI := new(mocks.MockBlockAPI) + mockBlockAPI := new(mocks.BlockAPI) wsconn.BlockAPI = mockBlockAPI diff --git a/dot/state/mock_observer.go b/dot/state/mock_observer.go index 06c7bcabb7..0164f2cf54 100644 --- a/dot/state/mock_observer.go +++ b/dot/state/mock_observer.go @@ -1,4 +1,4 @@ -// Code generated by mockery v2.8.0. DO NOT EDIT. +// Code generated by mockery v2.9.4. DO NOT EDIT. package state diff --git a/dot/state/storage_notify.go b/dot/state/storage_notify.go index e9f3dbfbe1..805449c41a 100644 --- a/dot/state/storage_notify.go +++ b/dot/state/storage_notify.go @@ -34,6 +34,8 @@ type SubscriptionResult struct { Changes []KeyValue } +//go:generate mockery --name Observer --structname MockObserver --case underscore --inpackage + // Observer interface defines functions needed for observers, Observer Design Pattern type Observer interface { Update(result *SubscriptionResult) diff --git a/dot/sync/bootstrap_syncer_test.go b/dot/sync/bootstrap_syncer_test.go index fe2f67da0a..6c9b410c07 100644 --- a/dot/sync/bootstrap_syncer_test.go +++ b/dot/sync/bootstrap_syncer_test.go @@ -35,7 +35,7 @@ func newTestBootstrapSyncer(t *testing.T) *bootstrapSyncer { finHeader, err := types.NewHeader(common.NewHash([]byte{0}), trie.EmptyHash, trie.EmptyHash, big.NewInt(200), types.NewDigest()) require.NoError(t, err) - bs := new(syncmocks.MockBlockState) + bs := new(syncmocks.BlockState) bs.On("BestBlockHeader").Return(header, nil) bs.On("GetHighestFinalisedHeader").Return(finHeader, nil) diff --git a/dot/sync/chain_sync_test.go b/dot/sync/chain_sync_test.go index ef215e69b6..f4e4ab0dd1 100644 --- a/dot/sync/chain_sync_test.go +++ b/dot/sync/chain_sync_test.go @@ -46,12 +46,12 @@ func newTestChainSync(t *testing.T) (*chainSync, *blockQueue) { header, err := types.NewHeader(common.NewHash([]byte{0}), trie.EmptyHash, trie.EmptyHash, big.NewInt(0), types.NewDigest()) require.NoError(t, err) - bs := new(syncmocks.MockBlockState) + bs := new(syncmocks.BlockState) bs.On("BestBlockHeader").Return(header, nil) bs.On("GetFinalisedNotifierChannel").Return(make(chan *types.FinalisationInfo, 128), nil) bs.On("HasHeader", mock.AnythingOfType("common.Hash")).Return(true, nil) - net := new(syncmocks.MockNetwork) + net := new(syncmocks.Network) net.On("DoBlockRequest", mock.AnythingOfType("peer.ID"), mock.AnythingOfType("*network.BlockRequestMessage")).Return(nil, nil) readyBlocks := newBlockQueue(maxResponseSize) @@ -78,14 +78,14 @@ func TestChainSync_SetPeerHead(t *testing.T) { require.True(t, cs.pendingBlocks.hasBlock(hash)) // test case where peer has a lower head than us, but they are on the same chain as us - cs.blockState = new(syncmocks.MockBlockState) + cs.blockState = new(syncmocks.BlockState) header, err := types.NewHeader(common.NewHash([]byte{0}), trie.EmptyHash, trie.EmptyHash, big.NewInt(1000), types.NewDigest()) require.NoError(t, err) - cs.blockState.(*syncmocks.MockBlockState).On("BestBlockHeader").Return(header, nil) + cs.blockState.(*syncmocks.BlockState).On("BestBlockHeader").Return(header, nil) fin, err := types.NewHeader(common.NewHash([]byte{0}), trie.EmptyHash, trie.EmptyHash, big.NewInt(998), types.NewDigest()) require.NoError(t, err) - cs.blockState.(*syncmocks.MockBlockState).On("GetHighestFinalisedHeader").Return(fin, nil) - cs.blockState.(*syncmocks.MockBlockState).On("GetHashByNumber", mock.AnythingOfType("*big.Int")).Return(hash, nil) + cs.blockState.(*syncmocks.BlockState).On("GetHighestFinalisedHeader").Return(fin, nil) + cs.blockState.(*syncmocks.BlockState).On("GetHashByNumber", mock.AnythingOfType("*big.Int")).Return(hash, nil) number = big.NewInt(999) err = cs.setPeerHead(testPeer, hash, number) @@ -103,12 +103,12 @@ func TestChainSync_SetPeerHead(t *testing.T) { } // test case where peer has a lower head than us, and they are on an invalid fork - cs.blockState = new(syncmocks.MockBlockState) - cs.blockState.(*syncmocks.MockBlockState).On("BestBlockHeader").Return(header, nil) + cs.blockState = new(syncmocks.BlockState) + cs.blockState.(*syncmocks.BlockState).On("BestBlockHeader").Return(header, nil) fin, err = types.NewHeader(common.NewHash([]byte{0}), trie.EmptyHash, trie.EmptyHash, big.NewInt(1000), types.NewDigest()) require.NoError(t, err) - cs.blockState.(*syncmocks.MockBlockState).On("GetHighestFinalisedHeader").Return(fin, nil) - cs.blockState.(*syncmocks.MockBlockState).On("GetHashByNumber", mock.AnythingOfType("*big.Int")).Return(common.Hash{}, nil) + cs.blockState.(*syncmocks.BlockState).On("GetHighestFinalisedHeader").Return(fin, nil) + cs.blockState.(*syncmocks.BlockState).On("GetHashByNumber", mock.AnythingOfType("*big.Int")).Return(common.Hash{}, nil) number = big.NewInt(999) err = cs.setPeerHead(testPeer, hash, number) @@ -126,13 +126,13 @@ func TestChainSync_SetPeerHead(t *testing.T) { } // test case where peer has a lower head than us, but they are on a valid fork (that is not our chain) - cs.blockState = new(syncmocks.MockBlockState) - cs.blockState.(*syncmocks.MockBlockState).On("BestBlockHeader").Return(header, nil) + cs.blockState = new(syncmocks.BlockState) + cs.blockState.(*syncmocks.BlockState).On("BestBlockHeader").Return(header, nil) fin, err = types.NewHeader(common.NewHash([]byte{0}), trie.EmptyHash, trie.EmptyHash, big.NewInt(998), types.NewDigest()) require.NoError(t, err) - cs.blockState.(*syncmocks.MockBlockState).On("GetHighestFinalisedHeader").Return(fin, nil) - cs.blockState.(*syncmocks.MockBlockState).On("GetHashByNumber", mock.AnythingOfType("*big.Int")).Return(common.Hash{}, nil) - cs.blockState.(*syncmocks.MockBlockState).On("HasHeader", mock.AnythingOfType("common.Hash")).Return(true, nil) + cs.blockState.(*syncmocks.BlockState).On("GetHighestFinalisedHeader").Return(fin, nil) + cs.blockState.(*syncmocks.BlockState).On("GetHashByNumber", mock.AnythingOfType("*big.Int")).Return(common.Hash{}, nil) + cs.blockState.(*syncmocks.BlockState).On("HasHeader", mock.AnythingOfType("common.Hash")).Return(true, nil) number = big.NewInt(999) err = cs.setPeerHead(testPeer, hash, number) @@ -179,11 +179,11 @@ func TestChainSync_sync_bootstrap_withWorkerError(t *testing.T) { func TestChainSync_sync_tip(t *testing.T) { cs, _ := newTestChainSync(t) - cs.blockState = new(syncmocks.MockBlockState) + cs.blockState = new(syncmocks.BlockState) header, err := types.NewHeader(common.NewHash([]byte{0}), trie.EmptyHash, trie.EmptyHash, big.NewInt(1000), types.NewDigest()) require.NoError(t, err) - cs.blockState.(*syncmocks.MockBlockState).On("BestBlockHeader").Return(header, nil) - cs.blockState.(*syncmocks.MockBlockState).On("GetHighestFinalisedHeader").Return(header, nil) + cs.blockState.(*syncmocks.BlockState).On("BestBlockHeader").Return(header, nil) + cs.blockState.(*syncmocks.BlockState).On("GetHighestFinalisedHeader").Return(header, nil) go cs.sync() defer cs.cancel() @@ -551,7 +551,7 @@ func TestChainSync_validateResponse(t *testing.T) { func TestChainSync_validateResponse_firstBlock(t *testing.T) { cs, _ := newTestChainSync(t) - bs := new(syncmocks.MockBlockState) + bs := new(syncmocks.BlockState) bs.On("HasHeader", mock.AnythingOfType("common.Hash")).Return(false, nil) cs.blockState = bs @@ -620,8 +620,8 @@ func TestChainSync_doSync(t *testing.T) { }, } - cs.network = new(syncmocks.MockNetwork) - cs.network.(*syncmocks.MockNetwork).On("DoBlockRequest", mock.AnythingOfType("peer.ID"), mock.AnythingOfType("*network.BlockRequestMessage")).Return(resp, nil) + cs.network = new(syncmocks.Network) + cs.network.(*syncmocks.Network).On("DoBlockRequest", mock.AnythingOfType("peer.ID"), mock.AnythingOfType("*network.BlockRequestMessage")).Return(resp, nil) workerErr = cs.doSync(req) require.Nil(t, workerErr) @@ -652,8 +652,8 @@ func TestChainSync_doSync(t *testing.T) { // test to see if descending blocks get reversed req.Direction = network.Descending - cs.network = new(syncmocks.MockNetwork) - cs.network.(*syncmocks.MockNetwork).On("DoBlockRequest", mock.AnythingOfType("peer.ID"), mock.AnythingOfType("*network.BlockRequestMessage")).Return(resp, nil) + cs.network = new(syncmocks.Network) + cs.network.(*syncmocks.Network).On("DoBlockRequest", mock.AnythingOfType("peer.ID"), mock.AnythingOfType("*network.BlockRequestMessage")).Return(resp, nil) workerErr = cs.doSync(req) require.Nil(t, workerErr) diff --git a/dot/sync/interface.go b/dot/sync/interface.go index 92f3883501..2ba8c5b027 100644 --- a/dot/sync/interface.go +++ b/dot/sync/interface.go @@ -29,6 +29,8 @@ import ( "github.com/libp2p/go-libp2p-core/peer" ) +//go:generate mockery --name BlockState --structname BlockState --case underscore --keeptree + // BlockState is the interface for the block state type BlockState interface { BestBlockHash() common.Hash @@ -76,21 +78,29 @@ type TransactionState interface { RemoveExtrinsic(ext types.Extrinsic) } +//go:generate mockery --name BabeVerifier --structname BabeVerifier --case underscore --keeptree + // BabeVerifier deals with BABE block verification type BabeVerifier interface { VerifyBlock(header *types.Header) error } +//go:generate mockery --name FinalityGadget --structname FinalityGadget --case underscore --keeptree + // FinalityGadget implements justification verification functionality type FinalityGadget interface { VerifyBlockJustification(common.Hash, []byte) error } +//go:generate mockery --name BlockImportHandler --structname BlockImportHandler --case underscore --keeptree + // BlockImportHandler is the interface for the handler of newly imported blocks type BlockImportHandler interface { HandleBlockImport(block *types.Block, state *rtstorage.TrieState) error } +//go:generate mockery --name Network --structname Network --case underscore --keeptree + // Network is the interface for the network type Network interface { // DoBlockRequest sends a request to the given peer. If a response is received within a certain time period, it is returned, otherwise an error is returned. diff --git a/dot/sync/mocks/Network.go b/dot/sync/mocks/Network.go index f0b634d7b0..85560e977a 100644 --- a/dot/sync/mocks/Network.go +++ b/dot/sync/mocks/Network.go @@ -1,6 +1,6 @@ -// Code generated by mockery v2.8.0. DO NOT EDIT. +// Code generated by mockery v2.9.4. DO NOT EDIT. -package sync +package mocks import ( common "github.com/ChainSafe/gossamer/lib/common" @@ -11,13 +11,13 @@ import ( peer "github.com/libp2p/go-libp2p-core/peer" ) -// MockNetwork is an autogenerated mock type for the Network type -type MockNetwork struct { +// Network is an autogenerated mock type for the Network type +type Network struct { mock.Mock } // DoBlockRequest provides a mock function with given fields: to, req -func (_m *MockNetwork) DoBlockRequest(to peer.ID, req *network.BlockRequestMessage) (*network.BlockResponseMessage, error) { +func (_m *Network) DoBlockRequest(to peer.ID, req *network.BlockRequestMessage) (*network.BlockResponseMessage, error) { ret := _m.Called(to, req) var r0 *network.BlockResponseMessage @@ -40,7 +40,7 @@ func (_m *MockNetwork) DoBlockRequest(to peer.ID, req *network.BlockRequestMessa } // Peers provides a mock function with given fields: -func (_m *MockNetwork) Peers() []common.PeerInfo { +func (_m *Network) Peers() []common.PeerInfo { ret := _m.Called() var r0 []common.PeerInfo diff --git a/dot/sync/mocks/verifier.go b/dot/sync/mocks/babe_verifier.go similarity index 60% rename from dot/sync/mocks/verifier.go rename to dot/sync/mocks/babe_verifier.go index 3dac9db92f..34477841ce 100644 --- a/dot/sync/mocks/verifier.go +++ b/dot/sync/mocks/babe_verifier.go @@ -1,6 +1,6 @@ -// Code generated by mockery v2.8.0. DO NOT EDIT. +// Code generated by mockery v2.9.4. DO NOT EDIT. -package sync +package mocks import ( mock "github.com/stretchr/testify/mock" @@ -8,13 +8,13 @@ import ( types "github.com/ChainSafe/gossamer/dot/types" ) -// MockVerifier is an autogenerated mock type for the Verifier type -type MockVerifier struct { +// BabeVerifier is an autogenerated mock type for the BabeVerifier type +type BabeVerifier struct { mock.Mock } // VerifyBlock provides a mock function with given fields: header -func (_m *MockVerifier) VerifyBlock(header *types.Header) error { +func (_m *BabeVerifier) VerifyBlock(header *types.Header) error { ret := _m.Called(header) var r0 error diff --git a/dot/sync/mocks/block_import_handler.go b/dot/sync/mocks/block_import_handler.go index 8da0026722..342128f355 100644 --- a/dot/sync/mocks/block_import_handler.go +++ b/dot/sync/mocks/block_import_handler.go @@ -1,6 +1,6 @@ -// Code generated by mockery v2.8.0. DO NOT EDIT. +// Code generated by mockery v2.9.4. DO NOT EDIT. -package sync +package mocks import ( storage "github.com/ChainSafe/gossamer/lib/runtime/storage" @@ -9,13 +9,13 @@ import ( types "github.com/ChainSafe/gossamer/dot/types" ) -// MockBlockImportHandler is an autogenerated mock type for the BlockImportHandler type -type MockBlockImportHandler struct { +// BlockImportHandler is an autogenerated mock type for the BlockImportHandler type +type BlockImportHandler struct { mock.Mock } // HandleBlockImport provides a mock function with given fields: block, state -func (_m *MockBlockImportHandler) HandleBlockImport(block *types.Block, state *storage.TrieState) error { +func (_m *BlockImportHandler) HandleBlockImport(block *types.Block, state *storage.TrieState) error { ret := _m.Called(block, state) var r0 error diff --git a/dot/sync/mocks/BlockState.go b/dot/sync/mocks/block_state.go similarity index 81% rename from dot/sync/mocks/BlockState.go rename to dot/sync/mocks/block_state.go index c4c7d4863b..31c7dce2aa 100644 --- a/dot/sync/mocks/BlockState.go +++ b/dot/sync/mocks/block_state.go @@ -1,6 +1,6 @@ // Code generated by mockery v2.9.4. DO NOT EDIT. -package sync +package mocks import ( big "math/big" @@ -13,13 +13,13 @@ import ( types "github.com/ChainSafe/gossamer/dot/types" ) -// MockBlockState is an autogenerated mock type for the BlockState type -type MockBlockState struct { +// BlockState is an autogenerated mock type for the BlockState type +type BlockState struct { mock.Mock } // AddBlock provides a mock function with given fields: _a0 -func (_m *MockBlockState) AddBlock(_a0 *types.Block) error { +func (_m *BlockState) AddBlock(_a0 *types.Block) error { ret := _m.Called(_a0) var r0 error @@ -33,7 +33,7 @@ func (_m *MockBlockState) AddBlock(_a0 *types.Block) error { } // AddBlockToBlockTree provides a mock function with given fields: header -func (_m *MockBlockState) AddBlockToBlockTree(header *types.Header) error { +func (_m *BlockState) AddBlockToBlockTree(header *types.Header) error { ret := _m.Called(header) var r0 error @@ -47,7 +47,7 @@ func (_m *MockBlockState) AddBlockToBlockTree(header *types.Header) error { } // BestBlockHash provides a mock function with given fields: -func (_m *MockBlockState) BestBlockHash() common.Hash { +func (_m *BlockState) BestBlockHash() common.Hash { ret := _m.Called() var r0 common.Hash @@ -63,7 +63,7 @@ func (_m *MockBlockState) BestBlockHash() common.Hash { } // BestBlockHeader provides a mock function with given fields: -func (_m *MockBlockState) BestBlockHeader() (*types.Header, error) { +func (_m *BlockState) BestBlockHeader() (*types.Header, error) { ret := _m.Called() var r0 *types.Header @@ -86,7 +86,7 @@ func (_m *MockBlockState) BestBlockHeader() (*types.Header, error) { } // BestBlockNumber provides a mock function with given fields: -func (_m *MockBlockState) BestBlockNumber() (*big.Int, error) { +func (_m *BlockState) BestBlockNumber() (*big.Int, error) { ret := _m.Called() var r0 *big.Int @@ -109,7 +109,7 @@ func (_m *MockBlockState) BestBlockNumber() (*big.Int, error) { } // CompareAndSetBlockData provides a mock function with given fields: bd -func (_m *MockBlockState) CompareAndSetBlockData(bd *types.BlockData) error { +func (_m *BlockState) CompareAndSetBlockData(bd *types.BlockData) error { ret := _m.Called(bd) var r0 error @@ -123,7 +123,7 @@ func (_m *MockBlockState) CompareAndSetBlockData(bd *types.BlockData) error { } // GetBlockBody provides a mock function with given fields: _a0 -func (_m *MockBlockState) GetBlockBody(_a0 common.Hash) (*types.Body, error) { +func (_m *BlockState) GetBlockBody(_a0 common.Hash) (*types.Body, error) { ret := _m.Called(_a0) var r0 *types.Body @@ -146,7 +146,7 @@ func (_m *MockBlockState) GetBlockBody(_a0 common.Hash) (*types.Body, error) { } // GetBlockByHash provides a mock function with given fields: _a0 -func (_m *MockBlockState) GetBlockByHash(_a0 common.Hash) (*types.Block, error) { +func (_m *BlockState) GetBlockByHash(_a0 common.Hash) (*types.Block, error) { ret := _m.Called(_a0) var r0 *types.Block @@ -169,7 +169,7 @@ func (_m *MockBlockState) GetBlockByHash(_a0 common.Hash) (*types.Block, error) } // GetBlockByNumber provides a mock function with given fields: _a0 -func (_m *MockBlockState) GetBlockByNumber(_a0 *big.Int) (*types.Block, error) { +func (_m *BlockState) GetBlockByNumber(_a0 *big.Int) (*types.Block, error) { ret := _m.Called(_a0) var r0 *types.Block @@ -192,7 +192,7 @@ func (_m *MockBlockState) GetBlockByNumber(_a0 *big.Int) (*types.Block, error) { } // GetFinalisedNotifierChannel provides a mock function with given fields: -func (_m *MockBlockState) GetFinalisedNotifierChannel() chan *types.FinalisationInfo { +func (_m *BlockState) GetFinalisedNotifierChannel() chan *types.FinalisationInfo { ret := _m.Called() var r0 chan *types.FinalisationInfo @@ -208,7 +208,7 @@ func (_m *MockBlockState) GetFinalisedNotifierChannel() chan *types.Finalisation } // GetHashByNumber provides a mock function with given fields: _a0 -func (_m *MockBlockState) GetHashByNumber(_a0 *big.Int) (common.Hash, error) { +func (_m *BlockState) GetHashByNumber(_a0 *big.Int) (common.Hash, error) { ret := _m.Called(_a0) var r0 common.Hash @@ -231,7 +231,7 @@ func (_m *MockBlockState) GetHashByNumber(_a0 *big.Int) (common.Hash, error) { } // GetHeader provides a mock function with given fields: _a0 -func (_m *MockBlockState) GetHeader(_a0 common.Hash) (*types.Header, error) { +func (_m *BlockState) GetHeader(_a0 common.Hash) (*types.Header, error) { ret := _m.Called(_a0) var r0 *types.Header @@ -254,7 +254,7 @@ func (_m *MockBlockState) GetHeader(_a0 common.Hash) (*types.Header, error) { } // GetHighestFinalisedHeader provides a mock function with given fields: -func (_m *MockBlockState) GetHighestFinalisedHeader() (*types.Header, error) { +func (_m *BlockState) GetHighestFinalisedHeader() (*types.Header, error) { ret := _m.Called() var r0 *types.Header @@ -277,7 +277,7 @@ func (_m *MockBlockState) GetHighestFinalisedHeader() (*types.Header, error) { } // GetJustification provides a mock function with given fields: _a0 -func (_m *MockBlockState) GetJustification(_a0 common.Hash) ([]byte, error) { +func (_m *BlockState) GetJustification(_a0 common.Hash) ([]byte, error) { ret := _m.Called(_a0) var r0 []byte @@ -300,7 +300,7 @@ func (_m *MockBlockState) GetJustification(_a0 common.Hash) ([]byte, error) { } // GetMessageQueue provides a mock function with given fields: _a0 -func (_m *MockBlockState) GetMessageQueue(_a0 common.Hash) ([]byte, error) { +func (_m *BlockState) GetMessageQueue(_a0 common.Hash) ([]byte, error) { ret := _m.Called(_a0) var r0 []byte @@ -323,7 +323,7 @@ func (_m *MockBlockState) GetMessageQueue(_a0 common.Hash) ([]byte, error) { } // GetReceipt provides a mock function with given fields: _a0 -func (_m *MockBlockState) GetReceipt(_a0 common.Hash) ([]byte, error) { +func (_m *BlockState) GetReceipt(_a0 common.Hash) ([]byte, error) { ret := _m.Called(_a0) var r0 []byte @@ -346,7 +346,7 @@ func (_m *MockBlockState) GetReceipt(_a0 common.Hash) ([]byte, error) { } // GetRuntime provides a mock function with given fields: _a0 -func (_m *MockBlockState) GetRuntime(_a0 *common.Hash) (runtime.Instance, error) { +func (_m *BlockState) GetRuntime(_a0 *common.Hash) (runtime.Instance, error) { ret := _m.Called(_a0) var r0 runtime.Instance @@ -369,7 +369,7 @@ func (_m *MockBlockState) GetRuntime(_a0 *common.Hash) (runtime.Instance, error) } // HasBlockBody provides a mock function with given fields: hash -func (_m *MockBlockState) HasBlockBody(hash common.Hash) (bool, error) { +func (_m *BlockState) HasBlockBody(hash common.Hash) (bool, error) { ret := _m.Called(hash) var r0 bool @@ -390,7 +390,7 @@ func (_m *MockBlockState) HasBlockBody(hash common.Hash) (bool, error) { } // HasHeader provides a mock function with given fields: hash -func (_m *MockBlockState) HasHeader(hash common.Hash) (bool, error) { +func (_m *BlockState) HasHeader(hash common.Hash) (bool, error) { ret := _m.Called(hash) var r0 bool @@ -411,7 +411,7 @@ func (_m *MockBlockState) HasHeader(hash common.Hash) (bool, error) { } // SetFinalisedHash provides a mock function with given fields: hash, round, setID -func (_m *MockBlockState) SetFinalisedHash(hash common.Hash, round uint64, setID uint64) error { +func (_m *BlockState) SetFinalisedHash(hash common.Hash, round uint64, setID uint64) error { ret := _m.Called(hash, round, setID) var r0 error @@ -425,7 +425,7 @@ func (_m *MockBlockState) SetFinalisedHash(hash common.Hash, round uint64, setID } // SetHeader provides a mock function with given fields: _a0 -func (_m *MockBlockState) SetHeader(_a0 *types.Header) error { +func (_m *BlockState) SetHeader(_a0 *types.Header) error { ret := _m.Called(_a0) var r0 error @@ -439,7 +439,7 @@ func (_m *MockBlockState) SetHeader(_a0 *types.Header) error { } // SetJustification provides a mock function with given fields: hash, data -func (_m *MockBlockState) SetJustification(hash common.Hash, data []byte) error { +func (_m *BlockState) SetJustification(hash common.Hash, data []byte) error { ret := _m.Called(hash, data) var r0 error @@ -453,12 +453,12 @@ func (_m *MockBlockState) SetJustification(hash common.Hash, data []byte) error } // StoreRuntime provides a mock function with given fields: _a0, _a1 -func (_m *MockBlockState) StoreRuntime(_a0 common.Hash, _a1 runtime.Instance) { +func (_m *BlockState) StoreRuntime(_a0 common.Hash, _a1 runtime.Instance) { _m.Called(_a0, _a1) } // SubChain provides a mock function with given fields: start, end -func (_m *MockBlockState) SubChain(start common.Hash, end common.Hash) ([]common.Hash, error) { +func (_m *BlockState) SubChain(start common.Hash, end common.Hash) ([]common.Hash, error) { ret := _m.Called(start, end) var r0 []common.Hash diff --git a/dot/sync/mocks/FinalityGadget.go b/dot/sync/mocks/finality_gadget.go similarity index 89% rename from dot/sync/mocks/FinalityGadget.go rename to dot/sync/mocks/finality_gadget.go index 6c79912932..752753ebf2 100644 --- a/dot/sync/mocks/FinalityGadget.go +++ b/dot/sync/mocks/finality_gadget.go @@ -1,6 +1,6 @@ -// Code generated by mockery v2.8.0. DO NOT EDIT. +// Code generated by mockery v2.9.4. DO NOT EDIT. -package sync +package mocks import ( common "github.com/ChainSafe/gossamer/lib/common" diff --git a/dot/sync/syncer_test.go b/dot/sync/syncer_test.go index 2381ff3b48..0136343a5e 100644 --- a/dot/sync/syncer_test.go +++ b/dot/sync/syncer_test.go @@ -38,7 +38,7 @@ import ( log "github.com/ChainSafe/log15" "github.com/stretchr/testify/require" - syncmocks "github.com/ChainSafe/gossamer/dot/sync/mocks" + "github.com/ChainSafe/gossamer/dot/sync/mocks" ) func TestMain(m *testing.M) { @@ -55,21 +55,21 @@ func TestMain(m *testing.M) { os.Exit(code) } -func newMockFinalityGadget() *syncmocks.FinalityGadget { - m := new(syncmocks.FinalityGadget) +func newMockFinalityGadget() *mocks.FinalityGadget { + m := new(mocks.FinalityGadget) // using []uint8 instead of []byte: https://github.com/stretchr/testify/pull/969 m.On("VerifyBlockJustification", mock.AnythingOfType("common.Hash"), mock.AnythingOfType("[]uint8")).Return(nil) return m } -func newMockVerifier() *syncmocks.MockVerifier { - m := new(syncmocks.MockVerifier) +func newMockBabeVerifier() *mocks.BabeVerifier { + m := new(mocks.BabeVerifier) m.On("VerifyBlock", mock.AnythingOfType("*types.Header")).Return(nil) return m } -func newMockNetwork() *syncmocks.MockNetwork { - m := new(syncmocks.MockNetwork) +func newMockNetwork() *mocks.Network { + m := new(mocks.Network) m.On("DoBlockRequest", mock.AnythingOfType("peer.ID"), mock.AnythingOfType("*network.BlockRequestMessage")).Return(nil, nil) return m } @@ -126,8 +126,8 @@ func newTestSyncer(t *testing.T) *Service { cfg.BlockState.StoreRuntime(cfg.BlockState.BestBlockHash(), instance) - cfg.BlockImportHandler = new(syncmocks.MockBlockImportHandler) - cfg.BlockImportHandler.(*syncmocks.MockBlockImportHandler).On("HandleBlockImport", mock.AnythingOfType("*types.Block"), mock.AnythingOfType("*storage.TrieState")).Return(func(block *types.Block, ts *rtstorage.TrieState) error { + cfg.BlockImportHandler = new(mocks.BlockImportHandler) + cfg.BlockImportHandler.(*mocks.BlockImportHandler).On("HandleBlockImport", mock.AnythingOfType("*types.Block"), mock.AnythingOfType("*storage.TrieState")).Return(func(block *types.Block, ts *rtstorage.TrieState) error { // store updates state trie nodes in database if err = stateSrvc.Storage.StoreTrie(ts, &block.Header); err != nil { logger.Warn("failed to store state trie for imported block", "block", block.Header.Hash(), "error", err) @@ -144,7 +144,7 @@ func newTestSyncer(t *testing.T) *Service { }) cfg.TransactionState = stateSrvc.Transaction - cfg.BabeVerifier = newMockVerifier() + cfg.BabeVerifier = newMockBabeVerifier() cfg.LogLvl = log.LvlTrace cfg.FinalityGadget = newMockFinalityGadget() cfg.Network = newMockNetwork() diff --git a/dot/sync/tip_syncer_test.go b/dot/sync/tip_syncer_test.go index ebef24465d..4d3b7c7fc3 100644 --- a/dot/sync/tip_syncer_test.go +++ b/dot/sync/tip_syncer_test.go @@ -34,7 +34,7 @@ func newTestTipSyncer(t *testing.T) *tipSyncer { finHeader, err := types.NewHeader(common.NewHash([]byte{0}), trie.EmptyHash, trie.EmptyHash, big.NewInt(200), types.NewDigest()) require.NoError(t, err) - bs := new(syncmocks.MockBlockState) + bs := new(syncmocks.BlockState) bs.On("GetHighestFinalisedHeader").Return(finHeader, nil) bs.On("HasHeader", mock.AnythingOfType("common.Hash")).Return(true, nil) @@ -239,7 +239,7 @@ func TestTipSyncer_handleTick_case3(t *testing.T) { require.Equal(t, block.ToBlockData(), s.readyBlocks.pop()) // add pending block w/ full block, but block is not ready as parent is unknown - bs := new(syncmocks.MockBlockState) + bs := new(syncmocks.BlockState) bs.On("GetHighestFinalisedHeader").Return(fin, nil) bs.On("HasHeader", mock.AnythingOfType("common.Hash")).Return(false, nil) s.blockState = bs diff --git a/lib/babe/mocks/BlockImportHandler.go b/lib/babe/mocks/block_import_handler.go similarity index 93% rename from lib/babe/mocks/BlockImportHandler.go rename to lib/babe/mocks/block_import_handler.go index 4e2bc5f5f2..fcb01e9a0e 100644 --- a/lib/babe/mocks/BlockImportHandler.go +++ b/lib/babe/mocks/block_import_handler.go @@ -1,4 +1,4 @@ -// Code generated by mockery v2.8.0. DO NOT EDIT. +// Code generated by mockery v2.9.4. DO NOT EDIT. package mocks diff --git a/lib/babe/state.go b/lib/babe/state.go index 386f5bc758..4bbc3ad452 100644 --- a/lib/babe/state.go +++ b/lib/babe/state.go @@ -95,6 +95,8 @@ type DigestHandler interface { HandleDigests(*types.Header) } +//go:generate mockery --name BlockImportHandler --structname BlockImportHandler --case underscore --keeptree + // BlockImportHandler is the interface for the handler of new blocks type BlockImportHandler interface { HandleBlockProduced(block *types.Block, state *rtstorage.TrieState) error diff --git a/lib/grandpa/grandpa_test.go b/lib/grandpa/grandpa_test.go index 6502fed404..307a4b6d8b 100644 --- a/lib/grandpa/grandpa_test.go +++ b/lib/grandpa/grandpa_test.go @@ -39,7 +39,7 @@ import ( "github.com/stretchr/testify/require" - . "github.com/ChainSafe/gossamer/lib/grandpa/mocks" + "github.com/ChainSafe/gossamer/lib/grandpa/mocks" ethmetrics "github.com/ethereum/go-ethereum/metrics" ) @@ -56,8 +56,8 @@ var ( voters = newTestVoters() ) -func NewMockDigestHandler() *MockDigestHandler { - m := new(MockDigestHandler) +func NewMockDigestHandler() *mocks.DigestHandler { + m := new(mocks.DigestHandler) m.On("NextGrandpaAuthorityChange").Return(uint64(2 ^ 64 - 1)) return m } diff --git a/lib/grandpa/mocks/digest_handler.go b/lib/grandpa/mocks/digest_handler.go index 7665858eae..5c1f0a265b 100644 --- a/lib/grandpa/mocks/digest_handler.go +++ b/lib/grandpa/mocks/digest_handler.go @@ -1,16 +1,16 @@ -// Code generated by mockery v2.8.0. DO NOT EDIT. +// Code generated by mockery v2.9.4. DO NOT EDIT. -package grandpa +package mocks import mock "github.com/stretchr/testify/mock" -// MockDigestHandler is an autogenerated mock type for the DigestHandler type -type MockDigestHandler struct { +// DigestHandler is an autogenerated mock type for the DigestHandler type +type DigestHandler struct { mock.Mock } // NextGrandpaAuthorityChange provides a mock function with given fields: -func (_m *MockDigestHandler) NextGrandpaAuthorityChange() uint64 { +func (_m *DigestHandler) NextGrandpaAuthorityChange() uint64 { ret := _m.Called() var r0 uint64 diff --git a/lib/grandpa/mocks/network.go b/lib/grandpa/mocks/network.go index 760cec4b47..9ca106574c 100644 --- a/lib/grandpa/mocks/network.go +++ b/lib/grandpa/mocks/network.go @@ -1,6 +1,6 @@ -// Code generated by mockery v2.8.0. DO NOT EDIT. +// Code generated by mockery v2.9.4. DO NOT EDIT. -package grandpa +package mocks import ( network "github.com/ChainSafe/gossamer/dot/network" @@ -11,18 +11,23 @@ import ( protocol "github.com/libp2p/go-libp2p-core/protocol" ) -// MockNetwork is an autogenerated mock type for the Network type -type MockNetwork struct { +// Network is an autogenerated mock type for the Network type +type Network struct { mock.Mock } -// RegisterNotificationsProtocol provides a mock function with given fields: sub, messageID, handshakeGetter, handshakeDecoder, handshakeValidator, messageDecoder, messageHandler, overwriteProtocol -func (_m *MockNetwork) RegisterNotificationsProtocol(sub protocol.ID, messageID byte, handshakeGetter func() (network.Handshake, error), handshakeDecoder func([]byte) (network.Handshake, error), handshakeValidator func(peer.ID, network.Handshake) error, messageDecoder func([]byte) (network.NotificationsMessage, error), messageHandler func(peer.ID, network.NotificationsMessage) (bool, error), overwriteProtocol bool) error { - ret := _m.Called(sub, messageID, handshakeGetter, handshakeDecoder, handshakeValidator, messageDecoder, messageHandler, overwriteProtocol) +// GossipMessage provides a mock function with given fields: msg +func (_m *Network) GossipMessage(msg network.NotificationsMessage) { + _m.Called(msg) +} + +// RegisterNotificationsProtocol provides a mock function with given fields: sub, messageID, handshakeGetter, handshakeDecoder, handshakeValidator, messageDecoder, messageHandler, batchHandler +func (_m *Network) RegisterNotificationsProtocol(sub protocol.ID, messageID byte, handshakeGetter func() (network.Handshake, error), handshakeDecoder func([]byte) (network.Handshake, error), handshakeValidator func(peer.ID, network.Handshake) error, messageDecoder func([]byte) (network.NotificationsMessage, error), messageHandler func(peer.ID, network.NotificationsMessage) (bool, error), batchHandler func(peer.ID, network.NotificationsMessage) ([]*network.BatchMessage, error)) error { + ret := _m.Called(sub, messageID, handshakeGetter, handshakeDecoder, handshakeValidator, messageDecoder, messageHandler, batchHandler) var r0 error - if rf, ok := ret.Get(0).(func(protocol.ID, byte, func() (network.Handshake, error), func([]byte) (network.Handshake, error), func(peer.ID, network.Handshake) error, func([]byte) (network.NotificationsMessage, error), func(peer.ID, network.NotificationsMessage) (bool, error), bool) error); ok { - r0 = rf(sub, messageID, handshakeGetter, handshakeDecoder, handshakeValidator, messageDecoder, messageHandler, overwriteProtocol) + if rf, ok := ret.Get(0).(func(protocol.ID, byte, func() (network.Handshake, error), func([]byte) (network.Handshake, error), func(peer.ID, network.Handshake) error, func([]byte) (network.NotificationsMessage, error), func(peer.ID, network.NotificationsMessage) (bool, error), func(peer.ID, network.NotificationsMessage) ([]*network.BatchMessage, error)) error); ok { + r0 = rf(sub, messageID, handshakeGetter, handshakeDecoder, handshakeValidator, messageDecoder, messageHandler, batchHandler) } else { r0 = ret.Error(0) } @@ -30,12 +35,16 @@ func (_m *MockNetwork) RegisterNotificationsProtocol(sub protocol.ID, messageID return r0 } -// SendJustificationRequest provides a mock function with given fields: to, num -func (_m *MockNetwork) SendJustificationRequest(to peer.ID, num uint32) { - _m.Called(to, num) -} +// SendMessage provides a mock function with given fields: to, msg +func (_m *Network) SendMessage(to peer.ID, msg network.NotificationsMessage) error { + ret := _m.Called(to, msg) -// SendMessage provides a mock function with given fields: msg -func (_m *MockNetwork) SendMessage(msg network.NotificationsMessage) { - _m.Called(msg) + var r0 error + if rf, ok := ret.Get(0).(func(peer.ID, network.NotificationsMessage) error); ok { + r0 = rf(to, msg) + } else { + r0 = ret.Error(0) + } + + return r0 } diff --git a/lib/grandpa/state.go b/lib/grandpa/state.go index cbec44d859..8419e68712 100644 --- a/lib/grandpa/state.go +++ b/lib/grandpa/state.go @@ -66,11 +66,15 @@ type GrandpaState interface { //nolint GetPrecommits(round, setID uint64) ([]SignedVote, error) } +//go:generate mockery --name DigestHandler --structname DigestHandler --case underscore --keeptree + // DigestHandler is the interface required by GRANDPA for the digest handler type DigestHandler interface { // TODO: use GrandpaState instead (#1871) NextGrandpaAuthorityChange() uint64 } +//go:generate mockery --name Network --structname Network --case underscore --keeptree + // Network is the interface required by GRANDPA for the network type Network interface { GossipMessage(msg network.NotificationsMessage) diff --git a/lib/keystore/mock_keyring.go b/lib/keystore/mock_keyring.go deleted file mode 100644 index 1165d812f7..0000000000 --- a/lib/keystore/mock_keyring.go +++ /dev/null @@ -1,157 +0,0 @@ -// Code generated by mockery v2.8.0. DO NOT EDIT. - -package keystore - -import ( - crypto "github.com/ChainSafe/gossamer/lib/crypto" - mock "github.com/stretchr/testify/mock" -) - -// MockKeyring is an autogenerated mock type for the Keyring type -type MockKeyring struct { - mock.Mock -} - -// Alice provides a mock function with given fields: -func (_m *MockKeyring) Alice() crypto.Keypair { - ret := _m.Called() - - var r0 crypto.Keypair - if rf, ok := ret.Get(0).(func() crypto.Keypair); ok { - r0 = rf() - } else { - if ret.Get(0) != nil { - r0 = ret.Get(0).(crypto.Keypair) - } - } - - return r0 -} - -// Bob provides a mock function with given fields: -func (_m *MockKeyring) Bob() crypto.Keypair { - ret := _m.Called() - - var r0 crypto.Keypair - if rf, ok := ret.Get(0).(func() crypto.Keypair); ok { - r0 = rf() - } else { - if ret.Get(0) != nil { - r0 = ret.Get(0).(crypto.Keypair) - } - } - - return r0 -} - -// Charlie provides a mock function with given fields: -func (_m *MockKeyring) Charlie() crypto.Keypair { - ret := _m.Called() - - var r0 crypto.Keypair - if rf, ok := ret.Get(0).(func() crypto.Keypair); ok { - r0 = rf() - } else { - if ret.Get(0) != nil { - r0 = ret.Get(0).(crypto.Keypair) - } - } - - return r0 -} - -// Dave provides a mock function with given fields: -func (_m *MockKeyring) Dave() crypto.Keypair { - ret := _m.Called() - - var r0 crypto.Keypair - if rf, ok := ret.Get(0).(func() crypto.Keypair); ok { - r0 = rf() - } else { - if ret.Get(0) != nil { - r0 = ret.Get(0).(crypto.Keypair) - } - } - - return r0 -} - -// Eve provides a mock function with given fields: -func (_m *MockKeyring) Eve() crypto.Keypair { - ret := _m.Called() - - var r0 crypto.Keypair - if rf, ok := ret.Get(0).(func() crypto.Keypair); ok { - r0 = rf() - } else { - if ret.Get(0) != nil { - r0 = ret.Get(0).(crypto.Keypair) - } - } - - return r0 -} - -// Ferdie provides a mock function with given fields: -func (_m *MockKeyring) Ferdie() crypto.Keypair { - ret := _m.Called() - - var r0 crypto.Keypair - if rf, ok := ret.Get(0).(func() crypto.Keypair); ok { - r0 = rf() - } else { - if ret.Get(0) != nil { - r0 = ret.Get(0).(crypto.Keypair) - } - } - - return r0 -} - -// George provides a mock function with given fields: -func (_m *MockKeyring) George() crypto.Keypair { - ret := _m.Called() - - var r0 crypto.Keypair - if rf, ok := ret.Get(0).(func() crypto.Keypair); ok { - r0 = rf() - } else { - if ret.Get(0) != nil { - r0 = ret.Get(0).(crypto.Keypair) - } - } - - return r0 -} - -// Heather provides a mock function with given fields: -func (_m *MockKeyring) Heather() crypto.Keypair { - ret := _m.Called() - - var r0 crypto.Keypair - if rf, ok := ret.Get(0).(func() crypto.Keypair); ok { - r0 = rf() - } else { - if ret.Get(0) != nil { - r0 = ret.Get(0).(crypto.Keypair) - } - } - - return r0 -} - -// Ian provides a mock function with given fields: -func (_m *MockKeyring) Ian() crypto.Keypair { - ret := _m.Called() - - var r0 crypto.Keypair - if rf, ok := ret.Get(0).(func() crypto.Keypair); ok { - r0 = rf() - } else { - if ret.Get(0) != nil { - r0 = ret.Get(0).(crypto.Keypair) - } - } - - return r0 -} diff --git a/lib/keystore/mock_keystore.go b/lib/keystore/mock_keystore.go deleted file mode 100644 index c741132f2a..0000000000 --- a/lib/keystore/mock_keystore.go +++ /dev/null @@ -1,126 +0,0 @@ -// Code generated by mockery v2.8.0. DO NOT EDIT. - -package keystore - -import ( - common "github.com/ChainSafe/gossamer/lib/common" - crypto "github.com/ChainSafe/gossamer/lib/crypto" - - mock "github.com/stretchr/testify/mock" -) - -// MockKeystore is an autogenerated mock type for the Keystore type -type MockKeystore struct { - mock.Mock -} - -// GetKeypair provides a mock function with given fields: pub -func (_m *MockKeystore) GetKeypair(pub crypto.PublicKey) crypto.Keypair { - ret := _m.Called(pub) - - var r0 crypto.Keypair - if rf, ok := ret.Get(0).(func(crypto.PublicKey) crypto.Keypair); ok { - r0 = rf(pub) - } else { - if ret.Get(0) != nil { - r0 = ret.Get(0).(crypto.Keypair) - } - } - - return r0 -} - -// GetKeypairFromAddress provides a mock function with given fields: pub -func (_m *MockKeystore) GetKeypairFromAddress(pub common.Address) crypto.Keypair { - ret := _m.Called(pub) - - var r0 crypto.Keypair - if rf, ok := ret.Get(0).(func(common.Address) crypto.Keypair); ok { - r0 = rf(pub) - } else { - if ret.Get(0) != nil { - r0 = ret.Get(0).(crypto.Keypair) - } - } - - return r0 -} - -// Insert provides a mock function with given fields: kp -func (_m *MockKeystore) Insert(kp crypto.Keypair) { - _m.Called(kp) -} - -// Keypairs provides a mock function with given fields: -func (_m *MockKeystore) Keypairs() []crypto.Keypair { - ret := _m.Called() - - var r0 []crypto.Keypair - if rf, ok := ret.Get(0).(func() []crypto.Keypair); ok { - r0 = rf() - } else { - if ret.Get(0) != nil { - r0 = ret.Get(0).([]crypto.Keypair) - } - } - - return r0 -} - -// Name provides a mock function with given fields: -func (_m *MockKeystore) Name() Name { - ret := _m.Called() - - var r0 Name - if rf, ok := ret.Get(0).(func() Name); ok { - r0 = rf() - } else { - r0 = ret.Get(0).(Name) - } - - return r0 -} - -// PublicKeys provides a mock function with given fields: -func (_m *MockKeystore) PublicKeys() []crypto.PublicKey { - ret := _m.Called() - - var r0 []crypto.PublicKey - if rf, ok := ret.Get(0).(func() []crypto.PublicKey); ok { - r0 = rf() - } else { - if ret.Get(0) != nil { - r0 = ret.Get(0).([]crypto.PublicKey) - } - } - - return r0 -} - -// Size provides a mock function with given fields: -func (_m *MockKeystore) Size() int { - ret := _m.Called() - - var r0 int - if rf, ok := ret.Get(0).(func() int); ok { - r0 = rf() - } else { - r0 = ret.Get(0).(int) - } - - return r0 -} - -// Type provides a mock function with given fields: -func (_m *MockKeystore) Type() string { - ret := _m.Called() - - var r0 string - if rf, ok := ret.Get(0).(func() string); ok { - r0 = rf() - } else { - r0 = ret.Get(0).(string) - } - - return r0 -} diff --git a/lib/runtime/allocator_test.go b/lib/runtime/allocator_test.go index 97a62b8d7d..8f85e7c0e8 100644 --- a/lib/runtime/allocator_test.go +++ b/lib/runtime/allocator_test.go @@ -26,8 +26,8 @@ import ( "github.com/stretchr/testify/require" ) -func NewMemoryMock(size uint32) *MockMemory { - m := new(MockMemory) +func newMemoryMock(size uint32) *mockMemory { + m := new(mockMemory) testobj := make([]byte, size) m.On("Data").Return(testobj) @@ -291,7 +291,7 @@ var allTests = []testHolder{ // test holder func TestAllocator(t *testing.T) { for _, test := range allTests { - memmock := NewMemoryMock(1 << 16) + memmock := newMemoryMock(1 << 16) allocator := NewAllocator(memmock, test.offset) for _, theTest := range test.tests { @@ -336,7 +336,7 @@ func compareState(allocator FreeingBumpHeapAllocator, state allocatorState, resu // test that allocator should grow memory if the allocation request is larger than current size func TestShouldGrowMemory(t *testing.T) { - mem := NewMemoryMock(1 << 16) + mem := newMemoryMock(1 << 16) currentSize := mem.Length() fbha := NewAllocator(mem, 0) @@ -349,7 +349,7 @@ func TestShouldGrowMemory(t *testing.T) { // test that the allocator should grow memory if it's already full func TestShouldGrowMemoryIfFull(t *testing.T) { - mem := NewMemoryMock(1 << 16) + mem := newMemoryMock(1 << 16) currentSize := mem.Length() fbha := NewAllocator(mem, 0) @@ -369,10 +369,10 @@ func TestShouldGrowMemoryIfFull(t *testing.T) { // test to confirm that allocator can allocate the MaxPossibleAllocation func TestShouldAllocateMaxPossibleAllocationSize(t *testing.T) { // given, grow heap memory so that we have at least MaxPossibleAllocation available - mem := NewMemoryMock(1 << 16) + mem := newMemoryMock(1 << 16) pagesNeeded := (MaxPossibleAllocation / PageSize) - (mem.Length() / PageSize) + 1 - mem = NewMemoryMock(mem.Length() + pagesNeeded*65*1024) + mem = newMemoryMock(mem.Length() + pagesNeeded*65*1024) fbha := NewAllocator(mem, 0) @@ -388,7 +388,7 @@ func TestShouldAllocateMaxPossibleAllocationSize(t *testing.T) { // test that allocator should not allocate memory if request is too large func TestShouldNotAllocateIfRequestSizeTooLarge(t *testing.T) { - fbha := NewAllocator(NewMemoryMock(1<<16), 0) + fbha := NewAllocator(newMemoryMock(1<<16), 0) // when _, err := fbha.Allocate(MaxPossibleAllocation + 1) diff --git a/lib/runtime/interface.go b/lib/runtime/interface.go index f9855d25d9..d881fce0b1 100644 --- a/lib/runtime/interface.go +++ b/lib/runtime/interface.go @@ -25,6 +25,8 @@ import ( "github.com/ChainSafe/gossamer/lib/trie" ) +//go:generate mockery --name Instance --structname Instance --case underscore --keeptree + // Instance is the interface a v0.8 runtime instance must implement type Instance interface { UpdateRuntimeCode([]byte) error @@ -94,6 +96,8 @@ type BasicStorage interface { Del(key []byte) error } +//go:generate mockery --name TransactionState --structname TransactionState --case underscore --keeptree + // TransactionState interface for adding transactions to pool type TransactionState interface { AddToPool(vt *transaction.ValidTransaction) common.Hash diff --git a/lib/runtime/memory.go b/lib/runtime/memory.go index b7bdcd74a1..253e073960 100644 --- a/lib/runtime/memory.go +++ b/lib/runtime/memory.go @@ -19,6 +19,8 @@ package runtime // PageSize is 65kb const PageSize = 65536 +//go:generate mockery --name Memory --structname mockMemory --case underscore --inpackage --filename mock_memory_test.go + // Memory is a raw memory interface type Memory interface { Data() []byte diff --git a/lib/runtime/mock_memory.go b/lib/runtime/mock_memory_test.go similarity index 73% rename from lib/runtime/mock_memory.go rename to lib/runtime/mock_memory_test.go index a8c0bcf45b..405365beeb 100644 --- a/lib/runtime/mock_memory.go +++ b/lib/runtime/mock_memory_test.go @@ -1,16 +1,16 @@ -// Code generated by mockery v2.8.0. DO NOT EDIT. +// Code generated by mockery v2.9.4. DO NOT EDIT. package runtime import mock "github.com/stretchr/testify/mock" -// MockMemory is an autogenerated mock type for the Memory type -type MockMemory struct { +// mockMemory is an autogenerated mock type for the Memory type +type mockMemory struct { mock.Mock } // Data provides a mock function with given fields: -func (_m *MockMemory) Data() []byte { +func (_m *mockMemory) Data() []byte { ret := _m.Called() var r0 []byte @@ -26,7 +26,7 @@ func (_m *MockMemory) Data() []byte { } // Grow provides a mock function with given fields: _a0 -func (_m *MockMemory) Grow(_a0 uint32) error { +func (_m *mockMemory) Grow(_a0 uint32) error { ret := _m.Called(_a0) var r0 error @@ -40,7 +40,7 @@ func (_m *MockMemory) Grow(_a0 uint32) error { } // Length provides a mock function with given fields: -func (_m *MockMemory) Length() uint32 { +func (_m *mockMemory) Length() uint32 { ret := _m.Called() var r0 uint32 diff --git a/lib/runtime/mocks/instance.go b/lib/runtime/mocks/instance.go index 2a1cb0e1d5..ca866f4d17 100644 --- a/lib/runtime/mocks/instance.go +++ b/lib/runtime/mocks/instance.go @@ -1,4 +1,4 @@ -// Code generated by mockery v0.0.0-dev. DO NOT EDIT. +// Code generated by mockery v2.9.4. DO NOT EDIT. package mocks @@ -15,13 +15,13 @@ import ( types "github.com/ChainSafe/gossamer/dot/types" ) -// MockInstance is an autogenerated mock type for the Instance type -type MockInstance struct { +// Instance is an autogenerated mock type for the Instance type +type Instance struct { mock.Mock } // ApplyExtrinsic provides a mock function with given fields: data -func (_m *MockInstance) ApplyExtrinsic(data types.Extrinsic) ([]byte, error) { +func (_m *Instance) ApplyExtrinsic(data types.Extrinsic) ([]byte, error) { ret := _m.Called(data) var r0 []byte @@ -44,7 +44,7 @@ func (_m *MockInstance) ApplyExtrinsic(data types.Extrinsic) ([]byte, error) { } // BabeConfiguration provides a mock function with given fields: -func (_m *MockInstance) BabeConfiguration() (*types.BabeConfiguration, error) { +func (_m *Instance) BabeConfiguration() (*types.BabeConfiguration, error) { ret := _m.Called() var r0 *types.BabeConfiguration @@ -67,12 +67,12 @@ func (_m *MockInstance) BabeConfiguration() (*types.BabeConfiguration, error) { } // CheckInherents provides a mock function with given fields: -func (_m *MockInstance) CheckInherents() { +func (_m *Instance) CheckInherents() { _m.Called() } // CheckRuntimeVersion provides a mock function with given fields: _a0 -func (_m *MockInstance) CheckRuntimeVersion(_a0 []byte) (runtime.Version, error) { +func (_m *Instance) CheckRuntimeVersion(_a0 []byte) (runtime.Version, error) { ret := _m.Called(_a0) var r0 runtime.Version @@ -95,7 +95,7 @@ func (_m *MockInstance) CheckRuntimeVersion(_a0 []byte) (runtime.Version, error) } // DecodeSessionKeys provides a mock function with given fields: enc -func (_m *MockInstance) DecodeSessionKeys(enc []byte) ([]byte, error) { +func (_m *Instance) DecodeSessionKeys(enc []byte) ([]byte, error) { ret := _m.Called(enc) var r0 []byte @@ -118,7 +118,7 @@ func (_m *MockInstance) DecodeSessionKeys(enc []byte) ([]byte, error) { } // Exec provides a mock function with given fields: function, data -func (_m *MockInstance) Exec(function string, data []byte) ([]byte, error) { +func (_m *Instance) Exec(function string, data []byte) ([]byte, error) { ret := _m.Called(function, data) var r0 []byte @@ -141,7 +141,7 @@ func (_m *MockInstance) Exec(function string, data []byte) ([]byte, error) { } // ExecuteBlock provides a mock function with given fields: block -func (_m *MockInstance) ExecuteBlock(block *types.Block) ([]byte, error) { +func (_m *Instance) ExecuteBlock(block *types.Block) ([]byte, error) { ret := _m.Called(block) var r0 []byte @@ -164,7 +164,7 @@ func (_m *MockInstance) ExecuteBlock(block *types.Block) ([]byte, error) { } // FinalizeBlock provides a mock function with given fields: -func (_m *MockInstance) FinalizeBlock() (*types.Header, error) { +func (_m *Instance) FinalizeBlock() (*types.Header, error) { ret := _m.Called() var r0 *types.Header @@ -187,12 +187,12 @@ func (_m *MockInstance) FinalizeBlock() (*types.Header, error) { } // GenerateSessionKeys provides a mock function with given fields: -func (_m *MockInstance) GenerateSessionKeys() { +func (_m *Instance) GenerateSessionKeys() { _m.Called() } // GetCodeHash provides a mock function with given fields: -func (_m *MockInstance) GetCodeHash() common.Hash { +func (_m *Instance) GetCodeHash() common.Hash { ret := _m.Called() var r0 common.Hash @@ -208,7 +208,7 @@ func (_m *MockInstance) GetCodeHash() common.Hash { } // GrandpaAuthorities provides a mock function with given fields: -func (_m *MockInstance) GrandpaAuthorities() ([]types.Authority, error) { +func (_m *Instance) GrandpaAuthorities() ([]types.Authority, error) { ret := _m.Called() var r0 []types.Authority @@ -231,7 +231,7 @@ func (_m *MockInstance) GrandpaAuthorities() ([]types.Authority, error) { } // InherentExtrinsics provides a mock function with given fields: data -func (_m *MockInstance) InherentExtrinsics(data []byte) ([]byte, error) { +func (_m *Instance) InherentExtrinsics(data []byte) ([]byte, error) { ret := _m.Called(data) var r0 []byte @@ -254,7 +254,7 @@ func (_m *MockInstance) InherentExtrinsics(data []byte) ([]byte, error) { } // InitializeBlock provides a mock function with given fields: header -func (_m *MockInstance) InitializeBlock(header *types.Header) error { +func (_m *Instance) InitializeBlock(header *types.Header) error { ret := _m.Called(header) var r0 error @@ -268,7 +268,7 @@ func (_m *MockInstance) InitializeBlock(header *types.Header) error { } // Keystore provides a mock function with given fields: -func (_m *MockInstance) Keystore() *keystore.GlobalKeystore { +func (_m *Instance) Keystore() *keystore.GlobalKeystore { ret := _m.Called() var r0 *keystore.GlobalKeystore @@ -284,7 +284,7 @@ func (_m *MockInstance) Keystore() *keystore.GlobalKeystore { } // Metadata provides a mock function with given fields: -func (_m *MockInstance) Metadata() ([]byte, error) { +func (_m *Instance) Metadata() ([]byte, error) { ret := _m.Called() var r0 []byte @@ -307,7 +307,7 @@ func (_m *MockInstance) Metadata() ([]byte, error) { } // NetworkService provides a mock function with given fields: -func (_m *MockInstance) NetworkService() runtime.BasicNetwork { +func (_m *Instance) NetworkService() runtime.BasicNetwork { ret := _m.Called() var r0 runtime.BasicNetwork @@ -323,7 +323,7 @@ func (_m *MockInstance) NetworkService() runtime.BasicNetwork { } // NodeStorage provides a mock function with given fields: -func (_m *MockInstance) NodeStorage() runtime.NodeStorage { +func (_m *Instance) NodeStorage() runtime.NodeStorage { ret := _m.Called() var r0 runtime.NodeStorage @@ -337,12 +337,12 @@ func (_m *MockInstance) NodeStorage() runtime.NodeStorage { } // OffchainWorker provides a mock function with given fields: -func (_m *MockInstance) OffchainWorker() { +func (_m *Instance) OffchainWorker() { _m.Called() } // PaymentQueryInfo provides a mock function with given fields: ext -func (_m *MockInstance) PaymentQueryInfo(ext []byte) (*types.TransactionPaymentQueryInfo, error) { +func (_m *Instance) PaymentQueryInfo(ext []byte) (*types.TransactionPaymentQueryInfo, error) { ret := _m.Called(ext) var r0 *types.TransactionPaymentQueryInfo @@ -365,22 +365,22 @@ func (_m *MockInstance) PaymentQueryInfo(ext []byte) (*types.TransactionPaymentQ } // RandomSeed provides a mock function with given fields: -func (_m *MockInstance) RandomSeed() { +func (_m *Instance) RandomSeed() { _m.Called() } // SetContextStorage provides a mock function with given fields: s -func (_m *MockInstance) SetContextStorage(s runtime.Storage) { +func (_m *Instance) SetContextStorage(s runtime.Storage) { _m.Called(s) } // Stop provides a mock function with given fields: -func (_m *MockInstance) Stop() { +func (_m *Instance) Stop() { _m.Called() } // UpdateRuntimeCode provides a mock function with given fields: _a0 -func (_m *MockInstance) UpdateRuntimeCode(_a0 []byte) error { +func (_m *Instance) UpdateRuntimeCode(_a0 []byte) error { ret := _m.Called(_a0) var r0 error @@ -394,7 +394,7 @@ func (_m *MockInstance) UpdateRuntimeCode(_a0 []byte) error { } // ValidateTransaction provides a mock function with given fields: e -func (_m *MockInstance) ValidateTransaction(e types.Extrinsic) (*transaction.Validity, error) { +func (_m *Instance) ValidateTransaction(e types.Extrinsic) (*transaction.Validity, error) { ret := _m.Called(e) var r0 *transaction.Validity @@ -417,7 +417,7 @@ func (_m *MockInstance) ValidateTransaction(e types.Extrinsic) (*transaction.Val } // Validator provides a mock function with given fields: -func (_m *MockInstance) Validator() bool { +func (_m *Instance) Validator() bool { ret := _m.Called() var r0 bool @@ -431,7 +431,7 @@ func (_m *MockInstance) Validator() bool { } // Version provides a mock function with given fields: -func (_m *MockInstance) Version() (runtime.Version, error) { +func (_m *Instance) Version() (runtime.Version, error) { ret := _m.Called() var r0 runtime.Version diff --git a/lib/runtime/mock_transaction_state.go b/lib/runtime/mocks/transaction_state.go similarity index 63% rename from lib/runtime/mock_transaction_state.go rename to lib/runtime/mocks/transaction_state.go index e27caec9e4..57dd453d06 100644 --- a/lib/runtime/mock_transaction_state.go +++ b/lib/runtime/mocks/transaction_state.go @@ -1,6 +1,6 @@ -// Code generated by mockery v2.8.0. DO NOT EDIT. +// Code generated by mockery v2.9.4. DO NOT EDIT. -package runtime +package mocks import ( common "github.com/ChainSafe/gossamer/lib/common" @@ -9,13 +9,13 @@ import ( transaction "github.com/ChainSafe/gossamer/lib/transaction" ) -// MockTransactionState is an autogenerated mock type for the TransactionState type -type MockTransactionState struct { +// TransactionState is an autogenerated mock type for the TransactionState type +type TransactionState struct { mock.Mock } // AddToPool provides a mock function with given fields: vt -func (_m *MockTransactionState) AddToPool(vt *transaction.ValidTransaction) common.Hash { +func (_m *TransactionState) AddToPool(vt *transaction.ValidTransaction) common.Hash { ret := _m.Called(vt) var r0 common.Hash diff --git a/lib/runtime/mocks/version.go b/lib/runtime/mocks/version.go index 5f6989faac..393d58564b 100644 --- a/lib/runtime/mocks/version.go +++ b/lib/runtime/mocks/version.go @@ -1,4 +1,4 @@ -// Code generated by mockery 2.9.0. DO NOT EDIT. +// Code generated by mockery v2.9.4. DO NOT EDIT. package mocks diff --git a/lib/runtime/version.go b/lib/runtime/version.go index 0856f65fac..e1d44ec0a1 100644 --- a/lib/runtime/version.go +++ b/lib/runtime/version.go @@ -20,6 +20,8 @@ import ( "github.com/ChainSafe/gossamer/pkg/scale" ) +//go:generate mockery --name Version --structname Version --case underscore --keeptree + // Version represents the data returned by runtime call core_version type Version interface { SpecName() []byte diff --git a/lib/runtime/wasmer/test_helpers.go b/lib/runtime/wasmer/test_helpers.go index 556739bca1..81e89151ea 100644 --- a/lib/runtime/wasmer/test_helpers.go +++ b/lib/runtime/wasmer/test_helpers.go @@ -23,6 +23,7 @@ import ( "github.com/ChainSafe/gossamer/lib/common" "github.com/ChainSafe/gossamer/lib/keystore" "github.com/ChainSafe/gossamer/lib/runtime" + "github.com/ChainSafe/gossamer/lib/runtime/mocks" "github.com/ChainSafe/gossamer/lib/runtime/storage" "github.com/ChainSafe/gossamer/lib/trie" log "github.com/ChainSafe/log15" @@ -81,14 +82,14 @@ func setupConfig(t *testing.T, targetRuntime string, tt *trie.Trie, lvl log.Lvl, cfg.LogLvl = lvl cfg.NodeStorage = ns cfg.Network = new(runtime.TestRuntimeNetwork) - cfg.Transaction = NewTransactionStateMock() + cfg.Transaction = newTransactionStateMock() cfg.Role = role return fp, cfg } // NewTransactionStateMock create and return an runtime Transaction State interface mock -func NewTransactionStateMock() *runtime.MockTransactionState { - m := new(runtime.MockTransactionState) +func newTransactionStateMock() *mocks.TransactionState { + m := new(mocks.TransactionState) m.On("AddToPool", mock.AnythingOfType("*transaction.ValidTransaction")).Return(common.BytesToHash([]byte("test"))) return m } diff --git a/lib/scale/decode_ptr_test.go b/lib/scale/decode_ptr_test.go index c1c2711fca..32be225b50 100644 --- a/lib/scale/decode_ptr_test.go +++ b/lib/scale/decode_ptr_test.go @@ -229,15 +229,15 @@ func TestDecodePtr_DecodeCommonHash(t *testing.T) { } // add Decode func to MockTypeA -func (tr *MockTypeA) Decode(in []byte) error { +func (tr *mockTypeA) Decode(in []byte) error { return DecodePtr(in, tr) } // test decoding for MockTypeA (which has Decode func) func TestDecodeCustom_DecodeMockTypeA(t *testing.T) { - expected := &MockTypeA{A: "hello"} + expected := &mockTypeA{A: "hello"} encoded := []byte{20, 104, 101, 108, 108, 111} - mockType := new(MockTypeA) + mockType := new(mockTypeA) err := DecodeCustom(encoded, mockType) require.Nil(t, err) @@ -246,9 +246,9 @@ func TestDecodeCustom_DecodeMockTypeA(t *testing.T) { // test decoding for MockTypeB (which does not have Decode func) func TestDecodeCustom_DecodeMockTypeB(t *testing.T) { - expected := &MockTypeB{A: "hello"} + expected := &mockTypeB{A: "hello"} encoded := []byte{20, 104, 101, 108, 108, 111} - mockType := new(MockTypeB) + mockType := new(mockTypeB) err := DecodeCustom(encoded, mockType) require.Nil(t, err) @@ -256,16 +256,16 @@ func TestDecodeCustom_DecodeMockTypeB(t *testing.T) { } // add Decode func to MockTypeC which will return fake data (so we'll know when it was called) -func (tr *MockTypeC) Decode(in []byte) error { +func (tr *mockTypeC) Decode(in []byte) error { tr.A = "goodbye" return nil } // test decoding for MockTypeC (which has Decode func that returns fake data (A: "goodbye")) func TestDecodeCustom_DecodeMockTypeC(t *testing.T) { - expected := &MockTypeC{A: "goodbye"} + expected := &mockTypeC{A: "goodbye"} encoded := []byte{20, 104, 101, 108, 108, 111} - mockType := new(MockTypeC) + mockType := new(mockTypeC) err := DecodeCustom(encoded, mockType) require.Nil(t, err) @@ -273,14 +273,14 @@ func TestDecodeCustom_DecodeMockTypeC(t *testing.T) { } // add Decode func to MockTypeD which will return an error -func (tr *MockTypeD) Decode(in []byte) error { +func (tr *mockTypeD) Decode(in []byte) error { return errors.New("error decoding") } // test decoding for MockTypeD (which has Decode func that returns error) func TestDecodeCustom_DecodeMockTypeD(t *testing.T) { encoded := []byte{20, 104, 101, 108, 108, 111} - mockType := new(MockTypeD) + mockType := new(mockTypeD) err := DecodeCustom(encoded, mockType) require.EqualError(t, err, "error decoding") diff --git a/lib/scale/encode_test.go b/lib/scale/encode_test.go index 88f80f91f1..33b069d4f9 100644 --- a/lib/scale/encode_test.go +++ b/lib/scale/encode_test.go @@ -242,18 +242,18 @@ func TestEncodeAndDecodeStringArrayInStruct(t *testing.T) { } // test type for encoding -type MockTypeA struct { +type mockTypeA struct { A string } // Encode func for TypeReal that uses actual Scale Encode -func (tr *MockTypeA) Encode() ([]byte, error) { +func (tr *mockTypeA) Encode() ([]byte, error) { return Encode(tr) } // test to confirm EncodeCustom is return Scale Encoded result func TestEncodeCustomMockTypeA(t *testing.T) { - test := &MockTypeA{A: "hello"} + test := &mockTypeA{A: "hello"} encCust, err := EncodeCustom(test) require.Nil(t, err) @@ -265,13 +265,13 @@ func TestEncodeCustomMockTypeA(t *testing.T) { } // test type for encoding, this type does not have Encode func -type MockTypeB struct { +type mockTypeB struct { A string } // test to confirm EncodeCustom is return Scale Encoded result func TestEncodeCustomMockTypeB(t *testing.T) { - test := &MockTypeB{A: "hello"} + test := &mockTypeB{A: "hello"} encCust, err := EncodeCustom(test) require.Nil(t, err) @@ -283,18 +283,18 @@ func TestEncodeCustomMockTypeB(t *testing.T) { } // test types for encoding -type MockTypeC struct { +type mockTypeC struct { A string } // Encode func for MockTypeC that return fake byte array [1, 2, 3] -func (tr *MockTypeC) Encode() ([]byte, error) { +func (tr *mockTypeC) Encode() ([]byte, error) { return []byte{1, 2, 3}, nil } // test to confirm EncodeCustom is using type's Encode function func TestEncodeCustomMockTypeC(t *testing.T) { - test := &MockTypeC{A: "hello"} + test := &mockTypeC{A: "hello"} expected := []byte{1, 2, 3} encCust, err := EncodeCustom(test) @@ -304,18 +304,18 @@ func TestEncodeCustomMockTypeC(t *testing.T) { } // test types for encoding -type MockTypeD struct { +type mockTypeD struct { A string } // Encode func for MockTypeD that return an error -func (tr *MockTypeD) Encode() ([]byte, error) { +func (tr *mockTypeD) Encode() ([]byte, error) { return nil, errors.New("error encoding") } // test to confirm EncodeCustom is handling errors func TestEncodeCustomMockTypeD(t *testing.T) { - test := &MockTypeD{A: "hello"} + test := &mockTypeD{A: "hello"} _, err := EncodeCustom(test) require.EqualError(t, err, "error encoding") diff --git a/lib/services/mocks/service.go b/lib/services/mocks/service.go index 604f83f568..859e4435b4 100644 --- a/lib/services/mocks/service.go +++ b/lib/services/mocks/service.go @@ -1,16 +1,16 @@ -// Code generated by mockery v2.8.0. DO NOT EDIT. +// Code generated by mockery v2.9.4. DO NOT EDIT. -package services +package mocks import mock "github.com/stretchr/testify/mock" -// MockService is an autogenerated mock type for the Service type -type MockService struct { +// Service is an autogenerated mock type for the Service type +type Service struct { mock.Mock } // Start provides a mock function with given fields: -func (_m *MockService) Start() error { +func (_m *Service) Start() error { ret := _m.Called() var r0 error @@ -24,7 +24,7 @@ func (_m *MockService) Start() error { } // Stop provides a mock function with given fields: -func (_m *MockService) Stop() error { +func (_m *Service) Stop() error { ret := _m.Called() var r0 error diff --git a/lib/services/services.go b/lib/services/services.go index e3e324b279..29ac934d7e 100644 --- a/lib/services/services.go +++ b/lib/services/services.go @@ -23,6 +23,8 @@ import ( log "github.com/ChainSafe/log15" ) +//go:generate mockery --name Service --structname Service --case underscore --keeptree + // Service must be implemented by all services type Service interface { Start() error diff --git a/lib/services/services_test.go b/lib/services/services_test.go index 3ee162f893..231162f64d 100644 --- a/lib/services/services_test.go +++ b/lib/services/services_test.go @@ -19,15 +19,15 @@ package services import ( "testing" - . "github.com/ChainSafe/gossamer/lib/services/mocks" + "github.com/ChainSafe/gossamer/lib/services/mocks" "github.com/stretchr/testify/require" ) func TestServiceRegistry_RegisterService(t *testing.T) { r := NewServiceRegistry() - r.RegisterService(&MockService{}) - r.RegisterService(&MockService{}) + r.RegisterService(&mocks.Service{}) + r.RegisterService(&mocks.Service{}) require.Len(t, r.services, 1) } @@ -35,7 +35,7 @@ func TestServiceRegistry_RegisterService(t *testing.T) { func TestServiceRegistry_StartStopAll(t *testing.T) { r := NewServiceRegistry() - m := new(MockService) + m := new(mocks.Service) m.On("Start").Return(nil) m.On("Stop").Return(nil) @@ -51,7 +51,7 @@ func TestServiceRegistry_StartStopAll(t *testing.T) { func TestServiceRegistry_Get_Err(t *testing.T) { r := NewServiceRegistry() - a := new(MockService) + a := new(mocks.Service) a.On("Start").Return(nil) a.On("Stop").Return(nil)