2019-10-04 15:46:49 -07:00
|
|
|
package hashutil_test
|
2018-12-27 14:48:09 +08:00
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"testing"
|
2019-10-04 15:46:49 -07:00
|
|
|
|
|
|
|
"github.com/prysmaticlabs/prysm/shared/hashutil"
|
2018-12-27 14:48:09 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestMerkleRoot(t *testing.T) {
|
|
|
|
valueSet := [][]byte{
|
2019-01-09 03:49:17 -06:00
|
|
|
{'a'},
|
|
|
|
{'b'},
|
|
|
|
{'c'},
|
|
|
|
{'d'},
|
2018-12-27 14:48:09 +08:00
|
|
|
}
|
|
|
|
|
2019-02-20 11:21:35 -07:00
|
|
|
hashedV1 := []byte{'a'}
|
|
|
|
hashedV2 := []byte{'b'}
|
|
|
|
hashedV3 := []byte{'c'}
|
|
|
|
hashedV4 := []byte{'d'}
|
2018-12-30 11:03:36 +08:00
|
|
|
|
2019-10-04 15:46:49 -07:00
|
|
|
leftNode := hashutil.Hash(append(hashedV1[:], hashedV2[:]...))
|
|
|
|
rightNode := hashutil.Hash(append(hashedV3[:], hashedV4[:]...))
|
|
|
|
expectedRoot := hashutil.Hash(append(leftNode[:], rightNode[:]...))
|
2018-12-27 14:48:09 +08:00
|
|
|
|
2019-10-04 15:46:49 -07:00
|
|
|
if !bytes.Equal(expectedRoot[:], hashutil.MerkleRoot(valueSet)) {
|
|
|
|
t.Errorf("Expected Merkle root and computed merkle root are not equal %#x , %#x", expectedRoot, hashutil.MerkleRoot(valueSet))
|
2018-12-27 14:48:09 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|