Skip to content

Commit

Permalink
refactor: eth client and logger (#304)
Browse files Browse the repository at this point in the history
* refactor: eth client and logger

* fix mocks

* fix tests

* clean instrumented client

* fix typo

* add comments to clients

* add comments explaning fake behavior

* move test logger to testutils

* add source for log
  • Loading branch information
shrimalmadhur authored Jul 23, 2024
1 parent 500607f commit 7c76ea7
Show file tree
Hide file tree
Showing 30 changed files with 291 additions and 918 deletions.
4 changes: 2 additions & 2 deletions chainio/clients/avsregistry/bindings.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ type ContractBindings struct {
func NewAVSRegistryContractBindings(
registryCoordinatorAddr gethcommon.Address,
operatorStateRetrieverAddr gethcommon.Address,
ethclient eth.Client,
ethclient eth.HttpBackend,
logger logging.Logger,
) (*ContractBindings, error) {
contractBlsRegistryCoordinator, err := regcoordinator.NewContractRegistryCoordinator(
Expand Down Expand Up @@ -124,7 +124,7 @@ func NewAVSRegistryContractBindings(
// NewBindingsFromConfig creates a new instance of ContractBindings
func NewBindingsFromConfig(
cfg Config,
client eth.Client,
client eth.HttpBackend,
logger logging.Logger,
) (*ContractBindings, error) {
var (
Expand Down
4 changes: 2 additions & 2 deletions chainio/clients/avsregistry/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ import (

func BuildClients(
config Config,
client eth.Client,
wsClient eth.Client,
client eth.HttpBackend,
wsClient eth.WsBackend,
txMgr txmgr.TxManager,
logger logging.Logger,
) (*ChainReader, *ChainSubscriber, *ChainWriter, *ContractBindings, error) {
Expand Down
8 changes: 4 additions & 4 deletions chainio/clients/avsregistry/reader.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ type ChainReader struct {
registryCoordinator *regcoord.ContractRegistryCoordinator
operatorStateRetriever *opstateretriever.ContractOperatorStateRetriever
stakeRegistry *stakeregistry.ContractStakeRegistry
ethClient eth.Client
ethClient eth.HttpBackend
}

func NewChainReader(
Expand All @@ -47,7 +47,7 @@ func NewChainReader(
operatorStateRetriever *opstateretriever.ContractOperatorStateRetriever,
stakeRegistry *stakeregistry.ContractStakeRegistry,
logger logging.Logger,
ethClient eth.Client,
ethClient eth.HttpBackend,
) *ChainReader {
logger = logger.With(logging.ComponentKey, "avsregistry/ChainReader")

Expand All @@ -65,7 +65,7 @@ func NewChainReader(
// NewReaderFromConfig creates a new ChainReader
func NewReaderFromConfig(
cfg Config,
client eth.Client,
client eth.HttpBackend,
logger logging.Logger,
) (*ChainReader, error) {
bindings, err := NewBindingsFromConfig(cfg, client, logger)
Expand All @@ -89,7 +89,7 @@ func NewReaderFromConfig(
func BuildAvsRegistryChainReader(
registryCoordinatorAddr common.Address,
operatorStateRetrieverAddr common.Address,
ethClient eth.Client,
ethClient eth.HttpBackend,
logger logging.Logger,
) (*ChainReader, error) {
contractRegistryCoordinator, err := regcoord.NewContractRegistryCoordinator(registryCoordinatorAddr, ethClient)
Expand Down
4 changes: 2 additions & 2 deletions chainio/clients/avsregistry/subscriber.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ func NewChainSubscriber(
// Deprecated: Use NewSubscriberFromConfig instead
func BuildAvsRegistryChainSubscriber(
regCoordAddr common.Address,
ethWsClient eth.Client,
ethWsClient eth.WsBackend,
logger logging.Logger,
) (*ChainSubscriber, error) {
regCoord, err := regcoord.NewContractRegistryCoordinator(regCoordAddr, ethWsClient)
Expand All @@ -60,7 +60,7 @@ func BuildAvsRegistryChainSubscriber(
// A websocket ETH Client must be provided
func NewSubscriberFromConfig(
cfg Config,
wsClient eth.Client,
wsClient eth.WsBackend,
logger logging.Logger,
) (*ChainSubscriber, error) {
bindings, err := NewBindingsFromConfig(cfg, wsClient, logger)
Expand Down
8 changes: 4 additions & 4 deletions chainio/clients/avsregistry/writer.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ type ChainWriter struct {
blsApkRegistry *blsapkregistry.ContractBLSApkRegistry
elReader eLReader
logger logging.Logger
ethClient eth.Client
ethClient eth.HttpBackend
txMgr txmgr.TxManager
}

Expand All @@ -57,7 +57,7 @@ func NewChainWriter(
blsApkRegistry *blsapkregistry.ContractBLSApkRegistry,
elReader eLReader,
logger logging.Logger,
ethClient eth.Client,
ethClient eth.HttpBackend,
txMgr txmgr.TxManager,
) *ChainWriter {
logger = logger.With(logging.ComponentKey, "avsregistry/ChainWriter")
Expand All @@ -81,7 +81,7 @@ func BuildAvsRegistryChainWriter(
registryCoordinatorAddr gethcommon.Address,
operatorStateRetrieverAddr gethcommon.Address,
logger logging.Logger,
ethClient eth.Client,
ethClient eth.HttpBackend,
txMgr txmgr.TxManager,
) (*ChainWriter, error) {
registryCoordinator, err := regcoord.NewContractRegistryCoordinator(registryCoordinatorAddr, ethClient)
Expand Down Expand Up @@ -147,7 +147,7 @@ func BuildAvsRegistryChainWriter(
// NewWriterFromConfig creates a new ChainWriter from the provided config
func NewWriterFromConfig(
cfg Config,
client eth.Client,
client eth.HttpBackend,
txMgr txmgr.TxManager,
logger logging.Logger,
) (*ChainWriter, error) {
Expand Down
10 changes: 6 additions & 4 deletions chainio/clients/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import (
"crypto/ecdsa"
"time"

"github.com/ethereum/go-ethereum/ethclient"

"github.com/Layr-Labs/eigensdk-go/chainio/clients/avsregistry"
"github.com/Layr-Labs/eigensdk-go/chainio/clients/elcontracts"
"github.com/Layr-Labs/eigensdk-go/chainio/clients/eth"
Expand Down Expand Up @@ -39,8 +41,8 @@ type Clients struct {
AvsRegistryChainWriter *avsregistry.ChainWriter
ElChainReader *elcontracts.ChainReader
ElChainWriter *elcontracts.ChainWriter
EthHttpClient eth.Client
EthWsClient eth.Client
EthHttpClient eth.HttpBackend
EthWsClient eth.WsBackend
Wallet wallet.Wallet
TxManager txmgr.TxManager
AvsRegistryContractBindings *avsregistry.ContractBindings
Expand All @@ -61,12 +63,12 @@ func BuildAll(
eigenMetrics := metrics.NewEigenMetrics(config.AvsName, config.PromMetricsIpPortAddress, promReg, logger)

// creating two types of Eth clients: HTTP and WS
ethHttpClient, err := eth.NewClient(config.EthHttpUrl)
ethHttpClient, err := ethclient.Dial(config.EthHttpUrl)
if err != nil {
return nil, utils.WrapError("Failed to create Eth Http client", err)
}

ethWsClient, err := eth.NewClient(config.EthWsUrl)
ethWsClient, err := ethclient.Dial(config.EthWsUrl)
if err != nil {
return nil, utils.WrapError("Failed to create Eth WS client", err)
}
Expand Down
4 changes: 2 additions & 2 deletions chainio/clients/elcontracts/bindings.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ type ContractBindings struct {

func NewBindingsFromConfig(
cfg Config,
client eth.Client,
client eth.HttpBackend,
logger logging.Logger,
) (*ContractBindings, error) {
var (
Expand Down Expand Up @@ -117,7 +117,7 @@ func isZeroAddress(address gethcommon.Address) bool {
func NewEigenlayerContractBindings(
delegationManagerAddr gethcommon.Address,
avsDirectoryAddr gethcommon.Address,
ethclient eth.Client,
ethclient eth.HttpBackend,
logger logging.Logger,
) (*ContractBindings, error) {
contractDelegationManager, err := delegationmanager.NewContractDelegationManager(delegationManagerAddr, ethclient)
Expand Down
2 changes: 1 addition & 1 deletion chainio/clients/elcontracts/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (

func BuildClients(
config Config,
client eth.Client,
client eth.HttpBackend,
txMgr txmgr.TxManager,
logger logging.Logger,
eigenMetrics *metrics.EigenMetrics,
Expand Down
8 changes: 4 additions & 4 deletions chainio/clients/elcontracts/reader.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ type ChainReader struct {
strategyManager *strategymanager.ContractStrategyManager
avsDirectory *avsdirectory.ContractAVSDirectory
rewardsCoordinator *rewardscoordinator.ContractIRewardsCoordinator
ethClient eth.Client
ethClient eth.HttpBackend
}

func NewChainReader(
Expand All @@ -45,7 +45,7 @@ func NewChainReader(
avsDirectory *avsdirectory.ContractAVSDirectory,
rewardsCoordinator *rewardscoordinator.ContractIRewardsCoordinator,
logger logging.Logger,
ethClient eth.Client,
ethClient eth.HttpBackend,
) *ChainReader {
logger = logger.With(logging.ComponentKey, "elcontracts/reader")

Expand All @@ -65,7 +65,7 @@ func NewChainReader(
func BuildELChainReader(
delegationManagerAddr gethcommon.Address,
avsDirectoryAddr gethcommon.Address,
ethClient eth.Client,
ethClient eth.HttpBackend,
logger logging.Logger,
) (*ChainReader, error) {
elContractBindings, err := NewEigenlayerContractBindings(
Expand All @@ -90,7 +90,7 @@ func BuildELChainReader(

func NewReaderFromConfig(
cfg Config,
ethClient eth.Client,
ethClient eth.HttpBackend,
logger logging.Logger,
) (*ChainReader, error) {
elContractBindings, err := NewBindingsFromConfig(
Expand Down
8 changes: 4 additions & 4 deletions chainio/clients/elcontracts/writer.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ type ChainWriter struct {
rewardsCoordinator *rewardscoordinator.ContractIRewardsCoordinator
strategyManagerAddr gethcommon.Address
elChainReader Reader
ethClient eth.Client
ethClient eth.HttpBackend
logger logging.Logger
txMgr txmgr.TxManager
}
Expand All @@ -50,7 +50,7 @@ func NewChainWriter(
rewardsCoordinator *rewardscoordinator.ContractIRewardsCoordinator,
strategyManagerAddr gethcommon.Address,
elChainReader Reader,
ethClient eth.Client,
ethClient eth.HttpBackend,
logger logging.Logger,
eigenMetrics metrics.Metrics,
txMgr txmgr.TxManager,
Expand All @@ -75,7 +75,7 @@ func NewChainWriter(
func BuildELChainWriter(
delegationManagerAddr gethcommon.Address,
avsDirectoryAddr gethcommon.Address,
ethClient eth.Client,
ethClient eth.HttpBackend,
logger logging.Logger,
eigenMetrics metrics.Metrics,
txMgr txmgr.TxManager,
Expand Down Expand Up @@ -114,7 +114,7 @@ func BuildELChainWriter(

func NewWriterFromConfig(
cfg Config,
ethClient eth.Client,
ethClient eth.HttpBackend,
logger logging.Logger,
eigenMetrics metrics.Metrics,
txMgr txmgr.TxManager,
Expand Down
63 changes: 19 additions & 44 deletions chainio/clients/eth/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,54 +4,29 @@ import (
"context"
"math/big"

"github.com/ethereum/go-ethereum"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/ethclient"

"github.com/ethereum/go-ethereum/accounts/abi/bind"
)

type Client interface {
ChainID(ctx context.Context) (*big.Int, error)
BalanceAt(ctx context.Context, account common.Address, blockNumber *big.Int) (*big.Int, error)
BlockByHash(ctx context.Context, hash common.Hash) (*types.Block, error)
BlockByNumber(ctx context.Context, number *big.Int) (*types.Block, error)
// HttpBackend is the HTTP ETH Client interface
// It is exactly the same as the WsBackend and there is no difference between them to the compiler,
// but we keep them separate as a signal to the programmer that an eth.Client with an underlying http/ws connection is
// expected
type HttpBackend interface {
bind.ContractBackend

BlockNumber(ctx context.Context) (uint64, error)
CallContract(ctx context.Context, msg ethereum.CallMsg, blockNumber *big.Int) ([]byte, error)
CallContractAtHash(ctx context.Context, msg ethereum.CallMsg, blockHash common.Hash) ([]byte, error)
CodeAt(ctx context.Context, account common.Address, blockNumber *big.Int) ([]byte, error)
EstimateGas(ctx context.Context, msg ethereum.CallMsg) (uint64, error)
FeeHistory(
ctx context.Context,
blockCount uint64,
lastBlock *big.Int,
rewardPercentiles []float64,
) (*ethereum.FeeHistory, error)
FilterLogs(ctx context.Context, q ethereum.FilterQuery) ([]types.Log, error)
HeaderByHash(ctx context.Context, hash common.Hash) (*types.Header, error)
HeaderByNumber(ctx context.Context, number *big.Int) (*types.Header, error)
NetworkID(ctx context.Context) (*big.Int, error)
NonceAt(ctx context.Context, account common.Address, blockNumber *big.Int) (uint64, error)
PeerCount(ctx context.Context) (uint64, error)
PendingBalanceAt(ctx context.Context, account common.Address) (*big.Int, error)
PendingCallContract(ctx context.Context, msg ethereum.CallMsg) ([]byte, error)
PendingCodeAt(ctx context.Context, account common.Address) ([]byte, error)
PendingNonceAt(ctx context.Context, account common.Address) (uint64, error)
PendingStorageAt(ctx context.Context, account common.Address, key common.Hash) ([]byte, error)
PendingTransactionCount(ctx context.Context) (uint, error)
SendTransaction(ctx context.Context, tx *types.Transaction) error
StorageAt(ctx context.Context, account common.Address, key common.Hash, blockNumber *big.Int) ([]byte, error)
SubscribeFilterLogs(ctx context.Context, q ethereum.FilterQuery, ch chan<- types.Log) (ethereum.Subscription, error)
SubscribeNewHead(ctx context.Context, ch chan<- *types.Header) (ethereum.Subscription, error)
SuggestGasPrice(ctx context.Context) (*big.Int, error)
SuggestGasTipCap(ctx context.Context) (*big.Int, error)
SyncProgress(ctx context.Context) (*ethereum.SyncProgress, error)
TransactionByHash(ctx context.Context, hash common.Hash) (tx *types.Transaction, isPending bool, err error)
TransactionCount(ctx context.Context, blockHash common.Hash) (uint, error)
TransactionInBlock(ctx context.Context, blockHash common.Hash, index uint) (*types.Transaction, error)
TransactionReceipt(ctx context.Context, txHash common.Hash) (*types.Receipt, error)
TransactionSender(ctx context.Context, tx *types.Transaction, block common.Hash, index uint) (common.Address, error)
BlockByNumber(ctx context.Context, number *big.Int) (*types.Block, error)
}

func NewClient(rpcAddress string) (Client, error) {
return ethclient.Dial(rpcAddress)
// WsBackend is the Websocket ETH Client interface
// It is exactly the same as the HttpBackend and there is no difference between them to the compiler,
// but we keep them separate as a signal to the programmer that an eth.Client with an underlying http/ws connection is
// expected
type WsBackend interface {
bind.ContractBackend

BlockNumber(ctx context.Context) (uint64, error)
BlockByNumber(ctx context.Context, number *big.Int) (*types.Block, error)
}
Loading

0 comments on commit 7c76ea7

Please sign in to comment.