Commit 141a480f authored by Conner Fromknecht's avatar Conner Fromknecht

feat: pass BatchType as argument to Write

parent ec3857eb
...@@ -78,7 +78,6 @@ func GenSequencerBatchParams( ...@@ -78,7 +78,6 @@ func GenSequencerBatchParams(
shouldStartAtElement uint64, shouldStartAtElement uint64,
blockOffset uint64, blockOffset uint64,
batch []BatchElement, batch []BatchElement,
batchType BatchType,
) (*AppendSequencerBatchParams, error) { ) (*AppendSequencerBatchParams, error) {
var ( var (
...@@ -189,6 +188,5 @@ func GenSequencerBatchParams( ...@@ -189,6 +188,5 @@ func GenSequencerBatchParams(
TotalElementsToAppend: uint64(len(batch)), TotalElementsToAppend: uint64(len(batch)),
Contexts: contexts, Contexts: contexts,
Txs: txs, Txs: txs,
Type: batchType,
}, nil }, nil
} }
...@@ -199,13 +199,13 @@ func (d *Driver) CraftBatchTx( ...@@ -199,13 +199,13 @@ func (d *Driver) CraftBatchTx(
var pruneCount int var pruneCount int
for { for {
batchParams, err := GenSequencerBatchParams( batchParams, err := GenSequencerBatchParams(
shouldStartAt, d.cfg.BlockOffset, batchElements, d.cfg.BatchType, shouldStartAt, d.cfg.BlockOffset, batchElements,
) )
if err != nil { if err != nil {
return nil, err return nil, err
} }
batchArguments, err := batchParams.Serialize() batchArguments, err := batchParams.Serialize(d.cfg.BatchType)
if err != nil { if err != nil {
return nil, err return nil, err
} }
......
...@@ -146,9 +146,6 @@ type AppendSequencerBatchParams struct { ...@@ -146,9 +146,6 @@ type AppendSequencerBatchParams struct {
// Txs contains all sequencer txs that will be recorded in the L1 CTC // Txs contains all sequencer txs that will be recorded in the L1 CTC
// contract. // contract.
Txs []*CachedTx Txs []*CachedTx
// The type of the batch
Type BatchType
} }
// Write encodes the AppendSequencerBatchParams using the following format: // Write encodes the AppendSequencerBatchParams using the following format:
...@@ -173,7 +170,11 @@ type AppendSequencerBatchParams struct { ...@@ -173,7 +170,11 @@ type AppendSequencerBatchParams struct {
// //
// Note that writing to a bytes.Buffer cannot // Note that writing to a bytes.Buffer cannot
// error, so errors are ignored here // error, so errors are ignored here
func (p *AppendSequencerBatchParams) Write(w *bytes.Buffer) error { func (p *AppendSequencerBatchParams) Write(
w *bytes.Buffer,
batchType BatchType,
) error {
_ = writeUint64(w, p.ShouldStartAtElement, 5) _ = writeUint64(w, p.ShouldStartAtElement, 5)
_ = writeUint64(w, p.TotalElementsToAppend, 3) _ = writeUint64(w, p.TotalElementsToAppend, 3)
...@@ -190,7 +191,7 @@ func (p *AppendSequencerBatchParams) Write(w *bytes.Buffer) error { ...@@ -190,7 +191,7 @@ func (p *AppendSequencerBatchParams) Write(w *bytes.Buffer) error {
// copy the contexts as to not malleate the struct // copy the contexts as to not malleate the struct
// when it is a typed batch // when it is a typed batch
contexts := make([]BatchContext, 0, len(p.Contexts)+1) contexts := make([]BatchContext, 0, len(p.Contexts)+1)
if p.Type == BatchTypeZlib { if batchType == BatchTypeZlib {
// All zero values for the single batch context // All zero values for the single batch context
// is desired here as blocknumber 0 means it is a zlib batch // is desired here as blocknumber 0 means it is a zlib batch
contexts = append(contexts, BatchContext{}) contexts = append(contexts, BatchContext{})
...@@ -203,7 +204,7 @@ func (p *AppendSequencerBatchParams) Write(w *bytes.Buffer) error { ...@@ -203,7 +204,7 @@ func (p *AppendSequencerBatchParams) Write(w *bytes.Buffer) error {
context.Write(w) context.Write(w)
} }
switch p.Type { switch batchType {
case BatchTypeLegacy: case BatchTypeLegacy:
// Write each length-prefixed tx. // Write each length-prefixed tx.
for _, tx := range p.Txs { for _, tx := range p.Txs {
...@@ -225,7 +226,7 @@ func (p *AppendSequencerBatchParams) Write(w *bytes.Buffer) error { ...@@ -225,7 +226,7 @@ func (p *AppendSequencerBatchParams) Write(w *bytes.Buffer) error {
} }
default: default:
return fmt.Errorf("Unknown batch type: %s", p.Type) return fmt.Errorf("Unknown batch type: %s", batchType)
} }
return nil return nil
...@@ -233,9 +234,12 @@ func (p *AppendSequencerBatchParams) Write(w *bytes.Buffer) error { ...@@ -233,9 +234,12 @@ func (p *AppendSequencerBatchParams) Write(w *bytes.Buffer) error {
// Serialize performs the same encoding as Write, but returns the resulting // Serialize performs the same encoding as Write, but returns the resulting
// bytes slice. // bytes slice.
func (p *AppendSequencerBatchParams) Serialize() ([]byte, error) { func (p *AppendSequencerBatchParams) Serialize(
batchType BatchType,
) ([]byte, error) {
var buf bytes.Buffer var buf bytes.Buffer
if err := p.Write(&buf); err != nil { if err := p.Write(&buf, batchType); err != nil {
return nil, err return nil, err
} }
return buf.Bytes(), nil return buf.Bytes(), nil
...@@ -277,15 +281,10 @@ func (p *AppendSequencerBatchParams) Read(r io.Reader) error { ...@@ -277,15 +281,10 @@ func (p *AppendSequencerBatchParams) Read(r io.Reader) error {
p.Contexts = append(p.Contexts, batchContext) p.Contexts = append(p.Contexts, batchContext)
} }
// Assume that it is a legacy batch at first
p.Type = BatchTypeLegacy
// Handle backwards compatible batch types // Handle backwards compatible batch types
if len(p.Contexts) > 0 && p.Contexts[0].Timestamp == 0 { if len(p.Contexts) > 0 && p.Contexts[0].Timestamp == 0 {
switch p.Contexts[0].BlockNumber { switch p.Contexts[0].BlockNumber {
case 0: case 0:
// zlib compressed transaction data
p.Type = BatchTypeZlib
// remove the first dummy context // remove the first dummy context
p.Contexts = p.Contexts[1:] p.Contexts = p.Contexts[1:]
numContexts-- numContexts--
......
...@@ -119,7 +119,6 @@ func testAppendSequencerBatchParamsEncodeDecode( ...@@ -119,7 +119,6 @@ func testAppendSequencerBatchParamsEncodeDecode(
TotalElementsToAppend: test.TotalElementsToAppend, TotalElementsToAppend: test.TotalElementsToAppend,
Contexts: test.Contexts, Contexts: test.Contexts,
Txs: nil, Txs: nil,
Type: sequencer.BatchTypeLegacy,
} }
// Decode the batch from the test string. // Decode the batch from the test string.
...@@ -133,7 +132,6 @@ func testAppendSequencerBatchParamsEncodeDecode( ...@@ -133,7 +132,6 @@ func testAppendSequencerBatchParamsEncodeDecode(
} else { } else {
require.Nil(t, err) require.Nil(t, err)
} }
require.Equal(t, params.Type, sequencer.BatchTypeLegacy)
// Assert that the decoded params match the expected params. The // Assert that the decoded params match the expected params. The
// transactions are compared serparetly (via hash), since the internal // transactions are compared serparetly (via hash), since the internal
...@@ -149,7 +147,7 @@ func testAppendSequencerBatchParamsEncodeDecode( ...@@ -149,7 +147,7 @@ func testAppendSequencerBatchParamsEncodeDecode(
// Finally, encode the decoded object and assert it matches the original // Finally, encode the decoded object and assert it matches the original
// hex string. // hex string.
paramsBytes, err := params.Serialize() paramsBytes, err := params.Serialize(sequencer.BatchTypeLegacy)
// Return early when testing error cases, no need to reserialize again // Return early when testing error cases, no need to reserialize again
if test.Error { if test.Error {
...@@ -161,17 +159,14 @@ func testAppendSequencerBatchParamsEncodeDecode( ...@@ -161,17 +159,14 @@ func testAppendSequencerBatchParamsEncodeDecode(
require.Equal(t, test.HexEncoding, hex.EncodeToString(paramsBytes)) require.Equal(t, test.HexEncoding, hex.EncodeToString(paramsBytes))
// Serialize the batches in compressed form // Serialize the batches in compressed form
params.Type = sequencer.BatchTypeZlib compressedParamsBytes, err := params.Serialize(sequencer.BatchTypeZlib)
compressedParamsBytes, err := params.Serialize()
require.Nil(t, err) require.Nil(t, err)
// Deserialize the compressed batch // Deserialize the compressed batch
var paramsCompressed sequencer.AppendSequencerBatchParams var paramsCompressed sequencer.AppendSequencerBatchParams
err = paramsCompressed.Read(bytes.NewReader(compressedParamsBytes)) err = paramsCompressed.Read(bytes.NewReader(compressedParamsBytes))
require.Nil(t, err) require.Nil(t, err)
require.Equal(t, paramsCompressed.Type, sequencer.BatchTypeZlib)
expParams.Type = sequencer.BatchTypeZlib
decompressedTxs := paramsCompressed.Txs decompressedTxs := paramsCompressed.Txs
paramsCompressed.Txs = nil paramsCompressed.Txs = nil
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment