Skip to content
Merged
Show file tree
Hide file tree
Changes from 6 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
108 changes: 108 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,110 @@ 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)

// Create legacy-account with some tokens
legacyName := "legacy-account"
legacyAddress := cli.AddKey(legacyName)
require.NotEmpty(t, legacyAddress)

// Create a receiver account
receiverName := "receiver-account"
receiverAddress := cli.AddKey(receiverName)
require.NotEmpty(t, receiverAddress)

sut.ModifyGenesisCLI(t,
[]string{"genesis", "add-genesis-account", legacyAddress, "20000000stake"},
[]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, "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(19999999), 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 := int64(19999999 - 1000000 - 1) // Initial balance - transferred amount - fee
expectedReceiverBalance := int64(1000000 + 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
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