Skip to content

Commit

Permalink
feat(taiko-client): add proof status check before generating proof (#…
Browse files Browse the repository at this point in the history
…17711)

Co-authored-by: David <david@taiko.xyz>
  • Loading branch information
YoGhurt111 and davidtaikocha committed Jul 3, 2024
1 parent 71eea3f commit 9a8e15e
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 49 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"context"
"errors"
"math/big"
"time"

"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/types"
Expand All @@ -13,8 +12,7 @@ import (
)

var (
proofPollingInterval = 10 * time.Second
errProofGenerating = errors.New("proof is generating")
errProofGenerating = errors.New("proof is generating")
)

// ProofRequestBody represents a request body to generate a proof.
Expand Down
63 changes: 29 additions & 34 deletions packages/taiko-client/prover/proof_producer/sgx_producer.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import (
"net/http"
"time"

"github.com/cenkalti/backoff/v4"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/log"
Expand Down Expand Up @@ -113,47 +112,43 @@ func (s *SGXProofProducer) callProverDaemon(ctx context.Context, opts *ProofRequ
proof []byte
start = time.Now()
)
if err := backoff.Retry(func() error {
if ctx.Err() != nil {
return nil
}
output, err := s.requestProof(opts)
if err != nil {
log.Error("Failed to request proof", "height", opts.BlockID, "error", err, "endpoint", s.RaikoHostEndpoint)
return err
}

if output == nil {
log.Info(
"Proof generating",
"height", opts.BlockID,
"time", time.Since(start),
"producer", "SGXProofProducer",
)
return errProofGenerating
}

log.Debug("Proof generation output", "output", output)

// Raiko returns "" as proof when proof type is native,
// so we just convert "" to bytes
if s.ProofType == ProofTypeCPU {
proof = common.Hex2Bytes(output.Data.Proof)
} else {
proof = common.Hex2Bytes(output.Data.Proof[2:])
}

if ctx.Err() != nil {
return nil, ctx.Err()
}
output, err := s.requestProof(opts)
if err != nil {
log.Error("Failed to request proof", "height", opts.BlockID, "error", err, "endpoint", s.RaikoHostEndpoint)
return nil, err
}

if output == nil {
log.Info(
"Proof generated",
"Proof generating",
"height", opts.BlockID,
"time", time.Since(start),
"producer", "SGXProofProducer",
)
return nil
}, backoff.WithContext(backoff.NewConstantBackOff(proofPollingInterval), ctx)); err != nil {
return nil, err
return nil, errProofGenerating
}

log.Debug("Proof generation output", "output", output)

// Raiko returns "" as proof when proof type is native,
// so we just convert "" to bytes
if s.ProofType == ProofTypeCPU {
proof = common.Hex2Bytes(output.Data.Proof)
} else {
proof = common.Hex2Bytes(output.Data.Proof[2:])
}

log.Info(
"Proof generated",
"height", opts.BlockID,
"time", time.Since(start),
"producer", "SGXProofProducer",
)

return proof, nil
}

Expand Down
48 changes: 36 additions & 12 deletions packages/taiko-client/prover/proof_submitter/proof_submitter.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"math/big"
"time"

"github.com/cenkalti/backoff/v4"
"github.com/ethereum-optimism/optimism/op-service/txmgr"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/types"
Expand All @@ -25,6 +26,7 @@ import (
var (
_ Submitter = (*ProofSubmitter)(nil)
submissionDelayRandomBumpRange float64 = 20
proofPollingInterval = 10 * time.Second
)

// ProofSubmitter is responsible requesting proofs for the given L2
Expand Down Expand Up @@ -126,19 +128,41 @@ func (s *ProofSubmitter) RequestProof(ctx context.Context, event *bindings.Taiko
}

// Send the generated proof.
result, err := s.proofProducer.RequestProof(
ctx,
opts,
event.BlockId,
&event.Meta,
header,
)
if err != nil {
return fmt.Errorf("failed to request proof (id: %d): %w", event.BlockId, err)
if err := backoff.Retry(
func() error {
// Check if there is a need to generate proof
proofStatus, err := rpc.GetBlockProofStatus(
ctx,
s.rpc,
opts.BlockID,
opts.ProverAddress,
s.proverSetAddress,
)
if err != nil {
return err
}
if proofStatus.IsSubmitted && !proofStatus.Invalid {
return nil
}

result, err := s.proofProducer.RequestProof(
ctx,
opts,
event.BlockId,
&event.Meta,
header,
)
if err != nil {
return fmt.Errorf("failed to request proof (id: %d): %w", event.BlockId, err)
}
s.resultCh <- result
metrics.ProverQueuedProofCounter.Add(1)
return nil
},
backoff.WithContext(backoff.NewConstantBackOff(proofPollingInterval), ctx),
); err != nil {
log.Error("Request proof error", "error", err)
}
s.resultCh <- result

metrics.ProverQueuedProofCounter.Add(1)

return nil
}
Expand Down

0 comments on commit 9a8e15e

Please sign in to comment.