Add gaskv ante decorator (#268)

* feature: add gaskv ante handler

* test: add test case for gaskv consumption

* chore: fix typo

* test: split up consumption test cases

* test: replace contains with equal

* fix: linter error
---------

Signed-off-by: Lorenz Herzberger <lorenzherzberger@gmail.com>
This commit is contained in:
Lorenz Herzberger 2024-01-10 16:49:48 +01:00 committed by GitHub
parent fc9e795bd0
commit 4042968dff
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 142 additions and 0 deletions

View File

@ -21,6 +21,7 @@ type HandlerOptions struct {
TxFeeChecker TxFeeChecker TxFeeChecker TxFeeChecker
MachineKeeper MachineKeeper MachineKeeper MachineKeeper
DaoKeeper DaoKeeper DaoKeeper DaoKeeper
StakingKeeper StakingKeeper
} }
// NewAnteHandler returns an AnteHandler that checks and increments sequence // NewAnteHandler returns an AnteHandler that checks and increments sequence
@ -45,10 +46,14 @@ func NewAnteHandler(options HandlerOptions) (sdk.AnteHandler, error) {
if options.DaoKeeper == nil { if options.DaoKeeper == nil {
return nil, errorsmod.Wrap(sdkerrors.ErrLogic, "dao keeper is required for ante builder") return nil, errorsmod.Wrap(sdkerrors.ErrLogic, "dao keeper is required for ante builder")
} }
if options.StakingKeeper == nil {
return nil, errorsmod.Wrap(sdkerrors.ErrLogic, "staking keeper is required for ante builder")
}
anteDecorators := []sdk.AnteDecorator{ anteDecorators := []sdk.AnteDecorator{
ante.NewSetUpContextDecorator(), // outermost AnteDecorator. SetUpContext must be called first ante.NewSetUpContextDecorator(), // outermost AnteDecorator. SetUpContext must be called first
ante.NewExtensionOptionsDecorator(options.ExtensionOptionChecker), ante.NewExtensionOptionsDecorator(options.ExtensionOptionChecker),
NewGasKVCostDecorator(options.StakingKeeper),
ante.NewValidateBasicDecorator(), ante.NewValidateBasicDecorator(),
ante.NewTxTimeoutHeightDecorator(), ante.NewTxTimeoutHeightDecorator(),
ante.NewValidateMemoDecorator(options.AccountKeeper), ante.NewValidateMemoDecorator(options.AccountKeeper),

View File

@ -5,6 +5,7 @@ import (
sdk "github.com/cosmos/cosmos-sdk/types" sdk "github.com/cosmos/cosmos-sdk/types"
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types" authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"
stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types"
daotypes "github.com/planetmint/planetmint-go/x/dao/types" daotypes "github.com/planetmint/planetmint-go/x/dao/types"
) )
@ -39,3 +40,7 @@ type DaoKeeper interface {
GetMintAddress(ctx sdk.Context) (mintAddress string) GetMintAddress(ctx sdk.Context) (mintAddress string)
IsValidReissuanceProposal(ctx sdk.Context, msg *daotypes.MsgReissueRDDLProposal) (isValid bool) IsValidReissuanceProposal(ctx sdk.Context, msg *daotypes.MsgReissueRDDLProposal) (isValid bool)
} }
type StakingKeeper interface {
GetValidator(ctx sdk.Context, addr sdk.ValAddress) (validator stakingtypes.Validator, found bool)
}

View File

@ -0,0 +1,38 @@
package ante
import (
sdk "github.com/cosmos/cosmos-sdk/types"
)
type GasKVCostDecorator struct {
sk StakingKeeper
}
func NewGasKVCostDecorator(sk StakingKeeper) GasKVCostDecorator {
return GasKVCostDecorator{sk: sk}
}
func (gc GasKVCostDecorator) AnteHandle(ctx sdk.Context, tx sdk.Tx, simulate bool, next sdk.AnteHandler) (_ sdk.Context, err error) {
if !simulate && ctx.BlockHeight() != 0 {
msgs := tx.GetMsgs()
signers := msgs[0].GetSigners()
signer := signers[0]
valAddr := sdk.ValAddress(signer)
_, found := gc.sk.GetValidator(ctx, valAddr)
if found {
ctx = ctx.WithKVGasConfig(sdk.GasConfig{
HasCost: 0,
DeleteCost: 0,
ReadCostFlat: 0,
ReadCostPerByte: 0,
WriteCostFlat: 0,
WriteCostPerByte: 0,
IterNextCostFlat: 0,
})
}
}
return next(ctx, tx, simulate)
}

View File

@ -779,6 +779,7 @@ func New(
SigGasConsumer: ante.DefaultSigVerificationGasConsumer, SigGasConsumer: ante.DefaultSigVerificationGasConsumer,
MachineKeeper: app.MachineKeeper, MachineKeeper: app.MachineKeeper,
DaoKeeper: app.DaoKeeper, DaoKeeper: app.DaoKeeper,
StakingKeeper: app.StakingKeeper,
}, },
) )
if err != nil { if err != nil {

View File

@ -17,3 +17,8 @@ func TestPopE2ETestSuite(t *testing.T) {
cfg := network.DefaultConfig() cfg := network.DefaultConfig()
suite.Run(t, NewPopSelectionE2ETestSuite(cfg)) suite.Run(t, NewPopSelectionE2ETestSuite(cfg))
} }
func TestGasConsumptionE2ETestSuite(t *testing.T) {
cfg := network.DefaultConfig()
suite.Run(t, NewGasConsumptionE2ETestSuite(cfg))
}

View File

@ -0,0 +1,88 @@
package dao
import (
sdk "github.com/cosmos/cosmos-sdk/types"
banktypes "github.com/cosmos/cosmos-sdk/x/bank/types"
"github.com/planetmint/planetmint-go/config"
"github.com/planetmint/planetmint-go/lib"
clitestutil "github.com/planetmint/planetmint-go/testutil/cli"
e2etestutil "github.com/planetmint/planetmint-go/testutil/e2e"
"github.com/planetmint/planetmint-go/testutil/network"
"github.com/planetmint/planetmint-go/testutil/sample"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/suite"
)
type GasConsumptionE2ETestSuite struct {
suite.Suite
cfg network.Config
network *network.Network
}
func NewGasConsumptionE2ETestSuite(cfg network.Config) *GasConsumptionE2ETestSuite {
return &GasConsumptionE2ETestSuite{cfg: cfg}
}
func (s *GasConsumptionE2ETestSuite) SetupSuite() {
s.T().Log("setting up e2e test suite")
conf := config.GetConfig()
conf.FeeDenom = "stake"
s.network = network.New(s.T(), s.cfg)
account, err := e2etestutil.CreateAccount(s.network, sample.Name, sample.Mnemonic)
s.Require().NoError(err)
err = e2etestutil.FundAccount(s.network, account)
s.Require().NoError(err)
}
func (s *GasConsumptionE2ETestSuite) TearDownSuite() {
s.T().Log("tearing down e2e test suites")
}
func (s *GasConsumptionE2ETestSuite) TestValidatorConsumption() {
val := s.network.Validators[0]
k, err := val.ClientCtx.Keyring.Key(sample.Name)
s.Require().NoError(err)
addr, _ := k.GetAddress()
// send huge tx but as val and with no gas kv costs
msgs := createMsgs(val.Address, addr, 10)
out, err := lib.BroadcastTxWithFileLock(val.Address, msgs...)
s.Require().NoError(err)
s.Require().NoError(s.network.WaitForNextBlock())
_, err = clitestutil.GetRawLogFromTxOut(val, out)
s.Require().NoError(err)
}
func (s *GasConsumptionE2ETestSuite) TestNonValidatorConsumptionOverflow() {
val := s.network.Validators[0]
k, err := val.ClientCtx.Keyring.Key(sample.Name)
s.Require().NoError(err)
addr, _ := k.GetAddress()
// exceed gas limit with too many msgs as non validator
msgs := createMsgs(addr, val.Address, 10)
out, err := lib.BroadcastTxWithFileLock(addr, msgs...)
s.Require().NoError(err)
s.Require().NoError(s.network.WaitForNextBlock())
_, err = clitestutil.GetRawLogFromTxOut(val, out)
s.Require().Error(err)
assert.Equal(s.T(), err.Error(), "out of gas in location: Has; gasWanted: 200000, gasUsed: 200241: out of gas")
}
func createMsgs(from sdk.AccAddress, to sdk.AccAddress, n int) (msgs []sdk.Msg) {
coins := sdk.NewCoins(sdk.NewInt64Coin("stake", 10))
for i := 0; i < n; i++ {
msg := banktypes.NewMsgSend(from, to, coins)
msgs = append(msgs, msg)
}
return
}