From b88f70de0e2ae447389d220537d7be84e63a74e1 Mon Sep 17 00:00:00 2001 From: "alex.sharov" Date: Sat, 7 Aug 2021 14:05:02 +0700 Subject: [PATCH] reject too large transactions --- txpool/types.go | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/txpool/types.go b/txpool/types.go index cb4069b46..439dfb90d 100644 --- a/txpool/types.go +++ b/txpool/types.go @@ -117,6 +117,22 @@ const ParseTransactionErrorPrefix = "parse transaction payload" // ParseTransaction extracts all the information from the transactions's payload (RLP) necessary to build TxSlot // it also performs syntactic validation of the transactions func (ctx *TxParseContext) ParseTransaction(payload []byte, pos int, slot *TxSlot, sender []byte) (p int, err error) { + const ( + // txSlotSize is used to calculate how many data slots a single transaction + // takes up based on its size. The slots are used as DoS protection, ensuring + // that validating a new transaction remains a constant operation (in reality + // O(maxslots), where max slots are 4 currently). + txSlotSize = 32 * 1024 + + // txMaxSize is the maximum size a single transaction can have. This field has + // non-trivial consequences: larger transactions are significantly harder and + // more expensive to propagate; larger transactions also take more resources + // to validate whether they fit into the pool or not. + txMaxSize = 4 * txSlotSize // 128KB + ) + if len(payload) > txMaxSize { + return 0, fmt.Errorf("%s: too large tx.size=%dKb", ParseTransactionErrorPrefix, len(payload)/1024) + } if len(payload) == 0 { return 0, fmt.Errorf("%s: empty rlp", ParseTransactionErrorPrefix) }