initialize sig cache for verification.Initializer (#13295)

* initialize sig cache for verification.Initializer

* gaz

* lint

---------

Co-authored-by: Kasey Kirkham <kasey@users.noreply.github.com>
This commit is contained in:
kasey 2023-12-07 14:14:01 -06:00 committed by GitHub
parent 83af9a5694
commit 40a3ebab91
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 37 additions and 11 deletions

View File

@ -24,7 +24,6 @@ go_library(
"//config/fieldparams:go_default_library",
"//config/params:go_default_library",
"//consensus-types/blocks:go_default_library",
"//consensus-types/interfaces:go_default_library",
"//consensus-types/primitives:go_default_library",
"//crypto/bls:go_default_library",
"//encoding/bytesutil:go_default_library",
@ -43,6 +42,7 @@ go_test(
srcs = [
"blob_test.go",
"cache_test.go",
"initializer_test.go",
],
embed = [":go_default_library"],
deps = [
@ -56,6 +56,7 @@ go_test(
"//consensus-types/blocks:go_default_library",
"//consensus-types/primitives:go_default_library",
"//crypto/bls:go_default_library",
"//encoding/bytesutil:go_default_library",
"//proto/prysm/v1alpha1:go_default_library",
"//runtime/interop:go_default_library",
"//testing/require:go_default_library",

View File

@ -9,15 +9,9 @@ import (
"github.com/prysmaticlabs/prysm/v4/beacon-chain/startup"
"github.com/prysmaticlabs/prysm/v4/beacon-chain/state"
"github.com/prysmaticlabs/prysm/v4/consensus-types/blocks"
"github.com/prysmaticlabs/prysm/v4/consensus-types/interfaces"
"github.com/prysmaticlabs/prysm/v4/consensus-types/primitives"
)
// Database represents the db methods that the verifiers need.
type Database interface {
Block(ctx context.Context, blockRoot [32]byte) (interfaces.ReadOnlySignedBeaconBlock, error)
}
// Forkchoicer represents the forkchoice methods that the verifiers need.
// Note that forkchoice is used here in a lock-free fashion, assuming that a version of forkchoice
// is given that internally handles the details of locking the underlying store.
@ -40,7 +34,6 @@ type sharedResources struct {
fc Forkchoicer
sc SignatureCache
pc ProposerCache
db Database
sr StateByRooter
}
@ -71,12 +64,12 @@ type InitializerWaiter struct {
}
// NewInitializerWaiter creates an InitializerWaiter which can be used to obtain an Initializer once async dependencies are ready.
func NewInitializerWaiter(cw startup.ClockWaiter, fc Forkchoicer, sc SignatureCache, pc ProposerCache, db Database, sr StateByRooter) *InitializerWaiter {
func NewInitializerWaiter(cw startup.ClockWaiter, fc Forkchoicer, sr StateByRooter) *InitializerWaiter {
pc := newPropCache()
// signature cache is initialized in WaitForInitializer, since we need the genesis validators root, which can be obtained from startup.Clock.
shared := &sharedResources{
fc: fc,
sc: sc,
pc: pc,
db: db,
sr: sr,
}
return &InitializerWaiter{cw: cw, ini: &Initializer{shared: shared}}
@ -88,6 +81,10 @@ func (w *InitializerWaiter) WaitForInitializer(ctx context.Context) (*Initialize
if err := w.waitForReady(ctx); err != nil {
return nil, err
}
// We wait until this point to initialize the signature cache because here we have access to the genesis validator root.
vr := w.ini.shared.clock.GenesisValidatorsRoot()
sc := newSigCache(vr[:], DefaultSignatureCacheSize)
w.ini.shared.sc = sc
return w.ini, nil
}

View File

@ -0,0 +1,28 @@
package verification
import (
"bytes"
"context"
"testing"
"time"
"github.com/prysmaticlabs/prysm/v4/beacon-chain/startup"
"github.com/prysmaticlabs/prysm/v4/encoding/bytesutil"
"github.com/prysmaticlabs/prysm/v4/testing/require"
)
func TestInitializerWaiter(t *testing.T) {
ctx := context.Background()
vr := bytesutil.ToBytes32([]byte{0, 1, 1, 2, 3, 5})
gen := time.Now()
c := startup.NewClock(gen, vr)
cs := startup.NewClockSynchronizer()
require.NoError(t, cs.SetClock(c))
w := NewInitializerWaiter(cs, &mockForkchoicer{}, &mockStateByRooter{})
ini, err := w.WaitForInitializer(ctx)
require.NoError(t, err)
csc, ok := ini.shared.sc.(*sigCache)
require.Equal(t, true, ok)
require.Equal(t, true, bytes.Equal(vr[:], csc.valRoot))
}