Skip to content

Commit 37d9898

Browse files
authored
fix(CodecV7): fix L1 message hash bug (#48)
* add sender to from field in tx data for L1 messages * add AuthorizationList
1 parent 012aaee commit 37d9898

File tree

2 files changed

+61
-27
lines changed

2 files changed

+61
-27
lines changed

encoding/da.go

Lines changed: 38 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -381,31 +381,35 @@ func TxsToTxsData(txs types.Transactions) []*types.TransactionData {
381381
v, r, s := tx.RawSignatureValues()
382382

383383
nonce := tx.Nonce()
384+
var from common.Address
384385

385386
// We need QueueIndex in `NewBatchHeader`. However, `TransactionData`
386387
// does not have this field. Since `L1MessageTx` do not have a nonce,
387388
// we reuse this field for storing the queue index.
388389
if msg := tx.AsL1MessageTx(); msg != nil {
389390
nonce = msg.QueueIndex
391+
from = msg.Sender
390392
}
391393

392394
txsData[i] = &types.TransactionData{
393-
Type: tx.Type(),
394-
TxHash: tx.Hash().String(),
395-
Nonce: nonce,
396-
ChainId: (*hexutil.Big)(tx.ChainId()),
397-
Gas: tx.Gas(),
398-
GasPrice: (*hexutil.Big)(tx.GasPrice()),
399-
GasTipCap: (*hexutil.Big)(tx.GasTipCap()),
400-
GasFeeCap: (*hexutil.Big)(tx.GasFeeCap()),
401-
To: tx.To(),
402-
Value: (*hexutil.Big)(tx.Value()),
403-
Data: hexutil.Encode(tx.Data()),
404-
IsCreate: tx.To() == nil,
405-
AccessList: tx.AccessList(),
406-
V: (*hexutil.Big)(v),
407-
R: (*hexutil.Big)(r),
408-
S: (*hexutil.Big)(s),
395+
Type: tx.Type(),
396+
TxHash: tx.Hash().String(),
397+
Nonce: nonce,
398+
ChainId: (*hexutil.Big)(tx.ChainId()),
399+
Gas: tx.Gas(),
400+
GasPrice: (*hexutil.Big)(tx.GasPrice()),
401+
GasTipCap: (*hexutil.Big)(tx.GasTipCap()),
402+
GasFeeCap: (*hexutil.Big)(tx.GasFeeCap()),
403+
From: from,
404+
To: tx.To(),
405+
Value: (*hexutil.Big)(tx.Value()),
406+
Data: hexutil.Encode(tx.Data()),
407+
IsCreate: tx.To() == nil,
408+
AccessList: tx.AccessList(),
409+
AuthorizationList: tx.SetCodeAuthorizations(),
410+
V: (*hexutil.Big)(v),
411+
R: (*hexutil.Big)(r),
412+
S: (*hexutil.Big)(s),
409413
}
410414
}
411415
return txsData
@@ -796,18 +800,9 @@ func MessageQueueV2ApplyL1MessagesFromBlocks(initialQueueHash common.Hash, block
796800
continue
797801
}
798802

799-
data, err := hexutil.Decode(txData.Data)
803+
l1Message, err := l1MessageFromTxData(txData)
800804
if err != nil {
801-
return common.Hash{}, fmt.Errorf("failed to decode txData.Data: data=%v, err=%w", txData.Data, err)
802-
}
803-
804-
l1Message := &types.L1MessageTx{
805-
QueueIndex: txData.Nonce,
806-
Gas: txData.Gas,
807-
To: txData.To,
808-
Value: txData.Value.ToInt(),
809-
Data: data,
810-
Sender: txData.From,
805+
return common.Hash{}, fmt.Errorf("failed to decode L1 message from tx data: %w", err)
811806
}
812807

813808
rollingHash = messageQueueV2ApplyL1Message(rollingHash, l1Message)
@@ -817,6 +812,22 @@ func MessageQueueV2ApplyL1MessagesFromBlocks(initialQueueHash common.Hash, block
817812
return rollingHash, nil
818813
}
819814

815+
func l1MessageFromTxData(txData *types.TransactionData) (*types.L1MessageTx, error) {
816+
data, err := hexutil.Decode(txData.Data)
817+
if err != nil {
818+
return nil, fmt.Errorf("failed to decode txData.Data: data=%v, err=%w", txData.Data, err)
819+
}
820+
821+
return &types.L1MessageTx{
822+
QueueIndex: txData.Nonce,
823+
Gas: txData.Gas,
824+
To: txData.To,
825+
Value: txData.Value.ToInt(),
826+
Data: data,
827+
Sender: txData.From,
828+
}, nil
829+
}
830+
820831
func MessageQueueV2ApplyL1Messages(initialQueueHash common.Hash, messages []*types.L1MessageTx) common.Hash {
821832
rollingHash := initialQueueHash
822833
for _, message := range messages {

encoding/da_test.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,15 @@ package encoding
33
import (
44
"encoding/hex"
55
"encoding/json"
6+
"math/big"
67
"os"
78
"testing"
89

910
"github.com/scroll-tech/go-ethereum/common"
1011
"github.com/scroll-tech/go-ethereum/core/types"
1112
"github.com/scroll-tech/go-ethereum/log"
1213
"github.com/stretchr/testify/assert"
14+
"github.com/stretchr/testify/require"
1315

1416
"github.com/scroll-tech/da-codec/encoding/zstd"
1517
)
@@ -222,3 +224,24 @@ func TestMessageQueueV2EncodeRollingHash(t *testing.T) {
222224
})
223225
}
224226
}
227+
228+
func TestTxsToTxsData_L1Message(t *testing.T) {
229+
msg := &types.L1MessageTx{
230+
QueueIndex: 100,
231+
Gas: 99,
232+
To: &common.Address{0x01, 0x02, 0x03},
233+
Value: new(big.Int).SetInt64(1337),
234+
Data: []byte{0x01, 0x02, 0x03},
235+
Sender: common.Address{0x04, 0x05, 0x06},
236+
}
237+
238+
tx := types.NewTx(msg)
239+
240+
txData := TxsToTxsData([]*types.Transaction{tx})
241+
require.Len(t, txData, 1)
242+
243+
decoded, err := l1MessageFromTxData(txData[0])
244+
require.NoError(t, err)
245+
246+
require.Equal(t, tx.Hash(), types.NewTx(decoded).Hash())
247+
}

0 commit comments

Comments
 (0)