From d1f6a9f544528b41495dd3f3281afdf89aaa6b6b Mon Sep 17 00:00:00 2001 From: Justus Date: Wed, 9 Mar 2022 03:44:53 -0600 Subject: [PATCH] core/types: improve error for too short transaction / receipt encoding (#24256) Co-authored-by: Felix Lange --- cmd/evm/testdata/15/exp3.json | 10 +++++----- core/types/receipt.go | 24 +++++------------------- core/types/receipt_test.go | 2 +- core/types/transaction.go | 10 ++++------ core/types/transaction_test.go | 2 +- 5 files changed, 16 insertions(+), 32 deletions(-) diff --git a/cmd/evm/testdata/15/exp3.json b/cmd/evm/testdata/15/exp3.json index 6c46d267cf..d7606a2073 100644 --- a/cmd/evm/testdata/15/exp3.json +++ b/cmd/evm/testdata/15/exp3.json @@ -21,19 +21,19 @@ "error": "transaction type not supported" }, { - "error": "rlp: expected List" + "error": "typed transaction too short" }, { - "error": "rlp: expected List" + "error": "typed transaction too short" }, { - "error": "rlp: expected List" + "error": "typed transaction too short" }, { - "error": "rlp: expected List" + "error": "typed transaction too short" }, { - "error": "rlp: expected List" + "error": "typed transaction too short" }, { "error": "rlp: expected input list for types.AccessListTx" diff --git a/core/types/receipt.go b/core/types/receipt.go index 2f8a4c3235..03e2d7500e 100644 --- a/core/types/receipt.go +++ b/core/types/receipt.go @@ -38,8 +38,7 @@ var ( receiptStatusSuccessfulRLP = []byte{0x01} ) -// This error is returned when a typed receipt is decoded, but the string is empty. -var errEmptyTypedReceipt = errors.New("empty typed receipt bytes") +var errShortTypedReceipt = errors.New("typed receipt too short") const ( // ReceiptStatusFailed is the status code of a transaction if execution failed. @@ -182,26 +181,13 @@ func (r *Receipt) DecodeRLP(s *rlp.Stream) error { } r.Type = LegacyTxType return r.setFromRLP(dec) - case kind == rlp.String: + default: // It's an EIP-2718 typed tx receipt. b, err := s.Bytes() if err != nil { return err } - if len(b) == 0 { - return errEmptyTypedReceipt - } - r.Type = b[0] - if r.Type == AccessListTxType || r.Type == DynamicFeeTxType { - var dec receiptRLP - if err := rlp.DecodeBytes(b[1:], &dec); err != nil { - return err - } - return r.setFromRLP(dec) - } - return ErrTxTypeNotSupported - default: - return rlp.ErrExpectedList + return r.decodeTyped(b) } } @@ -224,8 +210,8 @@ func (r *Receipt) UnmarshalBinary(b []byte) error { // decodeTyped decodes a typed receipt from the canonical format. func (r *Receipt) decodeTyped(b []byte) error { - if len(b) == 0 { - return errEmptyTypedReceipt + if len(b) <= 1 { + return errShortTypedReceipt } switch b[0] { case DynamicFeeTxType, AccessListTxType: diff --git a/core/types/receipt_test.go b/core/types/receipt_test.go index 613559a658..bba18d2a7b 100644 --- a/core/types/receipt_test.go +++ b/core/types/receipt_test.go @@ -86,7 +86,7 @@ func TestDecodeEmptyTypedReceipt(t *testing.T) { input := []byte{0x80} var r Receipt err := rlp.DecodeBytes(input, &r) - if err != errEmptyTypedReceipt { + if err != errShortTypedReceipt { t.Fatal("wrong error:", err) } } diff --git a/core/types/transaction.go b/core/types/transaction.go index 83f1766e67..29820a0d78 100644 --- a/core/types/transaction.go +++ b/core/types/transaction.go @@ -37,7 +37,7 @@ var ( ErrInvalidTxType = errors.New("transaction type not valid in this context") ErrTxTypeNotSupported = errors.New("transaction type not supported") ErrGasFeeCapTooLow = errors.New("fee cap less than base fee") - errEmptyTypedTx = errors.New("empty typed transaction bytes") + errShortTypedTx = errors.New("typed transaction too short") ) // Transaction types. @@ -134,7 +134,7 @@ func (tx *Transaction) DecodeRLP(s *rlp.Stream) error { tx.setDecoded(&inner, int(rlp.ListSize(size))) } return err - case kind == rlp.String: + default: // It's an EIP-2718 typed TX envelope. var b []byte if b, err = s.Bytes(); err != nil { @@ -145,8 +145,6 @@ func (tx *Transaction) DecodeRLP(s *rlp.Stream) error { tx.setDecoded(inner, len(b)) } return err - default: - return rlp.ErrExpectedList } } @@ -174,8 +172,8 @@ func (tx *Transaction) UnmarshalBinary(b []byte) error { // decodeTyped decodes a typed transaction from the canonical format. func (tx *Transaction) decodeTyped(b []byte) (TxData, error) { - if len(b) == 0 { - return nil, errEmptyTypedTx + if len(b) <= 1 { + return nil, errShortTypedTx } switch b[0] { case AccessListTxType: diff --git a/core/types/transaction_test.go b/core/types/transaction_test.go index 58c95071b2..a4755675cd 100644 --- a/core/types/transaction_test.go +++ b/core/types/transaction_test.go @@ -76,7 +76,7 @@ func TestDecodeEmptyTypedTx(t *testing.T) { input := []byte{0x80} var tx Transaction err := rlp.DecodeBytes(input, &tx) - if err != errEmptyTypedTx { + if err != errShortTypedTx { t.Fatal("wrong error:", err) } }