Skip to content

Commit

Permalink
clean instrumented client
Browse files Browse the repository at this point in the history
  • Loading branch information
shrimalmadhur committed Jul 23, 2024
1 parent 3cdac1a commit 4c532f4
Showing 1 changed file with 2 additions and 110 deletions.
112 changes: 2 additions & 110 deletions chainio/clients/eth/instrumented_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,48 +12,6 @@ import (
"github.com/ethereum/go-ethereum/ethclient"
)

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)
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)
}

// InstrumentedClient is a wrapper around the geth ethclient that instruments
// all the calls made to it. It counts each eth_ call made to it, and records the duration of each call,
// and exposes these as prometheus metrics
Expand All @@ -69,7 +27,8 @@ type InstrumentedClient struct {
clientAndVersion string
}

var _ client = (*InstrumentedClient)(nil)
var _ HttpBackend = (*InstrumentedClient)(nil)
var _ WsBackend = (*InstrumentedClient)(nil)

func NewInstrumentedClient(rpcAddress string, rpcCallsCollector *rpccalls.Collector) (*InstrumentedClient, error) {
client, err := ethclient.Dial(rpcAddress)
Expand Down Expand Up @@ -157,19 +116,6 @@ func (iec *InstrumentedClient) CallContract(
return bytes, nil
}

func (iec *InstrumentedClient) CallContractAtHash(
ctx context.Context,
msg ethereum.CallMsg,
blockHash common.Hash,
) ([]byte, error) {
callContractAtHash := func() ([]byte, error) { return iec.client.CallContractAtHash(ctx, msg, blockHash) }
bytes, err := instrumentFunction[[]byte](callContractAtHash, "eth_call", iec)
if err != nil {
return nil, err
}
return bytes, nil
}

func (iec *InstrumentedClient) CodeAt(
ctx context.Context,
contract common.Address,
Expand Down Expand Up @@ -247,15 +193,6 @@ func (iec *InstrumentedClient) HeaderByNumber(ctx context.Context, number *big.I
return header, nil
}

func (iec *InstrumentedClient) NetworkID(ctx context.Context) (*big.Int, error) {
networkID := func() (*big.Int, error) { return iec.client.NetworkID(ctx) }
id, err := instrumentFunction[*big.Int](networkID, "net_version", iec)
if err != nil {
return nil, err
}
return id, nil
}

func (iec *InstrumentedClient) NonceAt(
ctx context.Context,
account common.Address,
Expand All @@ -269,15 +206,6 @@ func (iec *InstrumentedClient) NonceAt(
return nonce, nil
}

func (iec *InstrumentedClient) PeerCount(ctx context.Context) (uint64, error) {
peerCount := func() (uint64, error) { return iec.client.PeerCount(ctx) }
count, err := instrumentFunction[uint64](peerCount, "net_peerCount", iec)
if err != nil {
return 0, err
}
return count, nil
}

func (iec *InstrumentedClient) PendingBalanceAt(ctx context.Context, account common.Address) (*big.Int, error) {
pendingBalanceAt := func() (*big.Int, error) { return iec.client.PendingBalanceAt(ctx, account) }
balance, err := instrumentFunction[*big.Int](pendingBalanceAt, "eth_getBalance", iec)
Expand Down Expand Up @@ -502,42 +430,6 @@ func (iec *InstrumentedClient) TransactionReceipt(ctx context.Context, txHash co
return receipt, nil
}

func (iec *InstrumentedClient) TransactionSender(
ctx context.Context,
tx *types.Transaction,
block common.Hash,
index uint,
) (common.Address, error) {
transactionSender := func() (common.Address, error) { return iec.client.TransactionSender(ctx, tx, block, index) }
address, err := instrumentFunction[common.Address](
transactionSender,
"eth_getSender",
iec,
)
if err != nil {
return common.Address{}, err
}
return address, nil
}

// Extra methods

// TODO(samlaf): feels weird that we have to write this function ourselves
//
// perhaps the gethClient interface should be the instrumented client,
// and then ethclient can take an instrumentedGethClient?
func (iec *InstrumentedClient) WaitForTransactionReceipt(ctx context.Context, txHash common.Hash) *types.Receipt {
for {
// verifying transaction receipt
receipt, err := iec.TransactionReceipt(ctx, txHash)
if err != nil {
time.Sleep(2 * time.Second)
} else {
return receipt
}
}
}

// Not sure why this method is not exposed in the ethclient itself...
// but it is needed to comply with the rpc metrics defined in avs-node spec
// https://docs.eigenlayer.xyz/eigenlayer/avs-guides/spec/metrics/metrics-prom-spec
Expand Down

0 comments on commit 4c532f4

Please sign in to comment.