diff --git a/prover/proof_producer/dummy_producer.go b/prover/proof_producer/dummy_producer.go index 26d056c36..faa19d912 100644 --- a/prover/proof_producer/dummy_producer.go +++ b/prover/proof_producer/dummy_producer.go @@ -21,9 +21,8 @@ func (o *DummyProofProducer) RequestProof( meta *bindings.TaikoDataBlockMetadata, header *types.Header, tier uint16, - resultCh chan *ProofWithHeader, -) error { - resultCh <- &ProofWithHeader{ +) (*ProofWithHeader, error) { + return &ProofWithHeader{ BlockID: blockID, Meta: meta, Header: header, @@ -31,7 +30,5 @@ func (o *DummyProofProducer) RequestProof( Degree: CircuitsIdx, Opts: opts, Tier: tier, - } - - return nil + }, nil } diff --git a/prover/proof_producer/dummy_producer_test.go b/prover/proof_producer/dummy_producer_test.go index f3cdcc10d..0af86eb77 100644 --- a/prover/proof_producer/dummy_producer_test.go +++ b/prover/proof_producer/dummy_producer_test.go @@ -13,13 +13,6 @@ import ( ) func TestDummyProducerRequestProof(t *testing.T) { - producer := &DummyProofProducer{} - - resCh := make(chan *ProofWithHeader, 1) - - var tier uint16 = 1024 - - blockID := common.Big32 header := &types.Header{ ParentHash: randHash(), UncleHash: randHash(), @@ -36,17 +29,22 @@ func TestDummyProducerRequestProof(t *testing.T) { MixDigest: randHash(), Nonce: types.BlockNonce{}, } - require.Nil(t, producer.RequestProof( + + var ( + producer = &DummyProofProducer{} + tier uint16 = 1024 + blockID = common.Big32 + ) + res, err := producer.RequestProof( context.Background(), &ProofRequestOptions{}, blockID, &bindings.TaikoDataBlockMetadata{}, header, tier, - resCh, - )) + ) + require.Nil(t, err) - res := <-resCh require.Equal(t, res.BlockID, blockID) require.Equal(t, res.Header, header) require.Equal(t, tier, res.Tier) diff --git a/prover/proof_producer/guardian_producer.go b/prover/proof_producer/guardian_producer.go index 770d60f30..bd90cbd57 100644 --- a/prover/proof_producer/guardian_producer.go +++ b/prover/proof_producer/guardian_producer.go @@ -22,8 +22,7 @@ func (g *GuardianProofProducer) RequestProof( blockID *big.Int, meta *bindings.TaikoDataBlockMetadata, header *types.Header, - resultCh chan *ProofWithHeader, -) error { +) (*ProofWithHeader, error) { log.Info( "Request guardian proof", "blockID", blockID, @@ -32,7 +31,7 @@ func (g *GuardianProofProducer) RequestProof( "hash", header.Hash(), ) - return g.DummyProofProducer.RequestProof(ctx, opts, blockID, meta, header, g.Tier(), resultCh) + return g.DummyProofProducer.RequestProof(ctx, opts, blockID, meta, header, g.Tier()) } // Tier implements the ProofProducer interface. diff --git a/prover/proof_producer/guardian_producer_test.go b/prover/proof_producer/guardian_producer_test.go index 72e19ebb1..0bcd24c13 100644 --- a/prover/proof_producer/guardian_producer_test.go +++ b/prover/proof_producer/guardian_producer_test.go @@ -14,11 +14,6 @@ import ( ) func TestGuardianProducerRequestProof(t *testing.T) { - producer := &GuardianProofProducer{} - - resCh := make(chan *ProofWithHeader, 1) - - blockID := common.Big32 header := &types.Header{ ParentHash: randHash(), UncleHash: randHash(), @@ -35,16 +30,20 @@ func TestGuardianProducerRequestProof(t *testing.T) { MixDigest: randHash(), Nonce: types.BlockNonce{}, } - require.Nil(t, producer.RequestProof( + + var ( + producer = &GuardianProofProducer{} + blockID = common.Big32 + ) + res, err := producer.RequestProof( context.Background(), &ProofRequestOptions{}, blockID, &bindings.TaikoDataBlockMetadata{}, header, - resCh, - )) + ) + require.Nil(t, err) - res := <-resCh require.Equal(t, res.BlockID, blockID) require.Equal(t, res.Header, header) require.Equal(t, res.Tier, encoding.TierGuardianID) diff --git a/prover/proof_producer/optimistic_producer.go b/prover/proof_producer/optimistic_producer.go index 7815af584..db050f9e7 100644 --- a/prover/proof_producer/optimistic_producer.go +++ b/prover/proof_producer/optimistic_producer.go @@ -21,8 +21,7 @@ func (o *OptimisticProofProducer) RequestProof( blockID *big.Int, meta *bindings.TaikoDataBlockMetadata, header *types.Header, - resultCh chan *ProofWithHeader, -) error { +) (*ProofWithHeader, error) { log.Info( "Request optimistic proof", "blockID", blockID, @@ -31,7 +30,7 @@ func (o *OptimisticProofProducer) RequestProof( "hash", header.Hash(), ) - return o.DummyProofProducer.RequestProof(ctx, opts, blockID, meta, header, o.Tier(), resultCh) + return o.DummyProofProducer.RequestProof(ctx, opts, blockID, meta, header, o.Tier()) } // Tier implements the ProofProducer interface. diff --git a/prover/proof_producer/optimistic_producer_test.go b/prover/proof_producer/optimistic_producer_test.go index 1b869b54c..62782865f 100644 --- a/prover/proof_producer/optimistic_producer_test.go +++ b/prover/proof_producer/optimistic_producer_test.go @@ -16,11 +16,6 @@ import ( ) func TestOptimisticRequestProof(t *testing.T) { - producer := &OptimisticProofProducer{} - - resCh := make(chan *ProofWithHeader, 1) - - blockID := common.Big32 header := &types.Header{ ParentHash: randHash(), UncleHash: randHash(), @@ -37,16 +32,20 @@ func TestOptimisticRequestProof(t *testing.T) { MixDigest: randHash(), Nonce: types.BlockNonce{}, } - require.Nil(t, producer.RequestProof( + + var ( + producer = &OptimisticProofProducer{} + blockID = common.Big32 + ) + res, err := producer.RequestProof( context.Background(), &ProofRequestOptions{}, blockID, &bindings.TaikoDataBlockMetadata{}, header, - resCh, - )) + ) + require.Nil(t, err) - res := <-resCh require.Equal(t, res.BlockID, blockID) require.Equal(t, res.Header, header) require.Equal(t, res.Tier, encoding.TierOptimisticID) @@ -54,11 +53,6 @@ func TestOptimisticRequestProof(t *testing.T) { } func TestProofCancel(t *testing.T) { - optimisticProofProducer := &OptimisticProofProducer{} - - resCh := make(chan *ProofWithHeader, 1) - - blockID := common.Big32 header := &types.Header{ ParentHash: randHash(), UncleHash: randHash(), @@ -75,14 +69,19 @@ func TestProofCancel(t *testing.T) { MixDigest: randHash(), Nonce: types.BlockNonce{}, } - require.Nil(t, optimisticProofProducer.RequestProof( + + var ( + optimisticProofProducer = &OptimisticProofProducer{} + blockID = common.Big32 + ) + _, err := optimisticProofProducer.RequestProof( context.Background(), &ProofRequestOptions{}, blockID, &bindings.TaikoDataBlockMetadata{}, header, - resCh, - )) + ) + require.Nil(t, err) // Cancel the proof request, should return nil require.Nil(t, optimisticProofProducer.Cancel(context.Background(), blockID)) diff --git a/prover/proof_producer/proof_producer.go b/prover/proof_producer/proof_producer.go index 44102b84f..0417415d0 100644 --- a/prover/proof_producer/proof_producer.go +++ b/prover/proof_producer/proof_producer.go @@ -49,8 +49,7 @@ type ProofProducer interface { blockID *big.Int, meta *bindings.TaikoDataBlockMetadata, header *types.Header, - resultCh chan *ProofWithHeader, - ) error + ) (*ProofWithHeader, error) Cancellable() bool Cancel(ctx context.Context, blockID *big.Int) error Tier() uint16 diff --git a/prover/proof_producer/sgx_and_zkevm_rpcd_producer.go b/prover/proof_producer/sgx_and_zkevm_rpcd_producer.go index 53d5bcfe0..453c73fff 100644 --- a/prover/proof_producer/sgx_and_zkevm_rpcd_producer.go +++ b/prover/proof_producer/sgx_and_zkevm_rpcd_producer.go @@ -25,8 +25,7 @@ func (o *SGXAndZkevmRpcdProducer) RequestProof( blockID *big.Int, meta *bindings.TaikoDataBlockMetadata, header *types.Header, - resultCh chan *ProofWithHeader, -) error { +) (*ProofWithHeader, error) { log.Info( "Request SGX+PSE proof", "blockID", blockID, @@ -35,30 +34,34 @@ func (o *SGXAndZkevmRpcdProducer) RequestProof( "hash", header.Hash(), ) - sgxProofCh := make(chan *ProofWithHeader, 1) - pseZkEvmProofCh := make(chan *ProofWithHeader, 1) - + proofs := make([][]byte, 2) g, ctx := errgroup.WithContext(ctx) g.Go(func() error { - return o.SGXProofProducer.RequestProof(ctx, opts, blockID, meta, header, sgxProofCh) + res, err := o.SGXProofProducer.RequestProof(ctx, opts, blockID, meta, header) + if err == nil { + proofs[0] = res.Proof + } + return err }) g.Go(func() error { - return o.ZkevmRpcdProducer.RequestProof(ctx, opts, blockID, meta, header, pseZkEvmProofCh) + res, err := o.ZkevmRpcdProducer.RequestProof(ctx, opts, blockID, meta, header) + if err == nil { + proofs[1] = res.Proof + } + return err }) if err := g.Wait(); err != nil { - return err + return nil, err } - resultCh <- &ProofWithHeader{ + return &ProofWithHeader{ BlockID: blockID, Meta: meta, Header: header, - Proof: append((<-sgxProofCh).Proof, (<-pseZkEvmProofCh).Proof...), + Proof: append(proofs[0], proofs[1]...), Opts: opts, Tier: o.Tier(), - } - - return nil + }, nil } // Tier implements the ProofProducer interface. diff --git a/prover/proof_producer/sgx_producer.go b/prover/proof_producer/sgx_producer.go index a8c38c83f..6e2a33558 100644 --- a/prover/proof_producer/sgx_producer.go +++ b/prover/proof_producer/sgx_producer.go @@ -84,8 +84,7 @@ func (s *SGXProofProducer) RequestProof( blockID *big.Int, meta *bindings.TaikoDataBlockMetadata, header *types.Header, - resultCh chan *ProofWithHeader, -) error { +) (*ProofWithHeader, error) { log.Info( "Request proof from raiko-host service", "blockID", blockID, @@ -95,15 +94,17 @@ func (s *SGXProofProducer) RequestProof( ) if s.DummyProofProducer != nil { - return s.DummyProofProducer.RequestProof(ctx, opts, blockID, meta, header, s.Tier(), resultCh) + return s.DummyProofProducer.RequestProof(ctx, opts, blockID, meta, header, s.Tier()) } proof, err := s.callProverDaemon(ctx, opts) if err != nil { - return err + return nil, err } - resultCh <- &ProofWithHeader{ + metrics.ProverSgxProofGeneratedCounter.Inc(1) + + return &ProofWithHeader{ BlockID: blockID, Header: header, Meta: meta, @@ -111,11 +112,7 @@ func (s *SGXProofProducer) RequestProof( Degree: 0, Opts: opts, Tier: s.Tier(), - } - - metrics.ProverSgxProofGeneratedCounter.Inc(1) - - return nil + }, nil } // callProverDaemon keeps polling the proverd service to get the requested proof. diff --git a/prover/proof_producer/sgx_producer_test.go b/prover/proof_producer/sgx_producer_test.go index da7fc38f0..ac5c1a28f 100644 --- a/prover/proof_producer/sgx_producer_test.go +++ b/prover/proof_producer/sgx_producer_test.go @@ -14,13 +14,6 @@ import ( ) func TestSGXProducerRequestProof(t *testing.T) { - producer := &SGXProofProducer{ - DummyProofProducer: &DummyProofProducer{}, - } - - resCh := make(chan *ProofWithHeader, 1) - - blockID := common.Big32 header := &types.Header{ ParentHash: randHash(), UncleHash: randHash(), @@ -37,16 +30,22 @@ func TestSGXProducerRequestProof(t *testing.T) { MixDigest: randHash(), Nonce: types.BlockNonce{}, } - require.Nil(t, producer.RequestProof( + + var ( + producer = &SGXProofProducer{ + DummyProofProducer: &DummyProofProducer{}, + } + blockID = common.Big32 + ) + res, err := producer.RequestProof( context.Background(), &ProofRequestOptions{}, blockID, &bindings.TaikoDataBlockMetadata{}, header, - resCh, - )) + ) + require.Nil(t, err) - res := <-resCh require.Equal(t, res.BlockID, blockID) require.Equal(t, res.Header, header) require.Equal(t, res.Tier, encoding.TierSgxID) diff --git a/prover/proof_producer/zkevm_rpcd_producer.go b/prover/proof_producer/zkevm_rpcd_producer.go index 9acf0c1fc..49f42bff4 100644 --- a/prover/proof_producer/zkevm_rpcd_producer.go +++ b/prover/proof_producer/zkevm_rpcd_producer.go @@ -151,8 +151,7 @@ func (p *ZkevmRpcdProducer) RequestProof( blockID *big.Int, meta *bindings.TaikoDataBlockMetadata, header *types.Header, - resultCh chan *ProofWithHeader, -) error { +) (*ProofWithHeader, error) { log.Info( "Request proof from zkevm-chain proverd service", "blockID", blockID, @@ -162,7 +161,7 @@ func (p *ZkevmRpcdProducer) RequestProof( ) if p.DummyProofProducer != nil { - return p.DummyProofProducer.RequestProof(ctx, opts, blockID, meta, header, p.Tier(), resultCh) + return p.DummyProofProducer.RequestProof(ctx, opts, blockID, meta, header, p.Tier()) } var ( @@ -176,14 +175,16 @@ func (p *ZkevmRpcdProducer) RequestProof( proof, degree, err = p.callProverDaemon(ctx, opts, meta) } if err != nil { - return err + return nil, err } if proof, err = encoding.EncodeZKEvmProof(proof); err != nil { - return err + return nil, err } - resultCh <- &ProofWithHeader{ + metrics.ProverPseProofGeneratedCounter.Inc(1) + + return &ProofWithHeader{ BlockID: blockID, Header: header, Meta: meta, @@ -191,11 +192,7 @@ func (p *ZkevmRpcdProducer) RequestProof( Degree: degree, Opts: opts, Tier: p.Tier(), - } - - metrics.ProverPseProofGeneratedCounter.Inc(1) - - return nil + }, nil } // callProverDaemon keeps polling the proverd service to get the requested proof. diff --git a/prover/proof_producer/zkevm_rpcd_producer_test.go b/prover/proof_producer/zkevm_rpcd_producer_test.go index d8345908e..d3c63ce12 100644 --- a/prover/proof_producer/zkevm_rpcd_producer_test.go +++ b/prover/proof_producer/zkevm_rpcd_producer_test.go @@ -28,8 +28,6 @@ func TestNewZkevmRpcdProducer(t *testing.T) { return []byte{0}, CircuitsIdx, nil } - resCh := make(chan *ProofWithHeader, 1) - blockID := common.Big32 header := &types.Header{ ParentHash: randHash(), @@ -48,16 +46,15 @@ func TestNewZkevmRpcdProducer(t *testing.T) { Nonce: types.BlockNonce{}, } - require.Nil(t, dummyZkevmRpcdProducer.RequestProof( + res, err := dummyZkevmRpcdProducer.RequestProof( context.Background(), &ProofRequestOptions{}, blockID, &bindings.TaikoDataBlockMetadata{}, header, - resCh, - )) + ) + require.Nil(t, err) - res := <-resCh require.Equal(t, res.BlockID, blockID) require.Equal(t, res.Header, header) require.NotEmpty(t, res.Proof) diff --git a/prover/proof_submitter/proof_submitter.go b/prover/proof_submitter/proof_submitter.go index be8b7b1d3..0cc3e13cb 100644 --- a/prover/proof_submitter/proof_submitter.go +++ b/prover/proof_submitter/proof_submitter.go @@ -157,16 +157,17 @@ func (s *ProofSubmitter) RequestProof(ctx context.Context, event *bindings.Taiko } // Send the generated proof. - if err := s.proofProducer.RequestProof( + result, err := s.proofProducer.RequestProof( ctx, opts, event.BlockId, &event.Meta, block.Header(), - s.resultCh, - ); err != nil { + ) + if err != nil { return fmt.Errorf("failed to request proof (id: %d): %w", event.BlockId, err) } + s.resultCh <- result metrics.ProverQueuedProofCounter.Inc(1)