github.com/prysmaticlabs/prysm@v1.4.4/shared/testutil/block_test.go (about)

     1  package testutil
     2  
     3  import (
     4  	"context"
     5  	"testing"
     6  
     7  	types "github.com/prysmaticlabs/eth2-types"
     8  	"github.com/prysmaticlabs/prysm/beacon-chain/core/helpers"
     9  	"github.com/prysmaticlabs/prysm/beacon-chain/core/state"
    10  	"github.com/prysmaticlabs/prysm/beacon-chain/core/state/stateutils"
    11  	v1 "github.com/prysmaticlabs/prysm/proto/eth/v1"
    12  	eth "github.com/prysmaticlabs/prysm/proto/eth/v1alpha1"
    13  	"github.com/prysmaticlabs/prysm/proto/eth/v1alpha1/wrapper"
    14  	"github.com/prysmaticlabs/prysm/shared/bytesutil"
    15  	"github.com/prysmaticlabs/prysm/shared/params"
    16  	"github.com/prysmaticlabs/prysm/shared/testutil/require"
    17  )
    18  
    19  func TestGenerateFullBlock_PassesStateTransition(t *testing.T) {
    20  	beaconState, privs := DeterministicGenesisState(t, 128)
    21  	conf := &BlockGenConfig{
    22  		NumAttestations: 1,
    23  	}
    24  	block, err := GenerateFullBlock(beaconState, privs, conf, beaconState.Slot())
    25  	require.NoError(t, err)
    26  	_, err = state.ExecuteStateTransition(context.Background(), beaconState, wrapper.WrappedPhase0SignedBeaconBlock(block))
    27  	require.NoError(t, err)
    28  }
    29  
    30  func TestGenerateFullBlock_ThousandValidators(t *testing.T) {
    31  	params.SetupTestConfigCleanup(t)
    32  	params.OverrideBeaconConfig(params.MinimalSpecConfig())
    33  	beaconState, privs := DeterministicGenesisState(t, 1024)
    34  	conf := &BlockGenConfig{
    35  		NumAttestations: 4,
    36  	}
    37  	block, err := GenerateFullBlock(beaconState, privs, conf, beaconState.Slot())
    38  	require.NoError(t, err)
    39  	_, err = state.ExecuteStateTransition(context.Background(), beaconState, wrapper.WrappedPhase0SignedBeaconBlock(block))
    40  	require.NoError(t, err)
    41  }
    42  
    43  func TestGenerateFullBlock_Passes4Epochs(t *testing.T) {
    44  	// Changing to minimal config as this will process 4 epochs of blocks.
    45  	params.SetupTestConfigCleanup(t)
    46  	params.OverrideBeaconConfig(params.MinimalSpecConfig())
    47  	beaconState, privs := DeterministicGenesisState(t, 64)
    48  
    49  	conf := &BlockGenConfig{
    50  		NumAttestations: 2,
    51  	}
    52  	finalSlot := params.BeaconConfig().SlotsPerEpoch*4 + 3
    53  	for i := 0; i < int(finalSlot); i++ {
    54  		helpers.ClearCache()
    55  		block, err := GenerateFullBlock(beaconState, privs, conf, beaconState.Slot())
    56  		require.NoError(t, err)
    57  		beaconState, err = state.ExecuteStateTransition(context.Background(), beaconState, wrapper.WrappedPhase0SignedBeaconBlock(block))
    58  		require.NoError(t, err)
    59  	}
    60  
    61  	// Blocks are one slot ahead of beacon state.
    62  	if finalSlot != beaconState.Slot() {
    63  		t.Fatalf("expected output slot to be %d, received %d", finalSlot, beaconState.Slot())
    64  	}
    65  	if beaconState.CurrentJustifiedCheckpoint().Epoch != 3 {
    66  		t.Fatalf("expected justified epoch to change to 3, received %d", beaconState.CurrentJustifiedCheckpoint().Epoch)
    67  	}
    68  	if beaconState.FinalizedCheckpointEpoch() != 2 {
    69  		t.Fatalf("expected finalized epoch to change to 2, received %d", beaconState.CurrentJustifiedCheckpoint().Epoch)
    70  	}
    71  }
    72  
    73  func TestGenerateFullBlock_ValidProposerSlashings(t *testing.T) {
    74  	params.SetupTestConfigCleanup(t)
    75  	params.OverrideBeaconConfig(params.MinimalSpecConfig())
    76  	beaconState, privs := DeterministicGenesisState(t, 32)
    77  	conf := &BlockGenConfig{
    78  		NumProposerSlashings: 1,
    79  	}
    80  	block, err := GenerateFullBlock(beaconState, privs, conf, beaconState.Slot()+1)
    81  	require.NoError(t, err)
    82  	beaconState, err = state.ExecuteStateTransition(context.Background(), beaconState, wrapper.WrappedPhase0SignedBeaconBlock(block))
    83  	require.NoError(t, err)
    84  
    85  	slashableIndice := block.Block.Body.ProposerSlashings[0].Header_1.Header.ProposerIndex
    86  	if val, err := beaconState.ValidatorAtIndexReadOnly(slashableIndice); err != nil || !val.Slashed() {
    87  		require.NoError(t, err)
    88  		t.Fatal("expected validator to be slashed")
    89  	}
    90  }
    91  
    92  func TestGenerateFullBlock_ValidAttesterSlashings(t *testing.T) {
    93  	params.SetupTestConfigCleanup(t)
    94  	params.OverrideBeaconConfig(params.MinimalSpecConfig())
    95  	beaconState, privs := DeterministicGenesisState(t, 32)
    96  	conf := &BlockGenConfig{
    97  		NumAttesterSlashings: 1,
    98  	}
    99  	block, err := GenerateFullBlock(beaconState, privs, conf, beaconState.Slot())
   100  	require.NoError(t, err)
   101  	beaconState, err = state.ExecuteStateTransition(context.Background(), beaconState, wrapper.WrappedPhase0SignedBeaconBlock(block))
   102  	require.NoError(t, err)
   103  
   104  	slashableIndices := block.Block.Body.AttesterSlashings[0].Attestation_1.AttestingIndices
   105  	if val, err := beaconState.ValidatorAtIndexReadOnly(types.ValidatorIndex(slashableIndices[0])); err != nil || !val.Slashed() {
   106  		require.NoError(t, err)
   107  		t.Fatal("expected validator to be slashed")
   108  	}
   109  }
   110  
   111  func TestGenerateFullBlock_ValidAttestations(t *testing.T) {
   112  	params.SetupTestConfigCleanup(t)
   113  	params.OverrideBeaconConfig(params.MinimalSpecConfig())
   114  
   115  	beaconState, privs := DeterministicGenesisState(t, 256)
   116  	conf := &BlockGenConfig{
   117  		NumAttestations: 4,
   118  	}
   119  	block, err := GenerateFullBlock(beaconState, privs, conf, beaconState.Slot())
   120  	require.NoError(t, err)
   121  	beaconState, err = state.ExecuteStateTransition(context.Background(), beaconState, wrapper.WrappedPhase0SignedBeaconBlock(block))
   122  	require.NoError(t, err)
   123  	atts, err := beaconState.CurrentEpochAttestations()
   124  	require.NoError(t, err)
   125  	if len(atts) != 4 {
   126  		t.Fatal("expected 4 attestations to be saved to the beacon state")
   127  	}
   128  }
   129  
   130  func TestGenerateFullBlock_ValidDeposits(t *testing.T) {
   131  	beaconState, privs := DeterministicGenesisState(t, 256)
   132  	deposits, _, err := DeterministicDepositsAndKeys(257)
   133  	require.NoError(t, err)
   134  	eth1Data, err := DeterministicEth1Data(len(deposits))
   135  	require.NoError(t, err)
   136  	require.NoError(t, beaconState.SetEth1Data(eth1Data))
   137  	conf := &BlockGenConfig{
   138  		NumDeposits: 1,
   139  	}
   140  	block, err := GenerateFullBlock(beaconState, privs, conf, beaconState.Slot())
   141  	require.NoError(t, err)
   142  	beaconState, err = state.ExecuteStateTransition(context.Background(), beaconState, wrapper.WrappedPhase0SignedBeaconBlock(block))
   143  	require.NoError(t, err)
   144  
   145  	depositedPubkey := block.Block.Body.Deposits[0].Data.PublicKey
   146  	valIndexMap := stateutils.ValidatorIndexMap(beaconState.Validators())
   147  	index := valIndexMap[bytesutil.ToBytes48(depositedPubkey)]
   148  	val, err := beaconState.ValidatorAtIndexReadOnly(index)
   149  	require.NoError(t, err)
   150  	if val.EffectiveBalance() != params.BeaconConfig().MaxEffectiveBalance {
   151  		t.Fatalf(
   152  			"expected validator balance to be max effective balance, received %d",
   153  			val.EffectiveBalance(),
   154  		)
   155  	}
   156  }
   157  
   158  func TestGenerateFullBlock_ValidVoluntaryExits(t *testing.T) {
   159  	beaconState, privs := DeterministicGenesisState(t, 256)
   160  	// Moving the state 2048 epochs forward due to PERSISTENT_COMMITTEE_PERIOD.
   161  	err := beaconState.SetSlot(params.BeaconConfig().SlotsPerEpoch.Mul(uint64(params.BeaconConfig().ShardCommitteePeriod)).Add(3))
   162  	require.NoError(t, err)
   163  	conf := &BlockGenConfig{
   164  		NumVoluntaryExits: 1,
   165  	}
   166  	block, err := GenerateFullBlock(beaconState, privs, conf, beaconState.Slot())
   167  	require.NoError(t, err)
   168  	beaconState, err = state.ExecuteStateTransition(context.Background(), beaconState, wrapper.WrappedPhase0SignedBeaconBlock(block))
   169  	require.NoError(t, err)
   170  
   171  	exitedIndex := block.Block.Body.VoluntaryExits[0].Exit.ValidatorIndex
   172  
   173  	val, err := beaconState.ValidatorAtIndexReadOnly(exitedIndex)
   174  	require.NoError(t, err)
   175  	if val.ExitEpoch() == params.BeaconConfig().FarFutureEpoch {
   176  		t.Fatal("expected exiting validator index to be marked as exiting")
   177  	}
   178  }
   179  
   180  func TestHydrateSignedBeaconBlock_NoError(t *testing.T) {
   181  	b := &eth.SignedBeaconBlock{}
   182  	b = HydrateSignedBeaconBlock(b)
   183  	_, err := b.HashTreeRoot()
   184  	require.NoError(t, err)
   185  	_, err = b.Block.HashTreeRoot()
   186  	require.NoError(t, err)
   187  	_, err = b.Block.Body.HashTreeRoot()
   188  	require.NoError(t, err)
   189  }
   190  
   191  func TestHydrateV1SignedBeaconBlock_NoError(t *testing.T) {
   192  	b := &v1.SignedBeaconBlock{}
   193  	b = HydrateV1SignedBeaconBlock(b)
   194  	_, err := b.HashTreeRoot()
   195  	require.NoError(t, err)
   196  	_, err = b.Block.HashTreeRoot()
   197  	require.NoError(t, err)
   198  	_, err = b.Block.Body.HashTreeRoot()
   199  	require.NoError(t, err)
   200  }