From a7a3f71960af7f79a8ad9ba26079305e9e03fdce Mon Sep 17 00:00:00 2001 From: Bryan White Date: Tue, 16 Jul 2024 10:07:29 +0200 Subject: [PATCH] refactor: move applicable root methods to MerkleRoot and inherit in MerkleSumRoot --- root.go | 70 ++++++++++++++++++++++++++++---------------------------- types.go | 2 +- 2 files changed, 36 insertions(+), 36 deletions(-) diff --git a/root.go b/root.go index 618bbd9..29eb43d 100644 --- a/root.go +++ b/root.go @@ -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) @@ -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 } @@ -52,18 +29,41 @@ 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") } @@ -71,6 +71,13 @@ func (root MerkleSumRoot) validateBasic() error { 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) @@ -78,13 +85,6 @@ func (root MerkleSumRoot) sum() uint64 { 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 diff --git a/types.go b/types.go index fa2ca4c..641c96a 100644 --- a/types.go +++ b/types.go @@ -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 {