github.com/prysmaticlabs/prysm@v1.4.4/beacon-chain/p2p/types/types_test.go (about)

     1  package types
     2  
     3  import (
     4  	"encoding/hex"
     5  	"testing"
     6  
     7  	"github.com/prysmaticlabs/prysm/shared/params"
     8  	"github.com/prysmaticlabs/prysm/shared/testutil/assert"
     9  	"github.com/prysmaticlabs/prysm/shared/testutil/require"
    10  )
    11  
    12  func TestBeaconBlockByRootsReq_Limit(t *testing.T) {
    13  	fixedRoots := make([][32]byte, 0)
    14  	for i := uint64(0); i < params.BeaconNetworkConfig().MaxRequestBlocks+100; i++ {
    15  		fixedRoots = append(fixedRoots, [32]byte{byte(i)})
    16  	}
    17  	req := BeaconBlockByRootsReq(fixedRoots)
    18  
    19  	_, err := req.MarshalSSZ()
    20  	require.ErrorContains(t, "beacon block by roots request exceeds max size", err)
    21  
    22  	buf := make([]byte, 0)
    23  	for _, rt := range fixedRoots {
    24  		buf = append(buf, rt[:]...)
    25  	}
    26  	req2 := BeaconBlockByRootsReq(nil)
    27  	require.ErrorContains(t, "expected buffer with length of upto", req2.UnmarshalSSZ(buf))
    28  }
    29  
    30  func TestErrorResponse_Limit(t *testing.T) {
    31  	errorMessage := make([]byte, 0)
    32  	// Provide a message of size 6400 bytes.
    33  	for i := uint64(0); i < 200; i++ {
    34  		byteArr := [32]byte{byte(i)}
    35  		errorMessage = append(errorMessage, byteArr[:]...)
    36  	}
    37  	errMsg := ErrorMessage{}
    38  	require.ErrorContains(t, "expected buffer with length of upto", errMsg.UnmarshalSSZ(errorMessage))
    39  }
    40  
    41  func TestRoundTripSerialization(t *testing.T) {
    42  	roundTripTestBlocksByRootReq(t)
    43  	roundTripTestErrorMessage(t)
    44  }
    45  
    46  func roundTripTestBlocksByRootReq(t *testing.T) {
    47  	fixedRoots := make([][32]byte, 0)
    48  	for i := 0; i < 200; i++ {
    49  		fixedRoots = append(fixedRoots, [32]byte{byte(i)})
    50  	}
    51  	req := BeaconBlockByRootsReq(fixedRoots)
    52  
    53  	marshalledObj, err := req.MarshalSSZ()
    54  	require.NoError(t, err)
    55  	newVal := BeaconBlockByRootsReq(nil)
    56  
    57  	require.NoError(t, newVal.UnmarshalSSZ(marshalledObj))
    58  	assert.DeepEqual(t, [][32]byte(newVal), fixedRoots)
    59  }
    60  
    61  func roundTripTestErrorMessage(t *testing.T) {
    62  	errMsg := []byte{'e', 'r', 'r', 'o', 'r'}
    63  	sszErr := make(ErrorMessage, len(errMsg))
    64  	copy(sszErr, errMsg)
    65  
    66  	marshalledObj, err := sszErr.MarshalSSZ()
    67  	require.NoError(t, err)
    68  	newVal := ErrorMessage(nil)
    69  
    70  	require.NoError(t, newVal.UnmarshalSSZ(marshalledObj))
    71  	assert.DeepEqual(t, []byte(newVal), errMsg)
    72  }
    73  
    74  func TestSSZBytes_HashTreeRoot(t *testing.T) {
    75  	tests := []struct {
    76  		name        string
    77  		actualValue []byte
    78  		root        []byte
    79  		wantErr     bool
    80  	}{
    81  		{
    82  			name:        "random1",
    83  			actualValue: hexDecodeOrDie(t, "844e1063e0b396eed17be8eddb7eecd1fe3ea46542a4b72f7466e77325e5aa6d"),
    84  			root:        hexDecodeOrDie(t, "844e1063e0b396eed17be8eddb7eecd1fe3ea46542a4b72f7466e77325e5aa6d"),
    85  			wantErr:     false,
    86  		},
    87  		{
    88  			name:        "random1",
    89  			actualValue: hexDecodeOrDie(t, "7b16162ecd9a28fa80a475080b0e4fff4c27efe19ce5134ce3554b72274d59fd534400ba4c7f699aa1c307cd37c2b103"),
    90  			root:        hexDecodeOrDie(t, "128ed34ee798b9f00716f9ba5c000df5c99443dabc4d3f2e9bb86c77c732e007"),
    91  			wantErr:     false,
    92  		},
    93  		{
    94  			name:        "random2",
    95  			actualValue: []byte{},
    96  			root:        hexDecodeOrDie(t, "0000000000000000000000000000000000000000000000000000000000000000"),
    97  			wantErr:     false,
    98  		},
    99  	}
   100  	for _, tt := range tests {
   101  		t.Run(tt.name, func(t *testing.T) {
   102  			s := SSZBytes(tt.actualValue)
   103  			htr, err := s.HashTreeRoot()
   104  			require.NoError(t, err)
   105  			require.DeepEqual(t, tt.root, htr[:])
   106  		})
   107  	}
   108  }
   109  
   110  func hexDecodeOrDie(t *testing.T, str string) []byte {
   111  	decoded, err := hex.DecodeString(str)
   112  	require.NoError(t, err)
   113  	return decoded
   114  }