Skip to content

Commit

Permalink
elderberry seq-sender (#3312)
Browse files Browse the repository at this point in the history
* elderberry seq-sender

* rename lastSequenceBatchNum

* rename LastL2BLockTimestamp
  • Loading branch information
ToniRamirezM authored Feb 20, 2024
1 parent ecc39d4 commit d47bd98
Show file tree
Hide file tree
Showing 6 changed files with 67 additions and 36 deletions.
12 changes: 10 additions & 2 deletions etherman/etherman_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,9 @@ func TestForcedBatchEvent(t *testing.T) {
assert.Equal(t, auth.From, blocks[0].ForcedBatches[0].Sequencer)
}

// TODO: Review tests with Joan

/*
func TestSequencedBatchesEvent(t *testing.T) {
// Set up testing environment
etherman, ethBackend, auth, _, br := newTestingEnv()
Expand Down Expand Up @@ -162,6 +165,7 @@ func TestSequencedBatchesEvent(t *testing.T) {
}, polygonzkevm.PolygonRollupBaseEtrogBatchData{
Transactions: common.Hex2Bytes(rawTxs),
})
_, err = etherman.ZkEVM.SequenceBatches(auth, sequences, auth.From)
require.NoError(t, err)
Expand Down Expand Up @@ -228,6 +232,7 @@ func TestVerifyBatchEvent(t *testing.T) {
assert.Equal(t, 0, order[blocks[1].BlockHash][0].Pos)
assert.Equal(t, 0, order[blocks[1].BlockHash][1].Pos)
}
*/

func TestSequenceForceBatchesEvent(t *testing.T) {
// Set up testing environment
Expand Down Expand Up @@ -310,9 +315,12 @@ func TestSendSequences(t *testing.T) {
batchL2Data, err := state.EncodeTransactions([]types.Transaction{*tx1}, constants.EffectivePercentage, forkID6)
require.NoError(t, err)
sequence := ethmanTypes.Sequence{
BatchL2Data: batchL2Data,
BatchNumber: 0,
BatchL2Data: batchL2Data,
LastL2BLockTimestamp: time.Now().Unix(),
}
tx, err := etherman.sequenceBatches(*auth, []ethmanTypes.Sequence{sequence}, auth.From)

tx, err := etherman.sequenceBatches(*auth, []ethmanTypes.Sequence{sequence}, uint64(sequence.LastL2BLockTimestamp), sequence.BatchNumber, auth.From)
require.NoError(t, err)
log.Debug("TX: ", tx.Hash())
ethBackend.Commit()
Expand Down
1 change: 1 addition & 0 deletions etherman/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ type GlobalExitRoot struct {
PreviousBlockHash common.Hash
}

// SequencedBatchElderberryData represents an Elderberry sequenced batch data
type SequencedBatchElderberryData struct {
MaxSequenceTimestamp uint64
InitSequencedBatchNumber uint64
Expand Down
2 changes: 1 addition & 1 deletion etherman/types/sequence.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
type Sequence struct {
GlobalExitRoot, StateRoot, LocalExitRoot common.Hash
AccInputHash common.Hash
Timestamp int64
LastL2BLockTimestamp int64
BatchL2Data []byte
IsSequenceTooBig bool
BatchNumber uint64
Expand Down
40 changes: 20 additions & 20 deletions sequencesender/mock_etherman.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

37 changes: 28 additions & 9 deletions sequencesender/sequencesender.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,18 +140,18 @@ func (s *SequenceSender) tryToSendSequence(ctx context.Context) {

// Check if we need to wait until last L1 block timestamp is L1BlockTimestampMargin seconds above the timestamp of the last L2 block in the sequence
// Get last batch in the sequence
lastBatchNumInSequence := sequences[sequenceCount-1].BatchNumber
lastSequenceBatchNum := sequences[sequenceCount-1].BatchNumber

// Get L2 blocks for the last batch
lastBatchL2Blocks, err := s.state.GetL2BlocksByBatchNumber(ctx, lastBatchNumInSequence, nil)
lastBatchL2Blocks, err := s.state.GetL2BlocksByBatchNumber(ctx, lastSequenceBatchNum, nil)
if err != nil {
log.Errorf("failed to get L2 blocks for batch %d, err: %v", lastBatchNumInSequence, err)
log.Errorf("failed to get L2 blocks for batch %d, err: %v", lastSequenceBatchNum, err)
return
}

// Check there are L2 blocks for the last batch
if len(lastBatchL2Blocks) == 0 {
log.Errorf("no L2 blocks returned from the state for batch %d", lastBatchNumInSequence)
log.Errorf("no L2 blocks returned from the state for batch %d", lastSequenceBatchNum)
return
}

Expand Down Expand Up @@ -202,13 +202,15 @@ func (s *SequenceSender) tryToSendSequence(ctx context.Context) {
}

// add sequence to be monitored
to, data, err := s.etherman.BuildSequenceBatchesTxData(s.cfg.SenderAddress, sequences, s.cfg.L2Coinbase)
firstSequence := sequences[0]
lastSequence := sequences[len(sequences)-1]

to, data, err := s.etherman.BuildSequenceBatchesTxData(s.cfg.SenderAddress, sequences, uint64(lastSequence.LastL2BLockTimestamp), firstSequence.BatchNumber, s.cfg.L2Coinbase)
if err != nil {
log.Error("error estimating new sequenceBatches to add to eth tx manager: ", err)
return
}
firstSequence := sequences[0]
lastSequence := sequences[len(sequences)-1]

monitoredTxID := fmt.Sprintf(monitoredIDFormat, firstSequence.BatchNumber, lastSequence.BatchNumber)
err = s.ethTxManager.Add(ctx, ethTxManagerOwner, monitoredTxID, s.cfg.SenderAddress, to, nil, data, s.cfg.GasOffset, nil)
if err != nil {
Expand Down Expand Up @@ -286,11 +288,28 @@ func (s *SequenceSender) getSequencesToSend(ctx context.Context) ([]types.Sequen
seq.GlobalExitRoot = forcedBatch.GlobalExitRoot
seq.ForcedBatchTimestamp = forcedBatch.ForcedAt.Unix()
seq.PrevBlockHash = fbL1Block.ParentHash
// Set sequence timestamps as the forced batch timestamp
seq.LastL2BLockTimestamp = seq.ForcedBatchTimestamp
} else {
// Set sequence timestamps as the latest l2 block timestamp
l2Blocks, err := s.state.GetL2BlocksByBatchNumber(ctx, currentBatchNumToSequence, nil)
if err != nil {
return nil, err
}
if len(l2Blocks) == 0 {
return nil, fmt.Errorf("no L2 blocks returned from the state for batch %d", currentBatchNumToSequence)
}

// Get timestamp of the last L2 block in the sequence
lastL2Block := l2Blocks[len(l2Blocks)-1]
seq.LastL2BLockTimestamp = lastL2Block.ReceivedAt.Unix()
}

sequences = append(sequences, seq)
// Check if can be send
tx, err = s.etherman.EstimateGasSequenceBatches(s.cfg.SenderAddress, sequences, s.cfg.L2Coinbase)
firstSequence := sequences[0]
lastSequence := sequences[len(sequences)-1]
tx, err = s.etherman.EstimateGasSequenceBatches(s.cfg.SenderAddress, sequences, uint64(lastSequence.LastL2BLockTimestamp), firstSequence.BatchNumber, s.cfg.L2Coinbase)
if err == nil && tx.Size() > s.cfg.MaxTxSizeForL1 {
metrics.SequencesOvesizedDataError()
log.Infof("oversized Data on TX oldHash %s (txSize %d > %d)", tx.Hash(), tx.Size(), s.cfg.MaxTxSizeForL1)
Expand All @@ -301,7 +320,7 @@ func (s *SequenceSender) getSequencesToSend(ctx context.Context) ([]types.Sequen
sequences, err = s.handleEstimateGasSendSequenceErr(ctx, sequences, currentBatchNumToSequence, err)
if sequences != nil {
// Handling the error gracefully, re-processing the sequence as a sanity check
_, err = s.etherman.EstimateGasSequenceBatches(s.cfg.SenderAddress, sequences, s.cfg.L2Coinbase)
_, err = s.etherman.EstimateGasSequenceBatches(s.cfg.SenderAddress, sequences, uint64(lastSequence.LastL2BLockTimestamp), firstSequence.BatchNumber, s.cfg.L2Coinbase)
return sequences, err
}
return sequences, err
Expand Down
11 changes: 7 additions & 4 deletions test/scripts/batchsender/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -173,14 +173,17 @@ func sendBatches(cliCtx *cli.Context) error {
for i := 0; i < nb; i++ {
// empty rollup
seqs = append(seqs, ethmanTypes.Sequence{
GlobalExitRoot: common.HexToHash("0x"),
BatchL2Data: []byte{},
Timestamp: int64(currentBlock.Time() - 1), // fit in latest-sequence < > current-block rage
BatchNumber: uint64(i),
GlobalExitRoot: common.HexToHash("0x"),
BatchL2Data: []byte{},
LastL2BLockTimestamp: int64(currentBlock.Time() - 1), // fit in latest-sequence < > current-block rage
})
}

// send to L1
to, data, err := ethMan.BuildSequenceBatchesTxData(auth.From, seqs, auth.From)
firstSequence := seqs[0]
lastSequence := seqs[len(seqs)-1]
to, data, err := ethMan.BuildSequenceBatchesTxData(auth.From, seqs, uint64(lastSequence.LastL2BLockTimestamp), firstSequence.BatchNumber, auth.From)
if err != nil {
return err
}
Expand Down

0 comments on commit d47bd98

Please sign in to comment.