Skip to content

Commit

Permalink
fix: make ut succeed
Browse files Browse the repository at this point in the history
  • Loading branch information
2020xibao committed Feb 28, 2025
1 parent 34b9560 commit bf3372a
Show file tree
Hide file tree
Showing 15 changed files with 184 additions and 173 deletions.
2 changes: 1 addition & 1 deletion op-node/rollup/chain_spec_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ var testConfig = Config{
GasLimit: 30_000_000,
},
},
BlockTime: 2,
BlockTime: 2000,
MaxSequencerDrift: 600,
SeqWindowSize: 3600,
ChannelTimeout: 300,
Expand Down
78 changes: 41 additions & 37 deletions op-node/rollup/derive/batch_queue_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"encoding/binary"
"errors"
"fmt"
"io"
"math/big"
"math/rand"
Expand Down Expand Up @@ -55,7 +56,7 @@ func b(chainId *big.Int, timestamp uint64, epoch eth.L1BlockRef) *SingularBatch
txData, _ := tx.MarshalBinary()
return &SingularBatch{
ParentHash: mockHash(timestamp-2, 2),
Timestamp: timestamp,
Timestamp: timestamp * 1000,
EpochNum: rollup.Epoch(epoch.Number),
EpochHash: epoch.Hash,
Transactions: []hexutil.Bytes{txData},
Expand Down Expand Up @@ -101,18 +102,18 @@ func singularBatchToPayload(t *testing.T, batch *SingularBatch, blockNumber uint
txs = append(txs, batch.Transactions...)
return eth.ExecutionPayloadEnvelope{
ExecutionPayload: &eth.ExecutionPayload{
BlockHash: mockHash(batch.Timestamp, 2),
BlockHash: mockHash(batch.Timestamp/1000, 2),
ParentHash: batch.ParentHash,
BlockNumber: hexutil.Uint64(blockNumber),
Timestamp: hexutil.Uint64(batch.Timestamp),
Timestamp: hexutil.Uint64(batch.Timestamp / 1000),
Transactions: txs,
},
}
}

func singularBatchToBlockRef(t *testing.T, batch *SingularBatch, blockNumber uint64) eth.L2BlockRef {
return eth.L2BlockRef{
Hash: mockHash(batch.Timestamp, 2),
Hash: mockHash(batch.Timestamp/1000, 2),
Number: blockNumber,
ParentHash: batch.ParentHash,
Time: batch.Timestamp,
Expand Down Expand Up @@ -251,12 +252,12 @@ func BatchQueueEager(t *testing.T, batchType int) {

// expected output of BatchQueue.NextBatch()
expectedOutputBatches := []*SingularBatch{
b(cfg.L2ChainID, 12000, l1[0]),
b(cfg.L2ChainID, 14000, l1[0]),
b(cfg.L2ChainID, 16000, l1[0]),
b(cfg.L2ChainID, 18000, l1[0]),
b(cfg.L2ChainID, 20000, l1[0]),
b(cfg.L2ChainID, 22000, l1[0]),
b(cfg.L2ChainID, 12, l1[0]),
b(cfg.L2ChainID, 14, l1[0]),
b(cfg.L2ChainID, 16, l1[0]),
b(cfg.L2ChainID, 18, l1[0]),
b(cfg.L2ChainID, 20, l1[0]),
b(cfg.L2ChainID, 22, l1[0]),
nil,
}
// expected error of BatchQueue.NextBatch()
Expand Down Expand Up @@ -289,14 +290,16 @@ func BatchQueueEager(t *testing.T, batchType int) {

for i := 0; i < len(expectedOutputBatches); i++ {
b, _, e := bq.NextBatch(context.Background(), safeHead)
log.Info("DEBUG: ", ", i=", i, ", b=", b, ", safe_head=", safeHead)
fmt.Printf("DEBUG: i=%v, b=%v, safehead=%v\n", i, b, safeHead)
require.ErrorIs(t, e, expectedOutputErrors[i])
if b == nil {
require.Nil(t, expectedOutputBatches[i])
} else {
require.Equal(t, expectedOutputBatches[i], b)
safeHead.Number += 1
safeHead.Time += cfg.BlockTime / 1000
safeHead.Hash = mockHash(b.Timestamp, 2)
safeHead.Hash = mockHash(b.Timestamp/1000, 2)
safeHead.L1Origin = b.Epoch()
}
}
Expand All @@ -320,7 +323,7 @@ func BatchQueueInvalidInternalAdvance(t *testing.T, batchType int) {
Genesis: rollup.Genesis{
L2Time: 10,
},
BlockTime: 2,
BlockTime: 2000,
MaxSequencerDrift: 600,
SeqWindowSize: 2,
DeltaTime: getDeltaTime(batchType),
Expand Down Expand Up @@ -373,7 +376,7 @@ func BatchQueueInvalidInternalAdvance(t *testing.T, batchType int) {
require.Equal(t, expectedOutputBatches[i], b)
safeHead.Number += 1
safeHead.Time += 2
safeHead.Hash = mockHash(b.Timestamp, 2)
safeHead.Hash = mockHash(b.Timestamp/1000, 2)
safeHead.L1Origin = b.Epoch()
}
}
Expand All @@ -396,7 +399,7 @@ func BatchQueueInvalidInternalAdvance(t *testing.T, batchType int) {
b, _, e = bq.NextBatch(context.Background(), safeHead)
require.Nil(t, e)
require.NotNil(t, b)
require.Equal(t, safeHead.Time+2, b.Timestamp)
require.Equal(t, (safeHead.Time+2)*1000, b.Timestamp)
require.Equal(t, rollup.Epoch(1), b.EpochNum)
safeHead.Number += 1
safeHead.Time += 2
Expand All @@ -412,7 +415,7 @@ func BatchQueueInvalidInternalAdvance(t *testing.T, batchType int) {
require.Nil(t, e)
require.NotNil(t, b)
require.Equal(t, rollup.Epoch(2), b.EpochNum)
require.Equal(t, safeHead.Time+2, b.Timestamp)
require.Equal(t, (safeHead.Time+2)*1000, b.Timestamp)
safeHead.Number += 1
safeHead.Time += 2
safeHead.Hash = mockHash(b.Timestamp, 2)
Expand All @@ -432,14 +435,15 @@ func BatchQueueMissing(t *testing.T, batchType int) {
Number: 0,
ParentHash: common.Hash{},
Time: 10,
MilliPartTime: 0,
L1Origin: l1[0].ID(),
SequenceNumber: 0,
}
cfg := &rollup.Config{
Genesis: rollup.Genesis{
L2Time: 10,
},
BlockTime: 2,
BlockTime: 2000,
MaxSequencerDrift: 600,
SeqWindowSize: 2,
DeltaTime: getDeltaTime(batchType),
Expand Down Expand Up @@ -497,22 +501,22 @@ func BatchQueueMissing(t *testing.T, batchType int) {
// Check for a generated batch at t = 12
b, _, e = bq.NextBatch(context.Background(), safeHead)
require.Nil(t, e)
require.Equal(t, b.Timestamp, uint64(12))
require.Equal(t, b.Timestamp, uint64(12000))
require.Empty(t, b.Transactions)
require.Equal(t, rollup.Epoch(0), b.EpochNum)
safeHead.Number += 1
safeHead.Time += 2
safeHead.Hash = mockHash(b.Timestamp, 2)
safeHead.Hash = mockHash(b.Timestamp/1000, 2)

// Check for generated batch at t = 14
b, _, e = bq.NextBatch(context.Background(), safeHead)
require.Nil(t, e)
require.Equal(t, b.Timestamp, uint64(14))
require.Equal(t, b.Timestamp, uint64(14000))
require.Empty(t, b.Transactions)
require.Equal(t, rollup.Epoch(0), b.EpochNum)
safeHead.Number += 1
safeHead.Time += 2
safeHead.Hash = mockHash(b.Timestamp, 2)
safeHead.Hash = mockHash(b.Timestamp/1000, 2)

// Check for the inputted batch at t = 16
b, _, e = bq.NextBatch(context.Background(), safeHead)
Expand All @@ -521,7 +525,7 @@ func BatchQueueMissing(t *testing.T, batchType int) {
require.Equal(t, rollup.Epoch(0), b.EpochNum)
safeHead.Number += 1
safeHead.Time += 2
safeHead.Hash = mockHash(b.Timestamp, 2)
safeHead.Hash = mockHash(b.Timestamp/1000, 2)

// Advance the origin. At this point the batch with timestamp 18 will be created
input.origin = l1[3]
Expand All @@ -533,7 +537,7 @@ func BatchQueueMissing(t *testing.T, batchType int) {
require.Equal(t, e, io.EOF)
b, _, e = bq.NextBatch(context.Background(), safeHead)
require.Nil(t, e)
require.Equal(t, b.Timestamp, uint64(18))
require.Equal(t, b.Timestamp, uint64(18000))
require.Empty(t, b.Transactions)
require.Equal(t, rollup.Epoch(1), b.EpochNum)
}
Expand All @@ -556,7 +560,7 @@ func BatchQueueAdvancedEpoch(t *testing.T, batchType int) {
Genesis: rollup.Genesis{
L2Time: 10,
},
BlockTime: 2,
BlockTime: 2000,
MaxSequencerDrift: 600,
SeqWindowSize: 30,
DeltaTime: getDeltaTime(batchType),
Expand Down Expand Up @@ -619,8 +623,8 @@ func BatchQueueAdvancedEpoch(t *testing.T, batchType int) {
} else {
require.Equal(t, expectedOutput, b)
safeHead.Number += 1
safeHead.Time += cfg.BlockTime
safeHead.Hash = mockHash(b.Timestamp, 2)
safeHead.Time += cfg.BlockTime / 1000
safeHead.Hash = mockHash(b.Timestamp/1000, 2)
safeHead.L1Origin = b.Epoch()
}
}
Expand All @@ -643,7 +647,7 @@ func BatchQueueShuffle(t *testing.T, batchType int) {
Genesis: rollup.Genesis{
L2Time: 10,
},
BlockTime: 2,
BlockTime: 2000,
MaxSequencerDrift: 600,
SeqWindowSize: 30,
DeltaTime: getDeltaTime(batchType),
Expand Down Expand Up @@ -718,8 +722,8 @@ func BatchQueueShuffle(t *testing.T, batchType int) {
} else {
require.Equal(t, expectedOutput, b)
safeHead.Number += 1
safeHead.Time += cfg.BlockTime
safeHead.Hash = mockHash(b.Timestamp, 2)
safeHead.Time += cfg.BlockTime / 1000
safeHead.Hash = mockHash(b.Timestamp/1000, 2)
safeHead.L1Origin = b.Epoch()
}
}
Expand All @@ -741,7 +745,7 @@ func TestBatchQueueOverlappingSpanBatch(t *testing.T) {
Genesis: rollup.Genesis{
L2Time: 10,
},
BlockTime: 2,
BlockTime: 2000,
MaxSequencerDrift: 600,
SeqWindowSize: 30,
DeltaTime: getDeltaTime(SpanBatchType),
Expand Down Expand Up @@ -821,8 +825,8 @@ func TestBatchQueueOverlappingSpanBatch(t *testing.T) {
} else {
require.Equal(t, expectedOutputBatches[i], b)
safeHead.Number += 1
safeHead.Time += cfg.BlockTime
safeHead.Hash = mockHash(b.Timestamp, 2)
safeHead.Time += cfg.BlockTime / 1000
safeHead.Hash = mockHash(b.Timestamp/1000, 2)
safeHead.L1Origin = b.Epoch()
}
}
Expand All @@ -846,7 +850,7 @@ func TestBatchQueueComplex(t *testing.T) {
Genesis: rollup.Genesis{
L2Time: 10,
},
BlockTime: 2,
BlockTime: 2000,
MaxSequencerDrift: 600,
SeqWindowSize: 30,
DeltaTime: getDeltaTime(SpanBatchType),
Expand Down Expand Up @@ -939,8 +943,8 @@ func TestBatchQueueComplex(t *testing.T) {
} else {
require.Equal(t, expectedOutput, b)
safeHead.Number += 1
safeHead.Time += cfg.BlockTime
safeHead.Hash = mockHash(b.Timestamp, 2)
safeHead.Time += cfg.BlockTime / 1000
safeHead.Hash = mockHash(b.Timestamp/1000, 2)
safeHead.L1Origin = b.Epoch()
}
}
Expand All @@ -964,7 +968,7 @@ func TestBatchQueueResetSpan(t *testing.T) {
Genesis: rollup.Genesis{
L2Time: 10,
},
BlockTime: 2,
BlockTime: 2000,
MaxSequencerDrift: 600,
SeqWindowSize: 30,
DeltaTime: getDeltaTime(SpanBatchType),
Expand Down Expand Up @@ -997,8 +1001,8 @@ func TestBatchQueueResetSpan(t *testing.T) {

// This NextBatch() will return the second singular batch.
safeHead.Number += 1
safeHead.Time += cfg.BlockTime
safeHead.Hash = mockHash(nextBatch.Timestamp, 2)
safeHead.Time += cfg.BlockTime / 1000
safeHead.Hash = mockHash(nextBatch.Timestamp/1000, 2)
safeHead.L1Origin = nextBatch.Epoch()
nextBatch, _, err = bq.NextBatch(context.Background(), safeHead)
require.NoError(t, err)
Expand Down
4 changes: 2 additions & 2 deletions op-node/rollup/derive/batch_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ func RandomRawSpanBatch(rng *rand.Rand, chainId *big.Int) *RawSpanBatch {

func RandomValidConsecutiveSingularBatches(rng *rand.Rand, chainID *big.Int) []*SingularBatch {
blockCount := 2 + rng.Intn(128)
l2BlockTime := uint64(2)
l2BlockTime := uint64(2) * 1000 // ms

var singularBatches []*SingularBatch
for i := 0; i < blockCount; i++ {
Expand All @@ -87,7 +87,7 @@ func RandomValidConsecutiveSingularBatches(rng *rand.Rand, chainID *big.Int) []*
}
l1BlockNum := rng.Uint64()
// make sure oldest timestamp is large enough
singularBatches[0].Timestamp += 256
singularBatches[0].Timestamp += 256 * 1000 // ms
for i := 0; i < blockCount; i++ {
originChangedBit := rng.Intn(2)
if originChangedBit == 1 {
Expand Down
8 changes: 4 additions & 4 deletions op-node/rollup/derive/batches.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ func checkSingularBatch(cfg *rollup.Config, log log.Logger, l1Blocks []eth.L1Blo

spec := rollup.NewChainSpec(cfg)
// Check if we ran out of sequencer time drift
if max := batchOrigin.Time + spec.MaxSequencerDrift(batchOrigin.Time); batch.Timestamp > max {
if max := (batchOrigin.Time + spec.MaxSequencerDrift(batchOrigin.Time)) * 1000; batch.Timestamp > max {
if len(batch.Transactions) == 0 {
// If the sequencer is co-operating by producing an empty batch,
// then allow the batch if it was the right thing to do to maintain the L2 time >= L1 time invariant.
Expand All @@ -136,7 +136,7 @@ func checkSingularBatch(cfg *rollup.Config, log log.Logger, l1Blocks []eth.L1Blo
return BatchUndecided
}
nextOrigin := l1Blocks[1]
if batch.Timestamp >= nextOrigin.Time { // check if the next L1 origin could have been adopted
if batch.Timestamp >= nextOrigin.MilliTimestamp() { // check if the next L1 origin could have been adopted
log.Info("batch exceeded sequencer time drift without adopting next origin, and next L1 origin would have been valid")
return BatchDrop
} else {
Expand Down Expand Up @@ -272,7 +272,7 @@ func checkSpanBatch(ctx context.Context, cfg *rollup.Config, log log.Logger, l1B
originAdvanced := startEpochNum == parentBlock.L1Origin.Number+1

for i := 0; i < batch.GetBlockCount(); i++ {
if batch.GetBlockTimestamp(i) <= l2SafeHead.Time {
if batch.GetBlockTimestamp(i) <= l2SafeHead.MillisecondTimestamp() {
continue
}
var l1Origin eth.L1BlockRef
Expand All @@ -297,7 +297,7 @@ func checkSpanBatch(ctx context.Context, cfg *rollup.Config, log log.Logger, l1B

spec := rollup.NewChainSpec(cfg)
// Check if we ran out of sequencer time drift
if max := l1Origin.Time + spec.MaxSequencerDrift(l1Origin.Time); blockTimestamp > max {
if max := (l1Origin.Time + spec.MaxSequencerDrift(l1Origin.Time)) * 1000; blockTimestamp > max {
if len(batch.GetBlockTransactions(i)) == 0 {
// If the sequencer is co-operating by producing an empty batch,
// then allow the batch if it was the right thing to do to maintain the L2 time >= L1 time invariant.
Expand Down
Loading

0 comments on commit bf3372a

Please sign in to comment.