-
Notifications
You must be signed in to change notification settings - Fork 6
/
clients.go
73 lines (62 loc) · 2.19 KB
/
clients.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
package clients
import (
"context"
"time"
"github.com/danyalprout/replayor/packages/config"
"github.com/ethereum-optimism/optimism/op-service/client"
"github.com/ethereum-optimism/optimism/op-service/retry"
"github.com/ethereum-optimism/optimism/op-service/sources"
"github.com/ethereum/go-ethereum/ethclient"
"github.com/ethereum/go-ethereum/log"
gn "github.com/ethereum/go-ethereum/node"
"github.com/ethereum/go-ethereum/rpc"
)
type Clients struct {
SourceNode *ethclient.Client
DestNode *ethclient.Client
EngineApi *sources.EngineAPIClient
}
func SetupClients(cfg config.ReplayorConfig, logger log.Logger, ctx context.Context) (Clients, error) {
logger.Info("dialing source node url", "url", cfg.SourceNodeUrl)
sourceNode, err := ethclient.Dial(cfg.SourceNodeUrl)
if err != nil {
return Clients{}, err
}
// Add some retries around connecting to the dest node as it starts up at the same time as the replayor
destNode, err := retry.Do(ctx, 120, retry.Fixed(time.Second), func() (*ethclient.Client, error) {
logger.Info("dialing destination node url", "url", cfg.ExecutionUrl)
d, e := ethclient.Dial(cfg.ExecutionUrl)
if e != nil {
logger.Info("waiting for geth (exec) to start")
}
return d, e
})
if err != nil {
return Clients{}, err
}
auth := rpc.WithHTTPAuth(gn.NewJWTAuth(cfg.EngineApiSecret))
opts := []client.RPCOption{
client.WithGethRPCOptions(auth),
client.WithBatchCallTimeout(10 * time.Minute),
client.WithCallTimeout(10 * time.Minute),
}
// Add some retries around connecting to the dest node as it starts up at the same time as the replayor
engineApi, err := retry.Do(ctx, 120, retry.Fixed(time.Second), func() (*sources.EngineAPIClient, error) {
logger.Info("dialing destination engine api url", "url", cfg.EngineApiUrl)
l2Node, err := client.NewRPC(ctx, logger, cfg.EngineApiUrl, opts...)
if err != nil {
logger.Info("waiting for geth (rpc) to start")
return nil, err
}
engineApi := sources.NewEngineAPIClientWithTimeout(l2Node, logger, cfg.RollupConfig, 10*time.Minute)
return engineApi, nil
})
if err != nil {
return Clients{}, err
}
return Clients{
SourceNode: sourceNode,
DestNode: destNode,
EngineApi: engineApi,
}, nil
}