Skip to content

Commit

Permalink
add tests for codecv0 and ccodecv1
Browse files Browse the repository at this point in the history
  • Loading branch information
NazariiDenha committed Jun 12, 2024
1 parent 7665c1e commit 83fbd77
Show file tree
Hide file tree
Showing 6 changed files with 107 additions and 1 deletion.
3 changes: 3 additions & 0 deletions encoding/bitmap.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,9 @@ func DecodeBitmap(skippedL1MessageBitmap []byte, totalL1MessagePopped int) ([]*b

// IsL1MessageSkipped checks if index is skipped in bitmap
func IsL1MessageSkipped(skippedBitmap []*big.Int, index uint64) bool {
if index > uint64(len(skippedBitmap))*256 {
return false
}
quo := index / 256
rem := index % 256
return skippedBitmap[quo].Bit(int(rem)) != 0
Expand Down
1 change: 1 addition & 0 deletions encoding/codecv0/codecv0.go
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,7 @@ func DecodeDAChunksRawTx(bytes [][]byte) ([]*DAChunkRawTx, error) {
for i := 0; i < numBlocks; i++ {
startIdx := 1 + i*BlockContextByteSize // add 1 to skip numBlocks byte
endIdx := startIdx + BlockContextByteSize
blocks[i] = &DABlock{}
err := blocks[i].Decode(chunk[startIdx:endIdx])
if err != nil {
return nil, err
Expand Down
47 changes: 46 additions & 1 deletion encoding/codecv0/codecv0_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -258,12 +258,44 @@ func TestCodecV0(t *testing.T) {
assert.NoError(t, err)
assert.Equal(t, uint64(15189), chunk2L1CommitGas)

daChunk2, err := NewDAChunk(chunk2, 0)
daChunk2, err := NewDAChunk(chunk2, 11)
assert.NoError(t, err)
chunkBytes2, err := daChunk2.Encode()
assert.NoError(t, err)
assert.Equal(t, 61, len(chunkBytes2))

daChunksRawTx, err := DecodeDAChunksRawTx([][]byte{chunkBytes1, chunkBytes2})
assert.NoError(t, err)
// assert number of chunks
assert.Equal(t, 2, len(daChunksRawTx))

// assert block in first chunk
assert.Equal(t, 3, len(daChunksRawTx[0].Blocks))
assert.Equal(t, daChunk1.Blocks[0], daChunksRawTx[0].Blocks[0])
assert.Equal(t, daChunk1.Blocks[1], daChunksRawTx[0].Blocks[1])
daChunksRawTx[0].Blocks[2].BaseFee = nil
assert.Equal(t, daChunk1.Blocks[2], daChunksRawTx[0].Blocks[2])

// assert block in second chunk
assert.Equal(t, 1, len(daChunksRawTx[1].Blocks))
daChunksRawTx[1].Blocks[0].BaseFee = nil
assert.Equal(t, daChunk2.Blocks[0], daChunksRawTx[1].Blocks[0])

// assert transactions in first chunk
assert.Equal(t, 3, len(daChunksRawTx[0].Transactions))
// here number of transactions in encoded and decoded chunks may be different, because decodec chunks doesn't contain l1msgs
assert.Equal(t, 2, len(daChunksRawTx[0].Transactions[0]))
assert.Equal(t, 1, len(daChunksRawTx[0].Transactions[1]))
assert.Equal(t, 1, len(daChunksRawTx[0].Transactions[2]))

assert.EqualValues(t, daChunk1.Transactions[0][0].TxHash, daChunksRawTx[0].Transactions[0][0].Hash().String())
assert.EqualValues(t, daChunk1.Transactions[0][1].TxHash, daChunksRawTx[0].Transactions[0][1].Hash().String())

// assert transactions in second chunk
assert.Equal(t, 1, len(daChunksRawTx[1].Transactions))
// here number of transactions in encoded and decoded chunks may be different, because decodec chunks doesn't contain l1msgs
assert.Equal(t, 0, len(daChunksRawTx[1].Transactions[0]))

batch = &encoding.Batch{
Index: 1,
TotalL1MessagePoppedBefore: 0,
Expand Down Expand Up @@ -297,6 +329,19 @@ func TestCodecV0(t *testing.T) {
decodedBatchHexString = hex.EncodeToString(decodedBatchBytes)
assert.Equal(t, batchHexString, decodedBatchHexString)

decodedBitmap, err := encoding.DecodeBitmap(decodedDABatch.SkippedL1MessageBitmap, int(decodedDABatch.L1MessagePopped))
assert.NoError(t, err)
assert.True(t, encoding.IsL1MessageSkipped(decodedBitmap, 0))
assert.True(t, encoding.IsL1MessageSkipped(decodedBitmap, 11))
assert.False(t, encoding.IsL1MessageSkipped(decodedBitmap, 10))
assert.True(t, encoding.IsL1MessageSkipped(decodedBitmap, 11))
assert.True(t, encoding.IsL1MessageSkipped(decodedBitmap, 36))
assert.False(t, encoding.IsL1MessageSkipped(decodedBitmap, 37))
assert.False(t, encoding.IsL1MessageSkipped(decodedBitmap, 38))
assert.False(t, encoding.IsL1MessageSkipped(decodedBitmap, 39))
assert.False(t, encoding.IsL1MessageSkipped(decodedBitmap, 40))
assert.False(t, encoding.IsL1MessageSkipped(decodedBitmap, 41))

// Test case: many consecutive L1 Msgs in 1 bitmap, no leading skipped msgs.
chunk = &encoding.Chunk{
Blocks: []*encoding.Block{block4},
Expand Down
1 change: 1 addition & 0 deletions encoding/codecv1/codecv1.go
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,7 @@ func DecodeDAChunksRawTx(bytes [][]byte) ([]*DAChunkRawTx, error) {
for i := 0; i < numBlocks; i++ {
startIdx := 1 + i*BlockContextByteSize // add 1 to skip numBlocks byte
endIdx := startIdx + BlockContextByteSize
blocks[i] = &DABlock{}
err := blocks[i].Decode(chunk[startIdx:endIdx])
if err != nil {
return nil, err
Expand Down
55 changes: 55 additions & 0 deletions encoding/codecv1/codecv1_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -473,6 +473,61 @@ func TestCodecV1BatchBlob(t *testing.T) {
assert.Equal(t, "0x01b63f87bdd2caa8d43500d47ee59204f61af95339483c62ff436c6beabf47bf", batch.BlobVersionedHash.Hex())
}

func TestCodecV1Decode(t *testing.T) {
trace0 := readBlockFromJSON(t, "../testdata/blockTrace_02.json")
trace1 := readBlockFromJSON(t, "../testdata/blockTrace_03.json")
chunk0 := &encoding.Chunk{Blocks: []*encoding.Block{trace0, trace1}}
daChunk0, err := NewDAChunk(chunk0, 0)
assert.NoError(t, err)
chunkBytes0 := daChunk0.Encode()

trace2 := readBlockFromJSON(t, "../testdata/blockTrace_04.json")
trace3 := readBlockFromJSON(t, "../testdata/blockTrace_05.json")
chunk1 := &encoding.Chunk{Blocks: []*encoding.Block{trace2, trace3}}
daChunk1, err := NewDAChunk(chunk1, 0)
assert.NoError(t, err)
chunkBytes1 := daChunk1.Encode()

originalBatch := &encoding.Batch{Chunks: []*encoding.Chunk{chunk0, chunk1}}
batch, err := NewDABatch(originalBatch)
assert.NoError(t, err)

daChunksRawTx, err := DecodeDAChunksRawTx([][]byte{chunkBytes0, chunkBytes1})
assert.NoError(t, err)
// assert number of chunks
assert.Equal(t, 2, len(daChunksRawTx))

// assert block in first chunk
assert.Equal(t, 2, len(daChunksRawTx[0].Blocks))
assert.Equal(t, daChunk0.Blocks[0], daChunksRawTx[0].Blocks[0])
assert.Equal(t, daChunk0.Blocks[1], daChunksRawTx[0].Blocks[1])

// assert block in second chunk
assert.Equal(t, 2, len(daChunksRawTx[1].Blocks))
daChunksRawTx[1].Blocks[0].BaseFee = nil
assert.Equal(t, daChunk1.Blocks[0], daChunksRawTx[1].Blocks[0])
daChunksRawTx[1].Blocks[1].BaseFee = nil
assert.Equal(t, daChunk1.Blocks[1], daChunksRawTx[1].Blocks[1])

blob := batch.Blob()
DecodeTxsFromBlob(blob, daChunksRawTx)

// assert transactions in first chunk
assert.Equal(t, 2, len(daChunksRawTx[0].Transactions))
// here number of transactions in encoded and decoded chunks may be different, because decodec chunks doesn't contain l1msgs
assert.Equal(t, 2, len(daChunksRawTx[0].Transactions[0]))
assert.Equal(t, 1, len(daChunksRawTx[0].Transactions[1]))

assert.EqualValues(t, daChunk0.Transactions[0][0].TxHash, daChunksRawTx[0].Transactions[0][0].Hash().String())
assert.EqualValues(t, daChunk0.Transactions[0][1].TxHash, daChunksRawTx[0].Transactions[0][1].Hash().String())

// assert transactions in second chunk
assert.Equal(t, 2, len(daChunksRawTx[1].Transactions))
// here number of transactions in encoded and decoded chunks may be different, because decodec chunks doesn't contain l1msgs
assert.Equal(t, 1, len(daChunksRawTx[1].Transactions[0]))
assert.Equal(t, 0, len(daChunksRawTx[1].Transactions[1]))
}

func TestCodecV1BatchChallenge(t *testing.T) {
trace2 := readBlockFromJSON(t, "../testdata/blockTrace_02.json")
chunk2 := &encoding.Chunk{Blocks: []*encoding.Block{trace2}}
Expand Down
1 change: 1 addition & 0 deletions encoding/codecv2/codecv2.go
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,7 @@ func DecodeDAChunksRawTx(bytes [][]byte) ([]*DAChunkRawTx, error) {
for i := 0; i < numBlocks; i++ {
startIdx := 1 + i*BlockContextByteSize // add 1 to skip numBlocks byte
endIdx := startIdx + BlockContextByteSize
blocks[i] = &DABlock{}
err := blocks[i].Decode(chunk[startIdx:endIdx])
if err != nil {
return nil, err
Expand Down

0 comments on commit 83fbd77

Please sign in to comment.