diff --git a/mocks/eth_client.generated.go b/mocks/eth_client.generated.go index 50565e2a..01651d39 100644 --- a/mocks/eth_client.generated.go +++ b/mocks/eth_client.generated.go @@ -3,11 +3,12 @@ package mocks import ( - context "context" big "math/big" common "github.com/ethereum/go-ethereum/common" + context "context" + mock "github.com/stretchr/testify/mock" types "github.com/ethereum/go-ethereum/core/types" diff --git a/mocks/eth_client_factory.generated.go b/mocks/eth_client_factory.generated.go index 23d1e9a0..fb9fb92d 100644 --- a/mocks/eth_client_factory.generated.go +++ b/mocks/eth_client_factory.generated.go @@ -5,7 +5,7 @@ package mocks import ( context "context" - interfaces "github.com/0xPolygon/cdk-data-availability/types/interfaces" + types "github.com/0xPolygon/cdk-data-availability/types" mock "github.com/stretchr/testify/mock" ) @@ -15,19 +15,19 @@ type IEthClientFactory struct { } // CreateEthClient provides a mock function with given fields: ctx, url -func (_m *IEthClientFactory) CreateEthClient(ctx context.Context, url string) (interfaces.IEthClient, error) { +func (_m *IEthClientFactory) CreateEthClient(ctx context.Context, url string) (types.IEthClient, error) { ret := _m.Called(ctx, url) - var r0 interfaces.IEthClient + var r0 types.IEthClient var r1 error - if rf, ok := ret.Get(0).(func(context.Context, string) (interfaces.IEthClient, error)); ok { + if rf, ok := ret.Get(0).(func(context.Context, string) (types.IEthClient, error)); ok { return rf(ctx, url) } - if rf, ok := ret.Get(0).(func(context.Context, string) interfaces.IEthClient); ok { + if rf, ok := ret.Get(0).(func(context.Context, string) types.IEthClient); ok { r0 = rf(ctx, url) } else { if ret.Get(0) != nil { - r0 = ret.Get(0).(interfaces.IEthClient) + r0 = ret.Get(0).(types.IEthClient) } } diff --git a/sequencer/call.go b/sequencer/call.go index a852b743..75185610 100644 --- a/sequencer/call.go +++ b/sequencer/call.go @@ -22,13 +22,15 @@ func GetData(url string, batchNum uint64) (*SeqBatch, error) { if err != nil { return nil, err } + if response.Error != nil { return nil, fmt.Errorf("%d - %s", response.Error.Code, response.Error.Message) } + var result SeqBatch - err = json.Unmarshal(response.Result, &result) - if err != nil { + if err = json.Unmarshal(response.Result, &result); err != nil { return nil, err } + return &result, nil } diff --git a/sequencer/tracker.go b/sequencer/tracker.go index 2662fe35..f48d96c5 100644 --- a/sequencer/tracker.go +++ b/sequencer/tracker.go @@ -109,8 +109,8 @@ func (st *Tracker) trackAddrChanges() { // if no subscription, retry until established for err != nil { <-time.After(st.retry) - sub, err = st.client.CDKValidium.WatchSetTrustedSequencer(opts, events) - if err != nil { + + if sub, err = st.client.CDKValidium.WatchSetTrustedSequencer(opts, events); err != nil { log.Errorf("error subscribing to trusted sequencer event, retrying: %v", err) } } @@ -153,8 +153,8 @@ func (st *Tracker) trackUrlChanges() { // if no subscription, retry until established for err != nil { <-time.After(st.retry) - sub, err = st.client.CDKValidium.WatchSetTrustedSequencerURL(opts, events) - if err != nil { + + if sub, err = st.client.CDKValidium.WatchSetTrustedSequencerURL(opts, events); err != nil { log.Errorf("error subscribing to trusted sequencer event, retrying: %v", err) } } diff --git a/synchronizer/init.go b/synchronizer/init.go index 02327ead..20aca06d 100644 --- a/synchronizer/init.go +++ b/synchronizer/init.go @@ -8,7 +8,7 @@ import ( "github.com/0xPolygon/cdk-data-availability/config" "github.com/0xPolygon/cdk-data-availability/db" "github.com/0xPolygon/cdk-data-availability/log" - "github.com/0xPolygon/cdk-data-availability/types/interfaces" + "github.com/0xPolygon/cdk-data-availability/types" "github.com/ethereum/go-ethereum/common" ) @@ -18,7 +18,7 @@ const ( ) // InitStartBlock initializes the L1 sync task by finding the inception block for the CDKValidium contract -func InitStartBlock(db db.DB, ethClientFactory interfaces.IEthClientFactory, l1 config.L1Config) error { +func InitStartBlock(db db.DB, ethClientFactory types.IEthClientFactory, l1 config.L1Config) error { ctx, cancel := context.WithTimeout(context.Background(), initBlockTimeout) defer cancel() @@ -45,7 +45,7 @@ func InitStartBlock(db db.DB, ethClientFactory interfaces.IEthClientFactory, l1 return setStartBlock(db, startBlock.Uint64()) } -func findContractDeploymentBlock(ctx context.Context, eth interfaces.IEthClient, contract common.Address) (*big.Int, error) { +func findContractDeploymentBlock(ctx context.Context, eth types.IEthClient, contract common.Address) (*big.Int, error) { latestBlock, err := eth.BlockByNumber(ctx, nil) if err != nil { return nil, err @@ -55,7 +55,7 @@ func findContractDeploymentBlock(ctx context.Context, eth interfaces.IEthClient, } // findCode is an O(log(n)) search for the inception block of a contract at the given address -func findCode(ctx context.Context, eth interfaces.IEthClient, address common.Address, startBlock, endBlock int64) int64 { +func findCode(ctx context.Context, eth types.IEthClient, address common.Address, startBlock, endBlock int64) int64 { if startBlock == endBlock { return startBlock } @@ -67,7 +67,7 @@ func findCode(ctx context.Context, eth interfaces.IEthClient, address common.Add } } -func codeLen(ctx context.Context, eth interfaces.IEthClient, address common.Address, blockNumber int64) int64 { +func codeLen(ctx context.Context, eth types.IEthClient, address common.Address, blockNumber int64) int64 { data, err := eth.CodeAt(ctx, address, big.NewInt(blockNumber)) if err != nil { return 0 diff --git a/types/batch_test.go b/types/batch_test.go index f299aea6..a70ce702 100644 --- a/types/batch_test.go +++ b/types/batch_test.go @@ -2,6 +2,7 @@ package types import ( "crypto/ecdsa" + "fmt" "testing" "github.com/ethereum/go-ethereum/common" @@ -50,14 +51,21 @@ func TestBatchSigning(t *testing.T) { require.NoError(t, err) privKeys = append(privKeys, pk) } - for _, c := range testBatchCases { - for _, pk := range privKeys { - signedBatch, err := c.Sign(pk) - require.NoError(t, err) - actualAddr, err := signedBatch.Signer() - require.NoError(t, err) - expectedAddr := crypto.PubkeyToAddress(pk.PublicKey) - assert.Equal(t, expectedAddr, actualAddr) - } + + for i, c := range testBatchCases { + c := c + + t.Run(fmt.Sprintf("%d. case", i+1), func(t *testing.T) { + t.Parallel() + + for _, pk := range privKeys { + signedBatch, err := c.Sign(pk) + require.NoError(t, err) + actualAddr, err := signedBatch.Signer() + require.NoError(t, err) + expectedAddr := crypto.PubkeyToAddress(pk.PublicKey) + assert.Equal(t, expectedAddr, actualAddr) + } + }) } } diff --git a/types/factories.go b/types/factories.go index 5d480f1a..97da6530 100644 --- a/types/factories.go +++ b/types/factories.go @@ -2,17 +2,34 @@ package types import ( "context" + "math/big" - "github.com/0xPolygon/cdk-data-availability/types/interfaces" + "github.com/ethereum/go-ethereum/common" + "github.com/ethereum/go-ethereum/core/types" "github.com/ethereum/go-ethereum/ethclient" ) -var _ interfaces.IEthClientFactory = (*EthClientFactoryImpl)(nil) +// IEthClient defines functions that an ethereum rpc client should implement +// +//go:generate mockery --name IEthClient --output ../mocks --case=underscore --filename eth_client.generated.go +type IEthClient interface { + BlockByNumber(ctx context.Context, number *big.Int) (*types.Block, error) + CodeAt(ctx context.Context, account common.Address, blockNumber *big.Int) ([]byte, error) +} + +// IEthClientFactory defines functions for a EthClient factory +// +//go:generate mockery --name IEthClientFactory --output ../mocks --case=underscore --filename eth_client_factory.generated.go +type IEthClientFactory interface { + CreateEthClient(ctx context.Context, url string) (IEthClient, error) +} + +var _ IEthClientFactory = (*EthClientFactoryImpl)(nil) // EthClientFactoryImpl is the implementation of EthClientFactory interface type EthClientFactoryImpl struct{} // CreateEthClient creates a new eth client -func (e *EthClientFactoryImpl) CreateEthClient(ctx context.Context, url string) (interfaces.IEthClient, error) { +func (e *EthClientFactoryImpl) CreateEthClient(ctx context.Context, url string) (IEthClient, error) { return ethclient.DialContext(ctx, url) } diff --git a/types/interfaces/interfaces.go b/types/interfaces/interfaces.go deleted file mode 100644 index a48e3442..00000000 --- a/types/interfaces/interfaces.go +++ /dev/null @@ -1,24 +0,0 @@ -package interfaces - -import ( - "context" - "math/big" - - "github.com/ethereum/go-ethereum/common" - "github.com/ethereum/go-ethereum/core/types" -) - -// IEthClientFactory defines functions for a EthClient factory -// -//go:generate mockery --name IEthClientFactory --output ../../mocks --case=underscore --filename eth_client_factory.generated.go -type IEthClientFactory interface { - CreateEthClient(ctx context.Context, url string) (IEthClient, error) -} - -// IEthClient defines functions that an ethereum rpc client should implement -// -//go:generate mockery --name IEthClient --output ../../mocks --case=underscore --filename eth_client.generated.go -type IEthClient interface { - BlockByNumber(ctx context.Context, number *big.Int) (*types.Block, error) - CodeAt(ctx context.Context, account common.Address, blockNumber *big.Int) ([]byte, error) -} diff --git a/types/offchaindata.go b/types/offchaindata.go deleted file mode 100644 index 4a162a7a..00000000 --- a/types/offchaindata.go +++ /dev/null @@ -1,9 +0,0 @@ -package types - -import "github.com/ethereum/go-ethereum/common" - -// OffChainData represents some data that is not stored on chain and should be preserved -type OffChainData struct { - Key common.Hash - Value []byte -} diff --git a/types/types.go b/types/types.go index 98efee8f..9f82f26a 100644 --- a/types/types.go +++ b/types/types.go @@ -15,11 +15,10 @@ const ( hexBitSize64 = 64 ) -// ArgBytesPtr helps to marshal byte array values provided in the RPC requests -func ArgBytesPtr(b []byte) *ArgBytes { - bb := ArgBytes(b) - - return &bb +// OffChainData represents some data that is not stored on chain and should be preserved +type OffChainData struct { + Key common.Hash + Value []byte } // ArgUint64 helps to marshal uint64 values provided in the RPC requests @@ -50,14 +49,16 @@ func (b ArgUint64) Hex() string { return string(bb) } -// ArgUint64Ptr returns the pointer of the provided ArgUint64 -func ArgUint64Ptr(a ArgUint64) *ArgUint64 { - return &a -} - // ArgBytes helps to marshal byte array values provided in the RPC requests type ArgBytes []byte +// ArgBytesPtr helps to marshal byte array values provided in the RPC requests +func ArgBytesPtr(b []byte) *ArgBytes { + bb := ArgBytes(b) + + return &bb +} + // MarshalText marshals into text func (b ArgBytes) MarshalText() ([]byte, error) { return encodeToHex(b), nil