@@ -67,7 +67,7 @@ func NewL2WatcherClient(ctx context.Context, client *ethclient.Client, confirmat
67
67
savedHeight = 0
68
68
}
69
69
70
- return & WatcherClient {
70
+ w := WatcherClient {
71
71
ctx : ctx ,
72
72
Client : client ,
73
73
orm : orm ,
@@ -79,6 +79,75 @@ func NewL2WatcherClient(ctx context.Context, client *ethclient.Client, confirmat
79
79
stopped : 0 ,
80
80
batchProposer : newBatchProposer (bpCfg , orm ),
81
81
}
82
+
83
+ // Initialize genesis before we do anything else
84
+ if err := w .initializeGenesis (); err != nil {
85
+ panic (fmt .Sprintf ("failed to initialize L2 genesis batch, err: %v" , err ))
86
+ }
87
+
88
+ return & w
89
+ }
90
+
91
+ func (w * WatcherClient ) initializeGenesis () error {
92
+ if count , err := w .orm .GetBatchCount (); err != nil {
93
+ return fmt .Errorf ("failed to get batch count: %v" , err )
94
+ } else if count > 0 {
95
+ log .Info ("genesis already imported" )
96
+ return nil
97
+ }
98
+
99
+ genesis , err := w .HeaderByNumber (w .ctx , big .NewInt (0 ))
100
+ if err != nil {
101
+ return fmt .Errorf ("failed to retrieve L2 genesis header: %v" , err )
102
+ }
103
+
104
+ // EIP1559 is disabled so the RPC won't return baseFeePerGas. However, l2geth
105
+ // still uses BaseFee when calculating the block hash. If we keep it as <nil>
106
+ // here the genesis hash will not match.
107
+ genesis .BaseFee = big .NewInt (0 )
108
+
109
+ log .Info ("retrieved L2 genesis header" , "hash" , genesis .Hash ().String ())
110
+
111
+ trace := & types.BlockTrace {
112
+ Coinbase : nil ,
113
+ Header : genesis ,
114
+ Transactions : []* types.TransactionData {},
115
+ StorageTrace : nil ,
116
+ ExecutionResults : []* types.ExecutionResult {},
117
+ MPTWitness : nil ,
118
+ }
119
+
120
+ if err := w .orm .InsertBlockTraces ([]* types.BlockTrace {trace }); err != nil {
121
+ return fmt .Errorf ("failed to insert block traces: %v" , err )
122
+ }
123
+
124
+ blocks , err := w .orm .GetUnbatchedBlocks (map [string ]interface {}{})
125
+ if err != nil {
126
+ return err
127
+ }
128
+
129
+ if len (blocks ) != 1 {
130
+ return fmt .Errorf ("unexpected number of unbatched blocks in db, expected: 1, actual: %v" , len (blocks ))
131
+ }
132
+
133
+ batchID , err := w .batchProposer .createBatchForBlocks (blocks )
134
+ if err != nil {
135
+ return fmt .Errorf ("failed to create batch: %v" , err )
136
+ }
137
+
138
+ err = w .orm .UpdateProvingStatus (batchID , orm .ProvingTaskProved )
139
+ if err != nil {
140
+ return fmt .Errorf ("failed to update genesis batch proving status: %v" , err )
141
+ }
142
+
143
+ err = w .orm .UpdateRollupStatus (w .ctx , batchID , orm .RollupFinalized )
144
+ if err != nil {
145
+ return fmt .Errorf ("failed to update genesis batch rollup status: %v" , err )
146
+ }
147
+
148
+ log .Info ("successfully imported genesis batch" )
149
+
150
+ return nil
82
151
}
83
152
84
153
// Start the Listening process
0 commit comments