Update decode method to match encode method

This commit is contained in:
Austin Roberts 2024-01-17 01:13:40 -06:00
parent b2ccbb14a3
commit 7e5f3b9e62
2 changed files with 11 additions and 11 deletions

View File

@ -36,6 +36,7 @@ var (
ErrInvalidTxType = errors.New("transaction type not valid in this context") ErrInvalidTxType = errors.New("transaction type not valid in this context")
ErrTxTypeNotSupported = errors.New("transaction type not supported") ErrTxTypeNotSupported = errors.New("transaction type not supported")
ErrGasFeeCapTooLow = errors.New("fee cap less than base fee") ErrGasFeeCapTooLow = errors.New("fee cap less than base fee")
errShortTypedTx = errors.New("typed transaction too short")
errEmptyTypedTx = errors.New("empty typed transaction bytes") errEmptyTypedTx = errors.New("empty typed transaction bytes")
) )
@ -103,6 +104,7 @@ type TxData interface {
effectiveGasPrice(dst *big.Int, baseFee *big.Int) *big.Int effectiveGasPrice(dst *big.Int, baseFee *big.Int) *big.Int
encode(b *bytes.Buffer) error encode(b *bytes.Buffer) error
decode([]byte) error
} }
// EncodeRLP implements rlp.Encoder // EncodeRLP implements rlp.Encoder
@ -195,26 +197,24 @@ 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) <= 1 { if len(b) <= 1 {
return nil, errEmptyTypedTx return nil, errShortTypedTx
} }
var inner TxData
switch b[0] { switch b[0] {
case AccessListTxType: case AccessListTxType:
var inner AccessListTx inner = new(AccessListTx)
err := rlp.DecodeBytes(b[1:], &inner)
return &inner, err
case DynamicFeeTxType: case DynamicFeeTxType:
var inner DynamicFeeTx inner = new(DynamicFeeTx)
err := rlp.DecodeBytes(b[1:], &inner)
return &inner, err
case BlobTxType: case BlobTxType:
var inner BlobTx inner = new(BlobTx)
err := rlp.DecodeBytes(b[1:], &inner)
return &inner, err
default: default:
return nil, ErrTxTypeNotSupported return nil, ErrTxTypeNotSupported
} }
err := inner.decode(b[1:])
return inner, err
} }
// setDecoded sets the inner transaction and size after decoding. // setDecoded sets the inner transaction and size after decoding.
func (tx *Transaction) setDecoded(inner TxData, size uint64) { func (tx *Transaction) setDecoded(inner TxData, size uint64) {
tx.inner = inner tx.inner = inner

View File

@ -82,7 +82,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)
} }
} }