beacon/light: fixed some bugs

This commit is contained in:
zsfelfoldi 2023-11-30 06:35:18 +01:00
parent 9fd49b01c9
commit 0ee5336c52
2 changed files with 11 additions and 14 deletions

View file

@ -52,6 +52,7 @@ func newCanonicalStore[T any](db ethdb.KeyValueStore, keyPrefix []byte,
var ( var (
iter = db.NewIterator(keyPrefix, nil) iter = db.NewIterator(keyPrefix, nil)
kl = len(keyPrefix) kl = len(keyPrefix)
first = true
) )
for iter.Next() { for iter.Next() {
if len(iter.Key()) != kl+8 { if len(iter.Key()) != kl+8 {
@ -59,12 +60,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.Start == 0 { if first {
cs.periods.Start = period cs.periods.Start = period
} else if cs.periods.End != 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
} }
first = false
cs.periods.End = period + 1 cs.periods.End = period + 1
} }
iter.Release() iter.Release()
@ -123,18 +125,22 @@ func (cs *canonicalStore[T]) deleteFrom(batch ethdb.Batch, fromPeriod uint64) (d
// get returns the item at the given period or the null value of the given type // get returns the item at the given period or the null value of the given type
// if no item is present. // if no item is present.
// Note: get is thread safe in itself and therefore can be called either with
// locked or unlocked chain mutex.
func (cs *canonicalStore[T]) get(period uint64) (value T, ok bool) { func (cs *canonicalStore[T]) get(period uint64) (value T, ok bool) {
if !cs.periods.Contains(period) {
return
}
if value, ok = cs.cache.Get(period); ok { if value, ok = cs.cache.Get(period); ok {
return return
} }
if enc, err := cs.db.Get(cs.databaseKey(period)); err == nil { if enc, err := cs.db.Get(cs.databaseKey(period)); err == nil {
if v, err := cs.decode(enc); err == nil { if v, err := cs.decode(enc); err == nil {
value, ok = v, true value, ok = v, true
cs.cache.Add(period, value)
} else { } else {
log.Error("Error decoding canonical store value", "error", err) log.Error("Error decoding canonical store value", "error", err)
} }
} else {
log.Error("Canonical store value not found", "period", period, "start", cs.periods.Start, "end", cs.periods.End)
} }
return return
} }

View file

@ -248,9 +248,6 @@ func (s *CommitteeChain) CheckpointInit(bootstrap *types.BootstrapData) error {
// Note that the period where the first committee is added has to have a fixed // Note that the period where the first committee is added has to have a fixed
// root which can either come from a BootstrapData or a trusted source. // root which can either come from a BootstrapData or a trusted source.
func (s *CommitteeChain) addFixedCommitteeRoot(period uint64, root common.Hash) error { func (s *CommitteeChain) addFixedCommitteeRoot(period uint64, root common.Hash) error {
s.chainmu.Lock()
defer s.chainmu.Unlock()
if root == (common.Hash{}) { if root == (common.Hash{}) {
return ErrWrongCommitteeRoot return ErrWrongCommitteeRoot
} }
@ -299,9 +296,6 @@ func (s *CommitteeChain) addFixedCommitteeRoot(period uint64, root common.Hash)
// It also maintains chain consistency, meaning that it also deletes updates and // It also maintains chain consistency, meaning that it also deletes updates and
// committees if they are no longer supported by a valid update chain. // committees if they are no longer supported by a valid update chain.
func (s *CommitteeChain) deleteFixedCommitteeRootsFrom(period uint64) error { func (s *CommitteeChain) deleteFixedCommitteeRootsFrom(period uint64) error {
s.chainmu.Lock()
defer s.chainmu.Unlock()
if period >= s.fixedCommitteeRoots.periods.End { if period >= s.fixedCommitteeRoots.periods.End {
return nil return nil
} }
@ -342,9 +336,6 @@ func (s *CommitteeChain) deleteCommitteesFrom(batch ethdb.Batch, period uint64)
// addCommittee adds a committee at the given period if possible. // addCommittee adds a committee at the given period if possible.
func (s *CommitteeChain) addCommittee(period uint64, committee *types.SerializedSyncCommittee) error { func (s *CommitteeChain) addCommittee(period uint64, committee *types.SerializedSyncCommittee) error {
s.chainmu.Lock()
defer s.chainmu.Unlock()
if !s.committees.periods.CanExpand(period) { if !s.committees.periods.CanExpand(period) {
return ErrInvalidPeriod return ErrInvalidPeriod
} }