Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ Ref: https://keepachangelog.com/en/1.0.0/

- (store) [#12](https://github.com/EscanBE/evermint/pull/12) Add local `snapshots` management commands
- (store) [#14](https://github.com/EscanBE/evermint/pull/14) Add `inspect` command and sub-commands
- (test+rpc) [#74](https://github.com/EscanBE/evermint/pull/74) Add integration test util + add IT skeleton for Json-RPC

### Improvement

Expand Down
12 changes: 12 additions & 0 deletions integration_test_util/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
### Integration Test Suite utility
was created with aims to:
- Provide a simple way to set up and run integration tests
- Able to run integration tests in parallel
- Environment nearest to E2E testing
- Initialize a chain with CometBFT node to fully functional testing
- Init chain with pre-defined set of validators and wallets, easier to trace and debug
- Able to set up test for Json-RPC server

Weak points:
- Only support Linux & MacOS, possible to make it compatible with Windows by enhance the TemporaryHolder functionality
- Easy to get import circle error, need a dedicated folder for integration test
185 changes: 185 additions & 0 deletions integration_test_util/accounts.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,185 @@
package integration_test_util

//goland:noinspection GoSnakeCaseUsage,SpellCheckingInspection
import (
"github.com/EscanBE/evermint/v12/crypto/ethsecp256k1"
etherminthd "github.com/EscanBE/evermint/v12/crypto/hd"
itutiltypes "github.com/EscanBE/evermint/v12/integration_test_util/types"
cosmoshd "github.com/cosmos/cosmos-sdk/crypto/hd"
"github.com/cosmos/cosmos-sdk/crypto/keyring"
"github.com/stretchr/testify/require"
"testing"
)

// newValidatorAccounts inits and return predefined validator accounts.
// By defining this, data in test cases will be more consistency.
func newValidatorAccounts(t *testing.T) itutiltypes.TestAccounts {
val1Account := newTestAccountFromMnemonic(t, IT_VAL_1_MNEMONIC)
val1Account.Type = itutiltypes.TestAccountTypeValidator
require.Equal(t, IT_VAL_1_VAL_ADDR, val1Account.GetValidatorAddress().String())
require.Equal(t, IT_VAL_1_CONS_ADDR, val1Account.GetConsensusAddress().String())
require.Equal(t, IT_VAL_1_ADDR, val1Account.GetCosmosAddress().String())

val2Account := newTestAccountFromMnemonic(t, IT_VAL_2_MNEMONIC)
val2Account.Type = itutiltypes.TestAccountTypeValidator
require.Equal(t, IT_VAL_2_VAL_ADDR, val2Account.GetValidatorAddress().String())
require.Equal(t, IT_VAL_2_CONS_ADDR, val2Account.GetConsensusAddress().String())
require.Equal(t, IT_VAL_2_ADDR, val2Account.GetCosmosAddress().String())

val3Account := newTestAccountFromMnemonic(t, IT_VAL_3_MNEMONIC)
val3Account.Type = itutiltypes.TestAccountTypeValidator
require.Equal(t, IT_VAL_3_VAL_ADDR, val3Account.GetValidatorAddress().String())
require.Equal(t, IT_VAL_3_CONS_ADDR, val3Account.GetConsensusAddress().String())
require.Equal(t, IT_VAL_3_ADDR, val3Account.GetCosmosAddress().String())

val4Account := newTestAccountFromMnemonic(t, IT_VAL_4_MNEMONIC)
val4Account.Type = itutiltypes.TestAccountTypeValidator
require.Equal(t, IT_VAL_4_VAL_ADDR, val4Account.GetValidatorAddress().String())
require.Equal(t, IT_VAL_4_CONS_ADDR, val4Account.GetConsensusAddress().String())
require.Equal(t, IT_VAL_4_ADDR, val4Account.GetCosmosAddress().String())

val5Account := newTestAccountFromMnemonic(t, IT_VAL_5_MNEMONIC)
val5Account.Type = itutiltypes.TestAccountTypeValidator
require.Equal(t, IT_VAL_5_VAL_ADDR, val5Account.GetValidatorAddress().String())
require.Equal(t, IT_VAL_5_CONS_ADDR, val5Account.GetConsensusAddress().String())
require.Equal(t, IT_VAL_5_ADDR, val5Account.GetCosmosAddress().String())

return []*itutiltypes.TestAccount{
val1Account,
val2Account,
val3Account,
val4Account,
val5Account,
}
}

// newWalletsAccounts inits and return predefined wallet accounts.
// By defining this, data in test cases will be more consistency.
func newWalletsAccounts(t *testing.T) itutiltypes.TestAccounts {
wal1Account := newTestAccountFromMnemonic(t, IT_WAL_1_MNEMONIC)
wal1Account.Type = itutiltypes.TestAccountTypeWallet
require.Equal(t, IT_WAL_1_ETH_ADDR, wal1Account.GetEthAddress().String())
require.Equal(t, IT_WAL_1_ADDR, wal1Account.GetCosmosAddress().String())

wal2Account := newTestAccountFromMnemonic(t, IT_WAL_2_MNEMONIC)
wal2Account.Type = itutiltypes.TestAccountTypeWallet
require.Equal(t, IT_WAL_2_ETH_ADDR, wal2Account.GetEthAddress().String())
require.Equal(t, IT_WAL_2_ADDR, wal2Account.GetCosmosAddress().String())

wal3Account := newTestAccountFromMnemonic(t, IT_WAL_3_MNEMONIC)
wal3Account.Type = itutiltypes.TestAccountTypeWallet
require.Equal(t, IT_WAL_3_ETH_ADDR, wal3Account.GetEthAddress().String())
require.Equal(t, IT_WAL_3_ADDR, wal3Account.GetCosmosAddress().String())

wal4Account := newTestAccountFromMnemonic(t, IT_WAL_4_MNEMONIC)
wal4Account.Type = itutiltypes.TestAccountTypeWallet
require.Equal(t, IT_WAL_4_ETH_ADDR, wal4Account.GetEthAddress().String())
require.Equal(t, IT_WAL_4_ADDR, wal4Account.GetCosmosAddress().String())

wal5Account := newTestAccountFromMnemonic(t, IT_WAL_5_MNEMONIC)
wal5Account.Type = itutiltypes.TestAccountTypeWallet
require.Equal(t, IT_WAL_5_ETH_ADDR, wal5Account.GetEthAddress().String())
require.Equal(t, IT_WAL_5_ADDR, wal5Account.GetCosmosAddress().String())

return []*itutiltypes.TestAccount{
wal1Account,
wal2Account,
wal3Account,
wal4Account,
wal5Account,
}
}

// newTestAccountFromMnemonic creates a new test account from a mnemonic.
func newTestAccountFromMnemonic(t *testing.T, mnemonic string) *itutiltypes.TestAccount {
var algo keyring.SignatureAlgo
var err error

//goland:noinspection SpellCheckingInspection
algo, err = keyring.NewSigningAlgoFromString("eth_secp256k1", supportedKeyringAlgorithms)

derivedPriv, err := algo.Derive()(mnemonic, "", hdPath)

privKey := algo.Generate()(derivedPriv)
require.NoError(t, err)

priv := &ethsecp256k1.PrivKey{
Key: privKey.Bytes(),
}

return NewTestAccount(t, priv)
}

// NewTestAccount creates a new test account. If the private key is not provided, a new one will be generated.
func NewTestAccount(t *testing.T, nilAblePrivKey *ethsecp256k1.PrivKey) *itutiltypes.TestAccount {
testAccount := &itutiltypes.TestAccount{}

var err error

if nilAblePrivKey == nil {
nilAblePrivKey, err = ethsecp256k1.GenerateKey()
require.NoError(t, err)
require.NotNil(t, nilAblePrivKey)
}

testAccount.PrivateKey = nilAblePrivKey
testAccount.Signer = itutiltypes.NewSigner(nilAblePrivKey)

return testAccount
}

var supportedKeyringAlgorithms = keyring.SigningAlgoList{etherminthd.EthSecp256k1, cosmoshd.Secp256k1}
var hdPath = cosmoshd.CreateHDPath(60, 0, 0).String()

// TODO implement rename-chain compatible

//goland:noinspection GoSnakeCaseUsage,SpellCheckingInspection
const (
IT_VAL_1_ADDR = "evm1cqetlv987ntelz7s6ntvv95ltrns9qt6lqulcz"
IT_VAL_1_VAL_ADDR = "evmvaloper1cqetlv987ntelz7s6ntvv95ltrns9qt6et40np"
IT_VAL_1_CONS_ADDR = "evmvalcons1vv3kjxtrh7jredjehk5xw66r62euensst2lxka"
IT_VAL_1_MNEMONIC = "camera foster skate whisper faith opera axis false van urban clean pet shove census surface injury phone alley cup school pet edge trial pony"

IT_VAL_2_ADDR = "evm19k6gu9tkr40uyhf86sjmlgy6hu4lpfx40p482t"
IT_VAL_2_VAL_ADDR = "evmvaloper19k6gu9tkr40uyhf86sjmlgy6hu4lpfx4f2uhpg"
IT_VAL_2_CONS_ADDR = "evmvalcons19fphsrnm2rx9jk4exfdeq46d6ptwlpy3w0cllv"
IT_VAL_2_MNEMONIC = "explain captain crucial fault symptom degree divorce beyond path security jewel alien beach finish bridge decide toast scene pelican sorry achieve off denial wall"

IT_VAL_3_ADDR = "evm1rxczyg2x94dqcn77t4pyhcndg3r889dw9rn0uk"
IT_VAL_3_VAL_ADDR = "evmvaloper1rxczyg2x94dqcn77t4pyhcndg3r889dwrg6lh4"
IT_VAL_3_CONS_ADDR = "evmvalcons1vxky3ld4llhaqk8nl6pw6xkxqy97rwdarue89z"
IT_VAL_3_MNEMONIC = "worth talent fire announce file skull acquire ethics injury yard home list clap guard busy describe bag front grass noise index vacuum govern number"

IT_VAL_4_ADDR = "evm1gmjvfd4pr0yd94t0x8xw4uwg2j0cn9g9hn0t6r"
IT_VAL_4_VAL_ADDR = "evmvaloper1gmjvfd4pr0yd94t0x8xw4uwg2j0cn9g93cxm3q"
IT_VAL_4_CONS_ADDR = "evmvalcons1yl9a7v952ejxju9fec6hqeuuku4372pncyvrv2"
IT_VAL_4_MNEMONIC = "question joke action slice mistake carbon virtual still culture push estate inhale true endless market flip hammer word lecture pen toddler lyrics creek regular"

IT_VAL_5_ADDR = "evm1fpveqajjpt2emsfkr5xwp80074mkn38x777ezk"
IT_VAL_5_VAL_ADDR = "evmvaloper1fpveqajjpt2emsfkr5xwp80074mkn38xc4hff4"
IT_VAL_5_CONS_ADDR = "evmvalcons1p6n7qpnn5lqyyujzrp344drz228l3wx0y3fvt5"
IT_VAL_5_MNEMONIC = "tornado fuel drill critic indicate pool few wheat omit sight stage focus mountain amused neck surge post giant vague nut marine spoon fragile outdoor"
)

//goland:noinspection GoSnakeCaseUsage,SpellCheckingInspection
const (
IT_WAL_1_ADDR = "evm139mq752delxv78jvtmwxhasyrycufsvr5jkxf3"
IT_WAL_1_ETH_ADDR = "0x89760f514DCfCCCf1E4c5eDC6Bf6041931c4c183"
IT_WAL_1_MNEMONIC = "curtain hat remain song receive tower stereo hope frog cheap brown plate raccoon post reflect wool sail salmon game salon group glimpse adult shift"

IT_WAL_2_ADDR = "evm1yxmxrj9zwrkc855zdt2fk83m0r63tcjuvyy9sq"
IT_WAL_2_ETH_ADDR = "0x21b661c8A270ed83D2826aD49b1E3B78F515E25C"
IT_WAL_2_MNEMONIC = "coral drink glow assist canyon ankle hole buffalo vendor foster void clip welcome slush cherry omit member legal account lunar often hen winter culture"

IT_WAL_3_ADDR = "evm1v3uay5np5a93kpv80rfldxkhe32hxsdg8ya87c"
IT_WAL_3_ETH_ADDR = "0x6479D25261A74B1b058778d3F69Ad7cC557341A8"
IT_WAL_3_MNEMONIC = "depth skull anxiety weasel pulp interest seek junk trumpet orbit glance drink comfort much alarm during lady strong matrix enable write pledge alcohol buzz"

IT_WAL_4_ADDR = "evm1zsdj9vsw44kk46fmnka7k76smsaxgh6ps9l8p0"
IT_WAL_4_ETH_ADDR = "0x141B22B20ead6d6AE93B9DBBeB7b50DC3A645F41"
IT_WAL_4_MNEMONIC = "author humble raise whisper allow appear typical release fossil address spy jazz damage runway spy gossip add embark wrap frost toe advice matrix laundry"

IT_WAL_5_ADDR = "evm1862crydur2cpjww66dhfzcc26yglvrcsh8x7at"
IT_WAL_5_ETH_ADDR = "0x3E958191BC1AB01939DAD36e91630Ad111F60f10"
IT_WAL_5_MNEMONIC = "museum stumble kingdom impulse replace angle exercise trial spring sphere cube brief foil bridge dish earn practice surprise quantum hunt scale solve october scout"
)
72 changes: 72 additions & 0 deletions integration_test_util/accounts_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
package integration_test_util

//goland:noinspection SpellCheckingInspection
import (
"encoding/hex"
"github.com/stretchr/testify/require"
"testing"
)

//goland:noinspection SpellCheckingInspection
func Test_TestAccount_KeysAndAddresses(t *testing.T) {
t.Run("test validator account", func(t *testing.T) {
valAcc := newTestAccountFromMnemonic(t, IT_VAL_1_MNEMONIC)

require.Equal(t, IT_VAL_1_VAL_ADDR, valAcc.GetValidatorAddress().String())

require.Equal(t, IT_VAL_1_CONS_ADDR, valAcc.GetConsensusAddress().String())

require.Equal(t, IT_VAL_1_ADDR, valAcc.GetCosmosAddress().String())

require.Equal(
t,
"EthPubKeySecp256k1{0213B178097B00B5E87CD81D4F02E0F6DBF4B10608CE4B409A630C7E837F973350}",
valAcc.GetPubKey().String(),
)

require.Equal(
t,
"PubKeyEd25519{EC78352B42A13C1938886E4242AD7F2DD39DA55B886ECA06B826D50008C7C086}",
valAcc.GetSdkPubKey().String(),
)

const tmPub = "6323691963BFA43CB659BDA8676B43D2B3CCCE10"
require.Equal(
t,
tmPub,
valAcc.GetTmPubKey().Address().String(),
)
tmPrivKey := valAcc.GetTmPrivKey()
require.Equal(
t,
tmPub,
tmPrivKey.PubKey().Address().String(),
)

require.Equal(
t,
"671959de1da2c3860f59ee81c19eeba23f18f585e22c47e3dfe559970d473038ec78352b42a13c1938886e4242ad7f2dd39da55b886eca06b826d50008c7c086",
hex.EncodeToString(tmPrivKey.Bytes()),
)
})

t.Run("test wallet account", func(t *testing.T) {
walAcc := newTestAccountFromMnemonic(t, IT_WAL_1_MNEMONIC)

require.Equal(t, IT_WAL_1_ETH_ADDR, walAcc.GetEthAddress().String())

require.Equal(t, IT_WAL_1_ADDR, walAcc.GetCosmosAddress().String())

require.Equal(
t,
"EthPubKeySecp256k1{020826F89EFBC5E5CFC8930E9E976D12EFA79821BF89AF73B9DAE6DE41ACEF6DB3}",
walAcc.GetPubKey().String(),
)

require.Equal(
t,
"PubKeyEd25519{7570E4698A126B47D5E2698C74ED728F2DE06593AE72E1987FA25BCFCD46A4BA}",
walAcc.GetSdkPubKey().String(),
)
})
}
Loading