Change ProcessDeposits argument to use deposits (#8696)

This commit is contained in:
terence tsao 2021-04-02 07:48:41 -07:00 committed by GitHub
parent af3d3e8cd3
commit f67228bacb
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 24 additions and 19 deletions

View File

@ -331,16 +331,18 @@ func TestFuzzVerifyAttestation_10000(t *testing.T) {
func TestFuzzProcessDeposits_10000(t *testing.T) { func TestFuzzProcessDeposits_10000(t *testing.T) {
fuzzer := fuzz.NewWithSeed(0) fuzzer := fuzz.NewWithSeed(0)
state := &pb.BeaconState{} state := &pb.BeaconState{}
b := &eth.SignedBeaconBlock{} deposits := make([]*eth.Deposit, 100)
ctx := context.Background() ctx := context.Background()
for i := 0; i < 10000; i++ { for i := 0; i < 10000; i++ {
fuzzer.Fuzz(state) fuzzer.Fuzz(state)
fuzzer.Fuzz(b) for i := range deposits {
fuzzer.Fuzz(deposits[i])
}
s, err := stateV0.InitializeFromProtoUnsafe(state) s, err := stateV0.InitializeFromProtoUnsafe(state)
require.NoError(t, err) require.NoError(t, err)
r, err := ProcessDeposits(ctx, s, b) r, err := ProcessDeposits(ctx, s, deposits)
if err != nil && r != nil { 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, deposits)
} }
} }
} }

View File

@ -24,8 +24,7 @@ func ProcessPreGenesisDeposits(
deposits []*ethpb.Deposit, deposits []*ethpb.Deposit,
) (iface.BeaconState, error) { ) (iface.BeaconState, error) {
var err error var err error
beaconState, err = ProcessDeposits(ctx, beaconState, &ethpb.SignedBeaconBlock{ beaconState, err = ProcessDeposits(ctx, beaconState, deposits)
Block: &ethpb.BeaconBlock{Body: &ethpb.BeaconBlockBody{Deposits: deposits}}})
if err != nil { if err != nil {
return nil, errors.Wrap(err, "could not process deposit") return nil, errors.Wrap(err, "could not process deposit")
} }
@ -68,13 +67,8 @@ func ProcessPreGenesisDeposits(
func ProcessDeposits( func ProcessDeposits(
ctx context.Context, ctx context.Context,
beaconState iface.BeaconState, beaconState iface.BeaconState,
b *ethpb.SignedBeaconBlock, deposits []*ethpb.Deposit,
) (iface.BeaconState, error) { ) (iface.BeaconState, error) {
if err := helpers.VerifyNilBeaconBlock(b); err != nil {
return nil, err
}
deposits := b.Block.Body.Deposits
var err error var err error
domain, err := helpers.ComputeDomain(params.BeaconConfig().DomainDeposit, nil, nil) domain, err := helpers.ComputeDomain(params.BeaconConfig().DomainDeposit, nil, nil)
if err != nil { if err != nil {

View File

@ -50,7 +50,7 @@ func TestProcessDeposits_SameValidatorMultipleDepositsSameBlock(t *testing.T) {
}, },
}) })
require.NoError(t, err) require.NoError(t, err)
newState, err := blocks.ProcessDeposits(context.Background(), beaconState, b) newState, err := blocks.ProcessDeposits(context.Background(), beaconState, b.Block.Body.Deposits)
require.NoError(t, err, "Expected block deposits to process correctly") require.NoError(t, err, "Expected block deposits to process correctly")
assert.Equal(t, 2, len(newState.Validators()), "Incorrect validator count") assert.Equal(t, 2, len(newState.Validators()), "Incorrect validator count")
@ -88,7 +88,7 @@ func TestProcessDeposits_MerkleBranchFailsVerification(t *testing.T) {
}) })
require.NoError(t, err) require.NoError(t, err)
want := "deposit root did not verify" want := "deposit root did not verify"
_, err = blocks.ProcessDeposits(context.Background(), beaconState, b) _, err = blocks.ProcessDeposits(context.Background(), beaconState, b.Block.Body.Deposits)
assert.ErrorContains(t, want, err) assert.ErrorContains(t, want, err)
} }
@ -121,7 +121,7 @@ func TestProcessDeposits_AddsNewValidatorDeposit(t *testing.T) {
}, },
}) })
require.NoError(t, err) require.NoError(t, err)
newState, err := blocks.ProcessDeposits(context.Background(), beaconState, b) newState, err := blocks.ProcessDeposits(context.Background(), beaconState, b.Block.Body.Deposits)
require.NoError(t, err, "Expected block deposits to process correctly") require.NoError(t, err, "Expected block deposits to process correctly")
if newState.Balances()[1] != dep[0].Data.Amount { if newState.Balances()[1] != dep[0].Data.Amount {
t.Errorf( t.Errorf(
@ -183,7 +183,7 @@ func TestProcessDeposits_RepeatedDeposit_IncreasesValidatorBalance(t *testing.T)
}, },
}) })
require.NoError(t, err) require.NoError(t, err)
newState, err := blocks.ProcessDeposits(context.Background(), beaconState, b) newState, err := blocks.ProcessDeposits(context.Background(), beaconState, b.Block.Body.Deposits)
require.NoError(t, err, "Process deposit failed") require.NoError(t, err, "Process deposit failed")
assert.Equal(t, uint64(1000+50), newState.Balances()[1], "Expected balance at index 1 to be 1050") assert.Equal(t, uint64(1000+50), newState.Balances()[1], "Expected balance at index 1 to be 1050")
} }

