2018-12-27 06:48:09 +00:00
|
|
|
package hashutil
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestMerkleRoot(t *testing.T) {
|
|
|
|
valueSet := [][]byte{
|
2019-01-09 09:49:17 +00:00
|
|
|
{'a'},
|
|
|
|
{'b'},
|
|
|
|
{'c'},
|
|
|
|
{'d'},
|
2018-12-27 06:48:09 +00:00
|
|
|
}
|
|
|
|
|
2018-12-30 03:03:36 +00:00
|
|
|
hashedV1 := Hash([]byte{'a'})
|
|
|
|
hashedV2 := Hash([]byte{'b'})
|
|
|
|
hashedV3 := Hash([]byte{'c'})
|
|
|
|
hashedV4 := Hash([]byte{'d'})
|
|
|
|
|
|
|
|
leftNode := Hash(append(hashedV1[:], hashedV2[:]...))
|
|
|
|
rightNode := Hash(append(hashedV3[:], hashedV4[:]...))
|
2018-12-27 06:48:09 +00:00
|
|
|
expectedRoot := Hash(append(leftNode[:], rightNode[:]...))
|
|
|
|
|
|
|
|
if !bytes.Equal(expectedRoot[:], MerkleRoot(valueSet)) {
|
|
|
|
t.Errorf("Expected Merkle root and computed merkle root are not equal %#x , %#x", expectedRoot, MerkleRoot(valueSet))
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|