-
Notifications
You must be signed in to change notification settings - Fork 0
/
other_msgs.go
83 lines (70 loc) · 2.3 KB
/
other_msgs.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
package prepare
import (
"fmt"
"time"
gometrics "github.com/armon/go-metrics"
"github.com/cosmos/gogoproto/proto"
"github.com/cosmos/cosmos-sdk/telemetry"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/jinxprotocol/v4-chain/protocol/lib/ante"
"github.com/jinxprotocol/v4-chain/protocol/lib/metrics"
)
// GetGroupMsgOther returns two separate slices of byte txs given a single slice of byte txs and max bytes.
// The first slice contains the first N txs where the total bytes of the N txs is <= max bytes.
// The second slice contains the rest of txs, if any.
func GetGroupMsgOther(availableTxs [][]byte, maxBytes uint64) ([][]byte, [][]byte) {
var (
txsToInclude [][]byte
txsRemainder [][]byte
byteCount uint64
)
for _, tx := range availableTxs {
byteCount += uint64(len(tx))
if byteCount <= maxBytes {
txsToInclude = append(txsToInclude, tx)
} else {
txsRemainder = append(txsRemainder, tx)
}
}
return txsToInclude, txsRemainder
}
// RemoveDisallowMsgs removes any txs that contain a disallowed msg.
func RemoveDisallowMsgs(ctx sdk.Context, decoder sdk.TxDecoder, txs [][]byte) [][]byte {
defer telemetry.ModuleMeasureSince(
ModuleName,
time.Now(),
metrics.RemoveDisallowMsgs,
metrics.Latency,
)
var filteredTxs [][]byte
for _, txBytes := range txs {
// Decode tx so we can read msgs.
tx, err := decoder(txBytes)
if err != nil {
ctx.Logger().Error(fmt.Sprintf("RemoveDisallowMsgs: failed to decode tx: %v", err))
continue // continue to next tx.
}
// For each msg in tx, check if it is disallowed.
containsDisllowMsg := false
for _, msg := range tx.GetMsgs() {
if ante.IsDisallowExternalSubmitMsg(msg) {
telemetry.IncrCounterWithLabels(
[]string{ModuleName, metrics.RemoveDisallowMsgs, metrics.DisallowMsg, metrics.Count},
1,
[]gometrics.Label{metrics.GetLabelForStringValue(metrics.Detail, proto.MessageName(msg))},
)
containsDisllowMsg = true
break // break out of loop over msgs.
}
}
// If tx contains disallowed msg, skip it.
if containsDisllowMsg {
ctx.Logger().Error(
fmt.Sprintf("RemoveDisallowMsgs: skipping tx with disallowed msg. Size: %d", len(txBytes)))
continue // continue to next tx.
}
// Otherwise, add tx to filtered txs.
filteredTxs = append(filteredTxs, txBytes)
}
return filteredTxs
}