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(accounts): fix integration tests #22418

Merged
merged 8 commits into from
Nov 11, 2024
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 15 additions & 8 deletions simapp/app_di.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ import (
basedepinject "cosmossdk.io/x/accounts/defaults/base/depinject"
lockupdepinject "cosmossdk.io/x/accounts/defaults/lockup/depinject"
multisigdepinject "cosmossdk.io/x/accounts/defaults/multisig/depinject"
"cosmossdk.io/x/accounts/testing/account_abstraction"
"cosmossdk.io/x/accounts/testing/counter"
bankkeeper "cosmossdk.io/x/bank/keeper"
circuitkeeper "cosmossdk.io/x/circuit/keeper"
consensuskeeper "cosmossdk.io/x/consensus/keeper"
Expand Down Expand Up @@ -183,6 +185,10 @@ func NewSimApp(
// return fmt.Errorf("invalid pub key size")
// }
// })

// TESTING: do not add below account types
counter.ProvideAccount,
account_abstraction.ProvideAccount,
),
)
)
Expand Down Expand Up @@ -299,14 +305,15 @@ func (app *SimApp) setCustomAnteHandler() {
anteHandler, err := NewAnteHandler(
HandlerOptions{
ante.HandlerOptions{
AccountKeeper: app.AuthKeeper,
BankKeeper: app.BankKeeper,
ConsensusKeeper: app.ConsensusParamsKeeper,
SignModeHandler: app.txConfig.SignModeHandler(),
FeegrantKeeper: app.FeeGrantKeeper,
SigGasConsumer: ante.DefaultSigVerificationGasConsumer,
UnorderedTxManager: app.UnorderedTxManager,
Environment: app.AuthKeeper.Environment,
AccountKeeper: app.AuthKeeper,
BankKeeper: app.BankKeeper,
ConsensusKeeper: app.ConsensusParamsKeeper,
SignModeHandler: app.txConfig.SignModeHandler(),
FeegrantKeeper: app.FeeGrantKeeper,
SigGasConsumer: ante.DefaultSigVerificationGasConsumer,
UnorderedTxManager: app.UnorderedTxManager,
Environment: app.AuthKeeper.Environment,
AccountAbstractionKeeper: app.AccountsKeeper,
},
&app.CircuitBreakerKeeper,
},
Expand Down
21 changes: 19 additions & 2 deletions tests/integration/accounts/base_account_test.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
//go:build app_v1

package accounts

import (
Expand All @@ -15,12 +13,18 @@ import (
banktypes "cosmossdk.io/x/bank/types"

codectypes "github.com/cosmos/cosmos-sdk/codec/types"
"github.com/cosmos/cosmos-sdk/crypto/keys/secp256k1"
cryptotypes "github.com/cosmos/cosmos-sdk/crypto/types"
"github.com/cosmos/cosmos-sdk/testutil/sims"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/stretchr/testify/require"
)

var (
privKey = secp256k1.GenPrivKey()
accCreator = []byte("creator")
)

Comment on lines +23 to +26
Copy link
Contributor

Choose a reason for hiding this comment

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

🛠️ Refactor suggestion

Consider improving test determinism and maintainability.

The current implementation has several potential issues:

  1. Using GenPrivKey() at the package level could lead to non-deterministic tests. Consider moving key generation into a test setup function with a fixed seed.
  2. The magic string "creator" should be documented or defined as a named constant.
  3. Global variables can make tests harder to maintain and understand. Consider using a test fixture struct.

Here's a suggested refactor:

-var (
-    privKey    = secp256k1.GenPrivKey()
-    accCreator = []byte("creator")
-)

+const (
+    testCreatorID = "creator"
+)
+
+type testFixture struct {
+    privKey    cryptotypes.PrivKey
+    accCreator []byte
+}
+
+func setupTestFixture(t *testing.T) *testFixture {
+    t.Helper()
+    return &testFixture{
+        privKey:    secp256k1.GenPrivKeyFromSecret([]byte("fixed_test_seed")),
+        accCreator: []byte(testCreatorID),
+    }
+}
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
var (
privKey = secp256k1.GenPrivKey()
accCreator = []byte("creator")
)
const (
testCreatorID = "creator"
)
type testFixture struct {
privKey cryptotypes.PrivKey
accCreator []byte
}
func setupTestFixture(t *testing.T) *testFixture {
t.Helper()
return &testFixture{
privKey: secp256k1.GenPrivKeyFromSecret([]byte("fixed_test_seed")),
accCreator: []byte(testCreatorID),
}
}

func TestBaseAccount(t *testing.T) {
app := setupApp(t)
ak := app.AccountsKeeper
Expand Down Expand Up @@ -95,3 +99,16 @@ func toAnyPb(t *testing.T, pm gogoproto.Message) *codectypes.Any {
require.NoError(t, err)
return pb
}

func coins(t *testing.T, s string) sdk.Coins {
t.Helper()
coins, err := sdk.ParseCoinsNormalized(s)
require.NoError(t, err)
return coins
}

func setupApp(t *testing.T) *simapp.SimApp {
t.Helper()
app := simapp.Setup(t, false)
return app
}
2 changes: 0 additions & 2 deletions tests/integration/accounts/wiring_test.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
//go:build app_v1

package accounts

import (
Expand Down
4 changes: 4 additions & 0 deletions x/accounts/testing/account_abstraction/minimal.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,3 +74,7 @@ func (a MinimalAbstractedAccount) RegisterExecuteHandlers(builder *accountstd.Ex
func (a MinimalAbstractedAccount) RegisterQueryHandlers(builder *accountstd.QueryBuilder) {
accountstd.RegisterQueryHandler(builder, a.QueryAuthenticateMethods) // implements account_abstraction
}

func ProvideAccount() accountstd.DepinjectAccount {
return accountstd.DIAccount("aa_minimal", NewMinimalAbstractedAccount)
}
4 changes: 4 additions & 0 deletions x/accounts/testing/counter/counter.go
Original file line number Diff line number Diff line change
Expand Up @@ -149,3 +149,7 @@ func (a Account) RegisterExecuteHandlers(builder *accountstd.ExecuteBuilder) {
func (a Account) RegisterQueryHandlers(builder *accountstd.QueryBuilder) {
accountstd.RegisterQueryHandler(builder, a.QueryCounter)
}

func ProvideAccount() accountstd.DepinjectAccount {
return accountstd.DIAccount("counter", NewAccount)
}
17 changes: 9 additions & 8 deletions x/validate/depinject.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,14 +138,15 @@ func newBaseAppOption(in ModuleInputs) func(app *baseapp.BaseApp) {
func newAnteHandler(in ModuleInputs) (sdk.AnteHandler, error) {
anteHandler, err := ante.NewAnteHandler(
ante.HandlerOptions{
Environment: in.Environment,
AccountKeeper: in.AccountKeeper,
ConsensusKeeper: in.ConsensusKeeper,
BankKeeper: in.BankKeeper,
SignModeHandler: in.TxConfig.SignModeHandler(),
FeegrantKeeper: in.FeeGrantKeeper,
SigGasConsumer: ante.DefaultSigVerificationGasConsumer,
UnorderedTxManager: in.UnorderedTxManager,
Environment: in.Environment,
AccountKeeper: in.AccountKeeper,
ConsensusKeeper: in.ConsensusKeeper,
BankKeeper: in.BankKeeper,
SignModeHandler: in.TxConfig.SignModeHandler(),
FeegrantKeeper: in.FeeGrantKeeper,
SigGasConsumer: ante.DefaultSigVerificationGasConsumer,
UnorderedTxManager: in.UnorderedTxManager,
AccountAbstractionKeeper: in.AccountAbstractionKeeper,
Copy link
Member

Choose a reason for hiding this comment

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

nice catch!

},
)
if err != nil {
Expand Down
Loading