Skip to content

Commit ca7cd2f

Browse files
authored
refactor: small clenaups in helper funcs (#289)
Micro PR that I did before and forget to push. Use `min` and `max` builtin funcs instead of own implementations. <!-- This is an auto-generated comment: release notes by coderabbit.ai --> ## Summary by CodeRabbit These updates focus on internal code quality improvements that enhance maintainability and clarity without changing user-visible functionality: - **Refactor** - Streamlined internal logic by removing redundant comparison functions and adopting more concise coding patterns. - **Tests** - Updated test routines to reflect improved naming conventions and ensure consistency. - **Chores** - Simplified numerical operations and removed unnecessary type conversions for more robust code maintenance. <!-- end of auto-generated comment: release notes by coderabbit.ai -->
1 parent a336f12 commit ca7cd2f

File tree

6 files changed

+21
-32
lines changed

6 files changed

+21
-32
lines changed

hasher.go

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -313,20 +313,6 @@ func (n *NmtHasher) HashNode(left, right []byte) ([]byte, error) {
313313
return h.Sum(res), nil
314314
}
315315

316-
func max(ns []byte, ns2 []byte) []byte {
317-
if bytes.Compare(ns, ns2) >= 0 {
318-
return ns
319-
}
320-
return ns2
321-
}
322-
323-
func min(ns []byte, ns2 []byte) []byte {
324-
if bytes.Compare(ns, ns2) <= 0 {
325-
return ns
326-
}
327-
return ns2
328-
}
329-
330316
// computeNsRange computes the namespace range of the parent node based on the namespace ranges of its left and right children.
331317
func computeNsRange(leftMinNs, leftMaxNs, rightMinNs, rightMaxNs []byte, ignoreMaxNs bool, precomputedMaxNs namespace.ID) (minNs []byte, maxNs []byte) {
332318
minNs = leftMinNs

hasher_test.go

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -749,7 +749,7 @@ func TestMax(t *testing.T) {
749749

750750
for _, ts := range tt {
751751
t.Run(ts.name, func(t *testing.T) {
752-
maxResult := max(ts.ns, ts.ns2)
752+
maxResult := maxNs(ts.ns, ts.ns2)
753753
assert.Equal(t, ts.expected, maxResult)
754754
})
755755
}
@@ -784,7 +784,7 @@ func TestMin(t *testing.T) {
784784

785785
for _, ts := range tt {
786786
t.Run(ts.name, func(t *testing.T) {
787-
minResult := min(ts.ns, ts.ns2)
787+
minResult := minNs(ts.ns, ts.ns2)
788788
assert.Equal(t, ts.expected, minResult)
789789
})
790790
}
@@ -926,3 +926,17 @@ func TestEmptyRoot(t *testing.T) {
926926
assert.Equal(t, want, got)
927927
})
928928
}
929+
930+
func maxNs(ns []byte, ns2 []byte) []byte {
931+
if bytes.Compare(ns, ns2) >= 0 {
932+
return ns
933+
}
934+
return ns2
935+
}
936+
937+
func minNs(ns []byte, ns2 []byte) []byte {
938+
if bytes.Compare(ns, ns2) <= 0 {
939+
return ns
940+
}
941+
return ns2
942+
}

nmt_test.go

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -878,9 +878,7 @@ func exampleNMT2(nidSize int, ignoreMaxNamespace bool, leavesNIDs ...byte) *Name
878878
}
879879

880880
func swap(slice [][]byte, i int, j int) {
881-
temp := slice[i]
882-
slice[i] = slice[j]
883-
slice[j] = temp
881+
slice[i], slice[j] = slice[j], slice[i]
884882
}
885883

886884
// Test_buildRangeProof_Err tests that buildRangeProof returns an error when the underlying tree has an invalid state e.g., leaves are not ordered by namespace ID or a leaf hash is corrupted.

proof.go

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -632,7 +632,7 @@ func ToLeafRanges(proofStart, proofEnd, subtreeWidth int) ([]LeafRange, error) {
632632
// Note: This method is Celestia specific.
633633
func nextLeafRange(currentStart, currentEnd, subtreeWidth int) (LeafRange, error) {
634634
currentLeafRange := currentEnd - currentStart
635-
minimum := minInt(currentLeafRange, subtreeWidth)
635+
minimum := min(currentLeafRange, subtreeWidth)
636636
uMinimum, err := safeIntToUint(minimum)
637637
if err != nil {
638638
return LeafRange{}, fmt.Errorf("failed to convert subtree root range to Uint %w", err)
@@ -713,10 +713,3 @@ func safeIntToUint(val int) (uint, error) {
713713
}
714714
return uint(val), nil
715715
}
716-
717-
func minInt(val1, val2 int) int {
718-
if val1 > val2 {
719-
return val2
720-
}
721-
return val1
722-
}

proof_test.go

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -514,9 +514,7 @@ func TestVerifyNamespace_False(t *testing.T) {
514514
require.True(t, absenceProof9_2.IsOfAbsence())
515515

516516
// swap leafHashes of the absence proofs
517-
buffer := absenceProof9_2.leafHash
518-
absenceProof9_2.leafHash = absenceProof9_1.leafHash
519-
absenceProof9_1.leafHash = buffer
517+
absenceProof9_2.leafHash, absenceProof9_1.leafHash = absenceProof9_1.leafHash, absenceProof9_2.leafHash
520518

521519
hasher := sha256.New()
522520

@@ -1489,7 +1487,7 @@ func TestMinInt(t *testing.T) {
14891487

14901488
for _, tt := range tests {
14911489
t.Run(fmt.Sprintf("val1=%d, val2=%d", tt.val1, tt.val2), func(t *testing.T) {
1492-
result := minInt(tt.val1, tt.val2)
1490+
result := min(tt.val1, tt.val2)
14931491
if result != tt.expected {
14941492
t.Errorf("expected %d, got %d", tt.expected, result)
14951493
}

subrootpaths.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ var (
1616
// this is just a quick function to return that representation as a list of ints
1717
func subdivide(idxStart uint, width uint) []int {
1818
var path []int
19-
pathlen := int(bits.Len(width) - 1)
19+
pathlen := bits.Len(width) - 1
2020
for i := pathlen - 1; i >= 0; i-- {
2121
if (idxStart & (1 << i)) == 0 {
2222
path = append(path, 0)

0 commit comments

Comments
 (0)