-
Notifications
You must be signed in to change notification settings - Fork 2.1k
/
config.go
189 lines (168 loc) · 6.87 KB
/
config.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
package processor
import (
"crypto/ecdsa"
"fmt"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/crypto"
"github.com/taikoxyz/taiko-mono/packages/relayer/cmd/flags"
"github.com/taikoxyz/taiko-mono/packages/relayer/pkg/db"
"github.com/taikoxyz/taiko-mono/packages/relayer/pkg/queue"
"github.com/taikoxyz/taiko-mono/packages/relayer/pkg/queue/rabbitmq"
"github.com/urfave/cli/v2"
"gorm.io/driver/mysql"
"gorm.io/gorm"
"gorm.io/gorm/logger"
)
// hopConfig is a config struct that must be provided for an individual
// hop, when the processor is not configured to only process srcChain => destChain.
// for instance, when going from L2A to L2B, we have a hop of the shared "L1".
// the hopConfig in this case should be the L1 signalServiceAddress, taikoAddress,
// and rpcURL. If we have multiple hops, such as an L3 deployed on L2A to L2B,
// the hops would be L2A and L1, and multiple configs should be passed in.
type hopConfig struct {
signalServiceAddress common.Address
taikoAddress common.Address
rpcURL string
}
// Config is a struct used to initialize a processor.
type Config struct {
// address configs
SrcSignalServiceAddress common.Address
DestBridgeAddress common.Address
DestERC721VaultAddress common.Address
DestERC20VaultAddress common.Address
DestERC1155VaultAddress common.Address
DestTaikoAddress common.Address
// private key
ProcessorPrivateKey *ecdsa.PrivateKey
TargetTxHash *common.Hash
// processing configs
HeaderSyncInterval uint64
Confirmations uint64
ConfirmationsTimeout uint64
ProfitableOnly bool
EnableTaikoL2 bool
// backoff configs
BackoffRetryInterval uint64
BackOffMaxRetrys uint64
// db configs
DatabaseUsername string
DatabasePassword string
DatabaseName string
DatabaseHost string
DatabaseMaxIdleConns uint64
DatabaseMaxOpenConns uint64
DatabaseMaxConnLifetime uint64
// queue configs
QueueUsername string
QueuePassword string
QueueHost string
QueuePort uint64
QueuePrefetch uint64
// rpc configs
SrcRPCUrl string
DestRPCUrl string
ETHClientTimeout uint64
OpenQueueFunc func() (queue.Queue, error)
OpenDBFunc func() (DB, error)
hopConfigs []hopConfig
CacheOption int
}
// NewConfigFromCliContext creates a new config instance from command line flags.
func NewConfigFromCliContext(c *cli.Context) (*Config, error) {
processorPrivateKey, err := crypto.ToECDSA(
common.Hex2Bytes(c.String(flags.ProcessorPrivateKey.Name)),
)
if err != nil {
return nil, fmt.Errorf("invalid processorPrivateKey: %w", err)
}
hopSignalServiceAddresses := c.StringSlice(flags.HopSignalServiceAddresses.Name)
hopTaikoAddresses := c.StringSlice(flags.HopTaikoAddresses.Name)
hopRPCUrls := c.StringSlice(flags.HopRPCUrls.Name)
if len(hopSignalServiceAddresses) != len(hopTaikoAddresses) ||
len(hopSignalServiceAddresses) != len(hopRPCUrls) ||
len(hopTaikoAddresses) != len(hopRPCUrls) {
return nil, fmt.Errorf("all hop parameters must be of same length")
}
hopConfigs := []hopConfig{}
for i, hopSignalServiceAddress := range hopSignalServiceAddresses {
hopConfigs = append(hopConfigs, hopConfig{
signalServiceAddress: common.HexToAddress(hopSignalServiceAddress),
rpcURL: hopRPCUrls[i],
taikoAddress: common.HexToAddress(hopTaikoAddresses[i]),
})
}
var targetTxHash *common.Hash
if c.IsSet(flags.TargetTxHash.Name) {
hash := common.HexToHash(c.String(flags.TargetTxHash.Name))
targetTxHash = &hash
}
return &Config{
hopConfigs: hopConfigs,
ProcessorPrivateKey: processorPrivateKey,
SrcSignalServiceAddress: common.HexToAddress(c.String(flags.SrcSignalServiceAddress.Name)),
DestTaikoAddress: common.HexToAddress(c.String(flags.DestTaikoAddress.Name)),
DestBridgeAddress: common.HexToAddress(c.String(flags.DestBridgeAddress.Name)),
DestERC721VaultAddress: common.HexToAddress(c.String(flags.DestERC721VaultAddress.Name)),
DestERC20VaultAddress: common.HexToAddress(c.String(flags.DestERC20VaultAddress.Name)),
DestERC1155VaultAddress: common.HexToAddress(c.String(flags.DestERC1155VaultAddress.Name)),
DatabaseUsername: c.String(flags.DatabaseUsername.Name),
DatabasePassword: c.String(flags.DatabasePassword.Name),
DatabaseName: c.String(flags.DatabaseName.Name),
DatabaseHost: c.String(flags.DatabaseHost.Name),
DatabaseMaxIdleConns: c.Uint64(flags.DatabaseMaxIdleConns.Name),
DatabaseMaxOpenConns: c.Uint64(flags.DatabaseMaxOpenConns.Name),
DatabaseMaxConnLifetime: c.Uint64(flags.DatabaseConnMaxLifetime.Name),
QueueUsername: c.String(flags.QueueUsername.Name),
QueuePassword: c.String(flags.QueuePassword.Name),
QueuePort: c.Uint64(flags.QueuePort.Name),
QueueHost: c.String(flags.QueueHost.Name),
QueuePrefetch: c.Uint64(flags.QueuePrefetchCount.Name),
SrcRPCUrl: c.String(flags.SrcRPCUrl.Name),
DestRPCUrl: c.String(flags.DestRPCUrl.Name),
HeaderSyncInterval: c.Uint64(flags.HeaderSyncInterval.Name),
Confirmations: c.Uint64(flags.Confirmations.Name),
ConfirmationsTimeout: c.Uint64(flags.ConfirmationTimeout.Name),
EnableTaikoL2: c.Bool(flags.EnableTaikoL2.Name),
ProfitableOnly: c.Bool(flags.ProfitableOnly.Name),
BackoffRetryInterval: c.Uint64(flags.BackOffRetryInterval.Name),
BackOffMaxRetrys: c.Uint64(flags.BackOffMaxRetrys.Name),
ETHClientTimeout: c.Uint64(flags.ETHClientTimeout.Name),
TargetTxHash: targetTxHash,
CacheOption: c.Int(flags.CacheOption.Name),
OpenDBFunc: func() (DB, error) {
return db.OpenDBConnection(db.DBConnectionOpts{
Name: c.String(flags.DatabaseUsername.Name),
Password: c.String(flags.DatabasePassword.Name),
Database: c.String(flags.DatabaseName.Name),
Host: c.String(flags.DatabaseHost.Name),
MaxIdleConns: c.Uint64(flags.DatabaseMaxIdleConns.Name),
MaxOpenConns: c.Uint64(flags.DatabaseMaxOpenConns.Name),
MaxConnLifetime: c.Uint64(flags.DatabaseConnMaxLifetime.Name),
OpenFunc: func(dsn string) (*db.DB, error) {
gormDB, err := gorm.Open(mysql.Open(dsn), &gorm.Config{
Logger: logger.Default.LogMode(logger.Silent),
})
if err != nil {
return nil, err
}
return db.New(gormDB), nil
},
})
},
OpenQueueFunc: func() (queue.Queue, error) {
opts := queue.NewQueueOpts{
Username: c.String(flags.QueueUsername.Name),
Password: c.String(flags.QueuePassword.Name),
Host: c.String(flags.QueueHost.Name),
Port: c.String(flags.QueuePort.Name),
PrefetchCount: c.Uint64(flags.QueuePrefetchCount.Name),
}
q, err := rabbitmq.NewQueue(opts)
if err != nil {
return nil, err
}
return q, nil
},
}, nil
}