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

Commit

Permalink
refactor(pkg): refactor logic of block_batch_iterator (#620)
Browse files Browse the repository at this point in the history
Co-authored-by: yuguo <[email protected]>
Co-authored-by: David <[email protected]>
Co-authored-by: maskpp <[email protected]>
  • Loading branch information
4 people authored Mar 11, 2024
1 parent 0a29936 commit 20a221b
Show file tree
Hide file tree
Showing 7 changed files with 107 additions and 155 deletions.
8 changes: 8 additions & 0 deletions cmd/flags/prover.go
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,13 @@ var (
Usage: "Version or tag or the L2 Node Version used as an L2 RPC Url by this guardian prover",
Category: proverCategory,
}
// Confirmations specific flag
BlockConfirmations = &cli.Uint64Flag{
Name: "prover.blockConfirmations",
Usage: "Confirmations to the latest l1 block before submitting a proof for a l2 block",
Value: 6,
Category: proverCategory,
}
)

// ProverFlags All prover flags.
Expand Down Expand Up @@ -207,4 +214,5 @@ var ProverFlags = MergeFlags(CommonFlags, []cli.Flag{
Allowance,
L1NodeVersion,
L2NodeVersion,
BlockConfirmations,
})
39 changes: 28 additions & 11 deletions pkg/chain_iterator/block_batch_iterator.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
const (
DefaultBlocksReadPerEpoch = 1000
DefaultRetryInterval = 12 * time.Second
DefaultBlockConfirmations = 0
)

