@@ -9,11 +9,16 @@ import (
9
9
"syscall"
10
10
"time"
11
11
12
+ "github.com/NethermindEth/juno/db/pebble"
13
+ "github.com/NethermindEth/juno/l1data"
12
14
"github.com/NethermindEth/juno/node"
15
+ "github.com/NethermindEth/juno/syncl1"
13
16
"github.com/NethermindEth/juno/utils"
17
+ "github.com/ethereum/go-ethereum/ethclient"
14
18
"github.com/mitchellh/mapstructure"
15
19
"github.com/spf13/cobra"
16
20
"github.com/spf13/viper"
21
+ "tailscale.com/logtail/backoff"
17
22
)
18
23
19
24
const greeting = `
@@ -126,6 +131,43 @@ func main() {
126
131
n .Run (cmd .Context ())
127
132
return nil
128
133
})
134
+ endBlock := new (uint64 )
135
+ cmd .AddCommand (newSyncL1Cmd (endBlock , func (cmd * cobra.Command , _ []string ) error {
136
+ if config .Network != utils .MAINNET {
137
+ return fmt .Errorf ("syncing from L1 is only supported on mainnet right now" )
138
+ }
139
+
140
+ log , err := utils .NewZapLogger (config .LogLevel , config .Colour )
141
+ if err != nil {
142
+ return fmt .Errorf ("create logger: %v" , err )
143
+ }
144
+ database , err := pebble .New (config .DatabasePath , log )
145
+ if err != nil {
146
+ return fmt .Errorf ("open DB: %v" , err )
147
+ }
148
+ ethClient , err := ethclient .Dial (config .EthNode )
149
+ if err != nil {
150
+ return fmt .Errorf ("dial %s: %v" , config .EthNode , err )
151
+ }
152
+ l1Data , err := l1data .New (ethClient )
153
+ if err != nil {
154
+ return fmt .Errorf ("create l1 client: %v" , err )
155
+ }
156
+ l1Data .WithBackoff (backoff .NewBackoff ("syncl1" , func (format string , a ... any ) {
157
+ log .Warnw (fmt .Sprintf (format , a ... ))
158
+ }, time .Minute ))
159
+ fetcher := l1data .NewStateDiffFetcher (l1Data )
160
+ s , err := syncl1 .New (database , l1Data , fetcher , syncl1 .MainnetConfig , * endBlock , log )
161
+ if err != nil {
162
+ return fmt .Errorf ("new l1 synchronizer: %v" , err )
163
+ }
164
+
165
+ if err := s .Run (cmd .Context ()); err != nil {
166
+ return fmt .Errorf ("sync l1: %v" , err )
167
+ }
168
+
169
+ return nil
170
+ }))
129
171
130
172
if err := cmd .ExecuteContext (ctx ); err != nil {
131
173
os .Exit (1 )
@@ -150,9 +192,9 @@ func NewCmd(config *node.Config, run func(*cobra.Command, []string) error) *cobr
150
192
var cfgFile string
151
193
var cwdErr error
152
194
153
- // PreRunE populates the configuration struct from the Cobra flags and Viper configuration.
195
+ // PersistentPreRunE populates the configuration struct from the Cobra flags and Viper configuration.
154
196
// This is called in step 3 of the process described above.
155
- junoCmd .PreRunE = func (cmd * cobra.Command , _ []string ) error {
197
+ junoCmd .PersistentPreRunE = func (cmd * cobra.Command , _ []string ) error {
156
198
// If we couldn't find the current working directory and the database path is empty,
157
199
// return the error.
158
200
if cwdErr != nil && config .DatabasePath == "" {
@@ -193,20 +235,20 @@ func NewCmd(config *node.Config, run func(*cobra.Command, []string) error) *cobr
193
235
defaultNetwork := utils .MAINNET
194
236
195
237
junoCmd .Flags ().StringVar (& cfgFile , configF , defaultConfig , configFlagUsage )
196
- junoCmd .Flags ().Var (& defaultLogLevel , logLevelF , logLevelFlagUsage )
238
+ junoCmd .PersistentFlags ().Var (& defaultLogLevel , logLevelF , logLevelFlagUsage )
197
239
junoCmd .Flags ().Bool (httpF , defaultHTTP , httpUsage )
198
240
junoCmd .Flags ().String (httpHostF , defaulHost , httpHostUsage )
199
241
junoCmd .Flags ().Uint16 (httpPortF , defaultHTTPPort , httpPortUsage )
200
242
junoCmd .Flags ().Bool (wsF , defaultWS , wsUsage )
201
243
junoCmd .Flags ().String (wsHostF , defaulHost , wsHostUsage )
202
244
junoCmd .Flags ().Uint16 (wsPortF , defaultWSPort , wsPortUsage )
203
- junoCmd .Flags ().String (dbPathF , defaultDBPath , dbPathUsage )
204
- junoCmd .Flags ().Var (& defaultNetwork , networkF , networkUsage )
205
- junoCmd .Flags ().String (ethNodeF , defaultEthNode , ethNodeUsage )
245
+ junoCmd .PersistentFlags ().String (dbPathF , defaultDBPath , dbPathUsage )
246
+ junoCmd .PersistentFlags ().Var (& defaultNetwork , networkF , networkUsage )
247
+ junoCmd .PersistentFlags ().String (ethNodeF , defaultEthNode , ethNodeUsage )
206
248
junoCmd .Flags ().Bool (pprofF , defaultPprof , pprofUsage )
207
249
junoCmd .Flags ().String (pprofHostF , defaulHost , pprofHostUsage )
208
250
junoCmd .Flags ().Uint16 (pprofPortF , defaultPprofPort , pprofPortUsage )
209
- junoCmd .Flags ().Bool (colourF , defaultColour , colourUsage )
251
+ junoCmd .PersistentFlags ().Bool (colourF , defaultColour , colourUsage )
210
252
junoCmd .Flags ().Duration (pendingPollIntervalF , defaultPendingPollInterval , pendingPollIntervalUsage )
211
253
junoCmd .Flags ().Bool (p2pF , defaultP2p , p2pUsage )
212
254
junoCmd .Flags ().String (p2pAddrF , defaultP2pAddr , p2PAddrUsage )
0 commit comments