Skip to content

Commit

Permalink
Adjust deterministic gas values & add missing msg types (#352)
Browse files Browse the repository at this point in the history
* Add missing msg types to deterministic gas config
* Adjust deterministic gas values
* Use the same default mnemonics in integration-tests as znet does
  • Loading branch information
ysv authored Jan 20, 2023
1 parent 84ac04d commit 7959543
Show file tree
Hide file tree
Showing 6 changed files with 163 additions and 78 deletions.
4 changes: 3 additions & 1 deletion integration-tests/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ func init() {
)

flag.StringVar(&coredAddress, "cored-address", "tcp://localhost:26657", "Address of cored node started by znet")
flag.StringVar(&fundingMnemonic, "funding-mnemonic", "sad hobby filter tray ordinary gap half web cat hard call mystery describe member round trend friend beyond such clap frozen segment fan mistake", "Funding account mnemonic required by tests")
flag.StringVar(&fundingMnemonic, "funding-mnemonic", "pitch basic bundle cause toe sound warm love town crucial divorce shell olympic convince scene middle garment glimpse narrow during fix fruit suffer honey", "Funding account mnemonic required by tests")
flag.Var(&stakerMnemonics, "staker-mnemonic", "Staker account mnemonics required by tests, supports multiple")
flag.StringVar(&logFormat, "log-format", string(logger.ToolDefaultConfig.Format), "Format of logs produced by tests")

Expand All @@ -59,6 +59,8 @@ func init() {
if len(stakerMnemonics) == 0 {
stakerMnemonics = []string{
"biology rigid design broccoli adult hood modify tissue swallow arctic option improve quiz cliff inject soup ozone suffer fantasy layer negative eagle leader priority",
"enemy fix tribe swift alcohol metal salad edge episode dry tired address bless cloth error useful define rough fold swift confirm century wasp acoustic",
"act electric demand cancel duck invest below once obvious estate interest solution drink mango reason already clean host limit stadium smoke census pattern express",
}
}

Expand Down
2 changes: 1 addition & 1 deletion integration-tests/modules/assetft_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1380,7 +1380,7 @@ func TestAssetFTWhitelist(t *testing.T) {
requireT.NoError(
chain.Faucet.FundAccountsWithOptions(ctx, recipient, integrationtests.BalancesOptions{
Messages: []sdk.Msg{
&assetfttypes.MsgSetWhitelistedLimit{},
&banktypes.MsgSend{},
},
}))

Expand Down
12 changes: 8 additions & 4 deletions integration-tests/modules/bank_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -508,10 +508,14 @@ func TestBankMultiSend(t *testing.T) {
}

require.NoError(t, chain.Faucet.FundAccountsWithOptions(ctx, sender, integrationtests.BalancesOptions{
Messages: append([]sdk.Msg{&banktypes.MsgMultiSend{Outputs: []banktypes.Output{
{Coins: make(sdk.Coins, 2)},
{Coins: make(sdk.Coins, 2)},
}}}, issueMsgs...),
Messages: append([]sdk.Msg{&banktypes.MsgMultiSend{
Inputs: []banktypes.Input{
{Coins: make(sdk.Coins, 2)},
},
Outputs: []banktypes.Output{
{Coins: make(sdk.Coins, 2)},
{Coins: make(sdk.Coins, 2)},
}}}, issueMsgs...),
Amount: chain.NetworkConfig.AssetFTConfig.IssueFee.MulRaw(int64(len(issueMsgs))),
}))

Expand Down
107 changes: 63 additions & 44 deletions pkg/config/deterministic_gas.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@ import (
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"
"github.com/cosmos/cosmos-sdk/x/authz"
banktypes "github.com/cosmos/cosmos-sdk/x/bank/types"
crisistypes "github.com/cosmos/cosmos-sdk/x/crisis/types"
distributiontypes "github.com/cosmos/cosmos-sdk/x/distribution/types"
evidencetypes "github.com/cosmos/cosmos-sdk/x/evidence/types"
feegranttypes "github.com/cosmos/cosmos-sdk/x/feegrant"
govtypes "github.com/cosmos/cosmos-sdk/x/gov/types"
slashingtypes "github.com/cosmos/cosmos-sdk/x/slashing/types"
Expand Down Expand Up @@ -40,59 +42,78 @@ func DefaultDeterministicGasRequirements() DeterministicGasRequirements {

dgr.gasByMsg = map[string]gasByMsgFunc{
// asset/ft
MsgName(&assetfttypes.MsgIssue{}): constantGasFunc(80000),
MsgName(&assetfttypes.MsgMint{}): constantGasFunc(35000),
MsgName(&assetfttypes.MsgBurn{}): constantGasFunc(35000),
MsgName(&assetfttypes.MsgFreeze{}): constantGasFunc(55000),
MsgName(&assetfttypes.MsgUnfreeze{}): constantGasFunc(55000),
MsgName(&assetfttypes.MsgGloballyFreeze{}): constantGasFunc(5000),
MsgName(&assetfttypes.MsgGloballyUnfreeze{}): constantGasFunc(5000),
MsgName(&assetfttypes.MsgSetWhitelistedLimit{}): constantGasFunc(35000),
MsgType(&assetfttypes.MsgIssue{}): constantGasFunc(70000),
MsgType(&assetfttypes.MsgMint{}): constantGasFunc(11000),
MsgType(&assetfttypes.MsgBurn{}): constantGasFunc(23000),
MsgType(&assetfttypes.MsgFreeze{}): constantGasFunc(5000),
MsgType(&assetfttypes.MsgUnfreeze{}): constantGasFunc(2500),
MsgType(&assetfttypes.MsgGloballyFreeze{}): constantGasFunc(5000),
MsgType(&assetfttypes.MsgGloballyUnfreeze{}): constantGasFunc(2500),
MsgType(&assetfttypes.MsgSetWhitelistedLimit{}): constantGasFunc(5000),

// asset/nft
// MsgType(&assetnfttypes.MsgBurn{}): constantGasFunc(16000),
// MsgType(&assetnfttypes.MsgIssueClass{}): constantGasFunc(16000),
// MsgType(&assetnfttypes.MsgMint{}): constantGasFunc(39000),

// authz
MsgName(&authz.MsgExec{}): dgr.authzMsgExecGasFunc(2000),
MsgName(&authz.MsgGrant{}): constantGasFunc(7000),
MsgName(&authz.MsgRevoke{}): constantGasFunc(7000),
MsgType(&authz.MsgExec{}): dgr.authzMsgExecGasFunc(2000),
MsgType(&authz.MsgGrant{}): constantGasFunc(7000),
MsgType(&authz.MsgRevoke{}): constantGasFunc(2500),

// bank
MsgName(&banktypes.MsgSend{}): bankSendMsgGasFunc(22000),
MsgName(&banktypes.MsgMultiSend{}): bankMultiSendMsgGasFunc(27000),
MsgType(&banktypes.MsgSend{}): bankSendMsgGasFunc(24000),
MsgType(&banktypes.MsgMultiSend{}): bankMultiSendMsgGasFunc(11000),

// distribution
MsgName(&distributiontypes.MsgFundCommunityPool{}): constantGasFunc(50000),
MsgName(&distributiontypes.MsgSetWithdrawAddress{}): constantGasFunc(50000),
MsgName(&distributiontypes.MsgWithdrawDelegatorReward{}): constantGasFunc(120000),
MsgName(&distributiontypes.MsgWithdrawValidatorCommission{}): constantGasFunc(50000),
MsgType(&distributiontypes.MsgFundCommunityPool{}): constantGasFunc(15000),
MsgType(&distributiontypes.MsgSetWithdrawAddress{}): constantGasFunc(5000),
MsgType(&distributiontypes.MsgWithdrawDelegatorReward{}): constantGasFunc(65000),
MsgType(&distributiontypes.MsgWithdrawValidatorCommission{}): constantGasFunc(22000),

// feegrant
MsgName(&feegranttypes.MsgGrantAllowance{}): constantGasFunc(13000),
MsgName(&feegranttypes.MsgRevokeAllowance{}): constantGasFunc(13000),
MsgType(&feegranttypes.MsgGrantAllowance{}): constantGasFunc(10000),
MsgType(&feegranttypes.MsgRevokeAllowance{}): constantGasFunc(2500),

// gov
MsgName(&govtypes.MsgSubmitProposal{}): constantGasFunc(95000),
MsgName(&govtypes.MsgVote{}): constantGasFunc(8000),
MsgName(&govtypes.MsgVoteWeighted{}): constantGasFunc(11000),
MsgName(&govtypes.MsgDeposit{}): constantGasFunc(11000),
MsgType(&govtypes.MsgSubmitProposal{}): constantGasFunc(65000),
MsgType(&govtypes.MsgVote{}): constantGasFunc(7000),
MsgType(&govtypes.MsgVoteWeighted{}): constantGasFunc(9000),
MsgType(&govtypes.MsgDeposit{}): constantGasFunc(52000),

// nft
// MsgType(&nfttypes.MsgSend{}): constantGasFunc(16000),

// slashing
MsgName(&slashingtypes.MsgUnjail{}): constantGasFunc(25000),
MsgType(&slashingtypes.MsgUnjail{}): constantGasFunc(25000),

// staking
MsgName(&stakingtypes.MsgDelegate{}): constantGasFunc(51000),
MsgName(&stakingtypes.MsgUndelegate{}): constantGasFunc(51000),
MsgName(&stakingtypes.MsgBeginRedelegate{}): constantGasFunc(51000),
MsgName(&stakingtypes.MsgCreateValidator{}): constantGasFunc(50000),
MsgName(&stakingtypes.MsgEditValidator{}): constantGasFunc(50000),
MsgType(&stakingtypes.MsgDelegate{}): constantGasFunc(69000),
MsgType(&stakingtypes.MsgUndelegate{}): constantGasFunc(112000),
MsgType(&stakingtypes.MsgBeginRedelegate{}): constantGasFunc(142000),
MsgType(&stakingtypes.MsgCreateValidator{}): constantGasFunc(76000),
MsgType(&stakingtypes.MsgEditValidator{}): constantGasFunc(13000),

// wasm
MsgName(&wasmtypes.MsgUpdateAdmin{}): constantGasFunc(8000),
MsgName(&wasmtypes.MsgClearAdmin{}): constantGasFunc(8000),
MsgType(&wasmtypes.MsgUpdateAdmin{}): constantGasFunc(8000),
MsgType(&wasmtypes.MsgClearAdmin{}): constantGasFunc(6500),
}

registerUndeterministicGasFuncs(
&dgr,
// wasm
[]sdk.Msg{
// crisis
// MsgVerifyInvariant is defined as undeterministic since fee
// charged by this tx type is defined as param inside module.
&crisistypes.MsgVerifyInvariant{},

// evidence
// MsgSubmitEvidence is defined as undeterministic since we do not
// have any custom evidence type implemented, so it should fail on
// ValidateBasic step.
&evidencetypes.MsgSubmitEvidence{},

// wasm
&wasmtypes.MsgStoreCode{},
&wasmtypes.MsgInstantiateContract{},
&wasmtypes.MsgInstantiateContract2{},
Expand All @@ -115,7 +136,7 @@ func (dgr DeterministicGasRequirements) TxBaseGas(params authtypes.Params) uint6
// GasRequiredByMessage returns gas required by message and true if message is deterministic.
// Function returns 0 and false if message is undeterministic or unknown.
func (dgr DeterministicGasRequirements) GasRequiredByMessage(msg sdk.Msg) (uint64, bool) {
gasFunc, ok := dgr.gasByMsg[MsgName(msg)]
gasFunc, ok := dgr.gasByMsg[MsgType(msg)]
if ok {
return gasFunc(msg)
}
Expand All @@ -126,11 +147,11 @@ func (dgr DeterministicGasRequirements) GasRequiredByMessage(msg sdk.Msg) (uint6
return 0, false
}

// MsgName returns TypeURL of a msg in cosmos SDK style.
// MsgType returns TypeURL of a msg in cosmos SDK style.
// Samples of values returned by the function:
// "/cosmos.distribution.v1beta1.MsgFundCommunityPool"
// "/coreum.asset.ft.v1.MsgMint"
func MsgName(msg sdk.Msg) string {
func MsgType(msg sdk.Msg) string {
return sdk.MsgTypeURL(msg)
}

Expand Down Expand Up @@ -161,7 +182,7 @@ func (dgr *DeterministicGasRequirements) authzMsgExecGasFunc(authzMsgExecOverhea

func registerUndeterministicGasFuncs(dgr *DeterministicGasRequirements, msgs []sdk.Msg) {
for _, msg := range msgs {
dgr.gasByMsg[MsgName(msg)] = underministicGasFunc()
dgr.gasByMsg[MsgType(msg)] = underministicGasFunc()
}
}

Expand Down Expand Up @@ -189,24 +210,22 @@ func bankSendMsgGasFunc(bankSendPerEntryGas uint64) gasByMsgFunc {
}
}

func bankMultiSendMsgGasFunc(bankMultiSendPerEntryGas uint64) gasByMsgFunc {
func bankMultiSendMsgGasFunc(bankMultiSendPerOperationGas uint64) gasByMsgFunc {
return func(msg sdk.Msg) (uint64, bool) {
m, ok := msg.(*banktypes.MsgMultiSend)
if !ok {
return 0, false
}
inputEntriesNum := 0
totalOperationsNum := 0
for _, inp := range m.Inputs {
inputEntriesNum += len(inp.Coins)
totalOperationsNum += len(inp.Coins)
}

outputEntriesNum := 0
for _, outp := range m.Outputs {
outputEntriesNum += len(outp.Coins)
totalOperationsNum += len(outp.Coins)
}

// Select max of input or output entries & use 1 as a fallback.
maxEntriesNum := lo.Max([]int{inputEntriesNum, outputEntriesNum, 1})
return uint64(maxEntriesNum) * bankMultiSendPerEntryGas, true
// Minimum 2 operations (1 input & 1 output) should be present inside any multi-send.
return uint64(lo.Max([]int{totalOperationsNum, 2})) * bankMultiSendPerOperationGas, true
}
}
Loading

0 comments on commit 7959543

Please sign in to comment.