diff --git a/encoding/codecv7_test.go b/encoding/codecv7_test.go index 12bf8d0..9ed0fec 100644 --- a/encoding/codecv7_test.go +++ b/encoding/codecv7_test.go @@ -48,6 +48,44 @@ func TestDecodeAllDeadlock(t *testing.T) { } } +// TestChunkHashUnique tests that the hashes of any two different chunks are different. +func TestChunkHashUnique(t *testing.T) { + // construct a chunk with a single, empty block + chunk1 := newDAChunkV7([]DABlock{&daBlockV7{ + daBlockV0: daBlockV0{ + number: 1, + timestamp: 0, + baseFee: big.NewInt(0), + gasLimit: 0, + numTransactions: 0, + numL1Messages: 0, + }, + lowestL1MessageQueueIndex: 0, + }}, [][]*types.TransactionData{{}}) + + chunkHash1, err := chunk1.Hash() + require.NoError(t, err) + + // construct a 2nd chunk with a single, empty block, + // the only difference is the block number. + chunk2 := newDAChunkV7([]DABlock{&daBlockV7{ + daBlockV0: daBlockV0{ + number: 2, + timestamp: 0, + baseFee: big.NewInt(0), + gasLimit: 0, + numTransactions: 0, + numL1Messages: 0, + }, + lowestL1MessageQueueIndex: 0, + }}, [][]*types.TransactionData{{}}) + + chunkHash2, err := chunk2.Hash() + require.NoError(t, err) + + require.NotEqual(t, chunkHash1, chunkHash2) +} + // TestCodecV7DABlockEncodeDecode tests the encoding and decoding of daBlockV7. func TestCodecV7DABlockEncodeDecode(t *testing.T) { codecV7, err := CodecFromVersion(CodecV7) diff --git a/encoding/codecv7_types.go b/encoding/codecv7_types.go index 77a09f7..af180c1 100644 --- a/encoding/codecv7_types.go +++ b/encoding/codecv7_types.go @@ -447,22 +447,26 @@ func newDAChunkV7(blocks []DABlock, transactions [][]*types.TransactionData) *da } // Hash computes the hash of the DAChunk data. +// Note: Starting from v7, the chunk hash is not used in +// the protocol anymore, it is simply a unique identifier. func (c *daChunkV7) Hash() (common.Hash, error) { var dataBytes []byte // concatenate block contexts for _, block := range c.blocks { + // append block number + var tmp [8]byte + binary.BigEndian.PutUint64(tmp[:], block.Number()) + dataBytes = append(dataBytes, tmp[:]...) + + // append encoded block context encodedBlock := block.Encode() dataBytes = append(dataBytes, encodedBlock...) } - // concatenate l1 tx hashes + // concatenate tx hashes for _, blockTxs := range c.transactions { for _, txData := range blockTxs { - if txData.Type != types.L1MessageTxType { - continue - } - hashBytes := common.FromHex(txData.TxHash) if len(hashBytes) != common.HashLength { return common.Hash{}, fmt.Errorf("unexpected hash: %s", txData.TxHash)