Skip to content

Commit 4b5b636

Browse files
author
colinlyguo
committed
add interface unit tests
1 parent 24eb594 commit 4b5b636

2 files changed

Lines changed: 117 additions & 22 deletions

File tree

encoding/codecv1_types.go

Lines changed: 0 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -121,28 +121,6 @@ func (b *daBatchV1) Hash() common.Hash {
121121
return crypto.Keccak256Hash(bytes)
122122
}
123123

124-
// BlobDataProof computes the abi-encoded blob verification data.
125-
func (b *daBatchV1) BlobDataProof() ([]byte, error) {
126-
if b.blob == nil {
127-
return nil, errors.New("called BlobDataProof with empty blob")
128-
}
129-
if b.z == nil {
130-
return nil, errors.New("called BlobDataProof with empty z")
131-
}
132-
133-
commitment, err := kzg4844.BlobToCommitment(b.blob)
134-
if err != nil {
135-
return nil, fmt.Errorf("failed to create blob commitment: %w", err)
136-
}
137-
138-
proof, y, err := kzg4844.ComputeProof(b.blob, *b.z)
139-
if err != nil {
140-
return nil, fmt.Errorf("failed to create KZG proof at point, err: %w, z: %v", err, hex.EncodeToString(b.z[:]))
141-
}
142-
143-
return blobDataProofFromValues(*b.z, y, commitment, proof), nil
144-
}
145-
146124
// Blob returns the blob of the batch.
147125
func (b *daBatchV1) Blob() *kzg4844.Blob {
148126
return b.blob

encoding/interfaces_test.go

Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
package encoding
2+
3+
import (
4+
"math/big"
5+
"testing"
6+
7+
"github.com/scroll-tech/go-ethereum/params"
8+
"github.com/stretchr/testify/assert"
9+
)
10+
11+
func TestCodecFromVersion(t *testing.T) {
12+
tests := []struct {
13+
name string
14+
version CodecVersion
15+
want Codec
16+
wantErr bool
17+
}{
18+
{"CodecV0", CodecV0, &DACodecV0{}, false},
19+
{"CodecV1", CodecV1, &DACodecV1{}, false},
20+
{"CodecV2", CodecV2, &DACodecV2{}, false},
21+
{"CodecV3", CodecV3, &DACodecV3{}, false},
22+
{"CodecV4", CodecV4, &DACodecV4{}, false},
23+
{"InvalidCodec", CodecVersion(99), nil, true},
24+
}
25+
26+
for _, tt := range tests {
27+
t.Run(tt.name, func(t *testing.T) {
28+
got, err := CodecFromVersion(tt.version)
29+
if tt.wantErr {
30+
assert.Error(t, err)
31+
} else {
32+
assert.NoError(t, err)
33+
assert.IsType(t, tt.want, got)
34+
}
35+
})
36+
}
37+
}
38+
39+
func TestCodecFromConfig(t *testing.T) {
40+
tests := []struct {
41+
name string
42+
config *params.ChainConfig
43+
blockNum *big.Int
44+
timestamp uint64
45+
want Codec
46+
}{
47+
{
48+
name: "DarwinV2 active",
49+
config: &params.ChainConfig{
50+
LondonBlock: big.NewInt(0),
51+
BernoulliBlock: big.NewInt(0),
52+
CurieBlock: big.NewInt(0),
53+
DarwinTime: new(uint64),
54+
DarwinV2Time: new(uint64),
55+
},
56+
blockNum: big.NewInt(0),
57+
timestamp: 0,
58+
want: &DACodecV4{},
59+
},
60+
{
61+
name: "Darwin active",
62+
config: &params.ChainConfig{
63+
LondonBlock: big.NewInt(0),
64+
BernoulliBlock: big.NewInt(0),
65+
CurieBlock: big.NewInt(0),
66+
DarwinTime: new(uint64),
67+
},
68+
blockNum: big.NewInt(0),
69+
timestamp: 0,
70+
want: &DACodecV3{},
71+
},
72+
{
73+
name: "Curie active",
74+
config: &params.ChainConfig{
75+
LondonBlock: big.NewInt(0),
76+
BernoulliBlock: big.NewInt(0),
77+
CurieBlock: big.NewInt(0),
78+
},
79+
blockNum: big.NewInt(0),
80+
timestamp: 0,
81+
want: &DACodecV2{},
82+
},
83+
{
84+
name: "Bernoulli active",
85+
config: &params.ChainConfig{
86+
LondonBlock: big.NewInt(0),
87+
BernoulliBlock: big.NewInt(0),
88+
},
89+
blockNum: big.NewInt(0),
90+
timestamp: 0,
91+
want: &DACodecV1{},
92+
},
93+
{
94+
name: "London active",
95+
config: &params.ChainConfig{
96+
LondonBlock: big.NewInt(0),
97+
},
98+
blockNum: big.NewInt(0),
99+
timestamp: 0,
100+
want: &DACodecV0{},
101+
},
102+
{
103+
name: "No upgrades",
104+
config: &params.ChainConfig{},
105+
blockNum: big.NewInt(0),
106+
timestamp: 0,
107+
want: &DACodecV0{},
108+
},
109+
}
110+
111+
for _, tt := range tests {
112+
t.Run(tt.name, func(t *testing.T) {
113+
got := CodecFromConfig(tt.config, tt.blockNum, tt.timestamp)
114+
assert.IsType(t, tt.want, got)
115+
})
116+
}
117+
}

0 commit comments

Comments
 (0)