-
Notifications
You must be signed in to change notification settings - Fork 20.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
beacon/light: add CommitteeChain #27766
Merged
Merged
Changes from all commits
Commits
Show all changes
19 commits
Select commit
Hold shift + click to select a range
574f085
beacon/light: add CommitteeChain
zsfelfoldi 8ff3896
beacon/light: added comments and removed unused code
zsfelfoldi 6eb5767
beacon/light: comments and various minor changes
zsfelfoldi 46bdc03
beacon/light: more issues fixed
zsfelfoldi b039c2c
beacon/light: reset invalid chain
zsfelfoldi 2776aef
beacon/light: rollback slot-by-slot instead of in a single batch
zsfelfoldi 7128f9e
beacon/light: changed AfterLast to Next
zsfelfoldi 3a46f01
beacon/light: bubble up getSyncCommittee errors
zsfelfoldi 2baa1bf
beacon/light: moved canonicalStore to separate file
zsfelfoldi 3d68662
beacon/light: multiple small changes
zsfelfoldi 623d195
beacon/light: changed First, Next to Start, End
zsfelfoldi 4d71502
beacon/light: more renames and cleanups
zsfelfoldi 9fd49b0
beacon/light: unexported fixed roots, moved checkpoint init into Comm…
zsfelfoldi 0ee5336
beacon/light: fixed some bugs
zsfelfoldi 94421cf
beacon/light: always use external db parameter in canonicalStore
zsfelfoldi 8b1390e
beacon/light: new Range methods, simplified deleteFrom
zsfelfoldi 0bf9f8e
beacon/light: addressed review comments
zsfelfoldi 3b38b3b
beacon/light: always use rlp encoding in canonicalStore
zsfelfoldi 751dc7a
beacon/light: fix potential database iterator leaking
rjl493456442 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,125 @@ | ||
// Copyright 2023 The go-ethereum Authors | ||
// This file is part of the go-ethereum library. | ||
// | ||
// The go-ethereum library is free software: you can redistribute it and/or modify | ||
// it under the terms of the GNU Lesser General Public License as published by | ||
// the Free Software Foundation, either version 3 of the License, or | ||
// (at your option) any later version. | ||
// | ||
// The go-ethereum library is distributed in the hope that it will be useful, | ||
// but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
// GNU Lesser General Public License for more details. | ||
// | ||
// You should have received a copy of the GNU Lesser General Public License | ||
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>. | ||
|
||
package light | ||
|
||
import ( | ||
"encoding/binary" | ||
"fmt" | ||
|
||
"github.com/ethereum/go-ethereum/common/lru" | ||
"github.com/ethereum/go-ethereum/ethdb" | ||
"github.com/ethereum/go-ethereum/log" | ||
"github.com/ethereum/go-ethereum/rlp" | ||
) | ||
|
||
// canonicalStore stores instances of the given type in a database and caches | ||
// them in memory, associated with a continuous range of period numbers. | ||
// Note: canonicalStore is not thread safe and it is the caller's responsibility | ||
// to avoid concurrent access. | ||
type canonicalStore[T any] struct { | ||
keyPrefix []byte | ||
periods periodRange | ||
cache *lru.Cache[uint64, T] | ||
} | ||
|
||
// newCanonicalStore creates a new canonicalStore and loads all keys associated | ||
// with the keyPrefix in order to determine the ranges available in the database. | ||
func newCanonicalStore[T any](db ethdb.Iteratee, keyPrefix []byte) (*canonicalStore[T], error) { | ||
cs := &canonicalStore[T]{ | ||
keyPrefix: keyPrefix, | ||
cache: lru.NewCache[uint64, T](100), | ||
} | ||
var ( | ||
iter = db.NewIterator(keyPrefix, nil) | ||
kl = len(keyPrefix) | ||
first = true | ||
) | ||
defer iter.Release() | ||
|
||
for iter.Next() { | ||
if len(iter.Key()) != kl+8 { | ||
log.Warn("Invalid key length in the canonical chain database", "key", fmt.Sprintf("%#x", iter.Key())) | ||
continue | ||
} | ||
period := binary.BigEndian.Uint64(iter.Key()[kl : kl+8]) | ||
if first { | ||
cs.periods.Start = period | ||
} else if cs.periods.End != period { | ||
return nil, fmt.Errorf("gap in the canonical chain database between periods %d and %d", cs.periods.End, period-1) | ||
} | ||
first = false | ||
cs.periods.End = period + 1 | ||
} | ||
return cs, nil | ||
} | ||
|
||
// databaseKey returns the database key belonging to the given period. | ||
func (cs *canonicalStore[T]) databaseKey(period uint64) []byte { | ||
return binary.BigEndian.AppendUint64(append([]byte{}, cs.keyPrefix...), period) | ||
} | ||
|
||
// add adds the given item to the database. It also ensures that the range remains | ||
// 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.Start, cs.periods.End, period) | ||
} | ||
enc, err := rlp.EncodeToBytes(value) | ||
if err != nil { | ||
return err | ||
} | ||
if err := backend.Put(cs.databaseKey(period), enc); err != nil { | ||
return err | ||
} | ||
cs.cache.Add(period, value) | ||
cs.periods.expand(period) | ||
return nil | ||
} | ||
|
||
// deleteFrom removes items starting from the given period. | ||
func (cs *canonicalStore[T]) deleteFrom(db ethdb.KeyValueWriter, fromPeriod uint64) (deleted periodRange) { | ||
keepRange, deleteRange := cs.periods.split(fromPeriod) | ||
deleteRange.each(func(period uint64) { | ||
db.Delete(cs.databaseKey(period)) | ||
cs.cache.Remove(period) | ||
}) | ||
cs.periods = keepRange | ||
return deleteRange | ||
} | ||
|
||
// get returns the item at the given period or the null value of the given type | ||
// if no item is present. | ||
func (cs *canonicalStore[T]) get(backend ethdb.KeyValueReader, period uint64) (T, bool) { | ||
var null, value T | ||
if !cs.periods.contains(period) { | ||
return null, false | ||
} | ||
if value, ok := cs.cache.Get(period); ok { | ||
return value, true | ||
} | ||
enc, err := backend.Get(cs.databaseKey(period)) | ||
if err != nil { | ||
log.Error("Canonical store value not found", "period", period, "start", cs.periods.Start, "end", cs.periods.End) | ||
return null, false | ||
} | ||
if err := rlp.DecodeBytes(enc, &value); err != nil { | ||
log.Error("Error decoding canonical store value", "error", err) | ||
return null, false | ||
} | ||
cs.cache.Add(period, value) | ||
return value, true | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't know if this should live in the
light
package. Maybe would be better ininternal
? Also is there an equivalent structure for storing regular chaindata? I think it is good to avoid creating new single-use generic data structures when possible.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sure, we can think about it as general purpose but on the other hand I see no other use case for it outside package
light
right now. I'll leave it as it is for now, according to the 2:1 vote results :)