beacon/light: changed First, Next to Start, End

This commit is contained in:
zsfelfoldi 2023-11-10 13:37:54 +01:00
parent 3d686626d7
commit 623d195b70
3 changed files with 44 additions and 44 deletions

View file

@ -59,13 +59,13 @@ func newCanonicalStore[T any](db ethdb.KeyValueStore, keyPrefix []byte,
continue continue
} }
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.Start == 0 {
cs.periods.First = period cs.periods.Start = period
} else if cs.periods.Next != period { } else if cs.periods.End != 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.Next = period + 1 cs.periods.End = period + 1
} }
iter.Release() iter.Release()
return cs return cs
@ -86,7 +86,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.Next, period) return fmt.Errorf("period expansion is not allowed, first: %d, next: %d, period: %d", cs.periods.Start, cs.periods.End, period)
} }
enc, err := cs.encode(value) enc, err := cs.encode(value)
if err != nil { if err != nil {
@ -102,19 +102,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.Next { if fromPeriod >= cs.periods.End {
return return
} }
if fromPeriod < cs.periods.First { if fromPeriod < cs.periods.Start {
fromPeriod = cs.periods.First fromPeriod = cs.periods.Start
} }
deleted = Range{First: fromPeriod, Next: cs.periods.Next} deleted = Range{Start: fromPeriod, End: cs.periods.End}
for period := fromPeriod; period < cs.periods.Next; period++ { for period := fromPeriod; period < cs.periods.End; 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.Start {
cs.periods.Next = fromPeriod cs.periods.End = fromPeriod
} else { } else {
cs.periods = Range{} cs.periods = Range{}
} }

View file

@ -146,23 +146,23 @@ 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() {
update, ok := s.updates.get(s.updates.periods.Next - 1) update, ok := s.updates.get(s.updates.periods.End - 1)
if !ok { if !ok {
log.Error("Sync committee update missing", "period", s.updates.periods.Next-1) log.Error("Sync committee update missing", "period", s.updates.periods.End-1)
s.Reset() s.Reset()
break break
} }
if valid, err := s.verifyUpdate(update); err != nil { if valid, err := s.verifyUpdate(update); err != nil {
log.Error("Error validating update", "period", s.updates.periods.Next-1, "error", err) log.Error("Error validating update", "period", s.updates.periods.End-1, "error", err)
} else if valid { } else if valid {
break break
} }
if err := s.rollback(s.updates.periods.Next); err != nil { if err := s.rollback(s.updates.periods.End); 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.Next-1) log.Trace("Sync committee chain loaded", "first period", s.committees.periods.Start, "last period", s.committees.periods.End-1)
} }
return s return s
} }
@ -171,27 +171,27 @@ func newCommitteeChain(db ethdb.KeyValueStore, config *types.ChainConfig, signer
func (s *CommitteeChain) checkConstraints() bool { func (s *CommitteeChain) checkConstraints() bool {
isNotInFixedRootRange := func(r Range) bool { isNotInFixedRootRange := func(r Range) bool {
return s.fixedRoots.periods.IsEmpty() || return s.fixedRoots.periods.IsEmpty() ||
r.First < s.fixedRoots.periods.First || r.Start < s.fixedRoots.periods.Start ||
r.First >= s.fixedRoots.periods.Next r.Start >= s.fixedRoots.periods.End
} }
valid := true valid := true
if !s.updates.periods.IsEmpty() { if !s.updates.periods.IsEmpty() {
if isNotInFixedRootRange(s.updates.periods) { if isNotInFixedRootRange(s.updates.periods) {
log.Error("First update is not in the fixed roots range") log.Error("Start update is not in the fixed roots range")
valid = false valid = false
} }
if s.committees.periods.First > s.updates.periods.First || s.committees.periods.Next <= s.updates.periods.Next { if s.committees.periods.Start > s.updates.periods.Start || s.committees.periods.End <= s.updates.periods.End {
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 isNotInFixedRootRange(s.committees.periods) { if isNotInFixedRootRange(s.committees.periods) {
log.Error("First committee is not in the fixed roots range") log.Error("Start committee is not in the fixed roots range")
valid = false valid = false
} }
if s.committees.periods.Next > s.fixedRoots.periods.Next && s.committees.periods.Next > s.updates.periods.Next+1 { if s.committees.periods.End > s.fixedRoots.periods.End && s.committees.periods.End > s.updates.periods.End+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
} }
@ -238,7 +238,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.Next; p < period; p++ { for p := s.fixedRoots.periods.End; 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
} }
@ -267,12 +267,12 @@ 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.Next { if period >= s.fixedRoots.periods.End {
return nil return nil
} }
batch := s.db.NewBatch() batch := s.db.NewBatch()
s.fixedRoots.deleteFrom(batch, period) s.fixedRoots.deleteFrom(batch, period)
if s.updates.periods.IsEmpty() || period <= s.updates.periods.First { if s.updates.periods.IsEmpty() || period <= s.updates.periods.Start {
// Note: the first period of the update chain should always be fixed so if // Note: the first period of the update chain should always be fixed so if
// the fixed root at the first update is removed then the entire update chain // the fixed root at the first update is removed then the entire update chain
// and the proven committees have to be removed. Earlier committees in the // and the proven committees have to be removed. Earlier committees in the
@ -284,7 +284,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.Next + 1 // not proven by updates fromPeriod := s.updates.periods.End + 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
} }
@ -300,7 +300,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.Next; period++ { for period := deleted.Start; period < deleted.End; period++ {
s.committeeCache.Remove(period) s.committeeCache.Remove(period)
} }
} }
@ -414,20 +414,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.Next, true return s.updates.periods.End, true
} }
return s.committees.periods.Next - 1, true return s.committees.periods.End - 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.Next + 1 max := s.updates.periods.End + 1
if s.committees.periods.Next > max { if s.committees.periods.End > max {
max = s.committees.periods.Next max = s.committees.periods.End
} }
if s.fixedRoots.periods.Next > max { if s.fixedRoots.periods.End > max {
max = s.fixedRoots.periods.Next max = s.fixedRoots.periods.End
} }
for max > period { for max > period {
max-- max--

View file

@ -18,41 +18,41 @@ 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, Next uint64 Start, End 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.Next == a.First return a.End == a.Start
} }
// 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.Next return period >= a.Start && period < a.End
} }
// 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.Next) return a.IsEmpty() || (period+1 >= a.Start && period <= a.End)
} }
// 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.Next = period, period+1 a.Start, a.End = period, period+1
return return
} }
if a.Includes(period) { if a.Includes(period) {
return return
} }
if a.First == period+1 { if a.Start == period+1 {
a.First-- a.Start--
return return
} }
if a.Next == period { if a.End == period {
a.Next++ a.End++
return return
} }
} }