Skip to content

Commit 1e07a66

Browse files
author
colinlyguo
committed
add simple and nil functions unit tests
1 parent 3faa778 commit 1e07a66

File tree

11 files changed

+103
-8
lines changed

11 files changed

+103
-8
lines changed

encoding/codecv0_test.go

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66

77
"github.com/scroll-tech/go-ethereum/common"
88
"github.com/scroll-tech/go-ethereum/core/types"
9+
"github.com/scroll-tech/go-ethereum/crypto/kzg4844"
910
"github.com/stretchr/testify/assert"
1011
"github.com/stretchr/testify/require"
1112
)
@@ -618,3 +619,57 @@ func TestCodecV0DecodeDAChunksRawTx(t *testing.T) {
618619
assert.Equal(t, 1, len(daChunksRawTx[1].Transactions[0]))
619620
assert.Equal(t, 0, len(daChunksRawTx[1].Transactions[1]))
620621
}
622+
623+
func TestDACodecV0SimpleMethods(t *testing.T) {
624+
codecv0, err := CodecFromVersion(CodecV0)
625+
require.NoError(t, err)
626+
627+
t.Run("Version", func(t *testing.T) {
628+
version := codecv0.Version()
629+
assert.Equal(t, CodecV0, version)
630+
})
631+
632+
t.Run("CheckChunkCompressedDataCompatibility", func(t *testing.T) {
633+
chunk := &Chunk{}
634+
compatible, err := codecv0.CheckChunkCompressedDataCompatibility(chunk)
635+
assert.NoError(t, err)
636+
assert.True(t, compatible)
637+
})
638+
639+
t.Run("CheckBatchCompressedDataCompatibility", func(t *testing.T) {
640+
batch := &Batch{}
641+
compatible, err := codecv0.CheckBatchCompressedDataCompatibility(batch)
642+
assert.NoError(t, err)
643+
assert.True(t, compatible)
644+
})
645+
646+
t.Run("EstimateChunkL1CommitBatchSizeAndBlobSize", func(t *testing.T) {
647+
chunk := &Chunk{}
648+
batchSize, blobSize, err := codecv0.EstimateChunkL1CommitBatchSizeAndBlobSize(chunk)
649+
assert.NoError(t, err)
650+
assert.Equal(t, uint64(0), batchSize)
651+
assert.Equal(t, uint64(0), blobSize)
652+
})
653+
654+
t.Run("EstimateBatchL1CommitBatchSizeAndBlobSize", func(t *testing.T) {
655+
batch := &Batch{}
656+
batchSize, blobSize, err := codecv0.EstimateBatchL1CommitBatchSizeAndBlobSize(batch)
657+
assert.NoError(t, err)
658+
assert.Equal(t, uint64(0), batchSize)
659+
assert.Equal(t, uint64(0), blobSize)
660+
})
661+
662+
t.Run("JSONFromBytes", func(t *testing.T) {
663+
data := []byte("test data")
664+
json, err := codecv0.JSONFromBytes(data)
665+
assert.NoError(t, err)
666+
assert.Nil(t, json)
667+
})
668+
669+
t.Run("DecodeTxsFromBlob", func(t *testing.T) {
670+
blob := &kzg4844.Blob{}
671+
chunks := []*DAChunkRawTx{}
672+
err := codecv0.DecodeTxsFromBlob(blob, chunks)
673+
assert.NoError(t, err)
674+
})
675+
}

encoding/codecv1.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -215,7 +215,7 @@ func (d *DACodecV1) constructBlobPayload(chunks []*Chunk, maxNumChunksPerBatch i
215215
// compute blob versioned hash
216216
c, err := kzg4844.BlobToCommitment(blob)
217217
if err != nil {
218-
return nil, common.Hash{}, nil, errors.New("failed to create blob commitment")
218+
return nil, common.Hash{}, nil, fmt.Errorf("failed to create blob commitment: %w", err)
219219
}
220220
blobVersionedHash := kzg4844.CalcBlobHashV1(sha256.New(), &c)
221221

encoding/codecv1_test.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -987,3 +987,13 @@ func TestCodecV1BatchStandardTestCases(t *testing.T) {
987987
assert.Equal(t, common.HexToHash(tc.expectedBatchHash), batch.Hash())
988988
}
989989
}
990+
991+
func TestDACodecV1SimpleMethods(t *testing.T) {
992+
codecv1, err := CodecFromVersion(CodecV1)
993+
require.NoError(t, err)
994+
995+
t.Run("Version", func(t *testing.T) {
996+
version := codecv1.Version()
997+
assert.Equal(t, CodecV1, version)
998+
})
999+
}

