Skip to content

Commit

Permalink
test: x/accounts systemtests (#22320)
Browse files Browse the repository at this point in the history
  • Loading branch information
lucaslopezf authored Oct 23, 2024
1 parent 1c949f7 commit 8fa3090
Show file tree
Hide file tree
Showing 6 changed files with 147 additions and 7 deletions.
9 changes: 9 additions & 0 deletions simapp/simd/cmd/root_di.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ import (
"cosmossdk.io/depinject"
"cosmossdk.io/log"
"cosmossdk.io/simapp"
basedepinject "cosmossdk.io/x/accounts/defaults/base/depinject"
lockupdepinject "cosmossdk.io/x/accounts/defaults/lockup/depinject"
multisigdepinject "cosmossdk.io/x/accounts/defaults/multisig/depinject"

"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/client/config"
Expand All @@ -42,6 +45,12 @@ func NewRootCmd() *cobra.Command {
depinject.Supply(log.NewNopLogger()),
depinject.Provide(
ProvideClientContext,
multisigdepinject.ProvideAccount,
basedepinject.ProvideAccount,
lockupdepinject.ProvideAllLockupAccounts,

// provide base account options
basedepinject.ProvideSecp256K1PubKey,
),
),
&autoCliOpts,
Expand Down
13 changes: 12 additions & 1 deletion simapp/v2/simdv2/cmd/root_di.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ import (
"cosmossdk.io/log"
"cosmossdk.io/runtime/v2"
"cosmossdk.io/simapp/v2"
basedepinject "cosmossdk.io/x/accounts/defaults/base/depinject"
lockupdepinject "cosmossdk.io/x/accounts/defaults/lockup/depinject"
multisigdepinject "cosmossdk.io/x/accounts/defaults/multisig/depinject"

"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/client/config"
Expand All @@ -36,7 +39,15 @@ func NewRootCmd[T transaction.Tx]() *cobra.Command {
if err := depinject.Inject(
depinject.Configs(
simapp.AppConfig(),
depinject.Provide(ProvideClientContext),
depinject.Provide(
ProvideClientContext,
// inject desired account types:
multisigdepinject.ProvideAccount,
basedepinject.ProvideAccount,
lockupdepinject.ProvideAllLockupAccounts,

// provide base account options
basedepinject.ProvideSecp256K1PubKey),
depinject.Supply(log.NewNopLogger()),
),
&autoCliOpts,
Expand Down
102 changes: 102 additions & 0 deletions tests/systemtests/account_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package systemtests

import (
"fmt"
"strings"
"testing"

Expand Down Expand Up @@ -51,3 +52,104 @@ func TestAccountCreation(t *testing.T) {
rsp5 := cli.CustomQuery("q", "auth", "account", account1Addr)
assert.Equal(t, "1", gjson.Get(rsp5, "account.value.sequence").String(), rsp5)
}

func TestAccountsMigration(t *testing.T) {
sut.ResetChain(t)
cli := NewCLIWrapper(t, sut, verbose)

legacyAddress := cli.GetKeyAddr(defaultSrcAddr)
// Create a receiver account
receiverName := "receiver-account"
receiverAddress := cli.AddKey(receiverName)
require.NotEmpty(t, receiverAddress)
sut.ModifyGenesisCLI(t,
[]string{"genesis", "add-genesis-account", receiverAddress, "1000000stake"},
)

sut.StartChain(t)

// Get pubkey
pubKeyValue := cli.GetPubKeyByCustomField(legacyAddress, "address")
require.NotEmpty(t, pubKeyValue, "Public key for legacy-account not found")

// 1. Verify the account
rsp := cli.CustomQuery("q", "auth", "account", legacyAddress)
require.NotEmpty(t, rsp)
accountInfo := gjson.Parse(rsp)
addressFromResponse := accountInfo.Get("account.value.address").String()
require.Equal(t, legacyAddress, addressFromResponse, "The address in the response should match the legacy address")

// 2. Migrate this account from x/auth to x/accounts

// Verify the account not exist in account
rsp = cli.WithRunErrorsIgnored().CustomQuery("q", "accounts", "query", legacyAddress, "cosmos.accounts.defaults.base.v1.QuerySequence", "{}")
require.Contains(t, rsp, "not found: key")

accountInitMsg := fmt.Sprintf(`{
"@type": "/cosmos.accounts.defaults.base.v1.MsgInit",
"pub_key": {
"@type": "/cosmos.crypto.secp256k1.PubKey",
"key": "%s"
},
"init_sequence": "0"
}`, pubKeyValue)

rsp = cli.RunAndWait("tx", "auth", "migrate-account",
"--account-type=base",
fmt.Sprintf("--account-init-msg=%s", accountInitMsg),
fmt.Sprintf("--from=%s", legacyAddress),
"--fees=1stake")
RequireTxSuccess(t, rsp)

// 3. Now the account should be existed, query the account Sequence
rsp = cli.CustomQuery("q", "accounts", "query", legacyAddress, "cosmos.accounts.defaults.base.v1.QuerySequence", "{}")
sequence := gjson.Get(rsp, "response.sequence").Exists()
require.True(t, sequence, "Sequence field should exist")

// 4. Execute a transaction using the bank module

// Check initial balances
legacyBalance := cli.QueryBalance(legacyAddress, "stake")
receiverBalance := cli.QueryBalance(receiverAddress, "stake")
require.Equal(t, int64(399999999), legacyBalance) // 20000000 - 1 (fee for migration)
require.Equal(t, int64(1000000), receiverBalance)

transferAmount := "1000000"
rsp = cli.RunAndWait("tx", "bank", "send",
legacyAddress,
receiverAddress,
transferAmount+"stake",
fmt.Sprintf("--from=%s", legacyAddress),
"--fees=1stake")
RequireTxSuccess(t, rsp)

// Verify the balances after the transaction
newLegacyBalance := cli.QueryBalance(legacyAddress, "stake")
newReceiverBalance := cli.QueryBalance(receiverAddress, "stake")

expectedLegacyBalance := legacyBalance - 1000000 - 1 // Initial balance - transferred amount - fee
expectedReceiverBalance := receiverBalance + 1000000 // Initial balance + transferred amount

require.Equal(t, expectedLegacyBalance, newLegacyBalance, "Legacy account balance is incorrect after transfer")
require.Equal(t, expectedReceiverBalance, newReceiverBalance, "Receiver account balance is incorrect after transfer")

// 5. Test swapKey functionality
newKeyName := "new-key"
newKeyAddress := cli.AddKey(newKeyName)
require.NotEmpty(t, newKeyAddress)

newPubKey := cli.GetPubKeyByCustomField(newKeyAddress, "address")
require.NotEmpty(t, newPubKey, "Public key for new-key not found")

swapKeyMsg := fmt.Sprintf(`{
"new_pub_key": {
"@type": "/cosmos.crypto.secp256k1.PubKey",
"key": "%s"
}
}`, newPubKey)

rsp = cli.RunAndWait("tx", "accounts", "execute", legacyAddress, "cosmos.accounts.defaults.base.v1.MsgSwapPubKey", swapKeyMsg,
fmt.Sprintf("--from=%s", legacyAddress),
"--fees=1stake")
RequireTxSuccess(t, rsp)
}
17 changes: 17 additions & 0 deletions tests/systemtests/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -331,6 +331,23 @@ func (c CLIWrapper) GetKeyAddrPrefix(name, prefix string) string {
return addr
}

// GetPubKeyByCustomField returns pubkey in base64 by custom field
func (c CLIWrapper) GetPubKeyByCustomField(addr, field string) string {
keysListOutput := c.Keys("keys", "list")
keysList := gjson.Parse(keysListOutput)

var pubKeyValue string
keysList.ForEach(func(_, value gjson.Result) bool {
if value.Get(field).String() == addr {
pubKeyJSON := gjson.Parse(value.Get("pubkey").String())
pubKeyValue = pubKeyJSON.Get("key").String()
return false
}
return true
})
return pubKeyValue
}

const defaultSrcAddr = "node0"

// FundAddress sends the token amount to the destination address
Expand Down
1 change: 1 addition & 0 deletions x/accounts/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (

gogoproto "github.com/cosmos/gogoproto/proto"

_ "cosmossdk.io/api/cosmos/accounts/defaults/base/v1" // import for side-effects
"cosmossdk.io/collections"
"cosmossdk.io/core/address"
"cosmossdk.io/core/appmodule"
Expand Down
12 changes: 6 additions & 6 deletions x/accounts/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,21 +42,21 @@ type AppModule struct {
k Keeper
}

func (m AppModule) IsAppModule() {}
func (AppModule) IsAppModule() {}

// Name returns the module's name.
// Deprecated: kept for legacy reasons.
func (AppModule) Name() string { return ModuleName }
func (am AppModule) Name() string { return ModuleName }

func (m AppModule) RegisterInterfaces(registrar registry.InterfaceRegistrar) {
func (AppModule) RegisterInterfaces(registrar registry.InterfaceRegistrar) {
msgservice.RegisterMsgServiceDesc(registrar, v1.MsgServiceDesc())
}

// App module services

func (m AppModule) RegisterServices(registar grpc.ServiceRegistrar) error {
v1.RegisterQueryServer(registar, NewQueryServer(m.k))
v1.RegisterMsgServer(registar, NewMsgServer(m.k))
func (am AppModule) RegisterServices(registrar grpc.ServiceRegistrar) error {
v1.RegisterQueryServer(registrar, NewQueryServer(am.k))
v1.RegisterMsgServer(registrar, NewMsgServer(am.k))

return nil
}
Expand Down

0 comments on commit 8fa3090

Please sign in to comment.