From 6cb4212bf45d63850ee72a1426a3eca88cd8f130 Mon Sep 17 00:00:00 2001 From: francis <28549164+FrancisLennon17@users.noreply.github.com> Date: Fri, 17 Jan 2025 10:05:51 +0000 Subject: [PATCH] fix: review comments --- hasher.go | 32 ++++++++++++++++++-------------- nmt_test.go | 2 +- 2 files changed, 19 insertions(+), 15 deletions(-) diff --git a/hasher.go b/hasher.go index fd8e05db..f0d174ff 100644 --- a/hasher.go +++ b/hasher.go @@ -154,9 +154,8 @@ func (n *NmtHasher) BlockSize() int { func (n *NmtHasher) EmptyRoot() []byte { n.baseHasher.Reset() - emptyNs := bytes.Repeat([]byte{0}, int(n.NamespaceLen)*2) h := n.baseHasher.Sum(nil) - digest := append(emptyNs, h...) + digest := append(make([]byte, int(n.NamespaceLen)*2), h...) return digest } @@ -212,6 +211,7 @@ func (n *NmtHasher) MustHashLeaf(ndata []byte) []byte { return res } +// nsIDRange represents the range of namespace IDs with minimum and maximum values. type nsIDRange struct { Min, Max namespace.ID } @@ -235,10 +235,10 @@ func (n *NmtHasher) tryFetchNodeNSRange(node []byte) (nsIDRange, error) { } // ValidateNodeFormat checks whether the supplied node conforms to the -// namespaced hash format and returns ErrInvalidNodeLen if not. -func (n *NmtHasher) ValidateNodeFormat(node []byte) (err error) { - _, err = n.tryFetchNodeNSRange(node) - return +// namespaced hash format and returns an error if not. +func (n *NmtHasher) ValidateNodeFormat(node []byte) error { + _, err := n.tryFetchNodeNSRange(node) + return err } // tryFetchLeftAndRightNSRange attempts to return the min/max namespace ids of both @@ -246,25 +246,29 @@ func (n *NmtHasher) ValidateNodeFormat(node []byte) (err error) { // and right comply by the namespace hash format, and are correctly ordered // according to their namespace IDs. func (n *NmtHasher) tryFetchLeftAndRightNSRanges(left, right []byte) ( - lNsRange nsIDRange, - rNsRange nsIDRange, - err error, + nsIDRange, + nsIDRange, + error, ) { + var lNsRange nsIDRange + var rNsRange nsIDRange + var err error + lNsRange, err = n.tryFetchNodeNSRange(left) if err != nil { - return + return lNsRange, rNsRange, err } rNsRange, err = n.tryFetchNodeNSRange(right) if err != nil { - return + return lNsRange, rNsRange, err } // check the namespace range of the left and right children if rNsRange.Min.Less(lNsRange.Max) { - err = fmt.Errorf("%w: the maximum namespace of the left child %x is greater than the min namespace of the right child %x", ErrUnorderedSiblings, lNsRange.Max, rNsRange.Min) - return + err = fmt.Errorf("%w: the min namespace ID of the right child %d is less than the max namespace ID of the left child %d", ErrUnorderedSiblings, rNsRange.Min, lNsRange.Max) } - return + + return lNsRange, rNsRange, err } // ValidateNodes is a helper function to verify the diff --git a/nmt_test.go b/nmt_test.go index 9728ff5d..1ffdccff 100644 --- a/nmt_test.go +++ b/nmt_test.go @@ -704,7 +704,7 @@ func BenchmarkComputeRoot(b *testing.B) { {"64-leaves", 64, 8, 256}, {"128-leaves", 128, 8, 256}, {"256-leaves", 256, 8, 256}, - //{"20k-leaves", 20000, 8, 512}, + {"20k-leaves", 20000, 8, 512}, } for _, tt := range tests {