Skip to content

Commit bd220c7

Browse files
committed
Add batched commitment unit tests
1 parent 3ad472e commit bd220c7

File tree

2 files changed

+127
-1
lines changed

2 files changed

+127
-1
lines changed

op-alt-da/commitment.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -223,7 +223,8 @@ func NewBatchedCommitment(comms []CommitmentData) (BatchedCommitment, error) {
223223

224224
// DecodeBatchedCommitment validates and casts the commitment into a BatchedCommitment
225225
func DecodeBatchedCommitment(commitment []byte) (BatchedCommitment, error) {
226-
if len(commitment) == 0 {
226+
// Need at least: 1 byte for type + 2 bytes for first commitment length
227+
if len(commitment) < 3 {
227228
return nil, ErrInvalidCommitment
228229
}
229230

op-alt-da/commitment_test.go

Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ func encodeCommitmentData(commitmentType CommitmentType, data []byte) []byte {
1515
// TestCommitmentData tests the CommitmentData type and its implementations,
1616
// by encoding and decoding the commitment data and verifying the input data.
1717
func TestCommitmentData(t *testing.T) {
18+
t.Parallel()
1819

1920
type tcase struct {
2021
name string
@@ -82,3 +83,127 @@ func TestCommitmentData(t *testing.T) {
8283
}
8384
}
8485

86+
87+
func TestBatchedCommitment(t *testing.T) {
88+
t.Parallel()
89+
90+
t.Run("empty batch", func(t *testing.T) {
91+
_, err := NewBatchedCommitment(nil)
92+
require.ErrorIs(t, err, ErrInvalidCommitment)
93+
})
94+
95+
t.Run("mixed types", func(t *testing.T) {
96+
comms := []CommitmentData{
97+
NewKeccak256Commitment([]byte("data1")),
98+
NewGenericCommitment([]byte("data2")),
99+
}
100+
_, err := NewBatchedCommitment(comms)
101+
require.Error(t, err)
102+
})
103+
104+
t.Run("valid keccak batch", func(t *testing.T) {
105+
inputs := [][]byte{
106+
[]byte("data1"),
107+
[]byte("data2"),
108+
[]byte("data3"),
109+
}
110+
comms := make([]CommitmentData, len(inputs))
111+
for i, input := range inputs {
112+
comms[i] = NewKeccak256Commitment(input)
113+
}
114+
115+
// Create batch
116+
batch, err := NewBatchedCommitment(comms)
117+
require.NoError(t, err)
118+
119+
// Decode batch
120+
decoded, err := batch.GetCommitments()
121+
require.NoError(t, err)
122+
require.Equal(t, len(comms), len(decoded))
123+
124+
// Verify each commitment matches and can verify its input
125+
for i, comm := range decoded {
126+
require.Equal(t, Keccak256CommitmentType, comm.CommitmentType())
127+
require.NoError(t, comm.Verify(inputs[i]))
128+
require.Equal(t, comms[i].Encode(), comm.Encode())
129+
}
130+
})
131+
132+
t.Run("valid generic batch", func(t *testing.T) {
133+
datas := [][]byte{
134+
[]byte("generic1"),
135+
[]byte("generic2"),
136+
[]byte("generic3"),
137+
}
138+
comms := make([]CommitmentData, len(datas))
139+
for i, data := range datas {
140+
comms[i] = NewGenericCommitment(data)
141+
}
142+
143+
// Create batch
144+
batch, err := NewBatchedCommitment(comms)
145+
require.NoError(t, err)
146+
147+
// Test batch encoding/decoding
148+
decoded, err := batch.GetCommitments()
149+
require.NoError(t, err)
150+
require.Equal(t, len(comms), len(decoded))
151+
152+
// Verify each commitment matches
153+
for i, comm := range decoded {
154+
require.Equal(t, GenericCommitmentType, comm.CommitmentType())
155+
require.Equal(t, comms[i].Encode(), comm.Encode())
156+
}
157+
})
158+
159+
t.Run("malformed batch data", func(t *testing.T) {
160+
testCases := []struct {
161+
name string
162+
data []byte
163+
}{
164+
{"empty data", []byte{}},
165+
{"only type byte", []byte{byte(Keccak256CommitmentType)}},
166+
{"incomplete length", []byte{byte(Keccak256CommitmentType), 0}},
167+
{"length with no data", []byte{byte(Keccak256CommitmentType), 0, 32}},
168+
{"invalid type", []byte{255, 0, 32, 1, 2, 3}},
169+
}
170+
171+
for _, tc := range testCases {
172+
t.Run(tc.name, func(t *testing.T) {
173+
_, err := DecodeBatchedCommitment(tc.data)
174+
require.ErrorIs(t, err, ErrInvalidCommitment)
175+
})
176+
}
177+
})
178+
179+
t.Run("batch roundtrip", func(t *testing.T) {
180+
// Create a batch
181+
comms := []CommitmentData{
182+
NewKeccak256Commitment([]byte("data1")),
183+
NewKeccak256Commitment([]byte("data2")),
184+
}
185+
batch, err := NewBatchedCommitment(comms)
186+
require.NoError(t, err)
187+
188+
// Encode it
189+
encoded := batch.Encode()
190+
191+
// Decode it
192+
decoded, err := DecodeCommitmentData(encoded)
193+
require.NoError(t, err)
194+
195+
// Verify it's a batched commitment
196+
batchComm, ok := decoded.(BatchedCommitment)
197+
require.True(t, ok)
198+
199+
// Get the individual commitments
200+
decodedComms, err := batchComm.GetCommitments()
201+
require.NoError(t, err)
202+
203+
// Verify they match the original
204+
require.Equal(t, len(comms), len(decodedComms))
205+
for i := range comms {
206+
require.Equal(t, comms[i].Encode(), decodedComms[i].Encode())
207+
}
208+
})
209+
}

0 commit comments

Comments
 (0)