Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

don't merge this #104

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 19 additions & 3 deletions inclusion/commitment.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import (
"crypto/sha256"
"fmt"
"math"

sh "github.com/celestiaorg/go-square/v2/share"
"github.com/celestiaorg/nmt"
Expand Down Expand Up @@ -87,21 +89,35 @@
// https://docs.grin.mw/wiki/chain-state/merkle-mountain-range/
// https://github.com/opentimestamps/opentimestamps-server/blob/master/doc/merkle-mountain-range.md
func MerkleMountainRangeSizes(totalSize, maxTreeSize uint64) ([]uint64, error) {
var treeSizes []uint64
//var treeSizes []uint64

Check failure on line 92 in inclusion/commitment.go

View workflow job for this annotation

GitHub Actions / lint / golangci-lint

commentFormatting: put a space between `//` and comment text (gocritic)
bigTrees := totalSize / maxTreeSize
fmt.Println("big trees: ", bigTrees)
leftovers := totalSize % maxTreeSize
fmt.Println("leftovers: ", leftovers)
var numTrees int
if leftovers == 0 {
numTrees = int(bigTrees)
} else {
numTrees = int(bigTrees) + int(math.Floor(math.Log2(float64(leftovers)))) + int(leftovers%2)
}
fmt.Println("num trees: ", numTrees)
treeSizes := make([]uint64, int(numTrees))

count := 0
for totalSize != 0 {
switch {
case totalSize >= maxTreeSize:
treeSizes = append(treeSizes, maxTreeSize)
treeSizes[count] = maxTreeSize
totalSize -= maxTreeSize
case totalSize < maxTreeSize:
treeSize, err := RoundDownPowerOfTwo(totalSize)
if err != nil {
return treeSizes, err
}
treeSizes = append(treeSizes, treeSize)
treeSizes[count] = treeSize
totalSize -= treeSize
}
count++
}

return treeSizes, nil
Expand Down
2 changes: 2 additions & 0 deletions inclusion/commitment_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package inclusion_test
import (
"bytes"
"crypto/sha256"
"fmt"
"testing"

"github.com/celestiaorg/go-square/v2/inclusion"
Expand Down Expand Up @@ -51,6 +52,7 @@ func TestMerkleMountainRangeSizes(t *testing.T) {
},
}
for _, tt := range tests {
fmt.Println("totalSize:", tt.totalSize, "maxTreeSize:", tt.squareSize)
res, err := inclusion.MerkleMountainRangeSizes(tt.totalSize, tt.squareSize)
require.NoError(t, err)
assert.Equal(t, tt.expected, res)
Expand Down
Loading