Skip to content

Commit

Permalink
code optimization and refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
shijiang.guo authored and HXHke committed Sep 26, 2023
1 parent e084e0f commit f877f61
Show file tree
Hide file tree
Showing 7 changed files with 37 additions and 166 deletions.
67 changes: 5 additions & 62 deletions batch-submitter/drivers/sequencer/batch.go
Original file line number Diff line number Diff line change
@@ -1,19 +1,11 @@
package sequencer

import (
"errors"
"fmt"

l2types "github.com/mantlenetworkio/mantle/l2geth/core/types"
)

var (
// ErrBlockWithInvalidContext signals an attempt to generate a
// BatchContext that specifies a total of zero txs.
ErrBlockWithInvalidContext = errors.New("attempted to generate batch " +
"context with 0 queued and 0 sequenced txs")
)

// BatchElement reflects the contents of an atomic update to the L2 state.
// Currently, each BatchElement is constructed from a single block containing
// exactly one tx.
Expand All @@ -24,16 +16,8 @@ type BatchElement struct {
// BlockNumber is the L1 BlockNumber of the batch.
BlockNumber uint64

// Tx is the optional transaction that was applied in this batch.
//
// NOTE: This field will only be populated for sequencer txs.
Tx *CachedTx
}

// IsSequencerTx returns true if this batch contains a tx that needs to be
// posted to the L1 CTC contract.
func (b *BatchElement) IsSequencerTx() bool {
return b.Tx != nil
// whether it is a Sequencer transaction
IsSequencerTx bool
}

// BatchElementFromBlock constructs a BatchElement from a single L2 block. This
Expand All @@ -54,50 +38,9 @@ func BatchElementFromBlock(block *l2types.Block) BatchElement {
isSequencerTx := tx.QueueOrigin() == l2types.QueueOriginSequencer

// Only include sequencer txs in the returned BatchElement.
var cachedTx *CachedTx
if isSequencerTx {
cachedTx = NewCachedTx(tx)
}

return BatchElement{
Timestamp: block.Time(),
BlockNumber: l1BlockNumber,
Tx: cachedTx,
Timestamp: block.Time(),
BlockNumber: l1BlockNumber,
IsSequencerTx: isSequencerTx,
}
}

type groupedBlock struct {
sequenced []BatchElement
queued []BatchElement
}

// GenSequencerBatchParams generates a valid AppendSequencerBatchParams from a
// list of BatchElements. The BatchElements are assumed to be ordered in
// ascending order by L2 block height.
func GenSequencerBatchParams(
shouldStartAtElement uint64,
blockOffset uint64,
batchNumber uint64,
timestamp uint64,
blockNumber uint64,

numSequencedTxs uint64,
numSubsequentQueueTxs uint64,

) (*AppendSequencerBatchParams, error) {
var (
contexts []BatchContext
)
contexts = append(contexts, BatchContext{
NumSequencedTxs: numSequencedTxs,
NumSubsequentQueueTxs: numSubsequentQueueTxs,
Timestamp: timestamp,
BlockNumber: blockNumber,
})

return &AppendSequencerBatchParams{
ShouldStartAtElement: shouldStartAtElement - blockOffset,
TotalElementsToAppend: batchNumber,
Contexts: contexts,
}, nil
}
9 changes: 4 additions & 5 deletions batch-submitter/drivers/sequencer/batch_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,11 @@ import (
"math/big"
"testing"

"github.com/stretchr/testify/require"

"github.com/mantlenetworkio/mantle/batch-submitter/drivers/sequencer"
l2common "github.com/mantlenetworkio/mantle/l2geth/common"
l2types "github.com/mantlenetworkio/mantle/l2geth/core/types"
"github.com/stretchr/testify/require"
)

