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
  • Loading branch information
YoGhurt111 committed Jul 1, 2024
1 parent d86893c commit ae49b24
Show file tree
Hide file tree
Showing 11 changed files with 58 additions and 27 deletions.
2 changes: 2 additions & 0 deletions packages/taiko-client/prover/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,8 @@ func (p *Prover) initProofSubmitters(
JWT: p.cfg.RaikoJWT,
ProofType: proofProducer.ProofTypeSgx,
Dummy: p.cfg.Dummy,
RPC: p.rpc,
ProverSetAddress: p.cfg.ProverSetAddress,
}
case encoding.TierGuardianMinorityID:
producer = proofProducer.NewGuardianProofProducer(encoding.TierGuardianMinorityID, p.cfg.EnableLivenessBondProof)
Expand Down
4 changes: 2 additions & 2 deletions packages/taiko-client/prover/proof_producer/dummy_producer.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,13 @@ func (o *DummyProofProducer) RequestProof(
meta *bindings.TaikoDataBlockMetadata,
header *types.Header,
tier uint16,
) (*ProofWithHeader, error) {
) (*ProofWithHeader, bool, error) {
return &ProofWithHeader{
BlockID: blockID,
Meta: meta,
Header: header,
Proof: bytes.Repeat([]byte{0xff}, 100),
Opts: opts,
Tier: tier,
}, nil
}, true, nil
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,15 @@ func TestDummyProducerRequestProof(t *testing.T) {
tier uint16 = 1024
blockID = common.Big32
)
res, err := producer.RequestProof(
res, needToSend, err := producer.RequestProof(
&ProofRequestOptions{},
blockID,
&bindings.TaikoDataBlockMetadata{},
header,
tier,
)
require.Nil(t, err)
require.True(t, needToSend)

require.Equal(t, res.BlockID, blockID)
require.Equal(t, res.Header, header)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ func (g *GuardianProofProducer) RequestProof(
blockID *big.Int,
meta *bindings.TaikoDataBlockMetadata,
header *types.Header,
) (*ProofWithHeader, error) {
) (*ProofWithHeader, bool, error) {
log.Info(
"Request guardian proof",
"blockID", blockID,
Expand All @@ -53,7 +53,7 @@ func (g *GuardianProofProducer) RequestProof(
Proof: crypto.Keccak256([]byte("RETURN_LIVENESS_BOND")),
Opts: opts,
Tier: g.tier,
}, nil
}, true, nil
}

return g.DummyProofProducer.RequestProof(opts, blockID, meta, header, g.Tier())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,14 +36,15 @@ func TestGuardianProducerRequestProof(t *testing.T) {
producer = NewGuardianProofProducer(encoding.TierGuardianMajorityID, false)
blockID = common.Big32
)
res, err := producer.RequestProof(
res, needToSend, err := producer.RequestProof(
context.Background(),
&ProofRequestOptions{},
blockID,
&bindings.TaikoDataBlockMetadata{},
header,
)
require.Nil(t, err)
require.True(t, needToSend)

require.Equal(t, res.BlockID, blockID)
require.Equal(t, res.Header, header)
Expand Down Expand Up @@ -73,14 +74,15 @@ func TestGuardianProducerRequestProofReturnLivenessBond(t *testing.T) {
producer = NewGuardianProofProducer(encoding.TierGuardianMajorityID, true)
blockID = common.Big32
)
res, err := producer.RequestProof(
res, needToSend, err := producer.RequestProof(
context.Background(),
&ProofRequestOptions{},
blockID,
&bindings.TaikoDataBlockMetadata{},
header,
)
require.Nil(t, err)
require.True(t, needToSend)

require.Equal(t, res.BlockID, blockID)
require.Equal(t, res.Header, header)
Expand Down Expand Up @@ -111,14 +113,15 @@ func TestMinorityRequestProof(t *testing.T) {
producer = NewGuardianProofProducer(encoding.TierGuardianMinorityID, false)
blockID = common.Big32
)
res, err := producer.RequestProof(
res, needToSend, err := producer.RequestProof(
context.Background(),
&ProofRequestOptions{},
blockID,
&bindings.TaikoDataBlockMetadata{},
header,
)
require.Nil(t, err)
require.True(t, needToSend)

require.Equal(t, res.BlockID, blockID)
require.Equal(t, res.Header, header)
Expand Down Expand Up @@ -148,14 +151,15 @@ func TestRequestMinorityProofReturnLivenessBond(t *testing.T) {
producer = NewGuardianProofProducer(encoding.TierGuardianMinorityID, true)
blockID = common.Big32
)
res, err := producer.RequestProof(
res, needToSend, err := producer.RequestProof(
context.Background(),
&ProofRequestOptions{},
blockID,
&bindings.TaikoDataBlockMetadata{},
header,
)
require.Nil(t, err)
require.True(t, needToSend)

require.Equal(t, res.BlockID, blockID)
require.Equal(t, res.Header, header)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ func (o *OptimisticProofProducer) RequestProof(
blockID *big.Int,
meta *bindings.TaikoDataBlockMetadata,
header *types.Header,
) (*ProofWithHeader, error) {
) (*ProofWithHeader, bool, error) {
log.Info(
"Request optimistic proof",
"blockID", blockID,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,14 +37,15 @@ func TestOptimisticRequestProof(t *testing.T) {
producer = &OptimisticProofProducer{}
blockID = common.Big32
)
res, err := producer.RequestProof(
res, needToSend, err := producer.RequestProof(
context.Background(),
&ProofRequestOptions{},
blockID,
&bindings.TaikoDataBlockMetadata{},
header,
)
require.Nil(t, err)
require.True(t, needToSend)

require.Equal(t, res.BlockID, blockID)
require.Equal(t, res.Header, header)
Expand Down Expand Up @@ -74,14 +75,15 @@ func TestProofCancel(t *testing.T) {
optimisticProofProducer = &OptimisticProofProducer{}
blockID = common.Big32
)
_, err := optimisticProofProducer.RequestProof(
_, needToSend, err := optimisticProofProducer.RequestProof(
context.Background(),
&ProofRequestOptions{},
blockID,
&bindings.TaikoDataBlockMetadata{},
header,
)
require.Nil(t, err)
require.True(t, needToSend)
}

func randHash() common.Hash {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,6 @@ type ProofProducer interface {
blockID *big.Int,
meta *bindings.TaikoDataBlockMetadata,
header *types.Header,
) (*ProofWithHeader, error)
) (*ProofWithHeader, bool, error)
Tier() uint16
}
38 changes: 29 additions & 9 deletions packages/taiko-client/prover/proof_producer/sgx_producer.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ import (
"net/http"
"time"

"github.com/taikoxyz/taiko-mono/packages/taiko-client/pkg/rpc"

"github.com/cenkalti/backoff/v4"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/types"
Expand All @@ -32,6 +34,8 @@ type SGXProofProducer struct {
ProofType string // Proof type
JWT string // JWT provided by Raiko
Dummy bool
RPC *rpc.Client
ProverSetAddress common.Address
DummyProofProducer
}

Expand Down Expand Up @@ -77,7 +81,7 @@ func (s *SGXProofProducer) RequestProof(
blockID *big.Int,
meta *bindings.TaikoDataBlockMetadata,
header *types.Header,
) (*ProofWithHeader, error) {
) (*ProofWithHeader, bool, error) {
log.Info(
"Request proof from raiko-host service",
"blockID", blockID,
Expand All @@ -90,9 +94,9 @@ func (s *SGXProofProducer) RequestProof(
return s.DummyProofProducer.RequestProof(opts, blockID, meta, header, s.Tier())
}

proof, err := s.callProverDaemon(ctx, opts)
proof, needToSend, err := s.callProverDaemon(ctx, opts)
if err != nil {
return nil, err
return nil, false, err
}

metrics.ProverSgxProofGeneratedCounter.Add(1)
Expand All @@ -104,19 +108,35 @@ func (s *SGXProofProducer) RequestProof(
Proof: proof,
Opts: opts,
Tier: s.Tier(),
}, nil
}, needToSend, nil
}

// callProverDaemon keeps polling the proverd service to get the requested proof.
func (s *SGXProofProducer) callProverDaemon(ctx context.Context, opts *ProofRequestOptions) ([]byte, error) {
func (s *SGXProofProducer) callProverDaemon(ctx context.Context, opts *ProofRequestOptions) ([]byte, bool, error) {
var (
proof []byte
start = time.Now()
proof []byte
start = time.Now()
needToSend = true
)
if err := backoff.Retry(func() error {
if ctx.Err() != nil {
return nil
}
// 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 {
needToSend = false
return nil
}
output, err := s.requestProof(opts)
if err != nil {
log.Error("Failed to request proof", "height", opts.BlockID, "error", err, "endpoint", s.RaikoHostEndpoint)
Expand Down Expand Up @@ -151,10 +171,10 @@ func (s *SGXProofProducer) callProverDaemon(ctx context.Context, opts *ProofRequ
)
return nil
}, backoff.WithContext(backoff.NewConstantBackOff(proofPollingInterval), ctx)); err != nil {
return nil, err
return nil, false, err
}

return proof, nil
return proof, needToSend, nil
}

// requestProof sends a RPC request to proverd to try to get the requested proof.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,15 @@ func TestSGXProducerRequestProof(t *testing.T) {
producer = &SGXProofProducer{Dummy: true}
blockID = common.Big32
)
res, err := producer.RequestProof(
res, needToSend, err := producer.RequestProof(
context.Background(),
&ProofRequestOptions{},
blockID,
&bindings.TaikoDataBlockMetadata{},
header,
)
require.Nil(t, err)
require.True(t, needToSend)

require.Equal(t, res.BlockID, blockID)
require.Equal(t, res.Header, header)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ func (s *ProofSubmitter) RequestProof(ctx context.Context, event *bindings.Taiko
}

// Send the generated proof.
result, err := s.proofProducer.RequestProof(
result, needToSend, err := s.proofProducer.RequestProof(
ctx,
opts,
event.BlockId,
Expand All @@ -136,10 +136,11 @@ func (s *ProofSubmitter) RequestProof(ctx context.Context, event *bindings.Taiko
if err != nil {
return fmt.Errorf("failed to request proof (id: %d): %w", event.BlockId, err)
}
s.resultCh <- result

metrics.ProverQueuedProofCounter.Add(1)
if needToSend {
s.resultCh <- result

metrics.ProverQueuedProofCounter.Add(1)
}
return nil
}

Expand Down

0 comments on commit ae49b24

Please sign in to comment.