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

Commit

Permalink
feat(prover): remove result channel in proof_producer.go (#516)
Browse files Browse the repository at this point in the history
  • Loading branch information
mask-pp committed Jan 21, 2024
1 parent d7aad5a commit 46779ca
Show file tree
Hide file tree
Showing 13 changed files with 89 additions and 105 deletions.
9 changes: 3 additions & 6 deletions prover/proof_producer/dummy_producer.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,17 +21,14 @@ 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,
Proof: bytes.Repeat([]byte{0xff}, 100),
Degree: CircuitsIdx,
Opts: opts,
Tier: tier,
}

return nil
}, nil
}
20 changes: 9 additions & 11 deletions prover/proof_producer/dummy_producer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand All @@ -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)
Expand Down
5 changes: 2 additions & 3 deletions prover/proof_producer/guardian_producer.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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.
Expand Down
17 changes: 8 additions & 9 deletions prover/proof_producer/guardian_producer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand All @@ -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)
Expand Down
5 changes: 2 additions & 3 deletions prover/proof_producer/optimistic_producer.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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.
Expand Down
33 changes: 16 additions & 17 deletions prover/proof_producer/optimistic_producer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand All @@ -37,28 +32,27 @@ 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)
require.NotEmpty(t, res.Proof)
}

func TestProofCancel(t *testing.T) {
optimisticProofProducer := &OptimisticProofProducer{}

resCh := make(chan *ProofWithHeader, 1)

blockID := common.Big32
header := &types.Header{
ParentHash: randHash(),
UncleHash: randHash(),
Expand All @@ -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))
Expand Down
3 changes: 1 addition & 2 deletions prover/proof_producer/proof_producer.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
29 changes: 16 additions & 13 deletions prover/proof_producer/sgx_and_zkevm_rpcd_producer.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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.
Expand Down
17 changes: 7 additions & 10 deletions prover/proof_producer/sgx_producer.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -95,27 +94,25 @@ 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,
Proof: proof,
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.
Expand Down
21 changes: 10 additions & 11 deletions prover/proof_producer/sgx_producer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand All @@ -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)
Expand Down
Loading

0 comments on commit 46779ca

Please sign in to comment.