Skip to content

Commit 4cf3c3b

Browse files
cloudgrayaljo242
andauthored
refactor: fix integration test suite to resolve lock contention problem from external app injection (#810)
* chore: use tmp homedir for integration test It is to prevent race conditions related to home directory locks in chains that use the WASM module. * refactor: make precompile tests runnable with other chain binaries * chore: remove redundant addr prefix setup from eip712 test * chore: update CHANGELOG.md * chore: add description about app injection to integration test suite --------- Co-authored-by: Alex | Cosmos Labs <[email protected]>
1 parent 0f7b883 commit 4cf3c3b

File tree

10 files changed

+56
-24
lines changed

10 files changed

+56
-24
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
### IMPROVEMENTS
88

99
- [\#758](https://github.com/cosmos/evm/pull/758) Cleanup precompiles abi.json.
10+
- [\#810](https://github.com/cosmos/evm/pull/810) Fix integration test suite to resolve lock contention problem from external app injection
1011
- [\#811](https://github.com/cosmos/evm/pull/811) Use sdk's DefaultBondDenom for default evm denom in genesis.
1112

1213
### FEATURES

evmd/tests/integration/create_app.go

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package integration
22

33
import (
44
"encoding/json"
5+
"os"
56

67
"github.com/cosmos/cosmos-sdk/client/flags"
78

@@ -14,7 +15,6 @@ import (
1415
"github.com/cosmos/evm/testutil/constants"
1516
feemarkettypes "github.com/cosmos/evm/x/feemarket/types"
1617

17-
clienthelpers "cosmossdk.io/client/v2/helpers"
1818
"cosmossdk.io/log"
1919

2020
"github.com/cosmos/cosmos-sdk/baseapp"
@@ -26,7 +26,9 @@ import (
2626
// CreateEvmd creates an evm app for regular integration tests (non-mempool)
2727
// This version uses a noop mempool to avoid state issues during transaction processing
2828
func CreateEvmd(chainID string, evmChainID uint64, customBaseAppOptions ...func(*baseapp.BaseApp)) evm.EvmApp {
29-
defaultNodeHome, err := clienthelpers.GetNodeHomeDirectory(".evmd")
29+
// A temporary home directory is created and used to prevent race conditions
30+
// related to home directory locks in chains that use the WASM module.
31+
defaultNodeHome, err := os.MkdirTemp("", "evmd-temp-homedir")
3032
if err != nil {
3133
panic(err)
3234
}
@@ -51,12 +53,17 @@ func CreateEvmd(chainID string, evmChainID uint64, customBaseAppOptions ...func(
5153
// SetupEvmd initializes a new evmd app with default genesis state.
5254
// It is used in IBC integration tests to create a new evmd app instance.
5355
func SetupEvmd() (ibctesting.TestingApp, map[string]json.RawMessage) {
56+
defaultNodeHome, err := os.MkdirTemp("", "evmd-temp-homedir")
57+
if err != nil {
58+
panic(err)
59+
}
60+
5461
app := evmd.NewExampleApp(
5562
log.NewNopLogger(),
5663
dbm.NewMemDB(),
5764
nil,
5865
true,
59-
NewAppOptionsWithFlagHomeAndChainID("", constants.EighteenDecimalsChainID),
66+
NewAppOptionsWithFlagHomeAndChainID(defaultNodeHome, constants.EighteenDecimalsChainID),
6067
)
6168
// disable base fee for testing
6269
genesisState := app.DefaultGenesis()

tests/integration/README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,9 @@ All tests defined here can be used by any client application that implements the
2626
You can find usage examples under `evmd/tests/integration`.
2727

2828
For instance, if you want to test your own application with the Bank Precompile Integration Test Suite,
29-
implement your own `CreateApp` function and pass it in as shown below:
29+
30+
1. Refer to interfaces.go file and implement `EvmApp` interface in your own app.
31+
2. Refer to the `evmd/tests/integration/create_app.go` file and implement your own `CreateApp` function and pass it in as shown below:
3032

3133
```go
3234
package integration

tests/integration/eip712/test_eip712.go

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,8 +71,6 @@ func (s *TestSuite) SetupTest() {
7171
s.config = nw.GetEncodingConfig()
7272
s.clientCtx = client.Context{}.WithTxConfig(s.config.TxConfig)
7373
s.denom = evmtypes.GetEVMCoinDenom()
74-
75-
sdk.GetConfig().SetBech32PrefixForAccount("cosmos", "")
7674
}
7775

7876
// createTestAddress creates random test addresses for messages

tests/integration/precompiles/bank/test_integration.go

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -119,8 +119,8 @@ func TestIntegrationSuite(t *testing.T, create network.CreateEvmApp, options ...
119119
contractData ContractData
120120
passCheck testutil.LogCheckArgs
121121

122-
cosmosEVMTotalSupply, _ = new(big.Int).SetString("200003000000000000000000", 10)
123-
xmplTotalSupply, _ = new(big.Int).SetString("200000000000000000000000", 10)
122+
cosmosEVMTotalSupply *big.Int
123+
xmplTotalSupply *big.Int
124124
)
125125

126126
BeforeEach(func() {
@@ -155,6 +155,13 @@ func TestIntegrationSuite(t *testing.T, create network.CreateEvmApp, options ...
155155

156156
err = is.network.NextBlock()
157157
Expect(err).ToNot(HaveOccurred(), "failed to advance block")
158+
159+
// Get total supply from bank keeper
160+
cosmosSupply := is.network.App.GetBankKeeper().GetSupply(is.network.GetContext(), is.bondDenom)
161+
cosmosEVMTotalSupply = new(big.Int).Set(cosmosSupply.Amount.BigInt())
162+
163+
xmplSupply := is.network.App.GetBankKeeper().GetSupply(is.network.GetContext(), is.tokenDenom)
164+
xmplTotalSupply = new(big.Int).Set(xmplSupply.Amount.BigInt())
158165
})
159166

160167
Context("Direct precompile queries", func() {

tests/integration/precompiles/bech32/test_bech32.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ func (s *PrecompileTestSuite) TestRun() {
110110
input, err := s.precompile.Pack(
111111
bech32.HexToBech32Method,
112112
s.keyring.GetAddr(0),
113-
"cosmos",
113+
sdk.GetConfig().GetBech32AccountAddrPrefix(),
114114
)
115115
s.Require().NoError(err, "failed to pack input")
116116
contract.Input = input
@@ -136,7 +136,7 @@ func (s *PrecompileTestSuite) TestRun() {
136136
input, err := s.precompile.Pack(
137137
bech32.HexToBech32Method,
138138
common.BytesToAddress(valAddrBz),
139-
"cosmosvaloper",
139+
sdk.GetConfig().GetBech32ValidatorAddrPrefix(),
140140
)
141141
s.Require().NoError(err, "failed to pack input")
142142
contract.Input = input
@@ -159,7 +159,7 @@ func (s *PrecompileTestSuite) TestRun() {
159159
input, err := s.precompile.Pack(
160160
bech32.HexToBech32Method,
161161
s.keyring.GetAddr(0),
162-
"cosmosvalcons",
162+
sdk.GetConfig().GetBech32ConsensusAddrPrefix(),
163163
)
164164
s.Require().NoError(err, "failed to pack input")
165165
contract.Input = input

tests/integration/precompiles/bech32/test_methods.go

Lines changed: 25 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ func (s *PrecompileTestSuite) TestHexToBech32() {
6262
func() []interface{} {
6363
return []interface{}{
6464
s.keyring.GetAddr(0),
65-
"cosmos",
65+
sdk.GetConfig().GetBech32AccountAddrPrefix(),
6666
}
6767
},
6868
func(data []byte) {
@@ -108,7 +108,7 @@ func (s *PrecompileTestSuite) TestBech32ToHex() {
108108
malleate func() []interface{}
109109
postCheck func(data []byte)
110110
expError bool
111-
errContains string
111+
errContains func() string
112112
}{
113113
{
114114
"fail - invalid args length",
@@ -117,7 +117,9 @@ func (s *PrecompileTestSuite) TestBech32ToHex() {
117117
},
118118
func([]byte) {},
119119
true,
120-
fmt.Sprintf(cmn.ErrInvalidNumberOfArgs, 1, 0),
120+
func() string {
121+
return fmt.Sprintf(cmn.ErrInvalidNumberOfArgs, 1, 0)
122+
},
121123
},
122124
{
123125
"fail - empty bech32 address",
@@ -128,7 +130,9 @@ func (s *PrecompileTestSuite) TestBech32ToHex() {
128130
},
129131
func([]byte) {},
130132
true,
131-
"invalid bech32 address",
133+
func() string {
134+
return "invalid bech32 address"
135+
},
132136
},
133137
{
134138
"fail - invalid bech32 address",
@@ -139,7 +143,9 @@ func (s *PrecompileTestSuite) TestBech32ToHex() {
139143
},
140144
func([]byte) {},
141145
true,
142-
fmt.Sprintf("invalid bech32 address: %s", "cosmos"),
146+
func() string {
147+
return fmt.Sprintf("invalid bech32 address: %s", "cosmos")
148+
},
143149
},
144150
{
145151
"fail - decoding bech32 failed",
@@ -150,7 +156,9 @@ func (s *PrecompileTestSuite) TestBech32ToHex() {
150156
},
151157
func([]byte) {},
152158
true,
153-
"decoding bech32 failed",
159+
func() string {
160+
return "decoding bech32 failed"
161+
},
154162
},
155163
{
156164
"fail - invalid address format",
@@ -161,7 +169,13 @@ func (s *PrecompileTestSuite) TestBech32ToHex() {
161169
},
162170
func([]byte) {},
163171
true,
164-
"address max length is 255",
172+
func() string {
173+
if addrVerifier := sdk.GetConfig().GetAddressVerifier(); addrVerifier != nil {
174+
err := addrVerifier(sdk.AccAddress(make([]byte, 256)))
175+
return err.Error()
176+
}
177+
return "address max length is 255"
178+
},
165179
},
166180
{
167181
"success - valid bech32 address",
@@ -179,7 +193,9 @@ func (s *PrecompileTestSuite) TestBech32ToHex() {
179193
s.Require().Equal(s.keyring.GetAddr(0), addr)
180194
},
181195
false,
182-
"",
196+
func() string {
197+
return ""
198+
},
183199
},
184200
}
185201

@@ -191,7 +207,7 @@ func (s *PrecompileTestSuite) TestBech32ToHex() {
191207

192208
if tc.expError {
193209
s.Require().Error(err)
194-
s.Require().ErrorContains(err, tc.errContains)
210+
s.Require().ErrorContains(err, tc.errContains())
195211
s.Require().Empty(bz)
196212
} else {
197213
s.Require().NoError(err)

tests/integration/precompiles/distribution/test_event.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,8 @@ func (s *PrecompileTestSuite) TestSetWithdrawAddressEvent() {
5858
err := cmn.UnpackLog(s.precompile.ABI, &setWithdrawerAddrEvent, distribution.EventTypeSetWithdrawAddress, *log)
5959
s.Require().NoError(err)
6060
s.Require().Equal(s.keyring.GetAddr(0), setWithdrawerAddrEvent.Caller)
61-
s.Require().Equal(sdk.MustBech32ifyAddressBytes("cosmos", s.keyring.GetAddr(0).Bytes()), setWithdrawerAddrEvent.WithdrawerAddress)
61+
bech32AddrPrefix := sdk.GetConfig().GetBech32AccountAddrPrefix()
62+
s.Require().Equal(sdk.MustBech32ifyAddressBytes(bech32AddrPrefix, s.keyring.GetAddr(0).Bytes()), setWithdrawerAddrEvent.WithdrawerAddress)
6263
},
6364
20000,
6465
false,

tests/integration/precompiles/distribution/test_tx.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import (
77
"github.com/ethereum/go-ethereum/common"
88
"github.com/ethereum/go-ethereum/core/vm"
99

10-
evmaddress "github.com/cosmos/evm/encoding/address"
1110
cmn "github.com/cosmos/evm/precompiles/common"
1211
"github.com/cosmos/evm/precompiles/distribution"
1312
"github.com/cosmos/evm/precompiles/testutil"
@@ -677,7 +676,7 @@ func (s *PrecompileTestSuite) TestDepositValidatorRewardsPoolMethod() {
677676
s.Require().True(success, "expected true, got false")
678677

679678
val := s.network.GetValidators()[0]
680-
valCodec := evmaddress.NewEvmCodec("cosmosvaloper")
679+
valCodec := s.network.App.GetStakingKeeper().ValidatorAddressCodec()
681680
valBz, err := valCodec.StringToBytes(val.GetOperator())
682681
s.Require().NoError(err)
683682

tests/integration/precompiles/staking/test_query.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -504,7 +504,8 @@ func (s *PrecompileTestSuite) TestRedelegation() {
504504
{
505505
name: "success - no redelegation found",
506506
malleate: func(srcOperatorAddr, _ string) []interface{} {
507-
nonExistentOperator := sdk.ValAddress([]byte("non-existent-operator"))
507+
nonExistentAddr, _ := testutiltx.NewAccAddressAndKey()
508+
nonExistentOperator := sdk.ValAddress(nonExistentAddr)
508509
return []interface{}{
509510
s.keyring.GetAddr(0),
510511
srcOperatorAddr,

0 commit comments

Comments
 (0)