@@ -63,14 +63,14 @@ var (
63
63
// signed beacon headers.
64
64
type CommitteeChain struct {
65
65
// chainmu guards against concurrent access to the canonicalStore structures
66
- // (updates, committees, fixedRoots ) and ensures that they stay consistent
66
+ // (updates, committees, fixedCommitteeRoots ) and ensures that they stay consistent
67
67
// with each other and with committeeCache.
68
- chainmu sync.RWMutex
69
- db ethdb.KeyValueStore
70
- updates * canonicalStore [* types.LightClientUpdate ]
71
- committees * canonicalStore [* types.SerializedSyncCommittee ]
72
- fixedRoots * canonicalStore [common.Hash ]
73
- committeeCache * lru.Cache [uint64 , syncCommittee ] // cache deserialized committees
68
+ chainmu sync.RWMutex
69
+ db ethdb.KeyValueStore
70
+ updates * canonicalStore [* types.LightClientUpdate ]
71
+ committees * canonicalStore [* types.SerializedSyncCommittee ]
72
+ fixedCommitteeRoots * canonicalStore [common.Hash ]
73
+ committeeCache * lru.Cache [uint64 , syncCommittee ] // cache deserialized committees
74
74
75
75
clock mclock.Clock // monotonic clock (simulated clock in tests)
76
76
unixNano func () int64 // system clock (simulated clock in tests)
@@ -91,10 +91,10 @@ func NewCommitteeChain(db ethdb.KeyValueStore, config *types.ChainConfig, signer
91
91
// clock source and signature verification for testing purposes.
92
92
func newCommitteeChain (db ethdb.KeyValueStore , config * types.ChainConfig , signerThreshold int , enforceTime bool , sigVerifier committeeSigVerifier , clock mclock.Clock , unixNano func () int64 ) * CommitteeChain {
93
93
var (
94
- fixedRootEncoder = func (root common.Hash ) ([]byte , error ) {
94
+ fixedCommitteeRootEncoder = func (root common.Hash ) ([]byte , error ) {
95
95
return root [:], nil
96
96
}
97
- fixedRootDecoder = func (enc []byte ) (root common.Hash , err error ) {
97
+ fixedCommitteeRootDecoder = func (enc []byte ) (root common.Hash , err error ) {
98
98
if len (enc ) != common .HashLength {
99
99
return common.Hash {}, errors .New ("incorrect length for committee root entry in the database" )
100
100
}
@@ -123,17 +123,17 @@ func newCommitteeChain(db ethdb.KeyValueStore, config *types.ChainConfig, signer
123
123
}
124
124
)
125
125
s := & CommitteeChain {
126
- fixedRoots : newCanonicalStore [common.Hash ](db , rawdb .FixedRootKey , fixedRootEncoder , fixedRootDecoder ),
127
- committees : newCanonicalStore [* types.SerializedSyncCommittee ](db , rawdb .SyncCommitteeKey , committeeEncoder , committeeDecoder ),
128
- updates : newCanonicalStore [* types.LightClientUpdate ](db , rawdb .BestUpdateKey , updateEncoder , updateDecoder ),
129
- committeeCache : lru.NewCache [uint64 , syncCommittee ](10 ),
130
- db : db ,
131
- sigVerifier : sigVerifier ,
132
- clock : clock ,
133
- unixNano : unixNano ,
134
- config : config ,
135
- signerThreshold : signerThreshold ,
136
- enforceTime : enforceTime ,
126
+ fixedCommitteeRoots : newCanonicalStore [common.Hash ](db , rawdb .FixedCommitteeRootKey , fixedCommitteeRootEncoder , fixedCommitteeRootDecoder ),
127
+ committees : newCanonicalStore [* types.SerializedSyncCommittee ](db , rawdb .SyncCommitteeKey , committeeEncoder , committeeDecoder ),
128
+ updates : newCanonicalStore [* types.LightClientUpdate ](db , rawdb .BestUpdateKey , updateEncoder , updateDecoder ),
129
+ committeeCache : lru.NewCache [uint64 , syncCommittee ](10 ),
130
+ db : db ,
131
+ sigVerifier : sigVerifier ,
132
+ clock : clock ,
133
+ unixNano : unixNano ,
134
+ config : config ,
135
+ signerThreshold : signerThreshold ,
136
+ enforceTime : enforceTime ,
137
137
minimumUpdateScore : types.UpdateScore {
138
138
SignerCount : uint32 (signerThreshold ),
139
139
SubPeriodIndex : params .SyncPeriodLength / 16 ,
@@ -169,15 +169,15 @@ func newCommitteeChain(db ethdb.KeyValueStore, config *types.ChainConfig, signer
169
169
170
170
// checkConstraints checks committee chain validity constraints
171
171
func (s * CommitteeChain ) checkConstraints () bool {
172
- isNotInFixedRootRange := func (r Range ) bool {
173
- return s .fixedRoots .periods .IsEmpty () ||
174
- r .Start < s .fixedRoots .periods .Start ||
175
- r .Start >= s .fixedRoots .periods .End
172
+ isNotInFixedCommitteeRootRange := func (r Range ) bool {
173
+ return s .fixedCommitteeRoots .periods .IsEmpty () ||
174
+ r .Start < s .fixedCommitteeRoots .periods .Start ||
175
+ r .Start >= s .fixedCommitteeRoots .periods .End
176
176
}
177
177
178
178
valid := true
179
179
if ! s .updates .periods .IsEmpty () {
180
- if isNotInFixedRootRange (s .updates .periods ) {
180
+ if isNotInFixedCommitteeRootRange (s .updates .periods ) {
181
181
log .Error ("Start update is not in the fixed roots range" )
182
182
valid = false
183
183
}
@@ -187,11 +187,11 @@ func (s *CommitteeChain) checkConstraints() bool {
187
187
}
188
188
}
189
189
if ! s .committees .periods .IsEmpty () {
190
- if isNotInFixedRootRange (s .committees .periods ) {
190
+ if isNotInFixedCommitteeRootRange (s .committees .periods ) {
191
191
log .Error ("Start committee is not in the fixed roots range" )
192
192
valid = false
193
193
}
194
- if s .committees .periods .End > s .fixedRoots .periods .End && s .committees .periods .End > s .updates .periods .End + 1 {
194
+ if s .committees .periods .End > s .fixedCommitteeRoots .periods .End && s .committees .periods .End > s .updates .periods .End + 1 {
195
195
log .Error ("Last committee is neither in the fixed roots range nor proven by updates" )
196
196
valid = false
197
197
}
@@ -209,10 +209,10 @@ func (s *CommitteeChain) Reset() {
209
209
}
210
210
}
211
211
212
- // AddFixedRoot sets a fixed committee root at the given period.
212
+ // AddFixedCommitteeRoot sets a fixed committee root at the given period.
213
213
// Note that the period where the first committee is added has to have a fixed
214
214
// root which can either come from a CheckpointData or a trusted source.
215
- func (s * CommitteeChain ) AddFixedRoot (period uint64 , root common.Hash ) error {
215
+ func (s * CommitteeChain ) AddFixedCommitteeRoot (period uint64 , root common.Hash ) error {
216
216
s .chainmu .Lock ()
217
217
defer s .chainmu .Unlock ()
218
218
@@ -222,7 +222,7 @@ func (s *CommitteeChain) AddFixedRoot(period uint64, root common.Hash) error {
222
222
223
223
batch := s .db .NewBatch ()
224
224
oldRoot := s .getCommitteeRoot (period )
225
- if ! s .fixedRoots .periods .CanExpand (period ) {
225
+ if ! s .fixedCommitteeRoots .periods .CanExpand (period ) {
226
226
// Note: the fixed committee root range should always be continuous and
227
227
// therefore the expected syncing method is to forward sync and optionally
228
228
// backward sync periods one by one, starting from a checkpoint. The only
@@ -238,8 +238,8 @@ func (s *CommitteeChain) AddFixedRoot(period uint64, root common.Hash) error {
238
238
// if the old root exists and matches the new one then it is guaranteed
239
239
// that the given period is after the existing fixed range and the roots
240
240
// in between can also be fixed.
241
- for p := s .fixedRoots .periods .End ; p < period ; p ++ {
242
- if err := s .fixedRoots .add (batch , p , s .getCommitteeRoot (p )); err != nil {
241
+ for p := s .fixedCommitteeRoots .periods .End ; p < period ; p ++ {
242
+ if err := s .fixedCommitteeRoots .add (batch , p , s .getCommitteeRoot (p )); err != nil {
243
243
return err
244
244
}
245
245
}
@@ -250,7 +250,7 @@ func (s *CommitteeChain) AddFixedRoot(period uint64, root common.Hash) error {
250
250
return err
251
251
}
252
252
}
253
- if err := s .fixedRoots .add (batch , period , root ); err != nil {
253
+ if err := s .fixedCommitteeRoots .add (batch , period , root ); err != nil {
254
254
return err
255
255
}
256
256
if err := batch .Write (); err != nil {
@@ -260,18 +260,18 @@ func (s *CommitteeChain) AddFixedRoot(period uint64, root common.Hash) error {
260
260
return nil
261
261
}
262
262
263
- // DeleteFixedRootsFrom deletes fixed roots starting from the given period.
263
+ // DeleteFixedCommitteeRootsFrom deletes fixed roots starting from the given period.
264
264
// It also maintains chain consistency, meaning that it also deletes updates and
265
265
// committees if they are no longer supported by a valid update chain.
266
- func (s * CommitteeChain ) DeleteFixedRootsFrom (period uint64 ) error {
266
+ func (s * CommitteeChain ) DeleteFixedCommitteeRootsFrom (period uint64 ) error {
267
267
s .chainmu .Lock ()
268
268
defer s .chainmu .Unlock ()
269
269
270
- if period >= s .fixedRoots .periods .End {
270
+ if period >= s .fixedCommitteeRoots .periods .End {
271
271
return nil
272
272
}
273
273
batch := s .db .NewBatch ()
274
- s .fixedRoots .deleteFrom (batch , period )
274
+ s .fixedCommitteeRoots .deleteFrom (batch , period )
275
275
if s .updates .periods .IsEmpty () || period <= s .updates .periods .Start {
276
276
// Note: the first period of the update chain should always be fixed so if
277
277
// the fixed root at the first update is removed then the entire update chain
@@ -327,7 +327,7 @@ func (s *CommitteeChain) AddCommittee(period uint64, committee *types.Serialized
327
327
if root != committee .Root () {
328
328
return ErrWrongCommitteeRoot
329
329
}
330
- if ! s .committees .periods .Includes (period ) {
330
+ if ! s .committees .periods .Contains (period ) {
331
331
if err := s .committees .add (s .db , period , committee ); err != nil {
332
332
return err
333
333
}
@@ -349,7 +349,7 @@ func (s *CommitteeChain) InsertUpdate(update *types.LightClientUpdate, nextCommi
349
349
defer s .chainmu .Unlock ()
350
350
351
351
period := update .AttestedHeader .Header .SyncPeriod ()
352
- if ! s .updates .periods .CanExpand (period ) || ! s .committees .periods .Includes (period ) {
352
+ if ! s .updates .periods .CanExpand (period ) || ! s .committees .periods .Contains (period ) {
353
353
return ErrInvalidPeriod
354
354
}
355
355
if s .minimumUpdateScore .BetterThan (update .Score ()) {
@@ -364,15 +364,15 @@ func (s *CommitteeChain) InsertUpdate(update *types.LightClientUpdate, nextCommi
364
364
}
365
365
return nil
366
366
}
367
- if s .fixedRoots .periods .Includes (period + 1 ) && reorg {
367
+ if s .fixedCommitteeRoots .periods .Contains (period + 1 ) && reorg {
368
368
return ErrCannotReorg
369
369
}
370
370
if ok , err := s .verifyUpdate (update ); err != nil {
371
371
return err
372
372
} else if ! ok {
373
373
return ErrInvalidUpdate
374
374
}
375
- addCommittee := ! s .committees .periods .Includes (period + 1 ) || reorg
375
+ addCommittee := ! s .committees .periods .Contains (period + 1 ) || reorg
376
376
if addCommittee {
377
377
if nextCommittee == nil {
378
378
return ErrNeedCommittee
@@ -426,14 +426,14 @@ func (s *CommitteeChain) rollback(period uint64) error {
426
426
if s .committees .periods .End > max {
427
427
max = s .committees .periods .End
428
428
}
429
- if s .fixedRoots .periods .End > max {
430
- max = s .fixedRoots .periods .End
429
+ if s .fixedCommitteeRoots .periods .End > max {
430
+ max = s .fixedCommitteeRoots .periods .End
431
431
}
432
432
for max > period {
433
433
max --
434
434
batch := s .db .NewBatch ()
435
435
s .deleteCommitteesFrom (batch , max )
436
- s .fixedRoots .deleteFrom (batch , max )
436
+ s .fixedCommitteeRoots .deleteFrom (batch , max )
437
437
if max > 0 {
438
438
s .updates .deleteFrom (batch , max - 1 )
439
439
}
@@ -449,7 +449,7 @@ func (s *CommitteeChain) rollback(period uint64) error {
449
449
// proven by a previous update or both. It returns an empty hash if the committee
450
450
// root is unknown.
451
451
func (s * CommitteeChain ) getCommitteeRoot (period uint64 ) common.Hash {
452
- if root , ok := s .fixedRoots .get (period ); ok || period == 0 {
452
+ if root , ok := s .fixedCommitteeRoots .get (period ); ok || period == 0 {
453
453
return root
454
454
}
455
455
if update , ok := s .updates .get (period - 1 ); ok {
0 commit comments