forked from celestiaorg/celestia-app
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvalidate_txs.go
118 lines (107 loc) · 3.83 KB
/
validate_txs.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
package app
import (
"github.com/celestiaorg/celestia-app/pkg/blob"
"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/telemetry"
sdk "github.com/cosmos/cosmos-sdk/types"
tmbytes "github.com/tendermint/tendermint/libs/bytes"
"github.com/tendermint/tendermint/libs/log"
coretypes "github.com/tendermint/tendermint/types"
)
// separateTxs decodes raw tendermint txs into normal and blob txs.
func separateTxs(_ client.TxConfig, rawTxs [][]byte) ([][]byte, []blob.BlobTx) {
normalTxs := make([][]byte, 0, len(rawTxs))
blobTxs := make([]blob.BlobTx, 0, len(rawTxs))
for _, rawTx := range rawTxs {
bTx, isBlob := blob.UnmarshalBlobTx(rawTx)
if isBlob {
blobTxs = append(blobTxs, bTx)
} else {
normalTxs = append(normalTxs, rawTx)
}
}
return normalTxs, blobTxs
}
// FilterTxs applies the antehandler to all proposed transactions and removes transactions that return an error.
func FilterTxs(logger log.Logger, ctx sdk.Context, handler sdk.AnteHandler, txConfig client.TxConfig, txs [][]byte) [][]byte {
normalTxs, blobTxs := separateTxs(txConfig, txs)
normalTxs, ctx = filterStdTxs(logger, txConfig.TxDecoder(), ctx, handler, normalTxs)
blobTxs, _ = filterBlobTxs(logger, txConfig.TxDecoder(), ctx, handler, blobTxs)
return append(normalTxs, encodeBlobTxs(blobTxs)...)
}
// filterStdTxs applies the provided antehandler to each transaction and removes
// transactions that return an error. Panics are caught by the checkTxValidity
// function used to apply the ante handler.
func filterStdTxs(logger log.Logger, dec sdk.TxDecoder, ctx sdk.Context, handler sdk.AnteHandler, txs [][]byte) ([][]byte, sdk.Context) {
n := 0
for _, tx := range txs {
sdkTx, err := dec(tx)
if err != nil {
logger.Error("decoding already checked transaction", "tx", tmbytes.HexBytes(coretypes.Tx(tx).Hash()), "error", err)
continue
}
ctx, err = handler(ctx, sdkTx, false)
// either the transaction is invalid (ie incorrect nonce) and we
// simply want to remove this tx, or we're catching a panic from one
// of the anteHanders which is logged.
if err != nil {
logger.Error(
"filtering already checked transaction",
"tx", tmbytes.HexBytes(coretypes.Tx(tx).Hash()),
"error", err,
"msgs", msgTypes(sdkTx),
)
telemetry.IncrCounter(1, "prepare_proposal", "invalid_std_txs")
continue
}
txs[n] = tx
n++
}
return txs[:n], ctx
}
// filterBlobTxs applies the provided antehandler to each transaction
// and removes transactions that return an error. Panics are caught by the checkTxValidity
// function used to apply the ante handler.
func filterBlobTxs(logger log.Logger, dec sdk.TxDecoder, ctx sdk.Context, handler sdk.AnteHandler, txs []blob.BlobTx) ([]blob.BlobTx, sdk.Context) {
n := 0
for _, tx := range txs {
sdkTx, err := dec(tx.Tx)
if err != nil {
logger.Error("decoding already checked blob transaction", "tx", tmbytes.HexBytes(coretypes.Tx(tx.Tx).Hash()), "error", err)
continue
}
ctx, err = handler(ctx, sdkTx, false)
// either the transaction is invalid (ie incorrect nonce) and we
// simply want to remove this tx, or we're catching a panic from one
// of the anteHanders which is logged.
if err != nil {
logger.Error(
"filtering already checked blob transaction", "tx", tmbytes.HexBytes(coretypes.Tx(tx.Tx).Hash()), "error", err,
)
telemetry.IncrCounter(1, "prepare_proposal", "invalid_blob_txs")
continue
}
txs[n] = tx
n++
}
return txs[:n], ctx
}
func msgTypes(sdkTx sdk.Tx) []string {
msgs := sdkTx.GetMsgs()
msgNames := make([]string, len(msgs))
for i, msg := range msgs {
msgNames[i] = sdk.MsgTypeURL(msg)
}
return msgNames
}
func encodeBlobTxs(blobTxs []blob.BlobTx) [][]byte {
txs := make([][]byte, len(blobTxs))
var err error
for i, tx := range blobTxs {
txs[i], err = blob.MarshalBlobTx(tx.Tx, tx.Blobs...)
if err != nil {
panic(err)
}
}
return txs
}