Repository: celestiaorg/nmt
Pull request #289 (2025-02-26 10:12:08)

refactor: small clenaups in helper funcs
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 -->

coderabbitai[bot]: <!-- This is an auto-generated comment: summarize by coderabbit.ai -->
<!-- This is an auto-generated comment: review in progress by coderabbit.ai -->

> [!NOTE]
> Currently processing new changes in this PR. This may take a few minutes, please wait...
> 
> <details>
> <summary>📥 Commits</summary>
> 
> Reviewing files that changed from the base of the PR and between a336f1288db6f74920edf8c2cbad335cd55df3a9 and 3b6dbf552cb2dab0cec77bb4e4f7f81c0e93217b.
> 
> </details>
> 
> <details>
> <summary>📒 Files selected for processing (6)</summary>
> 
> * `hasher.go` (0 hunks)
> * `hasher_test.go` (3 hunks)
> * `nmt_test.go` (1 hunks)
> * `proof.go` (1 hunks)
> * `proof_test.go` (2 hunks)
> * `subrootpaths.go` (1 hunks)
> 
> </details>
> 
> ```ascii
>  ___________________________________________________________________
> < Find some code, review it up, all day long you'll have good luck! >
>  -------------------------------------------------------------------
>   \
>    \   \
>         \ /\
>         ( )
>       .( o ).
> ```

<!-- end of auto-generated comment: review in progress by coderabbit.ai -->
<!-- usage_tips_start -->

> [!TIP]
> <details>
> <summary>CodeRabbit can generate a title for your PR based on the changes with custom instructions.</summary>
> 
> Add the `reviews.auto_title_instructions` setting in your project's settings in CodeRabbit to generate a title for your PR based on the changes in the PR with custom instructions.
> 
> </details>

<!-- usage_tips_end -->
<!-- tips_start -->

---

Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

<details>
<summary>❤️ Share</summary>

