Skip to content

Commit

Permalink
move fee and gaslimit under TxOption struct
Browse files Browse the repository at this point in the history
  • Loading branch information
vgonkivs committed May 9, 2024
1 parent 880368d commit 4df5175
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 34 deletions.
8 changes: 4 additions & 4 deletions nodebuilder/state/mocks/api.go

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

14 changes: 4 additions & 10 deletions nodebuilder/state/state.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,8 @@ type Module interface {
// SubmitPayForBlobWithOptions builds, signs and submits a PayForBlob transaction with additional options.
SubmitPayForBlobWithOptions(
ctx context.Context,
fee state.Int,
gasLim uint64,
blobs []*blob.Blob,
options *state.PayForBlobOptions,
options *state.TxOptions,
) (*state.TxResponse, error)
// CancelUnbondingDelegation cancels a user's pending undelegation from a validator.
CancelUnbondingDelegation(
Expand Down Expand Up @@ -140,10 +138,8 @@ type API struct {
) (*state.TxResponse, error) `perm:"write"`
SubmitPayForBlobWithOptions func(
ctx context.Context,
fee state.Int,
gasLim uint64,
blobs []*blob.Blob,
options *state.PayForBlobOptions,
options *state.TxOptions,
) (*state.TxResponse, error) `perm:"write"`
CancelUnbondingDelegation func(
ctx context.Context,
Expand Down Expand Up @@ -237,12 +233,10 @@ func (api *API) SubmitPayForBlob(

func (api *API) SubmitPayForBlobWithOptions(
ctx context.Context,
fee state.Int,
gasLim uint64,
blobs []*blob.Blob,
options *state.PayForBlobOptions,
options *state.TxOptions,
) (*state.TxResponse, error) {
return api.Internal.SubmitPayForBlobWithOptions(ctx, fee, gasLim, blobs, options)
return api.Internal.SubmitPayForBlobWithOptions(ctx, blobs, options)
}

func (api *API) CancelUnbondingDelegation(
Expand Down
4 changes: 1 addition & 3 deletions nodebuilder/state/stub.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,10 +56,8 @@ func (s stubbedStateModule) SubmitPayForBlob(

func (s stubbedStateModule) SubmitPayForBlobWithOptions(
context.Context,
state.Int,
uint64,
[]*blob.Blob,
*state.PayForBlobOptions,
*state.TxOptions,
) (*state.TxResponse, error) {
return nil, ErrNoStateAccess
}
Expand Down
29 changes: 15 additions & 14 deletions state/core_access.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"time"

sdkErrors "cosmossdk.io/errors"
sdkmath "cosmossdk.io/math"
nodeservice "github.com/cosmos/cosmos-sdk/client/grpc/node"
"github.com/cosmos/cosmos-sdk/client/grpc/tmservice"
"github.com/cosmos/cosmos-sdk/crypto/keyring"
Expand Down Expand Up @@ -218,18 +219,16 @@ func (ca *CoreAccessor) SubmitPayForBlob(
gasLim uint64,
blobs []*blob.Blob,
) (*TxResponse, error) {
return ca.SubmitPayForBlobWithOptions(ctx, fee, gasLim, blobs, nil)
return ca.SubmitPayForBlobWithOptions(ctx, blobs, &TxOptions{Fee: fee.Int64(), GasLimit: gasLim})
}

// SubmitPayForBlobWithOptions builds, signs, and submits a MsgPayForBlob with additional options defined
// in `PayForBlobOptions`. If gasLim is set to 0, the method will automatically estimate the gas limit.
// in `TxOptions`. If gasLim is set to 0, the method will automatically estimate the gas limit.
// If the fee is negative, the method will use the nodes min gas price multiplied by the gas limit.
func (ca *CoreAccessor) SubmitPayForBlobWithOptions(
ctx context.Context,
fee Int,
gasLim uint64,
blobs []*blob.Blob,
_ *PayForBlobOptions,
options *TxOptions,
) (*TxResponse, error) {
signer, err := ca.getSigner(ctx)
if err != nil {
Expand All @@ -250,7 +249,7 @@ func (ca *CoreAccessor) SubmitPayForBlobWithOptions(

// we only estimate gas if the user wants us to (by setting the gasLim to 0). In the future we may
// want to make these arguments optional.
if gasLim == 0 {
if options.GasLimit == 0 {
blobSizes := make([]uint32, len(blobs))
for i, blob := range blobs {
blobSizes[i] = uint32(len(blob.Data))
Expand All @@ -259,7 +258,7 @@ func (ca *CoreAccessor) SubmitPayForBlobWithOptions(
// TODO (@cmwaters): the default gas per byte and the default tx size cost per byte could be changed
// through governance. This section could be more robust by tracking these values and adjusting the
// gas limit accordingly (as is done for the gas price)
gasLim = apptypes.EstimateGas(blobSizes, appconsts.DefaultGasPerBlobByte, auth.DefaultTxSizeCostPerByte)
options.GasLimit = apptypes.EstimateGas(blobSizes, appconsts.DefaultGasPerBlobByte, auth.DefaultTxSizeCostPerByte)
}

minGasPrice := ca.getMinGasPrice()
Expand All @@ -268,39 +267,41 @@ func (ca *CoreAccessor) SubmitPayForBlobWithOptions(
// set granter and update gasLimit in case node run in a grantee mode
if !ca.granter.Empty() {
feeGrant = user.SetFeeGranter(ca.granter)
gasLim = uint64(float64(gasLim) * gasMultiplier)
options.GasLimit = uint64(float64(options.GasLimit) * gasMultiplier)
}

fee := sdkmath.NewInt(options.Fee)
// set the fee for the user as the minimum gas price multiplied by the gas limit
estimatedFee := false
if fee.IsNegative() {
estimatedFee = true
fee = sdktypes.NewInt(int64(math.Ceil(minGasPrice * float64(gasLim))))
fee = sdktypes.NewInt(int64(math.Ceil(minGasPrice * float64(options.GasLimit))))
}

var lastErr error
for attempt := 0; attempt < maxRetries; attempt++ {
options := []user.TxOption{user.SetGasLimit(gasLim), withFee(fee)}
opts := []user.TxOption{user.SetGasLimit(options.GasLimit), withFee(fee)}
if feeGrant != nil {
options = append(options, feeGrant)
opts = append(opts, feeGrant)
}
response, err := signer.SubmitPayForBlob(
ctx,
appblobs,
options...,
opts...,
)

// the node is capable of changing the min gas price at any time so we must be able to detect it and
// update our version accordingly
if apperrors.IsInsufficientMinGasPrice(err) && estimatedFee {
// The error message contains enough information to parse the new min gas price
minGasPrice, err = apperrors.ParseInsufficientMinGasPrice(err, minGasPrice, gasLim)
minGasPrice, err = apperrors.ParseInsufficientMinGasPrice(err, minGasPrice, options.GasLimit)
if err != nil {
return nil, fmt.Errorf("parsing insufficient min gas price error: %w", err)
}
ca.setMinGasPrice(minGasPrice)
lastErr = err
// update the fee to retry again
fee = sdktypes.NewInt(int64(math.Ceil(minGasPrice * float64(gasLim))))
fee = sdktypes.NewInt(int64(math.Ceil(minGasPrice * float64(options.GasLimit))))
continue
}

Expand Down
10 changes: 10 additions & 0 deletions state/core_access_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,16 @@ func TestSubmitPayForBlob(t *testing.T) {
}
})
}

for _, tc := range testcases {
t.Run(tc.name, func(t *testing.T) {
resp, err := ca.SubmitPayForBlobWithOptions(ctx, tc.blobs, &TxOptions{Fee: tc.fee.Int64(), GasLimit: tc.gasLim})
require.Equal(t, tc.expErr, err)
if err == nil {
require.EqualValues(t, 0, resp.Code)
}
})
}
}

func extractPort(addr string) string {
Expand Down
8 changes: 5 additions & 3 deletions state/state.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,11 @@ func (a Address) MarshalJSON() ([]byte, error) {
return []byte("\"" + a.Address.String() + "\""), nil
}

// PayForBlobOptions contains additional options that will be applied to the `MsgPayForBlob`.
type PayForBlobOptions struct {
// TxOptions contains additional options that will be applied to the `MsgPayForBlob`.
type TxOptions struct {
// Specifies the key that should be used to sign transactions.
// NOTE: This `KeyName` should be available in the `Keystore`.
KeyName string
KeyName string
Fee int64
GasLimit uint64
}

0 comments on commit 4df5175

Please sign in to comment.