-
Notifications
You must be signed in to change notification settings - Fork 17
/
mev_check_tx.go
294 lines (252 loc) · 8.92 KB
/
mev_check_tx.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
283
284
285
286
287
288
289
290
291
292
293
294
package checktx
import (
"context"
"fmt"
cometabci "github.com/cometbft/cometbft/abci/types"
"github.com/cometbft/cometbft/libs/log"
cmtproto "github.com/cometbft/cometbft/proto/tendermint/types"
storetypes "github.com/cosmos/cosmos-sdk/store/types"
sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
"github.com/skip-mev/block-sdk/block"
mevlane "github.com/skip-mev/block-sdk/lanes/mev"
"github.com/skip-mev/block-sdk/x/auction/types"
)
// MevCheckTxHandler is a wrapper around baseapp's CheckTx method that allows us to
// verify bid transactions against the latest committed state. All other transactions
// are executed normally using base app's CheckTx. This defines all of the
// dependencies that are required to verify a bid transaction.
type MEVCheckTxHandler struct {
// baseApp is utilized to retrieve the latest committed state and to call
// baseapp's CheckTx method.
baseApp BaseApp
// txDecoder is utilized to decode transactions to determine if they are
// bid transactions.
txDecoder sdk.TxDecoder
// MEVLane is utilized to retrieve the bid info of a transaction and to
// insert a bid transaction into the application-side mempool.
mevLane MEVLaneI
// anteHandler is utilized to verify the bid transaction against the latest
// committed state.
anteHandler sdk.AnteHandler
// checkTxHandler is the wrapped CheckTx handler that is used to execute all non-bid txs
checkTxHandler CheckTx
// chainID is the chainID of the application.
chainID string
}
// MEVLaneI defines the interface for the mev auction lane. This interface
// is utilized by both the x/auction module and the checkTx handler.
type MEVLaneI interface {
block.Lane
mevlane.Factory
GetTopAuctionTx(ctx context.Context) sdk.Tx
}
// BaseApp is an interface that allows us to call baseapp's CheckTx method
// as well as retrieve the latest committed state.
type BaseApp interface {
// CommitMultiStore is utilized to retrieve the latest committed state.
CommitMultiStore() storetypes.CommitMultiStore
// Logger is utilized to log errors.
Logger() log.Logger
// LastBlockHeight is utilized to retrieve the latest block height.
LastBlockHeight() int64
// GetConsensusParams is utilized to retrieve the consensus params.
GetConsensusParams(ctx sdk.Context) *cmtproto.ConsensusParams
}
// NewCheckTxHandler constructs a new CheckTxHandler instance. This method fails if the given LanedMempool does not have a lane
// adhering to the MevLaneI interface
func NewMEVCheckTxHandler(
baseApp BaseApp,
txDecoder sdk.TxDecoder,
mevLane MEVLaneI,
anteHandler sdk.AnteHandler,
checkTxHandler CheckTx,
chainID string,
) *MEVCheckTxHandler {
return &MEVCheckTxHandler{
baseApp: baseApp,
txDecoder: txDecoder,
mevLane: mevLane,
anteHandler: anteHandler,
checkTxHandler: checkTxHandler,
chainID: chainID,
}
}
// CheckTxHandler is a wrapper around baseapp's CheckTx method that allows us to
// verify bid transactions against the latest committed state. All other transactions
// are executed normally. We must verify each bid tx and all of its bundled transactions
// before we can insert it into the mempool against the latest commit state because
// otherwise the auction can be griefed. No state changes are applied to the state
// during this process.
func (handler *MEVCheckTxHandler) CheckTx() CheckTx {
return func(req cometabci.RequestCheckTx) (resp cometabci.ResponseCheckTx) {
defer func() {
if rec := recover(); rec != nil {
handler.baseApp.Logger().Error(
"panic in check tx handler",
"err", rec,
)
err := fmt.Errorf("panic in check tx handler: %s", rec)
resp = sdkerrors.ResponseCheckTxWithEvents(
err,
0,
0,
nil,
false,
)
}
}()
tx, err := handler.txDecoder(req.Tx)
if err != nil {
handler.baseApp.Logger().Info(
"failed to decode tx",
"err", err,
)
return sdkerrors.ResponseCheckTxWithEvents(
fmt.Errorf("failed to decode tx: %w", err),
0,
0,
nil,
false,
)
}
// Attempt to get the bid info of the transaction.
bidInfo, err := handler.mevLane.GetAuctionBidInfo(tx)
if err != nil {
handler.baseApp.Logger().Info(
"failed to get auction bid info",
"err", err,
)
return sdkerrors.ResponseCheckTxWithEvents(
fmt.Errorf("failed to get auction bid info: %w", err),
0,
0,
nil,
false,
)
}
// If this is not a bid transaction, we just execute it normally.
if bidInfo == nil {
return handler.checkTxHandler(req)
}
// We attempt to get the latest committed state in order to verify transactions
// as if they were to be executed at the top of the block. After verification, this
// context will be discarded and will not apply any state changes.
ctx := handler.GetContextForBidTx(req)
// Verify the bid transaction.
gasInfo, err := handler.ValidateBidTx(ctx, tx, bidInfo)
if err != nil {
handler.baseApp.Logger().Info(
"invalid bid tx",
"err", err,
"height", ctx.BlockHeight(),
"bid_height", bidInfo.Timeout,
"bidder", bidInfo.Bidder,
"bid", bidInfo.Bid,
"is_recheck_tx", ctx.IsReCheckTx(),
)
// attempt to remove the bid from the MEVLane (if it exists)
if handler.mevLane.Contains(tx) {
if err := handler.mevLane.Remove(tx); err != nil {
handler.baseApp.Logger().Error(
"failed to remove bid transaction from mev-lane",
"err", err,
)
}
}
return sdkerrors.ResponseCheckTxWithEvents(
fmt.Errorf("invalid bid tx: %w", err),
gasInfo.GasWanted,
gasInfo.GasUsed,
nil,
false,
)
}
handler.baseApp.Logger().Info(
"valid bid tx",
"height", ctx.BlockHeight(),
"bid_height", bidInfo.Timeout,
"bidder", bidInfo.Bidder,
"bid", bidInfo.Bid,
"inserting tx into mempool", true,
)
// If the bid transaction is valid, we know we can insert it into the mempool for consideration in the next block.
if err := handler.mevLane.Insert(ctx, tx); err != nil {
handler.baseApp.Logger().Info(
"invalid bid tx; failed to insert bid transaction into mempool",
"err", err,
)
return sdkerrors.ResponseCheckTxWithEvents(
fmt.Errorf("invalid bid tx; failed to insert bid transaction into mempool: %w", err),
gasInfo.GasWanted,
gasInfo.GasUsed,
nil,
false,
)
}
return cometabci.ResponseCheckTx{
Code: cometabci.CodeTypeOK,
GasWanted: int64(gasInfo.GasWanted),
GasUsed: int64(gasInfo.GasUsed),
}
}
}
// ValidateBidTx is utilized to verify the bid transaction against the latest committed state.
func (handler *MEVCheckTxHandler) ValidateBidTx(ctx sdk.Context, bidTx sdk.Tx, bidInfo *types.BidInfo) (sdk.GasInfo, error) {
// Verify the bid transaction.
ctx, err := handler.anteHandler(ctx, bidTx, false)
if err != nil {
return sdk.GasInfo{}, fmt.Errorf("invalid bid tx; failed to execute ante handler: %w", err)
}
// Store the gas info and priority of the bid transaction before applying changes with other transactions.
gasInfo := sdk.GasInfo{
GasWanted: ctx.GasMeter().Limit(),
GasUsed: ctx.GasMeter().GasConsumed(),
}
// Verify all of the bundled transactions.
for _, tx := range bidInfo.Transactions {
bundledTx, err := handler.mevLane.WrapBundleTransaction(tx)
if err != nil {
return gasInfo, fmt.Errorf("invalid bid tx; failed to decode bundled tx: %w", err)
}
// bid txs cannot be included in bundled txs
bidInfo, err := handler.mevLane.GetAuctionBidInfo(bundledTx)
if err != nil {
return gasInfo, fmt.Errorf("invalid bid tx; failed to get bid info: %w", err)
}
if bidInfo != nil {
return gasInfo, fmt.Errorf("invalid bid tx; bundled tx cannot be a bid tx")
}
if ctx, err = handler.anteHandler(ctx, bundledTx, false); err != nil {
return gasInfo, fmt.Errorf("invalid bid tx; failed to execute bundled transaction: %w", err)
}
}
return gasInfo, nil
}
// GetContextForBidTx is returns the latest committed state and sets the context given
// the checkTx request.
func (handler *MEVCheckTxHandler) GetContextForBidTx(req cometabci.RequestCheckTx) sdk.Context {
// Retrieve the commit multi-store which is used to retrieve the latest committed state.
ms := handler.baseApp.CommitMultiStore().CacheMultiStore()
// Create a new context based off of the latest committed state.
header := cmtproto.Header{
Height: handler.baseApp.LastBlockHeight(),
ChainID: handler.chainID, // TODO: Replace with actual chain ID. This is currently not exposed by the app.
}
ctx, _ := sdk.NewContext(ms, header, true, handler.baseApp.Logger()).CacheContext()
// Set the context to the correct checking mode.
switch req.Type {
case cometabci.CheckTxType_New:
ctx = ctx.WithIsCheckTx(true)
case cometabci.CheckTxType_Recheck:
ctx = ctx.WithIsReCheckTx(true)
default:
panic("unknown check tx type")
}
// Set the remaining important context values.
ctx = ctx.
WithTxBytes(req.Tx).
WithEventManager(sdk.NewEventManager()).
WithConsensusParams(handler.baseApp.GetConsensusParams(ctx))
return ctx
}