Skip to content

Commit

Permalink
fix: remove unnecessary subtree root threshold conversion
Browse files Browse the repository at this point in the history
  • Loading branch information
rach-id committed May 31, 2024
1 parent ad2f71d commit 82d1886
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 43 deletions.
23 changes: 4 additions & 19 deletions proof.go
Original file line number Diff line number Diff line change
Expand Up @@ -610,16 +610,13 @@ func ToLeafRanges(proofStart, proofEnd, subtreeRootThreshold int) ([]LeafRange,
if proofEnd <= proofStart {
return nil, fmt.Errorf("proof end %d should be stricly bigger than proof start %d", proofEnd, proofStart)
}
if subtreeRootThreshold < 0 {
if subtreeRootThreshold <= 0 {
return nil, fmt.Errorf("subtree root threshold cannot be negative %d", subtreeRootThreshold)
}
currentStart := proofStart
currentLeafRange := proofEnd - proofStart
var ranges []LeafRange
maximumLeafRange, err := subtreeRootThresholdToLeafRange(subtreeRootThreshold)
if err != nil {
return nil, err
}
maximumLeafRange := subtreeRootThreshold
for currentLeafRange != 0 {
nextRange, err := nextLeafRange(currentStart, proofEnd, maximumLeafRange)
if err != nil {
Expand All @@ -632,25 +629,13 @@ func ToLeafRanges(proofStart, proofEnd, subtreeRootThreshold int) ([]LeafRange,
return ranges, nil
}

// subtreeRootThresholdToLeafRange calculates the maximum number of leaves a subtree root
// can commit to.
// The subtree root threshold is defined as per ADR-013:
// https://github.com/celestiaorg/celestia-app/blob/main/docs/architecture/adr-013-non-interactive-default-rules-for-zero-padding.md
func subtreeRootThresholdToLeafRange(subtreeRootThreshold int) (int, error) {
if subtreeRootThreshold < 0 {
return 0, fmt.Errorf("subtree root threshold cannot be negative %d", subtreeRootThreshold)
}
return 1 << subtreeRootThreshold, nil
}

// 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.
// Note: This method is Celestia specific.
func nextLeafRange(currentStart, currentEnd, subtreeRootMaximumLeafRange int) (LeafRange, error) {
func nextLeafRange(currentStart, currentEnd, subtreeRootThreshold int) (LeafRange, error) {
currentLeafRange := currentEnd - currentStart
minimum := minInt(currentLeafRange, subtreeRootMaximumLeafRange)
minimum := minInt(currentLeafRange, subtreeRootThreshold)
uMinimum, err := safeIntToUint(minimum)
if err != nil {
return LeafRange{}, fmt.Errorf("failed to convert subtree root range to Uint %w", err)
Expand Down
24 changes: 0 additions & 24 deletions proof_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1156,30 +1156,6 @@ func TestLargestPowerOfTwo(t *testing.T) {
}
}

func TestSubtreeRootThresholdToLeafRange(t *testing.T) {
tests := []struct {
subtreeRootThreshold int
expected int
expectErr bool
}{
{0, 1, false},
{1, 2, false},
{-1, 0, true},
}

for _, tt := range tests {
t.Run(fmt.Sprintf("subtreeRootThreshold=%d", tt.subtreeRootThreshold), func(t *testing.T) {
result, err := subtreeRootThresholdToLeafRange(tt.subtreeRootThreshold)
if tt.expectErr {
assert.Error(t, err)
} else {
assert.NoError(t, err)
assert.Equal(t, tt.expected, result)
}
})
}
}

func TestToLeafRanges(t *testing.T) {
tests := []struct {
proofStart, proofEnd, subtreeRootThreshold int
Expand Down

0 comments on commit 82d1886

Please sign in to comment.