encoding/codecv1_types.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ func (b *daBatchV1) BlobDataProof() ([]byte, error) {
132132

133133
commitment, err := kzg4844.BlobToCommitment(b.blob)
134134
if err != nil {
135-
return nil, errors.New("failed to create blob commitment")
135+
return nil, fmt.Errorf("failed to create blob commitment: %w", err)
136136
}
137137

138138
proof, y, err := kzg4844.ComputeProof(b.blob, *b.z)
@@ -164,7 +164,7 @@ func (b *daBatchV1) BlobDataProofForPointEvaluation() ([]byte, error) {
164164

165165
commitment, err := kzg4844.BlobToCommitment(b.blob)
166166
if err != nil {
167-
return nil, errors.New("failed to create blob commitment")
167+
return nil, fmt.Errorf("failed to create blob commitment: %w", err)
168168
}
169169

170170
proof, y, err := kzg4844.ComputeProof(b.blob, *b.z)

encoding/codecv2.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,7 @@ func (d *DACodecV2) constructBlobPayload(chunks []*Chunk, maxNumChunksPerBatch i
182182
// compute blob versioned hash
183183
c, err := kzg4844.BlobToCommitment(blob)
184184
if err != nil {
185-
return nil, common.Hash{}, nil, nil, errors.New("failed to create blob commitment")
185+
return nil, common.Hash{}, nil, nil, fmt.Errorf("failed to create blob commitment: %w", err)
186186
}
187187
blobVersionedHash := kzg4844.CalcBlobHashV1(sha256.New(), &c)
188188

encoding/codecv2_test.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1028,3 +1028,13 @@ func TestCodecV2BatchStandardTestCases(t *testing.T) {
10281028
assert.Equal(t, common.HexToHash(tc.expectedBatchHash), batch.Hash())
10291029
}
10301030
}
1031+
1032+
func TestDACodecV2SimpleMethods(t *testing.T) {
1033+
codecv2, err := CodecFromVersion(CodecV2)
1034+
require.NoError(t, err)
1035+
1036+
t.Run("Version", func(t *testing.T) {
1037+
version := codecv2.Version()
1038+
assert.Equal(t, CodecV2, version)
1039+
})
1040+
}

encoding/codecv3.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -135,12 +135,12 @@ func (d *DACodecV3) EstimateBatchL1CommitGas(b *Batch) (uint64, error) {
135135
func (d *DACodecV3) JSONFromBytes(data []byte) ([]byte, error) {
136136
batch, err := d.NewDABatchFromBytes(data)
137137
if err != nil {
138-
return nil, fmt.Errorf("failed to decode DABatch from bytes: %w", err)
138+
return nil, fmt.Errorf("failed to decode DABatch from bytes, version %d, hash %s: %w", batch.Version(), batch.Hash(), err)
139139
}
140140

141141
jsonBytes, err := json.Marshal(batch)
142142
if err != nil {
143-
return nil, fmt.Errorf("failed to marshal DABatch to JSON: %w", err)
143+
return nil, fmt.Errorf("failed to marshal DABatch to JSON, version %d, hash %s: %w", batch.Version(), batch.Hash(), err)
144144
}
145145

146146
return jsonBytes, nil

encoding/codecv3_test.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1172,3 +1172,13 @@ func TestCodecV3BatchStandardTestCases(t *testing.T) {
11721172
assert.Equal(t, common.HexToHash(tc.expectedBatchHash), batch.Hash())
11731173
}
11741174
}
1175+
1176+
func TestDACodecV3SimpleMethods(t *testing.T) {
1177+
codecv3, err := CodecFromVersion(CodecV3)
1178+
require.NoError(t, err)
1179+
1180+
t.Run("Version", func(t *testing.T) {
1181+
version := codecv3.Version()
1182+
assert.Equal(t, CodecV3, version)
1183+
})
1184+
}

encoding/codecv3_types.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ func (b *daBatchV3) BlobDataProofForPointEvaluation() ([]byte, error) {
138138

139139
commitment, err := kzg4844.BlobToCommitment(b.blob)
140140
if err != nil {
141-
return nil, errors.New("failed to create blob commitment")
141+
return nil, fmt.Errorf("failed to create blob commitment: %w", err)
142142
}
143143

144144
proof, y, err := kzg4844.ComputeProof(b.blob, *b.z)

encoding/codecv4.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -228,7 +228,7 @@ func (d *DACodecV4) constructBlobPayload(chunks []*Chunk, maxNumChunksPerBatch i
228228
// compute blob versioned hash
229229
c, err := kzg4844.BlobToCommitment(blob)
230230
if err != nil {
231-
return nil, common.Hash{}, nil, nil, errors.New("failed to create blob commitment")
231+
return nil, common.Hash{}, nil, nil, fmt.Errorf("failed to create blob commitment: %w", err)
232232
}
233233
blobVersionedHash := kzg4844.CalcBlobHashV1(sha256.New(), &c)
234234

0 commit comments

Comments
 (0)