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

Commit

Permalink
feat(prover): update onBlockProven handler
Browse files Browse the repository at this point in the history
  • Loading branch information
davidtaikocha committed Oct 6, 2023
1 parent 9744475 commit 66f2701
Show file tree
Hide file tree
Showing 2 changed files with 93 additions and 23 deletions.
2 changes: 1 addition & 1 deletion integration_test/nodes/docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ services:
- "0.0.0.0"

l2_execution_engine:
image: gcr.io/evmchain/taiko-geth:taiko
image: gcr.io/evmchain/taiko-geth:sha-6a5990c
restart: unless-stopped
pull_policy: always
volumes:
Expand Down
114 changes: 92 additions & 22 deletions prover/prover.go
Original file line number Diff line number Diff line change
Expand Up @@ -260,16 +260,16 @@ func (p *Prover) eventLoop() {
if err := p.proveOp(); err != nil {
log.Error("Prove new blocks error", "error", err)
}
case <-p.blockProposedCh:
reqProving()
case e := <-p.blockVerifiedCh:
if err := p.onBlockVerified(p.ctx, e); err != nil {
log.Error("Handle BlockVerified event error", "error", err)
}
case e := <-p.transitionProvedCh:
if err := p.onBlockProven(p.ctx, e); err != nil {
log.Error("Handle BlockProven event error", "error", err)
if err := p.onTransitionProved(p.ctx, e); err != nil {
log.Error("Handle TransitionProved event error", "error", err)
}
case <-p.blockProposedCh:
reqProving()
case <-forceProvingTicker.C:
reqProving()
}
Expand Down Expand Up @@ -674,26 +674,96 @@ func (p *Prover) isValidProof(
return false, nil
}

// onBlockProven cancels proof generation if the proof is being generated by this prover,
// and the proof is not the guardian proof address.
func (p *Prover) onBlockProven(ctx context.Context, event *bindings.TaikoL1ClientTransitionProved) error {
func (p *Prover) onTransitionProved(ctx context.Context, event *bindings.TaikoL1ClientTransitionProved) error {
metrics.ProverReceivedProvenBlockGauge.Update(event.BlockId.Int64())

// cancel any proofs being generated for this block
// isValidProof, err := p.isValidProof(
// ctx,
// event.BlockId.Uint64(),
// event.ParentHash,
// event.BlockHash,
// )
// if err != nil {
// return err
// }

// // generate guardian proof if guardian prover, proof is invalid
// if p.cfg.GuardianProver {
// return p.requestProofForBlockId(event.BlockId, new(big.Int).SetUint64(event.Raw.BlockNumber))
// }
if !p.cfg.GuardianProver {
return nil
}

isValidProof, err := p.isValidProof(
ctx,
event.BlockId.Uint64(),
event.ParentHash,
event.BlockHash,
)
if err != nil {
return err
}

if isValidProof {
return nil
}

L1Height, err := p.rpc.TaikoL2.LatestSyncedL1Height(&bind.CallOpts{Context: ctx, BlockNumber: event.BlockId})
if err != nil {
return err
}

return p.requestProofByBlockID(event.BlockId, new(big.Int).SetUint64(L1Height))
}

// requestProofByBlockID performs a proving operation for the given block.
func (p *Prover) requestProofByBlockID(blockId *big.Int, l1Height *big.Int) error {
onBlockProposed := func(
ctx context.Context,
event *bindings.TaikoL1ClientBlockProposed,
end eventIterator.EndBlockProposedEventIterFunc,
) error {
// Only filter for exact blockID we want
if event.BlockId.Cmp(blockId) != 0 {
return nil
}

// Check whether the block has been verified.
isVerified, err := p.isBlockVerified(event.BlockId)
if err != nil {
return fmt.Errorf("failed to check if the current L2 block is verified: %w", err)
}

if isVerified {
log.Info("📋 Block has been verified", "blockID", event.BlockId)
return nil
}

// make sure to takea capacity before requesting proof
if !p.cfg.GuardianProver {
if _, ok := p.capacityManager.TakeOneCapacity(event.BlockId.Uint64()); !ok {
return errNoCapacity
}
}

p.proposeConcurrencyGuard <- struct{}{}

return p.validProofSubmitter.RequestProof(ctx, event)
}

handleBlockProposedEvent := func() error {
defer func() { <-p.proposeConcurrencyGuard }()

iter, err := eventIterator.NewBlockProposedIterator(p.ctx, &eventIterator.BlockProposedIteratorConfig{
Client: p.rpc.L1,
TaikoL1: p.rpc.TaikoL1,
StartHeight: l1Height,
EndHeight: new(big.Int).Add(l1Height, common.Big1),
OnBlockProposedEvent: onBlockProposed,
FilterQuery: []*big.Int{blockId},
})
if err != nil {
return err
}

return iter.Iter()
}

go func() {
if err := backoff.Retry(
func() error { return handleBlockProposedEvent() },
backoff.WithMaxRetries(backoff.NewConstantBackOff(p.cfg.BackOffRetryInterval), p.cfg.BackOffMaxRetrys),
); err != nil {
log.Error("Failed to request proof with a given block ID", "blockID", blockId, "error", err)
}
}()

return nil
}

0 comments on commit 66f2701

Please sign in to comment.