Skip to content

Commit 7cd522c

Browse files
Juicestusfjl
authored andcommitted
core/types: improve error for too short transaction / receipt encoding (ethereum#24256)
Co-authored-by: Felix Lange <fjl@twurst.com>
1 parent 08797f7 commit 7cd522c

File tree

5 files changed

+16
-32
lines changed

5 files changed

+16
-32
lines changed

cmd/evm/testdata/15/exp3.json

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,19 +21,19 @@
2121
"error": "transaction type not supported"
2222
},
2323
{
24-
"error": "rlp: expected List"
24+
"error": "typed transaction too short"
2525
},
2626
{
27-
"error": "rlp: expected List"
27+
"error": "typed transaction too short"
2828
},
2929
{
30-
"error": "rlp: expected List"
30+
"error": "typed transaction too short"
3131
},
3232
{
33-
"error": "rlp: expected List"
33+
"error": "typed transaction too short"
3434
},
3535
{
36-
"error": "rlp: expected List"
36+
"error": "typed transaction too short"
3737
},
3838
{
3939
"error": "rlp: expected input list for types.AccessListTx"

core/types/receipt.go

Lines changed: 5 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,7 @@ var (
3838
receiptStatusSuccessfulRLP = []byte{0x01}
3939
)
4040

41-
// This error is returned when a typed receipt is decoded, but the string is empty.
42-
var errEmptyTypedReceipt = errors.New("empty typed receipt bytes")
41+
var errShortTypedReceipt = errors.New("typed receipt too short")
4342

4443
const (
4544
// ReceiptStatusFailed is the status code of a transaction if execution failed.
@@ -182,26 +181,13 @@ func (r *Receipt) DecodeRLP(s *rlp.Stream) error {
182181
}
183182
r.Type = LegacyTxType
184183
return r.setFromRLP(dec)
185-
case kind == rlp.String:
184+
default:
186185
// It's an EIP-2718 typed tx receipt.
187186
b, err := s.Bytes()
188187
if err != nil {
189188
return err
190189
}
191-
if len(b) == 0 {
192-
return errEmptyTypedReceipt
193-
}
194-
r.Type = b[0]
195-
if r.Type == AccessListTxType || r.Type == DynamicFeeTxType {
196-
var dec receiptRLP
197-
if err := rlp.DecodeBytes(b[1:], &dec); err != nil {
198-
return err
199-
}
200-
return r.setFromRLP(dec)
201-
}
202-
return ErrTxTypeNotSupported
203-
default:
204-
return rlp.ErrExpectedList
190+
return r.decodeTyped(b)
205191
}
206192
}
207193

@@ -224,8 +210,8 @@ func (r *Receipt) UnmarshalBinary(b []byte) error {
224210

225211
// decodeTyped decodes a typed receipt from the canonical format.
226212
func (r *Receipt) decodeTyped(b []byte) error {
227-
if len(b) == 0 {
228-
return errEmptyTypedReceipt
213+
if len(b) <= 1 {
214+
return errShortTypedReceipt
229215
}
230216
switch b[0] {
231217
case DynamicFeeTxType, AccessListTxType:

core/types/receipt_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ func TestDecodeEmptyTypedReceipt(t *testing.T) {
8686
input := []byte{0x80}
8787
var r Receipt
8888
err := rlp.DecodeBytes(input, &r)
89-
if err != errEmptyTypedReceipt {
89+
if err != errShortTypedReceipt {
9090
t.Fatal("wrong error:", err)
9191
}
9292
}

core/types/transaction.go

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ var (
3737
ErrInvalidTxType = errors.New("transaction type not valid in this context")
3838
ErrTxTypeNotSupported = errors.New("transaction type not supported")
3939
ErrGasFeeCapTooLow = errors.New("fee cap less than base fee")
40-
errEmptyTypedTx = errors.New("empty typed transaction bytes")
40+
errShortTypedTx = errors.New("typed transaction too short")
4141
)
4242

4343
// Transaction types.
@@ -134,7 +134,7 @@ func (tx *Transaction) DecodeRLP(s *rlp.Stream) error {
134134
tx.setDecoded(&inner, int(rlp.ListSize(size)))
135135
}
136136
return err
137-
case kind == rlp.String:
137+
default:
138138
// It's an EIP-2718 typed TX envelope.
139139
var b []byte
140140
if b, err = s.Bytes(); err != nil {
@@ -145,8 +145,6 @@ func (tx *Transaction) DecodeRLP(s *rlp.Stream) error {
145145
tx.setDecoded(inner, len(b))
146146
}
147147
return err
148-
default:
149-
return rlp.ErrExpectedList
150148
}
151149
}
152150

@@ -174,8 +172,8 @@ func (tx *Transaction) UnmarshalBinary(b []byte) error {
174172

175173
// decodeTyped decodes a typed transaction from the canonical format.
176174
func (tx *Transaction) decodeTyped(b []byte) (TxData, error) {
177-
if len(b) == 0 {
178-
return nil, errEmptyTypedTx
175+
if len(b) <= 1 {
176+
return nil, errShortTypedTx
179177
}
180178
switch b[0] {
181179
case AccessListTxType:

core/types/transaction_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ func TestDecodeEmptyTypedTx(t *testing.T) {
7676
input := []byte{0x80}
7777
var tx Transaction
7878
err := rlp.DecodeBytes(input, &tx)
79-
if err != errEmptyTypedTx {
79+
if err != errShortTypedTx {
8080
t.Fatal("wrong error:", err)
8181
}
8282
}

0 commit comments

Comments
 (0)