beacon/light: changed AfterLast to Next

This commit is contained in:
Zsolt Felfoldi 2023-09-08 14:42:54 +02:00 committed by zsfelfoldi
parent 2776aefab0
commit 7128f9e3cb
3 changed files with 35 additions and 36 deletions

View file

@ -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) // roll back invalid updates (might be necessary if forks have been changed since last time)
for !s.updates.periods.IsEmpty() { 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 { 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 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) log.Error("Error writing batch into chain database", "error", err)
} }
} }
if !s.committees.periods.IsEmpty() { 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 return s
} }
@ -162,22 +162,22 @@ func (s *CommitteeChain) checkConstraints() bool {
valid := true valid := true
if !s.updates.periods.IsEmpty() { if !s.updates.periods.IsEmpty() {
if s.fixedRoots.periods.IsEmpty() || s.updates.periods.First < s.fixedRoots.periods.First || 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") log.Error("First update is not in the fixed roots range")
valid = false 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") log.Error("Missing committees in update range")
valid = false valid = false
} }
} }
if !s.committees.periods.IsEmpty() { if !s.committees.periods.IsEmpty() {
if s.fixedRoots.periods.IsEmpty() || s.committees.periods.First < s.fixedRoots.periods.First || 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") log.Error("First committee is not in the fixed roots range")
valid = false 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") log.Error("Last committee is neither in the fixed roots range nor proven by updates")
valid = false 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 // 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 // that the given period is after the existing fixed range and the roots
// in between can also be fixed. // 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 { if err := s.fixedRoots.add(batch, p, s.getCommitteeRoot(p)); err != nil {
return err return err
} }
@ -249,7 +249,7 @@ func (s *CommitteeChain) DeleteFixedRootsFrom(period uint64) error {
s.chainmu.Lock() s.chainmu.Lock()
defer s.chainmu.Unlock() defer s.chainmu.Unlock()
if period >= s.fixedRoots.periods.AfterLast { if period >= s.fixedRoots.periods.Next {
return nil return nil
} }
batch := s.db.NewBatch() 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 // get unfixed but are still proven by the update chain. If there were
// committees present after the range proven by updates, those should be // committees present after the range proven by updates, those should be
// removed if the belonging fixed roots are also removed. // 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 { if period > fromPeriod {
fromPeriod = period // also not justified by fixed roots 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. // deleteCommitteesFrom deletes committees starting from the given period.
func (s *CommitteeChain) deleteCommitteesFrom(batch ethdb.Batch, period uint64) { func (s *CommitteeChain) deleteCommitteesFrom(batch ethdb.Batch, period uint64) {
deleted := s.committees.deleteFrom(batch, period) 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) s.committeeCache.Remove(period)
} }
} }
@ -394,20 +394,20 @@ func (s *CommitteeChain) NextSyncPeriod() (uint64, bool) {
return 0, false return 0, false
} }
if !s.updates.periods.IsEmpty() { 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 // rollback removes all committees and fixed roots from the given period and updates
// starting from the previous period. // starting from the previous period.
func (s *CommitteeChain) rollback(period uint64) error { func (s *CommitteeChain) rollback(period uint64) error {
max := s.updates.periods.AfterLast + 1 max := s.updates.periods.Next + 1
if s.committees.periods.AfterLast > max { if s.committees.periods.Next > max {
max = s.committees.periods.AfterLast max = s.committees.periods.Next
} }
if s.fixedRoots.periods.AfterLast > max { if s.fixedRoots.periods.Next > max {
max = s.fixedRoots.periods.AfterLast max = s.fixedRoots.periods.Next
} }
for max > period { for max > period {
max-- max--
@ -541,11 +541,11 @@ func newCanonicalStore[T any](db ethdb.KeyValueStore, keyPrefix []byte,
period := binary.BigEndian.Uint64(iter.Key()[kl : kl+8]) period := binary.BigEndian.Uint64(iter.Key()[kl : kl+8])
if cs.periods.First == 0 { if cs.periods.First == 0 {
cs.periods.First = period cs.periods.First = period
} else if cs.periods.AfterLast != period { } else if cs.periods.Next != period {
log.Warn("Gap in the canonical chain database") log.Warn("Gap in the canonical chain database")
break // continuity guaranteed break // continuity guaranteed
} }
cs.periods.AfterLast = period + 1 cs.periods.Next = period + 1
} }
iter.Release() iter.Release()
return cs 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. // continuous. Can be used either with a batch or database backend.
func (cs *canonicalStore[T]) add(backend ethdb.KeyValueWriter, period uint64, value T) error { func (cs *canonicalStore[T]) add(backend ethdb.KeyValueWriter, period uint64, value T) error {
if !cs.periods.CanExpand(period) { 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) enc, err := cs.encode(value)
if err != nil { 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. // deleteFrom removes items starting from the given period.
func (cs *canonicalStore[T]) deleteFrom(batch ethdb.Batch, fromPeriod uint64) (deleted Range) { func (cs *canonicalStore[T]) deleteFrom(batch ethdb.Batch, fromPeriod uint64) (deleted Range) {
if fromPeriod >= cs.periods.AfterLast { if fromPeriod >= cs.periods.Next {
return return
} }
if fromPeriod < cs.periods.First { if fromPeriod < cs.periods.First {
fromPeriod = cs.periods.First fromPeriod = cs.periods.First
} }
deleted = Range{First: fromPeriod, AfterLast: cs.periods.AfterLast} deleted = Range{First: fromPeriod, Next: cs.periods.Next}
for period := fromPeriod; period < cs.periods.AfterLast; period++ { for period := fromPeriod; period < cs.periods.Next; period++ {
batch.Delete(cs.databaseKey(period)) batch.Delete(cs.databaseKey(period))
cs.cache.Remove(period) cs.cache.Remove(period)
} }
if fromPeriod > cs.periods.First { if fromPeriod > cs.periods.First {
cs.periods.AfterLast = fromPeriod cs.periods.Next = fromPeriod
} else { } else {
cs.periods = Range{} cs.periods = Range{}
} }

View file

@ -60,14 +60,14 @@ func TestCommitteeChainFixedRoots(t *testing.T) {
c.addFixedRoot(tcBase, 4, nil) c.addFixedRoot(tcBase, 4, nil)
c.addFixedRoot(tcBase, 5, nil) c.addFixedRoot(tcBase, 5, nil)
c.addFixedRoot(tcBase, 6, 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, 3, nil)
c.addFixedRoot(tcBase, 2, nil) c.addFixedRoot(tcBase, 2, nil)
if reload { if reload {
c.reloadChain() c.reloadChain()
} }
c.addCommittee(tcBase, 4, nil) 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, 5, nil)
c.addCommittee(tcBase, 6, nil) c.addCommittee(tcBase, 6, nil)
c.addCommittee(tcAnotherGenesis, 3, ErrWrongCommitteeRoot) c.addCommittee(tcAnotherGenesis, 3, ErrWrongCommitteeRoot)

View file

@ -18,31 +18,30 @@ package light
// Range represents a (possibly zero-length) range of integers (sync periods). // Range represents a (possibly zero-length) range of integers (sync periods).
type Range struct { type Range struct {
First uint64 First, Next uint64
AfterLast uint64
} }
// IsEmpty returns true if the length of the range is zero. // IsEmpty returns true if the length of the range is zero.
func (a Range) IsEmpty() bool { 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. // Includes returns true if the range includes the given period.
func (a Range) Includes(period uint64) bool { 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 // 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 // period (either the range is empty or the given period is inside, right before or
// right after the range). // right after the range).
func (a Range) CanExpand(period uint64) bool { 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). // Expand expands the range with the given period (assumes that CanExpand returned true).
func (a *Range) Expand(period uint64) { func (a *Range) Expand(period uint64) {
if a.IsEmpty() { if a.IsEmpty() {
a.First, a.AfterLast = period, period+1 a.First, a.Next = period, period+1
return return
} }
if a.Includes(period) { if a.Includes(period) {
@ -52,8 +51,8 @@ func (a *Range) Expand(period uint64) {
a.First-- a.First--
return return
} }
if a.AfterLast == period { if a.Next == period {
a.AfterLast++ a.Next++
return return
} }
} }