Skip to content

Commit

Permalink
fix: use correct encoding for L1 data fee of EIP2718 transactions (#793)
Browse files Browse the repository at this point in the history
* fix: use correct encoding for L1 data fee of EIP2718 transactions

* add tests

* lint

* Update rollup/fees/rollup_fee.go

Co-authored-by: Ömer Faruk Irmak <[email protected]>

---------

Co-authored-by: Ömer Faruk Irmak <[email protected]>
  • Loading branch information
Thegaram and omerfirmak authored Jun 3, 2024
1 parent 8c7491a commit 9ec83a5
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 17 deletions.
1 change: 0 additions & 1 deletion core/types/transaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@ 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")
errInvalidYParity = errors.New("'yParity' field must be 0 or 1")
errVYParityMismatch = errors.New("'v' and 'yParity' fields do not match")
Expand Down
38 changes: 36 additions & 2 deletions core/types/transaction_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,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)
}
}
Expand All @@ -94,11 +94,33 @@ func TestTransactionSigHash(t *testing.T) {
}

func TestTransactionEncode(t *testing.T) {
should := common.FromHex("f86103018207d094b94f5374fce5edbc8e2a8697c15331677e6ebf0b0a8255441ca098ff921201554726367d2be8c804a7ff89ccf285ebc57dff8ae4c44b9c19ac4aa08887321be575c8095f789dd4c743dfe42c1820f9231f98a962b210e3ac2452a3")

// EncodeToBytes
txb, err := rlp.EncodeToBytes(rightvrsTx)
if err != nil {
t.Fatalf("encode error: %v", err)
}
should := common.FromHex("f86103018207d094b94f5374fce5edbc8e2a8697c15331677e6ebf0b0a8255441ca098ff921201554726367d2be8c804a7ff89ccf285ebc57dff8ae4c44b9c19ac4aa08887321be575c8095f789dd4c743dfe42c1820f9231f98a962b210e3ac2452a3")
if !bytes.Equal(txb, should) {
t.Errorf("encoded RLP mismatch, got %x", txb)
}

// tx.EncodeRLP
raw := new(bytes.Buffer)
err = rightvrsTx.EncodeRLP(raw)
if err != nil {
t.Fatalf("encode error: %v", err)
}
txb = raw.Bytes()
if !bytes.Equal(txb, should) {
t.Errorf("encoded RLP mismatch, got %x", txb)
}

// tx.MarshalBinary
txb, err = rightvrsTx.MarshalBinary()
if err != nil {
t.Fatalf("encode error: %v", err)
}
if !bytes.Equal(txb, should) {
t.Errorf("encoded RLP mismatch, got %x", txb)
}
Expand Down Expand Up @@ -194,6 +216,7 @@ func TestEIP2930Signer(t *testing.T) {
func TestEIP2718TransactionEncode(t *testing.T) {
// RLP representation
{
// rlp.EncodeToBytes
have, err := rlp.EncodeToBytes(signedEip2718Tx)
if err != nil {
t.Fatalf("encode error: %v", err)
Expand All @@ -202,6 +225,17 @@ func TestEIP2718TransactionEncode(t *testing.T) {
if !bytes.Equal(have, want) {
t.Errorf("encoded RLP mismatch, got %x", have)
}

// tx.EncodeRLP
raw := new(bytes.Buffer)
err = signedEip2718Tx.EncodeRLP(raw)
if err != nil {
t.Fatalf("encode error: %v", err)
}
have = raw.Bytes()
if !bytes.Equal(have, want) {
t.Errorf("encoded RLP mismatch, got %x", have)
}
}
// Binary representation
{
Expand Down
2 changes: 1 addition & 1 deletion params/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import (
const (
VersionMajor = 5 // Major version component of the current release
VersionMinor = 3 // Minor version component of the current release
VersionPatch = 33 // Patch version component of the current release
VersionPatch = 34 // Patch version component of the current release
VersionMeta = "mainnet" // Version metadata to append to the version string
)

Expand Down
22 changes: 9 additions & 13 deletions rollup/fees/rollup_fee.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package fees

import (
"bytes"
"math"
"math/big"

"github.com/scroll-tech/go-ethereum/common"
Expand Down Expand Up @@ -40,7 +41,6 @@ type Message interface {
// required to compute the L1 fee
type StateDB interface {
GetState(common.Address, common.Hash) common.Hash
GetBalance(addr common.Address) *big.Int
}

type gpoState struct {
Expand All @@ -64,7 +64,7 @@ func EstimateL1DataFeeForMessage(msg Message, baseFee *big.Int, config *params.C
return nil, err
}

raw, err := rlpEncode(tx)
raw, err := tx.MarshalBinary()
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -133,16 +133,6 @@ func asUnsignedDynamicTx(msg Message, chainID *big.Int) *types.Transaction {
})
}

// rlpEncode RLP encodes the transaction into bytes
func rlpEncode(tx *types.Transaction) ([]byte, error) {
raw := new(bytes.Buffer)
if err := tx.EncodeRLP(raw); err != nil {
return nil, err
}

return raw.Bytes(), nil
}

func readGPOStorageSlots(addr common.Address, state StateDB) gpoState {
var gpoState gpoState
gpoState.l1BaseFee = state.GetState(addr, rcfg.L1BaseFeeSlot).Big()
Expand Down Expand Up @@ -216,7 +206,7 @@ func CalculateL1DataFee(tx *types.Transaction, state StateDB, config *params.Cha
return big.NewInt(0), nil
}

raw, err := rlpEncode(tx)
raw, err := tx.MarshalBinary()
if err != nil {
return nil, err
}
Expand All @@ -231,6 +221,12 @@ func CalculateL1DataFee(tx *types.Transaction, state StateDB, config *params.Cha
l1DataFee = calculateEncodedL1DataFeeCurie(raw, gpoState.l1BaseFee, gpoState.l1BlobBaseFee, gpoState.commitScalar, gpoState.blobScalar)
}

// ensure l1DataFee fits into uint64 for circuit compatibility
// (note: in practice this value should never be this big)
if !l1DataFee.IsUint64() {
l1DataFee.SetUint64(math.MaxUint64)
}

return l1DataFee, nil
}

Expand Down

0 comments on commit 9ec83a5

Please sign in to comment.