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

Add CalculateGas integration-tests & improve deterministicgas docs #706

Merged
merged 17 commits into from
Nov 13, 2023
3 changes: 1 addition & 2 deletions integration-tests/modules/bank_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -380,11 +380,10 @@ func TestBankSendGasEstimation(t *testing.T) {
Amount: sdk.NewCoins(chain.NewCoin(amountToSend)),
}

clientCtx := chain.ClientContext.WithFromAddress(sender)
bankSendGas := chain.GasLimitByMsgs(&banktypes.MsgSend{})
_, estimatedGas, err := client.CalculateGas(
ctx,
clientCtx,
chain.ClientContext.WithFromAddress(sender),
chain.TxFactory().
WithGas(bankSendGas),
msg)
Expand Down
84 changes: 84 additions & 0 deletions integration-tests/pkg/client_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
//go:build integrationtests

package pkg

import (
"fmt"
"testing"

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

integrationtests "github.com/CoreumFoundation/coreum/v3/integration-tests"
"github.com/CoreumFoundation/coreum/v3/pkg/client"
"github.com/CoreumFoundation/coreum/v3/testutil/integration"
)

func TestCalculateGas(t *testing.T) {
t.Parallel()

ctx, chain := integrationtests.NewCoreumTestingContext(t)

sender := chain.GenAccount()

multisigPublicKey, keyNamesSet, err := chain.GenMultisigAccount(4, 3)
require.NoError(t, err)

multisigAddress := sdk.AccAddress(multisigPublicKey.Address())
_ = keyNamesSet
//signer1KeyName := keyNamesSet[0]
//signer2KeyName := keyNamesSet[1]

chain.FundAccountWithOptions(ctx, t, sender, integration.BalancesOptions{Amount: sdkmath.NewInt(1)})
chain.FundAccountWithOptions(ctx, t, multisigAddress, integration.BalancesOptions{Amount: sdkmath.NewInt(1)})

tests := []struct {
name string
fromAddress sdk.AccAddress
msgs []sdk.Msg
expectedGas int
}{
{
name: "single address send",
fromAddress: sender,
msgs: []sdk.Msg{
&banktypes.MsgSend{
FromAddress: sender.String(),
ToAddress: sender.String(),
Amount: sdk.NewCoins(chain.NewCoin(sdkmath.NewInt(1))),
},
},
expectedGas: 65_000 + 1*50_000 + 0*10 + (1-1)*1000,
},
{
name: "multisig bank send",
fromAddress: multisigAddress,
msgs: []sdk.Msg{
&banktypes.MsgSend{
FromAddress: multisigAddress.String(),
ToAddress: multisigAddress.String(),
Amount: sdk.NewCoins(chain.NewCoin(sdkmath.NewInt(1))),
},
},
expectedGas: 65_000 + 1*50_000 + 0*10 + (3-1)*1000, // extra 2 signatures
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
_, estimatedGas, err := client.CalculateGas(
ctx,
chain.ClientContext.WithFromAddress(test.fromAddress),
chain.TxFactory(),
test.msgs...,
)
chainEstimatedGas := chain.GasLimitByMsgs(test.msgs...)

fmt.Printf("estimatedGas: %v\n", estimatedGas)
fmt.Printf("chainEstimatedGas: %v\n", chainEstimatedGas)
require.NoError(t, err)
require.EqualValues(t, test.expectedGas, int(estimatedGas))
})
}
}
5 changes: 3 additions & 2 deletions pkg/client/tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,8 +104,9 @@ func BroadcastTx(ctx context.Context, clientCtx Context, txf Factory, msgs ...sd

// CalculateGas simulates the execution of a transaction and returns the
// simulation response obtained by the query and the adjusted gas amount.
//
// FIXME(v47-multisig-calculate-gas-test) add test to calculate
// The main differences between our version and the one from cosmos-sdk are:
// - we respect context.Context
// - it correctly works when estimating for multisig accounts
func CalculateGas(ctx context.Context, clientCtx Context, txf Factory, msgs ...sdk.Msg) (*sdktx.SimulateResponse, uint64, error) {
txf, err := prepareFactory(ctx, clientCtx, txf)
if err != nil {
Expand Down
4 changes: 2 additions & 2 deletions x/deterministicgas/spec/README.tmpl.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ Let's say we have a transaction with 1 messages of type
signatures and the tx size is 1000 bytes, total will be:

`
TotalGas = {{ .FixedGas }} + 1 * {{ .BankSendPerCoinGas }} + 1 * {{ .SigVerifyCost }} + max(0, 1000-{{ .FreeBytes }}) * {{ .TxSizeCostPerByte }}
TotalGas = {{ .FixedGas }} + 1 * {{ .BankSendPerCoinGas }} + (1 - 1) * {{ .SigVerifyCost }} + max(0, 1000-{{ .FreeBytes }}) * {{ .TxSizeCostPerByte }}
`

#### Example 2
Expand All @@ -58,7 +58,7 @@ Let's say we have a transaction with 2 messages of type
signatures and the tx size is 2050 bytes, total will be:

`
TotalGas = {{ .FixedGas }} + 2 * {{ .MsgIssueGasPrice }} + 2 * {{ .SigVerifyCost }} + max(0, 2050-{{ .FreeBytes }}) * {{ .TxSizeCostPerByte }}
TotalGas = {{ .FixedGas }} + 2 * {{ .MsgIssueGasPrice }} + (2 - 1) * {{ .SigVerifyCost }} + max(0, 2050-{{ .FreeBytes }}) * {{ .TxSizeCostPerByte }}
`

## Gas Tables
Expand Down
Loading