Skip to content

Commit cfb316b

Browse files
committed
add daChunkV7 type to calculate chunk hash
1 parent 14d07e7 commit cfb316b

File tree

2 files changed

+48
-1
lines changed

2 files changed

+48
-1
lines changed

encoding/codecv7.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ func (d *DACodecV7) NewDAChunk(chunk *Chunk, totalL1MessagePoppedBefore uint64)
107107
txs = append(txs, block.Transactions)
108108
}
109109

110-
daChunk := newDAChunkV1(
110+
daChunk := newDAChunkV7(
111111
blocks, // blocks
112112
txs, // transactions
113113
)

encoding/codecv7_types.go

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -399,6 +399,53 @@ func (b *daBlockV7) Decode(data []byte) error {
399399
return nil
400400
}
401401

402+
// daChunkV7 groups consecutive DABlocks with their transactions.
403+
// Note: In DACodecV7 there is no notion of chunks. Blobs contain the entire batch data without any information of Chunks within.
404+
// However, for compatibility reasons DAChunks are still used in the codebase.
405+
// This way we can still uniquely identify a set of blocks and their L1 messages via their hash.
406+
type daChunkV7 struct {
407+
daChunkV1
408+
}
409+
410+
// newDAChunkV1 is a constructor for daChunkV1, initializing with blocks and transactions.
411+
func newDAChunkV7(blocks []DABlock, transactions [][]*types.TransactionData) *daChunkV7 {
412+
return &daChunkV7{
413+
daChunkV1{
414+
blocks: blocks,
415+
transactions: transactions,
416+
},
417+
}
418+
}
419+
420+
// Hash computes the hash of the DAChunk data.
421+
func (c *daChunkV7) Hash() (common.Hash, error) {
422+
var dataBytes []byte
423+
424+
// concatenate block contexts
425+
for _, block := range c.blocks {
426+
encodedBlock := block.Encode()
427+
dataBytes = append(dataBytes, encodedBlock...)
428+
}
429+
430+
// concatenate l1 tx hashes
431+
for _, blockTxs := range c.transactions {
432+
for _, txData := range blockTxs {
433+
if txData.Type != types.L1MessageTxType {
434+
continue
435+
}
436+
437+
hashBytes := common.FromHex(txData.TxHash)
438+
if len(hashBytes) != common.HashLength {
439+
return common.Hash{}, fmt.Errorf("unexpected hash: %s", txData.TxHash)
440+
}
441+
dataBytes = append(dataBytes, hashBytes...)
442+
}
443+
}
444+
445+
hash := crypto.Keccak256Hash(dataBytes)
446+
return hash, nil
447+
}
448+
402449
// decompressV7Bytes decompresses the given blob bytes into the original payload bytes.
403450
func decompressV7Bytes(compressedBytes []byte) ([]byte, error) {
404451
var res []byte

0 commit comments

Comments
 (0)