-
Notifications
You must be signed in to change notification settings - Fork 22
feat(CodecV7): add CodecV7 to support upgrade 5.2 Euclid phase2 #33
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 5 commits
Commits
Show all changes
47 commits
Select commit
Hold shift + click to select a range
286f209
add initial CodecV6 and daBatchV6
jonastheis 6767845
feat: add codecv5 and codecv6 for Euclid fork
omerfirmak cc9561b
implement blob encoding and decoding according to new blob layout
jonastheis 8c2a5cc
rename to CodecV7
jonastheis 9117170
add NewDABatchFromParams
jonastheis 4ef7bfc
add DecodeBlob to Codec
jonastheis bf16156
Update da.go
omerfirmak 2817674
Update interfaces.go
omerfirmak 7a60b34
Merge remote-tracking branch 'origin/omerfirmak/euclid' into feat/cod…
jonastheis 64133ef
fixes after merge
jonastheis 1dde89a
address review comments
jonastheis c9c1a44
add sanity checks for blob payload generation
jonastheis e980b3d
fix few small bugs uncovered by unit tests
jonastheis 0e930c6
upgrade to latest l2geth version and add correct getter for CodecV7 i…
jonastheis 5d200f3
fix linter warnings
jonastheis 5292e3c
add unit tests
jonastheis 3cfed43
go mod tidy
jonastheis eed341f
fix linter warnings
jonastheis be6b422
add function MessageQueueV2ApplyL1MessagesFromBlocks to compute the L…
jonastheis d77916b
fix lint and unit test errors
b71c047
call checkCompressedDataCompatibility only once -> constructBlobPaylo…
jonastheis cbed8b2
address review comments
jonastheis 392b6ff
update BlobEnvelopeV7 documentation
jonastheis edaf5d2
add CodecV7 to general util functions
jonastheis 894a93b
add InitialL1MessageQueueHash and LastL1MessageQueueHash to encoding.…
jonastheis f3271d9
Merge remote-tracking branch 'origin/main' into feat/codec-v6
jonastheis 2611ae1
go mod tidy
jonastheis 4d46aad
upgrade go-ethereum dependency to latest develop
jonastheis f4b274c
implement estimate functions
jonastheis 3c106a2
update TestMain and run go mod tidy
Thegaram 538036b
add NewDAChunk to CodecV7 for easier use in relayer
jonastheis 14d07e7
Merge branch 'feat/codec-v6' of github.com:scroll-tech/da-codec into …
jonastheis cfb316b
add daChunkV7 type to calculate chunk hash
jonastheis c6ae41e
allow batch.chunks but check consistency with batch.blocks
jonastheis d028c53
fix off-by-one error with L1 messages
jonastheis 8fa5e27
Fix: rolling hash implementation (#42)
roynalnaruto 4f13363
Apply suggestions from code review
jonastheis bcad556
rename initialL1MessageQueueHash -> prevL1MessageQueueHash and lastL1…
jonastheis 7522931
address review comments
jonastheis 32f5b49
address review comments
jonastheis 0247443
add challenge digest computation for batch
jonastheis 2043787
remove InitialL1MessageIndex from CodecV7
jonastheis de09af4
address review comments
jonastheis f9608ed
fix tests
jonastheis 01bd9b5
refactoring to minimize duplicate code and increase maintainability
jonastheis fca406c
fix nil pointer
jonastheis 5fd8356
address review comments
jonastheis File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,319 @@ | ||
| package encoding | ||
|
|
||
| import ( | ||
| "crypto/sha256" | ||
| "encoding/hex" | ||
| "encoding/json" | ||
| "errors" | ||
| "fmt" | ||
| "math" | ||
|
|
||
| "github.com/scroll-tech/go-ethereum/common" | ||
| "github.com/scroll-tech/go-ethereum/crypto/kzg4844" | ||
| "github.com/scroll-tech/go-ethereum/log" | ||
|
|
||
| "github.com/scroll-tech/da-codec/encoding/zstd" | ||
| ) | ||
|
|
||
| type DACodecV7 struct{} | ||
|
|
||
| // Version returns the codec version. | ||
| func (d *DACodecV7) Version() CodecVersion { | ||
| return CodecV7 | ||
| } | ||
|
|
||
| // MaxNumChunksPerBatch returns the maximum number of chunks per batch. | ||
| func (d *DACodecV7) MaxNumChunksPerBatch() int { | ||
| return 1 | ||
| } | ||
|
|
||
| // NewDABlock creates a new DABlock from the given Block and the total number of L1 messages popped before. | ||
| func (d *DACodecV7) NewDABlock(block *Block, _ uint64) (DABlock, error) { | ||
| if !block.Header.Number.IsUint64() { | ||
| return nil, errors.New("block number is not uint64") | ||
| } | ||
|
|
||
| // note: numL1Messages includes skipped messages | ||
| numL1Messages := block.NumL1MessagesNoSkipping() | ||
| if numL1Messages > math.MaxUint16 { | ||
| return nil, errors.New("number of L1 messages exceeds max uint16") | ||
| } | ||
|
|
||
| // note: numTransactions includes skipped messages | ||
jonastheis marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| numL2Transactions := block.NumL2Transactions() | ||
| numTransactions := uint64(numL1Messages) + numL2Transactions | ||
| if numTransactions > math.MaxUint16 { | ||
| return nil, errors.New("number of transactions exceeds max uint16") | ||
| } | ||
|
|
||
| daBlock := newDABlockV7( | ||
| block.Header.Number.Uint64(), // number | ||
| block.Header.Time, // timestamp | ||
| block.Header.BaseFee, // baseFee | ||
| block.Header.GasLimit, // gasLimit | ||
| uint16(numTransactions), // numTransactions | ||
| numL1Messages, // numL1Messages | ||
| ) | ||
|
|
||
| return daBlock, nil | ||
| } | ||
|
|
||
| // NewDAChunk creates a new DAChunk from the given Chunk and the total number of L1 messages popped before. | ||
| // Note: For DACodecV7, this function is not implemented since there is no notion of DAChunk in this version. Blobs | ||
| // contain the entire batch data, and it is up to a prover to decide the chunk sizes. | ||
Thegaram marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| func (d *DACodecV7) NewDAChunk(_ *Chunk, _ uint64) (DAChunk, error) { | ||
| return nil, nil | ||
| } | ||
|
|
||
| // NewDABatch creates a DABatch including blob from the provided Batch. | ||
| func (d *DACodecV7) NewDABatch(batch *Batch) (DABatch, error) { | ||
| if len(batch.Chunks) != 0 { | ||
| return nil, errors.New("batch must not contain any chunks") | ||
| } | ||
|
|
||
| if len(batch.Blocks) == 0 { | ||
| return nil, errors.New("batch must contain at least one block") | ||
| } | ||
|
|
||
| blob, blobVersionedHash, blobBytes, err := d.constructBlob(batch) | ||
| if err != nil { | ||
| return nil, fmt.Errorf("failed to construct blob: %w", err) | ||
| } | ||
|
|
||
| daBatch, err := newDABatchV7(CodecV7, batch.Index, blobVersionedHash, batch.ParentBatchHash, blob, blobBytes) | ||
| if err != nil { | ||
| return nil, fmt.Errorf("failed to construct DABatch: %w", err) | ||
| } | ||
|
|
||
| return daBatch, nil | ||
| } | ||
|
|
||
| func (d *DACodecV7) constructBlob(batch *Batch) (*kzg4844.Blob, common.Hash, []byte, error) { | ||
| enableCompression, err := d.CheckBatchCompressedDataCompatibility(batch) | ||
Thegaram marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| if err != nil { | ||
| return nil, common.Hash{}, nil, fmt.Errorf("failed to check batch compressed data compatibility: %w", err) | ||
| } | ||
|
|
||
| blobBytes := make([]byte, blobEnvelopeV7PayloadOffset) | ||
| blobBytes[blobEnvelopeV7VersionOffset] = uint8(CodecV7) | ||
|
|
||
| payloadBytes, err := d.constructBlobPayload(batch) | ||
| if err != nil { | ||
| return nil, common.Hash{}, nil, fmt.Errorf("failed to construct blob payload: %w", err) | ||
| } | ||
|
|
||
| if enableCompression { | ||
| // compressedPayloadBytes represents the compressed blob payload | ||
| compressedPayloadBytes, err := zstd.CompressScrollBatchBytes(payloadBytes) | ||
| if err != nil { | ||
| return nil, common.Hash{}, nil, fmt.Errorf("failed to compress blob payload: %w", err) | ||
| } | ||
| // Check compressed data compatibility. | ||
| if err = checkCompressedDataCompatibility(compressedPayloadBytes); err != nil { | ||
| log.Error("ConstructBlob: compressed data compatibility check failed", "err", err, "payloadBytes", hex.EncodeToString(payloadBytes), "compressedPayloadBytes", hex.EncodeToString(compressedPayloadBytes)) | ||
| return nil, common.Hash{}, nil, err | ||
| } | ||
| blobBytes[blobEnvelopeV7CompressedFlagOffset] = 0x1 | ||
| payloadBytes = compressedPayloadBytes | ||
| } else { | ||
| blobBytes[blobEnvelopeV7CompressedFlagOffset] = 0x0 | ||
| } | ||
|
|
||
| sizeSlice := encodeSize3Bytes(uint32(len(payloadBytes))) | ||
| copy(blobBytes[blobEnvelopeV7ByteSizeOffset:blobEnvelopeV7CompressedFlagOffset], sizeSlice) | ||
| blobBytes = append(blobBytes, payloadBytes...) | ||
|
|
||
| if len(blobBytes) > maxEffectiveBlobBytes { | ||
| log.Error("ConstructBlob: Blob payload exceeds maximum size", "size", len(blobBytes), "blobBytes", hex.EncodeToString(blobBytes)) | ||
| return nil, common.Hash{}, nil, errors.New("blob exceeds maximum size") | ||
| } | ||
|
|
||
| // convert raw data to BLSFieldElements | ||
| blob, err := makeBlobCanonical(blobBytes) | ||
| if err != nil { | ||
| return nil, common.Hash{}, nil, fmt.Errorf("failed to convert blobBytes to canonical form: %w", err) | ||
| } | ||
|
|
||
| // compute blob versioned hash | ||
| c, err := kzg4844.BlobToCommitment(blob) | ||
| if err != nil { | ||
| return nil, common.Hash{}, nil, fmt.Errorf("failed to create blob commitment: %w", err) | ||
| } | ||
| blobVersionedHash := kzg4844.CalcBlobHashV1(sha256.New(), &c) | ||
|
|
||
| return blob, blobVersionedHash, blobBytes, nil | ||
| } | ||
|
|
||
| func (d *DACodecV7) constructBlobPayload(batch *Batch) ([]byte, error) { | ||
| blobPayload := blobPayloadV7{ | ||
| initialL1MessageIndex: batch.InitialL1MessageIndex, | ||
| initialL1MessageQueueHash: batch.InitialL1MessageQueueHash, | ||
| lastL1MessageQueueHash: batch.LastL1MessageQueueHash, | ||
| blocks: batch.Blocks, | ||
| } | ||
|
|
||
| return blobPayload.Encode() | ||
| } | ||
|
|
||
| // NewDABatchFromBytes decodes the given byte slice into a DABatch. | ||
| // Note: This function only populates the batch header, it leaves the blob-related fields and skipped L1 message bitmap empty. | ||
jonastheis marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| func (d *DACodecV7) NewDABatchFromBytes(data []byte) (DABatch, error) { | ||
| daBatch, err := decodeDABatchV7(data) | ||
| if err != nil { | ||
| return nil, fmt.Errorf("failed to decode DA batch: %w", err) | ||
| } | ||
|
|
||
| if daBatch.version != CodecV7 { | ||
| return nil, fmt.Errorf("codec version mismatch: expected %d but found %d", CodecV7, daBatch.version) | ||
| } | ||
|
|
||
| return daBatch, nil | ||
| } | ||
|
|
||
| func (d *DACodecV7) NewDABatchFromParams(batchIndex uint64, blobVersionedHash, parentBatchHash common.Hash) (DABatch, error) { | ||
| return newDABatchV7(CodecV7, batchIndex, blobVersionedHash, parentBatchHash, nil, nil) | ||
| } | ||
|
|
||
| func (d *DACodecV7) DecodeDAChunksRawTx(_ [][]byte) ([]*DAChunkRawTx, error) { | ||
| return nil, nil | ||
Thegaram marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| } | ||
|
|
||
| func (d *DACodecV7) DecodeBlob(blob *kzg4844.Blob) (DABlobPayload, error) { | ||
| rawBytes := bytesFromBlobCanonical(blob) | ||
|
|
||
| // read the blob envelope header | ||
| version := rawBytes[blobEnvelopeV7VersionOffset] | ||
| if CodecVersion(version) != CodecV7 { | ||
| return nil, fmt.Errorf("codec version mismatch: expected %d but found %d", CodecV7, version) | ||
| } | ||
|
|
||
| // read the data size | ||
| blobEnvelopeSize := decodeSize3Bytes(rawBytes[blobEnvelopeV7ByteSizeOffset:blobEnvelopeV7CompressedFlagOffset]) | ||
| if blobEnvelopeSize+blobEnvelopeV7PayloadOffset > uint32(len(rawBytes)) { | ||
| return nil, fmt.Errorf("blob envelope size exceeds the raw data size: %d > %d", blobEnvelopeSize, len(rawBytes)) | ||
| } | ||
|
|
||
| payloadBytes := rawBytes[blobEnvelopeV7PayloadOffset : blobEnvelopeV7PayloadOffset+blobEnvelopeSize] | ||
|
|
||
| // read the compressed flag and decompress if needed | ||
| compressed := rawBytes[blobEnvelopeV7CompressedFlagOffset] | ||
| if compressed == 0x1 { | ||
| var err error | ||
| if payloadBytes, err = decompressV7Bytes(payloadBytes); err != nil { | ||
| return nil, fmt.Errorf("failed to decompress blob payload: %w", err) | ||
| } | ||
| } | ||
|
|
||
Thegaram marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| // read the payload | ||
| payload, err := decodeBlobPayloadV7(payloadBytes) | ||
| if err != nil { | ||
| return nil, fmt.Errorf("failed to decode blob payload: %w", err) | ||
| } | ||
|
|
||
| return payload, nil | ||
| } | ||
|
|
||
| func (d *DACodecV7) DecodeTxsFromBlob(blob *kzg4844.Blob, chunks []*DAChunkRawTx) error { | ||
| payload, err := d.DecodeBlob(blob) | ||
| if err != nil { | ||
| return fmt.Errorf("failed to decode blob: %w", err) | ||
| } | ||
|
|
||
| chunks = append(chunks, &DAChunkRawTx{ | ||
| Blocks: payload.Blocks(), | ||
| Transactions: payload.Transactions(), | ||
| }) | ||
|
|
||
| return nil | ||
| } | ||
|
|
||
| // checkCompressedDataCompatibility checks the compressed data compatibility for a batch. | ||
| // It constructs a blob payload, compresses the data, and checks the compressed data compatibility. | ||
| func (d *DACodecV7) checkCompressedDataCompatibility(batch *Batch) (bool, error) { | ||
| payloadBytes, err := d.constructBlobPayload(batch) | ||
| if err != nil { | ||
| return false, fmt.Errorf("failed to construct blob payload: %w", err) | ||
| } | ||
|
|
||
| compressedPayloadBytes, err := zstd.CompressScrollBatchBytes(payloadBytes) | ||
| if err != nil { | ||
| return false, fmt.Errorf("failed to compress blob payload: %w", err) | ||
| } | ||
|
|
||
| if err = checkCompressedDataCompatibility(compressedPayloadBytes); err != nil { | ||
| log.Warn("Compressed data compatibility check failed", "err", err, "payloadBytes", hex.EncodeToString(payloadBytes), "compressedPayloadBytes", hex.EncodeToString(compressedPayloadBytes)) | ||
| return false, nil | ||
| } | ||
|
|
||
| // check if compressed data is bigger or equal to the original data -> no need to compress | ||
| if len(compressedPayloadBytes) >= len(payloadBytes) { | ||
| log.Warn("Compressed data is bigger or equal to the original data", "payloadBytes", hex.EncodeToString(payloadBytes), "compressedPayloadBytes", hex.EncodeToString(compressedPayloadBytes)) | ||
| return false, nil | ||
| } | ||
|
|
||
| return true, nil | ||
| } | ||
|
|
||
| // CheckChunkCompressedDataCompatibility checks the compressed data compatibility for a batch built from a single chunk. | ||
| // Note: For DACodecV7, this function is not implemented since there is no notion of DAChunk in this version. Blobs | ||
| // contain the entire batch data, and it is up to a prover to decide the chunk sizes. | ||
| func (d *DACodecV7) CheckChunkCompressedDataCompatibility(_ *Chunk) (bool, error) { | ||
| return true, nil | ||
| } | ||
|
|
||
| // CheckBatchCompressedDataCompatibility checks the compressed data compatibility for a batch. | ||
| func (d *DACodecV7) CheckBatchCompressedDataCompatibility(b *Batch) (bool, error) { | ||
| return d.checkCompressedDataCompatibility(b) | ||
| } | ||
|
|
||
| // TODO: which of the Estimate* functions are needed? | ||
colinlyguo marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| func (d *DACodecV7) EstimateChunkL1CommitBatchSizeAndBlobSize(chunk *Chunk) (uint64, uint64, error) { | ||
| //TODO implement me after contracts are implemented | ||
| panic("implement me") | ||
| } | ||
|
|
||
| func (d *DACodecV7) EstimateBatchL1CommitBatchSizeAndBlobSize(batch *Batch) (uint64, uint64, error) { | ||
| //TODO implement me after contracts are implemented | ||
| panic("implement me") | ||
| } | ||
|
|
||
| func (d *DACodecV7) EstimateBlockL1CommitCalldataSize(block *Block) (uint64, error) { | ||
| //TODO implement me after contracts are implemented | ||
| panic("implement me") | ||
| } | ||
|
|
||
| func (d *DACodecV7) EstimateChunkL1CommitCalldataSize(chunk *Chunk) (uint64, error) { | ||
| //TODO implement me after contracts are implemented | ||
| panic("implement me") | ||
| } | ||
|
|
||
| func (d *DACodecV7) EstimateChunkL1CommitGas(chunk *Chunk) (uint64, error) { | ||
| //TODO implement me after contracts are implemented | ||
| panic("implement me") | ||
| } | ||
|
|
||
| func (d *DACodecV7) EstimateBatchL1CommitGas(batch *Batch) (uint64, error) { | ||
| //TODO implement me after contracts are implemented | ||
| panic("implement me") | ||
| } | ||
|
|
||
| func (d *DACodecV7) EstimateBatchL1CommitCalldataSize(batch *Batch) (uint64, error) { | ||
| //TODO implement me after contracts are implemented | ||
| panic("implement me") | ||
| } | ||
|
|
||
| // JSONFromBytes converts the bytes to a DABatch and then marshals it to JSON. | ||
| func (d *DACodecV7) JSONFromBytes(data []byte) ([]byte, error) { | ||
Thegaram marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| batch, err := d.NewDABatchFromBytes(data) | ||
| if err != nil { | ||
| return nil, fmt.Errorf("failed to decode DABatch from bytes: %w", err) | ||
| } | ||
|
|
||
| jsonBytes, err := json.Marshal(batch) | ||
| if err != nil { | ||
| return nil, fmt.Errorf("failed to marshal DABatch to JSON, version %d, hash %s: %w", batch.Version(), batch.Hash(), err) | ||
| } | ||
|
|
||
| return jsonBytes, nil | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.