-
Notifications
You must be signed in to change notification settings - Fork 293
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat!: limit the max tx size to 2 MiB (#3909)
## Overview Fixes [#3686](#3686) --------- Co-authored-by: Rootul P <[email protected]>
- Loading branch information
1 parent
d61a686
commit dcea313
Showing
14 changed files
with
213 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package ante | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/celestiaorg/celestia-app/v3/pkg/appconsts" | ||
v3 "github.com/celestiaorg/celestia-app/v3/pkg/appconsts/v3" | ||
sdk "github.com/cosmos/cosmos-sdk/types" | ||
) | ||
|
||
// MaxTxSizeDecorator ensures that a tx can not be larger than | ||
// application's configured versioned constant. | ||
type MaxTxSizeDecorator struct{} | ||
|
||
func NewMaxTxSizeDecorator() MaxTxSizeDecorator { | ||
return MaxTxSizeDecorator{} | ||
} | ||
|
||
// AnteHandle implements the AnteHandler interface. It ensures that tx size is under application's configured threshold. | ||
func (d MaxTxSizeDecorator) AnteHandle(ctx sdk.Context, tx sdk.Tx, simulate bool, next sdk.AnteHandler) (sdk.Context, error) { | ||
// Tx size rule applies to app versions v3 and onwards. | ||
if ctx.BlockHeader().Version.App < v3.Version { | ||
return next(ctx, tx, simulate) | ||
} | ||
|
||
currentTxSize := len(ctx.TxBytes()) | ||
maxTxBytes := appconsts.MaxTxBytes(ctx.BlockHeader().Version.App) | ||
if currentTxSize > maxTxBytes { | ||
bytesOverLimit := currentTxSize - maxTxBytes | ||
return ctx, fmt.Errorf("tx size %d bytes is larger than the application's configured threshold of %d bytes. Please reduce the size by %d bytes", currentTxSize, maxTxBytes, bytesOverLimit) | ||
} | ||
return next(ctx, tx, simulate) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
package ante_test | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/celestiaorg/celestia-app/v3/app/ante" | ||
v2 "github.com/celestiaorg/celestia-app/v3/pkg/appconsts/v2" | ||
v3 "github.com/celestiaorg/celestia-app/v3/pkg/appconsts/v3" | ||
sdk "github.com/cosmos/cosmos-sdk/types" | ||
"github.com/stretchr/testify/require" | ||
tmproto "github.com/tendermint/tendermint/proto/tendermint/types" | ||
version "github.com/tendermint/tendermint/proto/tendermint/version" | ||
) | ||
|
||
func TestMaxTxSizeDecorator(t *testing.T) { | ||
decorator := ante.NewMaxTxSizeDecorator() | ||
anteHandler := sdk.ChainAnteDecorators(decorator) | ||
|
||
testCases := []struct { | ||
name string | ||
txSize int | ||
expectError bool | ||
appVersion uint64 | ||
isCheckTx []bool | ||
}{ | ||
{ | ||
name: "good tx; under max tx bytes threshold", | ||
txSize: v3.MaxTxBytes - 1, | ||
appVersion: v3.Version, | ||
expectError: false, | ||
isCheckTx: []bool{true, false}, | ||
}, | ||
{ | ||
name: "bad tx; over max tx bytes threshold", | ||
txSize: v3.MaxTxBytes + 1, | ||
appVersion: v3.Version, | ||
expectError: true, | ||
isCheckTx: []bool{true, false}, | ||
}, | ||
{ | ||
name: "good tx; equal to max tx bytes threshold", | ||
txSize: v3.MaxTxBytes, | ||
appVersion: v3.Version, | ||
expectError: false, | ||
isCheckTx: []bool{true, false}, | ||
}, | ||
{ | ||
name: "good tx; limit only applies to v3 and above", | ||
txSize: v3.MaxTxBytes + 10, | ||
appVersion: v2.Version, | ||
expectError: false, | ||
isCheckTx: []bool{true, false}, | ||
}, | ||
} | ||
|
||
for _, tc := range testCases { | ||
t.Run(tc.name, func(t *testing.T) { | ||
for _, isCheckTx := range tc.isCheckTx { | ||
|
||
ctx := sdk.NewContext(nil, tmproto.Header{ | ||
Version: version.Consensus{ | ||
App: tc.appVersion, | ||
}, | ||
}, isCheckTx, nil) | ||
|
||
txBytes := make([]byte, tc.txSize) | ||
|
||
ctx = ctx.WithTxBytes(txBytes) | ||
_, err := anteHandler(ctx, nil, false) | ||
if tc.expectError { | ||
require.Error(t, err) | ||
} else { | ||
require.NoError(t, err) | ||
} | ||
} | ||
}) | ||
} | ||
} |
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters