Refactor process operation function arguments (#8767)

* Pass slash validator func as argument

* Refactor ProcessBlockHeaderNoVerify

* Refactor Eth1Data and Randao

* Refactor ProposerSlashing

* Refactor AttesterSlashing

* Refactor VoluntaryExit

* Add VerifyNilBeaconBlock to ProcessBlock

Co-authored-by: Victor Farazdagi <simple.square@gmail.com>
This commit is contained in:
terence tsao
2021-04-15 06:58:54 -07:00
committed by GitHub
parent 405e2a1a03
commit 169cd78bbd
20 changed files with 132 additions and 114 deletions

View File

@@ -36,15 +36,10 @@ import (
func ProcessAttesterSlashings(
ctx context.Context,
beaconState iface.BeaconState,
b *ethpb.SignedBeaconBlock,
slashings []*ethpb.AttesterSlashing,
slashFunc slashValidatorFunc,
) (iface.BeaconState, error) {
if err := helpers.VerifyNilBeaconBlock(b); err != nil {
return nil, err
}
body := b.Block.Body
for idx, slashing := range body.AttesterSlashings {
for idx, slashing := range slashings {
if err := VerifyAttesterSlashing(ctx, beaconState, slashing); err != nil {
return nil, errors.Wrapf(err, "could not verify attester slashing %d", idx)
}

View File

@@ -58,7 +58,7 @@ func TestProcessAttesterSlashings_DataNotSlashable(t *testing.T) {
AttesterSlashings: slashings,
},
}
_, err = blocks.ProcessAttesterSlashings(context.Background(), beaconState, b, v.SlashValidator)
_, err = blocks.ProcessAttesterSlashings(context.Background(), beaconState, b.Block.Body.AttesterSlashings, v.SlashValidator)
assert.ErrorContains(t, "attestations are not slashable", err)
}
@@ -93,7 +93,7 @@ func TestProcessAttesterSlashings_IndexedAttestationFailedToVerify(t *testing.T)
},
}
_, err = blocks.ProcessAttesterSlashings(context.Background(), beaconState, b, v.SlashValidator)
_, err = blocks.ProcessAttesterSlashings(context.Background(), beaconState, b.Block.Body.AttesterSlashings, v.SlashValidator)
assert.ErrorContains(t, "validator indices count exceeds MAX_VALIDATORS_PER_COMMITTEE", err)
}
@@ -145,7 +145,7 @@ func TestProcessAttesterSlashings_AppliesCorrectStatus(t *testing.T) {
},
}
newState, err := blocks.ProcessAttesterSlashings(context.Background(), beaconState, b, v.SlashValidator)
newState, err := blocks.ProcessAttesterSlashings(context.Background(), beaconState, b.Block.Body.AttesterSlashings, v.SlashValidator)
require.NoError(t, err)
newRegistry := newState.Validators()

View File

