beacon/light: always use external db parameter in canonicalStore

This commit is contained in:
zsfelfoldi 2023-11-30 06:46:05 +01:00
parent 0ee5336c52
commit 94421cfb43
2 changed files with 7 additions and 9 deletions

View file

@ -30,7 +30,6 @@ import (
// Note: canonicalStore is not thread safe and it is the caller's responsibility // Note: canonicalStore is not thread safe and it is the caller's responsibility
// to avoid concurrent access. // to avoid concurrent access.
type canonicalStore[T any] struct { type canonicalStore[T any] struct {
db ethdb.KeyValueStore
keyPrefix []byte keyPrefix []byte
periods Range periods Range
cache *lru.Cache[uint64, T] cache *lru.Cache[uint64, T]
@ -43,7 +42,6 @@ type canonicalStore[T any] struct {
func newCanonicalStore[T any](db ethdb.KeyValueStore, keyPrefix []byte, func newCanonicalStore[T any](db ethdb.KeyValueStore, keyPrefix []byte,
encode func(T) ([]byte, error), decode func([]byte) (T, error)) *canonicalStore[T] { encode func(T) ([]byte, error), decode func([]byte) (T, error)) *canonicalStore[T] {
cs := &canonicalStore[T]{ cs := &canonicalStore[T]{
db: db,
keyPrefix: keyPrefix, keyPrefix: keyPrefix,
encode: encode, encode: encode,
decode: decode, decode: decode,
@ -125,14 +123,14 @@ 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.
func (cs *canonicalStore[T]) get(period uint64) (value T, ok bool) { func (cs *canonicalStore[T]) get(backend ethdb.KeyValueReader, period uint64) (value T, ok bool) {
if !cs.periods.Contains(period) { if !cs.periods.Contains(period) {
return 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 := backend.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) cs.cache.Add(period, value)

View file

@ -146,7 +146,7 @@ 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.End - 1) update, ok := s.updates.get(s.db, s.updates.periods.End-1)
if !ok { if !ok {
log.Error("Sync committee update missing", "period", s.updates.periods.End-1) log.Error("Sync committee update missing", "period", s.updates.periods.End-1)
s.Reset() s.Reset()
@ -369,7 +369,7 @@ func (s *CommitteeChain) InsertUpdate(update *types.LightClientUpdate, nextCommi
} }
oldRoot := s.getCommitteeRoot(period + 1) oldRoot := s.getCommitteeRoot(period + 1)
reorg := oldRoot != (common.Hash{}) && oldRoot != update.NextSyncCommitteeRoot reorg := oldRoot != (common.Hash{}) && oldRoot != update.NextSyncCommitteeRoot
if oldUpdate, ok := s.updates.get(period); ok && !update.Score().BetterThan(oldUpdate.Score()) { if oldUpdate, ok := s.updates.get(s.db, period); ok && !update.Score().BetterThan(oldUpdate.Score()) {
// a better or equal update already exists; no changes, only fail if new one tried to reorg // a better or equal update already exists; no changes, only fail if new one tried to reorg
if reorg { if reorg {
return ErrCannotReorg return ErrCannotReorg
@ -461,10 +461,10 @@ func (s *CommitteeChain) rollback(period uint64) error {
// proven by a previous update or both. It returns an empty hash if the committee // proven by a previous update or both. It returns an empty hash if the committee
// root is unknown. // root is unknown.
func (s *CommitteeChain) getCommitteeRoot(period uint64) common.Hash { func (s *CommitteeChain) getCommitteeRoot(period uint64) common.Hash {
if root, ok := s.fixedCommitteeRoots.get(period); ok || period == 0 { if root, ok := s.fixedCommitteeRoots.get(s.db, period); ok || period == 0 {
return root return root
} }
if update, ok := s.updates.get(period - 1); ok { if update, ok := s.updates.get(s.db, period-1); ok {
return update.NextSyncCommitteeRoot return update.NextSyncCommitteeRoot
} }
return common.Hash{} return common.Hash{}
@ -475,7 +475,7 @@ func (s *CommitteeChain) getSyncCommittee(period uint64) (syncCommittee, error)
if c, ok := s.committeeCache.Get(period); ok { if c, ok := s.committeeCache.Get(period); ok {
return c, nil return c, nil
} }
if sc, ok := s.committees.get(period); ok { if sc, ok := s.committees.get(s.db, period); ok {
c, err := s.sigVerifier.deserializeSyncCommittee(sc) c, err := s.sigVerifier.deserializeSyncCommittee(sc)
if err != nil { if err != nil {
return nil, fmt.Errorf("Sync committee #%d deserialization error: %v", period, err) return nil, fmt.Errorf("Sync committee #%d deserialization error: %v", period, err)