1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
package sequencer_test
import (
"bytes"
"encoding/hex"
"encoding/json"
"os"
"testing"
"github.com/ethereum-optimism/optimism/batch-submitter/drivers/sequencer"
l2types "github.com/ethereum-optimism/optimism/l2geth/core/types"
l2rlp "github.com/ethereum-optimism/optimism/l2geth/rlp"
"github.com/stretchr/testify/require"
)
// TestBatchContextEncodeDecode tests the (de)serialization of a BatchContext
// against the spec test vector. The encoding should be:
// - num_sequenced_txs: 3 bytes
// - num_subsequent_queue_txs: 3 bytes
// - timestamp: 5 bytes
// - block_number: 5 bytes
func TestBatchContextEncodeDecode(t *testing.T) {
t.Parallel()
// Test vector is chosen such that each byte maps one to one with a
// specific byte of the parsed BatchContext and such that improper
// choice of endian-ness for any field will fail.
hexEncoding := "000102030405060708090a0b0c0d0e0f"
expBatch := sequencer.BatchContext{
NumSequencedTxs: 0x000102,
NumSubsequentQueueTxs: 0x030405,
Timestamp: 0x060708090a,
BlockNumber: 0x0b0c0d0e0f,
}
rawBytes, err := hex.DecodeString(hexEncoding)
require.Nil(t, err)
// Test Read produces expected batch.
var batch sequencer.BatchContext
err = batch.Read(bytes.NewReader(rawBytes))
require.Nil(t, err)
require.Equal(t, expBatch, batch)
// Test Write produces original test vector.
var buf bytes.Buffer
batch.Write(&buf)
require.Equal(t, hexEncoding, hex.EncodeToString(buf.Bytes()))
}
// AppendSequencerBatchParamsTestCases is an enclosing struct that holds the
// individual AppendSequencerBatchParamsTests. This is the root-level object
// that will be parsed from the JSON, spec test-vectors.
type AppendSequencerBatchParamsTestCases struct {
Tests []AppendSequencerBatchParamsTest `json:"tests"`
}
// AppendSequencerBatchParamsTest specifies a single instance of a valid
// encode/decode test case for an AppendequencerBatchParams.
type AppendSequencerBatchParamsTest struct {
Name string `json:"name"`
HexEncoding string `json:"hex_encoding"`
ShouldStartAtElement uint64 `json:"should_start_at_element"`
TotalElementsToAppend uint64 `json:"total_elements_to_append"`
Contexts []sequencer.BatchContext `json:"contexts"`
Txs []string `json:"txs"`
Error bool `json:"error"`
}
var appendSequencerBatchParamTests = AppendSequencerBatchParamsTestCases{}
func init() {
data, err := os.ReadFile("./testdata/valid_append_sequencer_batch_params.json")
if err != nil {
panic(err)
}
err = json.Unmarshal(data, &appendSequencerBatchParamTests)
if err != nil {
panic(err)
}
}
// TestAppendSequencerBatchParamsEncodeDecode asserts the proper encoding and
// decoding of valid serializations for AppendSequencerBatchParams.
func TestAppendSequencerBatchParamsEncodeDecode(t *testing.T) {
t.Parallel()
for _, test := range appendSequencerBatchParamTests.Tests {
t.Run(test.Name, func(t *testing.T) {
testAppendSequencerBatchParamsEncodeDecode(t, test)
})
}
}
func testAppendSequencerBatchParamsEncodeDecode(
t *testing.T, test AppendSequencerBatchParamsTest) {
// Decode the expected transactions from their hex serialization.
var expTxs []*l2types.Transaction
for _, txHex := range test.Txs {
txBytes, err := hex.DecodeString(txHex)
require.Nil(t, err)
rlpStream := l2rlp.NewStream(bytes.NewReader(txBytes), uint64(len(txBytes)))
tx := new(l2types.Transaction)
err = tx.DecodeRLP(rlpStream)
require.Nil(t, err)
expTxs = append(expTxs, tx)
}
// Construct the params we expect to decode, minus the txs. Those are
// compared separately below.
expParams := sequencer.AppendSequencerBatchParams{
ShouldStartAtElement: test.ShouldStartAtElement,
TotalElementsToAppend: test.TotalElementsToAppend,
Contexts: test.Contexts,
Txs: nil,
}
// Decode the batch from the test string.
rawBytes, err := hex.DecodeString(test.HexEncoding)
require.Nil(t, err)
var params sequencer.AppendSequencerBatchParams
err = params.Read(bytes.NewReader(rawBytes))
if test.Error {
require.ErrorIs(t, err, sequencer.ErrMalformedBatch)
} else {
require.Nil(t, err)
}
// Assert that the decoded params match the expected params. The
// transactions are compared serparetly (via hash), since the internal
// `time` field of each transaction will differ. This field is only used
// for spam prevention, so it is safe to ignore wrt. to serialization.
// The decoded txs are reset on the the decoded params afterwards to
// test the serialization.
decodedTxs := params.Txs
params.Txs = nil
require.Equal(t, expParams, params)
compareTxs(t, expTxs, decodedTxs)
params.Txs = decodedTxs
// Finally, encode the decoded object and assert it matches the original
// hex string.
paramsBytes, err := params.Serialize(sequencer.BatchTypeLegacy)
// Return early when testing error cases, no need to reserialize again
if test.Error {
require.ErrorIs(t, err, sequencer.ErrMalformedBatch)
return
}
require.Nil(t, err)
require.Equal(t, test.HexEncoding, hex.EncodeToString(paramsBytes))
// Serialize the batches in compressed form
compressedParamsBytes, err := params.Serialize(sequencer.BatchTypeZlib)
require.Nil(t, err)
// Deserialize the compressed batch
var paramsCompressed sequencer.AppendSequencerBatchParams
err = paramsCompressed.Read(bytes.NewReader(compressedParamsBytes))
require.Nil(t, err)
decompressedTxs := paramsCompressed.Txs
paramsCompressed.Txs = nil
require.Equal(t, expParams, paramsCompressed)
compareTxs(t, expTxs, decompressedTxs)
paramsCompressed.Txs = decompressedTxs
}
// compareTxs compares a list of two transactions, testing each pair by tx hash.
// This is used rather than require.Equal, since there `time` metadata on the
// decoded tx and the expected tx will differ, and can't be modified/ignored.
func compareTxs(t *testing.T, a []*l2types.Transaction, b []*sequencer.CachedTx) {
require.Equal(t, len(a), len(b))
for i, txA := range a {
require.Equal(t, txA.Hash(), b[i].Tx().Hash())
}
}
// TestMarkerContext asserts that each batch type returns the correct marker
// context.
func TestMarkerContext(t *testing.T) {
batchTypes := []sequencer.BatchType{
sequencer.BatchTypeLegacy,
sequencer.BatchTypeZlib,
}
for _, batchType := range batchTypes {
t.Run(batchType.String(), func(t *testing.T) {
markerContext := batchType.MarkerContext()
if batchType == sequencer.BatchTypeLegacy {
require.Nil(t, markerContext)
} else {
require.NotNil(t, markerContext)
// All marker contexts MUST have a zero timestamp.
require.Equal(t, uint64(0), markerContext.Timestamp)
// Currently all other fields besides block number are defined
// as zero.
require.Equal(t, uint64(0), markerContext.NumSequencedTxs)
require.Equal(t, uint64(0), markerContext.NumSubsequentQueueTxs)
// Assert that the block number for each batch type is set to
// the correct constant.
switch batchType {
case sequencer.BatchTypeZlib:
require.Equal(t, uint64(0), markerContext.BlockNumber)
default:
t.Fatalf("unknown batch type")
}
// Ensure MarkerBatchType produces the expected BatchType.
require.Equal(t, batchType, markerContext.MarkerBatchType())
}
})
}
}
// TestIsMarkerContext asserts that IsMarkerContext returns true iff the
// timestamp is zero.
func TestIsMarkerContext(t *testing.T) {
batchContext := sequencer.BatchContext{
NumSequencedTxs: 1,
NumSubsequentQueueTxs: 2,
Timestamp: 3,
BlockNumber: 4,
}
require.False(t, batchContext.IsMarkerContext())
batchContext = sequencer.BatchContext{
NumSequencedTxs: 0,
NumSubsequentQueueTxs: 0,
Timestamp: 3,
BlockNumber: 0,
}
require.False(t, batchContext.IsMarkerContext())
batchContext = sequencer.BatchContext{
NumSequencedTxs: 1,
NumSubsequentQueueTxs: 2,
Timestamp: 0,
BlockNumber: 4,
}
require.True(t, batchContext.IsMarkerContext())
}