func TestBatchElementFromBlock(t *testing.T) {
Expand All @@ -30,8 +31,7 @@ func TestBatchElementFromBlock(t *testing.T) {

require.Equal(t, element.Timestamp, expTime)
require.Equal(t, element.BlockNumber, expBlockNumber)
require.True(t, element.IsSequencerTx())
require.Equal(t, element.Tx.Tx(), expTx)
require.True(t, element.IsSequencerTx)

queueMeta := l2types.NewTransactionMeta(
new(big.Int).SetUint64(expBlockNumber), 0, nil,
Expand All @@ -44,6 +44,5 @@ func TestBatchElementFromBlock(t *testing.T) {

require.Equal(t, element.Timestamp, expTime)
require.Equal(t, element.BlockNumber, expBlockNumber)
require.False(t, element.IsSequencerTx())
require.Nil(t, element.Tx)
require.False(t, element.IsSequencerTx)
}
37 changes: 0 additions & 37 deletions batch-submitter/drivers/sequencer/cached_tx.go

This file was deleted.

31 changes: 21 additions & 10 deletions batch-submitter/drivers/sequencer/driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ import (
"math/big"
"strings"

kms "cloud.google.com/go/kms/apiv1"
"google.golang.org/api/option"

"github.com/ethereum/go-ethereum"
"github.com/ethereum/go-ethereum/accounts/abi"
"github.com/ethereum/go-ethereum/accounts/abi/bind"
Expand All @@ -23,9 +26,6 @@ import (
"github.com/mantlenetworkio/mantle/bss-core/metrics"
"github.com/mantlenetworkio/mantle/bss-core/txmgr"
l2ethclient "github.com/mantlenetworkio/mantle/l2geth/ethclient"

kms "cloud.google.com/go/kms/apiv1"
"google.golang.org/api/option"
)

const (
Expand Down Expand Up @@ -241,11 +241,10 @@ func (d *Driver) CraftBatchTx(
if err != nil {
return nil, err
}

// For each sequencer transaction, update our running total with the
// size of the transaction.
batchElement := BatchElementFromBlock(block)
if batchElement.IsSequencerTx() {
if batchElement.IsSequencerTx {
numSequencedTxs += 1
} else {
numSubsequentQueueTxs += 1
Expand All @@ -257,12 +256,24 @@ func (d *Driver) CraftBatchTx(
}
blocksLen := numSequencedTxs + numSubsequentQueueTxs
shouldStartAt := start.Uint64()

for {
batchParams, err := GenSequencerBatchParams(
shouldStartAt, d.cfg.BlockOffset, uint64(blocksLen), lastTimestamp,
lastBlockNumber, uint64(numSequencedTxs), uint64(numSubsequentQueueTxs))
if err != nil {
return nil, err
var (
contexts []BatchContext
)

batchContext := BatchContext{
NumSequencedTxs: uint64(numSequencedTxs),
NumSubsequentQueueTxs: uint64(numSubsequentQueueTxs),
Timestamp: lastTimestamp,
BlockNumber: lastBlockNumber,
}

contexts = append(contexts, batchContext)
batchParams := &AppendSequencerBatchParams{
ShouldStartAtElement: shouldStartAt - d.cfg.BlockOffset,
TotalElementsToAppend: uint64(blocksLen),
Contexts: contexts,
}

// Encode the batch arguments using the configured encoding type.
Expand Down
14 changes: 4 additions & 10 deletions batch-submitter/drivers/sequencer/encoding.go
Original file line number Diff line number Diff line change
Expand Up @@ -181,10 +181,6 @@ type AppendSequencerBatchParams struct {
// tx windows in Txs and implicitly allow one to compute how many
// (omitted) queued txs are in a given window.
Contexts []BatchContext

// Txs contains all sequencer txs that will be recorded in the L1 CTC
// contract.
Txs []*CachedTx
}

// WriteNoTxn encodes the AppendSequencerBatchParams using the following format:
Expand All @@ -209,7 +205,7 @@ type AppendSequencerBatchParams struct {
//
// Note that writing to a bytes.Buffer cannot
// error, so errors are ignored here
func (p *AppendSequencerBatchParams) WriteNoTxn(
func (p *AppendSequencerBatchParams) Write(
w *bytes.Buffer,
batchType BatchType,
) error {
Expand Down Expand Up @@ -240,7 +236,7 @@ func (p *AppendSequencerBatchParams) Serialize(
batchType BatchType,
) ([]byte, error) {
var buf bytes.Buffer
if err := p.WriteNoTxn(&buf, batchType); err != nil {
if err := p.Write(&buf, batchType); err != nil {
return nil, err
}
return buf.Bytes(), nil
Expand Down Expand Up @@ -320,10 +316,10 @@ func (p *AppendSequencerBatchParams) Read(r io.Reader) error {
// encoded object. Silence the error and return success if
// the batch is well formed.
if err == io.EOF {
if len(p.Contexts) == 0 && len(p.Txs) != 0 {
if len(p.Contexts) == 0 {
return ErrMalformedBatch
}
if len(p.Txs) == 0 && len(p.Contexts) != 0 {
if len(p.Contexts) != 0 {
return ErrMalformedBatch
}
return closeReader()
Expand All @@ -335,8 +331,6 @@ func (p *AppendSequencerBatchParams) Read(r io.Reader) error {
if err := tx.DecodeRLP(l2rlp.NewStream(r, txLen)); err != nil {
return err
}

p.Txs = append(p.Txs, NewCachedTx(tx))
}
}

Expand Down
43 changes: 2 additions & 41 deletions batch-submitter/drivers/sequencer/encoding_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@ import (
"testing"

"github.com/mantlenetworkio/mantle/batch-submitter/drivers/sequencer"
l2types "github.com/mantlenetworkio/mantle/l2geth/core/types"
l2rlp "github.com/mantlenetworkio/mantle/l2geth/rlp"
"github.com/stretchr/testify/require"
)

Expand Down Expand Up @@ -67,7 +65,6 @@ type AppendSequencerBatchParamsTest struct {
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"`
}

Expand Down Expand Up @@ -99,29 +96,12 @@ func TestAppendSequencerBatchParamsEncodeDecode(t *testing.T) {

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.
Expand All @@ -142,15 +122,11 @@ func testAppendSequencerBatchParamsEncodeDecode(
// 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, nil, nil)
paramsBytes, err := params.Serialize(sequencer.BatchTypeZlib)

// Return early when testing error cases, no need to reserialize again
if test.Error {
Expand All @@ -162,30 +138,15 @@ func testAppendSequencerBatchParamsEncodeDecode(
require.Equal(t, test.HexEncoding, hex.EncodeToString(paramsBytes))

// Serialize the batches in compressed form
compressedParamsBytes, err := params.Serialize(sequencer.BatchTypeZlib, nil, nil)
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
Expand Down
2 changes: 1 addition & 1 deletion datalayr

0 comments on commit f877f61

Please sign in to comment.