var (
Expand Down Expand Up @@ -54,6 +55,7 @@ type BlockBatchIterator struct {
isEnd bool
reorgRewindDepth uint64
retryInterval time.Duration
blockConfirmations *uint64
}

// BlockBatchIteratorConfig represents the configs of a block batch iterator.
Expand All @@ -65,6 +67,7 @@ type BlockBatchIteratorConfig struct {
OnBlocks OnBlocksFunc
ReorgRewindDepth *uint64
RetryInterval time.Duration
BlockConfirmations *uint64
}

// NewBlockBatchIterator creates a new block batch iterator instance.
Expand Down Expand Up @@ -95,12 +98,13 @@ func NewBlockBatchIterator(ctx context.Context, cfg *BlockBatchIteratorConfig) (
}

iterator := &BlockBatchIterator{
ctx: ctx,
client: cfg.Client,
chainID: cfg.Client.ChainID,
startHeight: cfg.StartHeight.Uint64(),
onBlocks: cfg.OnBlocks,
current: startHeader,
ctx: ctx,
client: cfg.Client,
chainID: cfg.Client.ChainID,
startHeight: cfg.StartHeight.Uint64(),
onBlocks: cfg.OnBlocks,
current: startHeader,
blockConfirmations: cfg.BlockConfirmations,
}

if cfg.MaxBlocksReadPerEpoch != nil {
Expand Down Expand Up @@ -165,18 +169,31 @@ func (i *BlockBatchIterator) iter() (err error) {
}

var (
endHeight uint64
endHeader *types.Header
destHeight uint64
isLastEpoch bool
endHeight uint64
endHeader *types.Header
destHeight uint64
isLastEpoch bool
blockConfirmations uint64
)

if i.blockConfirmations == nil {
blockConfirmations = DefaultBlockConfirmations
} else {
blockConfirmations = *i.blockConfirmations
}

if i.endHeight != nil {
destHeight = *i.endHeight
} else {
if destHeight, err = i.client.BlockNumber(i.ctx); err != nil {
destHeight, err = i.client.BlockNumber(i.ctx)
if err != nil {
return err
}
if destHeight > blockConfirmations {
destHeight -= blockConfirmations
} else {
destHeight = 0
}
}

if i.current.Number.Uint64() >= destHeight {
Expand Down
66 changes: 66 additions & 0 deletions pkg/chain_iterator/block_batch_iterator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package chainiterator

import (
"context"
"io"
"math/big"
"testing"
"time"
Expand Down Expand Up @@ -48,6 +49,71 @@ func (s *BlockBatchIteratorTestSuite) TestIter() {
s.Equal(headHeight, lastEnd.Uint64())
}

func (s *BlockBatchIteratorTestSuite) TestIterWithoutSpecifiedEndHeight() {
var maxBlocksReadPerEpoch uint64 = 2
var blockConfirmations uint64 = 6

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

lastEnd := common.Big0

iter, err := NewBlockBatchIterator(context.Background(), &BlockBatchIteratorConfig{
Client: s.RPCClient.L1,
MaxBlocksReadPerEpoch: &maxBlocksReadPerEpoch,
StartHeight: common.Big0,
BlockConfirmations: &blockConfirmations,
OnBlocks: func(
_ context.Context,
start, end *types.Header,
_ UpdateCurrentFunc,
_ EndIterFunc,
) error {
s.Equal(lastEnd.Uint64(), start.Number.Uint64())
lastEnd = end.Number
return nil
},
})

s.Nil(err)
s.Nil(iter.Iter())
s.Equal(headHeight-blockConfirmations, lastEnd.Uint64())
}

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

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

lastEnd := headHeight

var blockConfirmations = headHeight + 3

iter, err := NewBlockBatchIterator(context.Background(), &BlockBatchIteratorConfig{
Client: s.RPCClient.L1,
MaxBlocksReadPerEpoch: &maxBlocksReadPerEpoch,
StartHeight: new(big.Int).SetUint64(headHeight),
BlockConfirmations: &blockConfirmations,
OnBlocks: func(
_ context.Context,
start, end *types.Header,
_ UpdateCurrentFunc,
_ EndIterFunc,
) error {
s.Equal(lastEnd, start.Number.Uint64())
lastEnd = end.Number.Uint64()
return nil
},
})

s.Nil(err)
s.Equal(io.EOF, iter.iter())
s.Equal(headHeight, lastEnd)
}

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

Expand Down
2 changes: 2 additions & 0 deletions pkg/chain_iterator/event_iterator/block_proposed_iterator.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ type BlockProposedIteratorConfig struct {
EndHeight *big.Int
FilterQuery []*big.Int
OnBlockProposedEvent OnBlockProposedEvent
BlockConfirmations *uint64
}

// NewBlockProposedIterator creates a new instance of BlockProposed event iterator.
Expand All @@ -63,6 +64,7 @@ func NewBlockProposedIterator(ctx context.Context, cfg *BlockProposedIteratorCon
MaxBlocksReadPerEpoch: cfg.MaxBlocksReadPerEpoch,
StartHeight: cfg.StartHeight,
EndHeight: cfg.EndHeight,
BlockConfirmations: cfg.BlockConfirmations,
OnBlocks: assembleBlockProposedIteratorCallback(
cfg.Client,
cfg.TaikoL1,
Expand Down
144 changes: 0 additions & 144 deletions pkg/chain_iterator/event_iterator/transition_proven_iterator.go

This file was deleted.

2 changes: 2 additions & 0 deletions prover/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ type Config struct {
RaikoHostEndpoint string
L1NodeVersion string
L2NodeVersion string
BlockConfirmations uint64
}

// NewConfigFromCliContext creates a new config instance from command line flags.
Expand Down Expand Up @@ -176,5 +177,6 @@ func NewConfigFromCliContext(c *cli.Context) (*Config, error) {
Allowance: allowance,
L1NodeVersion: c.String(flags.L1NodeVersion.Name),
L2NodeVersion: c.String(flags.L2NodeVersion.Name),
BlockConfirmations: c.Uint64(flags.BlockConfirmations.Name),
}, nil
}
1 change: 1 addition & 0 deletions prover/prover.go
Original file line number Diff line number Diff line change
Expand Up @@ -339,6 +339,7 @@ func (p *Prover) proveOp() error {
TaikoL1: p.rpc.TaikoL1,
StartHeight: new(big.Int).SetUint64(p.sharedState.GetL1Current().Number.Uint64()),
OnBlockProposedEvent: p.blockProposedHandler.Handle,
BlockConfirmations: &p.cfg.BlockConfirmations,
})
if err != nil {
log.Error("Failed to start event iterator", "event", "BlockProposed", "error", err)
Expand Down

0 comments on commit 20a221b

Please sign in to comment.