Skip to content

Commit

Permalink
Merge branch 'master' into dzmitryhil/asset-ft-spendable
Browse files Browse the repository at this point in the history
  • Loading branch information
dzmitryhil authored Oct 26, 2023
2 parents 80d2cb3 + 6b189ce commit 4e04346
Show file tree
Hide file tree
Showing 8 changed files with 85 additions and 26 deletions.
8 changes: 0 additions & 8 deletions integration-tests/modules/assetft_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -977,8 +977,6 @@ func TestAssetFTBurn(t *testing.T) {
}

// TestAssetFTBurnRate tests burn rate functionality of fungible tokens.
//
//nolint:dupl
func TestAssetFTBurnRate(t *testing.T) {
t.Parallel()

Expand Down Expand Up @@ -1124,8 +1122,6 @@ func TestAssetFTBurnRate(t *testing.T) {
}

// TestAssetFTSendCommissionRate tests send commission rate functionality of fungible tokens.
//
//nolint:dupl
func TestAssetFTSendCommissionRate(t *testing.T) {
t.Parallel()

Expand Down Expand Up @@ -2042,8 +2038,6 @@ func TestSendCoreTokenWithRestrictedToken(t *testing.T) {
}

// TestNotEnoughBalanceForBurnRate checks tx will fail if there is not enough balance to cover burn rate.
//
//nolint:dupl // we expect code duplication in tests
func TestNotEnoughBalanceForBurnRate(t *testing.T) {
t.Parallel()

Expand Down Expand Up @@ -2121,8 +2115,6 @@ func TestNotEnoughBalanceForBurnRate(t *testing.T) {
}

// TestNotEnoughBalanceForCommissionRate checks tx will fail if there is not enough balance to cover commission rate.
//
//nolint:dupl // we expect code duplication in tests
func TestNotEnoughBalanceForCommissionRate(t *testing.T) {
t.Parallel()

Expand Down
83 changes: 83 additions & 0 deletions integration-tests/modules/gas_estimation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,13 @@ package modules
import (
"fmt"
"testing"
"time"

sdkmath "cosmossdk.io/math"
sdk "github.com/cosmos/cosmos-sdk/types"
authztypes "github.com/cosmos/cosmos-sdk/x/authz"
banktypes "github.com/cosmos/cosmos-sdk/x/bank/types"
"github.com/samber/lo"
"github.com/stretchr/testify/require"

integrationtests "github.com/CoreumFoundation/coreum/v3/integration-tests"
Expand Down Expand Up @@ -114,3 +117,83 @@ func TestBankSendEstimation(t *testing.T) {
fmt.Printf("%d\t%d\n", n, txRes.GasUsed)
}
}

// TestAuthzEstimation it estimates gas overhead required by authz message execution.
// It executes regular message first. Then the same message is executed using authz. By subtracting those values
// we know what the overhead of authz is.
// To get correct results, both authz and bank send must be temporarily configured as non-deterministic messages,
// to get real results.
func TestAuthzEstimation(t *testing.T) {
t.Parallel()

ctx, chain := integrationtests.NewCoreumTestingContext(t)

requireT := require.New(t)

granter := chain.GenAccount()
grantee := chain.GenAccount()
recipient1 := chain.GenAccount()
recipient2 := chain.GenAccount()

chain.Faucet.FundAccounts(ctx, t,
integration.FundedAccount{
Address: granter,
Amount: chain.NewCoin(sdk.NewInt(50000000)),
},
integration.FundedAccount{
Address: grantee,
Amount: chain.NewCoin(sdk.NewInt(50000000)),
},
)

// grant the authorization
grantMsg, err := authztypes.NewMsgGrant(
granter,
grantee,
authztypes.NewGenericAuthorization(sdk.MsgTypeURL(&banktypes.MsgSend{})),
lo.ToPtr(time.Now().Add(time.Minute)),
)
require.NoError(t, err)

_, err = client.BroadcastTx(
ctx,
chain.ClientContext.WithFromAddress(granter),
chain.TxFactory().WithGas(chain.GasLimitByMsgs(grantMsg)),
grantMsg,
)
requireT.NoError(err)

// execute regular message
amountToSend := sdkmath.NewInt(2_000)
txf := chain.TxFactory().WithSimulateAndExecute(true)
resRegular, err := client.BroadcastTx(
ctx,
chain.ClientContext.WithFromAddress(granter),
txf,
&banktypes.MsgSend{
FromAddress: granter.String(),
ToAddress: recipient1.String(),
Amount: sdk.NewCoins(chain.NewCoin(amountToSend)),
},
)
requireT.NoError(err)

// execute authz message
execMsg := authztypes.NewMsgExec(grantee, []sdk.Msg{
&banktypes.MsgSend{
FromAddress: granter.String(),
ToAddress: recipient2.String(),
Amount: sdk.NewCoins(chain.NewCoin(amountToSend)),
},
})

resAuthZ, err := client.BroadcastTx(
ctx,
chain.ClientContext.WithFromAddress(grantee),
txf,
&execMsg,
)
requireT.NoError(err)

fmt.Printf("Authz gas overhead: %d\n", resAuthZ.GasUsed-resRegular.GasUsed)
}
2 changes: 0 additions & 2 deletions x/asset/ft/keeper/keeper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -511,7 +511,6 @@ func TestKeeper_Burn(t *testing.T) {
requireT.ErrorIs(err, cosmoserrors.ErrInsufficientFunds)
}

//nolint:dupl // We don't care
func TestKeeper_BurnRate_BankSend(t *testing.T) {
requireT := require.New(t)

Expand Down Expand Up @@ -759,7 +758,6 @@ func TestKeeper_BurnRate_BankMultiSend(t *testing.T) {
}
}

//nolint:dupl // We don't care
func TestKeeper_SendCommissionRate_BankSend(t *testing.T) {
requireT := require.New(t)

Expand Down
2 changes: 0 additions & 2 deletions x/asset/ft/types/msgs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -305,7 +305,6 @@ func TestMsgUnfreeze_ValidateBasic(t *testing.T) {
}
}

//nolint:dupl // tests and mint tests are identical, but merging them is not beneficial
func TestMsgMint_ValidateBasic(t *testing.T) {
type M = types.MsgMint

Expand Down Expand Up @@ -352,7 +351,6 @@ func TestMsgMint_ValidateBasic(t *testing.T) {
}
}

//nolint:dupl // tests and mint tests are identical, but merging them is not beneficial
func TestMsgBurn_ValidateBasic(t *testing.T) {
type M = types.MsgBurn

Expand Down
2 changes: 0 additions & 2 deletions x/asset/ft/types/token_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -280,7 +280,6 @@ func TestValidateFeatures(t *testing.T) {
}
}

//nolint:dupl // We don't care
func TestValidateBurnRate(t *testing.T) {
testCases := []struct {
rate string
Expand Down Expand Up @@ -363,7 +362,6 @@ func TestValidateBurnRate(t *testing.T) {
}
}

//nolint:dupl // We don't care
func TestValidateSendCommissionRate(t *testing.T) {
testCases := []struct {
rate string
Expand Down
5 changes: 0 additions & 5 deletions x/asset/nft/types/msgs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -324,7 +324,6 @@ func TestMsgMint_ValidateBasic(t *testing.T) {
}
}

//nolint:dupl // test case duplicates are ok
func TestMsgBurn_ValidateBasic(t *testing.T) {
validMessage := types.MsgBurn{
Sender: "devcore172rc5sz2uclpsy3vvx3y79ah5dk450z5ruq2r5",
Expand Down Expand Up @@ -386,7 +385,6 @@ func TestMsgBurn_ValidateBasic(t *testing.T) {
}
}

//nolint:dupl // test case duplicates are ok
func TestMsgFreeze_ValidateBasic(t *testing.T) {
validMessage := types.MsgFreeze{
Sender: "devcore172rc5sz2uclpsy3vvx3y79ah5dk450z5ruq2r5",
Expand Down Expand Up @@ -448,7 +446,6 @@ func TestMsgFreeze_ValidateBasic(t *testing.T) {
}
}

//nolint:dupl // test case duplicates are ok
func TestMsgUnfreeze_ValidateBasic(t *testing.T) {
validMessage := types.MsgUnfreeze{
Sender: "devcore172rc5sz2uclpsy3vvx3y79ah5dk450z5ruq2r5",
Expand Down Expand Up @@ -510,7 +507,6 @@ func TestMsgUnfreeze_ValidateBasic(t *testing.T) {
}
}

//nolint:dupl // test case duplicates are ok
func TestMsgAddToWhitelist_ValidateBasic(t *testing.T) {
validMessage := types.MsgAddToWhitelist{
Sender: "devcore172rc5sz2uclpsy3vvx3y79ah5dk450z5ruq2r5",
Expand Down Expand Up @@ -582,7 +578,6 @@ func TestMsgAddToWhitelist_ValidateBasic(t *testing.T) {
}
}

//nolint:dupl // test case duplicates are ok
func TestMsgRemoveFromWhitelist_ValidateBasic(t *testing.T) {
validMessage := types.MsgRemoveFromWhitelist{
Sender: "devcore172rc5sz2uclpsy3vvx3y79ah5dk450z5ruq2r5",
Expand Down
7 changes: 1 addition & 6 deletions x/deterministicgas/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ import (
const (
BankSendPerCoinGas = 50000
BankMultiSendPerOperationsGas = 35000
AuthzExecOverhead = 2000
AuthzExecOverhead = 1500
)

type (
Expand Down Expand Up @@ -95,11 +95,6 @@ func DefaultConfig() Config {
MsgToMsgURL(&assetnfttypes.MsgRemoveFromClassWhitelist{}): constantGasFunc(3500),

// authz
// FIXME (v47-deterministic): We need a procedure to estimate the overhead of the authz. Proposal:
// 1. Estimate normal message
// 2. Estimate the same message executed using authz
// 3. Subtract one from the other
// We should have an integration test doing this.
MsgToMsgURL(&authz.MsgExec{}): cfg.authzMsgExecGasFunc(AuthzExecOverhead),
MsgToMsgURL(&authz.MsgGrant{}): constantGasFunc(28000),
MsgToMsgURL(&authz.MsgRevoke{}): constantGasFunc(8000),
Expand Down
2 changes: 1 addition & 1 deletion x/deterministicgas/spec/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ Real examples of special case tests could be found [here](https://github.com/Cor

`DeterministicGasForMsg = authzMsgExecOverhead + Sum(DeterministicGas(ChildMsg))`

`authzMsgExecOverhead` is currently equal to `2000`.
`authzMsgExecOverhead` is currently equal to `1500`.

### Nondeterministic messages

Expand Down

0 comments on commit 4e04346

Please sign in to comment.