github.com/prysmaticlabs/prysm@v1.4.4/shared/blockutil/block_utils_test.go (about)

     1  package blockutil
     2  
     3  import (
     4  	"testing"
     5  
     6  	eth "github.com/prysmaticlabs/prysm/proto/eth/v1alpha1"
     7  	"github.com/prysmaticlabs/prysm/shared/bytesutil"
     8  	"github.com/prysmaticlabs/prysm/shared/params"
     9  	"github.com/prysmaticlabs/prysm/shared/testutil/assert"
    10  	"github.com/prysmaticlabs/prysm/shared/testutil/require"
    11  )
    12  
    13  func TestBeaconBlockHeaderFromBlock(t *testing.T) {
    14  	hashLen := 32
    15  	blk := &eth.BeaconBlock{
    16  		Slot:          200,
    17  		ProposerIndex: 2,
    18  		ParentRoot:    bytesutil.PadTo([]byte("parent root"), hashLen),
    19  		StateRoot:     bytesutil.PadTo([]byte("state root"), hashLen),
    20  		Body: &eth.BeaconBlockBody{
    21  			Eth1Data: &eth.Eth1Data{
    22  				BlockHash:    bytesutil.PadTo([]byte("block hash"), hashLen),
    23  				DepositRoot:  bytesutil.PadTo([]byte("deposit root"), hashLen),
    24  				DepositCount: 1,
    25  			},
    26  			RandaoReveal:      bytesutil.PadTo([]byte("randao"), params.BeaconConfig().BLSSignatureLength),
    27  			Graffiti:          bytesutil.PadTo([]byte("teehee"), hashLen),
    28  			ProposerSlashings: []*eth.ProposerSlashing{},
    29  			AttesterSlashings: []*eth.AttesterSlashing{},
    30  			Attestations:      []*eth.Attestation{},
    31  			Deposits:          []*eth.Deposit{},
    32  			VoluntaryExits:    []*eth.SignedVoluntaryExit{},
    33  		},
    34  	}
    35  	bodyRoot, err := blk.Body.HashTreeRoot()
    36  	require.NoError(t, err)
    37  	want := &eth.BeaconBlockHeader{
    38  		Slot:          blk.Slot,
    39  		ProposerIndex: blk.ProposerIndex,
    40  		ParentRoot:    blk.ParentRoot,
    41  		StateRoot:     blk.StateRoot,
    42  		BodyRoot:      bodyRoot[:],
    43  	}
    44  
    45  	bh, err := BeaconBlockHeaderFromBlock(blk)
    46  	require.NoError(t, err)
    47  	assert.DeepEqual(t, want, bh)
    48  }
    49  
    50  func TestBeaconBlockHeaderFromBlock_NilBlockBody(t *testing.T) {
    51  	hashLen := 32
    52  	blk := &eth.BeaconBlock{
    53  		Slot:          200,
    54  		ProposerIndex: 2,
    55  		ParentRoot:    bytesutil.PadTo([]byte("parent root"), hashLen),
    56  		StateRoot:     bytesutil.PadTo([]byte("state root"), hashLen),
    57  	}
    58  	_, err := BeaconBlockHeaderFromBlock(blk)
    59  	require.ErrorContains(t, "nil block body", err)
    60  }
    61  
    62  func TestSignedBeaconBlockHeaderFromBlock(t *testing.T) {
    63  	hashLen := 32
    64  	blk := &eth.SignedBeaconBlock{Block: &eth.BeaconBlock{
    65  		Slot:          200,
    66  		ProposerIndex: 2,
    67  		ParentRoot:    bytesutil.PadTo([]byte("parent root"), hashLen),
    68  		StateRoot:     bytesutil.PadTo([]byte("state root"), hashLen),
    69  		Body: &eth.BeaconBlockBody{
    70  			Eth1Data: &eth.Eth1Data{
    71  				BlockHash:    bytesutil.PadTo([]byte("block hash"), hashLen),
    72  				DepositRoot:  bytesutil.PadTo([]byte("deposit root"), hashLen),
    73  				DepositCount: 1,
    74  			},
    75  			RandaoReveal:      bytesutil.PadTo([]byte("randao"), params.BeaconConfig().BLSSignatureLength),
    76  			Graffiti:          bytesutil.PadTo([]byte("teehee"), hashLen),
    77  			ProposerSlashings: []*eth.ProposerSlashing{},
    78  			AttesterSlashings: []*eth.AttesterSlashing{},
    79  			Attestations:      []*eth.Attestation{},
    80  			Deposits:          []*eth.Deposit{},
    81  			VoluntaryExits:    []*eth.SignedVoluntaryExit{},
    82  		},
    83  	},
    84  		Signature: bytesutil.PadTo([]byte("signature"), params.BeaconConfig().BLSSignatureLength),
    85  	}
    86  	bodyRoot, err := blk.Block.Body.HashTreeRoot()
    87  	require.NoError(t, err)
    88  	want := &eth.SignedBeaconBlockHeader{Header: &eth.BeaconBlockHeader{
    89  		Slot:          blk.Block.Slot,
    90  		ProposerIndex: blk.Block.ProposerIndex,
    91  		ParentRoot:    blk.Block.ParentRoot,
    92  		StateRoot:     blk.Block.StateRoot,
    93  		BodyRoot:      bodyRoot[:],
    94  	},
    95  		Signature: blk.Signature,
    96  	}
    97  
    98  	bh, err := SignedBeaconBlockHeaderFromBlock(blk)
    99  	require.NoError(t, err)
   100  	assert.DeepEqual(t, want, bh)
   101  }
   102  
   103  func TestSignedBeaconBlockHeaderFromBlock_NilBlockBody(t *testing.T) {
   104  	hashLen := 32
   105  	blk := &eth.SignedBeaconBlock{Block: &eth.BeaconBlock{
   106  		Slot:          200,
   107  		ProposerIndex: 2,
   108  		ParentRoot:    bytesutil.PadTo([]byte("parent root"), hashLen),
   109  		StateRoot:     bytesutil.PadTo([]byte("state root"), hashLen),
   110  	},
   111  		Signature: bytesutil.PadTo([]byte("signature"), params.BeaconConfig().BLSSignatureLength),
   112  	}
   113  	_, err := SignedBeaconBlockHeaderFromBlock(blk)
   114  	require.ErrorContains(t, "nil block", err)
   115  }