From 7128f9e3cbd83021e7d15118c80c19012416e4c9 Mon Sep 17 00:00:00 2001 From: Zsolt Felfoldi Date: Fri, 8 Sep 2023 14:42:54 +0200 Subject: [PATCH] beacon/light: changed AfterLast to Next --- beacon/light/committee_chain.go | 52 ++++++++++++++-------------- beacon/light/committee_chain_test.go | 4 +-- beacon/light/range.go | 15 ++++---- 3 files changed, 35 insertions(+), 36 deletions(-) diff --git a/beacon/light/committee_chain.go b/beacon/light/committee_chain.go index 170fdc199f..990af208de 100644 --- a/beacon/light/committee_chain.go +++ b/beacon/light/committee_chain.go @@ -141,18 +141,18 @@ func NewCommitteeChain(db ethdb.KeyValueStore, config *types.ChainConfig, signer } // roll back invalid updates (might be necessary if forks have been changed since last time) for !s.updates.periods.IsEmpty() { - if update, ok := s.updates.get(s.updates.periods.AfterLast - 1); !ok || s.verifyUpdate(update) { + if update, ok := s.updates.get(s.updates.periods.Next - 1); !ok || s.verifyUpdate(update) { if update == nil { - log.Error("Sync committee update missing", "period", s.updates.periods.AfterLast-1) + log.Error("Sync committee update missing", "period", s.updates.periods.Next-1) } break } - if err := s.rollback(s.updates.periods.AfterLast); err != nil { + if err := s.rollback(s.updates.periods.Next); err != nil { log.Error("Error writing batch into chain database", "error", err) } } if !s.committees.periods.IsEmpty() { - log.Trace("Sync committee chain loaded", "first period", s.committees.periods.First, "last period", s.committees.periods.AfterLast-1) + log.Trace("Sync committee chain loaded", "first period", s.committees.periods.First, "last period", s.committees.periods.Next-1) } return s } @@ -162,22 +162,22 @@ func (s *CommitteeChain) checkConstraints() bool { valid := true if !s.updates.periods.IsEmpty() { if s.fixedRoots.periods.IsEmpty() || s.updates.periods.First < s.fixedRoots.periods.First || - s.updates.periods.First >= s.fixedRoots.periods.AfterLast { + s.updates.periods.First >= s.fixedRoots.periods.Next { log.Error("First update is not in the fixed roots range") valid = false } - if s.committees.periods.First > s.updates.periods.First || s.committees.periods.AfterLast <= s.updates.periods.AfterLast { + if s.committees.periods.First > s.updates.periods.First || s.committees.periods.Next <= s.updates.periods.Next { log.Error("Missing committees in update range") valid = false } } if !s.committees.periods.IsEmpty() { if s.fixedRoots.periods.IsEmpty() || s.committees.periods.First < s.fixedRoots.periods.First || - s.committees.periods.First >= s.fixedRoots.periods.AfterLast { + s.committees.periods.First >= s.fixedRoots.periods.Next { log.Error("First committee is not in the fixed roots range") valid = false } - if s.committees.periods.AfterLast > s.fixedRoots.periods.AfterLast && s.committees.periods.AfterLast > s.updates.periods.AfterLast+1 { + if s.committees.periods.Next > s.fixedRoots.periods.Next && s.committees.periods.Next > s.updates.periods.Next+1 { log.Error("Last committee is neither in the fixed roots range nor proven by updates") valid = false } @@ -220,7 +220,7 @@ func (s *CommitteeChain) AddFixedRoot(period uint64, root common.Hash) error { // if the old root exists and matches the new one then it is guaranteed // that the given period is after the existing fixed range and the roots // in between can also be fixed. - for p := s.fixedRoots.periods.AfterLast; p < period; p++ { + for p := s.fixedRoots.periods.Next; p < period; p++ { if err := s.fixedRoots.add(batch, p, s.getCommitteeRoot(p)); err != nil { return err } @@ -249,7 +249,7 @@ func (s *CommitteeChain) DeleteFixedRootsFrom(period uint64) error { s.chainmu.Lock() defer s.chainmu.Unlock() - if period >= s.fixedRoots.periods.AfterLast { + if period >= s.fixedRoots.periods.Next { return nil } batch := s.db.NewBatch() @@ -266,7 +266,7 @@ func (s *CommitteeChain) DeleteFixedRootsFrom(period uint64) error { // get unfixed but are still proven by the update chain. If there were // committees present after the range proven by updates, those should be // removed if the belonging fixed roots are also removed. - fromPeriod := s.updates.periods.AfterLast + 1 // not proven by updates + fromPeriod := s.updates.periods.Next + 1 // not proven by updates if period > fromPeriod { fromPeriod = period // also not justified by fixed roots } @@ -282,7 +282,7 @@ func (s *CommitteeChain) DeleteFixedRootsFrom(period uint64) error { // deleteCommitteesFrom deletes committees starting from the given period. func (s *CommitteeChain) deleteCommitteesFrom(batch ethdb.Batch, period uint64) { deleted := s.committees.deleteFrom(batch, period) - for period := deleted.First; period < deleted.AfterLast; period++ { + for period := deleted.First; period < deleted.Next; period++ { s.committeeCache.Remove(period) } } @@ -394,20 +394,20 @@ func (s *CommitteeChain) NextSyncPeriod() (uint64, bool) { return 0, false } if !s.updates.periods.IsEmpty() { - return s.updates.periods.AfterLast, true + return s.updates.periods.Next, true } - return s.committees.periods.AfterLast - 1, true + return s.committees.periods.Next - 1, true } // rollback removes all committees and fixed roots from the given period and updates // starting from the previous period. func (s *CommitteeChain) rollback(period uint64) error { - max := s.updates.periods.AfterLast + 1 - if s.committees.periods.AfterLast > max { - max = s.committees.periods.AfterLast + max := s.updates.periods.Next + 1 + if s.committees.periods.Next > max { + max = s.committees.periods.Next } - if s.fixedRoots.periods.AfterLast > max { - max = s.fixedRoots.periods.AfterLast + if s.fixedRoots.periods.Next > max { + max = s.fixedRoots.periods.Next } for max > period { max-- @@ -541,11 +541,11 @@ func newCanonicalStore[T any](db ethdb.KeyValueStore, keyPrefix []byte, period := binary.BigEndian.Uint64(iter.Key()[kl : kl+8]) if cs.periods.First == 0 { cs.periods.First = period - } else if cs.periods.AfterLast != period { + } else if cs.periods.Next != period { log.Warn("Gap in the canonical chain database") break // continuity guaranteed } - cs.periods.AfterLast = period + 1 + cs.periods.Next = period + 1 } iter.Release() return cs @@ -566,7 +566,7 @@ func (cs *canonicalStore[T]) databaseKey(period uint64) []byte { // continuous. Can be used either with a batch or database backend. func (cs *canonicalStore[T]) add(backend ethdb.KeyValueWriter, period uint64, value T) error { if !cs.periods.CanExpand(period) { - return fmt.Errorf("period expansion is not allowed, first: %d, next: %d, period: %d", cs.periods.First, cs.periods.AfterLast, period) + return fmt.Errorf("period expansion is not allowed, first: %d, next: %d, period: %d", cs.periods.First, cs.periods.Next, period) } enc, err := cs.encode(value) if err != nil { @@ -582,19 +582,19 @@ func (cs *canonicalStore[T]) add(backend ethdb.KeyValueWriter, period uint64, va // deleteFrom removes items starting from the given period. func (cs *canonicalStore[T]) deleteFrom(batch ethdb.Batch, fromPeriod uint64) (deleted Range) { - if fromPeriod >= cs.periods.AfterLast { + if fromPeriod >= cs.periods.Next { return } if fromPeriod < cs.periods.First { fromPeriod = cs.periods.First } - deleted = Range{First: fromPeriod, AfterLast: cs.periods.AfterLast} - for period := fromPeriod; period < cs.periods.AfterLast; period++ { + deleted = Range{First: fromPeriod, Next: cs.periods.Next} + for period := fromPeriod; period < cs.periods.Next; period++ { batch.Delete(cs.databaseKey(period)) cs.cache.Remove(period) } if fromPeriod > cs.periods.First { - cs.periods.AfterLast = fromPeriod + cs.periods.Next = fromPeriod } else { cs.periods = Range{} } diff --git a/beacon/light/committee_chain_test.go b/beacon/light/committee_chain_test.go index 2cd5886866..b41c30c030 100644 --- a/beacon/light/committee_chain_test.go +++ b/beacon/light/committee_chain_test.go @@ -60,14 +60,14 @@ func TestCommitteeChainFixedRoots(t *testing.T) { c.addFixedRoot(tcBase, 4, nil) c.addFixedRoot(tcBase, 5, nil) c.addFixedRoot(tcBase, 6, nil) - c.addFixedRoot(tcBase, 8, ErrInvalidPeriod) // range has to be continuoous + c.addFixedRoot(tcBase, 8, ErrInvalidPeriod) // range has to be continuous c.addFixedRoot(tcBase, 3, nil) c.addFixedRoot(tcBase, 2, nil) if reload { c.reloadChain() } c.addCommittee(tcBase, 4, nil) - c.addCommittee(tcBase, 6, ErrInvalidPeriod) // range has to be continuoous + c.addCommittee(tcBase, 6, ErrInvalidPeriod) // range has to be continuous c.addCommittee(tcBase, 5, nil) c.addCommittee(tcBase, 6, nil) c.addCommittee(tcAnotherGenesis, 3, ErrWrongCommitteeRoot) diff --git a/beacon/light/range.go b/beacon/light/range.go index 2d58a6a513..903d0bcfe6 100644 --- a/beacon/light/range.go +++ b/beacon/light/range.go @@ -18,31 +18,30 @@ package light // Range represents a (possibly zero-length) range of integers (sync periods). type Range struct { - First uint64 - AfterLast uint64 + First, Next uint64 } // IsEmpty returns true if the length of the range is zero. func (a Range) IsEmpty() bool { - return a.AfterLast == a.First + return a.Next == a.First } // Includes returns true if the range includes the given period. func (a Range) Includes(period uint64) bool { - return period >= a.First && period < a.AfterLast + return period >= a.First && period < a.Next } // CanExpand returns true if the range includes or can be expanded with the given // period (either the range is empty or the given period is inside, right before or // right after the range). func (a Range) CanExpand(period uint64) bool { - return a.IsEmpty() || (period+1 >= a.First && period <= a.AfterLast) + return a.IsEmpty() || (period+1 >= a.First && period <= a.Next) } // Expand expands the range with the given period (assumes that CanExpand returned true). func (a *Range) Expand(period uint64) { if a.IsEmpty() { - a.First, a.AfterLast = period, period+1 + a.First, a.Next = period, period+1 return } if a.Includes(period) { @@ -52,8 +51,8 @@ func (a *Range) Expand(period uint64) { a.First-- return } - if a.AfterLast == period { - a.AfterLast++ + if a.Next == period { + a.Next++ return } }