From 01aed8b02245ec791142bc7a5f2c302f8e876eb2 Mon Sep 17 00:00:00 2001 From: c-node Date: Fri, 9 Aug 2024 23:51:01 -0400 Subject: [PATCH 1/2] this is pointless --- inclusion/commitment.go | 22 +++++++++++++++++++--- inclusion/commitment_test.go | 2 ++ 2 files changed, 21 insertions(+), 3 deletions(-) diff --git a/inclusion/commitment.go b/inclusion/commitment.go index c8de38b..5f1aa66 100644 --- a/inclusion/commitment.go +++ b/inclusion/commitment.go @@ -2,6 +2,8 @@ package inclusion import ( "crypto/sha256" + "fmt" + "math" sh "github.com/celestiaorg/go-square/v2/share" "github.com/celestiaorg/nmt" @@ -87,21 +89,35 @@ func CreateCommitments(blobs []*sh.Blob, merkleRootFn MerkleRootFn, subtreeRootT // 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 + bigTrees := totalSize / maxTreeSize + fmt.Println("big trees: ", bigTrees) + leftovers := totalSize - (bigTrees * 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 diff --git a/inclusion/commitment_test.go b/inclusion/commitment_test.go index 06e3c6f..b394c80 100644 --- a/inclusion/commitment_test.go +++ b/inclusion/commitment_test.go @@ -3,6 +3,7 @@ package inclusion_test import ( "bytes" "crypto/sha256" + "fmt" "testing" "github.com/celestiaorg/go-square/v2/inclusion" @@ -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) From 5ff968c509bf3d343f27562a5b6f19585e46c1bc Mon Sep 17 00:00:00 2001 From: c-node Date: Tue, 20 Aug 2024 20:06:11 -0400 Subject: [PATCH 2/2] try something --- inclusion/commitment.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/inclusion/commitment.go b/inclusion/commitment.go index 5f1aa66..d848cf5 100644 --- a/inclusion/commitment.go +++ b/inclusion/commitment.go @@ -92,7 +92,7 @@ func MerkleMountainRangeSizes(totalSize, maxTreeSize uint64) ([]uint64, error) { //var treeSizes []uint64 bigTrees := totalSize / maxTreeSize fmt.Println("big trees: ", bigTrees) - leftovers := totalSize - (bigTrees * maxTreeSize) + leftovers := totalSize % maxTreeSize fmt.Println("leftovers: ", leftovers) var numTrees int if leftovers == 0 {