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
38 changes: 38 additions & 0 deletions encoding/codecv7_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
14 changes: 9 additions & 5 deletions encoding/codecv7_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down