Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Get rid of "types/interfaces" package #40

Merged
merged 4 commits into from
Dec 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion mocks/eth_client.generated.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 6 additions & 6 deletions mocks/eth_client_factory.generated.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 4 additions & 2 deletions sequencer/call.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
8 changes: 4 additions & 4 deletions sequencer/tracker.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
Expand Down Expand Up @@ -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)
}
}
Expand Down
10 changes: 5 additions & 5 deletions synchronizer/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand All @@ -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()

Expand All @@ -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
Expand All @@ -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
}
Expand All @@ -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
Expand Down
26 changes: 17 additions & 9 deletions types/batch_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package types

import (
"crypto/ecdsa"
"fmt"
"testing"

"github.com/ethereum/go-ethereum/common"
Expand Down Expand Up @@ -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)
}
})
}
}
23 changes: 20 additions & 3 deletions types/factories.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
24 changes: 0 additions & 24 deletions types/interfaces/interfaces.go

This file was deleted.

9 changes: 0 additions & 9 deletions types/offchaindata.go

This file was deleted.

21 changes: 11 additions & 10 deletions types/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down