- [X](https://twitter.com/intent/tweet?text=I%20just%20used%20%40coderabbitai%20for%20my%20code%20review%2C%20and%20it%27s%20fantastic%21%20It%27s%20free%20for%20OSS%20and%20offers%20a%20free%20trial%20for%20the%20proprietary%20code.%20Check%20it%20out%3A&url=https%3A//coderabbit.ai)
- [Mastodon](https://mastodon.social/share?text=I%20just%20used%20%40coderabbitai%20for%20my%20code%20review%2C%20and%20it%27s%20fantastic%21%20It%27s%20free%20for%20OSS%20and%20offers%20a%20free%20trial%20for%20the%20proprietary%20code.%20Check%20it%20out%3A%20https%3A%2F%2Fcoderabbit.ai)
- [Reddit](https://www.reddit.com/submit?title=Great%20tool%20for%20code%20review%20-%20CodeRabbit&text=I%20just%20used%20CodeRabbit%20for%20my%20code%20review%2C%20and%20it%27s%20fantastic%21%20It%27s%20free%20for%20OSS%20and%20offers%20a%20free%20trial%20for%20proprietary%20code.%20Check%20it%20out%3A%20https%3A//coderabbit.ai)
- [LinkedIn](https://www.linkedin.com/sharing/share-offsite/?url=https%3A%2F%2Fcoderabbit.ai&mini=true&title=Great%20tool%20for%20code%20review%20-%20CodeRabbit&summary=I%20just%20used%20CodeRabbit%20for%20my%20code%20review%2C%20and%20it%27s%20fantastic%21%20It%27s%20free%20for%20OSS%20and%20offers%20a%20free%20trial%20for%20proprietary%20code)

</details>

<details>
<summary>🪧 Tips</summary>

### Chat

There are 3 ways to chat with [CodeRabbit](https://coderabbit.ai):

- Review comments: Directly reply to a review comment made by CodeRabbit. Example:
  - `I pushed a fix in commit <commit_id>, please review it.`
  - `Generate unit testing code for this file.`
  - `Open a follow-up GitHub issue for this discussion.`
- Files and specific lines of code (under the "Files changed" tab): Tag `@coderabbitai` in a new review comment at the desired location with your query. Examples:
  - `@coderabbitai generate unit testing code for this file.`
  -	`@coderabbitai modularize this function.`
- PR comments: Tag `@coderabbitai` in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
  - `@coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.`
  - `@coderabbitai read src/utils.ts and generate unit testing code.`
  - `@coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.`
  - `@coderabbitai help me debug CodeRabbit configuration file.`

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

### CodeRabbit Commands (Invoked using PR comments)

- `@coderabbitai pause` to pause the reviews on a PR.
- `@coderabbitai resume` to resume the paused reviews.
- `@coderabbitai review` to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
- `@coderabbitai full review` to do a full review from scratch and review all the files again.
- `@coderabbitai summary` to regenerate the summary of the PR.
- `@coderabbitai generate docstrings` to [generate docstrings](https://docs.coderabbit.ai/finishing-touches/docstrings) for this PR.
- `@coderabbitai resolve` resolve all the CodeRabbit review comments.
- `@coderabbitai configuration` to show the current CodeRabbit configuration for the repository.
- `@coderabbitai help` to get help.

### Other keywords and placeholders

- Add `@coderabbitai ignore` anywhere in the PR description to prevent this PR from being reviewed.
- Add `@coderabbitai summary` to generate the high-level summary at a specific location in the PR description.
- Add `@coderabbitai` anywhere in the PR title to generate the title automatically.

### CodeRabbit Configuration File (`.coderabbit.yaml`)

- You can programmatically configure CodeRabbit by adding a `.coderabbit.yaml` file to the root of your repository.
- Please see the [configuration documentation](https://docs.coderabbit.ai/guides/configure-coderabbit) for more information.
- If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: `# yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json`

### Documentation and Community

- Visit our [Documentation](https://docs.coderabbit.ai) for detailed information on how to use CodeRabbit.
- Join our [Discord Community](http://discord.gg/coderabbit) to get help, request features, and share feedback.
- Follow us on [X/Twitter](https://twitter.com/coderabbitai) for updates and announcements.

</details>

<!-- tips_end -->

Patch:
diff --git a/hasher.go b/hasher.go
index e7e0a65..2763cc1 100644
--- a/hasher.go
+++ b/hasher.go
@@ -313,20 +313,6 @@ func (n *NmtHasher) HashNode(left, right []byte) ([]byte, error) {
 	return h.Sum(res), nil
 }
 
-func max(ns []byte, ns2 []byte) []byte {
-	if bytes.Compare(ns, ns2) >= 0 {
-		return ns
-	}
-	return ns2
-}
-
-func min(ns []byte, ns2 []byte) []byte {
-	if bytes.Compare(ns, ns2) <= 0 {
-		return ns
-	}
-	return ns2
-}
-
 // computeNsRange computes the namespace range of the parent node based on the namespace ranges of its left and right children.
 func computeNsRange(leftMinNs, leftMaxNs, rightMinNs, rightMaxNs []byte, ignoreMaxNs bool, precomputedMaxNs namespace.ID) (minNs []byte, maxNs []byte) {
 	minNs = leftMinNs
diff --git a/proof.go b/proof.go
index 57b38b9..2bc4d71 100644
--- a/proof.go
+++ b/proof.go
@@ -632,7 +632,7 @@ func ToLeafRanges(proofStart, proofEnd, subtreeWidth int) ([]LeafRange, error) {
 // Note: This method is Celestia specific.
 func nextLeafRange(currentStart, currentEnd, subtreeWidth int) (LeafRange, error) {
 	currentLeafRange := currentEnd - currentStart
-	minimum := minInt(currentLeafRange, subtreeWidth)
+	minimum := min(currentLeafRange, subtreeWidth)
 	uMinimum, err := safeIntToUint(minimum)
 	if err != nil {
 		return LeafRange{}, fmt.Errorf("failed to convert subtree root range to Uint %w", err)
@@ -713,10 +713,3 @@ func safeIntToUint(val int) (uint, error) {
 	}
 	return uint(val), nil
 }
-
-func minInt(val1, val2 int) int {
-	if val1 > val2 {
-		return val2
-	}
-	return val1
-}
diff --git a/subrootpaths.go b/subrootpaths.go
index 4494f80..5d6c6d6 100644
--- a/subrootpaths.go
+++ b/subrootpaths.go
@@ -16,7 +16,7 @@ var (
 // this is just a quick function to return that representation as a list of ints
 func subdivide(idxStart uint, width uint) []int {
 	var path []int
-	pathlen := int(bits.Len(width) - 1)
+	pathlen := bits.Len(width) - 1
 	for i := pathlen - 1; i >= 0; i-- {
 		if (idxStart & (1 << i)) == 0 {
 			path = append(path, 0)


Test patch:
diff --git a/hasher_test.go b/hasher_test.go
index cefc169..61c4ceb 100644
--- a/hasher_test.go
+++ b/hasher_test.go
@@ -749,7 +749,7 @@ func TestMax(t *testing.T) {
 
 	for _, ts := range tt {
 		t.Run(ts.name, func(t *testing.T) {
-			maxResult := max(ts.ns, ts.ns2)
+			maxResult := maxNs(ts.ns, ts.ns2)
 			assert.Equal(t, ts.expected, maxResult)
 		})
 	}
@@ -784,7 +784,7 @@ func TestMin(t *testing.T) {
 
 	for _, ts := range tt {
 		t.Run(ts.name, func(t *testing.T) {
-			minResult := min(ts.ns, ts.ns2)
+			minResult := minNs(ts.ns, ts.ns2)
 			assert.Equal(t, ts.expected, minResult)
 		})
 	}
@@ -926,3 +926,17 @@ func TestEmptyRoot(t *testing.T) {
 		assert.Equal(t, want, got)
 	})
 }
+
+func maxNs(ns []byte, ns2 []byte) []byte {
+	if bytes.Compare(ns, ns2) >= 0 {
+		return ns
+	}
+	return ns2
+}
+
+func minNs(ns []byte, ns2 []byte) []byte {
+	if bytes.Compare(ns, ns2) <= 0 {
+		return ns
+	}
+	return ns2
+}
diff --git a/nmt_test.go b/nmt_test.go
index 1ffdccf..155239c 100644
--- a/nmt_test.go
+++ b/nmt_test.go
@@ -878,9 +878,7 @@ func exampleNMT2(nidSize int, ignoreMaxNamespace bool, leavesNIDs ...byte) *Name
 }
 
 func swap(slice [][]byte, i int, j int) {
-	temp := slice[i]
-	slice[i] = slice[j]
-	slice[j] = temp
+	slice[i], slice[j] = slice[j], slice[i]
 }
 
 // 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.
diff --git a/proof_test.go b/proof_test.go
index cb208cd..093a752 100644
--- a/proof_test.go
+++ b/proof_test.go
@@ -514,9 +514,7 @@ func TestVerifyNamespace_False(t *testing.T) {
 	require.True(t, absenceProof9_2.IsOfAbsence())
 
 	// swap leafHashes of the absence proofs
-	buffer := absenceProof9_2.leafHash
-	absenceProof9_2.leafHash = absenceProof9_1.leafHash
-	absenceProof9_1.leafHash = buffer
+	absenceProof9_2.leafHash, absenceProof9_1.leafHash = absenceProof9_1.leafHash, absenceProof9_2.leafHash
 
 	hasher := sha256.New()
 
@@ -1489,7 +1487,7 @@ func TestMinInt(t *testing.T) {
 
 	for _, tt := range tests {
 		t.Run(fmt.Sprintf("val1=%d, val2=%d", tt.val1, tt.val2), func(t *testing.T) {
-			result := minInt(tt.val1, tt.val2)
+			result := min(tt.val1, tt.val2)
 			if result != tt.expected {
 				t.Errorf("expected %d, got %d", tt.expected, result)
 			}