-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: update tryFinalize()
to check finality on each L2 block
#23
Conversation
updated to fix all comments ^ |
@lesterli let's make sure we run all the commands in test plan since we have disabled CI for now. I just found that |
op-node/rollup/finality/finalizer.go
Outdated
lastFinalizedBlock, err := fi.findFinalizedL2Block(fd.L2Block.Number, finalizedL2.Number) | ||
if err != nil { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this confused me a bit b/c when there is err but we don't log it here. only after digging into the code, I see that it's already emitted inside findFinalizedL2Block
so given that we only need to check "where there is error", a better API design is to return a bool hasErr
. wdyt?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
also findFinalizedL2Block
should be renamed to sth like findLastFinalizedL2BlockWithConsecutiveQuorom
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
fixed in 38f6625
op-node/rollup/finality/finalizer.go
Outdated
if err != nil { | ||
if lastFinalizedBlock != (eth.L2BlockRef{}) { | ||
// set finalized block(s) | ||
finalizedL2, finalizedDerivedFrom = fi.updateFinalized(lastFinalizedBlock, fd.L1Block) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
should we use lastFinalizedBlock.L1Origin
here instead of fd.L1Block
?
b/c lastFinalizedBlock
might not be the same as fd.L2Block.Number
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this is probably another edge case that test coverage failed. we should have a test case for this where fd.L1Block is different from lastFinalizedBlock.L1Origin
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
fixed in 38f6625
op-node/rollup/finality/finalizer.go
Outdated
break | ||
} | ||
// set finalized block(s) | ||
finalizedL2, finalizedDerivedFrom = fi.updateFinalized(lastFinalizedBlock, fd.L1Block) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this is the same call as in L252. i think the overall logic from line 249-264 is not very clean
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
same for the if lastFinalizedBlock == (eth.L2BlockRef{}) {
check. we checked it twice
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
fixed in 38f6625
op-node/rollup/finality/finalizer.go
Outdated
if err != nil && !errors.Is(err, sdk.ErrNoFpHasVotingPower) { | ||
fi.emitter.Emit(rollup.CriticalErrorEvent{Err: fmt.Errorf("failed to check if block %d is finalized on Babylon: %w", fd.L2Block.Number, err)}) | ||
|
||
if hasErr { | ||
return |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
thinking deeper on it. "return on err" is inappropriate now. if you see the below logic where if finalizedDerivedFrom != (eth.BlockID{}) {
is checked, whenever finalizedDerivedFrom
is set, we have to do some sanity checks.
so we need to:
- remove the "return on error" logic
findLastFinalizedL2BlockWithConsecutiveQuorom
doesn't need to returnhasErr
now
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
fixed in b28870d
op-node/rollup/finality/finalizer.go
Outdated
@@ -300,6 +292,80 @@ func (fi *Finalizer) tryFinalize() { | |||
} | |||
} | |||
|
|||
/* | |||
* findLastFinalizedL2BlockWithConsecutiveQuorom tries to find the finalized L2 block. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
tries to find the last finalized L2 block with consecutive quorom
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
fixed in c89dd55
op-node/rollup/finality/finalizer.go
Outdated
* for any other error types, emit an critical error event. | ||
*/ | ||
func (fi *Finalizer) findLastFinalizedL2BlockWithConsecutiveQuorom( | ||
fdL2BlockNumber uint64, | ||
finalizedL2Number uint64, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
let's add some comments to explain the return value and those arguments
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
fixed in c89dd55
op-node/rollup/finality/finalizer.go
Outdated
return eth.L2BlockRef{} | ||
} | ||
|
||
return l2Blocks[*lastFinalizedBlockNumber] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
what if lastFinalizedBlockNumber
is nil?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
fixed in c89dd55
op-node/rollup/finality/finalizer.go
Outdated
func (fi *Finalizer) findLastFinalizedL2BlockWithConsecutiveQuorom( | ||
fdL2BlockNumber uint64, | ||
finalizedL2Number uint64, | ||
) eth.L2BlockRef { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
why not return a pointer?
i think using a pointer type will be cleaner so the check above
if lastFinalizedBlock.Number != fd.L2Block.Number {
will be changed to
if lastFinalizedBlock == nil || lastFinalizedBlock.Number != fd.L2Block.Number {
so it's more explicit
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
fixed in c89dd55
op-node/rollup/finality/finalizer.go
Outdated
// https://github.com/babylonchain/babylon-finality-gadget/issues/59 | ||
if err != nil && !errors.Is(err, sdkclient.ErrNoFpHasVotingPower) { | ||
if lastFinalizedBlockNumber != nil { | ||
fi.log.Warn("Received error but also had the finalized block", "error", err, "blockNumber", *lastFinalizedBlockNumber) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
the logic here is not clean for the same reason that we have too many unnecessary nesting and condition checks
it can be simplified to be:
- if there is error, emit
- if last finalized block is not nil, return the block
- else return nil
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
fixed in c89dd55
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
given that this PR is already way too big. let's move all the changes in finalizer_test.go to another PR so we can start new discussion threads there
also the tests seem still failing
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ok I fixed all the comments above. @lesterli you can create a new PR to fix test. it's currently failing. I copied the code of finalizer_test.go
to https://gist.github.com/bap2pecs/65e97276d3253e3973f138ab84878e74
I am gonna merge this PR now. I tested against FP e2e tests and all passed
let's keep the branch for now since I don't know if FP or SDK repo depends on some commits in this branch
Summary
This PR updates
finalizer.go
to check finality on each L2 block,tryFinalize()
doesn't call on each L2 block b/conPendingSafeUpdate
only triggered upon a new L1 block.FinalizerL2Interface
interface which has the functionL2BlockRefByNumber(ctx context.Context, num uint64) (eth.L2BlockRef, error)
NewFinalizer
to add this interface as its input parameterNewFinalizer
and pass an instance of theFinalizerL2Interface
interfaceTest Plan
connect w FP e2e tests locally and see it passed