Skip to content

Commit b0ff5fd

Browse files
feat: create test chains with custom app creation functions (#8154) (#8189)
* feat: add additional constructor to create a test chain with a specific app creation function * chore: fix linter --------- Co-authored-by: Gjermund Garaba <[email protected]> (cherry picked from commit fe25b21) Co-authored-by: Cian Hatton <[email protected]>
1 parent 4d002d6 commit b0ff5fd

File tree

9 files changed

+39
-54
lines changed

9 files changed

+39
-54
lines changed

modules/apps/callbacks/callbacks_test.go

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,6 @@ import (
3232

3333
const maxCallbackGas = uint64(1000000)
3434

35-
func init() {
36-
ibctesting.DefaultTestingAppInit = SetupTestingApp
37-
}
38-
3935
// SetupTestingApp provides the duplicated simapp which is specific to the callbacks module on chain creation.
4036
func SetupTestingApp() (ibctesting.TestingApp, map[string]json.RawMessage) {
4137
db := dbm.NewMemDB()
@@ -67,7 +63,7 @@ type CallbacksTestSuite struct {
6763

6864
// setupChains sets up a coordinator with 2 test chains.
6965
func (s *CallbacksTestSuite) setupChains() {
70-
s.coordinator = ibctesting.NewCoordinator(s.T(), 2)
66+
s.coordinator = ibctesting.NewCustomAppCoordinator(s.T(), 2, SetupTestingApp)
7167
s.chainA = s.coordinator.GetChain(ibctesting.GetChainID(1))
7268
s.chainB = s.coordinator.GetChain(ibctesting.GetChainID(2))
7369
s.path = ibctesting.NewPath(s.chainA, s.chainB)

modules/apps/callbacks/v2/v2_test.go

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,6 @@ import (
2323

2424
const maxCallbackGas = uint64(1000000)
2525

26-
func init() {
27-
ibctesting.DefaultTestingAppInit = SetupTestingApp
28-
}
29-
3026
// SetupTestingApp provides the duplicated simapp which is specific to the callbacks module on chain creation.
3127
func SetupTestingApp() (ibctesting.TestingApp, map[string]json.RawMessage) {
3228
db := dbm.NewMemDB()
@@ -58,7 +54,7 @@ type CallbacksTestSuite struct {
5854

5955
// setupChains sets up a coordinator with 2 test chains.
6056
func (s *CallbacksTestSuite) setupChains() {
61-
s.coordinator = ibctesting.NewCoordinator(s.T(), 2)
57+
s.coordinator = ibctesting.NewCustomAppCoordinator(s.T(), 2, SetupTestingApp)
6258
s.chainA = s.coordinator.GetChain(ibctesting.GetChainID(1))
6359
s.chainB = s.coordinator.GetChain(ibctesting.GetChainID(2))
6460
s.path = ibctesting.NewPath(s.chainA, s.chainB)

modules/light-clients/08-wasm/internal/types/store_test.go

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,6 @@ import (
2424
ibctesting "github.com/cosmos/ibc-go/v10/testing"
2525
)
2626

27-
func init() {
28-
ibctesting.DefaultTestingAppInit = setupTestingApp
29-
}
30-
3127
var invalidPrefix = []byte("invalid/")
3228

3329
type TypesTestSuite struct {
@@ -41,9 +37,7 @@ func TestWasmTestSuite(t *testing.T) {
4137
}
4238

4339
func (suite *TypesTestSuite) SetupTest() {
44-
ibctesting.DefaultTestingAppInit = setupTestingApp
45-
46-
suite.coordinator = ibctesting.NewCoordinator(suite.T(), 1)
40+
suite.coordinator = ibctesting.NewCustomAppCoordinator(suite.T(), 1, setupTestingApp)
4741
suite.chainA = suite.coordinator.GetChain(ibctesting.GetChainID(1))
4842
}
4943

modules/light-clients/08-wasm/keeper/keeper_test.go

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -44,10 +44,6 @@ type KeeperTestSuite struct {
4444
chainA *ibctesting.TestChain
4545
}
4646

47-
func init() {
48-
ibctesting.DefaultTestingAppInit = setupTestingApp
49-
}
50-
5147
// setupTestingApp provides the duplicated simapp which is specific to the 08-wasm module on chain creation.
5248
func setupTestingApp() (ibctesting.TestingApp, map[string]json.RawMessage) {
5349
db := dbm.NewMemDB()
@@ -66,7 +62,7 @@ func GetSimApp(chain *ibctesting.TestChain) *simapp.SimApp {
6662
}
6763

6864
func (suite *KeeperTestSuite) SetupTest() {
69-
suite.coordinator = ibctesting.NewCoordinator(suite.T(), 1)
65+
suite.coordinator = ibctesting.NewCustomAppCoordinator(suite.T(), 1, setupTestingApp)
7066
suite.chainA = suite.coordinator.GetChain(ibctesting.GetChainID(1))
7167

7268
queryHelper := baseapp.NewQueryServerTestHelper(suite.chainA.GetContext(), GetSimApp(suite.chainA).InterfaceRegistry())
@@ -75,9 +71,7 @@ func (suite *KeeperTestSuite) SetupTest() {
7571

7672
// SetupWasmWithMockVM sets up mock cometbft chain with a mock vm.
7773
func (suite *KeeperTestSuite) SetupWasmWithMockVM() {
78-
ibctesting.DefaultTestingAppInit = suite.setupWasmWithMockVM
79-
80-
suite.coordinator = ibctesting.NewCoordinator(suite.T(), 1)
74+
suite.coordinator = ibctesting.NewCustomAppCoordinator(suite.T(), 1, suite.setupWasmWithMockVM)
8175
suite.chainA = suite.coordinator.GetChain(ibctesting.GetChainID(1))
8276
}
8377

@@ -115,8 +109,6 @@ func (suite *KeeperTestSuite) setupWasmWithMockVM() (ibctesting.TestingApp, map[
115109
db := dbm.NewMemDB()
116110
app := simapp.NewUnitTestSimApp(log.NewNopLogger(), db, nil, true, simtestutil.EmptyAppOptions{}, suite.mockVM)
117111

118-
// reset DefaultTestingAppInit to its original value
119-
ibctesting.DefaultTestingAppInit = setupTestingApp
120112
return app, app.DefaultGenesis()
121113
}
122114

modules/light-clients/08-wasm/types/types_test.go

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -32,16 +32,10 @@ func TestWasmTestSuite(t *testing.T) {
3232
}
3333

3434
func (suite *TypesTestSuite) SetupTest() {
35-
ibctesting.DefaultTestingAppInit = setupTestingApp
36-
37-
suite.coordinator = ibctesting.NewCoordinator(suite.T(), 1)
35+
suite.coordinator = ibctesting.NewCustomAppCoordinator(suite.T(), 1, setupTestingApp)
3836
suite.chainA = suite.coordinator.GetChain(ibctesting.GetChainID(1))
3937
}
4038

41-
func init() {
42-
ibctesting.DefaultTestingAppInit = setupTestingApp
43-
}
44-
4539
// GetSimApp returns the duplicated SimApp from within the 08-wasm directory.
4640
// This must be used instead of chain.GetSimApp() for tests within this directory.
4741
func GetSimApp(chain *ibctesting.TestChain) *simapp.SimApp {

modules/light-clients/08-wasm/wasm_test.go

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -41,16 +41,10 @@ func TestWasmTestSuite(t *testing.T) {
4141
}
4242

4343
func (suite *WasmTestSuite) SetupTest() {
44-
ibctesting.DefaultTestingAppInit = setupTestingApp
45-
46-
suite.coordinator = ibctesting.NewCoordinator(suite.T(), 1)
44+
suite.coordinator = ibctesting.NewCustomAppCoordinator(suite.T(), 1, setupTestingApp)
4745
suite.chainA = suite.coordinator.GetChain(ibctesting.GetChainID(1))
4846
}
4947

50-
func init() {
51-
ibctesting.DefaultTestingAppInit = setupTestingApp
52-
}
53-
5448
// GetSimApp returns the duplicated SimApp from within the 08-wasm directory.
5549
// This must be used instead of chain.GetSimApp() for tests within this directory.
5650
func GetSimApp(chain *ibctesting.TestChain) *simapp.SimApp {
@@ -70,9 +64,7 @@ func setupTestingApp() (ibctesting.TestingApp, map[string]json.RawMessage) {
7064

7165
// SetupWasmWithMockVM sets up mock cometbft chain with a mock vm.
7266
func (suite *WasmTestSuite) SetupWasmWithMockVM() {
73-
ibctesting.DefaultTestingAppInit = suite.setupWasmWithMockVM
74-
75-
suite.coordinator = ibctesting.NewCoordinator(suite.T(), 1)
67+
suite.coordinator = ibctesting.NewCustomAppCoordinator(suite.T(), 1, suite.setupWasmWithMockVM)
7668
suite.chainA = suite.coordinator.GetChain(ibctesting.GetChainID(1))
7769
suite.checksum = storeWasmCode(suite, wasmtesting.Code)
7870
}
@@ -111,8 +103,6 @@ func (suite *WasmTestSuite) setupWasmWithMockVM() (ibctesting.TestingApp, map[st
111103
db := dbm.NewMemDB()
112104
app := simapp.NewUnitTestSimApp(log.NewNopLogger(), db, nil, true, simtestutil.EmptyAppOptions{}, suite.mockVM)
113105

114-
// reset DefaultTestingAppInit to its original value
115-
ibctesting.DefaultTestingAppInit = setupTestingApp
116106
return app, app.DefaultGenesis()
117107
}
118108

testing/chain.go

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package ibctesting
22

33
import (
4+
"encoding/json"
45
"fmt"
56
"testing"
67
"time"
@@ -102,6 +103,10 @@ type TestChain struct {
102103
// CONTRACT: Validator array must be provided in the order expected by Tendermint.
103104
// i.e. sorted first by power and then lexicographically by address.
104105
func NewTestChainWithValSet(tb testing.TB, coord *Coordinator, chainID string, valSet *cmttypes.ValidatorSet, signers map[string]cmttypes.PrivValidator) *TestChain {
106+
return newTestChainWithValSet(tb, coord, chainID, valSet, signers, DefaultTestingAppInit)
107+
}
108+
109+
func newTestChainWithValSet(tb testing.TB, coord *Coordinator, chainID string, valSet *cmttypes.ValidatorSet, signers map[string]cmttypes.PrivValidator, appCreator AppCreator) *TestChain {
105110
tb.Helper()
106111
genAccs := []authtypes.GenesisAccount{}
107112
genBals := []banktypes.Balance{}
@@ -134,7 +139,7 @@ func NewTestChainWithValSet(tb testing.TB, coord *Coordinator, chainID string, v
134139
senderAccs = append(senderAccs, senderAcc)
135140
}
136141

137-
app := SetupWithGenesisValSet(tb, valSet, genAccs, chainID, sdk.DefaultPowerReduction, genBals...)
142+
app := setupWithGenesisValSet(tb, valSet, genAccs, chainID, sdk.DefaultPowerReduction, appCreator, genBals...)
138143

139144
// create current header and call begin block
140145
header := cmtproto.Header{
@@ -169,9 +174,11 @@ func NewTestChainWithValSet(tb testing.TB, coord *Coordinator, chainID string, v
169174
return chain
170175
}
171176

172-
// NewTestChain initializes a new test chain with a default of 4 validators
173-
// Use this function if the tests do not need custom control over the validator set
174-
func NewTestChain(t *testing.T, coord *Coordinator, chainID string) *TestChain {
177+
// AppCreator is a function which returns a TestingApp and a GenesisState
178+
type AppCreator func() (TestingApp, map[string]json.RawMessage)
179+
180+
// NewCustomAppTestChain creates a TestChain instance with the provided AppCreator function.
181+
func NewCustomAppTestChain(t *testing.T, coord *Coordinator, chainID string, appCreator AppCreator) *TestChain {
175182
t.Helper()
176183
// generate validators private/public key
177184
var (
@@ -193,7 +200,14 @@ func NewTestChain(t *testing.T, coord *Coordinator, chainID string) *TestChain {
193200
// or, if equal, by address lexical order
194201
valSet := cmttypes.NewValidatorSet(validators)
195202

196-
return NewTestChainWithValSet(t, coord, chainID, valSet, signersByAddress)
203+
return newTestChainWithValSet(t, coord, chainID, valSet, signersByAddress, appCreator)
204+
}
205+
206+
// NewTestChain initializes a new test chain with a default of 4 validators
207+
// Use this function if the tests do not need custom control over the validator set
208+
func NewTestChain(t *testing.T, coord *Coordinator, chainID string) *TestChain {
209+
t.Helper()
210+
return NewCustomAppTestChain(t, coord, chainID, DefaultTestingAppInit)
197211
}
198212

199213
// GetContext returns the current context for the application.

testing/coordinator.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,11 @@ type Coordinator struct {
2828

2929
// NewCoordinator initializes Coordinator with N TestChain's
3030
func NewCoordinator(t *testing.T, n int) *Coordinator {
31+
return NewCustomAppCoordinator(t, n, DefaultTestingAppInit)
32+
}
33+
34+
// NewCustomAppCoordinator initializes a Coordinator with N TestChain's using the given AppCreator function.
35+
func NewCustomAppCoordinator(t *testing.T, n int, appCreator AppCreator) *Coordinator {
3136
t.Helper()
3237
chains := make(map[string]*TestChain)
3338
coord := &Coordinator{
@@ -37,7 +42,7 @@ func NewCoordinator(t *testing.T, n int) *Coordinator {
3742

3843
for i := 1; i <= n; i++ {
3944
chainID := GetChainID(i)
40-
chains[chainID] = NewTestChain(t, coord, chainID)
45+
chains[chainID] = NewCustomAppTestChain(t, coord, chainID, appCreator)
4146
}
4247
coord.Chains = chains
4348

testing/testing_app.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ import (
3131
"github.com/cosmos/ibc-go/v10/testing/simapp"
3232
)
3333

34-
var DefaultTestingAppInit = SetupTestingApp
34+
var DefaultTestingAppInit AppCreator = SetupTestingApp
3535

3636
type TestingApp interface {
3737
servertypes.ABCI
@@ -60,8 +60,12 @@ func SetupTestingApp() (TestingApp, map[string]json.RawMessage) {
6060
// of one consensus engine unit (10^6) in the default token of the simapp from first genesis
6161
// account. A Nop logger is set in SimApp.
6262
func SetupWithGenesisValSet(tb testing.TB, valSet *cmttypes.ValidatorSet, genAccs []authtypes.GenesisAccount, chainID string, powerReduction sdkmath.Int, balances ...banktypes.Balance) TestingApp {
63+
return setupWithGenesisValSet(tb, valSet, genAccs, chainID, powerReduction, DefaultTestingAppInit, balances...)
64+
}
65+
66+
func setupWithGenesisValSet(tb testing.TB, valSet *cmttypes.ValidatorSet, genAccs []authtypes.GenesisAccount, chainID string, powerReduction sdkmath.Int, appCreator AppCreator, balances ...banktypes.Balance) TestingApp {
6367
tb.Helper()
64-
app, genesisState := DefaultTestingAppInit()
68+
app, genesisState := appCreator()
6569

6670
// ensure baseapp has a chain-id set before running InitChain
6771
baseapp.SetChainID(chainID)(app.GetBaseApp())

0 commit comments

Comments
 (0)