Skip to content

Commit 4d71502

Browse files
committed
beacon/light: more renames and cleanups
1 parent 623d195 commit 4d71502

File tree

5 files changed

+73
-77
lines changed

5 files changed

+73
-77
lines changed

beacon/light/checkpoint.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -53,11 +53,11 @@ func (c *CheckpointData) InitChain(chain *CommitteeChain) {
5353
}
5454
}
5555
period := c.Header.SyncPeriod()
56-
must(chain.DeleteFixedRootsFrom(period + 2))
57-
if chain.AddFixedRoot(period, c.CommitteeRoot) != nil {
56+
must(chain.DeleteFixedCommitteeRootsFrom(period + 2))
57+
if chain.AddFixedCommitteeRoot(period, c.CommitteeRoot) != nil {
5858
chain.Reset()
59-
must(chain.AddFixedRoot(period, c.CommitteeRoot))
59+
must(chain.AddFixedCommitteeRoot(period, c.CommitteeRoot))
6060
}
61-
must(chain.AddFixedRoot(period+1, common.Hash(c.CommitteeBranch[0])))
61+
must(chain.AddFixedCommitteeRoot(period+1, common.Hash(c.CommitteeBranch[0])))
6262
must(chain.AddCommittee(period, c.Committee))
6363
}

beacon/light/committee_chain.go

