Skip to content

Commit

Permalink
chore: export LeafRange
Browse files Browse the repository at this point in the history
  • Loading branch information
rach-id committed May 30, 2024
1 parent 4ba85fe commit 12b62f1
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 39 deletions.
12 changes: 6 additions & 6 deletions nmt.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)),
}
Expand Down Expand Up @@ -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,
}
Expand Down Expand Up @@ -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
Expand Down
20 changes: 10 additions & 10 deletions proof.go
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand All @@ -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
}
Expand Down Expand Up @@ -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:
Expand All @@ -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)
}
Expand All @@ -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

Check warning on line 621 in proof.go

View check run for this annotation

Codecov / codecov/patch

proof.go#L621

Added line #L621 was not covered by tests
Expand Down Expand Up @@ -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
Expand Down
46 changes: 23 additions & 23 deletions proof_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1183,22 +1183,22 @@ 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},
},
},
{
proofStart: 0,
proofEnd: 9,
subtreeRootThreshold: 3,
expectedRanges: []leafRange{
expectedRanges: []LeafRange{
{start: 0, end: 8},
{start: 8, end: 9},
},
Expand All @@ -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},
Expand All @@ -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},
Expand All @@ -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},
},
Expand All @@ -1242,15 +1242,15 @@ func TestToLeafRanges(t *testing.T) {
proofStart: 0,
proofEnd: 16,
subtreeRootThreshold: 4,
expectedRanges: []leafRange{
expectedRanges: []LeafRange{
{start: 0, end: 16},
},
},
{
proofStart: 4,
proofEnd: 12,
subtreeRootThreshold: 0,
expectedRanges: []leafRange{
expectedRanges: []LeafRange{
{start: 4, end: 5},
{start: 5, end: 6},
{start: 6, end: 7},
Expand All @@ -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},
Expand All @@ -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},
},
Expand All @@ -1285,7 +1285,7 @@ func TestToLeafRanges(t *testing.T) {
proofStart: 8,
proofEnd: 10,
subtreeRootThreshold: 1,
expectedRanges: []leafRange{
expectedRanges: []LeafRange{
{start: 8, end: 10},
},
},
Expand Down Expand Up @@ -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 {
Expand All @@ -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
}
Expand All @@ -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,
Expand Down

0 comments on commit 12b62f1

Please sign in to comment.