beacon/light: multiple small changes

This commit is contained in:
zsfelfoldi 2023-09-13 14:22:21 +02:00
parent 2baa1bf1d7
commit 3d686626d7
5 changed files with 36 additions and 21 deletions

View file

@ -38,7 +38,8 @@ type canonicalStore[T any] struct {
decode func([]byte) (T, error) decode func([]byte) (T, error)
} }
// newCanonicalStore creates a new canonicalStore. // 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.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]{

View file

@ -79,11 +79,17 @@ type CommitteeChain struct {
config *types.ChainConfig config *types.ChainConfig
signerThreshold int signerThreshold int
minimumUpdateScore types.UpdateScore minimumUpdateScore types.UpdateScore
enforceTime bool enforceTime bool // enforceTime specifies whether the age of a signed header should be checked
} }
// NewCommitteeChain creates a new CommitteeChain. // NewCommitteeChain creates a new CommitteeChain.
func NewCommitteeChain(db ethdb.KeyValueStore, config *types.ChainConfig, signerThreshold int, enforceTime bool, sigVerifier committeeSigVerifier, clock mclock.Clock, unixNano func() int64) *CommitteeChain { func NewCommitteeChain(db ethdb.KeyValueStore, config *types.ChainConfig, signerThreshold int, enforceTime bool) *CommitteeChain {
return newCommitteeChain(db, config, signerThreshold, enforceTime, blsVerifier{}, &mclock.System{}, func() int64 { return time.Now().UnixNano() })
}
// newCommitteeChain creates a new CommitteeChain with the option of replacing the
// clock source and signature verification for testing purposes.
func newCommitteeChain(db ethdb.KeyValueStore, config *types.ChainConfig, signerThreshold int, enforceTime bool, sigVerifier committeeSigVerifier, clock mclock.Clock, unixNano func() int64) *CommitteeChain {
var ( var (
fixedRootEncoder = func(root common.Hash) ([]byte, error) { fixedRootEncoder = func(root common.Hash) ([]byte, error) {
return root[:], nil return root[:], nil
@ -163,10 +169,15 @@ func NewCommitteeChain(db ethdb.KeyValueStore, config *types.ChainConfig, signer
// checkConstraints checks committee chain validity constraints // checkConstraints checks committee chain validity constraints
func (s *CommitteeChain) checkConstraints() bool { func (s *CommitteeChain) checkConstraints() bool {
isNotInFixedRootRange := func(r Range) bool {
return s.fixedRoots.periods.IsEmpty() ||
r.First < s.fixedRoots.periods.First ||
r.First >= s.fixedRoots.periods.Next
}
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 isNotInFixedRootRange(s.updates.periods) {
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
} }
@ -176,8 +187,7 @@ func (s *CommitteeChain) checkConstraints() bool {
} }
} }
if !s.committees.periods.IsEmpty() { if !s.committees.periods.IsEmpty() {
if s.fixedRoots.periods.IsEmpty() || s.committees.periods.First < s.fixedRoots.periods.First || if isNotInFixedRootRange(s.committees.periods) {
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
} }
@ -206,6 +216,10 @@ func (s *CommitteeChain) AddFixedRoot(period uint64, root common.Hash) error {
s.chainmu.Lock() s.chainmu.Lock()
defer s.chainmu.Unlock() defer s.chainmu.Unlock()
if root == (common.Hash{}) {
return ErrWrongCommitteeRoot
}
batch := s.db.NewBatch() batch := s.db.NewBatch()
oldRoot := s.getCommitteeRoot(period) oldRoot := s.getCommitteeRoot(period)
if !s.fixedRoots.periods.CanExpand(period) { if !s.fixedRoots.periods.CanExpand(period) {

View file

@ -241,12 +241,12 @@ func newCommitteeChainTest(t *testing.T, config types.ChainConfig, signerThresho
signerThreshold: signerThreshold, signerThreshold: signerThreshold,
enforceTime: enforceTime, enforceTime: enforceTime,
} }
c.chain = NewCommitteeChain(c.db, &config, signerThreshold, enforceTime, DummyVerifier{}, c.clock, func() int64 { return int64(c.clock.Now()) }) c.chain = newCommitteeChain(c.db, &config, signerThreshold, enforceTime, dummyVerifier{}, c.clock, func() int64 { return int64(c.clock.Now()) })
return c return c
} }
func (c *committeeChainTest) reloadChain() { func (c *committeeChainTest) reloadChain() {
c.chain = NewCommitteeChain(c.db, &c.config, c.signerThreshold, c.enforceTime, DummyVerifier{}, c.clock, func() int64 { return int64(c.clock.Now()) }) c.chain = newCommitteeChain(c.db, &c.config, c.signerThreshold, c.enforceTime, dummyVerifier{}, c.clock, func() int64 { return int64(c.clock.Now()) })
} }
func (c *committeeChainTest) setClockPeriod(period float64) { func (c *committeeChainTest) setClockPeriod(period float64) {

View file

@ -51,7 +51,7 @@ func GenerateTestUpdate(config *types.ChainConfig, period uint64, committee, nex
func GenerateTestSignedHeader(header types.Header, config *types.ChainConfig, committee *types.SerializedSyncCommittee, signatureSlot uint64, signerCount int) types.SignedHeader { func GenerateTestSignedHeader(header types.Header, config *types.ChainConfig, committee *types.SerializedSyncCommittee, signatureSlot uint64, signerCount int) types.SignedHeader {
bitmask := makeBitmask(signerCount) bitmask := makeBitmask(signerCount)
signingRoot, _ := config.Forks.SigningRoot(header) signingRoot, _ := config.Forks.SigningRoot(header)
c, _ := DummyVerifier{}.deserializeSyncCommittee(committee) c, _ := dummyVerifier{}.deserializeSyncCommittee(committee)
return types.SignedHeader{ return types.SignedHeader{
Header: header, Header: header,
Signature: types.SyncAggregate{ Signature: types.SyncAggregate{
@ -113,33 +113,33 @@ type committeeSigVerifier interface {
verifySignature(committee syncCommittee, signedRoot common.Hash, aggregate *types.SyncAggregate) bool verifySignature(committee syncCommittee, signedRoot common.Hash, aggregate *types.SyncAggregate) bool
} }
// BLSVerifier implements committeeSigVerifier // blsVerifier implements committeeSigVerifier
type BLSVerifier struct{} type blsVerifier struct{}
// deserializeSyncCommittee implements committeeSigVerifier // deserializeSyncCommittee implements committeeSigVerifier
func (BLSVerifier) deserializeSyncCommittee(s *types.SerializedSyncCommittee) (syncCommittee, error) { func (blsVerifier) deserializeSyncCommittee(s *types.SerializedSyncCommittee) (syncCommittee, error) {
return s.Deserialize() return s.Deserialize()
} }
// verifySignature implements committeeSigVerifier // verifySignature implements committeeSigVerifier
func (BLSVerifier) verifySignature(committee syncCommittee, signingRoot common.Hash, aggregate *types.SyncAggregate) bool { func (blsVerifier) verifySignature(committee syncCommittee, signingRoot common.Hash, aggregate *types.SyncAggregate) bool {
return committee.(*types.SyncCommittee).VerifySignature(signingRoot, aggregate) return committee.(*types.SyncCommittee).VerifySignature(signingRoot, aggregate)
} }
type dummySyncCommittee [32]byte type dummySyncCommittee [32]byte
// DummyVerifier implements committeeSigVerifier // dummyVerifier implements committeeSigVerifier
type DummyVerifier struct{} type dummyVerifier struct{}
// deserializeSyncCommittee implements committeeSigVerifier // deserializeSyncCommittee implements committeeSigVerifier
func (DummyVerifier) deserializeSyncCommittee(s *types.SerializedSyncCommittee) (syncCommittee, error) { func (dummyVerifier) deserializeSyncCommittee(s *types.SerializedSyncCommittee) (syncCommittee, error) {
var sc dummySyncCommittee var sc dummySyncCommittee
copy(sc[:], s[:32]) copy(sc[:], s[:32])
return sc, nil return sc, nil
} }
// verifySignature implements committeeSigVerifier // verifySignature implements committeeSigVerifier
func (DummyVerifier) verifySignature(committee syncCommittee, signingRoot common.Hash, aggregate *types.SyncAggregate) bool { func (dummyVerifier) verifySignature(committee syncCommittee, signingRoot common.Hash, aggregate *types.SyncAggregate) bool {
return aggregate.Signature == makeDummySignature(committee.(dummySyncCommittee), signingRoot, aggregate.Signers) return aggregate.Signature == makeDummySignature(committee.(dummySyncCommittee), signingRoot, aggregate.Signers)
} }

View file

@ -132,12 +132,12 @@ var (
CliqueSnapshotPrefix = []byte("clique-") CliqueSnapshotPrefix = []byte("clique-")
preimageCounter = metrics.NewRegisteredCounter("db/preimage/total", nil)
preimageHitCounter = metrics.NewRegisteredCounter("db/preimage/hits", nil)
BestUpdateKey = []byte("update-") // bigEndian64(syncPeriod) -> RLP(types.LightClientUpdate) (nextCommittee only referenced by root hash) BestUpdateKey = []byte("update-") // bigEndian64(syncPeriod) -> RLP(types.LightClientUpdate) (nextCommittee only referenced by root hash)
FixedRootKey = []byte("fixedRoot-") // bigEndian64(syncPeriod) -> committee root hash FixedRootKey = []byte("fixedRoot-") // bigEndian64(syncPeriod) -> committee root hash
SyncCommitteeKey = []byte("committee-") // bigEndian64(syncPeriod) -> serialized committee SyncCommitteeKey = []byte("committee-") // bigEndian64(syncPeriod) -> serialized committee
preimageCounter = metrics.NewRegisteredCounter("db/preimage/total", nil)
preimageHitCounter = metrics.NewRegisteredCounter("db/preimage/hits", nil)
) )
// LegacyTxLookupEntry is the legacy TxLookupEntry definition with some unnecessary // LegacyTxLookupEntry is the legacy TxLookupEntry definition with some unnecessary