@@ -2,6 +2,7 @@ package gnosis
2
2
3
3
import (
4
4
"context"
5
+ "fmt"
5
6
"math"
6
7
"math/big"
7
8
@@ -24,8 +25,7 @@ import (
24
25
)
25
26
26
27
const (
27
- ValidatorRegistrationMessageVersion = 0
28
- maxRequestBlockRange = 10_000
28
+ maxRequestBlockRange = 10_000
29
29
)
30
30
31
31
type ValidatorSyncer struct {
@@ -150,61 +150,83 @@ func (v *ValidatorSyncer) filterEvents(
150
150
Uint ("log-index" , event .Raw .Index ).
151
151
Logger ()
152
152
153
- msg := new (validatorregistry.RegistrationMessage )
153
+ msg := new (validatorregistry.AggregateRegistrationMessage )
154
154
err := msg .Unmarshal (event .Message )
155
155
if err != nil {
156
156
evLog .Warn ().
157
157
Err (err ).
158
158
Msg ("failed to unmarshal registration message" )
159
159
continue
160
160
}
161
- evLog = evLog .With ().Uint64 ("validator-index" , msg .ValidatorIndex ).Logger ()
162
161
163
162
if ! checkStaticRegistrationMessageFields (msg , v .ChainID , event .Raw .Address , evLog ) {
164
163
continue
165
164
}
166
165
167
- latestNonce , err := db .GetValidatorRegistrationNonceBefore (ctx , database.GetValidatorRegistrationNonceBeforeParams {
168
- ValidatorIndex : int64 (msg .ValidatorIndex ),
169
- BlockNumber : int64 (event .Raw .BlockNumber ),
170
- TxIndex : int64 (event .Raw .TxIndex ),
171
- LogIndex : int64 (event .Raw .Index ),
172
- })
173
- if err != nil && err != pgx .ErrNoRows {
174
- return nil , errors .Wrapf (err , "failed to query latest nonce for validator %d" , msg .ValidatorIndex )
175
- }
176
- if err == pgx .ErrNoRows {
177
- latestNonce = - 1
178
- }
179
- if msg .Nonce > math .MaxInt64 || int64 (msg .Nonce ) <= latestNonce {
180
- evLog .Warn ().
181
- Uint64 ("nonce" , msg .Nonce ).
182
- Int64 ("latest-nonce" , latestNonce ).
183
- Msg ("ignoring registration message with invalid nonce" )
184
- continue
185
- }
166
+ pubKeys := make ([]* blst.P1Affine , 0 )
167
+ for _ , validatorIndex := range msg .ValidatorIndices () {
168
+ evLog = evLog .With ().Int64 ("validator-index" , validatorIndex ).Logger ()
169
+ latestNonce , err := db .GetValidatorRegistrationNonceBefore (ctx , database.GetValidatorRegistrationNonceBeforeParams {
170
+ ValidatorIndex : validatorIndex ,
171
+ BlockNumber : int64 (event .Raw .BlockNumber ),
172
+ TxIndex : int64 (event .Raw .TxIndex ),
173
+ LogIndex : int64 (event .Raw .Index ),
174
+ })
175
+ if err != nil && err != pgx .ErrNoRows {
176
+ return nil , errors .Wrapf (err , "failed to query latest nonce for validator %d" , msg .ValidatorIndex )
177
+ }
178
+ if err == pgx .ErrNoRows {
179
+ latestNonce = - 1
180
+ }
186
181
187
- validator , err := v .BeaconAPIClient .GetValidatorByIndex (ctx , "head" , msg .ValidatorIndex )
188
- if err != nil {
189
- return nil , errors .Wrapf (err , "failed to get validator %d" , msg .ValidatorIndex )
190
- }
191
- if validator == nil {
192
- evLog .Warn ().Msg ("ignoring registration message for unknown validator" )
193
- continue
194
- }
195
- pubkey , err := validator .Data .Validator .GetPubkey ()
196
- if err != nil {
197
- return nil , errors .Wrapf (err , "failed to get pubkey of validator %d" , msg .ValidatorIndex )
182
+ if msg .Nonce > math .MaxInt32 || int64 (msg .Nonce ) <= latestNonce {
183
+ evLog .Warn ().
184
+ Uint32 ("nonce" , msg .Nonce ).
185
+ Int64 ("latest-nonce" , latestNonce ).
186
+ Msg ("ignoring registration message with invalid nonce" )
187
+ continue
188
+ }
189
+
190
+ validator , err := v .BeaconAPIClient .GetValidatorByIndex (ctx , "head" , uint64 (validatorIndex ))
191
+ if err != nil {
192
+ return nil , errors .Wrapf (err , "failed to get validator %d" , msg .ValidatorIndex )
193
+ }
194
+ if validator == nil {
195
+ evLog .Warn ().Msg ("ignoring registration message for unknown validator" )
196
+ continue
197
+ }
198
+ pubkey , err := validator .Data .Validator .GetPubkey ()
199
+ if err != nil {
200
+ return nil , errors .Wrapf (err , "failed to get pubkey of validator %d" , msg .ValidatorIndex )
201
+ }
202
+ pubKeys = append (pubKeys , pubkey )
198
203
}
204
+
199
205
sig := new (blst.P2Affine ).Uncompress (event .Signature )
200
206
if sig == nil {
201
207
evLog .Warn ().Msg ("ignoring registration message with undecodable signature" )
202
208
continue
203
209
}
204
- validSignature := validatorregistry .VerifySignature (sig , pubkey , msg )
205
- if ! validSignature {
206
- evLog .Warn ().Msg ("ignoring registration message with invalid signature" )
207
- continue
210
+
211
+ if msg .Version == validatorregistry .LegacyValidatorRegistrationMessageVersion {
212
+ msg := new (validatorregistry.LegacyRegistrationMessage )
213
+ err := msg .Unmarshal (event .Message )
214
+ if err != nil {
215
+ evLog .Warn ().
216
+ Err (err ).
217
+ Msg ("failed to unmarshal registration message" )
218
+ continue
219
+ }
220
+ if validSignature := validatorregistry .VerifySignature (sig , pubKeys [0 ], msg ); ! validSignature {
221
+ evLog .Warn ().Msg ("ignoring registration message with invalid signature" )
222
+ continue
223
+ }
224
+ } else {
225
+ validSignature := validatorregistry .VerifyAggregateSignature (sig , pubKeys , msg )
226
+ if ! validSignature {
227
+ evLog .Warn ().Msg ("ignoring registration message with invalid signature" )
228
+ continue
229
+ }
208
230
}
209
231
210
232
filteredEvents = append (filteredEvents , event )
@@ -215,37 +237,41 @@ func (v *ValidatorSyncer) filterEvents(
215
237
func (v * ValidatorSyncer ) insertEvents (ctx context.Context , tx pgx.Tx , events []* validatorRegistryBindings.ValidatorregistryUpdated ) error {
216
238
db := database .New (tx )
217
239
for _ , event := range events {
218
- msg := new (validatorregistry.RegistrationMessage )
240
+ msg := new (validatorregistry.AggregateRegistrationMessage )
219
241
err := msg .Unmarshal (event .Message )
220
242
if err != nil {
221
243
return errors .Wrap (err , "failed to unmarshal registration message" )
222
244
}
223
- err = db .InsertValidatorRegistration (ctx , database.InsertValidatorRegistrationParams {
224
- BlockNumber : int64 (event .Raw .BlockNumber ),
225
- BlockHash : event .Raw .BlockHash .Bytes (),
226
- TxIndex : int64 (event .Raw .TxIndex ),
227
- LogIndex : int64 (event .Raw .Index ),
228
- ValidatorIndex : int64 (msg .ValidatorIndex ),
229
- Nonce : int64 (msg .Nonce ),
230
- IsRegistration : msg .IsRegistration ,
231
- })
232
- if err != nil {
233
- return errors .Wrap (err , "failed to insert validator registration into db" )
245
+ for _ , validatorIndex := range msg .ValidatorIndices () {
246
+ err = db .InsertValidatorRegistration (ctx , database.InsertValidatorRegistrationParams {
247
+ BlockNumber : int64 (event .Raw .BlockNumber ),
248
+ BlockHash : event .Raw .BlockHash .Bytes (),
249
+ TxIndex : int64 (event .Raw .TxIndex ),
250
+ LogIndex : int64 (event .Raw .Index ),
251
+ ValidatorIndex : validatorIndex ,
252
+ Nonce : int64 (msg .Nonce ),
253
+ IsRegistration : msg .IsRegistration ,
254
+ })
255
+ if err != nil {
256
+ return errors .Wrap (err , "failed to insert validator registration into db" )
257
+ }
234
258
}
235
259
}
236
260
return nil
237
261
}
238
262
239
263
func checkStaticRegistrationMessageFields (
240
- msg * validatorregistry.RegistrationMessage ,
264
+ msg * validatorregistry.AggregateRegistrationMessage ,
241
265
chainID uint64 ,
242
266
validatorRegistryAddress common.Address ,
243
267
logger zerolog.Logger ,
244
268
) bool {
245
- if msg .Version != ValidatorRegistrationMessageVersion {
269
+ if msg .Version != validatorregistry .AggregateValidatorRegistrationMessageVersion &&
270
+ msg .Version != validatorregistry .LegacyValidatorRegistrationMessageVersion {
246
271
logger .Warn ().
247
272
Uint8 ("version" , msg .Version ).
248
- Uint8 ("expected-version" , ValidatorRegistrationMessageVersion ).
273
+ Str ("expected-version" , fmt .Sprintf ("%d or %d" , validatorregistry .LegacyValidatorRegistrationMessageVersion ,
274
+ validatorregistry .AggregateValidatorRegistrationMessageVersion )).
249
275
Uint64 ("validator-index" , msg .ValidatorIndex ).
250
276
Msg ("ignoring registration message with invalid version" )
251
277
return false
0 commit comments