@@ -72,14 +72,14 @@ func TestFuzzverifyDepositDataSigningRoot_10000(t *testing.T) {
func TestFuzzProcessEth1DataInBlock_10000(t *testing.T) {
fuzzer := fuzz.NewWithSeed(0)
b := &eth.SignedBeaconBlock{}
e := &eth.Eth1Data{}
state := &stateV0.BeaconState{}
for i := 0; i < 10000; i++ {
fuzzer.Fuzz(state)
fuzzer.Fuzz(b)
s, err := ProcessEth1DataInBlock(context.Background(), state, b)
fuzzer.Fuzz(e)
s, err := ProcessEth1DataInBlock(context.Background(), state, e)
if err != nil && s != nil {
t.Fatalf("state should be nil on err. found: %v on error: %v for state: %v and block: %v", s, err, state, b)
t.Fatalf("state should be nil on err. found: %v on error: %v for state: %v and eth1data: %v", s, err, state, e)
}
}
}
@@ -124,7 +124,7 @@ func TestFuzzProcessBlockHeaderNoVerify_10000(t *testing.T) {
fuzzer.Fuzz(block)
s, err := stateV0.InitializeFromProtoUnsafe(state)
require.NoError(t, err)
_, err = ProcessBlockHeaderNoVerify(s, block)
_, err = ProcessBlockHeaderNoVerify(s, block.Slot, block.ProposerIndex, block.ParentRoot, []byte{})
_ = err
}
}
@@ -156,7 +156,7 @@ func TestFuzzProcessRandaoNoVerify_10000(t *testing.T) {
fuzzer.Fuzz(blockBody)
s, err := stateV0.InitializeFromProtoUnsafe(state)
require.NoError(t, err)
r, err := ProcessRandaoNoVerify(s, blockBody)
r, err := ProcessRandaoNoVerify(s, blockBody.RandaoReveal)
if err != nil && r != nil {
t.Fatalf("return value should be nil on err. found: %v on error: %v for state: %v and block: %v", r, err, state, blockBody)
}
@@ -166,16 +166,16 @@ func TestFuzzProcessRandaoNoVerify_10000(t *testing.T) {
func TestFuzzProcessProposerSlashings_10000(t *testing.T) {
fuzzer := fuzz.NewWithSeed(0)
state := &pb.BeaconState{}
b := &eth.SignedBeaconBlock{}
p := &eth.ProposerSlashing{}
ctx := context.Background()
for i := 0; i < 10000; i++ {
fuzzer.Fuzz(state)
fuzzer.Fuzz(b)
fuzzer.Fuzz(p)
s, err := stateV0.InitializeFromProtoUnsafe(state)
require.NoError(t, err)
r, err := ProcessProposerSlashings(ctx, s, b, v.SlashValidator)
r, err := ProcessProposerSlashings(ctx, s, []*eth.ProposerSlashing{p}, v.SlashValidator)
if err != nil && r != nil {
t.Fatalf("return value should be nil on err. found: %v on error: %v for state: %v and block: %v", r, err, state, b)
t.Fatalf("return value should be nil on err. found: %v on error: %v for state: %v and slashing: %v", r, err, state, p)
}
}
}
@@ -197,16 +197,16 @@ func TestFuzzVerifyProposerSlashing_10000(t *testing.T) {
func TestFuzzProcessAttesterSlashings_10000(t *testing.T) {
fuzzer := fuzz.NewWithSeed(0)
state := &pb.BeaconState{}
b := &eth.SignedBeaconBlock{}
a := &eth.AttesterSlashing{}
ctx := context.Background()
for i := 0; i < 10000; i++ {
fuzzer.Fuzz(state)
fuzzer.Fuzz(b)
fuzzer.Fuzz(a)
s, err := stateV0.InitializeFromProtoUnsafe(state)
require.NoError(t, err)
r, err := ProcessAttesterSlashings(ctx, s, b, v.SlashValidator)
r, err := ProcessAttesterSlashings(ctx, s, []*eth.AttesterSlashing{a}, v.SlashValidator)
if err != nil && r != nil {
t.Fatalf("return value should be nil on err. found: %v on error: %v for state: %v and block: %v", r, err, state, b)
t.Fatalf("return value should be nil on err. found: %v on error: %v for state: %v and slashing: %v", r, err, state, a)
}
}
}
@@ -400,16 +400,16 @@ func TestFuzzverifyDeposit_10000(t *testing.T) {
func TestFuzzProcessVoluntaryExits_10000(t *testing.T) {
fuzzer := fuzz.NewWithSeed(0)
state := &pb.BeaconState{}
b := &eth.SignedBeaconBlock{}
e := &eth.SignedVoluntaryExit{}
ctx := context.Background()
for i := 0; i < 10000; i++ {
fuzzer.Fuzz(state)
fuzzer.Fuzz(b)
fuzzer.Fuzz(e)
s, err := stateV0.InitializeFromProtoUnsafe(state)
require.NoError(t, err)
r, err := ProcessVoluntaryExits(ctx, s, b)
r, err := ProcessVoluntaryExits(ctx, s, []*eth.SignedVoluntaryExit{e})
if err != nil && r != nil {
t.Fatalf("return value should be nil on err. found: %v on error: %v for state: %v and block: %v", r, err, state, b)
t.Fatalf("return value should be nil on err. found: %v on error: %v for state: %v and exit: %v", r, err, state, e)
}
}
}
@@ -417,15 +417,15 @@ func TestFuzzProcessVoluntaryExits_10000(t *testing.T) {
func TestFuzzProcessVoluntaryExitsNoVerify_10000(t *testing.T) {
fuzzer := fuzz.NewWithSeed(0)
state := &pb.BeaconState{}
b := &eth.SignedBeaconBlock{}
e := &eth.SignedVoluntaryExit{}
for i := 0; i < 10000; i++ {
fuzzer.Fuzz(state)
fuzzer.Fuzz(b)
fuzzer.Fuzz(e)
s, err := stateV0.InitializeFromProtoUnsafe(state)
require.NoError(t, err)
r, err := ProcessVoluntaryExits(context.Background(), s, b)
r, err := ProcessVoluntaryExits(context.Background(), s, []*eth.SignedVoluntaryExit{e})
if err != nil && r != nil {
t.Fatalf("return value should be nil on err. found: %v on error: %v for state: %v and block: %v", r, err, state, b)
t.Fatalf("return value should be nil on err. found: %v on error: %v for state: %v and block: %v", r, err, state, e)
}
}
}

View File

@@ -91,7 +91,7 @@ func TestProcessAttesterSlashings_RegressionSlashableIndices(t *testing.T) {
},
}
newState, err := blocks.ProcessAttesterSlashings(context.Background(), beaconState, b, v.SlashValidator)
newState, err := blocks.ProcessAttesterSlashings(context.Background(), beaconState, b.Block.Body.AttesterSlashings, v.SlashValidator)
require.NoError(t, err)
newRegistry := newState.Validators()
if !newRegistry[expectedSlashedVal].Slashed {

View File

@@ -20,23 +20,19 @@ import (
// state.eth1_data_votes.append(body.eth1_data)
// if state.eth1_data_votes.count(body.eth1_data) * 2 > EPOCHS_PER_ETH1_VOTING_PERIOD * SLOTS_PER_EPOCH:
// state.eth1_data = body.eth1_data
func ProcessEth1DataInBlock(_ context.Context, beaconState iface.BeaconState, b *ethpb.SignedBeaconBlock) (iface.BeaconState, error) {
block := b.Block
func ProcessEth1DataInBlock(_ context.Context, beaconState iface.BeaconState, eth1Data *ethpb.Eth1Data) (iface.BeaconState, error) {
if beaconState == nil {
return nil, errors.New("nil state")
}
if block == nil || block.Body == nil {
return nil, errors.New("nil block or block withought body")
}
if err := beaconState.AppendEth1DataVotes(block.Body.Eth1Data); err != nil {
if err := beaconState.AppendEth1DataVotes(eth1Data); err != nil {
return nil, err
}
hasSupport, err := Eth1DataHasEnoughSupport(beaconState, block.Body.Eth1Data)
hasSupport, err := Eth1DataHasEnoughSupport(beaconState, eth1Data)
if err != nil {
return nil, err
}
if hasSupport {
if err := beaconState.SetEth1Data(block.Body.Eth1Data); err != nil {
if err := beaconState.SetEth1Data(eth1Data); err != nil {
return nil, err
}
}

View File

@@ -178,7 +178,7 @@ func TestProcessEth1Data_SetsCorrectly(t *testing.T) {
period := uint64(params.BeaconConfig().SlotsPerEpoch.Mul(uint64(params.BeaconConfig().EpochsPerEth1VotingPeriod)))
var ok bool
for i := uint64(0); i < period; i++ {
processedState, err := blocks.ProcessEth1DataInBlock(context.Background(), beaconState, b)
processedState, err := blocks.ProcessEth1DataInBlock(context.Background(), beaconState, b.Block.Body.Eth1Data)
require.NoError(t, err)
beaconState, ok = processedState.(*stateV0.BeaconState)
require.Equal(t, true, ok)

View File

@@ -46,14 +46,8 @@ var ValidatorCannotExitYetMsg = "validator has not been active long enough to ex
func ProcessVoluntaryExits(
_ context.Context,
beaconState iface.BeaconState,
b *ethpb.SignedBeaconBlock,
exits []*ethpb.SignedVoluntaryExit,
) (iface.BeaconState, error) {
if err := helpers.VerifyNilBeaconBlock(b); err != nil {
return nil, err
}
body := b.Block.Body
exits := body.VoluntaryExits
for idx, exit := range exits {
if exit == nil || exit.Exit == nil {
return nil, errors.New("nil voluntary exit in block body")

View File

@@ -44,7 +44,7 @@ func TestProcessVoluntaryExits_NotActiveLongEnoughToExit(t *testing.T) {
}
want := "validator has not been active long enough to exit"
_, err = blocks.ProcessVoluntaryExits(context.Background(), state, b)
_, err = blocks.ProcessVoluntaryExits(context.Background(), state, b.Block.Body.VoluntaryExits)
assert.ErrorContains(t, want, err)
}
@@ -74,7 +74,7 @@ func TestProcessVoluntaryExits_ExitAlreadySubmitted(t *testing.T) {
}
want := "validator with index 0 has already submitted an exit, which will take place at epoch: 10"
_, err = blocks.ProcessVoluntaryExits(context.Background(), state, b)
_, err = blocks.ProcessVoluntaryExits(context.Background(), state, b.Block.Body.VoluntaryExits)
assert.ErrorContains(t, want, err)
}
@@ -122,7 +122,7 @@ func TestProcessVoluntaryExits_AppliesCorrectStatus(t *testing.T) {
},
}
newState, err := blocks.ProcessVoluntaryExits(context.Background(), state, b)
newState, err := blocks.ProcessVoluntaryExits(context.Background(), state, b.Block.Body.VoluntaryExits)
require.NoError(t, err, "Could not process exits")
newRegistry := newState.Validators()
if newRegistry[0].ExitEpoch != helpers.ActivationExitEpoch(types.Epoch(state.Slot()/params.BeaconConfig().SlotsPerEpoch)) {

View File

@@ -5,7 +5,7 @@ import (
"context"
"fmt"
"github.com/pkg/errors"
types "github.com/prysmaticlabs/eth2-types"
ethpb "github.com/prysmaticlabs/ethereumapis/eth/v1alpha1"
"github.com/prysmaticlabs/prysm/beacon-chain/core/helpers"
iface "github.com/prysmaticlabs/prysm/beacon-chain/state/interface"
@@ -42,7 +42,14 @@ func ProcessBlockHeader(
beaconState iface.BeaconState,
block *ethpb.SignedBeaconBlock,
) (iface.BeaconState, error) {
beaconState, err := ProcessBlockHeaderNoVerify(beaconState, block.Block)
if err := helpers.VerifyNilBeaconBlock(block); err != nil {
return nil, err
}
bodyRoot, err := block.Block.Body.HashTreeRoot()
if err != nil {
return nil, err
}
beaconState, err = ProcessBlockHeaderNoVerify(beaconState, block.Block.Slot, block.Block.ProposerIndex, block.Block.ParentRoot, bodyRoot[:])
if err != nil {
return nil, err
}
@@ -85,34 +92,32 @@ func ProcessBlockHeader(
// assert not proposer.slashed
func ProcessBlockHeaderNoVerify(
beaconState iface.BeaconState,
block *ethpb.BeaconBlock,
slot types.Slot, proposerIndex types.ValidatorIndex,
parentRoot, bodyRoot []byte,
) (iface.BeaconState, error) {
if block == nil {
return nil, errors.New("nil block")
}
if beaconState.Slot() != block.Slot {
return nil, fmt.Errorf("state slot: %d is different than block slot: %d", beaconState.Slot(), block.Slot)
if beaconState.Slot() != slot {
return nil, fmt.Errorf("state slot: %d is different than block slot: %d", beaconState.Slot(), slot)
}
idx, err := helpers.BeaconProposerIndex(beaconState)
if err != nil {
return nil, err
}
if block.ProposerIndex != idx {
return nil, fmt.Errorf("proposer index: %d is different than calculated: %d", block.ProposerIndex, idx)
if proposerIndex != idx {
return nil, fmt.Errorf("proposer index: %d is different than calculated: %d", proposerIndex, idx)
}
parentHeader := beaconState.LatestBlockHeader()
if parentHeader.Slot >= block.Slot {
return nil, fmt.Errorf("block.Slot %d must be greater than state.LatestBlockHeader.Slot %d", block.Slot, parentHeader.Slot)
if parentHeader.Slot >= slot {
return nil, fmt.Errorf("block.Slot %d must be greater than state.LatestBlockHeader.Slot %d", slot, parentHeader.Slot)
}
parentRoot, err := parentHeader.HashTreeRoot()
parentHeaderRoot, err := parentHeader.HashTreeRoot()
if err != nil {
return nil, err
}
if !bytes.Equal(block.ParentRoot, parentRoot[:]) {
if !bytes.Equal(parentRoot, parentHeaderRoot[:]) {
return nil, fmt.Errorf(
"parent root %#x does not match the latest block header signing root in state %#x",
block.ParentRoot, parentRoot)
parentRoot, parentHeaderRoot[:])
}
proposer, err := beaconState.ValidatorAtIndexReadOnly(idx)
@@ -123,16 +128,12 @@ func ProcessBlockHeaderNoVerify(
return nil, fmt.Errorf("proposer at index %d was previously slashed", idx)
}
bodyRoot, err := block.Body.HashTreeRoot()
if err != nil {
return nil, err
}
if err := beaconState.SetLatestBlockHeader(&ethpb.BeaconBlockHeader{
Slot: block.Slot,
ProposerIndex: block.ProposerIndex,
ParentRoot: block.ParentRoot,
Slot: slot,
ProposerIndex: proposerIndex,
ParentRoot: parentRoot,
StateRoot: params.BeaconConfig().ZeroHash[:],
BodyRoot: bodyRoot[:],
BodyRoot: bodyRoot,
}); err != nil {
return nil, err
}

View File

@@ -125,16 +125,13 @@ func TestProcessBlockHeader_DifferentSlots(t *testing.T) {
blockSig, err := helpers.ComputeDomainAndSign(state, currentEpoch, &sszBytes, params.BeaconConfig().DomainBeaconProposer, priv)
require.NoError(t, err)
validators[5896].PublicKey = priv.PublicKey().Marshal()
block := &ethpb.SignedBeaconBlock{
block := testutil.HydrateSignedBeaconBlock(&ethpb.SignedBeaconBlock{
Block: &ethpb.BeaconBlock{
Slot: 1,
Body: &ethpb.BeaconBlockBody{
RandaoReveal: []byte{'A', 'B', 'C'},
},
Slot: 1,
ParentRoot: lbhsr[:],
},
Signature: blockSig,
}
})
_, err = blocks.ProcessBlockHeader(context.Background(), state, block)
want := "is different than block slot"

View File

@@ -43,16 +43,11 @@ type slashValidatorFunc func(iface.BeaconState, types.ValidatorIndex) (iface.Bea
func ProcessProposerSlashings(
_ context.Context,
beaconState iface.BeaconState,
b *ethpb.SignedBeaconBlock,
slashings []*ethpb.ProposerSlashing,
slashFunc slashValidatorFunc,
) (iface.BeaconState, error) {
if err := helpers.VerifyNilBeaconBlock(b); err != nil {
return nil, err
}
body := b.Block.Body
var err error
for idx, slashing := range body.ProposerSlashings {
for idx, slashing := range slashings {
if slashing == nil {
return nil, errors.New("nil proposer slashings in block body")
}

View File

@@ -50,7 +50,7 @@ func TestProcessProposerSlashings_UnmatchedHeaderSlots(t *testing.T) {
},
}
want := "mismatched header slots"
_, err := blocks.ProcessProposerSlashings(context.Background(), beaconState, b, v.SlashValidator)
_, err := blocks.ProcessProposerSlashings(context.Background(), beaconState, b.Block.Body.ProposerSlashings, v.SlashValidator)
assert.ErrorContains(t, want, err)
}
@@ -83,7 +83,7 @@ func TestProcessProposerSlashings_SameHeaders(t *testing.T) {
},
}
want := "expected slashing headers to differ"
_, err := blocks.ProcessProposerSlashings(context.Background(), beaconState, b, v.SlashValidator)
_, err := blocks.ProcessProposerSlashings(context.Background(), beaconState, b.Block.Body.ProposerSlashings, v.SlashValidator)
assert.ErrorContains(t, want, err)
}
@@ -133,7 +133,7 @@ func TestProcessProposerSlashings_ValidatorNotSlashable(t *testing.T) {
"validator with key %#x is not slashable",
bytesutil.ToBytes48(beaconState.Validators()[0].PublicKey),
)
_, err = blocks.ProcessProposerSlashings(context.Background(), beaconState, b, v.SlashValidator)
_, err = blocks.ProcessProposerSlashings(context.Background(), beaconState, b.Block.Body.ProposerSlashings, v.SlashValidator)
assert.ErrorContains(t, want, err)
}
@@ -172,7 +172,7 @@ func TestProcessProposerSlashings_AppliesCorrectStatus(t *testing.T) {
block := testutil.NewBeaconBlock()
block.Block.Body.ProposerSlashings = slashings
newState, err := blocks.ProcessProposerSlashings(context.Background(), beaconState, block, v.SlashValidator)
newState, err := blocks.ProcessProposerSlashings(context.Background(), beaconState, block.Block.Body.ProposerSlashings, v.SlashValidator)
require.NoError(t, err)
newStateVals := newState.Validators()

View File

@@ -42,7 +42,7 @@ func ProcessRandao(
return nil, errors.Wrap(err, "could not verify block randao")
}
beaconState, err = ProcessRandaoNoVerify(beaconState, body)
beaconState, err = ProcessRandaoNoVerify(beaconState, body.RandaoReveal)
if err != nil {
return nil, errors.Wrap(err, "could not process randao")
}
@@ -60,7 +60,7 @@ func ProcessRandao(
// )
func ProcessRandaoNoVerify(
beaconState iface.BeaconState,
body *ethpb.BeaconBlockBody,
randaoReveal []byte,
) (iface.BeaconState, error) {
currentEpoch := helpers.SlotToEpoch(beaconState.Slot())
// If block randao passed verification, we XOR the state's latest randao mix with the block's
@@ -70,7 +70,7 @@ func ProcessRandaoNoVerify(
if err != nil {
return nil, err
}
blockRandaoReveal := hashutil.Hash(body.RandaoReveal)
blockRandaoReveal := hashutil.Hash(randaoReveal)
if len(blockRandaoReveal) != len(latestMixSlice) {
return nil, errors.New("blockRandaoReveal length doesnt match latestMixSlice length")
}

View File

@@ -28,7 +28,7 @@ func runAttesterSlashingTest(t *testing.T, config string) {
body := &ethpb.BeaconBlockBody{AttesterSlashings: []*ethpb.AttesterSlashing{attSlashing}}
testutil.RunBlockOperationTest(t, folderPath, body, func(ctx context.Context, s iface.BeaconState, b *ethpb.SignedBeaconBlock) (iface.BeaconState, error) {
return blocks.ProcessAttesterSlashings(ctx, s, b, v.SlashValidator)
return blocks.ProcessAttesterSlashings(ctx, s, b.Block.Body.AttesterSlashings, v.SlashValidator)
})
})
}

View File

@@ -46,7 +46,9 @@ func runBlockHeaderTest(t *testing.T, config string) {
}
// Spectest blocks are not signed, so we'll call NoVerify to skip sig verification.
beaconState, err := blocks.ProcessBlockHeaderNoVerify(preBeaconState, block)
bodyRoot, err := block.Body.HashTreeRoot()
require.NoError(t, err)
beaconState, err := blocks.ProcessBlockHeaderNoVerify(preBeaconState, block.Slot, block.ProposerIndex, block.ParentRoot, bodyRoot[:])
if postSSZExists {
require.NoError(t, err)

View File

@@ -28,7 +28,7 @@ func runProposerSlashingTest(t *testing.T, config string) {
body := &ethpb.BeaconBlockBody{ProposerSlashings: []*ethpb.ProposerSlashing{proposerSlashing}}
testutil.RunBlockOperationTest(t, folderPath, body, func(ctx context.Context, s iface.BeaconState, b *ethpb.SignedBeaconBlock) (iface.BeaconState, error) {
return blocks.ProcessProposerSlashings(ctx, s, b, v.SlashValidator)
return blocks.ProcessProposerSlashings(ctx, s, b.Block.Body.ProposerSlashings, v.SlashValidator)
})
})
}

View File

@@ -1,11 +1,13 @@
package spectest
import (
"context"
"path"
"testing"
ethpb "github.com/prysmaticlabs/ethereumapis/eth/v1alpha1"
"github.com/prysmaticlabs/prysm/beacon-chain/core/blocks"
iface "github.com/prysmaticlabs/prysm/beacon-chain/state/interface"
"github.com/prysmaticlabs/prysm/shared/params/spectest"
"github.com/prysmaticlabs/prysm/shared/testutil"
"github.com/prysmaticlabs/prysm/shared/testutil/require"
@@ -24,7 +26,9 @@ func runVoluntaryExitTest(t *testing.T, config string) {
require.NoError(t, voluntaryExit.UnmarshalSSZ(exitFile), "Failed to unmarshal")
body := &ethpb.BeaconBlockBody{VoluntaryExits: []*ethpb.SignedVoluntaryExit{voluntaryExit}}
testutil.RunBlockOperationTest(t, folderPath, body, blocks.ProcessVoluntaryExits)
testutil.RunBlockOperationTest(t, folderPath, body, func(ctx context.Context, s iface.BeaconState, b *ethpb.SignedBeaconBlock) (iface.BeaconState, error) {
return blocks.ProcessVoluntaryExits(ctx, s, b.Block.Body.VoluntaryExits)
})
})
}
}

View File

@@ -32,11 +32,19 @@ var processDepositsFunc = func(ctx context.Context, s iface.BeaconState, blk *et
return b.ProcessDeposits(ctx, s, blk.Block.Body.Deposits)
}
var processProposerSlashingFunc = func(ctx context.Context, s iface.BeaconState, blk *ethpb.SignedBeaconBlock) (iface.BeaconState, error) {
return b.ProcessProposerSlashings(ctx, s, blk, v.SlashValidator)
return b.ProcessProposerSlashings(ctx, s, blk.Block.Body.ProposerSlashings, v.SlashValidator)
}
var processAttesterSlashingFunc = func(ctx context.Context, s iface.BeaconState, blk *ethpb.SignedBeaconBlock) (iface.BeaconState, error) {
return b.ProcessAttesterSlashings(ctx, s, blk, v.SlashValidator)
return b.ProcessAttesterSlashings(ctx, s, blk.Block.Body.AttesterSlashings, v.SlashValidator)
}
var processEth1DataFunc = func(ctx context.Context, s iface.BeaconState, blk *ethpb.SignedBeaconBlock) (iface.BeaconState, error) {
return b.ProcessEth1DataInBlock(ctx, s, blk.Block.Body.Eth1Data)
}
var processExitFunc = func(ctx context.Context, s iface.BeaconState, blk *ethpb.SignedBeaconBlock) (iface.BeaconState, error) {
return b.ProcessVoluntaryExits(ctx, s, blk.Block.Body.VoluntaryExits)
}
// This defines the processing block routine as outlined in eth2 spec:
@@ -44,13 +52,13 @@ var processAttesterSlashingFunc = func(ctx context.Context, s iface.BeaconState,
var processingPipeline = []processFunc{
b.ProcessBlockHeader,
b.ProcessRandao,
b.ProcessEth1DataInBlock,
processEth1DataFunc,
VerifyOperationLengths,
processProposerSlashingFunc,
processAttesterSlashingFunc,
b.ProcessAttestations,
processDepositsFunc,
b.ProcessVoluntaryExits,
processExitFunc,
}
// ExecuteStateTransition defines the procedure for a state transition function.
@@ -314,6 +322,10 @@ func ProcessBlock(
defer span.End()
var err error
if err = helpers.VerifyNilBeaconBlock(signed); err != nil {
return nil, err
}
for _, p := range processingPipeline {
state, err = p(ctx, state, signed)
if err != nil {

View File

@@ -8,6 +8,7 @@ import (
"github.com/pkg/errors"
ethpb "github.com/prysmaticlabs/ethereumapis/eth/v1alpha1"
b "github.com/prysmaticlabs/prysm/beacon-chain/core/blocks"
"github.com/prysmaticlabs/prysm/beacon-chain/core/helpers"
v "github.com/prysmaticlabs/prysm/beacon-chain/core/validators"
iface "github.com/prysmaticlabs/prysm/beacon-chain/state/interface"
"github.com/prysmaticlabs/prysm/shared/bls"
@@ -168,8 +169,17 @@ func ProcessBlockNoVerifyAnySig(
) (*bls.SignatureSet, iface.BeaconState, error) {
ctx, span := trace.StartSpan(ctx, "core.state.ProcessBlockNoVerifyAnySig")
defer span.End()
if err := helpers.VerifyNilBeaconBlock(signed); err != nil {
return nil, nil, err
}
state, err := b.ProcessBlockHeaderNoVerify(state, signed.Block)
blk := signed.Block
body := blk.Body
bodyRoot, err := body.HashTreeRoot()
if err != nil {
return nil, nil, err
}
state, err = b.ProcessBlockHeaderNoVerify(state, blk.Slot, blk.ProposerIndex, blk.ParentRoot, bodyRoot[:])
if err != nil {
traceutil.AnnotateError(span, err)
return nil, nil, errors.Wrap(err, "could not process block header")
@@ -184,13 +194,13 @@ func ProcessBlockNoVerifyAnySig(
traceutil.AnnotateError(span, err)
return nil, nil, errors.Wrap(err, "could not retrieve randao signature set")
}
state, err = b.ProcessRandaoNoVerify(state, signed.Block.Body)
state, err = b.ProcessRandaoNoVerify(state, signed.Block.Body.RandaoReveal)
if err != nil {
traceutil.AnnotateError(span, err)
return nil, nil, errors.Wrap(err, "could not verify and process randao")
}
state, err = b.ProcessEth1DataInBlock(ctx, state, signed)
state, err = b.ProcessEth1DataInBlock(ctx, state, signed.Block.Body.Eth1Data)
if err != nil {
traceutil.AnnotateError(span, err)
return nil, nil, errors.Wrap(err, "could not process eth1 data")
@@ -240,16 +250,19 @@ func ProcessOperationsNoVerifyAttsSigs(
signedBeaconBlock *ethpb.SignedBeaconBlock) (iface.BeaconState, error) {
ctx, span := trace.StartSpan(ctx, "core.state.ProcessOperationsNoVerifyAttsSigs")
defer span.End()
if err := helpers.VerifyNilBeaconBlock(signedBeaconBlock); err != nil {
return nil, err
}
if _, err := VerifyOperationLengths(ctx, state, signedBeaconBlock); err != nil {
return nil, errors.Wrap(err, "could not verify operation lengths")
}
state, err := b.ProcessProposerSlashings(ctx, state, signedBeaconBlock, v.SlashValidator)
state, err := b.ProcessProposerSlashings(ctx, state, signedBeaconBlock.Block.Body.ProposerSlashings, v.SlashValidator)
if err != nil {
return nil, errors.Wrap(err, "could not process block proposer slashings")
}
state, err = b.ProcessAttesterSlashings(ctx, state, signedBeaconBlock, v.SlashValidator)
state, err = b.ProcessAttesterSlashings(ctx, state, signedBeaconBlock.Block.Body.AttesterSlashings, v.SlashValidator)
if err != nil {
return nil, errors.Wrap(err, "could not process block attester slashings")
}
@@ -261,7 +274,7 @@ func ProcessOperationsNoVerifyAttsSigs(
if err != nil {
return nil, errors.Wrap(err, "could not process block validator deposits")
}
state, err = b.ProcessVoluntaryExits(ctx, state, signedBeaconBlock)
state, err = b.ProcessVoluntaryExits(ctx, state, signedBeaconBlock.Block.Body.VoluntaryExits)
if err != nil {
return nil, errors.Wrap(err, "could not process validator exits")
}
@@ -278,20 +291,29 @@ func ProcessBlockForStateRoot(
) (iface.BeaconState, error) {
ctx, span := trace.StartSpan(ctx, "core.state.ProcessBlockForStateRoot")
defer span.End()
if err := helpers.VerifyNilBeaconBlock(signed); err != nil {
return nil, err
}
state, err := b.ProcessBlockHeaderNoVerify(state, signed.Block)
blk := signed.Block
body := blk.Body
bodyRoot, err := body.HashTreeRoot()
if err != nil {
return nil, err
}
state, err = b.ProcessBlockHeaderNoVerify(state, blk.Slot, blk.ProposerIndex, blk.ParentRoot, bodyRoot[:])
if err != nil {
traceutil.AnnotateError(span, err)
return nil, errors.Wrap(err, "could not process block header")
}
state, err = b.ProcessRandaoNoVerify(state, signed.Block.Body)
state, err = b.ProcessRandaoNoVerify(state, signed.Block.Body.RandaoReveal)
if err != nil {
traceutil.AnnotateError(span, err)
return nil, errors.Wrap(err, "could not verify and process randao")
}
state, err = b.ProcessEth1DataInBlock(ctx, state, signed)
state, err = b.ProcessEth1DataInBlock(ctx, state, signed.Block.Body.Eth1Data)
if err != nil {
traceutil.AnnotateError(span, err)
return nil, errors.Wrap(err, "could not process eth1 data")

View File

@@ -344,7 +344,7 @@ func TestProposer_PendingDeposits_Eth1DataVoteOK(t *testing.T) {
assert.Equal(t, 0, eth1Height.Cmp(height))
newState, err := b.ProcessEth1DataInBlock(ctx, beaconState, blk)
newState, err := b.ProcessEth1DataInBlock(ctx, beaconState, blk.Block.Body.Eth1Data)
require.NoError(t, err)
if proto.Equal(newState.Eth1Data(), vote) {
@@ -358,7 +358,7 @@ func TestProposer_PendingDeposits_Eth1DataVoteOK(t *testing.T) {
require.NoError(t, err)
assert.Equal(t, 0, eth1Height.Cmp(newHeight))
newState, err = b.ProcessEth1DataInBlock(ctx, beaconState, blk)
newState, err = b.ProcessEth1DataInBlock(ctx, beaconState, blk.Block.Body.Eth1Data)
require.NoError(t, err)
if !proto.Equal(newState.Eth1Data(), vote) {