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

feat: reorganizes use of clients and adds fallback clients #250

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
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
11 changes: 3 additions & 8 deletions oracle/pkg/l1Listener/l1Listener.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
blocktracker "github.com/primev/mev-commit/contracts-abi/clients/BlockTracker"
"github.com/primev/mev-commit/oracle/pkg/store"
"github.com/primev/mev-commit/x/contracts/events"
"github.com/primev/mev-commit/x/evmclients"
"github.com/prometheus/client_golang/prometheus"
"golang.org/x/sync/errgroup"
)
Expand All @@ -27,15 +28,9 @@ type WinnerRegister interface {
LastWinnerBlock() (int64, error)
}

type EthClient interface {
BlockNumber(ctx context.Context) (uint64, error)
HeaderByNumber(ctx context.Context, number *big.Int) (*types.Header, error)
BlockByNumber(ctx context.Context, number *big.Int) (*types.Block, error)
}

type L1Listener struct {
logger *slog.Logger
l1Client EthClient
l1Client evmclients.EthClient
winnerRegister WinnerRegister
eventMgr events.EventManager
recorder L1Recorder
Expand All @@ -44,7 +39,7 @@ type L1Listener struct {

func NewL1Listener(
logger *slog.Logger,
l1Client EthClient,
l1Client evmclients.EthClient,
winnerRegister WinnerRegister,
evtMgr events.EventManager,
recorder L1Recorder,
Expand Down
102 changes: 7 additions & 95 deletions oracle/pkg/node/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,13 @@ import (
"fmt"
"io"
"log/slog"
"math/big"
"net/url"
"strings"
"time"

"github.com/ethereum/go-ethereum/accounts/abi"
"github.com/ethereum/go-ethereum/accounts/abi/bind"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/ethclient"
bidderregistry "github.com/primev/mev-commit/contracts-abi/clients/BidderRegistry"
blocktracker "github.com/primev/mev-commit/contracts-abi/clients/BlockTracker"
Expand All @@ -30,6 +28,7 @@ import (
"github.com/primev/mev-commit/x/contracts/events/publisher"
"github.com/primev/mev-commit/x/contracts/transactor"
"github.com/primev/mev-commit/x/contracts/txmonitor"
"github.com/primev/mev-commit/x/evmclients"
"github.com/primev/mev-commit/x/keysigner"
)

Expand Down Expand Up @@ -107,7 +106,7 @@ func NewNode(opts *Options) (*Node, error) {
monitor := txmonitor.New(
owner,
settlementClient,
txmonitor.NewEVMHelperWithLogger(settlementClient.Client(), nd.logger),
evmclients.NewEVMHelperWithLogger(settlementClient.Client(), nd.logger),
st,
nd.logger.With("component", "tx_monitor"),
1024,
Expand Down Expand Up @@ -158,14 +157,14 @@ func NewNode(opts *Options) (*Node, error) {
)
}

var listenerL1Client l1Listener.EthClient
var listenerL1Client evmclients.EthClient

listenerL1Client = l1Client
if opts.LaggerdMode > 0 {
listenerL1Client = &laggerdL1Client{EthClient: listenerL1Client, amount: opts.LaggerdMode}
listenerL1Client = evmclients.NewLaggerdL1Client(listenerL1Client, opts.LaggerdMode)
}

listenerL1Client = &infiniteRetryL1Client{EthClient: listenerL1Client, logger: nd.logger}
listenerL1Client = evmclients.NewInfiniteRetryL1Client(listenerL1Client, nd.logger)

blockTracker, err := blocktracker.NewBlocktrackerTransactor(
opts.BlockTrackerContractAddr,
Expand Down Expand Up @@ -205,7 +204,7 @@ func NewNode(opts *Options) (*Node, error) {
}

if opts.OverrideWinners != nil && len(opts.OverrideWinners) > 0 {
listenerL1Client = &winnerOverrideL1Client{EthClient: listenerL1Client, winners: opts.OverrideWinners}
listenerL1Client = evmclients.NewWinnerOverrideL1Client(listenerL1Client, opts.OverrideWinners)
for _, winner := range opts.OverrideWinners {
nd.logger.Info("setting builder mapping", "builderName", winner, "builderAddress", winner)
err := setBuilderMapping(
Expand Down Expand Up @@ -239,7 +238,7 @@ func NewNode(opts *Options) (*Node, error) {
st,
evtMgr,
oracleTransactorSession,
txmonitor.NewEVMHelperWithLogger(l1Client.Client(), nd.logger),
evmclients.NewEVMHelperWithLogger(l1Client.Client(), nd.logger),
)
if err != nil {
nd.logger.Error("failed to instantiate updater", "error", err)
Expand Down Expand Up @@ -398,93 +397,6 @@ func getContractABIs(opts *Options) (map[common.Address]*abi.ABI, error) {
return abis, nil
}

type laggerdL1Client struct {
l1Listener.EthClient
amount int
}

func (l *laggerdL1Client) BlockNumber(ctx context.Context) (uint64, error) {
blkNum, err := l.EthClient.BlockNumber(ctx)
if err != nil {
return 0, err
}

return blkNum - uint64(l.amount), nil
}

type winnerOverrideL1Client struct {
l1Listener.EthClient
winners []string
}

func (w *winnerOverrideL1Client) HeaderByNumber(ctx context.Context, number *big.Int) (*types.Header, error) {
hdr, err := w.EthClient.HeaderByNumber(ctx, number)
if err != nil {
return nil, err
}

idx := number.Int64() % int64(len(w.winners))
hdr.Extra = []byte(w.winners[idx])

return hdr, nil
}

type infiniteRetryL1Client struct {
l1Listener.EthClient
logger *slog.Logger
}

func (i *infiniteRetryL1Client) BlockNumber(ctx context.Context) (uint64, error) {
var blkNum uint64
var err error
for retries := 50; retries > 0; retries-- {
blkNum, err = i.EthClient.BlockNumber(ctx)
if err == nil {
break
}
i.logger.Error("failed to get block number, retrying...", "error", err)
time.Sleep(2 * time.Second)
}
if err != nil {
return 0, err
}
return blkNum, nil
}

func (i *infiniteRetryL1Client) HeaderByNumber(ctx context.Context, number *big.Int) (*types.Header, error) {
var hdr *types.Header
var err error
for retries := 50; retries > 0; retries-- {
hdr, err = i.EthClient.HeaderByNumber(ctx, number)
if err == nil {
break
}
i.logger.Error("failed to get header by number, retrying...", "error", err)
time.Sleep(2 * time.Second)
}
if err != nil {
return nil, err
}
return hdr, nil
}

func (i *infiniteRetryL1Client) BlockByNumber(ctx context.Context, number *big.Int) (*types.Block, error) {
var blk *types.Block
var err error
for retries := 50; retries > 0; retries-- {
blk, err = i.EthClient.BlockByNumber(ctx, number)
if err == nil {
break
}
i.logger.Error("failed to get block by number, retrying...", "error", err)
time.Sleep(2 * time.Second)
}
if err != nil {
return nil, err
}
return blk, nil
}

func setBuilderMapping(
ctx context.Context,
bt *blocktracker.BlocktrackerTransactorSession,
Expand Down
6 changes: 3 additions & 3 deletions oracle/pkg/updater/updater.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import (
blocktracker "github.com/primev/mev-commit/contracts-abi/clients/BlockTracker"
preconf "github.com/primev/mev-commit/contracts-abi/clients/PreConfCommitmentStore"
"github.com/primev/mev-commit/x/contracts/events"
"github.com/primev/mev-commit/x/contracts/txmonitor"
"github.com/primev/mev-commit/x/evmclients"
"github.com/prometheus/client_golang/prometheus"
"golang.org/x/sync/errgroup"
)
Expand Down Expand Up @@ -105,7 +105,7 @@ type Updater struct {
openedCmts chan *preconf.PreconfcommitmentstoreCommitmentStored
currentWindow atomic.Int64
metrics *metrics
receiptBatcher txmonitor.BatchReceiptGetter
receiptBatcher evmclients.BatchReceiptGetter
}

func NewUpdater(
Expand All @@ -114,7 +114,7 @@ func NewUpdater(
winnerRegister WinnerRegister,
evtMgr events.EventManager,
oracle Oracle,
receiptBatcher txmonitor.BatchReceiptGetter,
receiptBatcher evmclients.BatchReceiptGetter,
) (*Updater, error) {
l1BlockCache, err := lru.New[uint64, map[string]TxMetadata](1024)
if err != nil {
Expand Down
8 changes: 4 additions & 4 deletions oracle/pkg/updater/updater_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import (
preconf "github.com/primev/mev-commit/contracts-abi/clients/PreConfCommitmentStore"
"github.com/primev/mev-commit/oracle/pkg/updater"
"github.com/primev/mev-commit/x/contracts/events"
"github.com/primev/mev-commit/x/contracts/txmonitor"
"github.com/primev/mev-commit/x/evmclients"
"github.com/primev/mev-commit/x/util"
"golang.org/x/crypto/sha3"
)
Expand All @@ -37,14 +37,14 @@ type testBatcher struct {
failedReceipts map[common.Hash]bool
}

func (t *testBatcher) BatchReceipts(ctx context.Context, txns []common.Hash) ([]txmonitor.Result, error) {
var results []txmonitor.Result
func (t *testBatcher) BatchReceipts(ctx context.Context, txns []common.Hash) ([]evmclients.Result, error) {
var results []evmclients.Result
for _, txn := range txns {
status := types.ReceiptStatusSuccessful
if t.failedReceipts[txn] {
status = types.ReceiptStatusFailed
}
results = append(results, txmonitor.Result{
results = append(results, evmclients.Result{
Receipt: &types.Receipt{
TxHash: txn,
Status: status,
Expand Down
5 changes: 3 additions & 2 deletions p2p/pkg/node/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ import (
"github.com/primev/mev-commit/x/contracts/events/publisher"
"github.com/primev/mev-commit/x/contracts/transactor"
"github.com/primev/mev-commit/x/contracts/txmonitor"
"github.com/primev/mev-commit/x/evmclients"
"github.com/primev/mev-commit/x/keysigner"
"google.golang.org/grpc"
"google.golang.org/grpc/connectivity"
Expand Down Expand Up @@ -190,7 +191,7 @@ func NewNode(opts *Options) (*Node, error) {
monitor := txmonitor.New(
opts.KeySigner.GetAddress(),
contractRPC,
txmonitor.NewEVMHelperWithLogger(contractRPC.Client(), opts.Logger.With("component", "txmonitor")),
evmclients.NewEVMHelperWithLogger(contractRPC.Client(), opts.Logger.With("component", "txmonitor")),
store,
opts.Logger.With("component", "txmonitor"),
1024,
Expand Down Expand Up @@ -342,7 +343,7 @@ func NewNode(opts *Options) (*Node, error) {
evtMgr,
store,
commitmentDA,
txmonitor.NewEVMHelperWithLogger(contractRPC.Client(), opts.Logger.With("component", "evm_helper")),
evmclients.NewEVMHelperWithLogger(contractRPC.Client(), opts.Logger.With("component", "evm_helper")),
optsGetter,
opts.Logger.With("component", "tracker"),
)
Expand Down
6 changes: 3 additions & 3 deletions p2p/pkg/preconfirmation/tracker/tracker.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import (
"github.com/primev/mev-commit/p2p/pkg/p2p"
"github.com/primev/mev-commit/p2p/pkg/store"
"github.com/primev/mev-commit/x/contracts/events"
"github.com/primev/mev-commit/x/contracts/txmonitor"
"github.com/primev/mev-commit/x/evmclients"
"github.com/prometheus/client_golang/prometheus"
"golang.org/x/sync/errgroup"
)
Expand All @@ -27,7 +27,7 @@ type Tracker struct {
evtMgr events.EventManager
store CommitmentStore
preconfContract PreconfContract
receiptGetter txmonitor.BatchReceiptGetter
receiptGetter evmclients.BatchReceiptGetter
optsGetter OptsGetter
newL1Blocks chan *blocktracker.BlocktrackerNewL1Block
enryptedCmts chan *preconfcommstore.PreconfcommitmentstoreEncryptedCommitmentStored
Expand Down Expand Up @@ -75,7 +75,7 @@ func NewTracker(
evtMgr events.EventManager,
store CommitmentStore,
preconfContract PreconfContract,
receiptGetter txmonitor.BatchReceiptGetter,
receiptGetter evmclients.BatchReceiptGetter,
optsGetter OptsGetter,
logger *slog.Logger,
) *Tracker {
Expand Down
8 changes: 4 additions & 4 deletions p2p/pkg/preconfirmation/tracker/tracker_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import (
preconftracker "github.com/primev/mev-commit/p2p/pkg/preconfirmation/tracker"
"github.com/primev/mev-commit/p2p/pkg/store"
"github.com/primev/mev-commit/x/contracts/events"
"github.com/primev/mev-commit/x/contracts/txmonitor"
"github.com/primev/mev-commit/x/evmclients"
"github.com/primev/mev-commit/x/util"
)

Expand Down Expand Up @@ -364,13 +364,13 @@ type testReceiptGetter struct {
count int
}

func (t *testReceiptGetter) BatchReceipts(_ context.Context, txns []common.Hash) ([]txmonitor.Result, error) {
func (t *testReceiptGetter) BatchReceipts(_ context.Context, txns []common.Hash) ([]evmclients.Result, error) {
if t.count != len(txns) {
return nil, fmt.Errorf("expected %d txns, got %d", t.count, len(txns))
}
results := make([]txmonitor.Result, 0, len(txns))
results := make([]evmclients.Result, 0, len(txns))
for range txns {
results = append(results, txmonitor.Result{
results = append(results, evmclients.Result{
Err: errors.New("test error"),
})
}
Expand Down
Loading
Loading