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

     1  package htrutils_test
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/prysmaticlabs/prysm/shared/hashutil"
     7  	"github.com/prysmaticlabs/prysm/shared/htrutils"
     8  	"github.com/prysmaticlabs/prysm/shared/testutil/assert"
     9  )
    10  
    11  func TestGetDepth(t *testing.T) {
    12  	trieSize := uint64(896745231)
    13  	expected := uint8(30)
    14  
    15  	result := htrutils.Depth(trieSize)
    16  	assert.Equal(t, expected, result)
    17  }
    18  
    19  func TestMerkleizeCountGreaterThanLimit(t *testing.T) {
    20  	hashFn := htrutils.NewHasherFunc(hashutil.CustomSHA256Hasher())
    21  	count := uint64(2)
    22  	limit := uint64(1)
    23  	chunks := [][]byte{{}}
    24  	leafIndexer := func(i uint64) []byte {
    25  		return chunks[i]
    26  	}
    27  	// Error if no panic
    28  	defer func() {
    29  		if r := recover(); r == nil {
    30  			t.Errorf("The code did not panic.")
    31  		}
    32  	}()
    33  	htrutils.Merkleize(hashFn, count, limit, leafIndexer)
    34  }
    35  
    36  func TestMerkleizeLimitAndCountAreZero(t *testing.T) {
    37  	hashFn := htrutils.NewHasherFunc(hashutil.CustomSHA256Hasher())
    38  	count := uint64(0)
    39  	limit := uint64(0)
    40  	chunks := [][]byte{{}}
    41  	leafIndexer := func(i uint64) []byte {
    42  		return chunks[i]
    43  	}
    44  	expected := [32]byte{}
    45  	result := htrutils.Merkleize(hashFn, count, limit, leafIndexer)
    46  	assert.Equal(t, expected, result)
    47  }
    48  
    49  func TestMerkleizeNormalPath(t *testing.T) {
    50  	hashFn := htrutils.NewHasherFunc(hashutil.CustomSHA256Hasher())
    51  	count := uint64(2)
    52  	limit := uint64(3)
    53  	chunks := [][]byte{{1, 2, 3, 4}, {5, 6, 7, 8}, {9, 10, 11, 12}}
    54  	leafIndexer := func(i uint64) []byte {
    55  		return chunks[i]
    56  	}
    57  	expected := [32]byte{95, 27, 253, 237, 215, 58, 147, 198, 175, 194, 180, 231, 154, 130, 205, 68, 146, 112, 225, 86, 6, 103, 186, 82, 7, 142, 33, 189, 174, 56, 199, 173}
    58  	result := htrutils.Merkleize(hashFn, count, limit, leafIndexer)
    59  	assert.Equal(t, expected, result)
    60  }
    61  
    62  func TestConstructProofCountGreaterThanLimit(t *testing.T) {
    63  	hashFn := htrutils.NewHasherFunc(hashutil.CustomSHA256Hasher())
    64  	count := uint64(2)
    65  	limit := uint64(1)
    66  	chunks := [][]byte{{}}
    67  	leafIndexer := func(i uint64) []byte {
    68  		return chunks[i]
    69  	}
    70  	index := uint64(0)
    71  	defer func() {
    72  		if r := recover(); r == nil {
    73  			t.Errorf("The code did not panic.")
    74  		}
    75  	}()
    76  	htrutils.ConstructProof(hashFn, count, limit, leafIndexer, index)
    77  }
    78  
    79  func TestConstructProofIndexGreaterThanEqualToLimit(t *testing.T) {
    80  	hashFn := htrutils.NewHasherFunc(hashutil.CustomSHA256Hasher())
    81  	count := uint64(1)
    82  	limit := uint64(1)
    83  	chunks := [][]byte{{}}
    84  	leafIndexer := func(i uint64) []byte {
    85  		return chunks[i]
    86  	}
    87  	index := uint64(1)
    88  	defer func() {
    89  		if r := recover(); r == nil {
    90  			t.Errorf("The code did not panic.")
    91  		}
    92  	}()
    93  	htrutils.ConstructProof(hashFn, count, limit, leafIndexer, index)
    94  }
    95  
    96  func TestConstructProofNormalPath(t *testing.T) {
    97  	hashFn := htrutils.NewHasherFunc(hashutil.CustomSHA256Hasher())
    98  	count := uint64(2)
    99  	limit := uint64(3)
   100  	chunks := [][]byte{{1, 2, 3, 4}, {5, 6, 7, 8}, {9, 10, 11, 12}}
   101  	leafIndexer := func(i uint64) []byte {
   102  		return chunks[i]
   103  	}
   104  	index := uint64(1)
   105  	expected := [][32]byte{
   106  		{1, 2, 3, 4, 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, 0},
   107  		{245, 165, 253, 66, 209, 106, 32, 48, 39, 152, 239, 110, 211, 9, 151, 155, 67, 0, 61, 35, 32, 217, 240, 232, 234, 152, 49, 169, 39, 89, 251, 75},
   108  	}
   109  	result := htrutils.ConstructProof(hashFn, count, limit, leafIndexer, index)
   110  	assert.Equal(t, len(expected), len(result))
   111  	for i, v := range expected {
   112  		assert.DeepEqual(t, result[i], v)
   113  	}
   114  }