-
Notifications
You must be signed in to change notification settings - Fork 745
/
Copy pathtest_utils.go
150 lines (125 loc) · 3.85 KB
/
test_utils.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
package integration
import (
"encoding/json"
"fmt"
"math/rand"
"os"
"testing"
"time"
"github.com/stretchr/testify/require"
abci "github.com/cometbft/cometbft/abci/types"
dbm "github.com/cosmos/cosmos-db"
ibctesting "github.com/cosmos/ibc-go/v8/testing"
"cosmossdk.io/log"
bam "github.com/cosmos/cosmos-sdk/baseapp"
"github.com/cosmos/cosmos-sdk/client"
cryptotypes "github.com/cosmos/cosmos-sdk/crypto/types"
simtestutil "github.com/cosmos/cosmos-sdk/testutil/sims"
sdk "github.com/cosmos/cosmos-sdk/types"
gaiaApp "github.com/cosmos/gaia/v19/app"
)
var app *gaiaApp.GaiaApp
// Some tests require a random directory to be created when runnin IBC testing suite with gaia.
// This is due to how CosmWasmVM initializes the VM - all IBC testing apps must have different dirs so they don't conflict.
func GaiaAppIniterRandomDir() (ibctesting.TestingApp, map[string]json.RawMessage) {
tmpDir, err := os.MkdirTemp("", "")
if err != nil {
panic(err)
}
app = gaiaApp.NewGaiaApp(
log.NewNopLogger(),
dbm.NewMemDB(),
nil,
true,
map[int64]bool{},
tmpDir,
gaiaApp.EmptyAppOptions{},
gaiaApp.EmptyWasmOptions)
testApp := ibctesting.TestingApp(app)
return testApp, app.ModuleBasics.DefaultGenesis(app.AppCodec())
}
// GaiaAppIniter implements ibctesting.AppIniter for the gaia app
func GaiaAppIniter() (ibctesting.TestingApp, map[string]json.RawMessage) {
app = gaiaApp.NewGaiaApp(
log.NewNopLogger(),
dbm.NewMemDB(),
nil,
true,
map[int64]bool{},
gaiaApp.DefaultNodeHome,
gaiaApp.EmptyAppOptions{},
gaiaApp.EmptyWasmOptions)
testApp := ibctesting.TestingApp(app)
return testApp, app.ModuleBasics.DefaultGenesis(app.AppCodec())
}
// SendMsgs() behavior must be changed since the default one uses zero fees
func OverrideSendMsgs(chains map[string]*ibctesting.TestChain, feeAmount sdk.Coin, gasLimit uint64) {
for _, chain := range chains {
chain := chain
chain.SendMsgsOverride = func(msgs ...sdk.Msg) (*abci.ExecTxResult, error) {
return SendMsgsOverride(chain, feeAmount, gasLimit, msgs...)
}
}
}
func SendMsgsOverride(chain *ibctesting.TestChain, feeAmount sdk.Coin, gasLimit uint64, msgs ...sdk.Msg) (*abci.ExecTxResult, error) {
// ensure the chain has the latest time
chain.Coordinator.UpdateTimeForChain(chain)
// increment acc sequence regardless of success or failure tx execution
defer func() {
err := chain.SenderAccount.SetSequence(chain.SenderAccount.GetSequence() + 1)
if err != nil {
panic(err)
}
}()
resp, err := SignAndDeliver(
chain.TB,
chain.TxConfig,
chain.App.GetBaseApp(),
msgs,
chain.ChainID,
[]uint64{chain.SenderAccount.GetAccountNumber()},
[]uint64{chain.SenderAccount.GetSequence()},
true,
chain.CurrentHeader.GetTime(),
chain.NextVals.Hash(),
feeAmount,
gasLimit,
chain.SenderPrivKey,
)
if err != nil {
return nil, err
}
require.Len(chain.TB, resp.TxResults, 1)
txResult := resp.TxResults[0]
if txResult.Code != 0 {
return txResult, fmt.Errorf("%s/%d: %q", txResult.Codespace, txResult.Code, txResult.Log)
}
chain.Coordinator.IncrementTime()
return txResult, nil
}
func SignAndDeliver(
tb testing.TB, txCfg client.TxConfig, app *bam.BaseApp, msgs []sdk.Msg,
chainID string, accNums, accSeqs []uint64, expPass bool, blockTime time.Time, nextValHash []byte, feeAmount sdk.Coin, gasLimit uint64, priv ...cryptotypes.PrivKey,
) (*abci.ResponseFinalizeBlock, error) {
tb.Helper()
tx, err := simtestutil.GenSignedMockTx(
rand.New(rand.NewSource(time.Now().UnixNano())),
txCfg,
msgs,
sdk.Coins{feeAmount},
gasLimit,
chainID,
accNums,
accSeqs,
priv...,
)
require.NoError(tb, err)
txBytes, err := txCfg.TxEncoder()(tx)
require.NoError(tb, err)
return app.FinalizeBlock(&abci.RequestFinalizeBlock{
Height: app.LastBlockHeight() + 1,
Time: blockTime,
NextValidatorsHash: nextValHash,
Txs: [][]byte{txBytes},
})
}