Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
65 changes: 38 additions & 27 deletions encoding/da.go
Original file line number Diff line number Diff line change
Expand Up @@ -381,31 +381,35 @@ 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{
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()),
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
Expand Down Expand Up @@ -796,18 +800,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)
Expand All @@ -817,6 +812,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 {
Expand Down
23 changes: 23 additions & 0 deletions encoding/da_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,15 @@ package encoding
import (
"encoding/hex"
"encoding/json"
"math/big"
"os"
"testing"

"github.com/scroll-tech/go-ethereum/common"
"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"
)
Expand Down Expand Up @@ -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())
}