-
Notifications
You must be signed in to change notification settings - Fork 111
/
generic_setup.go
176 lines (149 loc) · 6 KB
/
generic_setup.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
package ibc_testing
import (
"encoding/json"
"fmt"
"testing"
clienttypes "github.com/cosmos/ibc-go/v7/modules/core/02-client/types"
ibctesting "github.com/cosmos/ibc-go/v7/testing"
"github.com/stretchr/testify/suite"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cometbft/cometbft/abci/types"
tmencoding "github.com/cometbft/cometbft/crypto/encoding"
tmtypes "github.com/cometbft/cometbft/types"
testutil "github.com/cosmos/interchain-security/v3/testutil/integration"
testkeeper "github.com/cosmos/interchain-security/v3/testutil/keeper"
consumerkeeper "github.com/cosmos/interchain-security/v3/x/ccv/consumer/keeper"
)
type (
AppIniter func() (ibctesting.TestingApp, map[string]json.RawMessage)
ValSetAppIniter func([]types.ValidatorUpdate) AppIniter
)
// Contains generic setup code for running integration tests against a provider, consumer,
// and/or democracy consumer app.go implementation. You should not need to modify or replicate this file
// to run integration tests against your app.go implementations!
var (
FirstConsumerChainID string
provChainID string
democConsumerChainID string
)
func init() {
// Disable revision format
ibctesting.ChainIDSuffix = ""
FirstConsumerChainID = ibctesting.GetChainID(2)
provChainID = ibctesting.GetChainID(1)
democConsumerChainID = ibctesting.GetChainID(5000)
}
// ConsumerBundle serves as a way to store useful in-mem consumer app chain state
// and relevant IBC paths in the context of CCV integration testing.
type ConsumerBundle struct {
Chain *ibctesting.TestChain
App testutil.ConsumerApp
Path *ibctesting.Path
TransferPath *ibctesting.Path
}
// GetCtx returns the context for the ConsumerBundle
func (cb ConsumerBundle) GetCtx() sdk.Context {
return cb.Chain.GetContext()
}
// GetKeeper returns the keeper for the ConsumerBundle
func (cb ConsumerBundle) GetKeeper() consumerkeeper.Keeper {
return cb.App.GetConsumerKeeper()
}
// AddProvider adds a new provider chain to the coordinator and returns the test chain and app type
func AddProvider[T testutil.ProviderApp](t *testing.T, coordinator *ibctesting.Coordinator, appIniter AppIniter) (
*ibctesting.TestChain, T,
) {
t.Helper()
ibctesting.DefaultTestingAppInit = appIniter
provider := ibctesting.NewTestChain(t, coordinator, provChainID)
coordinator.Chains[provChainID] = provider
providerToReturn, ok := provider.App.(T)
if !ok {
panic(fmt.Sprintf("provider app type returned from app initer does not match app type passed in as type param: %T, %T",
provider.App, *new(T)))
}
return provider, providerToReturn
}
// AddDemocracyConsumer adds a new democ consumer chain to the coordinator and returns the test chain and app type
func AddDemocracyConsumer[T testutil.DemocConsumerApp](
coordinator *ibctesting.Coordinator,
s *suite.Suite,
appIniter ValSetAppIniter,
) (*ibctesting.TestChain, T) {
s.T().Helper()
// generate validators private/public key
valSet, valUpdates, signers := testutil.CreateValidators(s.T(), 4)
ibctesting.DefaultTestingAppInit = appIniter(valUpdates)
democConsumer := ibctesting.NewTestChainWithValSet(s.T(), coordinator, democConsumerChainID, valSet, signers)
coordinator.Chains[democConsumerChainID] = democConsumer
democConsumerToReturn, ok := democConsumer.App.(T)
if !ok {
panic(fmt.Sprintf("democ consumer app type returned from app initer does not match app type passed in as type param: %T, %T",
democConsumer.App, *new(T)))
}
return democConsumer, democConsumerToReturn
}
// AddConsumer adds a new consumer chain with "testchain<index+2>" as chainID to the coordinator
// and returns the test chain and app type. A new client is created on the provider to the new
// consumer chain (see CreateConsumerClient). The new consumer is initialized with the
// InitialValSet from the genesis state generated by the provider (see MakeConsumerGenesis).
//
// This method must be called after AddProvider.
func AddConsumer[Tp testutil.ProviderApp, Tc testutil.ConsumerApp](
coordinator *ibctesting.Coordinator,
s *suite.Suite,
index int,
appIniter ValSetAppIniter,
) *ConsumerBundle {
// consumer chain ID
chainID := ibctesting.GetChainID(index + 2)
// create client to the consumer on the provider chain
providerChain := coordinator.Chains[provChainID]
providerApp := providerChain.App.(Tp)
providerKeeper := providerApp.GetProviderKeeper()
prop := testkeeper.GetTestConsumerAdditionProp()
prop.ChainId = chainID
// NOTE: the initial height passed to CreateConsumerClient
// must be the height on the consumer when InitGenesis is called
prop.InitialHeight = clienttypes.Height{RevisionNumber: 0, RevisionHeight: 3}
err := providerKeeper.CreateConsumerClient(
providerChain.GetContext(),
prop,
)
s.Require().NoError(err)
// commit the state on the provider chain
coordinator.CommitBlock(providerChain)
// get genesis state created by the provider
consumerGenesisState, found := providerKeeper.GetConsumerGenesis(
providerChain.GetContext(),
chainID,
)
s.Require().True(found, "consumer genesis not found")
// use InitialValSet as the valset on the consumer
var valz []*tmtypes.Validator
for _, update := range consumerGenesisState.Provider.InitialValSet {
// tmPubKey update.PubKey
tmPubKey, err := tmencoding.PubKeyFromProto(update.PubKey)
s.Require().NoError(err)
valz = append(valz, &tmtypes.Validator{
PubKey: tmPubKey,
VotingPower: update.Power,
Address: tmPubKey.Address(),
ProposerPriority: 0,
})
}
// create and instantiate consumer chain
ibctesting.DefaultTestingAppInit = appIniter(consumerGenesisState.Provider.InitialValSet)
testChain := ibctesting.NewTestChainWithValSet(s.T(), coordinator, chainID,
tmtypes.NewValidatorSet(valz), providerChain.Signers)
coordinator.Chains[chainID] = testChain
consumerToReturn, ok := testChain.App.(Tc)
if !ok {
panic(fmt.Sprintf("consumer app type returned from app initer does not match app type passed in as type param: %T, %T",
testChain.App, *new(Tc)))
}
return &ConsumerBundle{
Chain: testChain,
App: consumerToReturn,
}
}