Skip to content

Commit

Permalink
chore: merge msg types and count occurrence
Browse files Browse the repository at this point in the history
  • Loading branch information
rach-id committed Oct 10, 2024
1 parent 02b2241 commit 54583ae
Showing 1 changed file with 14 additions and 21 deletions.
35 changes: 14 additions & 21 deletions app/validate_txs.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,8 @@ func filterStdTxs(logger log.Logger, dec sdk.TxDecoder, ctx sdk.Context, handler
logger.Error("decoding already checked transaction", "tx", tmbytes.HexBytes(coretypes.Tx(tx).Hash()), "error", err)
continue
}
msgTypes := msgTypes(sdkTx)
if count := countOccurrence(msgTypes, sdk.MsgTypeURL(&types.MsgSend{})); count != 0 {
msgTypes, occurrences := msgTypes(sdkTx)
if count := occurrences[sdk.MsgTypeURL(&types.MsgSend{})]; count != 0 {
if msgSendTransactionCount+count > v3consts.MsgSendTransactionCap {
logger.Debug("skipping tx because the msg send transaction cap was reached", "tx", tmbytes.HexBytes(coretypes.Tx(tx).Hash()))
continue
Expand Down Expand Up @@ -97,8 +97,8 @@ func filterBlobTxs(logger log.Logger, dec sdk.TxDecoder, ctx sdk.Context, handle
logger.Error("decoding already checked blob transaction", "tx", tmbytes.HexBytes(coretypes.Tx(tx.Tx).Hash()), "error", err)
continue
}
msgTypes := msgTypes(sdkTx)
if count := countOccurrence(msgTypes, sdk.MsgTypeURL(&types2.MsgPayForBlobs{})); count != 0 {
_, occurrences := msgTypes(sdkTx)
if count := occurrences[sdk.MsgTypeURL(&types2.MsgPayForBlobs{})]; count != 0 {
if pfbTransactionCount+count > v3consts.PFBTransactionCap {
logger.Debug("skipping tx because the pfb transaction cap was reached", "tx", tmbytes.HexBytes(coretypes.Tx(tx.Tx).Hash()))
continue
Expand All @@ -124,13 +124,18 @@ func filterBlobTxs(logger log.Logger, dec sdk.TxDecoder, ctx sdk.Context, handle
return txs[:n], ctx
}

func msgTypes(sdkTx sdk.Tx) []string {
// msgTypes takes an sdk transaction and returns the types of the messages
// included in it along with.
func msgTypes(sdkTx sdk.Tx) ([]string, map[string]int) {
msgs := sdkTx.GetMsgs()
msgNames := make([]string, len(msgs))
for i, msg := range msgs {
msgNames[i] = sdk.MsgTypeURL(msg)
types := make([]string, 0, len(msgs))
occurrences := make(map[string]int)
for _, msg := range msgs {
msgType := sdk.MsgTypeURL(msg)
types = append(types, msgType)
occurrences[msgType]++
}
return msgNames
return types, occurrences
}

func encodeBlobTxs(blobTxs []*tx.BlobTx) [][]byte {
Expand All @@ -144,15 +149,3 @@ func encodeBlobTxs(blobTxs []*tx.BlobTx) [][]byte {
}
return txs
}

// countOccurrence takes a strings slice and counts the number
// of time the provided item exists in that slice.
func countOccurrence(slice []string, item string) int {
count := 0
for _, v := range slice {
if v == item {
count++
}
}
return count
}

0 comments on commit 54583ae

Please sign in to comment.