Skip to content

Commit

Permalink
Merge branch 'develop' of github.com:prysmaticlabs/prysm into capella
Browse files Browse the repository at this point in the history
  • Loading branch information
terencechain committed Dec 2, 2022
2 parents 78fb685 + f7cecf9 commit 57bdb90
Show file tree
Hide file tree
Showing 10 changed files with 283 additions and 14 deletions.
6 changes: 5 additions & 1 deletion beacon-chain/blockchain/log.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,8 +129,12 @@ func logPayload(block interfaces.BeaconBlock) error {
return errors.Wrap(err, "could not get withdrawals")
}
fields["withdrawals"] = len(withdrawals)
changes, err := block.Body().BLSToExecutionChanges()
if err != nil {
return errors.Wrap(err, "could not get BLSToExecutionChanges")
}
fields["blsToExecutionChanges"] = len(changes)
}

log.WithFields(fields).Debug("Synced new payload")
return nil
}
7 changes: 2 additions & 5 deletions beacon-chain/blockchain/process_block.go
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ func (s *Service) onBlock(ctx context.Context, signed interfaces.SignedBeaconBlo
if err := s.handleBlockAttestations(ctx, signed.Block(), postState); err != nil {
return errors.Wrap(err, "could not handle block's attestations")
}
if err := s.handleBlockBLSToExecChanges(ctx, signed.Block()); err != nil {
if err := s.handleBlockBLSToExecChanges(signed.Block()); err != nil {
return errors.Wrap(err, "could not handle block's BLSToExecutionChanges")
}

Expand Down Expand Up @@ -559,7 +559,7 @@ func (s *Service) handleBlockAttestations(ctx context.Context, blk interfaces.Be
return nil
}

func (s *Service) handleBlockBLSToExecChanges(ctx context.Context, blk interfaces.BeaconBlock) error {
func (s *Service) handleBlockBLSToExecChanges(blk interfaces.BeaconBlock) error {
if blk.Version() < version.Capella {
return nil
}
Expand All @@ -568,9 +568,6 @@ func (s *Service) handleBlockBLSToExecChanges(ctx context.Context, blk interface
return errors.Wrap(err, "could not get BLSToExecutionChanges")
}
for _, change := range changes {
if ctx.Err() != nil {
return ctx.Err()
}
if err := s.cfg.BLSToExecPool.MarkIncluded(change); err != nil {
return errors.Wrap(err, "could not mark BLSToExecutionChange as included")
}
Expand Down
60 changes: 60 additions & 0 deletions beacon-chain/blockchain/process_block_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import (
doublylinkedtree "github.com/prysmaticlabs/prysm/v3/beacon-chain/forkchoice/doubly-linked-tree"
forkchoicetypes "github.com/prysmaticlabs/prysm/v3/beacon-chain/forkchoice/types"
"github.com/prysmaticlabs/prysm/v3/beacon-chain/operations/attestations"
"github.com/prysmaticlabs/prysm/v3/beacon-chain/operations/blstoexec"
"github.com/prysmaticlabs/prysm/v3/beacon-chain/state"
"github.com/prysmaticlabs/prysm/v3/beacon-chain/state/stategen"
fieldparams "github.com/prysmaticlabs/prysm/v3/config/fieldparams"
Expand Down Expand Up @@ -2304,6 +2305,65 @@ func TestFillMissingBlockPayloadId_DiffSlotExitEarly(t *testing.T) {
require.NoError(t, service.fillMissingBlockPayloadId(ctx, time.Unix(int64(params.BeaconConfig().SecondsPerSlot/2), 0)))
}

func TestHandleBBlockBLSToExecutionChanges(t *testing.T) {
ctx := context.Background()
beaconDB := testDB.SetupDB(t)
fc := doublylinkedtree.New()
pool := blstoexec.NewPool()
opts := []Option{
WithDatabase(beaconDB),
WithStateGen(stategen.New(beaconDB, fc)),
WithForkChoiceStore(fc),
WithStateNotifier(&mock.MockStateNotifier{}),
WithBLSToExecPool(pool),
}
service, err := NewService(ctx, opts...)
require.NoError(t, err)

t.Run("pre Capella block", func(t *testing.T) {
body := &ethpb.BeaconBlockBodyBellatrix{}
pbb := &ethpb.BeaconBlockBellatrix{
Body: body,
}
blk, err := consensusblocks.NewBeaconBlock(pbb)
require.NoError(t, err)
require.NoError(t, service.handleBlockBLSToExecChanges(blk))
})

t.Run("Post Capella no changes", func(t *testing.T) {
body := &ethpb.BeaconBlockBodyCapella{}
pbb := &ethpb.BeaconBlockCapella{
Body: body,
}
blk, err := consensusblocks.NewBeaconBlock(pbb)
require.NoError(t, err)
require.NoError(t, service.handleBlockBLSToExecChanges(blk))
})

t.Run("Post Capella some changes", func(t *testing.T) {
idx := types.ValidatorIndex(123)
change := &ethpb.BLSToExecutionChange{
ValidatorIndex: idx,
}
signedChange := &ethpb.SignedBLSToExecutionChange{
Message: change,
}
body := &ethpb.BeaconBlockBodyCapella{
BlsToExecutionChanges: []*ethpb.SignedBLSToExecutionChange{signedChange},
}
pbb := &ethpb.BeaconBlockCapella{
Body: body,
}
blk, err := consensusblocks.NewBeaconBlock(pbb)
require.NoError(t, err)

pool.InsertBLSToExecChange(signedChange)
require.Equal(t, true, pool.ValidatorExists(idx))
require.NoError(t, service.handleBlockBLSToExecChanges(blk))
require.Equal(t, false, pool.ValidatorExists(idx))
})
}

// Helper function to simulate the block being on time or delayed for proposer
// boost. It alters the genesisTime tracked by the store.
func driftGenesisTime(s *Service, slot int64, delay int64) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ func processJustificationBits(state state.BeaconState, totalActiveBalance, prevE
return newBits
}

// updateJustificationAndFinalization processes justification and finalization during
// weighJustificationAndFinalization processes justification and finalization during
// epoch processing. This is where a beacon node can justify and finalize a new epoch.
func weighJustificationAndFinalization(state state.BeaconState, newBits bitfield.Bitvector4) (state.BeaconState, error) {
jc, fc, err := computeCheckpoints(state, newBits)
Expand Down
6 changes: 6 additions & 0 deletions beacon-chain/sync/validate_bls_to_execution_change.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"github.com/prysmaticlabs/prysm/v3/beacon-chain/core/blocks"
"github.com/prysmaticlabs/prysm/v3/monitoring/tracing"
ethpb "github.com/prysmaticlabs/prysm/v3/proto/prysm/v1alpha1"
"github.com/prysmaticlabs/prysm/v3/runtime/version"
"go.opencensus.io/trace"
)

Expand Down Expand Up @@ -45,6 +46,11 @@ func (s *Service) validateBlsToExecutionChange(ctx context.Context, pid peer.ID,
if err != nil {
return pubsub.ValidationIgnore, err
}
// Ignore messages if our current head state doesn't support
// capella.
if st.Version() < version.Capella {
return pubsub.ValidationIgnore, nil
}
// Validate that the execution change object is valid.
_, err = blocks.ValidateBLSToExecutionChange(st, blsChange)
if err != nil {
Expand Down
37 changes: 37 additions & 0 deletions beacon-chain/sync/validate_bls_to_execution_change_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,43 @@ func TestService_ValidateBlsToExecutionChange(t *testing.T) {
}},
want: pubsub.ValidationIgnore,
},
{
name: "Non-capella Head state",
svc: NewService(context.Background(),
WithP2P(mockp2p.NewTestP2P(t)),
WithInitialSync(&mockSync.Sync{IsSyncing: false}),
WithChainService(chainService),
WithStateNotifier(chainService.StateNotifier()),
WithOperationNotifier(chainService.OperationNotifier()),
WithBlsToExecPool(blstoexec.NewPool()),
),
setupSvc: func(s *Service, msg *ethpb.SignedBLSToExecutionChange, topic string) (*Service, string) {
s.cfg.stateGen = stategen.New(beaconDB, doublylinkedtree.New())
s.cfg.beaconDB = beaconDB
s.initCaches()
st, _ := util.DeterministicGenesisStateBellatrix(t, 128)
s.cfg.chain = &mockChain.ChainService{
ValidatorsRoot: [32]byte{'A'},
Genesis: time.Now().Add(-time.Second * time.Duration(params.BeaconConfig().SecondsPerSlot) * time.Duration(10)),
State: st,
}

return s, topic
},
args: args{
ctx: context.Background(),
pid: "random",
topic: fmt.Sprintf(defaultTopic, fakeDigest),
msg: &ethpb.SignedBLSToExecutionChange{
Message: &ethpb.BLSToExecutionChange{
ValidatorIndex: 0,
FromBlsPubkey: make([]byte, 48),
ToExecutionAddress: make([]byte, 20),
},
Signature: emptySig[:],
}},
want: pubsub.ValidationIgnore,
},
{
name: "Non-existent Validator Index",
svc: NewService(context.Background(),
Expand Down
13 changes: 12 additions & 1 deletion consensus-types/payload-attribute/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
load("@prysm//tools/go:def.bzl", "go_library")
load("@prysm//tools/go:def.bzl", "go_library", "go_test")

go_library(
name = "go_default_library",
Expand All @@ -16,3 +16,14 @@ go_library(
"@com_github_pkg_errors//:go_default_library",
],
)

go_test(
name = "go_default_test",
srcs = ["getters_test.go"],
embed = [":go_default_library"],
deps = [
"//proto/engine/v1:go_default_library",
"//runtime/version:go_default_library",
"//testing/require:go_default_library",
],
)
17 changes: 13 additions & 4 deletions consensus-types/payload-attribute/getters.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ func (a *data) SuggestedFeeRecipient() []byte {
// Withdrawals returns the withdrawals of the payload attribute.
// Support for withdrawals was added in version 2 of the payload attribute.
func (a *data) Withdrawals() ([]*enginev1.Withdrawal, error) {
if a == nil {
return nil, errNilPayloadAttribute
}
if a.version < version.Capella {
return nil, blocks.ErrNotSupported("Withdrawals", a.version)
}
Expand All @@ -37,12 +40,15 @@ func (a *data) Withdrawals() ([]*enginev1.Withdrawal, error) {

// PbV1 returns the payload attribute in version 1.
func (a *data) PbV1() (*enginev1.PayloadAttributes, error) {
if a.timeStamp == 0 && len(a.prevRandao) == 0 {
return nil, nil
if a == nil {
return nil, errNilPayloadAttribute
}
if a.version != version.Bellatrix {
return nil, blocks.ErrNotSupported("PayloadAttributePbV1", a.version)
}
if a.timeStamp == 0 && len(a.prevRandao) == 0 {
return nil, nil
}
return &enginev1.PayloadAttributes{
Timestamp: a.timeStamp,
PrevRandao: a.prevRandao,
Expand All @@ -52,12 +58,15 @@ func (a *data) PbV1() (*enginev1.PayloadAttributes, error) {

// PbV2 returns the payload attribute in version 2.
func (a *data) PbV2() (*enginev1.PayloadAttributesV2, error) {
if a.timeStamp == 0 && len(a.prevRandao) == 0 {
return nil, nil
if a == nil {
return nil, errNilPayloadAttribute
}
if a.version != version.Capella {
return nil, blocks.ErrNotSupported("PayloadAttributePbV2", a.version)
}
if a.timeStamp == 0 && len(a.prevRandao) == 0 {
return nil, nil
}
return &enginev1.PayloadAttributesV2{
Timestamp: a.timeStamp,
PrevRandao: a.prevRandao,
Expand Down
143 changes: 143 additions & 0 deletions consensus-types/payload-attribute/getters_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,143 @@
package payloadattribute

import (
"testing"

enginev1 "github.com/prysmaticlabs/prysm/v3/proto/engine/v1"
"github.com/prysmaticlabs/prysm/v3/runtime/version"
"github.com/prysmaticlabs/prysm/v3/testing/require"
)

func TestPayloadAttributeGetters(t *testing.T) {
tests := []struct {
name string
tc func(t *testing.T)
}{
{
name: "Get version",
tc: func(t *testing.T) {
a := EmptyWithVersion(version.Capella)
require.Equal(t, version.Capella, a.Version())
},
},
{
name: "Get prev randao",
tc: func(t *testing.T) {
r := []byte{1, 2, 3}
a, err := New(&enginev1.PayloadAttributes{PrevRandao: r})
require.NoError(t, err)
require.DeepEqual(t, r, a.PrevRandao())
},
},
{
name: "Get suggested fee recipient",
tc: func(t *testing.T) {
r := []byte{4, 5, 6}
a, err := New(&enginev1.PayloadAttributes{SuggestedFeeRecipient: r})
require.NoError(t, err)
require.DeepEqual(t, r, a.SuggestedFeeRecipient())
},
},
{
name: "Get timestamp",
tc: func(t *testing.T) {
r := uint64(123)
a, err := New(&enginev1.PayloadAttributes{Timestamp: r})
require.NoError(t, err)
require.Equal(t, r, a.Timestamps())
},
},
{
name: "Get withdrawals (bellatrix)",
tc: func(t *testing.T) {
a := EmptyWithVersion(version.Bellatrix)
_, err := a.Withdrawals()
require.ErrorContains(t, "Withdrawals is not supported for bellatrix: unsupported getter", err)
},
},
{
name: "Get withdrawals (capella)",
tc: func(t *testing.T) {
wd := []*enginev1.Withdrawal{{WithdrawalIndex: 1}, {WithdrawalIndex: 2}, {WithdrawalIndex: 3}}
a, err := New(&enginev1.PayloadAttributesV2{Withdrawals: wd})
require.NoError(t, err)
got, err := a.Withdrawals()
require.NoError(t, err)
require.DeepEqual(t, wd, got)
},
},
{
name: "Get PbV1 (bad version)",
tc: func(t *testing.T) {
a, err := New(&enginev1.PayloadAttributes{})
require.NoError(t, err)
_, err = a.PbV2()
require.ErrorContains(t, "PayloadAttributePbV2 is not supported for bellatrix: unsupported getter", err)
},
},
{
name: "Get PbV2 (bad version)",
tc: func(t *testing.T) {
a, err := New(&enginev1.PayloadAttributesV2{})
require.NoError(t, err)
_, err = a.PbV1()
require.ErrorContains(t, "PayloadAttributePbV1 is not supported for capella: unsupported getter", err)
},
},
{
name: "Get PbV1 (nil)",
tc: func(t *testing.T) {
a, err := New(&enginev1.PayloadAttributes{})
require.NoError(t, err)
got, err := a.PbV1()
require.NoError(t, err)
require.Equal(t, (*enginev1.PayloadAttributes)(nil), got)
},
},
{
name: "Get PbV2 (nil)",
tc: func(t *testing.T) {
a, err := New(&enginev1.PayloadAttributesV2{})
require.NoError(t, err)
got, err := a.PbV2()
require.NoError(t, err)
require.Equal(t, (*enginev1.PayloadAttributesV2)(nil), got)
},
},
{
name: "Get PbV1",
tc: func(t *testing.T) {
p := &enginev1.PayloadAttributes{
Timestamp: 1,
PrevRandao: []byte{1, 2, 3},
SuggestedFeeRecipient: []byte{4, 5, 6},
}
a, err := New(p)
require.NoError(t, err)
got, err := a.PbV1()
require.NoError(t, err)
require.DeepEqual(t, p, got)
},
},
{
name: "Get PbV2",
tc: func(t *testing.T) {
p := &enginev1.PayloadAttributesV2{
Timestamp: 1,
PrevRandao: []byte{1, 2, 3},
SuggestedFeeRecipient: []byte{4, 5, 6},
Withdrawals: []*enginev1.Withdrawal{{WithdrawalIndex: 1}, {WithdrawalIndex: 2}, {WithdrawalIndex: 3}},
}
a, err := New(p)
require.NoError(t, err)
got, err := a.PbV2()
require.NoError(t, err)
require.DeepEqual(t, p, got)
},
},
}

for _, test := range tests {
t.Run(test.name, test.tc)
}
}
Loading

0 comments on commit 57bdb90

Please sign in to comment.