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

Commit

Permalink
feat: update pkg package
Browse files Browse the repository at this point in the history
  • Loading branch information
davidtaikocha committed Sep 29, 2023
1 parent 330b92f commit e58ace7
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 36 deletions.
4 changes: 2 additions & 2 deletions driver/state/l1_current.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,14 +63,14 @@ func (s *State) ResetL1Current(

iter, err := eventIterator.NewBlockProvenIterator(
ctx,
&eventIterator.BlockProvenIteratorConfig{
&eventIterator.TransitionProvenIteratorConfig{
Client: s.rpc.L1,
TaikoL1: s.rpc.TaikoL1,
StartHeight: s.GenesisL1Height,
EndHeight: s.GetL1Head().Number,
FilterQuery: []*big.Int{},
Reverse: true,
OnBlockProvenEvent: func(
OnTransitionProved: func(
ctx context.Context,
e *bindings.TaikoL1ClientBlockProven,
end eventIterator.EndBlockProvenEventIterFunc,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,42 +12,49 @@ import (
"github.com/taikoxyz/taiko-client/pkg/rpc"
)

// EndBlockProvenEventIterFunc ends the current iteration.
type EndBlockProvenEventIterFunc func()
// EndTransitionProvedEventIterFunc ends the current iteration.
type EndTransitionProvedEventIterFunc func()

// OnBlockProvenEvent represents the callback function which will be called when a TaikoL1.BlockProven event is
// OnTransitionProved represents the callback function which will be called when a TaikoL1.TransitionProved event is
// iterated.
type OnBlockProvenEvent func(context.Context, *bindings.TaikoL1ClientBlockProven, EndBlockProvenEventIterFunc) error
type OnTransitionProved func(
context.Context,
*bindings.TaikoL1ClientTransitionProved,
EndTransitionProvedEventIterFunc,
) error

// BlockProvenIterator iterates the emitted TaikoL1.BlockProven events in the chain,
// TransitionProvedIterator iterates the emitted TaikoL1.TransitionProved events in the chain,
// with the awareness of reorganization.
type BlockProvenIterator struct {
type TransitionProvedIterator struct {
ctx context.Context
taikoL1 *bindings.TaikoL1Client
blockBatchIterator *chainIterator.BlockBatchIterator
filterQuery []*big.Int
isEnd bool
}

// BlockProvenIteratorConfig represents the configs of a BlockProven event iterator.
type BlockProvenIteratorConfig struct {
// TransitionProvenIteratorConfig represents the configs of a TransitionProved event iterator.
type TransitionProvenIteratorConfig struct {
Client *rpc.EthClient
TaikoL1 *bindings.TaikoL1Client
MaxBlocksReadPerEpoch *uint64
StartHeight *big.Int
EndHeight *big.Int
FilterQuery []*big.Int
Reverse bool
OnBlockProvenEvent OnBlockProvenEvent
OnTransitionProved OnTransitionProved
}

// NewBlockProvenIterator creates a new instance of BlockProven event iterator.
func NewBlockProvenIterator(ctx context.Context, cfg *BlockProvenIteratorConfig) (*BlockProvenIterator, error) {
if cfg.OnBlockProvenEvent == nil {
// NewTransitionProvedIterator creates a new instance of TransitionProved event iterator.
func NewTransitionProvedIterator(
ctx context.Context,
cfg *TransitionProvenIteratorConfig,
) (*TransitionProvedIterator, error) {
if cfg.OnTransitionProved == nil {
return nil, errors.New("invalid callback")
}

iterator := &BlockProvenIterator{
iterator := &TransitionProvedIterator{
ctx: ctx,
taikoL1: cfg.TaikoL1,
filterQuery: cfg.FilterQuery,
Expand All @@ -60,11 +67,11 @@ func NewBlockProvenIterator(ctx context.Context, cfg *BlockProvenIteratorConfig)
StartHeight: cfg.StartHeight,
EndHeight: cfg.EndHeight,
Reverse: cfg.Reverse,
OnBlocks: assembleBlockProvenIteratorCallback(
OnBlocks: assembleTransitionProvedIteratorCallback(
cfg.Client,
cfg.TaikoL1,
cfg.FilterQuery,
cfg.OnBlockProvenEvent,
cfg.OnTransitionProved,
iterator,
),
})
Expand All @@ -78,24 +85,24 @@ func NewBlockProvenIterator(ctx context.Context, cfg *BlockProvenIteratorConfig)
}

// Iter iterates the given chain between the given start and end heights,
// will call the callback when a BlockProven event is iterated.
func (i *BlockProvenIterator) Iter() error {
// will call the callback when a TransitionProved event is iterated.
func (i *TransitionProvedIterator) Iter() error {
return i.blockBatchIterator.Iter()
}

// end ends the current iteration.
func (i *BlockProvenIterator) end() {
func (i *TransitionProvedIterator) end() {
i.isEnd = true
}

// assembleBlockProvenIteratorCallback assembles the callback which will be used
// assembleTransitionProvedIteratorCallback assembles the callback which will be used
// by a event iterator's inner block iterator.
func assembleBlockProvenIteratorCallback(
func assembleTransitionProvedIteratorCallback(
client *rpc.EthClient,
taikoL1Client *bindings.TaikoL1Client,
filterQuery []*big.Int,
callback OnBlockProvenEvent,
eventIter *BlockProvenIterator,
callback OnTransitionProved,
eventIter *TransitionProvedIterator,
) chainIterator.OnBlocksFunc {
return func(
ctx context.Context,
Expand All @@ -104,7 +111,7 @@ func assembleBlockProvenIteratorCallback(
endFunc chainIterator.EndIterFunc,
) error {
endHeight := end.Number.Uint64()
iter, err := taikoL1Client.FilterBlockProven(
iter, err := taikoL1Client.FilterTransitionProved(
&bind.FilterOpts{Start: start.Number.Uint64(), End: &endHeight, Context: ctx},
filterQuery,
)
Expand Down
1 change: 1 addition & 0 deletions pkg/rpc/methods.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ func (c *Client) ensureGenesisMatched(ctx context.Context) error {
&bind.FilterOpts{Start: stateVars.GenesisHeight, End: &stateVars.GenesisHeight, Context: ctxWithTimeout},
[]*big.Int{common.Big0},
nil,
nil,
)
if err != nil {
return err
Expand Down
14 changes: 7 additions & 7 deletions pkg/rpc/subscription.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ func SubscribeBlockVerified(
ch chan *bindings.TaikoL1ClientBlockVerified,
) event.Subscription {
return SubscribeEvent("BlockVerified", func(ctx context.Context) (event.Subscription, error) {
sub, err := taikoL1.WatchBlockVerified(nil, ch, nil, nil)
sub, err := taikoL1.WatchBlockVerified(nil, ch, nil, nil, nil)
if err != nil {
log.Error("Create TaikoL1.BlockVerified subscription error", "error", err)
return nil, err
Expand Down Expand Up @@ -81,15 +81,15 @@ func SubscribeXchainSynced(
})
}

// SubscribeBlockProven subscribes the protocol's BlockProven events.
func SubscribeBlockProven(
// SubscribeTransitionProved subscribes the protocol's TransitionProved events.
func SubscribeTransitionProved(
taikoL1 *bindings.TaikoL1Client,
ch chan *bindings.TaikoL1ClientBlockProven,
ch chan *bindings.TaikoL1ClientTransitionProved,
) event.Subscription {
return SubscribeEvent("BlockProven", func(ctx context.Context) (event.Subscription, error) {
sub, err := taikoL1.WatchBlockProven(nil, ch, nil)
return SubscribeEvent("TransitionProved", func(ctx context.Context) (event.Subscription, error) {
sub, err := taikoL1.WatchTransitionProved(nil, ch, nil)
if err != nil {
log.Error("Create TaikoL1.BlockProven subscription error", "error", err)
log.Error("Create TaikoL1.TransitionProved subscription error", "error", err)
return nil, err
}

Expand Down
6 changes: 3 additions & 3 deletions pkg/rpc/subscription_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,10 @@ func TestSubscribeSubscribeXchainSynced(t *testing.T) {
)
}

func TestSubscribeBlockProven(t *testing.T) {
require.NotNil(t, SubscribeBlockProven(
func TestSubscribeTransitionProved(t *testing.T) {
require.NotNil(t, SubscribeTransitionProved(
newTestClient(t).TaikoL1,
make(chan *bindings.TaikoL1ClientBlockProven, 1024)),
make(chan *bindings.TaikoL1ClientTransitionProved, 1024)),
)
}

Expand Down
2 changes: 1 addition & 1 deletion pkg/rpc/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ func NeedNewProof(
"📬 Block's proof has already been submitted by another prover",
"blockID", id,
"prover", transition.Prover,
"provenAt", transition.ProvenAt,
"timestamp", transition.Timestamp,
)

return false, nil
Expand Down

0 comments on commit e58ace7

Please sign in to comment.