diff --git a/Makefile b/Makefile index e1303d5..ca24997 100644 --- a/Makefile +++ b/Makefile @@ -4,6 +4,7 @@ include version.mk CHECK_GO := $(shell command -v go 2> /dev/null) CHECK_CURL := $(shell command -v curl 2> /dev/null) CHECK_DOCKER := $(shell command -v docker 2> /dev/null) +CHECK_MOCKERY := $(shell command -v mockery 2> /dev/null) check-go: ifndef CHECK_GO @@ -20,6 +21,11 @@ ifndef CHECK_DOCKER $(error "Docker is not installed. Please install Docker and retry.") endif +check-mockery: +ifndef CHECK_MOCKERY + $(error "Mockery is not installed. Please install Mockery and retry.") +endif + # Targets that require the checks build: check-go build-docker: check-docker @@ -32,6 +38,7 @@ install-linter: check-go check-curl lint: check-go unit-tests: check-go e2e-tests: check-go +generate-mocks: check-go check-mockery ARCH := $(shell uname -m) @@ -110,3 +117,7 @@ e2e-tests: ## Runs E2E tests .PHONY: unit-tests unit-tests: ## Runs unit tests go test -v -timeout=5m -race -shuffle=on -coverprofile coverage.out `go list ./... | grep -v test` + +.PHONY: generate-mocks +generate-mocks: ## Generates mocks and other autogenerated types + $(GOENVVARS) go generate ./... diff --git a/etherman/etherman.go b/etherman/etherman.go index 40e6c55..e6bedb5 100644 --- a/etherman/etherman.go +++ b/etherman/etherman.go @@ -4,10 +4,11 @@ import ( "context" "errors" "fmt" - "github.com/0xPolygon/beethoven/config" "math/big" "time" + "github.com/0xPolygon/beethoven/config" + "github.com/0xPolygonHermez/zkevm-node/etherman/smartcontracts/polygonrollupmanager" "github.com/0xPolygonHermez/zkevm-node/etherman/smartcontracts/polygonzkevm" "github.com/0xPolygonHermez/zkevm-node/log" @@ -28,12 +29,12 @@ const ( ) type Etherman struct { - ethClient EthereumClient + ethClient IEthereumClient auth bind.TransactOpts config *config.Config } -func New(ethClient EthereumClient, auth bind.TransactOpts, cfg *config.Config) (Etherman, error) { +func New(ethClient IEthereumClient, auth bind.TransactOpts, cfg *config.Config) (Etherman, error) { return Etherman{ ethClient: ethClient, auth: auth, diff --git a/etherman/etherman_test.go b/etherman/etherman_test.go index 6944c81..777f19f 100644 --- a/etherman/etherman_test.go +++ b/etherman/etherman_test.go @@ -3,12 +3,13 @@ package etherman import ( "context" "errors" + "math/big" + "testing" + "github.com/0xPolygon/beethoven/config" cdkTypes "github.com/0xPolygon/beethoven/rpc/types" "github.com/0xPolygon/beethoven/tx" "github.com/ethereum/go-ethereum/crypto" - "math/big" - "testing" "github.com/0xPolygon/beethoven/mocks" "github.com/ethereum/go-ethereum" @@ -23,7 +24,7 @@ func signer(from common.Address, tx *types.Transaction) (*types.Transaction, err return tx, nil } -func getEtherman(ethClientMock EthereumClient) Etherman { +func getEtherman(ethClientMock IEthereumClient) Etherman { ethman, _ := New( ethClientMock, bind.TransactOpts{ @@ -50,7 +51,7 @@ func TestGetSequencerAddr(t *testing.T) { t.Run("Returns expected error on 'TrustedSequencer' call (improperly formatted output)", func(t *testing.T) { t.Parallel() - ethClient := new(mocks.EthereumClientMock) + ethClient := mocks.NewEthereumClientMock(t) ethman := getEtherman(ethClient) ethClient.On( @@ -100,7 +101,7 @@ func TestGetSequencerAddr(t *testing.T) { t.Run("Returns expected error on 'RollupIDToRollupData' call (improperly formatted output)", func(t *testing.T) { t.Parallel() - ethClient := new(mocks.EthereumClientMock) + ethClient := mocks.NewEthereumClientMock(t) ethman := getEtherman(ethClient) ethClient.On( @@ -131,7 +132,7 @@ func TestGetSequencerAddr(t *testing.T) { t.Run("Returns expected sequencer address", func(t *testing.T) { t.Parallel() - ethClient := new(mocks.EthereumClientMock) + ethClient := mocks.NewEthereumClientMock(t) ethman := getEtherman(ethClient) ethClient.On( @@ -178,7 +179,7 @@ func TestGetSequencerAddr(t *testing.T) { func TestBuildTrustedVerifyBatches(t *testing.T) { t.Parallel() assert := assert.New(t) - ethman := getEtherman(new(mocks.EthereumClientMock)) + ethman := getEtherman(mocks.NewEthereumClientMock(t)) // Because we cant mock the ABI dependency is this the only test case that we somehow // can have here in a unit test. Further test coverage can get achieved with e2e or integration tests. @@ -204,7 +205,7 @@ func TestCallContract(t *testing.T) { assert := assert.New(t) t.Run("Returns expected value", func(t *testing.T) { - ethClient := new(mocks.EthereumClientMock) + ethClient := mocks.NewEthereumClientMock(t) ethman := getEtherman(ethClient) ethClient.On( @@ -247,7 +248,7 @@ func TestCallContract(t *testing.T) { }) t.Run("Returns expected error", func(t *testing.T) { - ethClient := new(mocks.EthereumClientMock) + ethClient := mocks.NewEthereumClientMock(t) ethman := getEtherman(ethClient) ethClient.On( @@ -295,7 +296,7 @@ func TestCheckTxWasMined(t *testing.T) { assert := assert.New(t) t.Run("Returns expected error on 'ethereum.NotFound'", func(t *testing.T) { - ethClient := new(mocks.EthereumClientMock) + ethClient := mocks.NewEthereumClientMock(t) ethman := getEtherman(ethClient) ethClient.On( @@ -316,7 +317,7 @@ func TestCheckTxWasMined(t *testing.T) { }) t.Run("Returns expected error", func(t *testing.T) { - ethClient := new(mocks.EthereumClientMock) + ethClient := mocks.NewEthereumClientMock(t) ethman := getEtherman(ethClient) ethClient.On( @@ -337,7 +338,7 @@ func TestCheckTxWasMined(t *testing.T) { }) t.Run("Returns the expected values", func(t *testing.T) { - ethClient := new(mocks.EthereumClientMock) + ethClient := mocks.NewEthereumClientMock(t) ethman := getEtherman(ethClient) ethClient.On( @@ -363,7 +364,7 @@ func TestCurrentNonce(t *testing.T) { assert := assert.New(t) t.Run("Returns the expected nonce value", func(t *testing.T) { - ethClient := new(mocks.EthereumClientMock) + ethClient := mocks.NewEthereumClientMock(t) ethman := getEtherman(ethClient) ethClient.On( @@ -384,7 +385,7 @@ func TestCurrentNonce(t *testing.T) { }) t.Run("Returns the expected error", func(t *testing.T) { - ethClient := new(mocks.EthereumClientMock) + ethClient := mocks.NewEthereumClientMock(t) ethman := getEtherman(ethClient) ethClient.On( @@ -410,7 +411,7 @@ func TestGetTx(t *testing.T) { assert := assert.New(t) t.Run("Returns the expected transaction", func(t *testing.T) { - ethClient := new(mocks.EthereumClientMock) + ethClient := mocks.NewEthereumClientMock(t) ethman := getEtherman(ethClient) ethClient.On( @@ -432,7 +433,7 @@ func TestGetTx(t *testing.T) { }) t.Run("Returns the expected error", func(t *testing.T) { - ethClient := new(mocks.EthereumClientMock) + ethClient := mocks.NewEthereumClientMock(t) ethman := getEtherman(ethClient) ethClient.On( @@ -459,7 +460,7 @@ func TestGetTxReceipt(t *testing.T) { assert := assert.New(t) t.Run("Returns expected receipt", func(t *testing.T) { - ethClient := new(mocks.EthereumClientMock) + ethClient := mocks.NewEthereumClientMock(t) ethman := getEtherman(ethClient) ethClient.On( @@ -479,7 +480,7 @@ func TestGetTxReceipt(t *testing.T) { }) t.Run("Returns expected error", func(t *testing.T) { - ethClient := new(mocks.EthereumClientMock) + ethClient := mocks.NewEthereumClientMock(t) ethman := getEtherman(ethClient) ethClient.On( @@ -512,7 +513,7 @@ func TestSendTx(t *testing.T) { ) t.Run("Returns expected value", func(t *testing.T) { - ethClient := new(mocks.EthereumClientMock) + ethClient := mocks.NewEthereumClientMock(t) ethman := getEtherman(ethClient) ethClient.On( @@ -530,7 +531,7 @@ func TestSendTx(t *testing.T) { }) t.Run("Returns expected error", func(t *testing.T) { - ethClient := new(mocks.EthereumClientMock) + ethClient := mocks.NewEthereumClientMock(t) ethman := getEtherman(ethClient) ethClient.On( @@ -553,7 +554,7 @@ func TestSuggestedGasPrice(t *testing.T) { assert := assert.New(t) t.Run("Returns expected value", func(t *testing.T) { - ethClient := new(mocks.EthereumClientMock) + ethClient := mocks.NewEthereumClientMock(t) ethman := getEtherman(ethClient) ethClient.On( @@ -572,7 +573,7 @@ func TestSuggestedGasPrice(t *testing.T) { }) t.Run("Returns expected error", func(t *testing.T) { - ethClient := new(mocks.EthereumClientMock) + ethClient := mocks.NewEthereumClientMock(t) ethman := getEtherman(ethClient) ethClient.On( @@ -596,7 +597,7 @@ func TestEstimateGas(t *testing.T) { assert := assert.New(t) t.Run("Returns the expected value", func(t *testing.T) { - ethclient := new(mocks.EthereumClientMock) + ethclient := mocks.NewEthereumClientMock(t) ethman := getEtherman(ethclient) ethclient.On( @@ -627,7 +628,7 @@ func TestEstimateGas(t *testing.T) { }) t.Run("Returns the expected error", func(t *testing.T) { - ethclient := new(mocks.EthereumClientMock) + ethclient := mocks.NewEthereumClientMock(t) ethman := getEtherman(ethclient) ethclient.On( @@ -671,7 +672,7 @@ func TestSignTx(t *testing.T) { ) t.Run("Returns the expected value", func(t *testing.T) { - ethClient := new(mocks.EthereumClientMock) + ethClient := mocks.NewEthereumClientMock(t) ethman := getEtherman(ethClient) transaction, err := ethman.SignTx(context.TODO(), common.Address{}, txData) @@ -694,7 +695,7 @@ func TestGetRevertMessage(t *testing.T) { ) t.Run("Returns an empty string and the expected error", func(t *testing.T) { - ethClient := new(mocks.EthereumClientMock) + ethClient := mocks.NewEthereumClientMock(t) ethman := getEtherman(ethClient) ethClient.On( @@ -713,7 +714,7 @@ func TestGetRevertMessage(t *testing.T) { }) t.Run("Returns an empty string and the error set to nil", func(t *testing.T) { - ethClient := new(mocks.EthereumClientMock) + ethClient := mocks.NewEthereumClientMock(t) ethman := getEtherman(ethClient) ethClient.On( @@ -734,7 +735,7 @@ func TestGetRevertMessage(t *testing.T) { }) t.Run("Returns the expected revert reason string", func(t *testing.T) { - ethClient := new(mocks.EthereumClientMock) + ethClient := mocks.NewEthereumClientMock(t) ethman := getEtherman(ethClient) key, _ := crypto.GenerateKey() @@ -784,7 +785,7 @@ func TestGetLastBlock(t *testing.T) { assert := assert.New(t) t.Run("Returns the expected values", func(t *testing.T) { - ethClient := new(mocks.EthereumClientMock) + ethClient := mocks.NewEthereumClientMock(t) ethman := getEtherman(ethClient) ethClient.On( @@ -811,7 +812,7 @@ func TestGetLastBlock(t *testing.T) { }) t.Run("Returns the expected error", func(t *testing.T) { - ethClient := new(mocks.EthereumClientMock) + ethClient := mocks.NewEthereumClientMock(t) ethman := getEtherman(ethClient) ethClient.On( diff --git a/etherman/interfaces.go b/etherman/interfaces.go index 9287bfb..c926d90 100644 --- a/etherman/interfaces.go +++ b/etherman/interfaces.go @@ -5,7 +5,8 @@ import ( "github.com/ethereum/go-ethereum/accounts/abi/bind" ) -type EthereumClient interface { +//go:generate mockery --name IEthereumClient --structname EthereumClientMock --output ../mocks --case=underscore --filename etherman_client.generated.go +type IEthereumClient interface { ethereum.ChainReader ethereum.ChainStateReader ethereum.ContractCaller diff --git a/interop/executor.go b/interop/executor.go index 9c9d16c..67d6d13 100644 --- a/interop/executor.go +++ b/interop/executor.go @@ -18,11 +18,11 @@ import ( "github.com/jackc/pgx/v4" ) -var _ types.ZkEVMClientClientCreator = (*zkEVMClientCreator)(nil) +var _ types.IZkEVMClientClientCreator = (*zkEVMClientCreator)(nil) type zkEVMClientCreator struct{} -func (zc *zkEVMClientCreator) NewClient(rpc string) types.ZkEVMClientInterface { +func (zc *zkEVMClientCreator) NewClient(rpc string) types.IZkEVMClient { return client.NewClient(rpc) } @@ -30,15 +30,15 @@ type Executor struct { logger *log.Logger interopAdminAddr common.Address config *config.Config - ethTxMan types.EthTxManager - etherman types.EthermanInterface - ZkEVMClientCreator types.ZkEVMClientClientCreator + ethTxMan types.IEthTxManager + etherman types.IEtherman + ZkEVMClientCreator types.IZkEVMClientClientCreator } func New(logger *log.Logger, cfg *config.Config, interopAdminAddr common.Address, - etherman types.EthermanInterface, - ethTxManager types.EthTxManager, + etherman types.IEtherman, + ethTxManager types.IEthTxManager, ) *Executor { return &Executor{ logger: logger, diff --git a/interop/executor_test.go b/interop/executor_test.go index 63bbdb1..4ea0830 100644 --- a/interop/executor_test.go +++ b/interop/executor_test.go @@ -23,8 +23,8 @@ import ( func TestNewExecutor(t *testing.T) { cfg := &config.Config{} interopAdminAddr := common.HexToAddress("0x1234567890abcdef") - etherman := &mocks.EthermanMock{} - ethTxManager := &mocks.EthTxManagerMock{} + etherman := mocks.NewEthermanMock(t) + ethTxManager := mocks.NewEthTxManagerMock(t) executor := New(nil, cfg, interopAdminAddr, etherman, ethTxManager) @@ -43,8 +43,8 @@ func TestExecutor_CheckTx(t *testing.T) { }, } interopAdminAddr := common.HexToAddress("0x1234567890abcdef") - etherman := &mocks.EthermanMock{} - ethTxManager := &mocks.EthTxManagerMock{} + etherman := mocks.NewEthermanMock(t) + ethTxManager := mocks.NewEthTxManagerMock(t) executor := New(log.WithFields("test", "test"), cfg, interopAdminAddr, etherman, ethTxManager) @@ -81,8 +81,8 @@ func TestExecutor_CheckTx(t *testing.T) { func TestExecutor_VerifyZKP(t *testing.T) { cfg := &config.Config{} interopAdminAddr := common.HexToAddress("0x1234567890abcdef") - etherman := &mocks.EthermanMock{} - ethTxManager := &mocks.EthTxManagerMock{} + etherman := mocks.NewEthermanMock(t) + ethTxManager := mocks.NewEthTxManagerMock(t) tnx := tx.Tx{ LastVerifiedBatch: 0, NewVerifiedBatch: 1, @@ -128,8 +128,8 @@ func TestExecutor_VerifyZKP(t *testing.T) { func TestExecutor_VerifySignature(t *testing.T) { cfg := &config.Config{} interopAdminAddr := common.HexToAddress("0x1234567890abcdef") - etherman := &mocks.EthermanMock{} - ethTxManager := &mocks.EthTxManagerMock{} + etherman := mocks.NewEthermanMock(t) + ethTxManager := mocks.NewEthTxManagerMock(t) executor := New(nil, cfg, interopAdminAddr, etherman, ethTxManager) @@ -164,8 +164,8 @@ func TestExecutor_VerifySignature(t *testing.T) { func TestExecutor_Execute(t *testing.T) { cfg := &config.Config{} interopAdminAddr := common.HexToAddress("0x1234567890abcdef") - etherman := &mocks.EthermanMock{} - ethTxManager := &mocks.EthTxManagerMock{} + etherman := mocks.NewEthermanMock(t) + ethTxManager := mocks.NewEthTxManagerMock(t) executor := New(log.WithFields("test", "test"), cfg, interopAdminAddr, etherman, ethTxManager) @@ -182,8 +182,8 @@ func TestExecutor_Execute(t *testing.T) { } // Mock the ZkEVMClientCreator.NewClient method - mockZkEVMClientCreator := &mocks.ZkEVMClientCreatorMock{} - mockZkEVMClient := &mocks.ZkEVMClientMock{} + mockZkEVMClientCreator := mocks.NewZkEVMClientClientCreatorMock(t) + mockZkEVMClient := mocks.NewZkEVMClientMock(t) mockZkEVMClientCreator.On("NewClient", mock.Anything).Return(mockZkEVMClient).Once() mockZkEVMClient.On("BatchByNumber", mock.Anything, big.NewInt(int64(signedTx.Tx.NewVerifiedBatch))). @@ -205,8 +205,8 @@ func TestExecutor_Execute(t *testing.T) { func TestExecutor_Settle(t *testing.T) { cfg := &config.Config{} interopAdminAddr := common.HexToAddress("0x1234567890abcdef") - etherman := &mocks.EthermanMock{} - ethTxManager := &mocks.EthTxManagerMock{} + etherman := mocks.NewEthermanMock(t) + ethTxManager := mocks.NewEthTxManagerMock(t) dbTx := &mocks.TxMock{} executor := New(nil, cfg, interopAdminAddr, etherman, ethTxManager) @@ -261,8 +261,8 @@ func TestExecutor_Settle(t *testing.T) { func TestExecutor_GetTxStatus(t *testing.T) { cfg := &config.Config{} interopAdminAddr := common.HexToAddress("0x1234567890abcdef") - etherman := &mocks.EthermanMock{} - ethTxManager := &mocks.EthTxManagerMock{} + etherman := mocks.NewEthermanMock(t) + ethTxManager := mocks.NewEthTxManagerMock(t) dbTx := &mocks.TxMock{} executor := New(nil, cfg, interopAdminAddr, etherman, ethTxManager) diff --git a/mocks/db.generated.go b/mocks/db.generated.go new file mode 100644 index 0000000..e950eb1 --- /dev/null +++ b/mocks/db.generated.go @@ -0,0 +1,59 @@ +// Code generated by mockery v2.38.0. DO NOT EDIT. + +package mocks + +import ( + context "context" + + pgx "github.com/jackc/pgx/v4" + mock "github.com/stretchr/testify/mock" +) + +// DBMock is an autogenerated mock type for the IDB type +type DBMock struct { + mock.Mock +} + +// BeginStateTransaction provides a mock function with given fields: ctx +func (_m *DBMock) BeginStateTransaction(ctx context.Context) (pgx.Tx, error) { + ret := _m.Called(ctx) + + if len(ret) == 0 { + panic("no return value specified for BeginStateTransaction") + } + + var r0 pgx.Tx + var r1 error + if rf, ok := ret.Get(0).(func(context.Context) (pgx.Tx, error)); ok { + return rf(ctx) + } + if rf, ok := ret.Get(0).(func(context.Context) pgx.Tx); ok { + r0 = rf(ctx) + } else { + if ret.Get(0) != nil { + r0 = ret.Get(0).(pgx.Tx) + } + } + + if rf, ok := ret.Get(1).(func(context.Context) error); ok { + r1 = rf(ctx) + } else { + r1 = ret.Error(1) + } + + return r0, r1 +} + +// NewDBMock creates a new instance of DBMock. It also registers a testing interface on the mock and a cleanup function to assert the mocks expectations. +// The first argument is typically a *testing.T value. +func NewDBMock(t interface { + mock.TestingT + Cleanup(func()) +}) *DBMock { + mock := &DBMock{} + mock.Mock.Test(t) + + t.Cleanup(func() { mock.AssertExpectations(t) }) + + return mock +} diff --git a/mocks/eth_tx_manager.generated.go b/mocks/eth_tx_manager.generated.go new file mode 100644 index 0000000..d83c45e --- /dev/null +++ b/mocks/eth_tx_manager.generated.go @@ -0,0 +1,116 @@ +// Code generated by mockery v2.38.0. DO NOT EDIT. + +package mocks + +import ( + context "context" + big "math/big" + + common "github.com/ethereum/go-ethereum/common" + + ethtxmanager "github.com/0xPolygonHermez/zkevm-node/ethtxmanager" + + mock "github.com/stretchr/testify/mock" + + pgx "github.com/jackc/pgx/v4" +) + +// EthTxManagerMock is an autogenerated mock type for the IEthTxManager type +type EthTxManagerMock struct { + mock.Mock +} + +// Add provides a mock function with given fields: ctx, owner, id, from, to, value, data, gasOffset, dbTx +func (_m *EthTxManagerMock) Add(ctx context.Context, owner string, id string, from common.Address, to *common.Address, value *big.Int, data []byte, gasOffset uint64, dbTx pgx.Tx) error { + ret := _m.Called(ctx, owner, id, from, to, value, data, gasOffset, dbTx) + + if len(ret) == 0 { + panic("no return value specified for Add") + } + + var r0 error + if rf, ok := ret.Get(0).(func(context.Context, string, string, common.Address, *common.Address, *big.Int, []byte, uint64, pgx.Tx) error); ok { + r0 = rf(ctx, owner, id, from, to, value, data, gasOffset, dbTx) + } else { + r0 = ret.Error(0) + } + + return r0 +} + +// ProcessPendingMonitoredTxs provides a mock function with given fields: ctx, owner, failedResultHandler, dbTx +func (_m *EthTxManagerMock) ProcessPendingMonitoredTxs(ctx context.Context, owner string, failedResultHandler ethtxmanager.ResultHandler, dbTx pgx.Tx) { + _m.Called(ctx, owner, failedResultHandler, dbTx) +} + +// Result provides a mock function with given fields: ctx, owner, id, dbTx +func (_m *EthTxManagerMock) Result(ctx context.Context, owner string, id string, dbTx pgx.Tx) (ethtxmanager.MonitoredTxResult, error) { + ret := _m.Called(ctx, owner, id, dbTx) + + if len(ret) == 0 { + panic("no return value specified for Result") + } + + var r0 ethtxmanager.MonitoredTxResult + var r1 error + if rf, ok := ret.Get(0).(func(context.Context, string, string, pgx.Tx) (ethtxmanager.MonitoredTxResult, error)); ok { + return rf(ctx, owner, id, dbTx) + } + if rf, ok := ret.Get(0).(func(context.Context, string, string, pgx.Tx) ethtxmanager.MonitoredTxResult); ok { + r0 = rf(ctx, owner, id, dbTx) + } else { + r0 = ret.Get(0).(ethtxmanager.MonitoredTxResult) + } + + if rf, ok := ret.Get(1).(func(context.Context, string, string, pgx.Tx) error); ok { + r1 = rf(ctx, owner, id, dbTx) + } else { + r1 = ret.Error(1) + } + + return r0, r1 +} + +// ResultsByStatus provides a mock function with given fields: ctx, owner, statuses, dbTx +func (_m *EthTxManagerMock) ResultsByStatus(ctx context.Context, owner string, statuses []ethtxmanager.MonitoredTxStatus, dbTx pgx.Tx) ([]ethtxmanager.MonitoredTxResult, error) { + ret := _m.Called(ctx, owner, statuses, dbTx) + + if len(ret) == 0 { + panic("no return value specified for ResultsByStatus") + } + + var r0 []ethtxmanager.MonitoredTxResult + var r1 error + if rf, ok := ret.Get(0).(func(context.Context, string, []ethtxmanager.MonitoredTxStatus, pgx.Tx) ([]ethtxmanager.MonitoredTxResult, error)); ok { + return rf(ctx, owner, statuses, dbTx) + } + if rf, ok := ret.Get(0).(func(context.Context, string, []ethtxmanager.MonitoredTxStatus, pgx.Tx) []ethtxmanager.MonitoredTxResult); ok { + r0 = rf(ctx, owner, statuses, dbTx) + } else { + if ret.Get(0) != nil { + r0 = ret.Get(0).([]ethtxmanager.MonitoredTxResult) + } + } + + if rf, ok := ret.Get(1).(func(context.Context, string, []ethtxmanager.MonitoredTxStatus, pgx.Tx) error); ok { + r1 = rf(ctx, owner, statuses, dbTx) + } else { + r1 = ret.Error(1) + } + + return r0, r1 +} + +// NewEthTxManagerMock creates a new instance of EthTxManagerMock. It also registers a testing interface on the mock and a cleanup function to assert the mocks expectations. +// The first argument is typically a *testing.T value. +func NewEthTxManagerMock(t interface { + mock.TestingT + Cleanup(func()) +}) *EthTxManagerMock { + mock := &EthTxManagerMock{} + mock.Mock.Test(t) + + t.Cleanup(func() { mock.AssertExpectations(t) }) + + return mock +} diff --git a/mocks/ethclient_mock.go b/mocks/ethclient_mock.go deleted file mode 100644 index 285108e..0000000 --- a/mocks/ethclient_mock.go +++ /dev/null @@ -1,146 +0,0 @@ -package mocks - -import ( - "context" - "github.com/ethereum/go-ethereum" - "github.com/ethereum/go-ethereum/common" - "github.com/ethereum/go-ethereum/core/types" - "github.com/stretchr/testify/mock" - "math/big" -) - -type EthereumClientMock struct { - mock.Mock -} - -func (e *EthereumClientMock) BlockByHash(ctx context.Context, hash common.Hash) (*types.Block, error) { - args := e.Called(ctx, hash) - - return args.Get(0).(*types.Block), args.Error(1) -} - -func (e *EthereumClientMock) HeaderByHash(ctx context.Context, hash common.Hash) (*types.Header, error) { - args := e.Called(ctx, hash) - - return args.Get(0).(*types.Header), args.Error(1) -} - -func (e *EthereumClientMock) HeaderByNumber(ctx context.Context, number *big.Int) (*types.Header, error) { - args := e.Called(ctx, number) - - return args.Get(0).(*types.Header), args.Error(1) -} - -func (e *EthereumClientMock) TransactionCount(ctx context.Context, blockHash common.Hash) (uint, error) { - args := e.Called(ctx, blockHash) - - return args.Get(0).(uint), args.Error(1) -} - -func (e *EthereumClientMock) TransactionInBlock(ctx context.Context, blockHash common.Hash, index uint) (*types.Transaction, error) { - args := e.Called(ctx, blockHash) - - return args.Get(0).(*types.Transaction), args.Error(1) -} - -func (e *EthereumClientMock) SubscribeNewHead(ctx context.Context, ch chan<- *types.Header) (ethereum.Subscription, error) { - args := e.Called(ctx, ch) - - return args.Get(0).(ethereum.Subscription), args.Error(1) -} - -func (e *EthereumClientMock) BalanceAt(ctx context.Context, account common.Address, blockNumber *big.Int) (*big.Int, error) { - args := e.Called(ctx, account, blockNumber) - - return args.Get(0).(*big.Int), args.Error(1) -} - -func (e *EthereumClientMock) StorageAt(ctx context.Context, account common.Address, key common.Hash, blockNumber *big.Int) ([]byte, error) { - args := e.Called(ctx, account, key, blockNumber) - - return args.Get(0).([]byte), args.Error(1) -} - -func (e *EthereumClientMock) CodeAt(ctx context.Context, account common.Address, blockNumber *big.Int) ([]byte, error) { - args := e.Called(ctx, account, blockNumber) - - return args.Get(0).([]byte), args.Error(1) -} - -func (e *EthereumClientMock) FilterLogs(ctx context.Context, q ethereum.FilterQuery) ([]types.Log, error) { - args := e.Called(ctx, q) - - return args.Get(0).([]types.Log), args.Error(1) -} - -func (e *EthereumClientMock) SubscribeFilterLogs(ctx context.Context, q ethereum.FilterQuery, ch chan<- types.Log) (ethereum.Subscription, error) { - args := e.Called(ctx, q, ch) - - return args.Get(0).(ethereum.Subscription), args.Error(1) -} - -func (e *EthereumClientMock) PendingCodeAt(ctx context.Context, account common.Address) ([]byte, error) { - args := e.Called(ctx, account) - - return args.Get(0).([]byte), args.Error(1) -} - -func (e *EthereumClientMock) PendingNonceAt(ctx context.Context, account common.Address) (uint64, error) { - args := e.Called(ctx, account) - - return args.Get(0).(uint64), args.Error(1) -} - -func (e *EthereumClientMock) SuggestGasTipCap(ctx context.Context) (*big.Int, error) { - args := e.Called(ctx) - - return args.Get(0).(*big.Int), args.Error(1) -} - -func (e *EthereumClientMock) CallContract(ctx context.Context, call ethereum.CallMsg, blockNumber *big.Int) ([]byte, error) { - args := e.Called(ctx, call, blockNumber) - - return args.Get(0).([]byte), args.Error(1) -} - -func (e *EthereumClientMock) TransactionReceipt(ctx context.Context, txHash common.Hash) (*types.Receipt, error) { - args := e.Called(ctx, txHash) - - return args.Get(0).(*types.Receipt), args.Error(1) -} - -func (e *EthereumClientMock) NonceAt(ctx context.Context, account common.Address, blockNumber *big.Int) (uint64, error) { - args := e.Called(ctx, account, blockNumber) - - return args.Get(0).(uint64), args.Error(1) -} - -func (e *EthereumClientMock) TransactionByHash(ctx context.Context, txHash common.Hash) (*types.Transaction, bool, error) { - args := e.Called(ctx, txHash) - - return args.Get(0).(*types.Transaction), args.Bool(1), args.Error(2) -} - -func (e *EthereumClientMock) SendTransaction(ctx context.Context, tx *types.Transaction) error { - args := e.Called(ctx, tx) - - return args.Error(0) -} - -func (e *EthereumClientMock) SuggestGasPrice(ctx context.Context) (*big.Int, error) { - args := e.Called(ctx) - - return args.Get(0).(*big.Int), args.Error(1) -} - -func (e *EthereumClientMock) EstimateGas(ctx context.Context, call ethereum.CallMsg) (uint64, error) { - args := e.Called(ctx, call) - - return args.Get(0).(uint64), args.Error(1) -} - -func (e *EthereumClientMock) BlockByNumber(ctx context.Context, number *big.Int) (*types.Block, error) { - args := e.Called(ctx, number) - - return args.Get(0).(*types.Block), args.Error(1) -} diff --git a/mocks/etherman.generated.go b/mocks/etherman.generated.go new file mode 100644 index 0000000..ab0af5a --- /dev/null +++ b/mocks/etherman.generated.go @@ -0,0 +1,125 @@ +// Code generated by mockery v2.38.0. DO NOT EDIT. + +package mocks + +import ( + context "context" + big "math/big" + + common "github.com/ethereum/go-ethereum/common" + + ethereum "github.com/ethereum/go-ethereum" + + mock "github.com/stretchr/testify/mock" + + tx "github.com/0xPolygon/beethoven/tx" +) + +// EthermanMock is an autogenerated mock type for the IEtherman type +type EthermanMock struct { + mock.Mock +} + +// BuildTrustedVerifyBatchesTxData provides a mock function with given fields: lastVerifiedBatch, newVerifiedBatch, proof, rollupId +func (_m *EthermanMock) BuildTrustedVerifyBatchesTxData(lastVerifiedBatch uint64, newVerifiedBatch uint64, proof tx.ZKP, rollupId uint32) ([]byte, error) { + ret := _m.Called(lastVerifiedBatch, newVerifiedBatch, proof, rollupId) + + if len(ret) == 0 { + panic("no return value specified for BuildTrustedVerifyBatchesTxData") + } + + var r0 []byte + var r1 error + if rf, ok := ret.Get(0).(func(uint64, uint64, tx.ZKP, uint32) ([]byte, error)); ok { + return rf(lastVerifiedBatch, newVerifiedBatch, proof, rollupId) + } + if rf, ok := ret.Get(0).(func(uint64, uint64, tx.ZKP, uint32) []byte); ok { + r0 = rf(lastVerifiedBatch, newVerifiedBatch, proof, rollupId) + } else { + if ret.Get(0) != nil { + r0 = ret.Get(0).([]byte) + } + } + + if rf, ok := ret.Get(1).(func(uint64, uint64, tx.ZKP, uint32) error); ok { + r1 = rf(lastVerifiedBatch, newVerifiedBatch, proof, rollupId) + } else { + r1 = ret.Error(1) + } + + return r0, r1 +} + +// CallContract provides a mock function with given fields: ctx, call, blockNumber +func (_m *EthermanMock) CallContract(ctx context.Context, call ethereum.CallMsg, blockNumber *big.Int) ([]byte, error) { + ret := _m.Called(ctx, call, blockNumber) + + if len(ret) == 0 { + panic("no return value specified for CallContract") + } + + var r0 []byte + var r1 error + if rf, ok := ret.Get(0).(func(context.Context, ethereum.CallMsg, *big.Int) ([]byte, error)); ok { + return rf(ctx, call, blockNumber) + } + if rf, ok := ret.Get(0).(func(context.Context, ethereum.CallMsg, *big.Int) []byte); ok { + r0 = rf(ctx, call, blockNumber) + } else { + if ret.Get(0) != nil { + r0 = ret.Get(0).([]byte) + } + } + + if rf, ok := ret.Get(1).(func(context.Context, ethereum.CallMsg, *big.Int) error); ok { + r1 = rf(ctx, call, blockNumber) + } else { + r1 = ret.Error(1) + } + + return r0, r1 +} + +// GetSequencerAddr provides a mock function with given fields: rollupId +func (_m *EthermanMock) GetSequencerAddr(rollupId uint32) (common.Address, error) { + ret := _m.Called(rollupId) + + if len(ret) == 0 { + panic("no return value specified for GetSequencerAddr") + } + + var r0 common.Address + var r1 error + if rf, ok := ret.Get(0).(func(uint32) (common.Address, error)); ok { + return rf(rollupId) + } + if rf, ok := ret.Get(0).(func(uint32) common.Address); ok { + r0 = rf(rollupId) + } else { + if ret.Get(0) != nil { + r0 = ret.Get(0).(common.Address) + } + } + + if rf, ok := ret.Get(1).(func(uint32) error); ok { + r1 = rf(rollupId) + } else { + r1 = ret.Error(1) + } + + return r0, r1 +} + +// NewEthermanMock creates a new instance of EthermanMock. It also registers a testing interface on the mock and a cleanup function to assert the mocks expectations. +// The first argument is typically a *testing.T value. +func NewEthermanMock(t interface { + mock.TestingT + Cleanup(func()) +}) *EthermanMock { + mock := &EthermanMock{} + mock.Mock.Test(t) + + t.Cleanup(func() { mock.AssertExpectations(t) }) + + return mock +} diff --git a/mocks/etherman_client.generated.go b/mocks/etherman_client.generated.go new file mode 100644 index 0000000..3dfb2e6 --- /dev/null +++ b/mocks/etherman_client.generated.go @@ -0,0 +1,682 @@ +// Code generated by mockery v2.38.0. DO NOT EDIT. + +package mocks + +import ( + context "context" + big "math/big" + + common "github.com/ethereum/go-ethereum/common" + + ethereum "github.com/ethereum/go-ethereum" + + mock "github.com/stretchr/testify/mock" + + types "github.com/ethereum/go-ethereum/core/types" +) + +// EthereumClientMock is an autogenerated mock type for the IEthereumClient type +type EthereumClientMock struct { + mock.Mock +} + +// BalanceAt provides a mock function with given fields: ctx, account, blockNumber +func (_m *EthereumClientMock) BalanceAt(ctx context.Context, account common.Address, blockNumber *big.Int) (*big.Int, error) { + ret := _m.Called(ctx, account, blockNumber) + + if len(ret) == 0 { + panic("no return value specified for BalanceAt") + } + + var r0 *big.Int + var r1 error + if rf, ok := ret.Get(0).(func(context.Context, common.Address, *big.Int) (*big.Int, error)); ok { + return rf(ctx, account, blockNumber) + } + if rf, ok := ret.Get(0).(func(context.Context, common.Address, *big.Int) *big.Int); ok { + r0 = rf(ctx, account, blockNumber) + } else { + if ret.Get(0) != nil { + r0 = ret.Get(0).(*big.Int) + } + } + + if rf, ok := ret.Get(1).(func(context.Context, common.Address, *big.Int) error); ok { + r1 = rf(ctx, account, blockNumber) + } else { + r1 = ret.Error(1) + } + + return r0, r1 +} + +// BlockByHash provides a mock function with given fields: ctx, hash +func (_m *EthereumClientMock) BlockByHash(ctx context.Context, hash common.Hash) (*types.Block, error) { + ret := _m.Called(ctx, hash) + + if len(ret) == 0 { + panic("no return value specified for BlockByHash") + } + + var r0 *types.Block + var r1 error + if rf, ok := ret.Get(0).(func(context.Context, common.Hash) (*types.Block, error)); ok { + return rf(ctx, hash) + } + if rf, ok := ret.Get(0).(func(context.Context, common.Hash) *types.Block); ok { + r0 = rf(ctx, hash) + } else { + if ret.Get(0) != nil { + r0 = ret.Get(0).(*types.Block) + } + } + + if rf, ok := ret.Get(1).(func(context.Context, common.Hash) error); ok { + r1 = rf(ctx, hash) + } else { + r1 = ret.Error(1) + } + + return r0, r1 +} + +// BlockByNumber provides a mock function with given fields: ctx, number +func (_m *EthereumClientMock) BlockByNumber(ctx context.Context, number *big.Int) (*types.Block, error) { + ret := _m.Called(ctx, number) + + if len(ret) == 0 { + panic("no return value specified for BlockByNumber") + } + + var r0 *types.Block + var r1 error + if rf, ok := ret.Get(0).(func(context.Context, *big.Int) (*types.Block, error)); ok { + return rf(ctx, number) + } + if rf, ok := ret.Get(0).(func(context.Context, *big.Int) *types.Block); ok { + r0 = rf(ctx, number) + } else { + if ret.Get(0) != nil { + r0 = ret.Get(0).(*types.Block) + } + } + + if rf, ok := ret.Get(1).(func(context.Context, *big.Int) error); ok { + r1 = rf(ctx, number) + } else { + r1 = ret.Error(1) + } + + return r0, r1 +} + +// CallContract provides a mock function with given fields: ctx, call, blockNumber +func (_m *EthereumClientMock) CallContract(ctx context.Context, call ethereum.CallMsg, blockNumber *big.Int) ([]byte, error) { + ret := _m.Called(ctx, call, blockNumber) + + if len(ret) == 0 { + panic("no return value specified for CallContract") + } + + var r0 []byte + var r1 error + if rf, ok := ret.Get(0).(func(context.Context, ethereum.CallMsg, *big.Int) ([]byte, error)); ok { + return rf(ctx, call, blockNumber) + } + if rf, ok := ret.Get(0).(func(context.Context, ethereum.CallMsg, *big.Int) []byte); ok { + r0 = rf(ctx, call, blockNumber) + } else { + if ret.Get(0) != nil { + r0 = ret.Get(0).([]byte) + } + } + + if rf, ok := ret.Get(1).(func(context.Context, ethereum.CallMsg, *big.Int) error); ok { + r1 = rf(ctx, call, blockNumber) + } else { + r1 = ret.Error(1) + } + + return r0, r1 +} + +// CodeAt provides a mock function with given fields: ctx, account, blockNumber +func (_m *EthereumClientMock) CodeAt(ctx context.Context, account common.Address, blockNumber *big.Int) ([]byte, error) { + ret := _m.Called(ctx, account, blockNumber) + + if len(ret) == 0 { + panic("no return value specified for CodeAt") + } + + var r0 []byte + var r1 error + if rf, ok := ret.Get(0).(func(context.Context, common.Address, *big.Int) ([]byte, error)); ok { + return rf(ctx, account, blockNumber) + } + if rf, ok := ret.Get(0).(func(context.Context, common.Address, *big.Int) []byte); ok { + r0 = rf(ctx, account, blockNumber) + } else { + if ret.Get(0) != nil { + r0 = ret.Get(0).([]byte) + } + } + + if rf, ok := ret.Get(1).(func(context.Context, common.Address, *big.Int) error); ok { + r1 = rf(ctx, account, blockNumber) + } else { + r1 = ret.Error(1) + } + + return r0, r1 +} + +// EstimateGas provides a mock function with given fields: ctx, call +func (_m *EthereumClientMock) EstimateGas(ctx context.Context, call ethereum.CallMsg) (uint64, error) { + ret := _m.Called(ctx, call) + + if len(ret) == 0 { + panic("no return value specified for EstimateGas") + } + + var r0 uint64 + var r1 error + if rf, ok := ret.Get(0).(func(context.Context, ethereum.CallMsg) (uint64, error)); ok { + return rf(ctx, call) + } + if rf, ok := ret.Get(0).(func(context.Context, ethereum.CallMsg) uint64); ok { + r0 = rf(ctx, call) + } else { + r0 = ret.Get(0).(uint64) + } + + if rf, ok := ret.Get(1).(func(context.Context, ethereum.CallMsg) error); ok { + r1 = rf(ctx, call) + } else { + r1 = ret.Error(1) + } + + return r0, r1 +} + +// FilterLogs provides a mock function with given fields: ctx, q +func (_m *EthereumClientMock) FilterLogs(ctx context.Context, q ethereum.FilterQuery) ([]types.Log, error) { + ret := _m.Called(ctx, q) + + if len(ret) == 0 { + panic("no return value specified for FilterLogs") + } + + var r0 []types.Log + var r1 error + if rf, ok := ret.Get(0).(func(context.Context, ethereum.FilterQuery) ([]types.Log, error)); ok { + return rf(ctx, q) + } + if rf, ok := ret.Get(0).(func(context.Context, ethereum.FilterQuery) []types.Log); ok { + r0 = rf(ctx, q) + } else { + if ret.Get(0) != nil { + r0 = ret.Get(0).([]types.Log) + } + } + + if rf, ok := ret.Get(1).(func(context.Context, ethereum.FilterQuery) error); ok { + r1 = rf(ctx, q) + } else { + r1 = ret.Error(1) + } + + return r0, r1 +} + +// HeaderByHash provides a mock function with given fields: ctx, hash +func (_m *EthereumClientMock) HeaderByHash(ctx context.Context, hash common.Hash) (*types.Header, error) { + ret := _m.Called(ctx, hash) + + if len(ret) == 0 { + panic("no return value specified for HeaderByHash") + } + + var r0 *types.Header + var r1 error + if rf, ok := ret.Get(0).(func(context.Context, common.Hash) (*types.Header, error)); ok { + return rf(ctx, hash) + } + if rf, ok := ret.Get(0).(func(context.Context, common.Hash) *types.Header); ok { + r0 = rf(ctx, hash) + } else { + if ret.Get(0) != nil { + r0 = ret.Get(0).(*types.Header) + } + } + + if rf, ok := ret.Get(1).(func(context.Context, common.Hash) error); ok { + r1 = rf(ctx, hash) + } else { + r1 = ret.Error(1) + } + + return r0, r1 +} + +// HeaderByNumber provides a mock function with given fields: ctx, number +func (_m *EthereumClientMock) HeaderByNumber(ctx context.Context, number *big.Int) (*types.Header, error) { + ret := _m.Called(ctx, number) + + if len(ret) == 0 { + panic("no return value specified for HeaderByNumber") + } + + var r0 *types.Header + var r1 error + if rf, ok := ret.Get(0).(func(context.Context, *big.Int) (*types.Header, error)); ok { + return rf(ctx, number) + } + if rf, ok := ret.Get(0).(func(context.Context, *big.Int) *types.Header); ok { + r0 = rf(ctx, number) + } else { + if ret.Get(0) != nil { + r0 = ret.Get(0).(*types.Header) + } + } + + if rf, ok := ret.Get(1).(func(context.Context, *big.Int) error); ok { + r1 = rf(ctx, number) + } else { + r1 = ret.Error(1) + } + + return r0, r1 +} + +// NonceAt provides a mock function with given fields: ctx, account, blockNumber +func (_m *EthereumClientMock) NonceAt(ctx context.Context, account common.Address, blockNumber *big.Int) (uint64, error) { + ret := _m.Called(ctx, account, blockNumber) + + if len(ret) == 0 { + panic("no return value specified for NonceAt") + } + + var r0 uint64 + var r1 error + if rf, ok := ret.Get(0).(func(context.Context, common.Address, *big.Int) (uint64, error)); ok { + return rf(ctx, account, blockNumber) + } + if rf, ok := ret.Get(0).(func(context.Context, common.Address, *big.Int) uint64); ok { + r0 = rf(ctx, account, blockNumber) + } else { + r0 = ret.Get(0).(uint64) + } + + if rf, ok := ret.Get(1).(func(context.Context, common.Address, *big.Int) error); ok { + r1 = rf(ctx, account, blockNumber) + } else { + r1 = ret.Error(1) + } + + return r0, r1 +} + +// PendingCodeAt provides a mock function with given fields: ctx, account +func (_m *EthereumClientMock) PendingCodeAt(ctx context.Context, account common.Address) ([]byte, error) { + ret := _m.Called(ctx, account) + + if len(ret) == 0 { + panic("no return value specified for PendingCodeAt") + } + + var r0 []byte + var r1 error + if rf, ok := ret.Get(0).(func(context.Context, common.Address) ([]byte, error)); ok { + return rf(ctx, account) + } + if rf, ok := ret.Get(0).(func(context.Context, common.Address) []byte); ok { + r0 = rf(ctx, account) + } else { + if ret.Get(0) != nil { + r0 = ret.Get(0).([]byte) + } + } + + if rf, ok := ret.Get(1).(func(context.Context, common.Address) error); ok { + r1 = rf(ctx, account) + } else { + r1 = ret.Error(1) + } + + return r0, r1 +} + +// PendingNonceAt provides a mock function with given fields: ctx, account +func (_m *EthereumClientMock) PendingNonceAt(ctx context.Context, account common.Address) (uint64, error) { + ret := _m.Called(ctx, account) + + if len(ret) == 0 { + panic("no return value specified for PendingNonceAt") + } + + var r0 uint64 + var r1 error + if rf, ok := ret.Get(0).(func(context.Context, common.Address) (uint64, error)); ok { + return rf(ctx, account) + } + if rf, ok := ret.Get(0).(func(context.Context, common.Address) uint64); ok { + r0 = rf(ctx, account) + } else { + r0 = ret.Get(0).(uint64) + } + + if rf, ok := ret.Get(1).(func(context.Context, common.Address) error); ok { + r1 = rf(ctx, account) + } else { + r1 = ret.Error(1) + } + + return r0, r1 +} + +// SendTransaction provides a mock function with given fields: ctx, tx +func (_m *EthereumClientMock) SendTransaction(ctx context.Context, tx *types.Transaction) error { + ret := _m.Called(ctx, tx) + + if len(ret) == 0 { + panic("no return value specified for SendTransaction") + } + + var r0 error + if rf, ok := ret.Get(0).(func(context.Context, *types.Transaction) error); ok { + r0 = rf(ctx, tx) + } else { + r0 = ret.Error(0) + } + + return r0 +} + +// StorageAt provides a mock function with given fields: ctx, account, key, blockNumber +func (_m *EthereumClientMock) StorageAt(ctx context.Context, account common.Address, key common.Hash, blockNumber *big.Int) ([]byte, error) { + ret := _m.Called(ctx, account, key, blockNumber) + + if len(ret) == 0 { + panic("no return value specified for StorageAt") + } + + var r0 []byte + var r1 error + if rf, ok := ret.Get(0).(func(context.Context, common.Address, common.Hash, *big.Int) ([]byte, error)); ok { + return rf(ctx, account, key, blockNumber) + } + if rf, ok := ret.Get(0).(func(context.Context, common.Address, common.Hash, *big.Int) []byte); ok { + r0 = rf(ctx, account, key, blockNumber) + } else { + if ret.Get(0) != nil { + r0 = ret.Get(0).([]byte) + } + } + + if rf, ok := ret.Get(1).(func(context.Context, common.Address, common.Hash, *big.Int) error); ok { + r1 = rf(ctx, account, key, blockNumber) + } else { + r1 = ret.Error(1) + } + + return r0, r1 +} + +// SubscribeFilterLogs provides a mock function with given fields: ctx, q, ch +func (_m *EthereumClientMock) SubscribeFilterLogs(ctx context.Context, q ethereum.FilterQuery, ch chan<- types.Log) (ethereum.Subscription, error) { + ret := _m.Called(ctx, q, ch) + + if len(ret) == 0 { + panic("no return value specified for SubscribeFilterLogs") + } + + var r0 ethereum.Subscription + var r1 error + if rf, ok := ret.Get(0).(func(context.Context, ethereum.FilterQuery, chan<- types.Log) (ethereum.Subscription, error)); ok { + return rf(ctx, q, ch) + } + if rf, ok := ret.Get(0).(func(context.Context, ethereum.FilterQuery, chan<- types.Log) ethereum.Subscription); ok { + r0 = rf(ctx, q, ch) + } else { + if ret.Get(0) != nil { + r0 = ret.Get(0).(ethereum.Subscription) + } + } + + if rf, ok := ret.Get(1).(func(context.Context, ethereum.FilterQuery, chan<- types.Log) error); ok { + r1 = rf(ctx, q, ch) + } else { + r1 = ret.Error(1) + } + + return r0, r1 +} + +// SubscribeNewHead provides a mock function with given fields: ctx, ch +func (_m *EthereumClientMock) SubscribeNewHead(ctx context.Context, ch chan<- *types.Header) (ethereum.Subscription, error) { + ret := _m.Called(ctx, ch) + + if len(ret) == 0 { + panic("no return value specified for SubscribeNewHead") + } + + var r0 ethereum.Subscription + var r1 error + if rf, ok := ret.Get(0).(func(context.Context, chan<- *types.Header) (ethereum.Subscription, error)); ok { + return rf(ctx, ch) + } + if rf, ok := ret.Get(0).(func(context.Context, chan<- *types.Header) ethereum.Subscription); ok { + r0 = rf(ctx, ch) + } else { + if ret.Get(0) != nil { + r0 = ret.Get(0).(ethereum.Subscription) + } + } + + if rf, ok := ret.Get(1).(func(context.Context, chan<- *types.Header) error); ok { + r1 = rf(ctx, ch) + } else { + r1 = ret.Error(1) + } + + return r0, r1 +} + +// SuggestGasPrice provides a mock function with given fields: ctx +func (_m *EthereumClientMock) SuggestGasPrice(ctx context.Context) (*big.Int, error) { + ret := _m.Called(ctx) + + if len(ret) == 0 { + panic("no return value specified for SuggestGasPrice") + } + + var r0 *big.Int + var r1 error + if rf, ok := ret.Get(0).(func(context.Context) (*big.Int, error)); ok { + return rf(ctx) + } + if rf, ok := ret.Get(0).(func(context.Context) *big.Int); ok { + r0 = rf(ctx) + } else { + if ret.Get(0) != nil { + r0 = ret.Get(0).(*big.Int) + } + } + + if rf, ok := ret.Get(1).(func(context.Context) error); ok { + r1 = rf(ctx) + } else { + r1 = ret.Error(1) + } + + return r0, r1 +} + +// SuggestGasTipCap provides a mock function with given fields: ctx +func (_m *EthereumClientMock) SuggestGasTipCap(ctx context.Context) (*big.Int, error) { + ret := _m.Called(ctx) + + if len(ret) == 0 { + panic("no return value specified for SuggestGasTipCap") + } + + var r0 *big.Int + var r1 error + if rf, ok := ret.Get(0).(func(context.Context) (*big.Int, error)); ok { + return rf(ctx) + } + if rf, ok := ret.Get(0).(func(context.Context) *big.Int); ok { + r0 = rf(ctx) + } else { + if ret.Get(0) != nil { + r0 = ret.Get(0).(*big.Int) + } + } + + if rf, ok := ret.Get(1).(func(context.Context) error); ok { + r1 = rf(ctx) + } else { + r1 = ret.Error(1) + } + + return r0, r1 +} + +// TransactionByHash provides a mock function with given fields: ctx, txHash +func (_m *EthereumClientMock) TransactionByHash(ctx context.Context, txHash common.Hash) (*types.Transaction, bool, error) { + ret := _m.Called(ctx, txHash) + + if len(ret) == 0 { + panic("no return value specified for TransactionByHash") + } + + var r0 *types.Transaction + var r1 bool + var r2 error + if rf, ok := ret.Get(0).(func(context.Context, common.Hash) (*types.Transaction, bool, error)); ok { + return rf(ctx, txHash) + } + if rf, ok := ret.Get(0).(func(context.Context, common.Hash) *types.Transaction); ok { + r0 = rf(ctx, txHash) + } else { + if ret.Get(0) != nil { + r0 = ret.Get(0).(*types.Transaction) + } + } + + if rf, ok := ret.Get(1).(func(context.Context, common.Hash) bool); ok { + r1 = rf(ctx, txHash) + } else { + r1 = ret.Get(1).(bool) + } + + if rf, ok := ret.Get(2).(func(context.Context, common.Hash) error); ok { + r2 = rf(ctx, txHash) + } else { + r2 = ret.Error(2) + } + + return r0, r1, r2 +} + +// TransactionCount provides a mock function with given fields: ctx, blockHash +func (_m *EthereumClientMock) TransactionCount(ctx context.Context, blockHash common.Hash) (uint, error) { + ret := _m.Called(ctx, blockHash) + + if len(ret) == 0 { + panic("no return value specified for TransactionCount") + } + + var r0 uint + var r1 error + if rf, ok := ret.Get(0).(func(context.Context, common.Hash) (uint, error)); ok { + return rf(ctx, blockHash) + } + if rf, ok := ret.Get(0).(func(context.Context, common.Hash) uint); ok { + r0 = rf(ctx, blockHash) + } else { + r0 = ret.Get(0).(uint) + } + + if rf, ok := ret.Get(1).(func(context.Context, common.Hash) error); ok { + r1 = rf(ctx, blockHash) + } else { + r1 = ret.Error(1) + } + + return r0, r1 +} + +// TransactionInBlock provides a mock function with given fields: ctx, blockHash, index +func (_m *EthereumClientMock) TransactionInBlock(ctx context.Context, blockHash common.Hash, index uint) (*types.Transaction, error) { + ret := _m.Called(ctx, blockHash, index) + + if len(ret) == 0 { + panic("no return value specified for TransactionInBlock") + } + + var r0 *types.Transaction + var r1 error + if rf, ok := ret.Get(0).(func(context.Context, common.Hash, uint) (*types.Transaction, error)); ok { + return rf(ctx, blockHash, index) + } + if rf, ok := ret.Get(0).(func(context.Context, common.Hash, uint) *types.Transaction); ok { + r0 = rf(ctx, blockHash, index) + } else { + if ret.Get(0) != nil { + r0 = ret.Get(0).(*types.Transaction) + } + } + + if rf, ok := ret.Get(1).(func(context.Context, common.Hash, uint) error); ok { + r1 = rf(ctx, blockHash, index) + } else { + r1 = ret.Error(1) + } + + return r0, r1 +} + +// TransactionReceipt provides a mock function with given fields: ctx, txHash +func (_m *EthereumClientMock) TransactionReceipt(ctx context.Context, txHash common.Hash) (*types.Receipt, error) { + ret := _m.Called(ctx, txHash) + + if len(ret) == 0 { + panic("no return value specified for TransactionReceipt") + } + + var r0 *types.Receipt + var r1 error + if rf, ok := ret.Get(0).(func(context.Context, common.Hash) (*types.Receipt, error)); ok { + return rf(ctx, txHash) + } + if rf, ok := ret.Get(0).(func(context.Context, common.Hash) *types.Receipt); ok { + r0 = rf(ctx, txHash) + } else { + if ret.Get(0) != nil { + r0 = ret.Get(0).(*types.Receipt) + } + } + + if rf, ok := ret.Get(1).(func(context.Context, common.Hash) error); ok { + r1 = rf(ctx, txHash) + } else { + r1 = ret.Error(1) + } + + return r0, r1 +} + +// NewEthereumClientMock creates a new instance of EthereumClientMock. It also registers a testing interface on the mock and a cleanup function to assert the mocks expectations. +// The first argument is typically a *testing.T value. +func NewEthereumClientMock(t interface { + mock.TestingT + Cleanup(func()) +}) *EthereumClientMock { + mock := &EthereumClientMock{} + mock.Mock.Test(t) + + t.Cleanup(func() { mock.AssertExpectations(t) }) + + return mock +} diff --git a/mocks/mocks.go b/mocks/mocks.go deleted file mode 100644 index dae56d9..0000000 --- a/mocks/mocks.go +++ /dev/null @@ -1,124 +0,0 @@ -package mocks - -import ( - "context" - "math/big" - - "github.com/0xPolygon/beethoven/tx" - "github.com/0xPolygon/beethoven/types" - - "github.com/0xPolygonHermez/zkevm-node/ethtxmanager" - validiumTypes "github.com/0xPolygonHermez/zkevm-node/jsonrpc/types" - "github.com/ethereum/go-ethereum" - "github.com/ethereum/go-ethereum/common" - "github.com/jackc/pgx/v4" - "github.com/stretchr/testify/mock" -) - -var _ types.EthermanInterface = (*EthermanMock)(nil) - -type EthermanMock struct { - mock.Mock -} - -func (e *EthermanMock) GetSequencerAddr(rollupId uint32) (common.Address, error) { - args := e.Called(rollupId) - - return args.Get(0).(common.Address), args.Error(1) //nolint:forcetypeassert -} - -func (e *EthermanMock) BuildTrustedVerifyBatchesTxData( - lastVerifiedBatch, - newVerifiedBatch uint64, - proof tx.ZKP, - rollupId uint32, -) (data []byte, err error) { - args := e.Called(lastVerifiedBatch, newVerifiedBatch, proof, rollupId) - - return args.Get(0).([]byte), args.Error(1) //nolint:forcetypeassert -} - -func (e *EthermanMock) CallContract(ctx context.Context, call ethereum.CallMsg, - blockNumber *big.Int) ([]byte, error) { - args := e.Called(ctx, call, blockNumber) - - return args.Get(0).([]byte), args.Error(1) //nolint:forcetypeassert -} - -var _ types.DBInterface = (*DbMock)(nil) - -type DbMock struct { - mock.Mock -} - -func (db *DbMock) BeginStateTransaction(ctx context.Context) (pgx.Tx, error) { - args := db.Called(ctx) - - tx, ok := args.Get(0).(pgx.Tx) - if !ok { - return nil, args.Error(1) - } - - return tx, args.Error(1) -} - -var _ types.EthTxManager = (*EthTxManagerMock)(nil) - -type EthTxManagerMock struct { - mock.Mock -} - -func (e *EthTxManagerMock) Add(ctx context.Context, owner, id string, - from common.Address, to *common.Address, value *big.Int, data []byte, gasOffset uint64, dbTx pgx.Tx) error { - args := e.Called(ctx, owner, id, from, to, value, data, gasOffset, dbTx) - - return args.Error(0) -} - -func (e *EthTxManagerMock) Result(ctx context.Context, owner, - id string, dbTx pgx.Tx) (ethtxmanager.MonitoredTxResult, error) { - args := e.Called(ctx, owner, id, dbTx) - - return args.Get(0).(ethtxmanager.MonitoredTxResult), args.Error(1) //nolint:forcetypeassert -} - -func (e *EthTxManagerMock) ResultsByStatus(ctx context.Context, owner string, - statuses []ethtxmanager.MonitoredTxStatus, dbTx pgx.Tx) ([]ethtxmanager.MonitoredTxResult, error) { - e.Called(ctx, owner, statuses, dbTx) - - return nil, nil -} - -func (e *EthTxManagerMock) ProcessPendingMonitoredTxs(ctx context.Context, owner string, - failedResultHandler ethtxmanager.ResultHandler, dbTx pgx.Tx) { - e.Called(ctx, owner, failedResultHandler, dbTx) -} - -var _ types.ZkEVMClientInterface = (*ZkEVMClientMock)(nil) - -type ZkEVMClientMock struct { - mock.Mock -} - -func (zkc *ZkEVMClientMock) BatchByNumber(ctx context.Context, number *big.Int) (*validiumTypes.Batch, error) { - args := zkc.Called(ctx, number) - - batch, ok := args.Get(0).(*validiumTypes.Batch) - if !ok { - return nil, args.Error(1) - } - - return batch, args.Error(1) -} - -var _ types.ZkEVMClientClientCreator = (*ZkEVMClientCreatorMock)(nil) - -type ZkEVMClientCreatorMock struct { - mock.Mock -} - -func (zc *ZkEVMClientCreatorMock) NewClient(rpc string) types.ZkEVMClientInterface { - args := zc.Called(rpc) - - return args.Get(0).(types.ZkEVMClientInterface) //nolint:forcetypeassert -} diff --git a/mocks/zk_evm_client.generated.go b/mocks/zk_evm_client.generated.go new file mode 100644 index 0000000..4cac43e --- /dev/null +++ b/mocks/zk_evm_client.generated.go @@ -0,0 +1,61 @@ +// Code generated by mockery v2.38.0. DO NOT EDIT. + +package mocks + +import ( + context "context" + big "math/big" + + mock "github.com/stretchr/testify/mock" + + types "github.com/0xPolygonHermez/zkevm-node/jsonrpc/types" +) + +// ZkEVMClientMock is an autogenerated mock type for the IZkEVMClient type +type ZkEVMClientMock struct { + mock.Mock +} + +// BatchByNumber provides a mock function with given fields: ctx, number +func (_m *ZkEVMClientMock) BatchByNumber(ctx context.Context, number *big.Int) (*types.Batch, error) { + ret := _m.Called(ctx, number) + + if len(ret) == 0 { + panic("no return value specified for BatchByNumber") + } + + var r0 *types.Batch + var r1 error + if rf, ok := ret.Get(0).(func(context.Context, *big.Int) (*types.Batch, error)); ok { + return rf(ctx, number) + } + if rf, ok := ret.Get(0).(func(context.Context, *big.Int) *types.Batch); ok { + r0 = rf(ctx, number) + } else { + if ret.Get(0) != nil { + r0 = ret.Get(0).(*types.Batch) + } + } + + if rf, ok := ret.Get(1).(func(context.Context, *big.Int) error); ok { + r1 = rf(ctx, number) + } else { + r1 = ret.Error(1) + } + + return r0, r1 +} + +// NewZkEVMClientMock creates a new instance of ZkEVMClientMock. It also registers a testing interface on the mock and a cleanup function to assert the mocks expectations. +// The first argument is typically a *testing.T value. +func NewZkEVMClientMock(t interface { + mock.TestingT + Cleanup(func()) +}) *ZkEVMClientMock { + mock := &ZkEVMClientMock{} + mock.Mock.Test(t) + + t.Cleanup(func() { mock.AssertExpectations(t) }) + + return mock +} diff --git a/mocks/zk_evm_client_creator.generated.go b/mocks/zk_evm_client_creator.generated.go new file mode 100644 index 0000000..dfa6dc8 --- /dev/null +++ b/mocks/zk_evm_client_creator.generated.go @@ -0,0 +1,47 @@ +// Code generated by mockery v2.38.0. DO NOT EDIT. + +package mocks + +import ( + types "github.com/0xPolygon/beethoven/types" + mock "github.com/stretchr/testify/mock" +) + +// ZkEVMClientClientCreatorMock is an autogenerated mock type for the IZkEVMClientClientCreator type +type ZkEVMClientClientCreatorMock struct { + mock.Mock +} + +// NewClient provides a mock function with given fields: rpc +func (_m *ZkEVMClientClientCreatorMock) NewClient(rpc string) types.IZkEVMClient { + ret := _m.Called(rpc) + + if len(ret) == 0 { + panic("no return value specified for NewClient") + } + + var r0 types.IZkEVMClient + if rf, ok := ret.Get(0).(func(string) types.IZkEVMClient); ok { + r0 = rf(rpc) + } else { + if ret.Get(0) != nil { + r0 = ret.Get(0).(types.IZkEVMClient) + } + } + + return r0 +} + +// NewZkEVMClientClientCreatorMock creates a new instance of ZkEVMClientClientCreatorMock. It also registers a testing interface on the mock and a cleanup function to assert the mocks expectations. +// The first argument is typically a *testing.T value. +func NewZkEVMClientClientCreatorMock(t interface { + mock.TestingT + Cleanup(func()) +}) *ZkEVMClientClientCreatorMock { + mock := &ZkEVMClientClientCreatorMock{} + mock.Mock.Test(t) + + t.Cleanup(func() { mock.AssertExpectations(t) }) + + return mock +} diff --git a/rpc/rpc.go b/rpc/rpc.go index b2bfe4e..266347b 100644 --- a/rpc/rpc.go +++ b/rpc/rpc.go @@ -24,14 +24,14 @@ const ( type InteropEndpoints struct { ctx context.Context executor *interop.Executor - db types.DBInterface + db types.IDB } // NewInteropEndpoints returns InteropEndpoints func NewInteropEndpoints( ctx context.Context, executor *interop.Executor, - db types.DBInterface, + db types.IDB, ) *InteropEndpoints { return &InteropEndpoints{ ctx: ctx, diff --git a/rpc/rpc_test.go b/rpc/rpc_test.go index 0b6fbb2..21da4f7 100644 --- a/rpc/rpc_test.go +++ b/rpc/rpc_test.go @@ -29,15 +29,15 @@ func TestInteropEndpointsGetTxStatus(t *testing.T) { t.Run("BeginStateTransaction returns an error", func(t *testing.T) { t.Parallel() - dbMock := new(mocks.DbMock) + dbMock := mocks.NewDBMock(t) dbMock.On("BeginStateTransaction", mock.Anything).Return(nil, errors.New("error")).Once() e := interop.New( log.WithFields("module", "test"), &config.Config{}, common.HexToAddress("0xadmin"), - new(mocks.EthermanMock), - new(mocks.EthTxManagerMock), + mocks.NewEthermanMock(t), + mocks.NewEthTxManagerMock(t), ) i := NewInteropEndpoints(context.Background(), e, dbMock) @@ -57,10 +57,10 @@ func TestInteropEndpointsGetTxStatus(t *testing.T) { txMock := new(mocks.TxMock) txMock.On("Rollback", mock.Anything).Return(nil).Once() - dbMock := new(mocks.DbMock) + dbMock := mocks.NewDBMock(t) dbMock.On("BeginStateTransaction", mock.Anything).Return(txMock, nil).Once() - txManagerMock := new(mocks.EthTxManagerMock) + txManagerMock := mocks.NewEthTxManagerMock(t) txManagerMock.On("Result", mock.Anything, ethTxManOwner, txHash.Hex(), txMock). Return(ethtxmanager.MonitoredTxResult{}, errors.New("error")).Once() @@ -68,7 +68,7 @@ func TestInteropEndpointsGetTxStatus(t *testing.T) { log.WithFields("module", "test"), &config.Config{}, common.HexToAddress("0xadmin"), - new(mocks.EthermanMock), + mocks.NewEthermanMock(t), txManagerMock, ) i := NewInteropEndpoints(context.Background(), e, dbMock) @@ -101,10 +101,10 @@ func TestInteropEndpointsGetTxStatus(t *testing.T) { txMock := new(mocks.TxMock) txMock.On("Rollback", mock.Anything).Return(nil).Once() - dbMock := new(mocks.DbMock) + dbMock := mocks.NewDBMock(t) dbMock.On("BeginStateTransaction", mock.Anything).Return(txMock, nil).Once() - txManagerMock := new(mocks.EthTxManagerMock) + txManagerMock := mocks.NewEthTxManagerMock(t) txManagerMock.On("Result", mock.Anything, ethTxManOwner, txHash.Hex(), txMock). Return(result, nil).Once() @@ -112,7 +112,7 @@ func TestInteropEndpointsGetTxStatus(t *testing.T) { log.WithFields("module", "test"), &config.Config{}, common.HexToAddress("0xadmin"), - new(mocks.EthermanMock), + mocks.NewEthermanMock(t), txManagerMock, ) i := NewInteropEndpoints(context.Background(), e, dbMock) @@ -161,12 +161,12 @@ func TestInteropEndpointsSendTx(t *testing.T) { RollupID: 1, } signedTx := &tx.SignedTx{Tx: tnx} - ethermanMock := new(mocks.EthermanMock) - zkEVMClientCreatorMock := new(mocks.ZkEVMClientCreatorMock) - zkEVMClientMock := new(mocks.ZkEVMClientMock) - dbMock := new(mocks.DbMock) + ethermanMock := mocks.NewEthermanMock(t) + zkEVMClientCreatorMock := mocks.NewZkEVMClientClientCreatorMock(t) + zkEVMClientMock := mocks.NewZkEVMClientMock(t) + dbMock := mocks.NewDBMock(t) txMock := new(mocks.TxMock) - ethTxManagerMock := new(mocks.EthTxManagerMock) + ethTxManagerMock := mocks.NewEthTxManagerMock(t) executeTestFn := func() { e := interop.New( diff --git a/types/interfaces.go b/types/interfaces.go index 2a198a6..1d4e003 100644 --- a/types/interfaces.go +++ b/types/interfaces.go @@ -12,28 +12,32 @@ import ( "github.com/jackc/pgx/v4" ) -type DBInterface interface { +//go:generate mockery --name IDB --structname DBMock --output ../mocks --case=underscore --filename db.generated.go +type IDB interface { BeginStateTransaction(ctx context.Context) (pgx.Tx, error) } -type EthermanInterface interface { +//go:generate mockery --name IEtherman --structname EthermanMock --output ../mocks --case=underscore --filename etherman.generated.go +type IEtherman interface { GetSequencerAddr(rollupId uint32) (common.Address, error) BuildTrustedVerifyBatchesTxData(lastVerifiedBatch, newVerifiedBatch uint64, proof tx.ZKP, rollupId uint32) (data []byte, err error) CallContract(ctx context.Context, call ethereum.CallMsg, blockNumber *big.Int) ([]byte, error) } -// ethTxManager contains the methods required to send txs to ethereum. -type EthTxManager interface { +//go:generate mockery --name IEthTxManager --structname EthTxManagerMock --output ../mocks --case=underscore --filename eth_tx_manager.generated.go +type IEthTxManager interface { Add(ctx context.Context, owner, id string, from common.Address, to *common.Address, value *big.Int, data []byte, gasOffset uint64, dbTx pgx.Tx) error Result(ctx context.Context, owner, id string, dbTx pgx.Tx) (ethtxmanager.MonitoredTxResult, error) ResultsByStatus(ctx context.Context, owner string, statuses []ethtxmanager.MonitoredTxStatus, dbTx pgx.Tx) ([]ethtxmanager.MonitoredTxResult, error) ProcessPendingMonitoredTxs(ctx context.Context, owner string, failedResultHandler ethtxmanager.ResultHandler, dbTx pgx.Tx) } -type ZkEVMClientInterface interface { +//go:generate mockery --name IZkEVMClient --structname ZkEVMClientMock --output ../mocks --case=underscore --filename zk_evm_client.generated.go +type IZkEVMClient interface { BatchByNumber(ctx context.Context, number *big.Int) (*types.Batch, error) } -type ZkEVMClientClientCreator interface { - NewClient(rpc string) ZkEVMClientInterface +//go:generate mockery --name IZkEVMClientClientCreator --structname ZkEVMClientClientCreatorMock --output ../mocks --case=underscore --filename zk_evm_client_creator.generated.go +type IZkEVMClientClientCreator interface { + NewClient(rpc string) IZkEVMClient }