Skip to content

Commit

Permalink
add simple and nil functions unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
colinlyguo committed Oct 18, 2024
1 parent 3faa778 commit 1e07a66
Show file tree
Hide file tree
Showing 11 changed files with 103 additions and 8 deletions.
55 changes: 55 additions & 0 deletions encoding/codecv0_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (

"github.com/scroll-tech/go-ethereum/common"
"github.com/scroll-tech/go-ethereum/core/types"
"github.com/scroll-tech/go-ethereum/crypto/kzg4844"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
Expand Down Expand Up @@ -618,3 +619,57 @@ func TestCodecV0DecodeDAChunksRawTx(t *testing.T) {
assert.Equal(t, 1, len(daChunksRawTx[1].Transactions[0]))
assert.Equal(t, 0, len(daChunksRawTx[1].Transactions[1]))
}

func TestDACodecV0SimpleMethods(t *testing.T) {
codecv0, err := CodecFromVersion(CodecV0)
require.NoError(t, err)

t.Run("Version", func(t *testing.T) {
version := codecv0.Version()
assert.Equal(t, CodecV0, version)
})

t.Run("CheckChunkCompressedDataCompatibility", func(t *testing.T) {
chunk := &Chunk{}
compatible, err := codecv0.CheckChunkCompressedDataCompatibility(chunk)
assert.NoError(t, err)
assert.True(t, compatible)
})

t.Run("CheckBatchCompressedDataCompatibility", func(t *testing.T) {
batch := &Batch{}
compatible, err := codecv0.CheckBatchCompressedDataCompatibility(batch)
assert.NoError(t, err)
assert.True(t, compatible)
})

t.Run("EstimateChunkL1CommitBatchSizeAndBlobSize", func(t *testing.T) {
chunk := &Chunk{}
batchSize, blobSize, err := codecv0.EstimateChunkL1CommitBatchSizeAndBlobSize(chunk)
assert.NoError(t, err)
assert.Equal(t, uint64(0), batchSize)
assert.Equal(t, uint64(0), blobSize)
})

t.Run("EstimateBatchL1CommitBatchSizeAndBlobSize", func(t *testing.T) {
batch := &Batch{}
batchSize, blobSize, err := codecv0.EstimateBatchL1CommitBatchSizeAndBlobSize(batch)
assert.NoError(t, err)
assert.Equal(t, uint64(0), batchSize)
assert.Equal(t, uint64(0), blobSize)
})

t.Run("JSONFromBytes", func(t *testing.T) {
data := []byte("test data")
json, err := codecv0.JSONFromBytes(data)
assert.NoError(t, err)
assert.Nil(t, json)
})

t.Run("DecodeTxsFromBlob", func(t *testing.T) {
blob := &kzg4844.Blob{}
chunks := []*DAChunkRawTx{}
err := codecv0.DecodeTxsFromBlob(blob, chunks)
assert.NoError(t, err)
})
}
2 changes: 1 addition & 1 deletion encoding/codecv1.go
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,7 @@ func (d *DACodecV1) constructBlobPayload(chunks []*Chunk, maxNumChunksPerBatch i
// compute blob versioned hash
c, err := kzg4844.BlobToCommitment(blob)
if err != nil {
return nil, common.Hash{}, nil, errors.New("failed to create blob commitment")
return nil, common.Hash{}, nil, fmt.Errorf("failed to create blob commitment: %w", err)
}
blobVersionedHash := kzg4844.CalcBlobHashV1(sha256.New(), &c)

Expand Down
10 changes: 10 additions & 0 deletions encoding/codecv1_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -987,3 +987,13 @@ func TestCodecV1BatchStandardTestCases(t *testing.T) {
assert.Equal(t, common.HexToHash(tc.expectedBatchHash), batch.Hash())
}
}

func TestDACodecV1SimpleMethods(t *testing.T) {
codecv1, err := CodecFromVersion(CodecV1)
require.NoError(t, err)

t.Run("Version", func(t *testing.T) {
version := codecv1.Version()
assert.Equal(t, CodecV1, version)
})
}
4 changes: 2 additions & 2 deletions encoding/codecv1_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ func (b *daBatchV1) BlobDataProof() ([]byte, error) {

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

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

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

proof, y, err := kzg4844.ComputeProof(b.blob, *b.z)
Expand Down
2 changes: 1 addition & 1 deletion encoding/codecv2.go
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ func (d *DACodecV2) constructBlobPayload(chunks []*Chunk, maxNumChunksPerBatch i
// compute blob versioned hash
c, err := kzg4844.BlobToCommitment(blob)
if err != nil {
return nil, common.Hash{}, nil, nil, errors.New("failed to create blob commitment")
return nil, common.Hash{}, nil, nil, fmt.Errorf("failed to create blob commitment: %w", err)
}
blobVersionedHash := kzg4844.CalcBlobHashV1(sha256.New(), &c)

Expand Down
10 changes: 10 additions & 0 deletions encoding/codecv2_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1028,3 +1028,13 @@ func TestCodecV2BatchStandardTestCases(t *testing.T) {
assert.Equal(t, common.HexToHash(tc.expectedBatchHash), batch.Hash())
}
}

func TestDACodecV2SimpleMethods(t *testing.T) {
codecv2, err := CodecFromVersion(CodecV2)
require.NoError(t, err)

t.Run("Version", func(t *testing.T) {
version := codecv2.Version()
assert.Equal(t, CodecV2, version)
})
}
4 changes: 2 additions & 2 deletions encoding/codecv3.go
Original file line number Diff line number Diff line change
Expand Up @@ -135,12 +135,12 @@ func (d *DACodecV3) EstimateBatchL1CommitGas(b *Batch) (uint64, error) {
func (d *DACodecV3) JSONFromBytes(data []byte) ([]byte, error) {
batch, err := d.NewDABatchFromBytes(data)
if err != nil {
return nil, fmt.Errorf("failed to decode DABatch from bytes: %w", err)
return nil, fmt.Errorf("failed to decode DABatch from bytes, version %d, hash %s: %w", batch.Version(), batch.Hash(), err)
}

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

return jsonBytes, nil
Expand Down
10 changes: 10 additions & 0 deletions encoding/codecv3_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1172,3 +1172,13 @@ func TestCodecV3BatchStandardTestCases(t *testing.T) {
assert.Equal(t, common.HexToHash(tc.expectedBatchHash), batch.Hash())
}
}

func TestDACodecV3SimpleMethods(t *testing.T) {
codecv3, err := CodecFromVersion(CodecV3)
require.NoError(t, err)

t.Run("Version", func(t *testing.T) {
version := codecv3.Version()
assert.Equal(t, CodecV3, version)
})
}
2 changes: 1 addition & 1 deletion encoding/codecv3_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ func (b *daBatchV3) BlobDataProofForPointEvaluation() ([]byte, error) {

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

proof, y, err := kzg4844.ComputeProof(b.blob, *b.z)
Expand Down
2 changes: 1 addition & 1 deletion encoding/codecv4.go
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,7 @@ func (d *DACodecV4) constructBlobPayload(chunks []*Chunk, maxNumChunksPerBatch i
// compute blob versioned hash
c, err := kzg4844.BlobToCommitment(blob)
if err != nil {
return nil, common.Hash{}, nil, nil, errors.New("failed to create blob commitment")
return nil, common.Hash{}, nil, nil, fmt.Errorf("failed to create blob commitment: %w", err)
}
blobVersionedHash := kzg4844.CalcBlobHashV1(sha256.New(), &c)

Expand Down
10 changes: 10 additions & 0 deletions encoding/codecv4_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1320,3 +1320,13 @@ func TestCodecV4BatchStandardTestCasesDisableCompression(t *testing.T) {
assert.Equal(t, common.HexToHash(tc.expectedBatchHash), batch.Hash())
}
}

func TestDACodecV4SimpleMethods(t *testing.T) {
codecv4, err := CodecFromVersion(CodecV4)
require.NoError(t, err)

t.Run("Version", func(t *testing.T) {
version := codecv4.Version()
assert.Equal(t, CodecV4, version)
})
}

0 comments on commit 1e07a66

Please sign in to comment.