Lines changed: 45 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -63,14 +63,14 @@ var (
6363
// signed beacon headers.
6464
type CommitteeChain struct {
6565
// 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
6767
// 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
7474

7575
clock mclock.Clock // monotonic clock (simulated clock in tests)
7676
unixNano func() int64 // system clock (simulated clock in tests)
@@ -91,10 +91,10 @@ func NewCommitteeChain(db ethdb.KeyValueStore, config *types.ChainConfig, signer
9191
// clock source and signature verification for testing purposes.
9292
func newCommitteeChain(db ethdb.KeyValueStore, config *types.ChainConfig, signerThreshold int, enforceTime bool, sigVerifier committeeSigVerifier, clock mclock.Clock, unixNano func() int64) *CommitteeChain {
9393
var (
94-
fixedRootEncoder = func(root common.Hash) ([]byte, error) {
94+
fixedCommitteeRootEncoder = func(root common.Hash) ([]byte, error) {
9595
return root[:], nil
9696
}
97-
fixedRootDecoder = func(enc []byte) (root common.Hash, err error) {
97+
fixedCommitteeRootDecoder = func(enc []byte) (root common.Hash, err error) {
9898
if len(enc) != common.HashLength {
9999
return common.Hash{}, errors.New("incorrect length for committee root entry in the database")
100100
}
@@ -123,17 +123,17 @@ func newCommitteeChain(db ethdb.KeyValueStore, config *types.ChainConfig, signer
123123
}
124124
)
125125
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,
137137
minimumUpdateScore: types.UpdateScore{
138138
SignerCount: uint32(signerThreshold),
139139
SubPeriodIndex: params.SyncPeriodLength / 16,
@@ -169,15 +169,15 @@ func newCommitteeChain(db ethdb.KeyValueStore, config *types.ChainConfig, signer
169169

170170
// checkConstraints checks committee chain validity constraints
171171
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
176176
}
177177

178178
valid := true
179179
if !s.updates.periods.IsEmpty() {
180-
if isNotInFixedRootRange(s.updates.periods) {
180+
if isNotInFixedCommitteeRootRange(s.updates.periods) {
181181
log.Error("Start update is not in the fixed roots range")
182182
valid = false
183183
}
@@ -187,11 +187,11 @@ func (s *CommitteeChain) checkConstraints() bool {
187187
}
188188
}
189189
if !s.committees.periods.IsEmpty() {
190-
if isNotInFixedRootRange(s.committees.periods) {
190+
if isNotInFixedCommitteeRootRange(s.committees.periods) {
191191
log.Error("Start committee is not in the fixed roots range")
192192
valid = false
193193
}
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 {
195195
log.Error("Last committee is neither in the fixed roots range nor proven by updates")
196196
valid = false
197197
}
@@ -209,10 +209,10 @@ func (s *CommitteeChain) Reset() {
209209
}
210210
}
211211

212-
// AddFixedRoot sets a fixed committee root at the given period.
212+
// AddFixedCommitteeRoot sets a fixed committee root at the given period.
213213
// Note that the period where the first committee is added has to have a fixed
214214
// 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 {
216216
s.chainmu.Lock()
217217
defer s.chainmu.Unlock()
218218

@@ -222,7 +222,7 @@ func (s *CommitteeChain) AddFixedRoot(period uint64, root common.Hash) error {
222222

223223
batch := s.db.NewBatch()
224224
oldRoot := s.getCommitteeRoot(period)
225-
if !s.fixedRoots.periods.CanExpand(period) {
225+
if !s.fixedCommitteeRoots.periods.CanExpand(period) {
226226
// Note: the fixed committee root range should always be continuous and
227227
// therefore the expected syncing method is to forward sync and optionally
228228
// 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 {
238238
// if the old root exists and matches the new one then it is guaranteed
239239
// that the given period is after the existing fixed range and the roots
240240
// 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 {
243243
return err
244244
}
245245
}
@@ -250,7 +250,7 @@ func (s *CommitteeChain) AddFixedRoot(period uint64, root common.Hash) error {
250250
return err
251251
}
252252
}
253-
if err := s.fixedRoots.add(batch, period, root); err != nil {
253+
if err := s.fixedCommitteeRoots.add(batch, period, root); err != nil {
254254
return err
255255
}
256256
if err := batch.Write(); err != nil {
@@ -260,18 +260,18 @@ func (s *CommitteeChain) AddFixedRoot(period uint64, root common.Hash) error {
260260
return nil
261261
}
262262

263-
// DeleteFixedRootsFrom deletes fixed roots starting from the given period.
263+
// DeleteFixedCommitteeRootsFrom deletes fixed roots starting from the given period.
264264
// It also maintains chain consistency, meaning that it also deletes updates and
265265
// 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 {
267267
s.chainmu.Lock()
268268
defer s.chainmu.Unlock()
269269

270-
if period >= s.fixedRoots.periods.End {
270+
if period >= s.fixedCommitteeRoots.periods.End {
271271
return nil
272272
}
273273
batch := s.db.NewBatch()
274-
s.fixedRoots.deleteFrom(batch, period)
274+
s.fixedCommitteeRoots.deleteFrom(batch, period)
275275
if s.updates.periods.IsEmpty() || period <= s.updates.periods.Start {
276276
// Note: the first period of the update chain should always be fixed so if
277277
// 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
327327
if root != committee.Root() {
328328
return ErrWrongCommitteeRoot
329329
}
330-
if !s.committees.periods.Includes(period) {
330+
if !s.committees.periods.Contains(period) {
331331
if err := s.committees.add(s.db, period, committee); err != nil {
332332
return err
333333
}
@@ -349,7 +349,7 @@ func (s *CommitteeChain) InsertUpdate(update *types.LightClientUpdate, nextCommi
349349
defer s.chainmu.Unlock()
350350

351351
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) {
353353
return ErrInvalidPeriod
354354
}
355355
if s.minimumUpdateScore.BetterThan(update.Score()) {
@@ -364,15 +364,15 @@ func (s *CommitteeChain) InsertUpdate(update *types.LightClientUpdate, nextCommi
364364
}
365365
return nil
366366
}
367-
if s.fixedRoots.periods.Includes(period+1) && reorg {
367+
if s.fixedCommitteeRoots.periods.Contains(period+1) && reorg {
368368
return ErrCannotReorg
369369
}
370370
if ok, err := s.verifyUpdate(update); err != nil {
371371
return err
372372
} else if !ok {
373373
return ErrInvalidUpdate
374374
}
375-
addCommittee := !s.committees.periods.Includes(period+1) || reorg
375+
addCommittee := !s.committees.periods.Contains(period+1) || reorg
376376
if addCommittee {
377377
if nextCommittee == nil {
378378
return ErrNeedCommittee
@@ -426,14 +426,14 @@ func (s *CommitteeChain) rollback(period uint64) error {
426426
if s.committees.periods.End > max {
427427
max = s.committees.periods.End
428428
}
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
431431
}
432432
for max > period {
433433
max--
434434
batch := s.db.NewBatch()
435435
s.deleteCommitteesFrom(batch, max)
436-
s.fixedRoots.deleteFrom(batch, max)
436+
s.fixedCommitteeRoots.deleteFrom(batch, max)
437437
if max > 0 {
438438
s.updates.deleteFrom(batch, max-1)
439439
}
@@ -449,7 +449,7 @@ func (s *CommitteeChain) rollback(period uint64) error {
449449
// proven by a previous update or both. It returns an empty hash if the committee
450450
// root is unknown.
451451
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 {
453453
return root
454454
}
455455
if update, ok := s.updates.get(period - 1); ok {

beacon/light/committee_chain_test.go

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -53,16 +53,16 @@ var (
5353
tcAnotherGenesis = newTestCommitteeChain(nil, tfAnotherGenesis, true, 0, 10, 400, false)
5454
)
5555

56-
func TestCommitteeChainFixedRoots(t *testing.T) {
56+
func TestCommitteeChainFixedCommitteeRoots(t *testing.T) {
5757
for _, reload := range []bool{false, true} {
5858
c := newCommitteeChainTest(t, tfBase, 300, true)
5959
c.setClockPeriod(7)
60-
c.addFixedRoot(tcBase, 4, nil)
61-
c.addFixedRoot(tcBase, 5, nil)
62-
c.addFixedRoot(tcBase, 6, nil)
63-
c.addFixedRoot(tcBase, 8, ErrInvalidPeriod) // range has to be continuous
64-
c.addFixedRoot(tcBase, 3, nil)
65-
c.addFixedRoot(tcBase, 2, nil)
60+
c.addFixedCommitteeRoot(tcBase, 4, nil)
61+
c.addFixedCommitteeRoot(tcBase, 5, nil)
62+
c.addFixedCommitteeRoot(tcBase, 6, nil)
63+
c.addFixedCommitteeRoot(tcBase, 8, ErrInvalidPeriod) // range has to be continuous
64+
c.addFixedCommitteeRoot(tcBase, 3, nil)
65+
c.addFixedCommitteeRoot(tcBase, 2, nil)
6666
if reload {
6767
c.reloadChain()
6868
}
@@ -87,8 +87,8 @@ func TestCommitteeChainCheckpointSync(t *testing.T) {
8787
c.setClockPeriod(6)
8888
}
8989
c.insertUpdate(tcBase, 3, true, ErrInvalidPeriod)
90-
c.addFixedRoot(tcBase, 3, nil)
91-
c.addFixedRoot(tcBase, 4, nil)
90+
c.addFixedCommitteeRoot(tcBase, 3, nil)
91+
c.addFixedCommitteeRoot(tcBase, 4, nil)
9292
c.insertUpdate(tcBase, 4, true, ErrInvalidPeriod) // still no committee
9393
c.addCommittee(tcBase, 3, nil)
9494
c.addCommittee(tcBase, 4, nil)
@@ -120,7 +120,7 @@ func TestCommitteeChainCheckpointSync(t *testing.T) {
120120
c.verifyRange(tcBase, 3, 7) // now period 7 can also be verified
121121
// try reverse syncing an update
122122
c.insertUpdate(tcBase, 2, false, ErrInvalidPeriod) // fixed committee is needed first
123-
c.addFixedRoot(tcBase, 2, nil)
123+
c.addFixedCommitteeRoot(tcBase, 2, nil)
124124
c.addCommittee(tcBase, 2, nil)
125125
c.insertUpdate(tcBase, 2, false, nil)
126126
c.verifyRange(tcBase, 2, 7)
@@ -133,8 +133,8 @@ func TestCommitteeChainReorg(t *testing.T) {
133133
for _, addBetterUpdates := range []bool{false, true} {
134134
c := newCommitteeChainTest(t, tfBase, 300, true)
135135
c.setClockPeriod(11)
136-
c.addFixedRoot(tcBase, 3, nil)
137-
c.addFixedRoot(tcBase, 4, nil)
136+
c.addFixedCommitteeRoot(tcBase, 3, nil)
137+
c.addFixedCommitteeRoot(tcBase, 4, nil)
138138
c.addCommittee(tcBase, 3, nil)
139139
for period := uint64(3); period < 10; period++ {
140140
c.insertUpdate(tcBase, period, true, nil)
@@ -193,8 +193,8 @@ func TestCommitteeChainFork(t *testing.T) {
193193
c := newCommitteeChainTest(t, tfAlternative, 300, true)
194194
c.setClockPeriod(11)
195195
// trying to sync a chain on an alternative fork with the base chain data
196-
c.addFixedRoot(tcBase, 0, nil)
197-
c.addFixedRoot(tcBase, 1, nil)
196+
c.addFixedCommitteeRoot(tcBase, 0, nil)
197+
c.addFixedCommitteeRoot(tcBase, 1, nil)
198198
c.addCommittee(tcBase, 0, nil)
199199
// shared section should sync without errors
200200
for period := uint64(0); period < 7; period++ {
@@ -258,9 +258,9 @@ func (c *committeeChainTest) setClockPeriod(period float64) {
258258
c.clock.Run(wait)
259259
}
260260

261-
func (c *committeeChainTest) addFixedRoot(tc *testCommitteeChain, period uint64, expErr error) {
262-
if err := c.chain.AddFixedRoot(period, tc.periods[period].committee.Root()); err != expErr {
263-
c.t.Errorf("Incorrect error output from AddFixedRoot at period %d (expected %v, got %v)", period, expErr, err)
261+
func (c *committeeChainTest) addFixedCommitteeRoot(tc *testCommitteeChain, period uint64, expErr error) {
262+
if err := c.chain.AddFixedCommitteeRoot(period, tc.periods[period].committee.Root()); err != expErr {
263+
c.t.Errorf("Incorrect error output from AddFixedCommitteeRoot at period %d (expected %v, got %v)", period, expErr, err)
264264
}
265265
}
266266

beacon/light/range.go

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,8 @@ func (a Range) IsEmpty() bool {
2626
return a.End == a.Start
2727
}
2828

29-
// Includes returns true if the range includes the given period.
30-
func (a Range) Includes(period uint64) bool {
29+
// Contains returns true if the range includes the given period.
30+
func (a Range) Contains(period uint64) bool {
3131
return period >= a.Start && period < a.End
3232
}
3333

@@ -38,21 +38,17 @@ func (a Range) CanExpand(period uint64) bool {
3838
return a.IsEmpty() || (period+1 >= a.Start && period <= a.End)
3939
}
4040

41-
// Expand expands the range with the given period (assumes that CanExpand returned true).
41+
// Expand expands the range with the given period.
42+
// This method assumes that CanExpand returned true: otherwise this is a no-op.
4243
func (a *Range) Expand(period uint64) {
4344
if a.IsEmpty() {
4445
a.Start, a.End = period, period+1
4546
return
4647
}
47-
if a.Includes(period) {
48-
return
49-
}
5048
if a.Start == period+1 {
5149
a.Start--
52-
return
5350
}
5451
if a.End == period {
5552
a.End++
56-
return
5753
}
5854
}

core/rawdb/schema.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -132,9 +132,9 @@ var (
132132

133133
CliqueSnapshotPrefix = []byte("clique-")
134134

135-
BestUpdateKey = []byte("update-") // bigEndian64(syncPeriod) -> RLP(types.LightClientUpdate) (nextCommittee only referenced by root hash)
136-
FixedRootKey = []byte("fixedRoot-") // bigEndian64(syncPeriod) -> committee root hash
137-
SyncCommitteeKey = []byte("committee-") // bigEndian64(syncPeriod) -> serialized committee
135+
BestUpdateKey = []byte("update-") // bigEndian64(syncPeriod) -> RLP(types.LightClientUpdate) (nextCommittee only referenced by root hash)
136+
FixedCommitteeRootKey = []byte("fixedRoot-") // bigEndian64(syncPeriod) -> committee root hash
137+
SyncCommitteeKey = []byte("committee-") // bigEndian64(syncPeriod) -> serialized committee
138138

139139
preimageCounter = metrics.NewRegisteredCounter("db/preimage/total", nil)
140140
preimageHitCounter = metrics.NewRegisteredCounter("db/preimage/hits", nil)

0 commit comments

Comments
 (0)