Skip to content

Commit

Permalink
refactor!: version gas scheduler variables
Browse files Browse the repository at this point in the history
  • Loading branch information
ninabarbakadze committed Jul 25, 2024
1 parent 978c38b commit 83ebe01
Show file tree
Hide file tree
Showing 12 changed files with 74 additions and 134 deletions.
7 changes: 7 additions & 0 deletions pkg/appconsts/v3/app_consts.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package v3

const (
Version uint64 = 3
TxSizeCostPerByte uint64 = 10
GasPerBlobByte uint32 = 8
)
14 changes: 12 additions & 2 deletions pkg/appconsts/versioned_consts.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
package appconsts

import (
v1 "github.com/celestiaorg/celestia-app/v3/pkg/appconsts/v1"
v2 "github.com/celestiaorg/celestia-app/v3/pkg/appconsts/v2"
"github.com/celestiaorg/celestia-app/v3/pkg/appconsts/v1"
"github.com/celestiaorg/celestia-app/v3/pkg/appconsts/v2"
"github.com/celestiaorg/celestia-app/v3/pkg/appconsts/v3"
)

const (
Expand All @@ -26,7 +27,16 @@ func SquareSizeUpperBound(_ uint64) int {
return v1.SquareSizeUpperBound
}

func TxSizeCostPerByte(_ uint64) uint64 {
return v3.TxSizeCostPerByte
}

func GasPerBlobByte(_ uint64) uint32 {
return v3.GasPerBlobByte
}

var (
DefaultSubtreeRootThreshold = SubtreeRootThreshold(LatestVersion)
DefaultSquareSizeUpperBound = SquareSizeUpperBound(LatestVersion)
DefaultTxSizeCostPerByte = TxSizeCostPerByte(LatestVersion)
)
68 changes: 36 additions & 32 deletions pkg/appconsts/versioned_consts_test.go
Original file line number Diff line number Diff line change
@@ -1,60 +1,64 @@
package appconsts_test

import (
"fmt"
"testing"

"github.com/stretchr/testify/require"

"github.com/celestiaorg/celestia-app/v3/pkg/appconsts"
v1 "github.com/celestiaorg/celestia-app/v3/pkg/appconsts/v1"
v2 "github.com/celestiaorg/celestia-app/v3/pkg/appconsts/v2"
v3 "github.com/celestiaorg/celestia-app/v3/pkg/appconsts/v3"
)

func TestSubtreeRootThreshold(t *testing.T) {
func TestVersionedConsts(t *testing.T) {
testCases := []struct {
version uint64
expected int
name string
version uint64
expectedConstant int
got int
}{
{
version: v1.Version,
expected: v1.SubtreeRootThreshold,
name: "SubtreeRootThreshold v1",
version: v1.Version,
expectedConstant: v1.SubtreeRootThreshold,
got: appconsts.SubtreeRootThreshold(v1.Version),
},
{
version: v2.Version,
expected: v2.SubtreeRootThreshold,
name: "SubtreeRootThreshold v2",
version: v2.Version,
expectedConstant: v2.SubtreeRootThreshold,
got: appconsts.SubtreeRootThreshold(v2.Version),
},
{
name: "SquareSizeUpperBound v1",
version: v1.Version,
expectedConstant: v1.SquareSizeUpperBound,
got: appconsts.SquareSizeUpperBound(v1.Version),
},
{
name: "SquareSizeUpperBound v2",
version: v2.Version,
expectedConstant: v2.SquareSizeUpperBound,
got: appconsts.SquareSizeUpperBound(v2.Version),
},
}

for _, tc := range testCases {
name := fmt.Sprintf("version %v", tc.version)
t.Run(name, func(t *testing.T) {
got := appconsts.SubtreeRootThreshold(tc.version)
require.Equal(t, tc.expected, got)
})
}
}

func TestSquareSizeUpperBound(t *testing.T) {
testCases := []struct {
version uint64
expected int
}{
{
version: v1.Version,
expected: v1.SquareSizeUpperBound,
name: "TxSizeCostPerByte v3",
version: v3.Version,
expectedConstant: int(v3.TxSizeCostPerByte),
got: int(appconsts.TxSizeCostPerByte(v3.Version)),
},
{
version: v2.Version,
expected: v2.SquareSizeUpperBound,
name: "GasPerBlobByte v3",
version: v3.Version,
expectedConstant: v3.GasPerBlobByte,

Check failure on line 54 in pkg/appconsts/versioned_consts_test.go

View workflow job for this annotation

GitHub Actions / test / test-short

cannot use v3.GasPerBlobByte (constant 8 of type uint32) as int value in struct literal

Check failure on line 54 in pkg/appconsts/versioned_consts_test.go

View workflow job for this annotation

GitHub Actions / test / test

cannot use v3.GasPerBlobByte (constant 8 of type uint32) as int value in struct literal

Check failure on line 54 in pkg/appconsts/versioned_consts_test.go

View workflow job for this annotation

GitHub Actions / test / test-race

cannot use v3.GasPerBlobByte (constant 8 of type uint32) as int value in struct literal
got: appconsts.GasPerBlobByte(v3.Version),

Check failure on line 55 in pkg/appconsts/versioned_consts_test.go

View workflow job for this annotation

GitHub Actions / test / test-short

cannot use appconsts.GasPerBlobByte(v3.Version) (value of type uint32) as int value in struct literal

Check failure on line 55 in pkg/appconsts/versioned_consts_test.go

View workflow job for this annotation

GitHub Actions / test / test

cannot use appconsts.GasPerBlobByte(v3.Version) (value of type uint32) as int value in struct literal

Check failure on line 55 in pkg/appconsts/versioned_consts_test.go

View workflow job for this annotation

GitHub Actions / test / test-race

cannot use appconsts.GasPerBlobByte(v3.Version) (value of type uint32) as int value in struct literal
},
}

for _, tc := range testCases {
name := fmt.Sprintf("version %v", tc.version)
t.Run(name, func(t *testing.T) {
got := appconsts.SquareSizeUpperBound(tc.version)
require.Equal(t, tc.expected, got)
t.Run(tc.name, func(t *testing.T) {
require.Equal(t, tc.expectedConstant, tc.got)
})
}
}
3 changes: 0 additions & 3 deletions proto/celestia/blob/v1/params.proto
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,6 @@ option go_package = "github.com/celestiaorg/celestia-app/x/blob/types";
message Params {
option (gogoproto.goproto_stringer) = false;

uint32 gas_per_blob_byte = 1
[ (gogoproto.moretags) = "yaml:\"gas_per_blob_byte\"" ];

uint64 gov_max_square_size = 2
[ (gogoproto.moretags) = "yaml:\"gov_max_square_size\"" ];
}
2 changes: 1 addition & 1 deletion specs/src/specs/ante_handler_v1.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ The AnteHandler chains together several decorators to ensure the following crite
- The tx's count of signatures <= the max number of signatures. The max number of signatures is [`TxSigLimit = 7`](https://github.com/cosmos/cosmos-sdk/blob/a429238fc267da88a8548bfebe0ba7fb28b82a13/x/auth/README.md?plain=1#L231).
- The tx's [gas_limit](https://github.com/cosmos/cosmos-sdk/blob/22c28366466e64ebf0df1ce5bec8b1130523552c/proto/cosmos/tx/v1beta1/tx.proto#L211-L213) is > the gas consumed based on the tx's signatures.
- The tx's [signatures](https://github.com/cosmos/cosmos-sdk/blob/22c28366466e64ebf0df1ce5bec8b1130523552c/types/tx/signing/signature.go#L10-L26) are valid. For each signature, ensure that the signature's sequence number (a.k.a nonce) matches the account sequence number of the signer.
- The tx's [gas_limit](https://github.com/cosmos/cosmos-sdk/blob/22c28366466e64ebf0df1ce5bec8b1130523552c/proto/cosmos/tx/v1beta1/tx.proto#L211-L213) is > the gas consumed based on the blob size(s). Since blobs are charged based on the number of shares they occupy, the gas consumed is calculated as follows: `gasToConsume = sharesNeeded(blob) * bytesPerShare * gasPerBlobByte`. Where `bytesPerShare` is a global constant (an alias for [`ShareSize = 512`](https://github.com/celestiaorg/celestia-app/blob/c90e61d5a2d0c0bd0e123df4ab416f6f0d141b7f/pkg/appconsts/global_consts.go#L27-L28)) and `gasPerBlobByte` is a governance parameter that can be modified (the [`DefaultGasPerBlobByte = 8`](https://github.com/celestiaorg/celestia-app/blob/c90e61d5a2d0c0bd0e123df4ab416f6f0d141b7f/pkg/appconsts/initial_consts.go#L16-L18)).
- The tx's [gas_limit](https://github.com/cosmos/cosmos-sdk/blob/22c28366466e64ebf0df1ce5bec8b1130523552c/proto/cosmos/tx/v1beta1/tx.proto#L211-L213) is > the gas consumed based on the blob size(s). Since blobs are charged based on the number of shares they occupy, the gas consumed is calculated as follows: `gasToConsume = sharesNeeded(blob) * bytesPerShare * gasPerBlobByte`. Where `bytesPerShare` is a global constant (an alias for [`ShareSize = 512`](https://github.com/celestiaorg/celestia-app/blob/c90e61d5a2d0c0bd0e123df4ab416f6f0d141b7f/pkg/appconsts/global_consts.go#L27-L28)) and `gasPerBlobByte` is an application constant that can be modified through hard fork (the [`DefaultGasPerBlobByte = 8`](https://github.com/celestiaorg/celestia-app/blob/c90e61d5a2d0c0bd0e123df4ab416f6f0d141b7f/pkg/appconsts/initial_consts.go#L16-L18)).
- The tx's total blob size is <= the max blob size. The max blob size is derived from the maximum valid square size. The max valid square size is the minimum of: `GovMaxSquareSize` and `SquareSizeUpperBound`.
- The tx does not contain a message of type [MsgSubmitProposal](https://github.com/cosmos/cosmos-sdk/blob/d6d929843bbd331b885467475bcb3050788e30ca/proto/cosmos/gov/v1/tx.proto#L33-L43) with zero proposal messages.
- The tx is not an IBC packet or update message that has already been processed.
Expand Down
4 changes: 3 additions & 1 deletion x/blob/ante/ante.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package ante

import (
"github.com/celestiaorg/celestia-app/v3/pkg/appconsts"
v3 "github.com/celestiaorg/celestia-app/v3/pkg/appconsts/v3"
"github.com/celestiaorg/celestia-app/v3/x/blob/types"

"cosmossdk.io/errors"
Expand Down Expand Up @@ -34,7 +36,7 @@ func (d MinGasPFBDecorator) AnteHandle(ctx sdk.Context, tx sdk.Tx, simulate bool
if pfb, ok := m.(*types.MsgPayForBlobs); ok {
if gasPerByte == 0 {
// lazily fetch the gas per byte param
gasPerByte = d.k.GasPerBlobByte(ctx)
gasPerByte = appconsts.GasPerBlobByte(v3.Version)
}
gasToConsume := pfb.Gas(gasPerByte)
if gasToConsume > txGas {
Expand Down
3 changes: 2 additions & 1 deletion x/blob/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"fmt"

"github.com/celestiaorg/celestia-app/v3/pkg/appconsts"
"github.com/celestiaorg/celestia-app/v3/x/blob/types"
"github.com/cosmos/cosmos-sdk/codec"
sdk "github.com/cosmos/cosmos-sdk/types"
Expand Down Expand Up @@ -43,7 +44,7 @@ func (k Keeper) Logger(ctx sdk.Context) log.Logger {
func (k Keeper) PayForBlobs(goCtx context.Context, msg *types.MsgPayForBlobs) (*types.MsgPayForBlobsResponse, error) {
ctx := sdk.UnwrapSDKContext(goCtx)

gasToConsume := types.GasToConsume(msg.BlobSizes, k.GasPerBlobByte(ctx))
gasToConsume := types.GasToConsume(msg.BlobSizes, appconsts.GasPerBlobByte(appconsts.LatestVersion))
ctx.GasMeter().ConsumeGas(gasToConsume, payForBlobGasDescriptor)

err := ctx.EventManager().EmitTypedEvent(
Expand Down
7 changes: 0 additions & 7 deletions x/blob/keeper/params.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
// GetParams gets all parameters as types.Params
func (k Keeper) GetParams(ctx sdk.Context) types.Params {
return types.NewParams(
k.GasPerBlobByte(ctx),
k.GovMaxSquareSize(ctx),
)
}
Expand All @@ -18,12 +17,6 @@ func (k Keeper) SetParams(ctx sdk.Context, params types.Params) {
k.paramStore.SetParamSet(ctx, &params)
}

// GasPerBlobByte returns the GasPerBlobByte param
func (k Keeper) GasPerBlobByte(ctx sdk.Context) (res uint32) {
k.paramStore.Get(ctx, types.KeyGasPerBlobByte, &res)
return res
}

// GovMaxSquareSize returns the GovMaxSquareSize param
func (k Keeper) GovMaxSquareSize(ctx sdk.Context) (res uint64) {
k.paramStore.Get(ctx, types.KeyGovMaxSquareSize, &res)
Expand Down
1 change: 0 additions & 1 deletion x/blob/keeper/params_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,4 @@ func TestGetParams(t *testing.T) {
k.SetParams(ctx, params)

require.EqualValues(t, params, k.GetParams(ctx))
require.EqualValues(t, params.GasPerBlobByte, k.GasPerBlobByte(ctx))
}
26 changes: 2 additions & 24 deletions x/blob/types/params.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@ import (
var _ paramtypes.ParamSet = (*Params)(nil)

var (
KeyGasPerBlobByte = []byte("GasPerBlobByte")
DefaultGasPerBlobByte uint32 = appconsts.DefaultGasPerBlobByte
KeyGovMaxSquareSize = []byte("GovMaxSquareSize")
DefaultGovMaxSquareSize uint64 = appconsts.DefaultGovMaxSquareSize
)
Expand All @@ -24,32 +22,26 @@ func ParamKeyTable() paramtypes.KeyTable {
}

// NewParams creates a new Params instance
func NewParams(gasPerBlobByte uint32, govMaxSquareSize uint64) Params {
func NewParams(govMaxSquareSize uint64) Params {
return Params{
GasPerBlobByte: gasPerBlobByte,
GovMaxSquareSize: govMaxSquareSize,
}
}

// DefaultParams returns a default set of parameters
func DefaultParams() Params {
return NewParams(DefaultGasPerBlobByte, appconsts.DefaultGovMaxSquareSize)
return NewParams(appconsts.DefaultGovMaxSquareSize)
}

// ParamSetPairs gets the list of param key-value pairs
func (p *Params) ParamSetPairs() paramtypes.ParamSetPairs {
return paramtypes.ParamSetPairs{
paramtypes.NewParamSetPair(KeyGasPerBlobByte, &p.GasPerBlobByte, validateGasPerBlobByte),
paramtypes.NewParamSetPair(KeyGovMaxSquareSize, &p.GovMaxSquareSize, validateGovMaxSquareSize),
}
}

// Validate validates the set of params
func (p Params) Validate() error {
err := validateGasPerBlobByte(p.GasPerBlobByte)
if err != nil {
return err
}
return validateGovMaxSquareSize(p.GovMaxSquareSize)
}

Expand All @@ -59,20 +51,6 @@ func (p Params) String() string {
return string(out)
}

// validateGasPerBlobByte validates the GasPerBlobByte param
func validateGasPerBlobByte(v interface{}) error {
gasPerBlobByte, ok := v.(uint32)
if !ok {
return fmt.Errorf("invalid parameter type: %T", v)
}

if gasPerBlobByte == 0 {
return fmt.Errorf("gas per blob byte cannot be 0")
}

return nil
}

// validateGovMaxSquareSize validates the GovMaxSquareSize param
func validateGovMaxSquareSize(v interface{}) error {
govMaxSquareSize, ok := v.(uint64)
Expand Down
60 changes: 11 additions & 49 deletions x/blob/types/params.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 83ebe01

Please sign in to comment.