Skip to content

Commit

Permalink
add JSONFromBytes unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
colinlyguo committed Oct 18, 2024
1 parent 42af292 commit e6cfa7d
Show file tree
Hide file tree
Showing 4 changed files with 117 additions and 1 deletion.
2 changes: 1 addition & 1 deletion encoding/codecv3.go
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ 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, version %d, hash %s: %w", batch.Version(), batch.Hash(), err)
return nil, fmt.Errorf("failed to decode DABatch from bytes: %w", err)
}

jsonBytes, err := json.Marshal(batch)
Expand Down
50 changes: 50 additions & 0 deletions encoding/codecv3_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"crypto/rand"
"encoding/hex"
"encoding/json"
"fmt"
"math"
"strings"
"testing"
Expand Down Expand Up @@ -582,6 +583,55 @@ func TestCodecV3DABatchJSONMarshalUnmarshal(t *testing.T) {
})
}

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

daBatch := daBatchV3{
daBatchV0: daBatchV0{
version: 3,
batchIndex: 293212,
l1MessagePopped: 7,
totalL1MessagePopped: 904750,
dataHash: common.HexToHash("0xa261ff31f8f78c19f65d14d6394eb911d53a3a3add9a9691b211caa5809be450"),
parentBatchHash: common.HexToHash("0xc37d3f6881f0ca6b02b1dc071483e02d0fe88cf2ff3663bb1ba9aa0dc034faee"),
},
blobVersionedHash: common.HexToHash("0x0120096572a3007f75c2a3ff82fa652976eae1c9428ec87ec258a8dcc84f488e"),
lastBlockTimestamp: 1721130505,
blobDataProof: [2]common.Hash{
common.HexToHash("0x496b144866cffedfd71423639984bf0d9ad4309ff7e35693f1baef3cdaf1471e"),
common.HexToHash("0x5eba7d42db109bfa124d1bc4dbcb421944b8aae6eae13a9d55eb460ce402785b"),
},
}

outputJSON, err := codecv3.JSONFromBytes(daBatch.Encode())
require.NoError(t, err, "JSONFromBytes failed")

var outputMap map[string]interface{}
err = json.Unmarshal(outputJSON, &outputMap)
require.NoError(t, err, "Failed to unmarshal output JSON")

expectedFields := map[string]interface{}{
"version": float64(daBatch.version),
"batch_index": float64(daBatch.batchIndex),
"l1_message_popped": float64(daBatch.l1MessagePopped),
"total_l1_message_popped": float64(daBatch.totalL1MessagePopped),
"data_hash": daBatch.dataHash.Hex(),
"blob_versioned_hash": daBatch.blobVersionedHash.Hex(),
"parent_batch_hash": daBatch.parentBatchHash.Hex(),
"last_block_timestamp": float64(daBatch.lastBlockTimestamp),
"blob_data_proof": []interface{}{
daBatch.blobDataProof[0].Hex(),
daBatch.blobDataProof[1].Hex(),
},
}

assert.Len(t, outputMap, len(expectedFields), "Unexpected number of fields in output")
for key, expectedValue := range expectedFields {
assert.Equal(t, expectedValue, outputMap[key], fmt.Sprintf("Mismatch in field %s", key))
}
}

func TestCodecV3CalldataSizeEstimation(t *testing.T) {
codecv3, err := CodecFromVersion(CodecV3)
require.NoError(t, err)
Expand Down
16 changes: 16 additions & 0 deletions encoding/codecv4.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"crypto/sha256"
"encoding/binary"
"encoding/hex"
"encoding/json"
"errors"
"fmt"
"math/big"
Expand Down Expand Up @@ -308,3 +309,18 @@ func (d *DACodecV4) CheckChunkCompressedDataCompatibility(c *Chunk) (bool, error
func (d *DACodecV4) CheckBatchCompressedDataCompatibility(b *Batch) (bool, error) {
return d.checkCompressedDataCompatibility(b.Chunks)
}

// JSONFromBytes converts the bytes to a daBatchV3 and then marshals it to JSON.
func (d *DACodecV4) JSONFromBytes(data []byte) ([]byte, error) {
batch, err := d.NewDABatchFromBytes(data) // this is different from the V3 implementation
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
}
50 changes: 50 additions & 0 deletions encoding/codecv4_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package encoding
import (
"encoding/hex"
"encoding/json"
"fmt"
"math"
"strings"
"testing"
Expand Down Expand Up @@ -581,6 +582,55 @@ func TestCodecV4DABatchJSONMarshalUnmarshal(t *testing.T) {
})
}

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

daBatch := daBatchV3{
daBatchV0: daBatchV0{
version: 4,
batchIndex: 293212,
l1MessagePopped: 7,
totalL1MessagePopped: 904750,
dataHash: common.HexToHash("0xa261ff31f8f78c19f65d14d6394eb911d53a3a3add9a9691b211caa5809be450"),
parentBatchHash: common.HexToHash("0xc37d3f6881f0ca6b02b1dc071483e02d0fe88cf2ff3663bb1ba9aa0dc034faee"),
},
blobVersionedHash: common.HexToHash("0x0120096572a3007f75c2a3ff82fa652976eae1c9428ec87ec258a8dcc84f488e"),
lastBlockTimestamp: 1721130505,
blobDataProof: [2]common.Hash{
common.HexToHash("0x496b144866cffedfd71423639984bf0d9ad4309ff7e35693f1baef3cdaf1471e"),
common.HexToHash("0x5eba7d42db109bfa124d1bc4dbcb421944b8aae6eae13a9d55eb460ce402785b"),
},
}

outputJSON, err := codecv4.JSONFromBytes(daBatch.Encode())
require.NoError(t, err, "JSONFromBytes failed")

var outputMap map[string]interface{}
err = json.Unmarshal(outputJSON, &outputMap)
require.NoError(t, err, "Failed to unmarshal output JSON")

expectedFields := map[string]interface{}{
"version": float64(daBatch.version),
"batch_index": float64(daBatch.batchIndex),
"l1_message_popped": float64(daBatch.l1MessagePopped),
"total_l1_message_popped": float64(daBatch.totalL1MessagePopped),
"data_hash": daBatch.dataHash.Hex(),
"blob_versioned_hash": daBatch.blobVersionedHash.Hex(),
"parent_batch_hash": daBatch.parentBatchHash.Hex(),
"last_block_timestamp": float64(daBatch.lastBlockTimestamp),
"blob_data_proof": []interface{}{
daBatch.blobDataProof[0].Hex(),
daBatch.blobDataProof[1].Hex(),
},
}

assert.Len(t, outputMap, len(expectedFields), "Unexpected number of fields in output")
for key, expectedValue := range expectedFields {
assert.Equal(t, expectedValue, outputMap[key], fmt.Sprintf("Mismatch in field %s", key))
}
}

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

0 comments on commit e6cfa7d

Please sign in to comment.