forked from ethereum-optimism/optimism
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathteleportr.go
185 lines (157 loc) · 4.46 KB
/
teleportr.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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
package teleportr
import (
"context"
"os"
"os/signal"
"syscall"
"time"
bsscore "github.com/ethereum-optimism/optimism/bss-core"
"github.com/ethereum-optimism/optimism/bss-core/dial"
"github.com/ethereum-optimism/optimism/bss-core/metrics"
"github.com/ethereum-optimism/optimism/bss-core/txmgr"
"github.com/ethereum-optimism/optimism/teleportr/db"
"github.com/ethereum-optimism/optimism/teleportr/drivers/disburser"
"github.com/ethereum/go-ethereum/log"
"github.com/urfave/cli"
)
func Main(gitVersion string) func(ctx *cli.Context) error {
return func(cliCtx *cli.Context) error {
cfg, err := NewConfig(cliCtx)
if err != nil {
return err
}
log.Info("Initializing teleportr")
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
var logHandler log.Handler
if cfg.LogTerminal {
logHandler = log.StreamHandler(os.Stdout, log.TerminalFormat(true))
} else {
logHandler = log.StreamHandler(os.Stdout, log.JSONFormat())
}
logLevel, err := log.LvlFromString(cfg.LogLevel)
if err != nil {
return err
}
log.Root().SetHandler(log.LvlFilterHandler(logLevel, logHandler))
disburserPrivKey, disburserAddr, err := bsscore.ParseWalletPrivKeyAndContractAddr(
"Teleportr", cfg.Mnemonic, cfg.DisburserHDPath,
cfg.DisburserPrivKey, cfg.DisburserAddress,
)
if err != nil {
return err
}
depositAddr, err := bsscore.ParseAddress(cfg.DepositAddress)
if err != nil {
return err
}
l1Client, err := dial.L1EthClientWithTimeout(ctx, cfg.L1EthRpc, cfg.DisableHTTP2)
if err != nil {
return err
}
defer l1Client.Close()
l2Client, err := dial.L1EthClientWithTimeout(ctx, cfg.L2EthRpc, cfg.DisableHTTP2)
if err != nil {
return err
}
defer l2Client.Close()
database, err := db.Open(db.Config{
Host: cfg.PostgresHost,
Port: uint16(cfg.PostgresPort),
User: cfg.PostgresUser,
Password: cfg.PostgresPassword,
DBName: cfg.PostgresDBName,
EnableSSL: cfg.PostgresEnableSSL,
})
if err != nil {
return err
}
defer database.Close()
if cfg.MetricsServerEnable {
go metrics.RunServer(cfg.MetricsHostname, cfg.MetricsPort)
}
chainID, err := l2Client.ChainID(ctx)
if err != nil {
return err
}
txManagerConfig := txmgr.Config{
ResubmissionTimeout: cfg.ResubmissionTimeout,
ReceiptQueryInterval: time.Second,
NumConfirmations: 1, // L2 insta confs
SafeAbortNonceTooLowCount: cfg.SafeAbortNonceTooLowCount,
}
teleportrDriver, err := disburser.NewDriver(disburser.Config{
Name: "Teleportr",
L1Client: l1Client,
L2Client: l2Client,
Database: database,
MaxTxSize: cfg.MaxL2TxSize,
NumConfirmations: cfg.NumDepositConfirmations,
DeployBlockNumber: cfg.DepositDeployBlockNumber,
FilterQueryMaxBlocks: cfg.FilterQueryMaxBlocks,
DepositAddr: depositAddr,
DisburserAddr: disburserAddr,
ChainID: chainID,
PrivKey: disburserPrivKey,
ChainMetricsEnable: cfg.MetricsServerEnable,
}, ctx)
if err != nil {
return err
}
teleportrService := bsscore.NewService(bsscore.ServiceConfig{
Context: ctx,
Driver: teleportrDriver,
PollInterval: cfg.PollInterval,
ClearPendingTx: false,
L1Client: l2Client,
TxManagerConfig: txManagerConfig,
})
services := []*bsscore.Service{teleportrService}
teleportr, err := bsscore.NewBatchSubmitter(ctx, cancel, services)
if err != nil {
return err
}
log.Info("Starting teleportr")
err = teleportr.Start()
if err != nil {
return err
}
defer teleportr.Stop()
log.Info("Teleportr started")
interruptChannel := make(chan os.Signal, 1)
signal.Notify(interruptChannel, []os.Signal{
os.Interrupt,
os.Kill,
syscall.SIGTERM,
syscall.SIGQUIT,
}...)
<-interruptChannel
return nil
}
}
func Migrate() func(ctx *cli.Context) error {
return func(cliCtx *cli.Context) error {
cfg, err := NewConfig(cliCtx)
if err != nil {
return err
}
log.Info("Initializing teleportr")
database, err := db.Open(db.Config{
Host: cfg.PostgresHost,
Port: uint16(cfg.PostgresPort),
User: cfg.PostgresUser,
Password: cfg.PostgresPassword,
DBName: cfg.PostgresDBName,
EnableSSL: cfg.PostgresEnableSSL,
})
if err != nil {
return err
}
log.Info("Migrating database")
if err := database.Migrate(); err != nil {
return err
}
log.Info("Done")
return nil
}
}