mirror of
https://github.com/ethereum/go-ethereum.git
synced 2026-07-18 02:40:45 +00:00
core/types: improve error for too short transaction / receipt encoding (#24256)
This commit is contained in:
parent
d1dc9e383c
commit
7cb33cc57d
4 changed files with 10 additions and 27 deletions
|
|
@ -38,8 +38,7 @@ var (
|
||||||
receiptStatusSuccessfulRLP = []byte{0x01}
|
receiptStatusSuccessfulRLP = []byte{0x01}
|
||||||
)
|
)
|
||||||
|
|
||||||
// This error is returned when a typed receipt is decoded, but the string is empty.
|
var errShortTypedReceipt = errors.New("typed receipt too short")
|
||||||
var errEmptyTypedReceipt = errors.New("empty typed receipt bytes")
|
|
||||||
|
|
||||||
const (
|
const (
|
||||||
// ReceiptStatusFailed is the status code of a transaction if execution failed.
|
// ReceiptStatusFailed is the status code of a transaction if execution failed.
|
||||||
|
|
@ -166,26 +165,13 @@ func (r *Receipt) DecodeRLP(s *rlp.Stream) error {
|
||||||
}
|
}
|
||||||
r.Type = LegacyTxType
|
r.Type = LegacyTxType
|
||||||
return r.setFromRLP(dec)
|
return r.setFromRLP(dec)
|
||||||
case kind == rlp.String:
|
default:
|
||||||
// It's an EIP-2718 typed tx receipt.
|
// It's an EIP-2718 typed tx receipt.
|
||||||
b, err := s.Bytes()
|
b, err := s.Bytes()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
if len(b) == 0 {
|
return r.decodeTyped(b)
|
||||||
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
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -208,8 +194,8 @@ func (r *Receipt) UnmarshalBinary(b []byte) error {
|
||||||
|
|
||||||
// decodeTyped decodes a typed receipt from the canonical format.
|
// decodeTyped decodes a typed receipt from the canonical format.
|
||||||
func (r *Receipt) decodeTyped(b []byte) error {
|
func (r *Receipt) decodeTyped(b []byte) error {
|
||||||
if len(b) == 0 {
|
if len(b) <= 1 {
|
||||||
return errEmptyTypedReceipt
|
return errShortTypedReceipt
|
||||||
}
|
}
|
||||||
switch b[0] {
|
switch b[0] {
|
||||||
case DynamicFeeTxType, AccessListTxType:
|
case DynamicFeeTxType, AccessListTxType:
|
||||||
|
|
|
||||||
|
|
@ -86,7 +86,7 @@ func TestDecodeEmptyTypedReceipt(t *testing.T) {
|
||||||
input := []byte{0x80}
|
input := []byte{0x80}
|
||||||
var r Receipt
|
var r Receipt
|
||||||
err := rlp.DecodeBytes(input, &r)
|
err := rlp.DecodeBytes(input, &r)
|
||||||
if err != errEmptyTypedReceipt {
|
if err != errShortTypedReceipt {
|
||||||
t.Fatal("wrong error:", err)
|
t.Fatal("wrong error:", err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -43,7 +43,6 @@ var (
|
||||||
errInvalidYParity = errors.New("'yParity' field must be 0 or 1")
|
errInvalidYParity = errors.New("'yParity' field must be 0 or 1")
|
||||||
errVYParityMismatch = errors.New("'v' and 'yParity' fields do not match")
|
errVYParityMismatch = errors.New("'v' and 'yParity' fields do not match")
|
||||||
errVYParityMissing = errors.New("missing 'yParity' or 'v' field in transaction")
|
errVYParityMissing = errors.New("missing 'yParity' or 'v' field in transaction")
|
||||||
errEmptyTypedTx = errors.New("empty typed transaction bytes")
|
|
||||||
errNoSigner = errors.New("missing signing methods")
|
errNoSigner = errors.New("missing signing methods")
|
||||||
ErrFeeCapTooLow = errors.New("fee cap less than base fee")
|
ErrFeeCapTooLow = errors.New("fee cap less than base fee")
|
||||||
|
|
||||||
|
|
@ -149,7 +148,7 @@ func (tx *Transaction) DecodeRLP(s *rlp.Stream) error {
|
||||||
tx.setDecoded(&inner, int(rlp.ListSize(size)))
|
tx.setDecoded(&inner, int(rlp.ListSize(size)))
|
||||||
}
|
}
|
||||||
return err
|
return err
|
||||||
case kind == rlp.String:
|
default:
|
||||||
// It's an EIP-2718 typed TX envelope.
|
// It's an EIP-2718 typed TX envelope.
|
||||||
var b []byte
|
var b []byte
|
||||||
if b, err = s.Bytes(); err != nil {
|
if b, err = s.Bytes(); err != nil {
|
||||||
|
|
@ -160,8 +159,6 @@ func (tx *Transaction) DecodeRLP(s *rlp.Stream) error {
|
||||||
tx.setDecoded(inner, len(b))
|
tx.setDecoded(inner, len(b))
|
||||||
}
|
}
|
||||||
return err
|
return err
|
||||||
default:
|
|
||||||
return rlp.ErrExpectedList
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -189,8 +186,8 @@ func (tx *Transaction) UnmarshalBinary(b []byte) error {
|
||||||
|
|
||||||
// decodeTyped decodes a typed transaction from the canonical format.
|
// decodeTyped decodes a typed transaction from the canonical format.
|
||||||
func (tx *Transaction) decodeTyped(b []byte) (TxData, error) {
|
func (tx *Transaction) decodeTyped(b []byte) (TxData, error) {
|
||||||
if len(b) == 0 {
|
if len(b) <= 1 {
|
||||||
return nil, errEmptyTypedTx
|
return nil, errShortTypedTx
|
||||||
}
|
}
|
||||||
switch b[0] {
|
switch b[0] {
|
||||||
case AccessListTxType:
|
case AccessListTxType:
|
||||||
|
|
|
||||||
|
|
@ -76,7 +76,7 @@ func TestDecodeEmptyTypedTx(t *testing.T) {
|
||||||
input := []byte{0x80}
|
input := []byte{0x80}
|
||||||
var tx Transaction
|
var tx Transaction
|
||||||
err := rlp.DecodeBytes(input, &tx)
|
err := rlp.DecodeBytes(input, &tx)
|
||||||
if err != errEmptyTypedTx {
|
if err != errShortTypedTx {
|
||||||
t.Fatal("wrong error:", err)
|
t.Fatal("wrong error:", err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue