forked from EscanBE/evermint
-
Notifications
You must be signed in to change notification settings - Fork 0
/
coordinator.go
199 lines (162 loc) · 6.07 KB
/
coordinator.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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
package ibctesting
import (
simtestutil "github.com/cosmos/cosmos-sdk/testutil/sims"
"math/rand"
"testing"
"time"
"github.com/servprotocolorg/serv/v12/app"
"github.com/cosmos/cosmos-sdk/baseapp"
"github.com/cosmos/cosmos-sdk/client"
cryptotypes "github.com/cosmos/cosmos-sdk/crypto/types"
sdk "github.com/cosmos/cosmos-sdk/types"
ibctesting "github.com/cosmos/ibc-go/v7/testing"
"github.com/stretchr/testify/require"
)
const DefaultFeeAmt = int64(150_000_000_000_000_000) // 0.15 EVMOS
var globalStartTime = time.Date(2020, 1, 2, 0, 0, 0, 0, time.UTC)
// NewCoordinator initializes Coordinator with N EVM TestChain's (Serv apps) and M Cosmos chains (Simulation Apps)
func NewCoordinator(t *testing.T, nEVMChains, mCosmosChains int) *ibctesting.Coordinator {
chains := make(map[string]*ibctesting.TestChain)
coord := &ibctesting.Coordinator{
T: t,
CurrentTime: globalStartTime,
}
for i := 1; i <= nEVMChains; i++ {
chainID := ibctesting.GetChainID(i)
// setup EVM chains
ibctesting.DefaultTestingAppInit = DefaultTestingAppInit(chainID)
chains[chainID] = NewTestChain(t, coord, chainID)
}
// setup Cosmos chains
ibctesting.DefaultTestingAppInit = ibctesting.SetupTestingApp
for j := 1 + nEVMChains; j <= nEVMChains+mCosmosChains; j++ {
chainID := ibctesting.GetChainID(j)
chains[chainID] = ibctesting.NewTestChain(t, coord, chainID)
}
coord.Chains = chains
return coord
}
// SetupPath constructs a TM client, connection, and channel on both chains provided. It will
// fail if any error occurs. The clientID's, TestConnections, and TestChannels are returned
// for both chains. The channels created are connected to the ibc-transfer application.
func SetupPath(coord *ibctesting.Coordinator, path *Path) {
SetupConnections(coord, path)
// channels can also be referenced through the returned connections
CreateChannels(coord, path)
}
// SetupClientConnections is a helper function to create clients and the appropriate
// connections on both the source and counterparty chain. It assumes the caller does not
// anticipate any errors.
func SetupConnections(coord *ibctesting.Coordinator, path *Path) {
SetupClients(coord, path)
CreateConnections(coord, path)
}
// CreateChannel constructs and executes channel handshake messages in order to create
// OPEN channels on chainA and chainB. The function expects the channels to be successfully
// opened otherwise testing will fail.
func CreateChannels(coord *ibctesting.Coordinator, path *Path) {
err := path.EndpointA.ChanOpenInit()
require.NoError(coord.T, err)
err = path.EndpointB.ChanOpenTry()
require.NoError(coord.T, err)
err = path.EndpointA.ChanOpenAck()
require.NoError(coord.T, err)
err = path.EndpointB.ChanOpenConfirm()
require.NoError(coord.T, err)
// ensure counterparty is up to date
err = path.EndpointA.UpdateClient()
require.NoError(coord.T, err)
}
// CreateConnection constructs and executes connection handshake messages in order to create
// OPEN channels on chainA and chainB. The connection information of for chainA and chainB
// are returned within a TestConnection struct. The function expects the connections to be
// successfully opened otherwise testing will fail.
func CreateConnections(coord *ibctesting.Coordinator, path *Path) {
err := path.EndpointA.ConnOpenInit()
require.NoError(coord.T, err)
err = path.EndpointB.ConnOpenTry()
require.NoError(coord.T, err)
err = path.EndpointA.ConnOpenAck()
require.NoError(coord.T, err)
err = path.EndpointB.ConnOpenConfirm()
require.NoError(coord.T, err)
// ensure counterparty is up to date
err = path.EndpointA.UpdateClient()
require.NoError(coord.T, err)
}
// SetupClients is a helper function to create clients on both chains. It assumes the
// caller does not anticipate any errors.
func SetupClients(coord *ibctesting.Coordinator, path *Path) {
err := path.EndpointA.CreateClient()
require.NoError(coord.T, err)
err = path.EndpointB.CreateClient()
require.NoError(coord.T, err)
}
func SendMsgs(chain *ibctesting.TestChain, feeAmt int64, msgs ...sdk.Msg) (*sdk.Result, error) {
var bondDenom string
// ensure the chain has the latest time
chain.Coordinator.UpdateTimeForChain(chain)
if servChain, ok := chain.App.(*app.Serv); ok {
bondDenom = servChain.StakingKeeper.BondDenom(chain.GetContext())
} else {
bondDenom = chain.GetSimApp().StakingKeeper.BondDenom(chain.GetContext())
}
fee := sdk.Coins{sdk.NewInt64Coin(bondDenom, feeAmt)}
_, r, err := SignAndDeliver(
chain.T,
chain.TxConfig,
chain.App.GetBaseApp(),
msgs,
fee,
chain.ChainID,
[]uint64{chain.SenderAccount.GetAccountNumber()},
[]uint64{chain.SenderAccount.GetSequence()},
true, chain.SenderPrivKey,
)
if err != nil {
return nil, err
}
// NextBlock calls app.Commit()
chain.NextBlock()
// increment sequence for successful transaction execution
err = chain.SenderAccount.SetSequence(chain.SenderAccount.GetSequence() + 1)
if err != nil {
return nil, err
}
chain.Coordinator.IncrementTime()
return r, nil
}
// SignAndDeliver signs and delivers a transaction. No simulation occurs as the
// ibc testing package causes checkState and deliverState to diverge in block time.
//
// CONTRACT: BeginBlock must be called before this function.
// Is a customization of IBC-go function that allows to modify the fee denom and amount
// IBC-go implementation: https://github.com/cosmos/ibc-go/blob/d34cef7e075dda1a24a0a3e9b6d3eff406cc606c/testing/simapp/test_helpers.go#L332-L364
func SignAndDeliver(
t *testing.T, txCfg client.TxConfig, app *baseapp.BaseApp, msgs []sdk.Msg,
fee sdk.Coins,
chainID string, accNums, accSeqs []uint64, expPass bool, priv ...cryptotypes.PrivKey,
) (sdk.GasInfo, *sdk.Result, error) {
tx, err := simtestutil.GenSignedMockTx(
rand.New(rand.NewSource(time.Now().UnixNano())),
txCfg,
msgs,
fee,
simtestutil.DefaultGenTxGas,
chainID,
accNums,
accSeqs,
priv...,
)
require.NoError(t, err)
// Simulate a sending a transaction
gInfo, res, err := app.SimDeliver(txCfg.TxEncoder(), tx)
if expPass {
require.NoError(t, err)
require.NotNil(t, res)
} else {
require.Error(t, err)
require.Nil(t, res)
}
return gInfo, res, err
}