Skip to content

Commit 9592886

Browse files
committed
add CompressScrollBatchBytes interface
1 parent 9e6ea1d commit 9592886

File tree

6 files changed

+38
-12
lines changed

6 files changed

+38
-12
lines changed

encoding/codecv0.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -430,3 +430,7 @@ func (d *DACodecV0) computeBatchDataHash(chunks []*Chunk, totalL1MessagePoppedBe
430430
dataHash := crypto.Keccak256Hash(dataBytes)
431431
return dataHash, nil
432432
}
433+
434+
func (d *DACodecV0) CompressScrollBatchBytes(batchBytes []byte) ([]byte, error) {
435+
return batchBytes, nil
436+
}

encoding/codecv2.go

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@ func (d *DACodecV2) constructBlobPayload(chunks []*Chunk, maxNumChunksPerBatch i
154154
copy(challengePreimage[0:], hash[:])
155155

156156
// blobBytes represents the compressed blob payload (batchBytes)
157-
blobBytes, err := zstd.CompressScrollBatchBytesLegacy(batchBytes)
157+
blobBytes, err := d.CompressScrollBatchBytes(batchBytes)
158158
if err != nil {
159159
return nil, common.Hash{}, nil, nil, common.Hash{}, err
160160
}
@@ -236,7 +236,7 @@ func (d *DACodecV2) EstimateChunkL1CommitBatchSizeAndBlobSize(c *Chunk) (uint64,
236236
if err != nil {
237237
return 0, 0, fmt.Errorf("failed to construct batch payload in blob: %w", err)
238238
}
239-
blobBytes, err := zstd.CompressScrollBatchBytesLegacy(batchBytes)
239+
blobBytes, err := d.CompressScrollBatchBytes(batchBytes)
240240
if err != nil {
241241
return 0, 0, fmt.Errorf("failed to compress scroll batch bytes: %w", err)
242242
}
@@ -249,7 +249,7 @@ func (d *DACodecV2) EstimateBatchL1CommitBatchSizeAndBlobSize(b *Batch) (uint64,
249249
if err != nil {
250250
return 0, 0, err
251251
}
252-
blobBytes, err := zstd.CompressScrollBatchBytesLegacy(batchBytes)
252+
blobBytes, err := d.CompressScrollBatchBytes(batchBytes)
253253
if err != nil {
254254
return 0, 0, err
255255
}
@@ -263,7 +263,7 @@ func (d *DACodecV2) checkCompressedDataCompatibility(chunks []*Chunk) (bool, err
263263
if err != nil {
264264
return false, fmt.Errorf("failed to construct batch payload in blob: %w", err)
265265
}
266-
blobBytes, err := zstd.CompressScrollBatchBytesLegacy(batchBytes)
266+
blobBytes, err := d.CompressScrollBatchBytes(batchBytes)
267267
if err != nil {
268268
return false, fmt.Errorf("failed to compress scroll batch bytes: %w", err)
269269
}
@@ -289,3 +289,8 @@ func (d *DACodecV2) CheckChunkCompressedDataCompatibility(c *Chunk) (bool, error
289289
func (d *DACodecV2) CheckBatchCompressedDataCompatibility(b *Batch) (bool, error) {
290290
return d.checkCompressedDataCompatibility(b.Chunks)
291291
}
292+
293+
// CompressScrollBatchBytes compresses the batch bytes using zstd compression.
294+
func (d *DACodecV2) CompressScrollBatchBytes(batchBytes []byte) ([]byte, error) {
295+
return zstd.CompressScrollBatchBytesLegacy(batchBytes)
296+
}

encoding/codecv4.go

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,6 @@ import (
1414
"github.com/scroll-tech/go-ethereum/crypto"
1515
"github.com/scroll-tech/go-ethereum/crypto/kzg4844"
1616
"github.com/scroll-tech/go-ethereum/log"
17-
18-
"github.com/scroll-tech/da-codec/encoding/zstd"
1917
)
2018

2119
type DACodecV4 struct {
@@ -205,7 +203,7 @@ func (d *DACodecV4) constructBlobPayload(chunks []*Chunk, maxNumChunksPerBatch i
205203
if enableCompression {
206204
// blobBytes represents the compressed blob payload (batchBytes)
207205
var err error
208-
blobBytes, err = zstd.CompressScrollBatchBytesLegacy(batchBytes)
206+
blobBytes, err = d.CompressScrollBatchBytes(batchBytes)
209207
if err != nil {
210208
return nil, common.Hash{}, nil, nil, common.Hash{}, err
211209
}
@@ -267,7 +265,7 @@ func (d *DACodecV4) estimateL1CommitBatchSizeAndBlobSize(chunks []*Chunk) (uint6
267265
return 0, 0, fmt.Errorf("failed to compress scroll batch bytes: %w", err)
268266
}
269267
if enableCompression {
270-
blobBytes, err := zstd.CompressScrollBatchBytesLegacy(batchBytes)
268+
blobBytes, err := d.CompressScrollBatchBytes(batchBytes)
271269
if err != nil {
272270
return 0, 0, err
273271
}
@@ -295,7 +293,7 @@ func (d *DACodecV4) checkCompressedDataCompatibility(chunks []*Chunk) (bool, err
295293
if err != nil {
296294
return false, fmt.Errorf("failed to construct batch payload in blob: %w", err)
297295
}
298-
blobBytes, err := zstd.CompressScrollBatchBytesLegacy(batchBytes)
296+
blobBytes, err := d.CompressScrollBatchBytes(batchBytes)
299297
if err != nil {
300298
return false, fmt.Errorf("failed to compress scroll batch bytes: %w", err)
301299
}

encoding/codecv7.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -234,7 +234,7 @@ func (d *DACodecV7) DecodeTxsFromBlob(blob *kzg4844.Blob, chunks []*DAChunkRawTx
234234
// If checkLength is true, this function returns if compression is needed based on the compressed data's length, which is used when doing batch bytes encoding.
235235
// If checkLength is false, this function returns the result of the compatibility check, which is used when determining the chunk and batch contents.
236236
func (d *DACodecV7) checkCompressedDataCompatibility(payloadBytes []byte, checkLength bool) ([]byte, bool, error) {
237-
compressedPayloadBytes, err := zstd.CompressScrollBatchBytesLegacy(payloadBytes)
237+
compressedPayloadBytes, err := d.CompressScrollBatchBytes(payloadBytes)
238238
if err != nil {
239239
return nil, false, fmt.Errorf("failed to compress blob payload: %w", err)
240240
}
@@ -388,3 +388,8 @@ func (d *DACodecV7) JSONFromBytes(data []byte) ([]byte, error) {
388388

389389
return jsonBytes, nil
390390
}
391+
392+
// CompressScrollBatchBytes compresses the batch bytes using zstd compression.
393+
func (d *DACodecV7) CompressScrollBatchBytes(batchBytes []byte) ([]byte, error) {
394+
return zstd.CompressScrollBatchBytesLegacy(batchBytes)
395+
}

encoding/codecv8.go

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import (
1414
"github.com/scroll-tech/da-codec/encoding/zstd"
1515
)
1616

17-
// DACodecV8 uses zstd.CompressScrollBatchBytesStandard for compression instead of zstd.CompressScrollBatchBytesLegacy.
17+
// DACodecV8 uses CompressScrollBatchBytesStandard for compression instead of CompressScrollBatchBytesLegacy.
1818
//
1919
// Note: Due to Go's method receiver behavior, we need to override all methods that call checkCompressedDataCompatibility.
2020
// When a method in DACodecV7 calls d.checkCompressedDataCompatibility(), it will always use DACodecV7's version,
@@ -45,7 +45,7 @@ func NewDACodecV8() *DACodecV8 {
4545
// If checkLength is true, this function returns if compression is needed based on the compressed data's length, which is used when doing batch bytes encoding.
4646
// If checkLength is false, this function returns the result of the compatibility check, which is used when determining the chunk and batch contents.
4747
func (d *DACodecV8) checkCompressedDataCompatibility(payloadBytes []byte, checkLength bool) ([]byte, bool, error) {
48-
compressedPayloadBytes, err := zstd.CompressScrollBatchBytesStandard(payloadBytes)
48+
compressedPayloadBytes, err := d.CompressScrollBatchBytes(payloadBytes)
4949
if err != nil {
5050
return nil, false, fmt.Errorf("failed to compress blob payload: %w", err)
5151
}
@@ -206,3 +206,8 @@ func (d *DACodecV8) EstimateChunkL1CommitBatchSizeAndBlobSize(chunk *Chunk) (uin
206206
func (d *DACodecV8) EstimateBatchL1CommitBatchSizeAndBlobSize(batch *Batch) (uint64, uint64, error) {
207207
return d.estimateL1CommitBatchSizeAndBlobSize(batch)
208208
}
209+
210+
// CompressScrollBatchBytes compresses the batch bytes using zstd compression.
211+
func (d *DACodecV8) CompressScrollBatchBytes(batchBytes []byte) ([]byte, error) {
212+
return zstd.CompressScrollBatchBytesStandard(batchBytes)
213+
}

encoding/interfaces.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,9 @@ type Codec interface {
7676
EstimateBatchL1CommitCalldataSize(*Batch) (uint64, error)
7777

7878
JSONFromBytes([]byte) ([]byte, error) // convert batch header bytes to JSON, this is only used to provide witness data for the prover.
79+
80+
// CompressScrollBatchBytes compresses batch bytes using the appropriate compression method for this codec version
81+
CompressScrollBatchBytes(batchBytes []byte) ([]byte, error)
7982
}
8083

8184
// CodecVersion represents the version of the codec.
@@ -140,3 +143,9 @@ func CodecFromConfig(chainCfg *params.ChainConfig, startBlockNumber *big.Int, st
140143
return &DACodecV0{}
141144
}
142145
}
146+
147+
// CompressScrollBatchBytes compresses batch bytes using the appropriate codec based on block number and timestamp
148+
func CompressScrollBatchBytes(batchBytes []byte, blockNumber uint64, blockTimestamp uint64, chainCfg *params.ChainConfig) ([]byte, error) {
149+
codec := CodecFromConfig(chainCfg, big.NewInt(int64(blockNumber)), blockTimestamp)
150+
return codec.CompressScrollBatchBytes(batchBytes)
151+
}

0 commit comments

Comments
 (0)