Skip to content

Commit

Permalink
refactor: move applicable root methods to MerkleRoot and inherit in M…
Browse files Browse the repository at this point in the history
…erkleSumRoot
  • Loading branch information
bryanchriswhite committed Jul 16, 2024
1 parent e499540 commit a7a3f71
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 36 deletions.
70 changes: 35 additions & 35 deletions root.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,33 +5,10 @@ import (
"fmt"
)

// MustSum returns the uint64 sum of the merkle root, it checks the length of the
// merkle root and if it is no the same as the size of the SMST's expected
// root hash it will panic.
func (root MerkleSumRoot) MustSum() uint64 {
sum, err := root.Sum()
if err != nil {
panic(err)
}

return sum
}

// Sum returns the uint64 sum of the merkle root, it checks the length of the
// merkle root and if it is no the same as the size of the SMST's expected
// root hash it will return an error.
func (root MerkleSumRoot) Sum() (uint64, error) {
if err := root.validateBasic(); err != nil {
return 0, err
}

return root.sum(), nil
}

// MustCount returns the uint64 count of the merkle root, a cryptographically secure
// count of the number of non-empty leafs in the tree. It panics if the root length
// is invalid.
func (root MerkleSumRoot) MustCount() uint64 {
func (root MerkleRoot) MustCount() uint64 {
count, err := root.Count()
if err != nil {
panic(err)
Expand All @@ -43,7 +20,7 @@ func (root MerkleSumRoot) MustCount() uint64 {
// Count returns the uint64 count of the merkle root, a cryptographically secure
// count of the number of non-empty leafs in the tree. It returns an error if the
// root length is invalid.
func (root MerkleSumRoot) Count() (uint64, error) {
func (root MerkleRoot) Count() (uint64, error) {
if err := root.validateBasic(); err != nil {
return 0, err
}
Expand All @@ -52,39 +29,62 @@ func (root MerkleSumRoot) Count() (uint64, error) {
}

// DigestSize returns the length of the digest portion of the root.
func (root MerkleSumRoot) DigestSize() int {
func (root MerkleRoot) DigestSize() int {
return len(root) - countSizeBytes - sumSizeBytes
}

// HasDigestSize returns true if the root digest size is the same as
// that of the size of the given hasher.
func (root MerkleSumRoot) HasDigestSize(size int) bool {
func (root MerkleRoot) HasDigestSize(size int) bool {
return root.DigestSize() == size
}

// MustSum returns the uint64 sum of the merkle root, it checks the length of the
// merkle root and if it is no the same as the size of the SMST's expected
// root hash it will panic.
func (root MerkleSumRoot) MustSum() uint64 {
sum, err := root.Sum()
if err != nil {
panic(err)
}

return sum
}

// Sum returns the uint64 sum of the merkle root, it checks the length of the
// merkle root and if it is no the same as the size of the SMST's expected
// root hash it will return an error.
func (root MerkleSumRoot) Sum() (uint64, error) {
if err := root.validateBasic(); err != nil {
return 0, err
}

return root.sum(), nil
}

// validateBasic returns an error if the root digest size is not a power of two.
func (root MerkleSumRoot) validateBasic() error {
func (root MerkleRoot) validateBasic() error {
if !isPowerOfTwo(root.DigestSize()) {
return fmt.Errorf("MerkleSumRoot#validateBasic: invalid root length")
}

return nil
}

// count returns the count of the node stored in the root.
func (root MerkleRoot) count() uint64 {
_, firstCountByteIdx := getFirstMetaByteIdx(root)

return binary.BigEndian.Uint64(root[firstCountByteIdx:])
}

// sum returns the sum of the node stored in the root.
func (root MerkleSumRoot) sum() uint64 {
firstSumByteIdx, firstCountByteIdx := getFirstMetaByteIdx(root)

return binary.BigEndian.Uint64(root[firstSumByteIdx:firstCountByteIdx])
}

// count returns the count of the node stored in the root.
func (root MerkleSumRoot) count() uint64 {
_, firstCountByteIdx := getFirstMetaByteIdx(root)

return binary.BigEndian.Uint64(root[firstCountByteIdx:])
}

// isPowerOfTwo function returns true if the input n is a power of 2
func isPowerOfTwo(n int) bool {
// A power of 2 has only one bit set in its binary representation
Expand Down
2 changes: 1 addition & 1 deletion types.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ var (
type MerkleRoot []byte

// MerkleSumRoot is a type alias for a byte slice returned from SparseMerkleSumTrie#Root().
type MerkleSumRoot []byte
type MerkleSumRoot = MerkleRoot

// A high-level interface that captures the behaviour of all types of nodes
type trieNode interface {
Expand Down

0 comments on commit a7a3f71

Please sign in to comment.