Skip to content
This repository has been archived by the owner on May 11, 2024. It is now read-only.

feat(pkg): delete reverse iter #509

Merged
merged 3 commits into from
Jan 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 3 additions & 63 deletions pkg/chain_iterator/block_batch_iterator.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,6 @@ type BlockBatchIterator struct {
current *types.Header
onBlocks OnBlocksFunc
isEnd bool
reverse bool
mask-pp marked this conversation as resolved.
Show resolved Hide resolved
reorgRewindDepth uint64
retryInterval time.Duration
}
Expand All @@ -64,7 +63,6 @@ type BlockBatchIteratorConfig struct {
StartHeight *big.Int
EndHeight *big.Int
OnBlocks OnBlocksFunc
Reverse bool
ReorgRewindDepth *uint64
RetryInterval *time.Duration
}
Expand Down Expand Up @@ -97,11 +95,7 @@ func NewBlockBatchIterator(ctx context.Context, cfg *BlockBatchIteratorConfig) (
return nil, fmt.Errorf("failed to get start header, height: %s, error: %w", cfg.StartHeight, err)
}

var endHeader *types.Header
if cfg.Reverse && cfg.EndHeight == nil {
return nil, fmt.Errorf("missing end height")
}
if endHeader, err = cfg.Client.HeaderByNumber(ctx, cfg.EndHeight); err != nil {
if _, err = cfg.Client.HeaderByNumber(ctx, cfg.EndHeight); err != nil {
return nil, fmt.Errorf("failed to get end header, height: %s, error: %w", cfg.EndHeight, err)
}

Expand All @@ -111,13 +105,7 @@ func NewBlockBatchIterator(ctx context.Context, cfg *BlockBatchIteratorConfig) (
chainID: chainID,
startHeight: cfg.StartHeight.Uint64(),
onBlocks: cfg.OnBlocks,
reverse: cfg.Reverse,
}

if cfg.Reverse {
iterator.current = endHeader
} else {
iterator.current = startHeader
current: startHeader,
}

if cfg.MaxBlocksReadPerEpoch != nil {
Expand All @@ -143,11 +131,6 @@ func NewBlockBatchIterator(ctx context.Context, cfg *BlockBatchIteratorConfig) (
// Iter iterates the given chain between the given start and end heights,
// will call the callback when a batch of blocks in chain are iterated.
func (i *BlockBatchIterator) Iter() error {
iterFunc := i.iter
if i.reverse {
iterFunc = i.reverseIter
}

iterOp := func() error {
for {
if i.ctx.Err() != nil {
Expand All @@ -159,7 +142,7 @@ func (i *BlockBatchIterator) Iter() error {
)
break
}
if err := iterFunc(); err != nil {
if err := i.iter(); err != nil {
if errors.Is(err, io.EOF) {
break
}
Expand Down Expand Up @@ -233,49 +216,6 @@ func (i *BlockBatchIterator) iter() (err error) {
return io.EOF
}

func (i *BlockBatchIterator) reverseIter() (err error) {
if err := i.ensureCurrentNotReorged(); err != nil {
return fmt.Errorf("failed to check whether iterator.current cursor has been reorged: %w", err)
}

var (
startHeight uint64
startHeader *types.Header
isLastEpoch bool
)

if i.current.Number.Uint64() <= i.startHeight {
return io.EOF
}

if i.current.Number.Uint64() <= i.blocksReadPerEpoch {
startHeight = 0
} else {
startHeight = i.current.Number.Uint64() - i.blocksReadPerEpoch
}

if startHeight <= i.startHeight {
startHeight = i.startHeight
isLastEpoch = true
}

if startHeader, err = i.client.HeaderByNumber(i.ctx, new(big.Int).SetUint64(startHeight)); err != nil {
return err
}

if err := i.onBlocks(i.ctx, startHeader, i.current, i.updateCurrent, i.end); err != nil {
return err
}

i.current = startHeader

if !isLastEpoch && !i.isEnd {
return errContinue
}

return io.EOF
}

// updateCurrent updates the iterator's current cursor.
func (i *BlockBatchIterator) updateCurrent(current *types.Header) {
if current == nil {
Expand Down
54 changes: 0 additions & 54 deletions pkg/chain_iterator/block_batch_iterator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,41 +48,6 @@ func (s *BlockBatchIteratorTestSuite) TestIter() {
s.Equal(headHeight, lastEnd.Uint64())
}

func (s *BlockBatchIteratorTestSuite) TestIterReverse() {
var (
maxBlocksReadPerEpoch uint64 = 2
startHeight uint64
)

headHeight, err := s.RPCClient.L1.BlockNumber(context.Background())
s.Nil(err)
s.Greater(headHeight, startHeight)

lastStart := new(big.Int).SetUint64(headHeight)

iter, err := NewBlockBatchIterator(context.Background(), &BlockBatchIteratorConfig{
Client: s.RPCClient.L1,
MaxBlocksReadPerEpoch: &maxBlocksReadPerEpoch,
StartHeight: new(big.Int).SetUint64(startHeight),
EndHeight: new(big.Int).SetUint64(headHeight),
Reverse: true,
OnBlocks: func(
ctx context.Context,
start, end *types.Header,
updateCurrentFunc UpdateCurrentFunc,
endIterFunc EndIterFunc,
) error {
s.Equal(lastStart.Uint64(), end.Number.Uint64())
lastStart = start.Number
return nil
},
})

s.Nil(err)
s.Nil(iter.Iter())
s.Equal(startHeight, lastStart.Uint64())
}

func (s *BlockBatchIteratorTestSuite) TestIterEndFunc() {
var maxBlocksReadPerEpoch uint64 = 2

Expand Down Expand Up @@ -196,25 +161,6 @@ func (s *BlockBatchIteratorTestSuite) TestBlockBatchIteratorConfig() {
})
s.ErrorContains(err4, "start height (2) > end height (0)")

_, err5 := NewBlockBatchIterator(context.Background(), &BlockBatchIteratorConfig{
Client: s.RPCClient.L1,
OnBlocks: func(
ctx context.Context,
start, end *types.Header,
updateCurrentFunc UpdateCurrentFunc,
endIterFunc EndIterFunc,
) error {
s.Equal(lastEnd.Uint64(), start.Number.Uint64())
lastEnd = end.Number
endIterFunc()
return nil
},
StartHeight: common.Big0,
Reverse: true,
EndHeight: nil,
})
s.ErrorContains(err5, "missing end height")

_, err6 := NewBlockBatchIterator(context.Background(), &BlockBatchIteratorConfig{
Client: s.RPCClient.L1,
OnBlocks: func(
Expand Down
2 changes: 0 additions & 2 deletions pkg/chain_iterator/event_iterator/block_proposed_iterator.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@ type BlockProposedIteratorConfig struct {
StartHeight *big.Int
EndHeight *big.Int
FilterQuery []*big.Int
Reverse bool
OnBlockProposedEvent OnBlockProposedEvent
}

Expand All @@ -64,7 +63,6 @@ func NewBlockProposedIterator(ctx context.Context, cfg *BlockProposedIteratorCon
MaxBlocksReadPerEpoch: cfg.MaxBlocksReadPerEpoch,
StartHeight: cfg.StartHeight,
EndHeight: cfg.EndHeight,
Reverse: cfg.Reverse,
OnBlocks: assembleBlockProposedIteratorCallback(
cfg.Client,
cfg.TaikoL1,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@ type TransitionProvenIteratorConfig struct {
StartHeight *big.Int
EndHeight *big.Int
FilterQuery []*big.Int
Reverse bool
OnTransitionProved OnTransitionProved
}

Expand All @@ -67,7 +66,6 @@ func NewTransitionProvedIterator(
MaxBlocksReadPerEpoch: cfg.MaxBlocksReadPerEpoch,
StartHeight: cfg.StartHeight,
EndHeight: cfg.EndHeight,
Reverse: cfg.Reverse,
OnBlocks: assembleTransitionProvedIteratorCallback(
cfg.Client,
cfg.TaikoL1,
Expand Down