Commit 6780c473 authored by Conner Fromknecht's avatar Conner Fromknecht

feat: cache go BSS txn serialization

parent 229fde5a
...@@ -27,7 +27,7 @@ type BatchElement struct { ...@@ -27,7 +27,7 @@ type BatchElement struct {
// Tx is the optional transaction that was applied in this batch. // Tx is the optional transaction that was applied in this batch.
// //
// NOTE: This field will only be populated for sequencer txs. // NOTE: This field will only be populated for sequencer txs.
Tx *l2types.Transaction Tx *CachedTx
} }
// IsSequencerTx returns true if this batch contains a tx that needs to be // IsSequencerTx returns true if this batch contains a tx that needs to be
...@@ -54,14 +54,15 @@ func BatchElementFromBlock(block *l2types.Block) BatchElement { ...@@ -54,14 +54,15 @@ func BatchElementFromBlock(block *l2types.Block) BatchElement {
isSequencerTx := tx.QueueOrigin() == l2types.QueueOriginSequencer isSequencerTx := tx.QueueOrigin() == l2types.QueueOriginSequencer
// Only include sequencer txs in the returned BatchElement. // Only include sequencer txs in the returned BatchElement.
if !isSequencerTx { var cachedTx *CachedTx
tx = nil if isSequencerTx {
cachedTx = NewCachedTx(tx)
} }
return BatchElement{ return BatchElement{
Timestamp: block.Time(), Timestamp: block.Time(),
BlockNumber: l1BlockNumber, BlockNumber: l1BlockNumber,
Tx: tx, Tx: cachedTx,
} }
} }
...@@ -82,7 +83,7 @@ func GenSequencerBatchParams( ...@@ -82,7 +83,7 @@ func GenSequencerBatchParams(
var ( var (
contexts []BatchContext contexts []BatchContext
groupedBlocks []groupedBlock groupedBlocks []groupedBlock
txs []*l2types.Transaction txs []*CachedTx
lastBlockIsSequencerTx bool lastBlockIsSequencerTx bool
lastTimestamp uint64 lastTimestamp uint64
lastBlockNumber uint64 lastBlockNumber uint64
......
...@@ -31,7 +31,7 @@ func TestBatchElementFromBlock(t *testing.T) { ...@@ -31,7 +31,7 @@ func TestBatchElementFromBlock(t *testing.T) {
require.Equal(t, element.Timestamp, expTime) require.Equal(t, element.Timestamp, expTime)
require.Equal(t, element.BlockNumber, expBlockNumber) require.Equal(t, element.BlockNumber, expBlockNumber)
require.True(t, element.IsSequencerTx()) require.True(t, element.IsSequencerTx())
require.Equal(t, element.Tx, expTx) require.Equal(t, element.Tx.Tx(), expTx)
queueMeta := l2types.NewTransactionMeta( queueMeta := l2types.NewTransactionMeta(
new(big.Int).SetUint64(expBlockNumber), 0, nil, new(big.Int).SetUint64(expBlockNumber), 0, nil,
......
package sequencer
import (
"bytes"
"fmt"
l2types "github.com/ethereum-optimism/optimism/l2geth/core/types"
)
type CachedTx struct {
tx *l2types.Transaction
rawTx []byte
}
func NewCachedTx(tx *l2types.Transaction) *CachedTx {
var txBuf bytes.Buffer
if err := tx.EncodeRLP(&txBuf); err != nil {
panic(fmt.Sprintf("Unable to encode tx: %v", err))
}
return &CachedTx{
tx: tx,
rawTx: txBuf.Bytes(),
}
}
func (t *CachedTx) Tx() *l2types.Transaction {
return t.tx
}
func (t *CachedTx) Size() int {
return len(t.rawTx)
}
func (t *CachedTx) RawTx() []byte {
return t.rawTx
}
...@@ -88,7 +88,7 @@ type AppendSequencerBatchParams struct { ...@@ -88,7 +88,7 @@ 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 []*l2types.Transaction Txs []*CachedTx
} }
// Write encodes the AppendSequencerBatchParams using the following format: // Write encodes the AppendSequencerBatchParams using the following format:
...@@ -110,16 +110,9 @@ func (p *AppendSequencerBatchParams) Write(w *bytes.Buffer) error { ...@@ -110,16 +110,9 @@ func (p *AppendSequencerBatchParams) Write(w *bytes.Buffer) error {
} }
// Write each length-prefixed tx. // Write each length-prefixed tx.
var txBuf bytes.Buffer
for _, tx := range p.Txs { for _, tx := range p.Txs {
txBuf.Reset() writeUint64(w, uint64(tx.Size()), 3)
_, _ = w.Write(tx.RawTx()) // can't fail for bytes.Buffer
if err := tx.EncodeRLP(&txBuf); err != nil {
return err
}
writeUint64(w, uint64(txBuf.Len()), 3)
_, _ = w.Write(txBuf.Bytes()) // can't fail for bytes.Buffer
} }
return nil return nil
...@@ -187,7 +180,7 @@ func (p *AppendSequencerBatchParams) Read(r io.Reader) error { ...@@ -187,7 +180,7 @@ func (p *AppendSequencerBatchParams) Read(r io.Reader) error {
return err return err
} }
p.Txs = append(p.Txs, tx) p.Txs = append(p.Txs, NewCachedTx(tx))
} }
} }
......
...@@ -297,9 +297,9 @@ func testAppendSequencerBatchParamsEncodeDecode( ...@@ -297,9 +297,9 @@ func testAppendSequencerBatchParamsEncodeDecode(
// compareTxs compares a list of two transactions, testing each pair by tx hash. // 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 // 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. // decoded tx and the expected tx will differ, and can't be modified/ignored.
func compareTxs(t *testing.T, a, b []*l2types.Transaction) { func compareTxs(t *testing.T, a []*l2types.Transaction, b []*sequencer.CachedTx) {
require.Equal(t, len(a), len(b)) require.Equal(t, len(a), len(b))
for i, txA := range a { for i, txA := range a {
require.Equal(t, txA.Hash(), b[i].Hash()) require.Equal(t, txA.Hash(), b[i].Tx().Hash())
} }
} }
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