forked from evmos/evmos
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathabci.go
259 lines (223 loc) · 7.64 KB
/
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
// Copyright Tharsis Labs Ltd.(Evmos)
// SPDX-License-Identifier:ENCL-1.0(https://github.com/evmos/evmos/blob/main/LICENSE)
package testutil
import (
"time"
errorsmod "cosmossdk.io/errors"
sdkmath "cosmossdk.io/math"
cryptotypes "github.com/cosmos/cosmos-sdk/crypto/types"
sdk "github.com/cosmos/cosmos-sdk/types"
errortypes "github.com/cosmos/cosmos-sdk/types/errors"
abci "github.com/tendermint/tendermint/abci/types"
tmproto "github.com/tendermint/tendermint/proto/tendermint/types"
tmtypes "github.com/tendermint/tendermint/types"
"github.com/evmos/evmos/v14/app"
"github.com/evmos/evmos/v14/encoding"
"github.com/evmos/evmos/v14/testutil/tx"
)
// Commit commits a block at a given time. Reminder: At the end of each
// Tendermint Consensus round the following methods are run
// 1. BeginBlock
// 2. DeliverTx
// 3. EndBlock
// 4. Commit
func Commit(ctx sdk.Context, app *app.Evmos, t time.Duration, vs *tmtypes.ValidatorSet) (sdk.Context, error) {
header, err := commit(ctx, app, t, vs)
if err != nil {
return ctx, err
}
return ctx.WithBlockHeader(header), nil
}
// CommitAndCreateNewCtx commits a block at a given time creating a ctx with the current settings
// This is useful to keep test settings that could be affected by EndBlockers, e.g.
// setting a baseFee == 0 and expecting this condition to continue after commit
func CommitAndCreateNewCtx(ctx sdk.Context, app *app.Evmos, t time.Duration, vs *tmtypes.ValidatorSet) (sdk.Context, error) {
header, err := commit(ctx, app, t, vs)
if err != nil {
return ctx, err
}
// NewContext function keeps the multistore
// but resets other context fields
// GasMeter is set as InfiniteGasMeter
newCtx := app.BaseApp.NewContext(false, header)
// set the reseted fields to keep the current ctx settings
newCtx = newCtx.WithMinGasPrices(ctx.MinGasPrices())
newCtx = newCtx.WithEventManager(ctx.EventManager())
newCtx = newCtx.WithKVGasConfig(ctx.KVGasConfig())
newCtx = newCtx.WithTransientKVGasConfig(ctx.TransientKVGasConfig())
return newCtx, nil
}
// DeliverTx delivers a cosmos tx for a given set of msgs
func DeliverTx(
ctx sdk.Context,
appEvmos *app.Evmos,
priv cryptotypes.PrivKey,
gasPrice *sdkmath.Int,
msgs ...sdk.Msg,
) (abci.ResponseDeliverTx, error) {
txConfig := encoding.MakeConfig(app.ModuleBasics).TxConfig
tx, err := tx.PrepareCosmosTx(
ctx,
appEvmos,
tx.CosmosTxArgs{
TxCfg: txConfig,
Priv: priv,
ChainID: ctx.ChainID(),
Gas: 10_000_000,
GasPrice: gasPrice,
Msgs: msgs,
},
)
if err != nil {
return abci.ResponseDeliverTx{}, err
}
return BroadcastTxBytes(appEvmos, txConfig.TxEncoder(), tx)
}
// DeliverEthTx generates and broadcasts a Cosmos Tx populated with MsgEthereumTx messages.
// If a private key is provided, it will attempt to sign all messages with the given private key,
// otherwise, it will assume the messages have already been signed.
func DeliverEthTx(
appEvmos *app.Evmos,
priv cryptotypes.PrivKey,
msgs ...sdk.Msg,
) (abci.ResponseDeliverTx, error) {
txConfig := encoding.MakeConfig(app.ModuleBasics).TxConfig
tx, err := tx.PrepareEthTx(txConfig, appEvmos, priv, msgs...)
if err != nil {
return abci.ResponseDeliverTx{}, err
}
res, err := BroadcastTxBytes(appEvmos, txConfig.TxEncoder(), tx)
if err != nil {
return abci.ResponseDeliverTx{}, err
}
codec := encoding.MakeConfig(app.ModuleBasics).Codec
if _, err := CheckEthTxResponse(res, codec); err != nil {
return abci.ResponseDeliverTx{}, err
}
return res, nil
}
// DeliverEthTxWithoutCheck generates and broadcasts a Cosmos Tx populated with MsgEthereumTx messages.
// If a private key is provided, it will attempt to sign all messages with the given private key,
// otherwise, it will assume the messages have already been signed. It does not check if the Eth tx is
// successful or not.
func DeliverEthTxWithoutCheck(
appEvmos *app.Evmos,
priv cryptotypes.PrivKey,
msgs ...sdk.Msg,
) (abci.ResponseDeliverTx, error) {
txConfig := encoding.MakeConfig(app.ModuleBasics).TxConfig
tx, err := tx.PrepareEthTx(txConfig, appEvmos, priv, msgs...)
if err != nil {
return abci.ResponseDeliverTx{}, err
}
res, err := BroadcastTxBytes(appEvmos, txConfig.TxEncoder(), tx)
if err != nil {
return abci.ResponseDeliverTx{}, err
}
return res, nil
}
// CheckTx checks a cosmos tx for a given set of msgs
func CheckTx(
ctx sdk.Context,
appEvmos *app.Evmos,
priv cryptotypes.PrivKey,
gasPrice *sdkmath.Int,
msgs ...sdk.Msg,
) (abci.ResponseCheckTx, error) {
txConfig := encoding.MakeConfig(app.ModuleBasics).TxConfig
tx, err := tx.PrepareCosmosTx(
ctx,
appEvmos,
tx.CosmosTxArgs{
TxCfg: txConfig,
Priv: priv,
ChainID: ctx.ChainID(),
GasPrice: gasPrice,
Gas: 10_000_000,
Msgs: msgs,
},
)
if err != nil {
return abci.ResponseCheckTx{}, err
}
return checkTxBytes(appEvmos, txConfig.TxEncoder(), tx)
}
// CheckEthTx checks a Ethereum tx for a given set of msgs
func CheckEthTx(
appEvmos *app.Evmos,
priv cryptotypes.PrivKey,
msgs ...sdk.Msg,
) (abci.ResponseCheckTx, error) {
txConfig := encoding.MakeConfig(app.ModuleBasics).TxConfig
tx, err := tx.PrepareEthTx(txConfig, appEvmos, priv, msgs...)
if err != nil {
return abci.ResponseCheckTx{}, err
}
return checkTxBytes(appEvmos, txConfig.TxEncoder(), tx)
}
// BroadcastTxBytes encodes a transaction and calls DeliverTx on the app.
func BroadcastTxBytes(app *app.Evmos, txEncoder sdk.TxEncoder, tx sdk.Tx) (abci.ResponseDeliverTx, error) {
// bz are bytes to be broadcasted over the network
bz, err := txEncoder(tx)
if err != nil {
return abci.ResponseDeliverTx{}, err
}
req := abci.RequestDeliverTx{Tx: bz}
res := app.BaseApp.DeliverTx(req)
if res.Code != 0 {
return abci.ResponseDeliverTx{}, errorsmod.Wrapf(errortypes.ErrInvalidRequest, res.Log)
}
return res, nil
}
// commit is a private helper function that runs the EndBlocker logic, commits the changes,
// updates the header, runs the BeginBlocker function and returns the updated header
func commit(ctx sdk.Context, app *app.Evmos, t time.Duration, vs *tmtypes.ValidatorSet) (tmproto.Header, error) {
header := ctx.BlockHeader()
if vs != nil {
res := app.EndBlock(abci.RequestEndBlock{Height: header.Height})
nextVals, err := applyValSetChanges(vs, res.ValidatorUpdates)
if err != nil {
return header, err
}
header.ValidatorsHash = vs.Hash()
header.NextValidatorsHash = nextVals.Hash()
} else {
app.EndBlocker(ctx, abci.RequestEndBlock{Height: header.Height})
}
_ = app.Commit()
header.Height++
header.Time = header.Time.Add(t)
header.AppHash = app.LastCommitID().Hash
app.BeginBlock(abci.RequestBeginBlock{
Header: header,
})
return header, nil
}
// checkTxBytes encodes a transaction and calls checkTx on the app.
func checkTxBytes(app *app.Evmos, txEncoder sdk.TxEncoder, tx sdk.Tx) (abci.ResponseCheckTx, error) {
bz, err := txEncoder(tx)
if err != nil {
return abci.ResponseCheckTx{}, err
}
req := abci.RequestCheckTx{Tx: bz}
res := app.BaseApp.CheckTx(req)
if res.Code != 0 {
return abci.ResponseCheckTx{}, errorsmod.Wrapf(errortypes.ErrInvalidRequest, res.Log)
}
return res, nil
}
// applyValSetChanges takes in tmtypes.ValidatorSet and []abci.ValidatorUpdate and will return a new tmtypes.ValidatorSet which has the
// provided validator updates applied to the provided 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
}