From 12b62f1dc0f31e8371eb4379fdbc59443ad38cd5 Mon Sep 17 00:00:00 2001 From: sweexordious Date: Thu, 30 May 2024 12:28:05 +0400 Subject: [PATCH] chore: export LeafRange --- nmt.go | 12 ++++++------ proof.go | 20 ++++++++++---------- proof_test.go | 46 +++++++++++++++++++++++----------------------- 3 files changed, 39 insertions(+), 39 deletions(-) diff --git a/nmt.go b/nmt.go index 5d2aa7f0..437d8065 100644 --- a/nmt.go +++ b/nmt.go @@ -107,9 +107,9 @@ type NamespacedMerkleTree struct { // namespaceRanges can be used to efficiently look up the range for an // existing namespace without iterating through the leaves. The map key is - // the string representation of a namespace.ID and the leafRange indicates + // the string representation of a namespace.ID and the LeafRange indicates // the range of the leaves matching that namespace ID in the tree - namespaceRanges map[string]leafRange + namespaceRanges map[string]LeafRange // minNID is the minimum namespace ID of the leaves minNID namespace.ID // maxNID is the maximum namespace ID of the leaves @@ -151,7 +151,7 @@ func New(h hash.Hash, setters ...Option) *NamespacedMerkleTree { visit: opts.NodeVisitor, leaves: make([][]byte, 0, opts.InitialCapacity), leafHashes: make([][]byte, 0, opts.InitialCapacity), - namespaceRanges: make(map[string]leafRange), + namespaceRanges: make(map[string]LeafRange), minNID: bytes.Repeat([]byte{0xFF}, int(opts.NamespaceIDSize)), maxNID: bytes.Repeat([]byte{0x00}, int(opts.NamespaceIDSize)), } @@ -590,12 +590,12 @@ func (n *NamespacedMerkleTree) updateNamespaceRanges() { lastNsStr := string(lastPushed[:n.treeHasher.NamespaceSize()]) lastRange, found := n.namespaceRanges[lastNsStr] if !found { - n.namespaceRanges[lastNsStr] = leafRange{ + n.namespaceRanges[lastNsStr] = LeafRange{ start: lastIndex, end: lastIndex + 1, } } else { - n.namespaceRanges[lastNsStr] = leafRange{ + n.namespaceRanges[lastNsStr] = LeafRange{ start: lastRange.start, end: lastRange.end + 1, } @@ -679,7 +679,7 @@ func isPowerOfTwo(n int) bool { return n > 0 && (n&(n-1)) == 0 } -type leafRange struct { +type LeafRange struct { // start and end denote the indices of a leaf in the tree. start ranges from // 0 up to the total number of leaves minus 1 end ranges from 1 up to the // total number of leaves end is non-inclusive diff --git a/proof.go b/proof.go index babf1c5a..61c471f2 100644 --- a/proof.go +++ b/proof.go @@ -475,7 +475,7 @@ func (proof Proof) VerifyInclusion(h hash.Hash, nid namespace.ID, leavesWithoutN // Using this method without making sure the above assumptions are respected // can return invalid results. // The subtreeRootThreshold is also defined in ADR-013. -// More information on the algorithm used can be found in the toLeafRanges() method docs. +// More information on the algorithm used can be found in the ToLeafRanges() method docs. func (proof Proof) VerifySubtreeRootInclusion(nth *NmtHasher, subtreeRoots [][]byte, subtreeRootThreshold int, root []byte) (bool, error) { // check that the proof range is valid if proof.Start() < 0 || proof.Start() >= proof.End() { @@ -500,7 +500,7 @@ func (proof Proof) VerifySubtreeRootInclusion(nth *NmtHasher, subtreeRoots [][]b } // get the subtree roots leaf ranges - ranges, err := toLeafRanges(proof.Start(), proof.End(), subtreeRootThreshold) + ranges, err := ToLeafRanges(proof.Start(), proof.End(), subtreeRootThreshold) if err != nil { return false, err } @@ -584,7 +584,7 @@ func (proof Proof) VerifySubtreeRootInclusion(nth *NmtHasher, subtreeRoots [][]b return bytes.Equal(rootHash, root), nil } -// toLeafRanges returns the leaf ranges corresponding to the provided subtree roots. +// ToLeafRanges returns the leaf ranges corresponding to the provided subtree roots. // It uses the subtree root threshold to calculate the maximum number of leaves a subtree root can // commit to. // The subtree root threshold is defined as per ADR-013: @@ -603,7 +603,7 @@ func (proof Proof) VerifySubtreeRootInclusion(nth *NmtHasher, subtreeRoots [][]b // - Go back to the loop condition. // // Note: This method is Celestia specific. -func toLeafRanges(proofStart, proofEnd, subtreeRootThreshold int) ([]leafRange, error) { +func ToLeafRanges(proofStart, proofEnd, subtreeRootThreshold int) ([]LeafRange, error) { if proofStart < 0 { return nil, fmt.Errorf("proof start %d shouldn't be strictly negative", proofStart) } @@ -615,7 +615,7 @@ func toLeafRanges(proofStart, proofEnd, subtreeRootThreshold int) ([]leafRange, } currentStart := proofStart currentLeafRange := proofEnd - proofStart - var ranges []leafRange + var ranges []LeafRange maximumLeafRange, err := subtreeRootThresholdToLeafRange(subtreeRootThreshold) if err != nil { return nil, err @@ -646,20 +646,20 @@ func subtreeRootThresholdToLeafRange(subtreeRootThreshold int) (int, error) { // nextLeafRange takes a proof start, proof end, and the maximum range a subtree // root can cover, and returns the corresponding subtree root range. // The subtreeRootMaximum LeafRange is calculated using subtreeRootThresholdToLeafRange() method. -// Check toLeafRanges() for more information on the algorithm used. +// Check ToLeafRanges() for more information on the algorithm used. // Note: This method is Celestia specific. -func nextLeafRange(currentStart, currentEnd, subtreeRootMaximumLeafRange int) (leafRange, error) { +func nextLeafRange(currentStart, currentEnd, subtreeRootMaximumLeafRange int) (LeafRange, error) { currentLeafRange := currentEnd - currentStart minimum := minInt(currentLeafRange, subtreeRootMaximumLeafRange) uMinimum, err := safeIntToUint(minimum) if err != nil { - return leafRange{}, fmt.Errorf("failed to convert subtree root range to Uint %w", err) + return LeafRange{}, fmt.Errorf("failed to convert subtree root range to Uint %w", err) } currentRange, err := largestPowerOfTwo(uMinimum) if err != nil { - return leafRange{}, err + return LeafRange{}, err } - return leafRange{start: currentStart, end: currentStart + currentRange}, nil + return LeafRange{start: currentStart, end: currentStart + currentRange}, nil } // largestPowerOfTwo calculates the largest power of two diff --git a/proof_test.go b/proof_test.go index e7b84918..59ef8492 100644 --- a/proof_test.go +++ b/proof_test.go @@ -1183,14 +1183,14 @@ func TestSubtreeRootThresholdToLeafRange(t *testing.T) { func TestToLeafRanges(t *testing.T) { tests := []struct { proofStart, proofEnd, subtreeRootThreshold int - expectedRanges []leafRange + expectedRanges []LeafRange expectError bool }{ { proofStart: 0, proofEnd: 8, subtreeRootThreshold: 3, - expectedRanges: []leafRange{ + expectedRanges: []LeafRange{ {start: 0, end: 8}, }, }, @@ -1198,7 +1198,7 @@ func TestToLeafRanges(t *testing.T) { proofStart: 0, proofEnd: 9, subtreeRootThreshold: 3, - expectedRanges: []leafRange{ + expectedRanges: []LeafRange{ {start: 0, end: 8}, {start: 8, end: 9}, }, @@ -1207,7 +1207,7 @@ func TestToLeafRanges(t *testing.T) { proofStart: 0, proofEnd: 16, subtreeRootThreshold: 1, - expectedRanges: []leafRange{ + expectedRanges: []LeafRange{ {start: 0, end: 2}, {start: 2, end: 4}, {start: 4, end: 6}, @@ -1222,7 +1222,7 @@ func TestToLeafRanges(t *testing.T) { proofStart: 0, proofEnd: 16, subtreeRootThreshold: 2, - expectedRanges: []leafRange{ + expectedRanges: []LeafRange{ {start: 0, end: 4}, {start: 4, end: 8}, {start: 8, end: 12}, @@ -1233,7 +1233,7 @@ func TestToLeafRanges(t *testing.T) { proofStart: 0, proofEnd: 16, subtreeRootThreshold: 3, - expectedRanges: []leafRange{ + expectedRanges: []LeafRange{ {start: 0, end: 8}, {start: 8, end: 16}, }, @@ -1242,7 +1242,7 @@ func TestToLeafRanges(t *testing.T) { proofStart: 0, proofEnd: 16, subtreeRootThreshold: 4, - expectedRanges: []leafRange{ + expectedRanges: []LeafRange{ {start: 0, end: 16}, }, }, @@ -1250,7 +1250,7 @@ func TestToLeafRanges(t *testing.T) { proofStart: 4, proofEnd: 12, subtreeRootThreshold: 0, - expectedRanges: []leafRange{ + expectedRanges: []LeafRange{ {start: 4, end: 5}, {start: 5, end: 6}, {start: 6, end: 7}, @@ -1265,7 +1265,7 @@ func TestToLeafRanges(t *testing.T) { proofStart: 4, proofEnd: 12, subtreeRootThreshold: 1, - expectedRanges: []leafRange{ + expectedRanges: []LeafRange{ {start: 4, end: 6}, {start: 6, end: 8}, {start: 8, end: 10}, @@ -1276,7 +1276,7 @@ func TestToLeafRanges(t *testing.T) { proofStart: 4, proofEnd: 12, subtreeRootThreshold: 2, - expectedRanges: []leafRange{ + expectedRanges: []LeafRange{ {start: 4, end: 8}, {start: 8, end: 12}, }, @@ -1285,7 +1285,7 @@ func TestToLeafRanges(t *testing.T) { proofStart: 8, proofEnd: 10, subtreeRootThreshold: 1, - expectedRanges: []leafRange{ + expectedRanges: []LeafRange{ {start: 8, end: 10}, }, }, @@ -1321,7 +1321,7 @@ func TestToLeafRanges(t *testing.T) { for _, tt := range tests { t.Run(fmt.Sprintf("proofStart=%d, proofEnd=%d, subtreeRootThreshold=%d", tt.proofStart, tt.proofEnd, tt.subtreeRootThreshold), func(t *testing.T) { - result, err := toLeafRanges(tt.proofStart, tt.proofEnd, tt.subtreeRootThreshold) + result, err := ToLeafRanges(tt.proofStart, tt.proofEnd, tt.subtreeRootThreshold) if tt.expectError { assert.Error(t, err) } else { @@ -1332,7 +1332,7 @@ func TestToLeafRanges(t *testing.T) { } } -func compareRanges(a, b []leafRange) bool { +func compareRanges(a, b []LeafRange) bool { if len(a) != len(b) { return false } @@ -1347,62 +1347,62 @@ func compareRanges(a, b []leafRange) bool { func TestNextLeafRange(t *testing.T) { tests := []struct { currentStart, currentEnd, subtreeRootMaximumLeafRange int - expectedRange leafRange + expectedRange LeafRange expectError bool }{ { currentStart: 0, currentEnd: 8, subtreeRootMaximumLeafRange: 4, - expectedRange: leafRange{start: 0, end: 4}, + expectedRange: LeafRange{start: 0, end: 4}, }, { currentStart: 4, currentEnd: 10, subtreeRootMaximumLeafRange: 8, - expectedRange: leafRange{start: 4, end: 8}, + expectedRange: LeafRange{start: 4, end: 8}, }, { currentStart: 4, currentEnd: 20, subtreeRootMaximumLeafRange: 16, - expectedRange: leafRange{start: 4, end: 20}, + expectedRange: LeafRange{start: 4, end: 20}, }, { currentStart: 4, currentEnd: 20, subtreeRootMaximumLeafRange: 1, - expectedRange: leafRange{start: 4, end: 5}, + expectedRange: LeafRange{start: 4, end: 5}, }, { currentStart: 4, currentEnd: 20, subtreeRootMaximumLeafRange: 2, - expectedRange: leafRange{start: 4, end: 6}, + expectedRange: LeafRange{start: 4, end: 6}, }, { currentStart: 4, currentEnd: 20, subtreeRootMaximumLeafRange: 4, - expectedRange: leafRange{start: 4, end: 8}, + expectedRange: LeafRange{start: 4, end: 8}, }, { currentStart: 4, currentEnd: 20, subtreeRootMaximumLeafRange: 8, - expectedRange: leafRange{start: 4, end: 12}, + expectedRange: LeafRange{start: 4, end: 12}, }, { currentStart: 0, currentEnd: 1, subtreeRootMaximumLeafRange: 1, - expectedRange: leafRange{start: 0, end: 1}, + expectedRange: LeafRange{start: 0, end: 1}, }, { currentStart: 0, currentEnd: 16, subtreeRootMaximumLeafRange: 16, - expectedRange: leafRange{start: 0, end: 16}, + expectedRange: LeafRange{start: 0, end: 16}, }, { currentStart: 0,