Skip to content

Commit

Permalink
make: mocks using mockgen (#10098)
Browse files Browse the repository at this point in the history
- replaces usages of `moq` in `erigon-lib` with `mockgen` (gomock)
- adds a `make mocks` and `mocks-clean` command for `erigon`
- updates existing `make mocks` command and adds a `mocks-clean` common
for `erigon-lib`
  • Loading branch information
taratorio authored Apr 28, 2024
1 parent d7d0960 commit 63578a8
Show file tree
Hide file tree
Showing 36 changed files with 2,837 additions and 3,435 deletions.
12 changes: 11 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ clean:

## devtools: installs dev tools (and checks for npm installation etc.)
devtools:
# Notice! If you adding new binary - add it also to cmd/hack/binary-deps/main.go file
# Notice! If you adding new binary - add it also to tools.go file
$(GOBUILD) -o $(GOBIN)/gencodec github.com/fjl/gencodec
$(GOBUILD) -o $(GOBIN)/mockgen go.uber.org/mock/mockgen
$(GOBUILD) -o $(GOBIN)/abigen ./cmd/abigen
Expand All @@ -213,6 +213,16 @@ devtools:
@type "solc" 2> /dev/null || echo 'Please install solc'
@type "protoc" 2> /dev/null || echo 'Please install protoc'

## mocks: generate test mocks
mocks: mocks-clean
@cd erigon-lib && $(MAKE) mocks
$(GOBUILD) -o $(GOBIN)/mockgen go.uber.org/mock/mockgen
PATH="$(GOBIN):$(PATH)" go generate -run "mockgen" ./...

## mocks-clean: cleans all generated test mocks
mocks-clean:
grep -r -l --exclude-dir=erigon-lib "^// Code generated by MockGen. DO NOT EDIT.$$" . | xargs rm -r

## bindings: generate test contracts and core contracts
bindings:
PATH=$(GOBIN):$(PATH) go generate ./tests/contracts/
Expand Down
15 changes: 8 additions & 7 deletions cl/beacon/handler/utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@ import (
"math"
"testing"

"github.com/ledgerwatch/log/v3"
"github.com/spf13/afero"
"github.com/stretchr/testify/require"
"go.uber.org/mock/gomock"

"github.com/ledgerwatch/erigon-lib/common/datadir"
"github.com/ledgerwatch/erigon-lib/kv"
"github.com/ledgerwatch/erigon-lib/kv/memdb"
Expand All @@ -20,18 +25,14 @@ import (
state_accessors "github.com/ledgerwatch/erigon/cl/persistence/state"
"github.com/ledgerwatch/erigon/cl/persistence/state/historical_states_reader"
"github.com/ledgerwatch/erigon/cl/phase1/core/state"
"github.com/ledgerwatch/erigon/cl/phase1/forkchoice"
mock_services2 "github.com/ledgerwatch/erigon/cl/phase1/forkchoice/mock_services"
"github.com/ledgerwatch/erigon/cl/phase1/network/services/mock_services"
"github.com/ledgerwatch/erigon/cl/pool"
"github.com/ledgerwatch/erigon/cl/utils/eth_clock"
"github.com/ledgerwatch/erigon/cl/validator/validator_params"
"github.com/ledgerwatch/log/v3"
"github.com/spf13/afero"
"github.com/stretchr/testify/require"
"go.uber.org/mock/gomock"
)

func setupTestingHandler(t *testing.T, v clparams.StateVersion, logger log.Logger) (db kv.RwDB, blocks []*cltypes.SignedBeaconBlock, f afero.Fs, preState, postState *state.CachingBeaconState, h *ApiHandler, opPool pool.OperationsPool, syncedData *synced_data.SyncedDataManager, fcu *forkchoice.ForkChoiceStorageMock, vp *validator_params.ValidatorParams) {
func setupTestingHandler(t *testing.T, v clparams.StateVersion, logger log.Logger) (db kv.RwDB, blocks []*cltypes.SignedBeaconBlock, f afero.Fs, preState, postState *state.CachingBeaconState, h *ApiHandler, opPool pool.OperationsPool, syncedData *synced_data.SyncedDataManager, fcu *mock_services2.ForkChoiceStorageMock, vp *validator_params.ValidatorParams) {
bcfg := clparams.MainnetBeaconConfig
if v == clparams.Phase0Version {
blocks, preState, postState = tests.GetPhase0Random()
Expand All @@ -45,7 +46,7 @@ func setupTestingHandler(t *testing.T, v clparams.StateVersion, logger log.Logge
bcfg.CapellaForkEpoch = 1
blocks, preState, postState = tests.GetCapellaRandom()
}
fcu = forkchoice.NewForkChoiceStorageMock(t)
fcu = mock_services2.NewForkChoiceStorageMock(t)
db = memdb.NewTestDB(t)
blobDb := memdb.NewTestDB(t)
var reader *tests.MockBlockReader
Expand Down
Original file line number Diff line number Diff line change
@@ -1,20 +1,22 @@
package forkchoice
package mock_services

import (
"context"
"testing"

"go.uber.org/mock/gomock"

"github.com/ledgerwatch/erigon-lib/common"
libcommon "github.com/ledgerwatch/erigon-lib/common"
"github.com/ledgerwatch/erigon/cl/cltypes"
"github.com/ledgerwatch/erigon/cl/cltypes/solid"
"github.com/ledgerwatch/erigon/cl/phase1/core/state"
"github.com/ledgerwatch/erigon/cl/phase1/execution_client"
"github.com/ledgerwatch/erigon/cl/phase1/forkchoice"
"github.com/ledgerwatch/erigon/cl/pool"
"github.com/ledgerwatch/erigon/cl/transition/impl/eth2"
"github.com/ledgerwatch/erigon/cl/validator/sync_contribution_pool"
syncpoolmock "github.com/ledgerwatch/erigon/cl/validator/sync_contribution_pool/mock_services"
"go.uber.org/mock/gomock"
)

// Make mocks with maps and simple setters and getters, panic on methods from ForkChoiceStorageWriter
Expand All @@ -39,7 +41,7 @@ type ForkChoiceStorageMock struct {
StateAtSlotVal map[uint64]*state.CachingBeaconState
GetSyncCommitteesVal map[uint64][2]*solid.SyncCommittee
GetFinalityCheckpointsVal map[common.Hash][3]solid.Checkpoint
WeightsMock []ForkNode
WeightsMock []forkchoice.ForkNode
LightClientBootstraps map[common.Hash]*cltypes.LightClientBootstrap
NewestLCUpdate *cltypes.LightClientUpdate
LCUpdates map[uint64]*cltypes.LightClientUpdate
Expand Down Expand Up @@ -229,7 +231,7 @@ func (f *ForkChoiceStorageMock) Partecipation(epoch uint64) (*solid.BitList, boo
return f.ParticipationVal, f.ParticipationVal != nil
}

func (f *ForkChoiceStorageMock) ForkNodes() []ForkNode {
func (f *ForkChoiceStorageMock) ForkNodes() []forkchoice.ForkNode {
return f.WeightsMock
}

Expand Down
11 changes: 6 additions & 5 deletions cl/phase1/network/services/aggregate_and_proof_service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ import (
"context"
"testing"

"github.com/stretchr/testify/require"
"go.uber.org/mock/gomock"

"github.com/ledgerwatch/erigon-lib/common"
libcommon "github.com/ledgerwatch/erigon-lib/common"
"github.com/ledgerwatch/erigon/cl/antiquary/tests"
Expand All @@ -12,10 +15,8 @@ import (
"github.com/ledgerwatch/erigon/cl/cltypes"
"github.com/ledgerwatch/erigon/cl/cltypes/solid"
"github.com/ledgerwatch/erigon/cl/phase1/core/state"
"github.com/ledgerwatch/erigon/cl/phase1/forkchoice"
"github.com/ledgerwatch/erigon/cl/phase1/forkchoice/mock_services"
"github.com/ledgerwatch/erigon/cl/pool"
"github.com/stretchr/testify/require"
"go.uber.org/mock/gomock"
)

func getAggregateAndProofAndState(t *testing.T) (*cltypes.SignedAggregateAndProof, *state.CachingBeaconState) {
Expand All @@ -40,12 +41,12 @@ func getAggregateAndProofAndState(t *testing.T) (*cltypes.SignedAggregateAndProo

}

func setupAggregateAndProofTest(t *testing.T) (AggregateAndProofService, *synced_data.SyncedDataManager, *forkchoice.ForkChoiceStorageMock) {
func setupAggregateAndProofTest(t *testing.T) (AggregateAndProofService, *synced_data.SyncedDataManager, *mock_services.ForkChoiceStorageMock) {
ctx, cn := context.WithCancel(context.Background())
cn()
cfg := &clparams.MainnetBeaconConfig
syncedDataManager := synced_data.NewSyncedDataManager(true, cfg)
forkchoiceMock := forkchoice.NewForkChoiceStorageMock(t)
forkchoiceMock := mock_services.NewForkChoiceStorageMock(t)
p := pool.OperationsPool{}
p.AttestationsPool = pool.NewOperationPool[libcommon.Bytes96, *solid.Attestation](100, "test")
blockService := NewAggregateAndProofService(ctx, syncedDataManager, forkchoiceMock, cfg, p, true)
Expand Down
11 changes: 6 additions & 5 deletions cl/phase1/network/services/attestation_service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ import (
"log"
"testing"

"github.com/stretchr/testify/suite"
"go.uber.org/mock/gomock"

"github.com/ledgerwatch/erigon-lib/common"
"github.com/ledgerwatch/erigon-lib/types/ssz"
mockSync "github.com/ledgerwatch/erigon/cl/beacon/synced_data/mock_services"
Expand All @@ -13,11 +16,9 @@ import (
"github.com/ledgerwatch/erigon/cl/cltypes/solid"
"github.com/ledgerwatch/erigon/cl/phase1/core/state"
mockState "github.com/ledgerwatch/erigon/cl/phase1/core/state/mock_services"
"github.com/ledgerwatch/erigon/cl/phase1/forkchoice"
"github.com/ledgerwatch/erigon/cl/phase1/forkchoice/mock_services"
"github.com/ledgerwatch/erigon/cl/utils/eth_clock"
mockCommittee "github.com/ledgerwatch/erigon/cl/validator/committee_subscription/mock_services"
"github.com/stretchr/testify/suite"
"go.uber.org/mock/gomock"
)

var (
Expand All @@ -38,7 +39,7 @@ var (
type attestationTestSuite struct {
suite.Suite
gomockCtrl *gomock.Controller
mockForkChoice *forkchoice.ForkChoiceStorageMock
mockForkChoice *mock_services.ForkChoiceStorageMock
syncedData *mockSync.MockSyncedData
beaconStateReader *mockState.MockBeaconStateReader
committeeSubscibe *mockCommittee.MockCommitteeSubscribe
Expand All @@ -49,7 +50,7 @@ type attestationTestSuite struct {

func (t *attestationTestSuite) SetupTest() {
t.gomockCtrl = gomock.NewController(t.T())
t.mockForkChoice = &forkchoice.ForkChoiceStorageMock{}
t.mockForkChoice = &mock_services.ForkChoiceStorageMock{}
t.syncedData = mockSync.NewMockSyncedData(t.gomockCtrl)
t.beaconStateReader = mockState.NewMockBeaconStateReader(t.gomockCtrl)
t.committeeSubscibe = mockCommittee.NewMockCommitteeSubscribe(t.gomockCtrl)
Expand Down
11 changes: 6 additions & 5 deletions cl/phase1/network/services/blob_sidecar_service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,18 @@ import (
_ "embed"
"testing"

"github.com/stretchr/testify/require"
"go.uber.org/mock/gomock"

"github.com/ledgerwatch/erigon-lib/common"
libcommon "github.com/ledgerwatch/erigon-lib/common"
"github.com/ledgerwatch/erigon/cl/beacon/synced_data"
"github.com/ledgerwatch/erigon/cl/clparams"
"github.com/ledgerwatch/erigon/cl/cltypes"
"github.com/ledgerwatch/erigon/cl/phase1/core/state"
"github.com/ledgerwatch/erigon/cl/phase1/forkchoice"
"github.com/ledgerwatch/erigon/cl/phase1/forkchoice/mock_services"
"github.com/ledgerwatch/erigon/cl/utils"
"github.com/ledgerwatch/erigon/cl/utils/eth_clock"
"github.com/stretchr/testify/require"
"go.uber.org/mock/gomock"
)

//go:embed test_data/blob_sidecar_service_blob.ssz_snappy
Expand Down Expand Up @@ -48,14 +49,14 @@ func getObjectsForBlobSidecarServiceTests(t *testing.T) (*state.CachingBeaconSta
return stateObj, block, sidecar
}

func setupBlobSidecarService(t *testing.T, ctrl *gomock.Controller, test bool) (BlobSidecarsService, *synced_data.SyncedDataManager, *eth_clock.MockEthereumClock, *forkchoice.ForkChoiceStorageMock) {
func setupBlobSidecarService(t *testing.T, ctrl *gomock.Controller, test bool) (BlobSidecarsService, *synced_data.SyncedDataManager, *eth_clock.MockEthereumClock, *mock_services.ForkChoiceStorageMock) {
ctx := context.Background()
ctx2, cn := context.WithTimeout(ctx, 1)
cn()
cfg := &clparams.MainnetBeaconConfig
syncedDataManager := synced_data.NewSyncedDataManager(true, cfg)
ethClock := eth_clock.NewMockEthereumClock(ctrl)
forkchoiceMock := forkchoice.NewForkChoiceStorageMock(t)
forkchoiceMock := mock_services.NewForkChoiceStorageMock(t)
blockService := NewBlobSidecarService(ctx2, cfg, forkchoiceMock, syncedDataManager, ethClock, test)
return blockService, syncedDataManager, ethClock, forkchoiceMock
}
Expand Down
11 changes: 6 additions & 5 deletions cl/phase1/network/services/block_service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,24 +4,25 @@ import (
"context"
"testing"

"github.com/stretchr/testify/require"
"go.uber.org/mock/gomock"

"github.com/ledgerwatch/erigon-lib/kv/memdb"
"github.com/ledgerwatch/erigon/cl/antiquary/tests"
"github.com/ledgerwatch/erigon/cl/beacon/synced_data"
"github.com/ledgerwatch/erigon/cl/clparams"
"github.com/ledgerwatch/erigon/cl/cltypes"
"github.com/ledgerwatch/erigon/cl/cltypes/solid"
"github.com/ledgerwatch/erigon/cl/phase1/forkchoice"
"github.com/ledgerwatch/erigon/cl/phase1/forkchoice/mock_services"
"github.com/ledgerwatch/erigon/cl/utils/eth_clock"
"github.com/stretchr/testify/require"
"go.uber.org/mock/gomock"
)

func setupBlockService(t *testing.T, ctrl *gomock.Controller) (BlockService, *synced_data.SyncedDataManager, *eth_clock.MockEthereumClock, *forkchoice.ForkChoiceStorageMock) {
func setupBlockService(t *testing.T, ctrl *gomock.Controller) (BlockService, *synced_data.SyncedDataManager, *eth_clock.MockEthereumClock, *mock_services.ForkChoiceStorageMock) {
db := memdb.NewTestDB(t)
cfg := &clparams.MainnetBeaconConfig
syncedDataManager := synced_data.NewSyncedDataManager(true, cfg)
ethClock := eth_clock.NewMockEthereumClock(ctrl)
forkchoiceMock := forkchoice.NewForkChoiceStorageMock(t)
forkchoiceMock := mock_services.NewForkChoiceStorageMock(t)
blockService := NewBlockService(context.Background(), db, forkchoiceMock, syncedDataManager, ethClock, cfg, nil)
return blockService, syncedDataManager, ethClock, forkchoiceMock
}
Expand Down
17 changes: 9 additions & 8 deletions cl/sentinel/handlers/blobs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,12 @@ import (
"testing"

"github.com/golang/snappy"
"github.com/libp2p/go-libp2p"
"github.com/libp2p/go-libp2p/core/peer"
"github.com/libp2p/go-libp2p/core/protocol"
"github.com/spf13/afero"
"github.com/stretchr/testify/require"

libcommon "github.com/ledgerwatch/erigon-lib/common"
"github.com/ledgerwatch/erigon-lib/kv/memdb"
"github.com/ledgerwatch/erigon/cl/antiquary/tests"
Expand All @@ -18,17 +24,12 @@ import (
"github.com/ledgerwatch/erigon/cl/cltypes"
"github.com/ledgerwatch/erigon/cl/cltypes/solid"
"github.com/ledgerwatch/erigon/cl/persistence/blob_storage"
"github.com/ledgerwatch/erigon/cl/phase1/forkchoice"
"github.com/ledgerwatch/erigon/cl/phase1/forkchoice/mock_services"
"github.com/ledgerwatch/erigon/cl/sentinel/communication"
"github.com/ledgerwatch/erigon/cl/sentinel/communication/ssz_snappy"
"github.com/ledgerwatch/erigon/cl/sentinel/peers"
"github.com/ledgerwatch/erigon/cl/utils"
"github.com/ledgerwatch/erigon/cl/utils/eth_clock"
"github.com/libp2p/go-libp2p"
"github.com/libp2p/go-libp2p/core/peer"
"github.com/libp2p/go-libp2p/core/protocol"
"github.com/spf13/afero"
"github.com/stretchr/testify/require"
)

func getEthClock(t *testing.T) eth_clock.EthereumClock {
Expand Down Expand Up @@ -101,7 +102,7 @@ func TestBlobsByRangeHandler(t *testing.T) {
nil,
beaconCfg,
ethClock,
nil, &forkchoice.ForkChoiceStorageMock{}, blobStorage, true,
nil, &mock_services.ForkChoiceStorageMock{}, blobStorage, true,
)
c.Start()
req := &cltypes.BlobsByRangeRequest{
Expand Down Expand Up @@ -222,7 +223,7 @@ func TestBlobsByIdentifiersHandler(t *testing.T) {
nil,
beaconCfg,
ethClock,
nil, &forkchoice.ForkChoiceStorageMock{}, blobStorage, true,
nil, &mock_services.ForkChoiceStorageMock{}, blobStorage, true,
)
c.Start()
req := solid.NewStaticListSSZ[*cltypes.BlobIdentifier](40269, 40)
Expand Down
13 changes: 7 additions & 6 deletions cl/sentinel/handlers/blocks_by_range_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,20 @@ import (
"testing"

"github.com/golang/snappy"
"github.com/libp2p/go-libp2p"
"github.com/libp2p/go-libp2p/core/peer"
"github.com/libp2p/go-libp2p/core/protocol"
"github.com/stretchr/testify/require"

libcommon "github.com/ledgerwatch/erigon-lib/common"
"github.com/ledgerwatch/erigon/cl/antiquary/tests"
"github.com/ledgerwatch/erigon/cl/clparams"
"github.com/ledgerwatch/erigon/cl/cltypes"
"github.com/ledgerwatch/erigon/cl/phase1/forkchoice"
"github.com/ledgerwatch/erigon/cl/phase1/forkchoice/mock_services"
"github.com/ledgerwatch/erigon/cl/sentinel/communication"
"github.com/ledgerwatch/erigon/cl/sentinel/communication/ssz_snappy"
"github.com/ledgerwatch/erigon/cl/sentinel/peers"
"github.com/ledgerwatch/erigon/cl/utils"
"github.com/libp2p/go-libp2p"
"github.com/libp2p/go-libp2p/core/peer"
"github.com/libp2p/go-libp2p/core/protocol"
"github.com/stretchr/testify/require"
)

func TestBlocksByRootHandler(t *testing.T) {
Expand Down Expand Up @@ -66,7 +67,7 @@ func TestBlocksByRootHandler(t *testing.T) {
nil,
beaconCfg,
ethClock,
nil, &forkchoice.ForkChoiceStorageMock{}, nil, true,
nil, &mock_services.ForkChoiceStorageMock{}, nil, true,
)
c.Start()
req := &cltypes.BeaconBlocksByRangeRequest{
Expand Down
13 changes: 7 additions & 6 deletions cl/sentinel/handlers/blocks_by_root_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,21 +9,22 @@ import (
"testing"

"github.com/golang/snappy"
"github.com/libp2p/go-libp2p"
"github.com/libp2p/go-libp2p/core/peer"
"github.com/libp2p/go-libp2p/core/protocol"
"github.com/stretchr/testify/require"

libcommon "github.com/ledgerwatch/erigon-lib/common"
"github.com/ledgerwatch/erigon/cl/antiquary/tests"
"github.com/ledgerwatch/erigon/cl/clparams"
"github.com/ledgerwatch/erigon/cl/cltypes"
"github.com/ledgerwatch/erigon/cl/cltypes/solid"
"github.com/ledgerwatch/erigon/cl/persistence/beacon_indicies"
"github.com/ledgerwatch/erigon/cl/phase1/forkchoice"
"github.com/ledgerwatch/erigon/cl/phase1/forkchoice/mock_services"
"github.com/ledgerwatch/erigon/cl/sentinel/communication"
"github.com/ledgerwatch/erigon/cl/sentinel/communication/ssz_snappy"
"github.com/ledgerwatch/erigon/cl/sentinel/peers"
"github.com/ledgerwatch/erigon/cl/utils"
"github.com/libp2p/go-libp2p"
"github.com/libp2p/go-libp2p/core/peer"
"github.com/libp2p/go-libp2p/core/protocol"
"github.com/stretchr/testify/require"
)

func TestBlocksByRangeHandler(t *testing.T) {
Expand Down Expand Up @@ -69,7 +70,7 @@ func TestBlocksByRangeHandler(t *testing.T) {
nil,
beaconCfg,
ethClock,
nil, &forkchoice.ForkChoiceStorageMock{}, nil, true,
nil, &mock_services.ForkChoiceStorageMock{}, nil, true,
)
c.Start()
var req solid.HashListSSZ = solid.NewHashList(len(expBlocks))
Expand Down
Loading

0 comments on commit 63578a8

Please sign in to comment.