forked from EscanBE/evermint
/
chain_suite_abci.go
282 lines (241 loc) · 7.93 KB
/
chain_suite_abci.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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
package integration_test_util
//goland:noinspection SpellCheckingInspection
import (
"context"
errorsmod "cosmossdk.io/errors"
sdkmath "cosmossdk.io/math"
"fmt"
itutiltypes "github.com/servprotocolorg/serv/v12/integration_test_util/types"
evmtypes "github.com/servprotocolorg/serv/v12/x/evm/types"
abci "github.com/cometbft/cometbft/abci/types"
tmproto "github.com/cometbft/cometbft/proto/tendermint/types"
coretypes "github.com/cometbft/cometbft/rpc/core/types"
tmtypes "github.com/cometbft/cometbft/types"
"github.com/cosmos/cosmos-sdk/codec"
sdk "github.com/cosmos/cosmos-sdk/types"
errortypes "github.com/cosmos/cosmos-sdk/types/errors"
authsigning "github.com/cosmos/cosmos-sdk/x/auth/signing"
"github.com/cosmos/gogoproto/proto"
"github.com/pkg/errors"
"time"
)
// commitAndCreateNewCtx commits a block at a given time, creating and return a new ctx for the next block
func (suite *ChainIntegrationTestSuite) commitAndCreateNewCtx(ctx sdk.Context, t time.Duration, vs *tmtypes.ValidatorSet) (sdk.Context, *tmtypes.ValidatorSet, error) {
header, nextVs, err := suite.commit(ctx, t, vs)
if err != nil {
return ctx, nil, err
}
newCtx := suite.createNewContext(ctx, header)
return newCtx, nextVs, nil
}
// createNewContext returns a new sdk.Context with the same settings as the old one
func (suite *ChainIntegrationTestSuite) createNewContext(oldCtx sdk.Context, header tmproto.Header) sdk.Context {
// NewContext function keeps the multistore
// but resets other context fields
// GasMeter is set as InfiniteGasMeter
var newCtx sdk.Context
if suite.HasTendermint() {
newCtx = sdk.NewContext(suite.BaseApp().CommitMultiStore(), header, false, suite.BaseApp().Logger())
} else {
newCtx = suite.BaseApp().NewContext(false, header)
}
// set the reset-ted fields to keep the current ctx settings
newCtx = newCtx.WithMinGasPrices(oldCtx.MinGasPrices())
newCtx = newCtx.WithEventManager(oldCtx.EventManager())
newCtx = newCtx.WithKVGasConfig(oldCtx.KVGasConfig())
newCtx = newCtx.WithTransientKVGasConfig(oldCtx.TransientKVGasConfig())
return newCtx
}
// DeliverTx delivers a Cosmos tx for a given set of msgs.
// The delivery mode is SYNC
func (suite *ChainIntegrationTestSuite) DeliverTx(
ctx sdk.Context,
signer *itutiltypes.TestAccount,
gasPrice *sdkmath.Int,
msgs ...sdk.Msg,
) (authsigning.Tx, abci.ResponseDeliverTx, error) {
suite.Require().NotNil(signer)
tx, err := suite.PrepareCosmosTx(
ctx,
signer,
CosmosTxArgs{
Gas: 10_000_000,
GasPrice: gasPrice,
Msgs: msgs,
},
)
if err != nil {
return nil, abci.ResponseDeliverTx{}, err
}
resDeliverTx, err := suite.BroadcastTx(tx)
return tx, resDeliverTx, err
}
// DeliverTxAsync is the same as DeliverTx but with Async delivery mode.
func (suite *ChainIntegrationTestSuite) DeliverTxAsync(
ctx sdk.Context,
signer *itutiltypes.TestAccount,
gasPrice *sdkmath.Int,
msgs ...sdk.Msg,
) (*coretypes.ResultBroadcastTx, error) {
suite.Require().NotNil(signer)
tx, err := suite.PrepareCosmosTx(
ctx,
signer,
CosmosTxArgs{
Gas: 10_000_000,
GasPrice: gasPrice,
Msgs: msgs,
},
)
if err != nil {
return nil, err
}
return suite.BroadcastTxAsync(tx)
}
// DeliverEthTx generates and broadcasts MsgEthereumTx message populated within a Cosmos tx.
// The delivery mode is SYNC
func (suite *ChainIntegrationTestSuite) DeliverEthTx(
signer *itutiltypes.TestAccount,
ethMsg *evmtypes.MsgEthereumTx,
) (*itutiltypes.ResponseDeliverEthTx, error) {
suite.Require().NotNil(signer)
tx, err := suite.PrepareEthTx(signer, ethMsg)
if err != nil {
return nil, err
}
responseDeliverTx, err := suite.BroadcastTx(tx)
if err != nil {
return nil, err
}
res := itutiltypes.NewResponseDeliverEthTx(&responseDeliverTx)
if _, err := checkEthTxResponse(responseDeliverTx, suite.EncodingConfig.Codec); err != nil {
return res, err
}
return res, nil
}
// DeliverEthTxAsync is the same as DeliverEthTx but with Async delivery mode.
func (suite *ChainIntegrationTestSuite) DeliverEthTxAsync(
account *itutiltypes.TestAccount,
ethMsg *evmtypes.MsgEthereumTx,
) error {
suite.Require().NotNil(account)
tx, err := suite.PrepareEthTx(account, ethMsg)
if err != nil {
return err
}
_, err = suite.BroadcastTxAsync(tx)
return err
}
// BroadcastTx does broadcast a tx over the network and returns the response
// The delivery mode is SYNC
func (suite *ChainIntegrationTestSuite) BroadcastTx(tx sdk.Tx) (responseDeliverTx abci.ResponseDeliverTx, err error) {
// bz are bytes to be broadcast over the network
var bz []byte
bz, err = suite.EncodingConfig.TxConfig.TxEncoder()(tx)
if err == nil {
if suite.HasTendermint() {
res, err := suite.QueryClients.TendermintRpcHttpClient.BroadcastTxCommit(context.Background(), bz)
suite.Require().NoError(err)
responseDeliverTx = res.DeliverTx
} else {
responseDeliverTx = suite.BaseApp().DeliverTx(
abci.RequestDeliverTx{
Tx: bz,
},
)
}
if responseDeliverTx.Code != 0 {
err = errorsmod.Wrapf(errortypes.ErrInvalidRequest, responseDeliverTx.Log)
responseDeliverTx = abci.ResponseDeliverTx{} // purge
}
}
return
}
// BroadcastTxAsync is the same as BroadcastTx but with Async delivery mode.
func (suite *ChainIntegrationTestSuite) BroadcastTxAsync(tx sdk.Tx) (resultBroadcastTx *coretypes.ResultBroadcastTx, err error) {
suite.EnsureTendermint()
// bz are bytes to be broadcast over the network
var bz []byte
bz, err = suite.EncodingConfig.TxConfig.TxEncoder()(tx)
if err == nil {
res, err := suite.QueryClients.TendermintRpcHttpClient.BroadcastTxAsync(context.Background(), bz)
suite.Require().NoError(err)
resultBroadcastTx = res
}
return
}
// commit is helper function, it:
//
// - Runs the EndBlocker logic.
//
// - Commits the changes.
//
// - Updates the header.
//
// - Runs the BeginBlocker logic.
//
// - Finally, returns the updated header.
func (suite *ChainIntegrationTestSuite) commit(ctx sdk.Context, t time.Duration, vs *tmtypes.ValidatorSet) (tmproto.Header, *tmtypes.ValidatorSet, error) {
var nextVals *tmtypes.ValidatorSet
baseApp := suite.BaseApp()
header := ctx.BlockHeader()
res := baseApp.EndBlock(abci.RequestEndBlock{
Height: header.Height,
})
if vs != nil {
var err error
nextVals, err = applyValSetChanges(vs, res.ValidatorUpdates)
if err != nil {
return header, nil, err
}
header.ValidatorsHash = vs.Hash()
header.NextValidatorsHash = nextVals.Hash()
}
_ = baseApp.Commit()
header.Height++
header.Time = header.Time.Add(t)
header.AppHash = baseApp.LastCommitID().Hash
baseApp.BeginBlock(abci.RequestBeginBlock{
Header: header,
})
return header, nextVals, nil
}
// applyValSetChanges applies the validator set changes to the given validator set
func applyValSetChanges(valSet *tmtypes.ValidatorSet, valUpdates []abci.ValidatorUpdate) (*tmtypes.ValidatorSet, error) {
updates, err := tmtypes.PB2TM.ValidatorUpdates(valUpdates)
if err != nil {
return nil, err
}
// must copy since validator set will mutate with UpdateWithChangeSet
newVals := valSet.Copy()
err = newVals.UpdateWithChangeSet(updates)
if err != nil {
return nil, err
}
return newVals, nil
}
func checkEthTxResponse(r abci.ResponseDeliverTx, cdc codec.Codec) ([]*evmtypes.MsgEthereumTxResponse, error) {
if !r.IsOK() {
return nil, fmt.Errorf("tx failed. Code: %d, Logs: %s", r.Code, r.Log)
}
var txData sdk.TxMsgData
if err := cdc.Unmarshal(r.Data, &txData); err != nil {
return nil, err
}
if len(txData.MsgResponses) == 0 {
return nil, fmt.Errorf("no message responses found")
}
responses := make([]*evmtypes.MsgEthereumTxResponse, 0, len(txData.MsgResponses))
for i := range txData.MsgResponses {
var res evmtypes.MsgEthereumTxResponse
if err := proto.Unmarshal(txData.MsgResponses[i].Value, &res); err != nil {
// TODO use corresponding proto for each chain
return nil, errors.Wrap(err, "failed to unmarshal proto")
}
if res.Failed() {
return nil, fmt.Errorf("tx failed. VmError: %s", res.VmError)
}
responses = append(responses, &res)
}
return responses, nil
}