-
Notifications
You must be signed in to change notification settings - Fork 106
/
Copy pathmt_batcher.go
233 lines (210 loc) · 7.19 KB
/
mt_batcher.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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
package mt_batcher
import (
"context"
"errors"
"strings"
"github.com/ethereum/go-ethereum/accounts/abi"
"github.com/ethereum/go-ethereum/accounts/abi/bind"
ethc "github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/log"
"github.com/mantlenetworkio/mantle/l2geth/common"
"github.com/mantlenetworkio/mantle/mt-batcher/bindings"
"github.com/mantlenetworkio/mantle/mt-batcher/l1l2client"
"github.com/mantlenetworkio/mantle/mt-batcher/metrics"
common2 "github.com/mantlenetworkio/mantle/mt-batcher/services/common"
"github.com/mantlenetworkio/mantle/mt-batcher/services/restorer"
"github.com/mantlenetworkio/mantle/mt-batcher/services/sequencer"
"github.com/Layr-Labs/datalayr/common/logging"
"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
}
mantleBatch, err := NewMantleBatch(cfg)
if err != nil {
return err
}
if err := mantleBatch.Start(); err != nil {
return err
}
defer mantleBatch.Stop()
log.Debug("mantle batcher started")
<-(chan struct{})(nil)
return nil
}
}
type MantleBatch struct {
ctx context.Context
cfg Config
sequencerDriver *sequencer.Driver
daService *restorer.DaService
metrics *metrics.MtBatchMetrics
}
func NewMantleBatch(cfg Config) (*MantleBatch, error) {
ctx := context.Background()
sequencerPrivKey, _, err := common2.ParseWalletPrivKeyAndContractAddr(
"MtBatcher", cfg.Mnemonic, cfg.SequencerHDPath,
cfg.PrivateKey, cfg.EigenContractAddress, cfg.Passphrase,
)
if err != nil {
return nil, err
}
mtFeePrivateKey, _, err := common2.ParseWalletPrivKeyAndContractAddr(
"MtBatcher", cfg.FeeMnemonic, cfg.FeeHDPath,
cfg.FeePrivateKey, cfg.EigenFeeContractAddress, cfg.Passphrase,
)
if err != nil {
return nil, err
}
l1Client, err := l1l2client.L1EthClientWithTimeout(ctx, cfg.L1EthRpc, cfg.DisableHTTP2)
if err != nil {
return nil, err
}
chainID, err := l1Client.ChainID(ctx)
if err != nil {
return nil, err
}
l2Client, err := l1l2client.DialL2EthClientWithTimeout(ctx, cfg.L2MtlRpc, cfg.DisableHTTP2)
if err != nil {
return nil, err
}
eigenContract, err := bindings.NewBVMEigenDataLayrChain(
ethc.Address(common.HexToAddress(cfg.EigenContractAddress)), l1Client,
)
if err != nil {
log.Error("MtBatcher binding eigenda contract fail", "err", err)
return nil, err
}
logger, err := logging.GetLogger(cfg.EigenLogConfig)
if err != nil {
return nil, err
}
parsed, err := abi.JSON(strings.NewReader(
bindings.BVMEigenDataLayrChainABI,
))
if err != nil {
log.Error("MtBatcher parse eigenda contract abi fail", "err", err)
return nil, err
}
rawEigenContract := bind.NewBoundContract(
ethc.Address(common.HexToAddress(cfg.EigenContractAddress)), parsed, l1Client, l1Client,
l1Client,
)
log.Debug("contract init success", "EigenContractAddress", cfg.EigenContractAddress)
eigenFeeContract, err := bindings.NewBVMEigenDataLayrFee(
ethc.Address(common.HexToAddress(cfg.EigenFeeContractAddress)),
l1Client,
)
if err != nil {
log.Error("MtBatcher binding eigen fee contract fail", "err", err)
return nil, err
}
feeParsed, err := abi.JSON(strings.NewReader(
bindings.BVMEigenDataLayrFeeABI,
))
if err != nil {
log.Error("MtBatcher parse eigen fee contract abi fail", "err", err)
return nil, err
}
rawEigenFeeContract := bind.NewBoundContract(
ethc.Address(common.HexToAddress(cfg.EigenFeeContractAddress)), feeParsed, l1Client, l1Client,
l1Client,
)
driverConfig := &sequencer.DriverConfig{
L1Client: l1Client,
L2Client: l2Client,
L1ChainID: chainID,
DtlClientUrl: cfg.DtlClientUrl,
EigenDaContract: eigenContract,
RawEigenContract: rawEigenContract,
EigenFeeContract: eigenFeeContract,
RawEigenFeeContract: rawEigenFeeContract,
FeeModelEnable: cfg.FeeModelEnable,
FeeSizeSec: cfg.FeeSizeSec,
FeePerBytePerTime: cfg.FeePerBytePerTime,
Logger: logger,
PrivKey: sequencerPrivKey,
FeePrivKey: mtFeePrivateKey,
BlockOffset: cfg.BlockOffset,
RollUpMinTxn: cfg.RollUpMinTxn,
RollUpMaxSize: cfg.RollUpMaxSize,
EigenLayerNode: cfg.EigenLayerNode,
DataStoreDuration: uint64(cfg.DataStoreDuration),
DataStoreTimeout: cfg.DataStoreTimeout,
DisperserSocket: cfg.DisperserEndpoint,
MainWorkerPollInterval: cfg.MainWorkerPollInterval,
CheckerWorkerPollInterval: cfg.CheckerWorkerPollInterval,
FeeWorkerPollInterval: cfg.FeeWorkerPollInterval,
GraphPollingDuration: cfg.PollingDuration,
DbPath: cfg.DbPath,
CheckerBatchIndex: cfg.CheckerBatchIndex,
CheckerEnable: cfg.CheckerEnable,
GraphProvider: cfg.GraphProvider,
ResubmissionTimeout: cfg.ResubmissionTimeout,
NumConfirmations: cfg.NumConfirmations,
SafeAbortNonceTooLowCount: cfg.SafeAbortNonceTooLowCount,
Metrics: metrics.NewMtBatchBase(),
EnableHsm: cfg.EnableHsm,
HsmAddress: cfg.HsmAddress,
HsmAPIName: cfg.HsmAPIName,
HsmCreden: cfg.HsmCreden,
HsmFeeAPIName: cfg.HsmFeeAPIName,
HsmFeeAddress: cfg.HsmFeeAddress,
MinTimeoutRollupTxn: cfg.MinTimeoutRollupTxn,
RollupTimeout: cfg.RollupTimeout,
}
if cfg.MinTimeoutRollupTxn >= cfg.RollUpMinTxn {
log.Error("new driver fail", "err", "config value error : MinTimeoutRollupTxn should less than RollUpMinTxn MinTimeoutRollupTxn(%v)>RollUpMinTxn(%v)", cfg.MinTimeoutRollupTxn, cfg.RollUpMinTxn)
return nil, errors.New("config value error : MinTimeoutRollupTxn should less than RollUpMinTxn")
}
log.Debug("hsm",
"enablehsm", driverConfig.EnableHsm, "hsmaddress", driverConfig.HsmAddress,
"hsmapiname", driverConfig.HsmAPIName, "HsmFeeAPIName", driverConfig.HsmFeeAPIName, "HsmFeeAddress", driverConfig.HsmFeeAddress)
driver, err := sequencer.NewDriver(ctx, driverConfig)
if err != nil {
log.Error("new driver fail", "err", err)
return nil, err
}
daServiceConfig := &restorer.DaServiceConfig{
EigenContract: eigenContract,
RetrieverSocket: cfg.RetrieverSocket,
GraphProvider: cfg.GraphProvider,
DaServicePort: cfg.EigenDaHttpPort,
EigenLayerNode: cfg.EigenLayerNode,
}
daService, err := restorer.NewDaService(ctx, daServiceConfig)
if err != nil {
log.Error("new da http service fail", "err", err)
return nil, err
}
return &MantleBatch{
ctx: ctx,
cfg: cfg,
sequencerDriver: driver,
daService: daService,
}, nil
}
func (mb *MantleBatch) Start() error {
if mb.cfg.MtlBatcherEnable {
if err := mb.sequencerDriver.Start(); err != nil {
return err
}
}
go func() {
err := mb.daService.Start()
if err != nil {
log.Error("da server failed to start", "err", err)
}
}()
if mb.cfg.MetricsServerEnable {
go metrics.StartServer(mb.cfg.MetricsHostname, mb.cfg.MetricsPort)
}
return nil
}
func (mb *MantleBatch) Stop() {
mb.daService.Stop()
mb.sequencerDriver.Stop()
}