From dcbf124653ca68a43f0d0c259a757ffa855ce247 Mon Sep 17 00:00:00 2001 From: jonastheis <4181434+jonastheis@users.noreply.github.com> Date: Thu, 13 Mar 2025 10:57:17 +0800 Subject: [PATCH 1/2] add sender to from field in tx data for L1 messages --- encoding/da.go | 32 +++++++++++++++++++++----------- encoding/da_test.go | 23 +++++++++++++++++++++++ 2 files changed, 44 insertions(+), 11 deletions(-) diff --git a/encoding/da.go b/encoding/da.go index e20dc0a..4ddc18b 100644 --- a/encoding/da.go +++ b/encoding/da.go @@ -381,12 +381,14 @@ func TxsToTxsData(txs types.Transactions) []*types.TransactionData { v, r, s := tx.RawSignatureValues() nonce := tx.Nonce() + var from common.Address // We need QueueIndex in `NewBatchHeader`. However, `TransactionData` // does not have this field. Since `L1MessageTx` do not have a nonce, // we reuse this field for storing the queue index. if msg := tx.AsL1MessageTx(); msg != nil { nonce = msg.QueueIndex + from = msg.Sender } txsData[i] = &types.TransactionData{ @@ -398,6 +400,7 @@ func TxsToTxsData(txs types.Transactions) []*types.TransactionData { GasPrice: (*hexutil.Big)(tx.GasPrice()), GasTipCap: (*hexutil.Big)(tx.GasTipCap()), GasFeeCap: (*hexutil.Big)(tx.GasFeeCap()), + From: from, To: tx.To(), Value: (*hexutil.Big)(tx.Value()), Data: hexutil.Encode(tx.Data()), @@ -796,18 +799,9 @@ func MessageQueueV2ApplyL1MessagesFromBlocks(initialQueueHash common.Hash, block continue } - data, err := hexutil.Decode(txData.Data) + l1Message, err := l1MessageFromTxData(txData) if err != nil { - return common.Hash{}, fmt.Errorf("failed to decode txData.Data: data=%v, err=%w", txData.Data, err) - } - - l1Message := &types.L1MessageTx{ - QueueIndex: txData.Nonce, - Gas: txData.Gas, - To: txData.To, - Value: txData.Value.ToInt(), - Data: data, - Sender: txData.From, + return common.Hash{}, fmt.Errorf("failed to decode L1 message from tx data: %w", err) } rollingHash = messageQueueV2ApplyL1Message(rollingHash, l1Message) @@ -817,6 +811,22 @@ func MessageQueueV2ApplyL1MessagesFromBlocks(initialQueueHash common.Hash, block return rollingHash, nil } +func l1MessageFromTxData(txData *types.TransactionData) (*types.L1MessageTx, error) { + data, err := hexutil.Decode(txData.Data) + if err != nil { + return nil, fmt.Errorf("failed to decode txData.Data: data=%v, err=%w", txData.Data, err) + } + + return &types.L1MessageTx{ + QueueIndex: txData.Nonce, + Gas: txData.Gas, + To: txData.To, + Value: txData.Value.ToInt(), + Data: data, + Sender: txData.From, + }, nil +} + func MessageQueueV2ApplyL1Messages(initialQueueHash common.Hash, messages []*types.L1MessageTx) common.Hash { rollingHash := initialQueueHash for _, message := range messages { diff --git a/encoding/da_test.go b/encoding/da_test.go index a36ea2b..a0fa6b2 100644 --- a/encoding/da_test.go +++ b/encoding/da_test.go @@ -3,6 +3,7 @@ package encoding import ( "encoding/hex" "encoding/json" + "math/big" "os" "testing" @@ -10,6 +11,7 @@ import ( "github.com/scroll-tech/go-ethereum/core/types" "github.com/scroll-tech/go-ethereum/log" "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" "github.com/scroll-tech/da-codec/encoding/zstd" ) @@ -222,3 +224,24 @@ func TestMessageQueueV2EncodeRollingHash(t *testing.T) { }) } } + +func TestTxsToTxsData_L1Message(t *testing.T) { + msg := &types.L1MessageTx{ + QueueIndex: 100, + Gas: 99, + To: &common.Address{0x01, 0x02, 0x03}, + Value: new(big.Int).SetInt64(1337), + Data: []byte{0x01, 0x02, 0x03}, + Sender: common.Address{0x04, 0x05, 0x06}, + } + + tx := types.NewTx(msg) + + txData := TxsToTxsData([]*types.Transaction{tx}) + require.Len(t, txData, 1) + + decoded, err := l1MessageFromTxData(txData[0]) + require.NoError(t, err) + + require.Equal(t, tx.Hash(), types.NewTx(decoded).Hash()) +} From a8341d04bc4e2a63d9142008c621e80c55386066 Mon Sep 17 00:00:00 2001 From: jonastheis <4181434+jonastheis@users.noreply.github.com> Date: Thu, 13 Mar 2025 11:00:21 +0800 Subject: [PATCH 2/2] add AuthorizationList --- encoding/da.go | 35 ++++++++++++++++++----------------- 1 file changed, 18 insertions(+), 17 deletions(-) diff --git a/encoding/da.go b/encoding/da.go index 4ddc18b..fa4ed88 100644 --- a/encoding/da.go +++ b/encoding/da.go @@ -392,23 +392,24 @@ func TxsToTxsData(txs types.Transactions) []*types.TransactionData { } txsData[i] = &types.TransactionData{ - Type: tx.Type(), - TxHash: tx.Hash().String(), - Nonce: nonce, - ChainId: (*hexutil.Big)(tx.ChainId()), - Gas: tx.Gas(), - GasPrice: (*hexutil.Big)(tx.GasPrice()), - GasTipCap: (*hexutil.Big)(tx.GasTipCap()), - GasFeeCap: (*hexutil.Big)(tx.GasFeeCap()), - From: from, - To: tx.To(), - Value: (*hexutil.Big)(tx.Value()), - Data: hexutil.Encode(tx.Data()), - IsCreate: tx.To() == nil, - AccessList: tx.AccessList(), - V: (*hexutil.Big)(v), - R: (*hexutil.Big)(r), - S: (*hexutil.Big)(s), + Type: tx.Type(), + TxHash: tx.Hash().String(), + Nonce: nonce, + ChainId: (*hexutil.Big)(tx.ChainId()), + Gas: tx.Gas(), + GasPrice: (*hexutil.Big)(tx.GasPrice()), + GasTipCap: (*hexutil.Big)(tx.GasTipCap()), + GasFeeCap: (*hexutil.Big)(tx.GasFeeCap()), + From: from, + To: tx.To(), + Value: (*hexutil.Big)(tx.Value()), + Data: hexutil.Encode(tx.Data()), + IsCreate: tx.To() == nil, + AccessList: tx.AccessList(), + AuthorizationList: tx.SetCodeAuthorizations(), + V: (*hexutil.Big)(v), + R: (*hexutil.Big)(r), + S: (*hexutil.Big)(s), } } return txsData