View File

@ -1,11 +1,13 @@
package spectest package spectest
import ( import (
"context"
"path" "path"
"testing" "testing"
ethpb "github.com/prysmaticlabs/ethereumapis/eth/v1alpha1" ethpb "github.com/prysmaticlabs/ethereumapis/eth/v1alpha1"
"github.com/prysmaticlabs/prysm/beacon-chain/core/blocks" "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/params/spectest"
"github.com/prysmaticlabs/prysm/shared/testutil" "github.com/prysmaticlabs/prysm/shared/testutil"
"github.com/prysmaticlabs/prysm/shared/testutil/require" "github.com/prysmaticlabs/prysm/shared/testutil/require"
@ -24,7 +26,10 @@ func runDepositTest(t *testing.T, config string) {
require.NoError(t, deposit.UnmarshalSSZ(depositFile), "Failed to unmarshal") require.NoError(t, deposit.UnmarshalSSZ(depositFile), "Failed to unmarshal")
body := &ethpb.BeaconBlockBody{Deposits: []*ethpb.Deposit{deposit}} body := &ethpb.BeaconBlockBody{Deposits: []*ethpb.Deposit{deposit}}
testutil.RunBlockOperationTest(t, folderPath, body, blocks.ProcessDeposits) processDepositsFunc := func(ctx context.Context, s iface.BeaconState, b *ethpb.SignedBeaconBlock) (iface.BeaconState, error) {
return blocks.ProcessDeposits(ctx, s, b.Block.Body.Deposits)
}
testutil.RunBlockOperationTest(t, folderPath, body, processDepositsFunc)
}) })
} }
} }

View File

@ -29,6 +29,10 @@ import (
// processFunc is a function that processes a block with a given state. State is mutated. // processFunc is a function that processes a block with a given state. State is mutated.
type processFunc func(context.Context, iface.BeaconState, *ethpb.SignedBeaconBlock) (iface.BeaconState, error) type processFunc func(context.Context, iface.BeaconState, *ethpb.SignedBeaconBlock) (iface.BeaconState, error)
var processDepositsFunc = func(ctx context.Context, s iface.BeaconState, blk *ethpb.SignedBeaconBlock) (iface.BeaconState, error) {
return b.ProcessDeposits(ctx, s, blk.Block.Body.Deposits)
}
// This defines the processing block routine as outlined in eth2 spec: // This defines the processing block routine as outlined in eth2 spec:
// https://github.com/ethereum/eth2.0-specs/blob/dev/specs/phase0/beacon-chain.md#block-processing // https://github.com/ethereum/eth2.0-specs/blob/dev/specs/phase0/beacon-chain.md#block-processing
var processingPipeline = []processFunc{ var processingPipeline = []processFunc{
@ -39,7 +43,7 @@ var processingPipeline = []processFunc{
b.ProcessProposerSlashings, b.ProcessProposerSlashings,
b.ProcessAttesterSlashings, b.ProcessAttesterSlashings,
b.ProcessAttestations, b.ProcessAttestations,
b.ProcessDeposits, processDepositsFunc,
b.ProcessVoluntaryExits, b.ProcessVoluntaryExits,
} }
@ -543,7 +547,7 @@ func ProcessOperationsNoVerifyAttsSigs(
if err != nil { if err != nil {
return nil, errors.Wrap(err, "could not process block attestations") return nil, errors.Wrap(err, "could not process block attestations")
} }
state, err = b.ProcessDeposits(ctx, state, signedBeaconBlock) state, err = b.ProcessDeposits(ctx, state, signedBeaconBlock.Block.Body.Deposits)
if err != nil { if err != nil {
return nil, errors.Wrap(err, "could not process block validator deposits") return nil, errors.Wrap(err, "could not process block validator deposits")
} }