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

fix: compact share Count #778

Merged
merged 2 commits into from
Sep 23, 2022
Merged
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
10 changes: 1 addition & 9 deletions app/estimate_square_size.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,15 +92,7 @@ func calculateCompactShareCount(txs []*parsedTx, evd core.EvidenceList, squareSi
panic(err)
}
}
txCount, available := txSplitter.Count()
if appconsts.CompactShareContentSize-available > 0 {
txCount++
}
evdCount, available := evdSplitter.Count()
if appconsts.CompactShareContentSize-available > 0 {
evdCount++
}
return txCount + evdCount
return txSplitter.Count() + evdSplitter.Count()
}

// estimateSquareSize uses the provided block data to estimate the square size
Expand Down
12 changes: 5 additions & 7 deletions pkg/shares/split_compact_shares.go
Original file line number Diff line number Diff line change
Expand Up @@ -153,15 +153,13 @@ func (css *CompactShareSplitter) Export() NamespacedShares {
return css.shares
}

// Count returns the current number of shares that will be made if exporting and
// the number of availableBytes in the last pending share.
func (css *CompactShareSplitter) Count() (count, availableBytes int) {
// Count returns the current number of shares that will be made if exporting.
func (css *CompactShareSplitter) Count() (shareCount int) {
if len(css.pendingShare.Share) > appconsts.NamespaceSize+appconsts.ShareInfoBytes {
return len(css.shares), 0
// pending share is non-empty, so we must add one to the count
return len(css.shares) + 1
}
// this doesn't account for the size of the reserved byte
availableBytes = appconsts.CompactShareContentSize - (len(css.pendingShare.Share) - appconsts.NamespaceSize - appconsts.ShareInfoBytes)
return len(css.shares), availableBytes
return len(css.shares)
}

var tailPaddingInfo, _ = NewInfoReservedByte(appconsts.ShareVersion, false)
Expand Down
33 changes: 33 additions & 0 deletions pkg/shares/split_compact_shares_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package shares

import (
"bytes"
"testing"

"github.com/celestiaorg/celestia-app/pkg/appconsts"
coretypes "github.com/tendermint/tendermint/types"
)

func TestCount(t *testing.T) {
type testCase struct {
transactions []coretypes.Tx
wantShareCount int
}
testCases := []testCase{
{transactions: []coretypes.Tx{}, wantShareCount: 0},
{transactions: []coretypes.Tx{[]byte{0}}, wantShareCount: 1},
{transactions: []coretypes.Tx{bytes.Repeat([]byte{0}, 100)}, wantShareCount: 1},
{transactions: []coretypes.Tx{bytes.Repeat([]byte{0}, appconsts.CompactShareContentSize+1)}, wantShareCount: 2},
{transactions: []coretypes.Tx{bytes.Repeat([]byte{0}, appconsts.CompactShareContentSize*2+1)}, wantShareCount: 3},
}
for _, tc := range testCases {
css := NewCompactShareSplitter(appconsts.TxNamespaceID, appconsts.ShareVersion)
for _, transaction := range tc.transactions {
css.WriteTx(transaction)
}
got := css.Count()
if got != tc.wantShareCount {
t.Errorf("count got %d want %d", got, tc.wantShareCount)
}
}
}