Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: update DefaultMaxTxSize and gas simulation logic #163

Merged
merged 1 commit into from
Mar 31, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion baseapp/baseapp.go
Original file line number Diff line number Diff line change
Expand Up @@ -582,7 +582,7 @@ func validateBasicTxMsgs(msgs []sdk.Msg) error {
return nil
}

// GetState returns the applications's deliverState if app is in runTxModeDeliver,
// GetState returns the application's deliverState if app is in runTxModeDeliver,
// otherwise it returns the application's checkstate.
func (app *BaseApp) getState(mode runTxMode) *state {
if mode == runTxModeDeliver {
Expand Down
55 changes: 25 additions & 30 deletions x/auth/ante/msg_gas.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,19 @@ import (
"fmt"

"cosmossdk.io/errors"
"github.com/cosmos/cosmos-sdk/codec/legacy"
cryptotypes "github.com/cosmos/cosmos-sdk/crypto/types"
sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
"github.com/cosmos/cosmos-sdk/x/auth/migrations/legacytx"
authsigning "github.com/cosmos/cosmos-sdk/x/auth/signing"
"github.com/cosmos/cosmos-sdk/x/gashub/types"
)

const (
// Length of the protobuf encoded bytes
EthSecp256k1PubkeySize = 79
EthSecp256k1SigSize = 65
FeeSize = 42
)

// ValidateTxSizeDecorator will validate tx bytes length given the parameters passed in
// If tx is too large decorator returns with error, otherwise call next AnteHandler
//
Expand All @@ -38,7 +42,7 @@ func (vtsd ValidateTxSizeDecorator) AnteHandle(ctx sdk.Context, tx sdk.Tx, simul

newCtx := ctx
txSize := newCtx.TxSize()
// simulate signatures in simulate mode
// get right tx size in simulate mode
if simulate {
// in simulate mode, each element should be a nil signature
sigs, err := sigTx.GetSignaturesV2()
Expand All @@ -47,33 +51,20 @@ func (vtsd ValidateTxSizeDecorator) AnteHandle(ctx sdk.Context, tx sdk.Tx, simul
}
n := len(sigs)

for i, signer := range sigTx.GetSigners() {
// if signature is already filled in, no need to simulate gas cost
if i < n && !isIncompleteSignature(sigs[i].Data) {
continue
}

var pubkey cryptotypes.PubKey

acc := vtsd.ak.GetAccount(ctx, signer)

// use placeholder simSecp256k1Pubkey if sig is nil
if acc == nil || acc.GetPubKey() == nil {
pubkey = simSecp256k1Pubkey
for i := range sigTx.GetSigners() {
if i < n {
if isIncompleteSignature(sigs[i].Data) {
txSize += EthSecp256k1SigSize
}
if sigs[i].PubKey == nil {
txSize += EthSecp256k1PubkeySize
}
} else {
pubkey = acc.GetPubKey()
}

// use stdsignature to mock the size of a full signature
simSig := legacytx.StdSignature{ //nolint:staticcheck // this will be removed when proto is ready
Signature: simSecp256k1Sig[:],
PubKey: pubkey,
txSize += EthSecp256k1SigSize + EthSecp256k1PubkeySize
}

sigBz := legacy.Cdc.MustMarshal(simSig)
txSize = txSize + uint64(len(sigBz)) + 14
}

txSize += FeeSize
newCtx = ctx.WithTxSize(txSize)
}

Expand Down Expand Up @@ -113,9 +104,9 @@ func (cmfg ConsumeMsgGasDecorator) AnteHandle(ctx sdk.Context, tx sdk.Tx, simula
}

if gasByTxSize > gasByMsgType {
ctx.GasMeter().ConsumeGas(gasByTxSize, "tx bytes length")
ctx.GasMeter().ConsumeGas(gasByTxSize, "gas cost by tx bytes length")
} else {
ctx.GasMeter().ConsumeGas(gasByMsgType, "msg type")
ctx.GasMeter().ConsumeGas(gasByMsgType, "gas cost by msg type")
}

return next(ctx, tx, simulate)
Expand All @@ -140,5 +131,9 @@ func (cmfg ConsumeMsgGasDecorator) getMsgGas(params types.Params, tx sdk.Tx) (ui
}

func (cmfg ConsumeMsgGasDecorator) getTxSizeGas(params types.Params, ctx sdk.Context) uint64 {
return params.GetMinGasPerByte() * ctx.TxSize()
txSize := ctx.TxSize()
if txSize < params.GetMaxTxSize()/2 {
return 0
}
return params.GetMinGasPerByte() * txSize
}
2 changes: 1 addition & 1 deletion x/gashub/types/params.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (

// Default parameter values
const (
DefaultMaxTxSize uint64 = 32 * 1024 // 32kb
DefaultMaxTxSize uint64 = 64 * 1024 // 32kb
DefaultMinGasPerByte uint64 = 5
)

Expand Down