From 7cc5d541bef0eac9078bc93eb5f1d9954b164e9b Mon Sep 17 00:00:00 2001 From: David Date: Tue, 30 May 2023 04:00:28 +0800 Subject: [PATCH] feat(prover): improve proof submission delay calculation (#249) --- driver/state/state.go | 13 +++---- prover/proof_producer/zkevm_cmd_producer.go | 2 +- prover/proof_producer/zkevm_rpcd_producer.go | 2 +- prover/proof_submitter/util.go | 36 ++++++++++++++------ prover/prover.go | 1 - 5 files changed, 32 insertions(+), 22 deletions(-) diff --git a/driver/state/state.go b/driver/state/state.go index 2e789bc33..d6deea238 100644 --- a/driver/state/state.go +++ b/driver/state/state.go @@ -13,6 +13,7 @@ import ( "github.com/ethereum/go-ethereum/event" "github.com/ethereum/go-ethereum/log" "github.com/taikoxyz/taiko-client/bindings" + "github.com/taikoxyz/taiko-client/bindings/encoding" "github.com/taikoxyz/taiko-client/metrics" "github.com/taikoxyz/taiko-client/pkg/rpc" ) @@ -167,17 +168,11 @@ func (s *State) startSubscriptions(ctx context.Context) { case e := <-s.blockProposedCh: s.setHeadBlockID(e.Id) case e := <-s.blockProvenCh: - if e.BlockHash != s.BlockDeadendHash { - log.Info("✅ Valid block proven", "blockID", e.Id, "hash", common.Hash(e.BlockHash), "prover", e.Prover) - } else { - log.Info("❎ Invalid block proven", "blockID", e.Id, "prover", e.Prover) + if e.Prover != encoding.SystemProverAddress && e.Prover != encoding.OracleProverAddress { + log.Info("✅ Block proven", "blockID", e.Id, "hash", common.Hash(e.BlockHash), "prover", e.Prover) } case e := <-s.blockVerifiedCh: - if e.BlockHash != s.BlockDeadendHash { - log.Info("📈 Valid block verified", "blockID", e.Id, "hash", common.Hash(e.BlockHash)) - } else { - log.Info("🗑 Invalid block verified", "blockID", e.Id) - } + log.Info("📈 Block verified", "blockID", e.Id, "hash", common.Hash(e.BlockHash)) case e := <-s.crossChainSynced: // Verify the protocol synced block, check if it exists in // L2 execution engine. diff --git a/prover/proof_producer/zkevm_cmd_producer.go b/prover/proof_producer/zkevm_cmd_producer.go index 130a85a4c..dc8348aee 100644 --- a/prover/proof_producer/zkevm_cmd_producer.go +++ b/prover/proof_producer/zkevm_cmd_producer.go @@ -160,6 +160,6 @@ func (p *ZkevmCmdProducer) outputToCalldata(output *ProverCmdOutput) []byte { // Right now, it is just a stub that does nothing, because it is not possible to cnacel the proof // with the current zkevm software. func (p *ZkevmCmdProducer) Cancel(ctx context.Context, blockID *big.Int) error { - log.Info("Cancel proof generation for block ", "blockId", blockID) + log.Info("Cancel proof generation for block", "blockId", blockID) return nil } diff --git a/prover/proof_producer/zkevm_rpcd_producer.go b/prover/proof_producer/zkevm_rpcd_producer.go index 9678288a2..7140b92ee 100644 --- a/prover/proof_producer/zkevm_rpcd_producer.go +++ b/prover/proof_producer/zkevm_rpcd_producer.go @@ -250,6 +250,6 @@ func (p *ZkevmRpcdProducer) requestProof(opts *ProofRequestOptions) (*RpcdOutput // Right now, it is just a stub that does nothing, because it is not possible to cnacel the proof // with the current zkevm software. func (p *ZkevmRpcdProducer) Cancel(ctx context.Context, blockID *big.Int) error { - log.Info("Cancel proof generation for block ", "blockId", blockID) + log.Info("Cancel proof generation for block", "blockId", blockID) return nil } diff --git a/prover/proof_submitter/util.go b/prover/proof_submitter/util.go index 5b4243104..0c558289b 100644 --- a/prover/proof_submitter/util.go +++ b/prover/proof_submitter/util.go @@ -28,8 +28,7 @@ var ( // is retryable. func isSubmitProofTxErrorRetryable(err error, blockID *big.Int) bool { if strings.HasPrefix(err.Error(), "L1_NOT_SPECIAL_PROVER") || - !strings.HasPrefix(err.Error(), "L1_") || - errors.Is(err, errNeedWaiting) { + !strings.HasPrefix(err.Error(), "L1_") { return true } @@ -72,7 +71,11 @@ func sendTxWithBackoff( expectedReward uint64, sendTxFunc func() (*types.Transaction, error), ) error { - var isUnretryableError bool + var ( + isUnretryableError bool + proposedTime = time.Unix(int64(proposedAt), 0) + ) + if err := backoff.Retry(func() error { if ctx.Err() != nil { return nil @@ -91,21 +94,34 @@ func sendTxWithBackoff( } if needNewProof { - proofTime := uint64(time.Now().Unix()) - (proposedAt) - reward, err := cli.TaikoL1.GetProofReward(nil, proofTime) + stateVar, err := cli.TaikoL1.GetStateVariables(nil) if err != nil { - log.Warn("Failed to get proof reward", "blockID", blockID, "proofTime", proofTime, "error", err) + log.Warn("Failed to get protocol state variables", "blockID", blockID, "error", err) return err } + targetDelay := stateVar.ProofTimeTarget * 4 + if stateVar.BlockFee != 0 { + targetDelay = expectedReward / stateVar.BlockFee * stateVar.ProofTimeTarget + if targetDelay < stateVar.ProofTimeTarget/4 { + targetDelay = stateVar.ProofTimeTarget / 4 + } else if targetDelay > stateVar.ProofTimeTarget*4 { + targetDelay = stateVar.ProofTimeTarget * 4 + } + } + log.Info( - "Current proof reward", - "currentReward", reward, + "Target delay", + "blockID", blockID, + "delay", targetDelay, "expectedReward", expectedReward, - "needWaiting", reward < expectedReward, + "blockFee", stateVar.BlockFee, + "proofTimeTarget", stateVar.ProofTimeTarget, + "proposedTime", proposedTime, + "timeToWait", time.Until(proposedTime.Add(time.Duration(targetDelay)*time.Second)), ) - if reward < expectedReward { + if time.Now().Before(proposedTime.Add(time.Duration(targetDelay) * time.Second)) { return errNeedWaiting } } diff --git a/prover/prover.go b/prover/prover.go index 5fb4a69e4..91616cc94 100644 --- a/prover/prover.go +++ b/prover/prover.go @@ -513,6 +513,5 @@ func (p *Prover) cancelProof(ctx context.Context, blockID uint64) { if cancel, ok := p.currentBlocksBeingProven[blockID]; ok { cancel() delete(p.currentBlocksBeingProven, blockID) - log.Info("Cancelled proof for ", "blockID", blockID) } }