Skip to content
This repository has been archived by the owner on May 29, 2024. It is now read-only.

Commit

Permalink
OP Indexer Client Support (#166)
Browse files Browse the repository at this point in the history
Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
  • Loading branch information
Ethen and dependabot[bot] authored Oct 11, 2023
1 parent 5f6db5f commit 35b4fab
Show file tree
Hide file tree
Showing 23 changed files with 396 additions and 106 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ jobs:
- name: Install foundry
uses: foundry-rs/foundry-toolchain@v1

- name: Setup devnet resources
- name: Setup devnet Resources
id: devnet
run: |
make devnet-allocs
Expand Down
21 changes: 4 additions & 17 deletions cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,27 +58,14 @@ func RunPessimism(_ *cli.Context) error {
logging.New(cfg.Environment)
logger := logging.WithContext(ctx)

l1Client, err := client.NewEthClient(ctx, cfg.L1RpcEndpoint)
if err != nil {
logger.Fatal("Error creating L1 client", zap.Error(err))
return err
}

l2Client, err := client.NewEthClient(ctx, cfg.L2RpcEndpoint)
if err != nil {
logger.Fatal("Error creating L1 client", zap.Error(err))
return err
}

l2Geth, err := client.NewGethClient(cfg.L2RpcEndpoint)
ss := state.NewMemState()
bundle, err := client.NewBundle(ctx, cfg.ClientConfig)
if err != nil {
logger.Fatal("Error creating L2 GETH client", zap.Error(err))
logger.Fatal("Error creating client bundle", zap.Error(err))
return err
}

ss := state.NewMemState()

ctx = app.InitializeContext(ctx, ss, l1Client, l2Client, l2Geth)
ctx = app.InitializeContext(ctx, ss, bundle)

pessimism, shutDown, err := app.NewPessimismApp(ctx, cfg)

Expand Down
18 changes: 14 additions & 4 deletions e2e/setup.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,12 @@ func CreateL2TestSuite(t *testing.T) *L2TestSuite {
}

ss := state.NewMemState()
ctx = app.InitializeContext(ctx, ss, node.L2Client, node.L2Client, nil)

bundle := &client.Bundle{
L1Client: node.L2Client,
L2Client: node.L2Client,
}
ctx = app.InitializeContext(ctx, ss, bundle)

appCfg := DefaultTestConfig()

Expand Down Expand Up @@ -150,9 +155,14 @@ func CreateSysTestSuite(t *testing.T) *SysTestSuite {
}

ss := state.NewMemState()
ctx = app.InitializeContext(ctx, ss,
sys.Clients["l1"],
sys.Clients["sequencer"], gethClient)

bundle := &client.Bundle{
L1Client: sys.Clients["l1"],
L2Client: sys.Clients["sequencer"],
L2Geth: gethClient,
}

ctx = app.InitializeContext(ctx, ss, bundle)

appCfg := DefaultTestConfig()

Expand Down
15 changes: 14 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,12 @@ require (
github.com/urfave/cli v1.22.2
go.uber.org/zap v1.25.0
golang.org/x/text v0.13.0
gopkg.in/yaml.v2 v2.4.0
gopkg.in/yaml.v3 v3.0.1
)

require (
github.com/BurntSushi/toml v1.3.2 // indirect
github.com/DataDog/zstd v1.5.2 // indirect
github.com/Microsoft/go-winio v0.6.1 // indirect
github.com/VictoriaMetrics/fastcache v1.10.0 // indirect
Expand Down Expand Up @@ -62,6 +64,8 @@ require (
github.com/fsnotify/fsnotify v1.6.0 // indirect
github.com/gballet/go-libpcsclite v0.0.0-20191108122812-4678299bea08 // indirect
github.com/getsentry/sentry-go v0.18.0 // indirect
github.com/go-chi/chi/v5 v5.0.10 // indirect
github.com/go-chi/docgen v1.2.0 // indirect
github.com/go-ole/go-ole v1.2.6 // indirect
github.com/go-sourcemap/sourcemap v2.1.3+incompatible // indirect
github.com/go-stack/stack v1.8.1 // indirect
Expand Down Expand Up @@ -92,9 +96,16 @@ require (
github.com/ipfs/go-cid v0.4.1 // indirect
github.com/ipfs/go-datastore v0.6.0 // indirect
github.com/ipfs/go-log/v2 v2.5.1 // indirect
github.com/jackc/pgio v1.0.0 // indirect
github.com/jackc/pgpassfile v1.0.0 // indirect
github.com/jackc/pgservicefile v0.0.0-20221227161230-091c0ba34f0a // indirect
github.com/jackc/pgtype v1.14.0 // indirect
github.com/jackc/pgx/v5 v5.4.3 // indirect
github.com/jackpal/go-nat-pmp v1.0.2 // indirect
github.com/jbenet/go-temp-err-catcher v0.1.0 // indirect
github.com/jbenet/goprocess v0.1.4 // indirect
github.com/jinzhu/inflection v1.0.0 // indirect
github.com/jinzhu/now v1.1.5 // indirect
github.com/klauspost/compress v1.16.7 // indirect
github.com/klauspost/cpuid/v2 v2.2.5 // indirect
github.com/koron/go-ssdp v0.0.4 // indirect
Expand Down Expand Up @@ -160,6 +171,7 @@ require (
github.com/shirou/gopsutil v3.21.11+incompatible // indirect
github.com/spaolacci/murmur3 v1.1.0 // indirect
github.com/status-im/keycard-go v0.2.0 // indirect
github.com/stretchr/objx v0.5.0 // indirect
github.com/supranational/blst v0.3.11 // indirect
github.com/syndtr/goleveldb v1.0.1-0.20220614013038-64ee5596c38a // indirect
github.com/tklauser/go-sysconf v0.3.12 // indirect
Expand All @@ -182,7 +194,8 @@ require (
golang.org/x/tools v0.13.0 // indirect
google.golang.org/protobuf v1.31.0 // indirect
gopkg.in/natefinch/lumberjack.v2 v2.0.0 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
gorm.io/driver/postgres v1.5.2 // indirect
gorm.io/gorm v1.25.4 // indirect
lukechampine.com/blake3 v1.2.1 // indirect
rsc.io/tmplfunc v0.0.3 // indirect
)
Expand Down
131 changes: 131 additions & 0 deletions go.sum

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions internal/api/service/health.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,9 @@ func (svc *PessimismService) CheckHealth() *models.HealthCheck {

func (svc *PessimismService) CheckETHRPCHealth(n core.Network) bool {
logger := logging.WithContext(svc.ctx)
ethClient, err := client.FromContext(svc.ctx, n)
ethClient, err := client.FromNetwork(svc.ctx, n)
if err != nil {
logger.Error("error getting client from context", zap.Error(err))
logger.Error("error getting client bundle from context", zap.Error(err))
return false
}

Expand Down
9 changes: 6 additions & 3 deletions internal/api/service/service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@ import (
"fmt"

svc "github.com/base-org/pessimism/internal/api/service"
"github.com/base-org/pessimism/internal/app"
"github.com/base-org/pessimism/internal/client"

"github.com/base-org/pessimism/internal/core"
"github.com/base-org/pessimism/internal/mocks"
"github.com/golang/mock/gomock"
)
Expand All @@ -33,8 +34,10 @@ func createTestSuite(ctrl *gomock.Controller) *testSuite {
ethClient := mocks.NewMockEthClient(ctrl)
ctx := context.Background()

ctx = context.WithValue(ctx, core.L1Client, ethClient)
ctx = context.WithValue(ctx, core.L2Client, ethClient)
ctx = app.InitializeContext(ctx, nil, &client.Bundle{
L1Client: ethClient,
L2Client: ethClient,
})

service := svc.New(ctx, sysMock)
return &testSuite{
Expand Down
11 changes: 2 additions & 9 deletions internal/app/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,19 +23,12 @@ import (
)

// InitializeContext ... Performs dependency injection to build context struct
func InitializeContext(ctx context.Context, ss state.Store,
l1Client, l2Client client.EthClient, l2geth client.GethClient) context.Context {
func InitializeContext(ctx context.Context, ss state.Store, cb *client.Bundle) context.Context {
ctx = context.WithValue(
ctx, core.State, ss)

ctx = context.WithValue(
ctx, core.L1Client, l1Client)

ctx = context.WithValue(
ctx, core.L2Client, l2Client)

return context.WithValue(
ctx, core.L2Geth, l2geth)
ctx, core.Clients, cb)
}

// InitializeMetrics ... Performs dependency injection to build metrics struct
Expand Down
89 changes: 89 additions & 0 deletions internal/client/client.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
package client

import (
"context"
"fmt"

"github.com/base-org/pessimism/internal/core"
"github.com/base-org/pessimism/internal/logging"
indexer_client "github.com/ethereum-optimism/optimism/indexer/client"
"go.uber.org/zap"
)

// Config ... Client configuration
type Config struct {
L1RpcEndpoint string
L2RpcEndpoint string

IndexerCfg *indexer_client.Config
}

// Bundle ... Used to store all client object references
type Bundle struct {
IndexerClient IndexerClient
L1Client EthClient
L2Client EthClient
L2Geth GethClient
}

// NewBundle ... Construct a new client bundle
func NewBundle(ctx context.Context, cfg *Config) (*Bundle, error) {
logger := logging.WithContext(ctx)

l1Client, err := NewEthClient(ctx, cfg.L1RpcEndpoint)
if err != nil {
logger.Fatal("Error creating L1 client", zap.Error(err))
return nil, err
}

l2Client, err := NewEthClient(ctx, cfg.L2RpcEndpoint)
if err != nil {
logger.Fatal("Error creating L1 client", zap.Error(err))
return nil, err
}

l2Geth, err := NewGethClient(cfg.L2RpcEndpoint)
if err != nil {
logger.Fatal("Error creating L2 GETH client", zap.Error(err))
return nil, err
}

indexerClient, err := NewIndexerClient(cfg.IndexerCfg)
if err != nil { // Indexer client is optional so we don't want to fatal
logger.Warn("Error creating indexer client", zap.Error(err))
}

return &Bundle{
IndexerClient: indexerClient,
L1Client: l1Client,
L2Client: l2Client,
L2Geth: l2Geth,
}, nil
}

// FromContext ... Retrieves the client bundle from the context
func FromContext(ctx context.Context) (*Bundle, error) {
b, err := ctx.Value(core.Clients).(*Bundle)
if !err {
return nil, fmt.Errorf("failed to retrieve client bundle from context")
}

return b, nil
}

// FromNetwork ... Retrieves an eth client from the context
func FromNetwork(ctx context.Context, n core.Network) (EthClient, error) {
bundle, err := FromContext(ctx)
if err != nil {
return nil, err
}

switch n {
case core.Layer1:
return bundle.L1Client, nil
case core.Layer2:
return bundle.L2Client, nil
default:
return nil, fmt.Errorf("invalid network supplied")
}
}
16 changes: 0 additions & 16 deletions internal/client/eth.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,8 @@ package client

import (
"context"
"fmt"
"math/big"

"github.com/base-org/pessimism/internal/core"
"github.com/ethereum/go-ethereum"

"github.com/ethereum/go-ethereum/common"
Expand All @@ -38,20 +36,6 @@ type EthClient interface {
ch chan<- types.Log) (ethereum.Subscription, error)
}

// FromContext ... Retrieves EthClient from context
func FromContext(ctx context.Context, layer core.Network) (EthClient, error) {
key := core.L1Client
if layer == core.Layer2 {
key = core.L2Client
}

if client, ok := ctx.Value(key).(EthClient); ok {
return client, nil
}

return nil, fmt.Errorf("could not load eth client object from context")
}

// NewEthClient ... Initializer
func NewEthClient(ctx context.Context, rawURL string) (EthClient, error) {
return ethclient.DialContext(ctx, rawURL)
Expand Down
10 changes: 0 additions & 10 deletions internal/client/geth.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,13 @@ package client

import (
"context"
"fmt"
"math/big"

"github.com/base-org/pessimism/internal/core"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/ethclient/gethclient"
"github.com/ethereum/go-ethereum/rpc"
)

func L2GethFromContext(ctx context.Context) (GethClient, error) {
if client, ok := ctx.Value(core.L2Geth).(GethClient); ok {
return client, nil
}

return nil, fmt.Errorf("could not load eth client object from context")
}

// GethClient ... Provides interface wrapper for gethClient functions
type GethClient interface {
GetProof(ctx context.Context, account common.Address, keys []string,
Expand Down
18 changes: 18 additions & 0 deletions internal/client/op_indexer.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
//go:generate mockgen -package mocks --destination ../mocks/indexer_client.go . IndexerClient

package client

import (
"github.com/ethereum-optimism/optimism/indexer/api/models"
"github.com/ethereum-optimism/optimism/indexer/client"
"github.com/ethereum/go-ethereum/common"
)

type IndexerClient interface {
GetAllWithdrawalsByAddress(common.Address) ([]models.WithdrawalItem, error)
}

// NewIndexerClient ... Construct a new indexer client
func NewIndexerClient(cfg *client.Config, opts ...client.Option) (IndexerClient, error) {
return client.NewClient(cfg, opts...)
}
Loading

0 comments on commit 35b4fab

Please sign in to comment.