Skip to content

Commit

Permalink
sync tx encoding/decoding
Browse files Browse the repository at this point in the history
  • Loading branch information
colinlyguo committed Mar 13, 2024
1 parent 4b91455 commit 9b92a00
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 15 deletions.
10 changes: 10 additions & 0 deletions core/types/access_list_tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,11 @@
package types

import (
"bytes"
"math/big"

"github.com/scroll-tech/go-ethereum/common"
"github.com/scroll-tech/go-ethereum/rlp"
)

//go:generate gencodec -type AccessTuple -out gen_access_tuple.go
Expand Down Expand Up @@ -113,3 +115,11 @@ func (tx *AccessListTx) rawSignatureValues() (v, r, s *big.Int) {
func (tx *AccessListTx) setSignatureValues(chainID, v, r, s *big.Int) {
tx.ChainID, tx.V, tx.R, tx.S = chainID, v, r, s
}

func (tx *AccessListTx) encode(b *bytes.Buffer) error {
return rlp.Encode(b, tx)
}

func (tx *AccessListTx) decode(input []byte) error {
return rlp.DecodeBytes(input, tx)
}
10 changes: 10 additions & 0 deletions core/types/dynamic_fee_tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,11 @@
package types

import (
"bytes"
"math/big"

"github.com/scroll-tech/go-ethereum/common"
"github.com/scroll-tech/go-ethereum/rlp"
)

type DynamicFeeTx struct {
Expand Down Expand Up @@ -101,3 +103,11 @@ func (tx *DynamicFeeTx) rawSignatureValues() (v, r, s *big.Int) {
func (tx *DynamicFeeTx) setSignatureValues(chainID, v, r, s *big.Int) {
tx.ChainID, tx.V, tx.R, tx.S = chainID, v, r, s
}

func (tx *DynamicFeeTx) encode(b *bytes.Buffer) error {
return rlp.Encode(b, tx)
}

func (tx *DynamicFeeTx) decode(input []byte) error {
return rlp.DecodeBytes(input, tx)
}
10 changes: 10 additions & 0 deletions core/types/l1_message_tx.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package types

import (
"bytes"
"math/big"

"github.com/scroll-tech/go-ethereum/common"
"github.com/scroll-tech/go-ethereum/rlp"
)

// payload, RLP encoded
Expand Down Expand Up @@ -52,3 +54,11 @@ func (tx *L1MessageTx) rawSignatureValues() (v, r, s *big.Int) {
func (tx *L1MessageTx) setSignatureValues(chainID, v, r, s *big.Int) {
// this is a noop for l1 message transactions
}

func (tx *L1MessageTx) encode(b *bytes.Buffer) error {
return rlp.Encode(b, tx)
}

func (tx *L1MessageTx) decode(input []byte) error {
return rlp.DecodeBytes(input, tx)
}
9 changes: 9 additions & 0 deletions core/types/legacy_tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package types

import (
"bytes"
"math/big"

"github.com/scroll-tech/go-ethereum/common"
Expand Down Expand Up @@ -110,3 +111,11 @@ func (tx *LegacyTx) rawSignatureValues() (v, r, s *big.Int) {
func (tx *LegacyTx) setSignatureValues(chainID, v, r, s *big.Int) {
tx.V, tx.R, tx.S = v, r, s
}

func (tx *LegacyTx) encode(*bytes.Buffer) error {
panic("encode called on LegacyTx")
}

func (tx *LegacyTx) decode([]byte) error {
panic("decode called on LegacyTx)")
}
31 changes: 16 additions & 15 deletions core/types/transaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ var (
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")
errInvalidYParity = errors.New("'yParity' field must be 0 or 1")
errVYParityMismatch = errors.New("'v' and 'yParity' fields do not match")
errVYParityMissing = errors.New("missing 'yParity' or 'v' field in transaction")
Expand Down Expand Up @@ -94,6 +95,9 @@ type TxData interface {

rawSignatureValues() (v, r, s *big.Int)
setSignatureValues(chainID, v, r, s *big.Int)

encode(*bytes.Buffer) error
decode([]byte) error
}

// EncodeRLP implements rlp.Encoder
Expand All @@ -114,7 +118,7 @@ func (tx *Transaction) EncodeRLP(w io.Writer) error {
// encodeTyped writes the canonical encoding of a typed transaction to w.
func (tx *Transaction) encodeTyped(w *bytes.Buffer) error {
w.WriteByte(tx.Type())
return rlp.Encode(w, tx.inner)
return tx.inner.encode(w)
}

// MarshalBinary returns the canonical encoding of the transaction.
Expand Down Expand Up @@ -143,6 +147,8 @@ func (tx *Transaction) DecodeRLP(s *rlp.Stream) error {
tx.setDecoded(&inner, int(rlp.ListSize(size)))
}
return err
case kind == rlp.Byte:
return errShortTypedTx
case kind == rlp.String:
// It's an EIP-2718 typed TX envelope.
var b []byte
Expand Down Expand Up @@ -183,29 +189,24 @@ 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
}
var inner TxData
switch b[0] {
case AccessListTxType:
var inner AccessListTx
err := rlp.DecodeBytes(b[1:], &inner)
return &inner, err
inner = new(AccessListTx)
case DynamicFeeTxType:
var inner DynamicFeeTx
err := rlp.DecodeBytes(b[1:], &inner)
return &inner, err
inner = new(DynamicFeeTx)
case BlobTxType:
var inner BlobTx
err := rlp.DecodeBytes(b[1:], &inner)
return &inner, err
inner = new(BlobTx)
case L1MessageTxType:
var inner L1MessageTx
err := rlp.DecodeBytes(b[1:], &inner)
return &inner, err
inner = new(L1MessageTx)
default:
return nil, ErrTxTypeNotSupported
}
err := inner.decode(b[1:])
return inner, err
}

// setDecoded sets the inner transaction and size after decoding.
Expand Down

0 comments on commit 9b92a00

Please sign in to comment.