Skip to content

Commit cb6acfa

Browse files
authored
refactor: abstract codec versions into common interfaces (#25)
* feat: support conditional encode * move append conditionalEncode flag after validity check * update da-codec * align naming * add ConvertBlobToBlobBytes utility functions * kept blob bytes * rename enableEncode to enableCompress * refactor: move some common functions to encoding (#24) * refactor: move some common functions to encoding * fix golint * move symbol replace script to zstd folder * refactor: move some util functions to public package * fix CI * add interfaces of codec * add SetCompression * move interface to encoding * refactor * add dablock.go * add dachunk.go * add dabatch.go * move computeBatchDataHash to codecv * fix * add DABatchBase * add GetCodecVersion * add BlobVersionedHashes * rename encoding.go to interfaces.go * add NewDABatchWithExpectedBlobVersionedHashes * tweak * fix a bug * add more logs * add DecodeDAChunks * add BlockRange interface * fix * add version check * add Version * remove DABatchBase * add DABlock * fixes * fix * add CodecFromVersion and CodecFromConfig * remove GetCodecVersion * fix typos * make Block fields internal * make chunk fields internal * make batch fields internal and add some tweaks * add JSONFromBytes * fix a typo * use register mode * fix CI * remove register mode * add common functions * add EstimateBlockL1CommitCalldataSize * add dabatch interfaces * update interface implementations * add data hash * fix codecv3 & codecv4 estimate gas * fix * fix bugs * tweak * add CheckChunkCompressedDataCompatibility & CheckBatchCompressedDataCompatibility * fix * add BlobDataProofForPointEvaluation * add nil check in NewDAChunk * make some util functions internal * remove GetMaxChunksPerBatch * fix CI * change receiver mark from o to d * remove SetCompression * fix * add GetChunkEnableCompression & GetBatchEnableCompression * fix * update l2geth dependency * make types internal * embed codecv0 <- codecv1 <- codecv2 <- codecv3 <- codecv4 * remove dablock.go dachunk.go dabatch.go * trigger ci * tweak * tweaks * add back TxsToTxsData * fix * fix * fix * fix * fix * fix * tweak * add back test block encode and test chunk encode unit tests * replace some constants with meaningful vars * rename daBatchV2 to daBatchV3 * add chunk hash unit tests * add batch encode * add batch hash unit tests * add batch data hash & json marshal unit tests * add calldata size unit tests * add commit gas estimation * add BatchSizeAndBlobSizeEstimation unit tests * add L1MessagePopped unit tests * add BlobEncodingAndHashing unit tests * add blob data proof unit tests * add TestDecodeBitmap * add BlobCompressDecompress unit tests * tweaks and bug fixes * fix goimport * add StandardTestCases * edge cases * fixes * address ai's comments * address ai's comments * address ai's comments * address comments * address ai's comment * remove some constants * address comments * add more logs * address comments * address comments * address comments * address comments * add simple and nil functions unit tests * add uncompressed case unit tests of DecodeTxsFromBlob * add interface unit tests * add codecv2 & codecv3 CompressedDataCompatibilityCheck unit tests * remove mock flag * add JSONFromBytes unit tests * add codecv4 CompressedDataCompatibilityCheck unit tests * add NewDABatchFromBytes unit tests * fix golint * tweaks
1 parent 41c6486 commit cb6acfa

30 files changed

+8377
-6325
lines changed

encoding/bitmap.go

Lines changed: 12 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ import (
77
"github.com/scroll-tech/go-ethereum/core/types"
88
)
99

10-
// ConstructSkippedBitmap constructs skipped L1 message bitmap of the batch.
11-
func ConstructSkippedBitmap(batchIndex uint64, chunks []*Chunk, totalL1MessagePoppedBefore uint64) ([]byte, uint64, error) {
10+
// constructSkippedBitmap constructs skipped L1 message bitmap of the batch.
11+
func constructSkippedBitmap(batchIndex uint64, chunks []*Chunk, totalL1MessagePoppedBefore uint64) ([]byte, uint64, error) {
1212
// skipped L1 message bitmap, an array of 256-bit bitmaps
1313
var skippedBitmap []*big.Int
1414

@@ -54,39 +54,29 @@ func ConstructSkippedBitmap(batchIndex uint64, chunks []*Chunk, totalL1MessagePo
5454
}
5555
}
5656

57-
bitmapBytes := make([]byte, len(skippedBitmap)*32)
57+
skippedL1MessageBitmap := make([]byte, len(skippedBitmap)*skippedL1MessageBitmapByteSize)
5858
for ii, num := range skippedBitmap {
5959
bytes := num.Bytes()
60-
padding := 32 - len(bytes)
61-
copy(bitmapBytes[32*ii+padding:], bytes)
60+
padding := skippedL1MessageBitmapByteSize - len(bytes)
61+
copy(skippedL1MessageBitmap[skippedL1MessageBitmapByteSize*ii+padding:], bytes)
6262
}
6363

64-
return bitmapBytes, nextIndex, nil
64+
return skippedL1MessageBitmap, nextIndex, nil
6565
}
6666

67-
// DecodeBitmap decodes skipped L1 message bitmap of the batch from bytes to big.Int's
68-
func DecodeBitmap(skippedL1MessageBitmap []byte, totalL1MessagePopped int) ([]*big.Int, error) {
67+
// decodeBitmap decodes skipped L1 message bitmap of the batch from bytes to big.Int's.
68+
func decodeBitmap(skippedL1MessageBitmap []byte, totalL1MessagePopped int) ([]*big.Int, error) {
6969
length := len(skippedL1MessageBitmap)
70-
if length%32 != 0 {
71-
return nil, fmt.Errorf("skippedL1MessageBitmap length doesn't match, skippedL1MessageBitmap length should be equal 0 modulo 32, length of skippedL1MessageBitmap: %v", length)
70+
if length%skippedL1MessageBitmapByteSize != 0 {
71+
return nil, fmt.Errorf("skippedL1MessageBitmap length doesn't match, skippedL1MessageBitmap length should be equal 0 modulo %v, length of skippedL1MessageBitmap: %v", skippedL1MessageBitmapByteSize, length)
7272
}
7373
if length*8 < totalL1MessagePopped {
7474
return nil, fmt.Errorf("skippedL1MessageBitmap length is too small, skippedL1MessageBitmap length should be at least %v, length of skippedL1MessageBitmap: %v", (totalL1MessagePopped+7)/8, length)
7575
}
7676
var skippedBitmap []*big.Int
77-
for index := 0; index < length/32; index++ {
78-
bitmap := big.NewInt(0).SetBytes(skippedL1MessageBitmap[index*32 : index*32+32])
77+
for index := 0; index < length/skippedL1MessageBitmapByteSize; index++ {
78+
bitmap := big.NewInt(0).SetBytes(skippedL1MessageBitmap[index*skippedL1MessageBitmapByteSize : index*skippedL1MessageBitmapByteSize+skippedL1MessageBitmapByteSize])
7979
skippedBitmap = append(skippedBitmap, bitmap)
8080
}
8181
return skippedBitmap, nil
8282
}
83-
84-
// IsL1MessageSkipped checks if index is skipped in bitmap
85-
func IsL1MessageSkipped(skippedBitmap []*big.Int, index uint64) bool {
86-
if index > uint64(len(skippedBitmap))*256 {
87-
return false
88-
}
89-
quo := index / 256
90-
rem := index % 256
91-
return skippedBitmap[quo].Bit(int(rem)) != 0
92-
}

encoding/bitmap_test.go

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package encoding
2+
3+
import (
4+
"encoding/hex"
5+
"math/big"
6+
"testing"
7+
8+
"github.com/stretchr/testify/assert"
9+
)
10+
11+
func TestDecodeBitmap(t *testing.T) {
12+
bitmapHex := "0000000000000000000000000000000000000000000000000000001ffffffbff"
13+
skippedL1MessageBitmap, err := hex.DecodeString(bitmapHex)
14+
assert.NoError(t, err)
15+
16+
decodedBitmap, err := decodeBitmap(skippedL1MessageBitmap, 42)
17+
assert.NoError(t, err)
18+
19+
isL1MessageSkipped := func(skippedBitmap []*big.Int, index uint64) bool {
20+
if index >= uint64(len(skippedBitmap))*256 {
21+
return false
22+
}
23+
quo := index / 256
24+
rem := index % 256
25+
return skippedBitmap[quo].Bit(int(rem)) == 1
26+
}
27+
28+
assert.True(t, isL1MessageSkipped(decodedBitmap, 0))
29+
assert.True(t, isL1MessageSkipped(decodedBitmap, 9))
30+
assert.False(t, isL1MessageSkipped(decodedBitmap, 10))
31+
assert.True(t, isL1MessageSkipped(decodedBitmap, 11))
32+
assert.True(t, isL1MessageSkipped(decodedBitmap, 36))
33+
assert.False(t, isL1MessageSkipped(decodedBitmap, 37))
34+
assert.False(t, isL1MessageSkipped(decodedBitmap, 38))
35+
assert.False(t, isL1MessageSkipped(decodedBitmap, 39))
36+
assert.False(t, isL1MessageSkipped(decodedBitmap, 40))
37+
assert.False(t, isL1MessageSkipped(decodedBitmap, 41))
38+
39+
_, err = decodeBitmap([]byte{0x00}, 8)
40+
assert.Error(t, err)
41+
42+
_, err = decodeBitmap([]byte{0x00, 0x00, 0x00, 0x00}, 33)
43+
assert.Error(t, err)
44+
}

0 commit comments

Comments
 (0)