github.com/prysmaticlabs/prysm@v1.4.4/shared/htrutils/htrutils_test.go (about)

     1  package htrutils_test
     2  
     3  import (
     4  	"testing"
     5  
     6  	pb "github.com/prysmaticlabs/prysm/proto/beacon/p2p/v1"
     7  	ethpb "github.com/prysmaticlabs/prysm/proto/eth/v1alpha1"
     8  	"github.com/prysmaticlabs/prysm/shared/hashutil"
     9  	"github.com/prysmaticlabs/prysm/shared/htrutils"
    10  	"github.com/prysmaticlabs/prysm/shared/testutil/assert"
    11  	"github.com/prysmaticlabs/prysm/shared/testutil/require"
    12  )
    13  
    14  func TestUint64Root(t *testing.T) {
    15  	uintVal := uint64(1234567890)
    16  	expected := [32]byte{210, 2, 150, 73, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}
    17  
    18  	result := htrutils.Uint64Root(uintVal)
    19  	assert.Equal(t, expected, result)
    20  }
    21  
    22  func TestForkRoot(t *testing.T) {
    23  	testFork := pb.Fork{
    24  		PreviousVersion: []byte{123},
    25  		CurrentVersion:  []byte{124},
    26  		Epoch:           1234567890,
    27  	}
    28  	expected := [32]byte{19, 46, 77, 103, 92, 175, 247, 33, 100, 64, 17, 111, 199, 145, 69, 38, 217, 112, 6, 16, 149, 201, 225, 144, 192, 228, 197, 172, 157, 78, 114, 140}
    29  
    30  	result, err := htrutils.ForkRoot(&testFork)
    31  	require.NoError(t, err)
    32  	assert.Equal(t, expected, result)
    33  }
    34  
    35  func TestCheckPointRoot(t *testing.T) {
    36  	testHasher := hashutil.CustomSHA256Hasher()
    37  	testCheckpoint := ethpb.Checkpoint{
    38  		Epoch: 1234567890,
    39  		Root:  []byte{222},
    40  	}
    41  	expected := [32]byte{228, 65, 39, 109, 183, 249, 167, 232, 125, 239, 25, 155, 207, 4, 84, 174, 176, 229, 175, 224, 62, 33, 215, 254, 170, 220, 132, 65, 246, 128, 68, 194}
    42  
    43  	result, err := htrutils.CheckpointRoot(testHasher, &testCheckpoint)
    44  	require.NoError(t, err)
    45  	assert.Equal(t, expected, result)
    46  }
    47  
    48  func TestHistoricalRootsRoot(t *testing.T) {
    49  	testHistoricalRoots := [][]byte{{123}, {234}}
    50  	expected := [32]byte{70, 204, 150, 196, 89, 138, 190, 205, 65, 207, 120, 166, 179, 247, 147, 20, 29, 133, 117, 116, 151, 234, 129, 32, 22, 15, 79, 178, 98, 73, 132, 152}
    51  
    52  	result, err := htrutils.HistoricalRootsRoot(testHistoricalRoots)
    53  	require.NoError(t, err)
    54  	assert.Equal(t, expected, result)
    55  }
    56  
    57  func TestSlashingsRoot(t *testing.T) {
    58  	testSlashingsRoot := []uint64{123, 234}
    59  	expected := [32]byte{123, 0, 0, 0, 0, 0, 0, 0, 234, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}
    60  
    61  	result, err := htrutils.SlashingsRoot(testSlashingsRoot)
    62  	require.NoError(t, err)
    63  	assert.Equal(t, expected, result)
    64  }