Skip to content

Commit

Permalink
feat: MaxBlobSize
Browse files Browse the repository at this point in the history
  • Loading branch information
rootulp committed Jul 22, 2023
1 parent 17c0f58 commit ab56e48
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 0 deletions.
29 changes: 29 additions & 0 deletions app/max_blob_size.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package app

import (
"github.com/celestiaorg/celestia-app/pkg/appconsts"
sdk "github.com/cosmos/cosmos-sdk/types"
)

// MaxBlobSize returns an upper bound for the maximum blob size that can fit in
// a single data square. Since the returned value is an upper bound, it is
// possible that slightly smaller blob may not fit due to shares that aren't
// occupied by the blob (i.e. the PFB tx shares).
func (app *App) MaxBlobSize(ctx sdk.Context) int {
maxSquareSize := app.GovSquareSizeUpperBound(ctx)
maxShares := maxSquareSize * maxSquareSize
maxShareBytes := maxShares * appconsts.ContinuationSparseShareContentSize

// TODO(rootulp): get MaxBytes consensus params from core
maxBlockBytes := appconsts.DefaultMaxBytes

return min(maxShareBytes, maxBlockBytes)
}

// min returns the smaller of a and b.
func min(a, b int) int {
if a < b {
return a
}
return b
}
55 changes: 55 additions & 0 deletions app/test/max_blob_size_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package app_test

import (
"testing"

"github.com/celestiaorg/celestia-app/app"
"github.com/celestiaorg/celestia-app/app/encoding"
testutil "github.com/celestiaorg/celestia-app/test/util"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/stretchr/testify/require"
abci "github.com/tendermint/tendermint/abci/types"
tmrand "github.com/tendermint/tendermint/libs/rand"
core "github.com/tendermint/tendermint/proto/tendermint/types"
tmproto "github.com/tendermint/tendermint/proto/tendermint/types"
coretypes "github.com/tendermint/tendermint/types"
)

// TestMaxBlobSize verifies that a blob size of MaxBlobSize can not fit in a
// block.
func TestMaxBlobSize(t *testing.T) {
if testing.Short() {
t.Skip("skipping TestMaxBlobSize in short mode.")
}
encConf := encoding.MakeConfig(app.ModuleEncodingRegisters...)
accounts := make([]string, 1)
for i := range accounts {
accounts[i] = tmrand.Str(20)
}

cparams := app.DefaultConsensusParams()
testApp, kr := testutil.SetupTestAppWithGenesisValSet(cparams, accounts...)
ctx := sdk.NewContext(testApp.CommitMultiStore(), tmproto.Header{}, false, nil)

maxBlobSize := testApp.MaxBlobSize(ctx)
txs := testutil.RandBlobTxsWithAccounts(
t,
testApp,
encConf.TxConfig.TxEncoder(),
kr,
maxBlobSize,
1,
false,
testutil.ChainID,
accounts,
)
resp := testApp.PrepareProposal(abci.RequestPrepareProposal{
BlockData: &core.Data{
Txs: coretypes.Txs(txs).ToSliceOfBytes(),
},
ChainId: testutil.ChainID,
})

// Verify that the blob tx wasn't included in the block.
require.Empty(t, 0, resp.BlockData.Txs)
}

0 comments on commit ab56e48

Please sign in to comment.