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

test: migrate e2e/genutil to systemtest #22325

Merged
merged 16 commits into from
Oct 25, 2024
67 changes: 67 additions & 0 deletions tests/systemtests/export_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
//go:build system_test

package systemtests

import (
"fmt"
"os"
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/tidwall/gjson"
)

func TestExportCmd(t *testing.T) {
// scenario: test bank send command
// given a running chain

sut.ResetChain(t)
cli := NewCLIWrapper(t, sut, verbose)
exportFile := "foobar.json"

sut.StartChain(t)

testCases := []struct {
name string
args []string
expErr bool
errMsg string
expZeroHeight bool
}{
{"invalid home dir", []string{"genesis", "export", "--home=foo"}, true, "no such file or directory", false},
{"should export correct height", []string{"genesis", "export"}, false, "", false},
{"should export correct height with --height", []string{"genesis", "export", "--height=5"}, false, "", false},
{"should export height 0 with --for-zero-height", []string{"genesis", "export", "--for-zero-height=true"}, false, "", true},
{"should export state to the specified file", []string{"genesis", "export", fmt.Sprintf("--output-document=%s", exportFile)}, false, "", false},
}

for _, tc := range testCases {

// fmt.Println(tc.name, res)
if tc.expErr {
assertOutput := func(_ assert.TestingT, gotErr error, gotOutputs ...interface{}) bool {
require.Contains(t, gotOutputs[0], tc.errMsg)
return false
}
cli.WithRunErrorMatcher(assertOutput).RunCommandWithArgs(tc.args...)
} else {
res := cli.RunCommandWithArgs(tc.args...)
if res == "" {
require.FileExists(t, exportFile)
os.Remove(exportFile)
} else {
height := gjson.Get(res, "initial_height").Int()
if tc.expZeroHeight {
require.Equal(t, height, int64(0))
} else {
require.Greater(t, height, int64(0))
}

// Check consensus params of exported state
maxGas := gjson.Get(res, "consensus.params.block.max_gas").Int()
require.Equal(t, maxGas, int64(MaxGas))
}
}
}
}
4 changes: 3 additions & 1 deletion tests/systemtests/system.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ var (

// ExecBinaryUnversionedRegExp regular expression to extract the unversioned binary name
ExecBinaryUnversionedRegExp = regexp.MustCompile(`^(\w+)-?.*$`)

MaxGas = 10_000_000
)

type TestnetInitializer interface {
Expand Down Expand Up @@ -130,7 +132,7 @@ func (s *SystemUnderTest) SetupChain() {
panic(fmt.Sprintf("failed to load genesis: %s", err))
}

genesisBz, err = sjson.SetRawBytes(genesisBz, "consensus.params.block.max_gas", []byte(fmt.Sprintf(`"%d"`, 10_000_000)))
genesisBz, err = sjson.SetRawBytes(genesisBz, "consensus.params.block.max_gas", []byte(fmt.Sprintf(`"%d"`, MaxGas)))
if err != nil {
panic(fmt.Sprintf("failed to set block max gas: %s", err))
}
Comment on lines +135 to 138
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codebase verification

Inconsistent Usage of Gas Limits Detected

Several hardcoded gas limits found alongside MaxGas usage may lead to inconsistencies.

  • x/simulation/simulate.go:40-45
  • types/tx/types.go:18-19,61-64
  • server/v2/cometbft/abci_test.go:37,335,374,638
  • baseapp/abci_utils.go:262,415
🔗 Analysis chain

Usage of MaxGas constant in SetupChain method

The MaxGas constant is now used to set the maximum gas limit in the genesis block configuration. This change improves code consistency and makes it easier to modify the gas limit across the system if needed.

However, to ensure this change doesn't have unintended consequences, we should verify its usage across the codebase.

🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Check for other occurrences of hardcoded gas limits that might need updating

# Search for potential hardcoded gas limits
rg -n '(?i)gas.*limit.*=.*\d{7,}' --type go

# Search for other uses of MaxGas to ensure consistent usage
rg -n 'MaxGas' --type go

Length of output: 5558

Expand Down
Loading