Skip to content

Commit

Permalink
test: expand ante test to account for different version logics
Browse files Browse the repository at this point in the history
  • Loading branch information
ninabarbakadze committed Sep 18, 2024
1 parent 0f3282a commit bc08f02
Showing 1 changed file with 55 additions and 25 deletions.
80 changes: 55 additions & 25 deletions x/blob/ante/ante_test.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
package ante_test

import (
"fmt"
"testing"

"github.com/celestiaorg/celestia-app/v3/app"
"github.com/celestiaorg/celestia-app/v3/app/encoding"
"github.com/celestiaorg/celestia-app/v3/pkg/appconsts"
v2 "github.com/celestiaorg/celestia-app/v3/pkg/appconsts/v2"
ante "github.com/celestiaorg/celestia-app/v3/x/blob/ante"
blob "github.com/celestiaorg/celestia-app/v3/x/blob/types"
"github.com/celestiaorg/go-square/v2/share"
Expand All @@ -25,8 +27,9 @@ func TestPFBAnteHandler(t *testing.T) {
testCases := []struct {
name string
pfb *blob.MsgPayForBlobs
txGas uint64
txGas func(uint32) uint32
gasConsumed uint64
versions []uint64
wantErr bool
}{
{
Expand All @@ -35,17 +38,23 @@ func TestPFBAnteHandler(t *testing.T) {
// 1 share = 512 bytes = 5120 gas
BlobSizes: []uint32{uint32(share.AvailableBytesFromSparseShares(1))},
},
txGas: share.ShareSize * testGasPerBlobByte,
txGas: func(testGasPerBlobByte uint32) uint32 {
return share.ShareSize * testGasPerBlobByte
},
gasConsumed: 0,
versions: []uint64{v2.Version, appconsts.LatestVersion},
wantErr: false,
},
{
name: "valid pfb multi blob",
pfb: &blob.MsgPayForBlobs{
BlobSizes: []uint32{uint32(share.AvailableBytesFromSparseShares(1)), uint32(share.AvailableBytesFromSparseShares(2))},
},
txGas: 3 * share.ShareSize * testGasPerBlobByte,
txGas: func(testGasPerBlobByte uint32) uint32 {
return 3 * share.ShareSize * testGasPerBlobByte
},
gasConsumed: 0,
versions: []uint64{v2.Version, appconsts.LatestVersion},
wantErr: false,
},
{
Expand All @@ -54,17 +63,23 @@ func TestPFBAnteHandler(t *testing.T) {
// 2 share = 1024 bytes = 10240 gas
BlobSizes: []uint32{uint32(share.AvailableBytesFromSparseShares(1) + 1)},
},
txGas: 2*share.ShareSize*testGasPerBlobByte - 1,
txGas: func(testGasPerBlobByte uint32) uint32 {
return 2*share.ShareSize*testGasPerBlobByte - 1
},
gasConsumed: 0,
versions: []uint64{v2.Version, appconsts.LatestVersion},
wantErr: true,
},
{
name: "pfb mulit blob not enough gas",
pfb: &blob.MsgPayForBlobs{
BlobSizes: []uint32{uint32(share.AvailableBytesFromSparseShares(1)), uint32(share.AvailableBytesFromSparseShares(2))},
},
txGas: 3*share.ShareSize*testGasPerBlobByte - 1,
txGas: func(testGasPerBlobByte uint32) uint32 {
return 3*share.ShareSize*testGasPerBlobByte - 1
},
gasConsumed: 0,
versions: []uint64{v2.Version, appconsts.LatestVersion},
wantErr: true,
},
{
Expand All @@ -73,8 +88,11 @@ func TestPFBAnteHandler(t *testing.T) {
// 1 share = 512 bytes = 5120 gas
BlobSizes: []uint32{uint32(share.AvailableBytesFromSparseShares(1))},
},
txGas: share.ShareSize*testGasPerBlobByte + 10000 - 1,
txGas: func(testGasPerBlobByte uint32) uint32 {
return share.ShareSize*testGasPerBlobByte + 10000 - 1
},
gasConsumed: 10000,
versions: []uint64{v2.Version, appconsts.LatestVersion},
wantErr: true,
},
{
Expand All @@ -83,31 +101,43 @@ func TestPFBAnteHandler(t *testing.T) {
// 1 share = 512 bytes = 5120 gas
BlobSizes: []uint32{uint32(share.AvailableBytesFromSparseShares(10))},
},
txGas: 1000000,
txGas: func(_ uint32) uint32 {
return 1000000
},
gasConsumed: 10000,
versions: []uint64{v2.Version, appconsts.LatestVersion},
wantErr: false,
},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
anteHandler := ante.NewMinGasPFBDecorator(mockBlobKeeper{})
ctx := sdk.NewContext(nil, tmproto.Header{
Version: version.Consensus{
App: appconsts.LatestVersion,
},
}, true, nil).WithGasMeter(sdk.NewGasMeter(tc.txGas))
for _, currentVersion := range tc.versions {
t.Run(fmt.Sprintf("%s v%d", tc.name, currentVersion), func(t *testing.T) {
anteHandler := ante.NewMinGasPFBDecorator(mockBlobKeeper{})
var gasPerBlobByte uint32
if currentVersion == v2.Version {
gasPerBlobByte = testGasPerBlobByte
} else {
gasPerBlobByte = appconsts.GasPerBlobByte(currentVersion)
}

ctx := sdk.NewContext(nil, tmproto.Header{
Version: version.Consensus{
App: currentVersion,
},
}, true, nil).WithGasMeter(sdk.NewGasMeter(uint64(tc.txGas(gasPerBlobByte)))).WithIsCheckTx(true)

ctx.GasMeter().ConsumeGas(tc.gasConsumed, "test")
txBuilder := txConfig.NewTxBuilder()
require.NoError(t, txBuilder.SetMsgs(tc.pfb))
tx := txBuilder.GetTx()
_, err := anteHandler.AnteHandle(ctx, tx, false, func(ctx sdk.Context, _ sdk.Tx, _ bool) (sdk.Context, error) { return ctx, nil })
if tc.wantErr {
require.Error(t, err)
} else {
require.NoError(t, err)
}
})
ctx.GasMeter().ConsumeGas(tc.gasConsumed, "test")
txBuilder := txConfig.NewTxBuilder()
require.NoError(t, txBuilder.SetMsgs(tc.pfb))
tx := txBuilder.GetTx()
_, err := anteHandler.AnteHandle(ctx, tx, false, func(ctx sdk.Context, _ sdk.Tx, _ bool) (sdk.Context, error) { return ctx, nil })
if tc.wantErr {
require.Error(t, err)
} else {
require.NoError(t, err)
}
})
}
}
}

Expand Down

0 comments on commit bc08f02

Please sign in to comment.