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

Commit

Permalink
feat(prover): improve submitters
Browse files Browse the repository at this point in the history
  • Loading branch information
davidtaikocha committed Oct 8, 2023
1 parent eb28b05 commit 422728d
Show file tree
Hide file tree
Showing 14 changed files with 272 additions and 79 deletions.
11 changes: 6 additions & 5 deletions bindings/encoding/struct.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,12 @@ import (
)

// Tier IDs defined in protocol.
const (
TierOptimisticID uint16 = 100
TierSgxID uint16 = 200
TierPseZkevmID uint16 = 300
TierGuardianID uint16 = 1000
var (
TierOptimisticID uint16 = 100
TierSgxID uint16 = 200
TierPseZkevmID uint16 = 300
TierGuardianID uint16 = 1000
ProtocolTiers []uint16 = []uint16{TierOptimisticID, TierSgxID, TierPseZkevmID, TierGuardianID}
)

// BlockHeader represents an Ethereum block header.
Expand Down
7 changes: 7 additions & 0 deletions cmd/flags/prover.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,12 @@ var (
Category: proverCategory,
Value: false,
}
ContestControversialProofs = &cli.BoolFlag{
Name: "prover.contestControversialProofs",
Usage: "Whether you want to contest proofs with different L2 hashes with higher tier proofs",
Category: proverCategory,
Value: false,
}
ProveBlockTxGasLimit = &cli.Uint64Flag{
Name: "prover.proveBlockTxGasLimit",
Usage: "Gas limit will be used for TaikoL1.proveBlock transactions",
Expand Down Expand Up @@ -161,6 +167,7 @@ var ProverFlags = MergeFlags(CommonFlags, []cli.Flag{
ProveBlockMaxTxGasTipCap,
Graffiti,
ProveUnassignedBlocks,
ContestControversialProofs,
ProveBlockTxGasLimit,
ProverHTTPServerPort,
ProverCapacity,
Expand Down
2 changes: 2 additions & 0 deletions prover/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ type Config struct {
BackOffMaxRetrys uint64
BackOffRetryInterval time.Duration
ProveUnassignedBlocks bool
ContestControversialProofs bool
RPCTimeout *time.Duration
WaitReceiptTimeout time.Duration
ProveBlockGasLimit *uint64
Expand Down Expand Up @@ -122,6 +123,7 @@ func NewConfigFromCliContext(c *cli.Context) (*Config, error) {
BackOffMaxRetrys: c.Uint64(flags.BackOffMaxRetrys.Name),
BackOffRetryInterval: c.Duration(flags.BackOffRetryInterval.Name),
ProveUnassignedBlocks: c.Bool(flags.ProveUnassignedBlocks.Name),
ContestControversialProofs: c.Bool(flags.ContestControversialProofs.Name),
RPCTimeout: timeout,
WaitReceiptTimeout: c.Duration(flags.WaitReceiptTimeout.Name),
ProveBlockGasLimit: proveBlockTxGasLimit,
Expand Down
35 changes: 35 additions & 0 deletions prover/proof_producer/dummy_producer.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package producer

import (
"bytes"
"context"
"math/big"

"github.com/ethereum/go-ethereum/core/types"
"github.com/taikoxyz/taiko-client/bindings"
)

// OptimisticProofProducer always returns a dummy proof.
type DummyProofProducer struct{}

// RequestProof returns a dummy proof to the result channel.
func (o *DummyProofProducer) RequestProof(
ctx context.Context,
opts *ProofRequestOptions,
blockID *big.Int,
meta *bindings.TaikoDataBlockMetadata,
header *types.Header,
tier uint16,
resultCh chan *ProofWithHeader,
) error {
resultCh <- &ProofWithHeader{
BlockID: blockID,
Meta: meta,
Header: header,
Proof: bytes.Repeat([]byte{0xff}, 100),
Degree: CircuitsIdx,
Opts: opts,
}

return nil
}
44 changes: 44 additions & 0 deletions prover/proof_producer/guardian_producer.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package producer

import (
"context"
"math/big"

"github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/log"
"github.com/taikoxyz/taiko-client/bindings"
"github.com/taikoxyz/taiko-client/bindings/encoding"
)

// GuardianProofProducer always returns an optimistic (dummy) proof.
type GuardianProofProducer struct{ *DummyProofProducer }

// RequestProof implements the ProofProducer interface.
func (g *GuardianProofProducer) RequestProof(
ctx context.Context,
opts *ProofRequestOptions,
blockID *big.Int,
meta *bindings.TaikoDataBlockMetadata,
header *types.Header,
resultCh chan *ProofWithHeader,
) error {
log.Info(
"Request guardian proof",
"blockID", blockID,
"coinbase", meta.Coinbase,
"height", header.Number,
"hash", header.Hash(),
)

return g.DummyProofProducer.RequestProof(ctx, opts, blockID, meta, header, g.Tier(), resultCh)
}

// Tier implements the ProofProducer interface.
func (g *GuardianProofProducer) Tier() uint16 {
return encoding.TierGuardianID
}

// Cancel cancels an existing proof generation.
func (g *GuardianProofProducer) Cancel(ctx context.Context, blockID *big.Int) error {
return nil
}
14 changes: 2 additions & 12 deletions prover/proof_producer/optimistic_producer.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package producer

import (
"bytes"
"context"
"math/big"

Expand All @@ -12,7 +11,7 @@ import (
)

// OptimisticProofProducer always returns an optimistic (dummy) proof.
type OptimisticProofProducer struct{}
type OptimisticProofProducer struct{ *DummyProofProducer }

// RequestProof implements the ProofProducer interface.
func (o *OptimisticProofProducer) RequestProof(
Expand All @@ -31,16 +30,7 @@ func (o *OptimisticProofProducer) RequestProof(
"hash", header.Hash(),
)

resultCh <- &ProofWithHeader{
BlockID: blockID,
Meta: meta,
Header: header,
Proof: bytes.Repeat([]byte{0xff}, 100),
Degree: CircuitsIdx,
Opts: opts,
}

return nil
return o.DummyProofProducer.RequestProof(ctx, opts, blockID, meta, header, o.Tier(), resultCh)
}

// Tier implements the ProofProducer interface.
Expand Down
3 changes: 2 additions & 1 deletion prover/proof_producer/proof_producer.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ const (

// ProofRequestOptions contains all options that need to be passed to zkEVM rpcd service.
type ProofRequestOptions struct {
Height *big.Int // the block number
BlockID *big.Int
ProverAddress common.Address
ProposeBlockTxHash common.Hash
L1SignalService common.Address
Expand All @@ -38,6 +38,7 @@ type ProofWithHeader struct {
Proof []byte
Degree uint64
Opts *ProofRequestOptions
Tier uint16
}

type ProofProducer interface {
Expand Down
44 changes: 44 additions & 0 deletions prover/proof_producer/sgx_producer.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package producer

import (
"context"
"math/big"

"github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/log"
"github.com/taikoxyz/taiko-client/bindings"
"github.com/taikoxyz/taiko-client/bindings/encoding"
)

// SGXProofProducer generates a SGX proof for the given block.
type SGXProofProducer struct{ *DummyProofProducer }

// RequestProof implements the ProofProducer interface.
func (s *SGXProofProducer) RequestProof(
ctx context.Context,
opts *ProofRequestOptions,
blockID *big.Int,
meta *bindings.TaikoDataBlockMetadata,
header *types.Header,
resultCh chan *ProofWithHeader,
) error {
log.Warn(
"SGX proof producer is unimplemented, will return a dummy proof instead",
"blockID", blockID,
"coinbase", meta.Coinbase,
"height", header.Number,
"hash", header.Hash(),
)

return s.DummyProofProducer.RequestProof(ctx, opts, blockID, meta, header, s.Tier(), resultCh)
}

// Tier implements the ProofProducer interface.
func (s *SGXProofProducer) Tier() uint16 {
return encoding.TierSgxID
}

// Cancel cancels an existing proof generation.
func (s *SGXProofProducer) Cancel(ctx context.Context, blockID *big.Int) error {
return nil
}
32 changes: 19 additions & 13 deletions prover/proof_producer/zkevm_rpcd_producer.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,15 @@ var (

// ZkevmRpcdProducer is responsible for requesting zk proofs from the given proverd endpoint.
type ZkevmRpcdProducer struct {
RpcdEndpoint string // a proverd RPC endpoint
Param string // parameter file to use
L1Endpoint string // a L1 node RPC endpoint
L2Endpoint string // a L2 execution engine's RPC endpoint
Retry bool // retry proof computation if error
CustomProofHook func() ([]byte, uint64, error) // only for testing purposes
ProofTimeTarget uint64 // used for calculating proof delay
ProtocolConfig *bindings.TaikoDataConfig // protocol configurations
RpcdEndpoint string // a proverd RPC endpoint
Param string // parameter file to use
L1Endpoint string // a L1 node RPC endpoint
L2Endpoint string // a L2 execution engine's RPC endpoint
Retry bool // retry proof computation if error
ProofTimeTarget uint64 // used for calculating proof delay
ProtocolConfig *bindings.TaikoDataConfig // protocol configurations
CustomProofHook func() ([]byte, uint64, error) // only for testing purposes
*DummyProofProducer // only for testing purposes
}

// RequestProofBody represents the JSON body for requesting the proof.
Expand Down Expand Up @@ -137,6 +138,10 @@ func (p *ZkevmRpcdProducer) RequestProof(
"hash", header.Hash(),
)

if p.DummyProofProducer != nil {
return p.DummyProofProducer.RequestProof(ctx, opts, blockID, meta, header, p.Tier(), resultCh)
}

var (
proof []byte
degree uint64
Expand All @@ -158,6 +163,7 @@ func (p *ZkevmRpcdProducer) RequestProof(
Proof: proof,
Degree: degree,
Opts: opts,
Tier: p.Tier(),
}

return nil
Expand All @@ -176,12 +182,12 @@ func (p *ZkevmRpcdProducer) callProverDaemon(ctx context.Context, opts *ProofReq
}
output, err := p.requestProof(opts)
if err != nil {
log.Error("Failed to request proof", "height", opts.Height, "err", err, "endpoint", p.RpcdEndpoint)
log.Error("Failed to request proof", "height", opts.BlockID, "err", err, "endpoint", p.RpcdEndpoint)
return err
}

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

Expand All @@ -195,7 +201,7 @@ func (p *ZkevmRpcdProducer) callProverDaemon(ctx context.Context, opts *ProofReq

proof = common.Hex2Bytes(proofOutput)
degree = output.Aggregation.Degree
log.Info("Proof generated", "height", opts.Height, "degree", degree, "time", time.Since(start))
log.Info("Proof generated", "height", opts.BlockID, "degree", degree, "time", time.Since(start))
return nil
}, backoff.NewConstantBackOff(proofPollingInterval)); err != nil {
return nil, 0, err
Expand All @@ -211,7 +217,7 @@ func (p *ZkevmRpcdProducer) requestProof(opts *ProofRequestOptions) (*RpcdOutput
Method: "proof",
Params: []*RequestProofBodyParam{{
Circuit: "super",
Block: opts.Height,
Block: opts.BlockID,
L2RPC: p.L2Endpoint,
Retry: true,
Param: p.Param,
Expand Down Expand Up @@ -249,7 +255,7 @@ func (p *ZkevmRpcdProducer) requestProof(opts *ProofRequestOptions) (*RpcdOutput

defer res.Body.Close()
if res.StatusCode != http.StatusOK {
return nil, fmt.Errorf("failed to request proof, id: %d, statusCode: %d", opts.Height, res.StatusCode)
return nil, fmt.Errorf("failed to request proof, id: %d, statusCode: %d", opts.BlockID, res.StatusCode)
}

resBytes, err := io.ReadAll(res.Body)
Expand Down
1 change: 1 addition & 0 deletions prover/proof_submitter/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,5 @@ type Submitter interface {
RequestProof(ctx context.Context, event *bindings.TaikoL1ClientBlockProposed) error
SubmitProof(ctx context.Context, proofWithHeader *proofProducer.ProofWithHeader) error
CancelProof(ctx context.Context, blockID *big.Int) error
Tier() uint16
}
13 changes: 9 additions & 4 deletions prover/proof_submitter/proof_submitter.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,8 @@ type ProofSubmitter struct {
mutex *sync.Mutex
}

// NewValidProofSubmitter creates a new Submitter instance.
func NewValidProofSubmitter(
// New creates a new ProofSubmitter instance.
func New(
rpcClient *rpc.Client,
proofProducer proofProducer.ProofProducer,
resultCh chan *proofProducer.ProofWithHeader,
Expand Down Expand Up @@ -132,7 +132,7 @@ func (s *ProofSubmitter) RequestProof(ctx context.Context, event *bindings.Taiko

// Request proof.
opts := &proofProducer.ProofRequestOptions{
Height: block.Number(),
BlockID: block.Number(),
ProverAddress: s.proverAddress,
ProposeBlockTxHash: event.Raw.TxHash,
L1SignalService: s.l1SignalService,
Expand Down Expand Up @@ -194,7 +194,7 @@ func (s *ProofSubmitter) SubmitProof(
}

log.Debug(
"Get the L2 block to prove",
"L2 block to prove",
"blockID", blockID,
"hash", block.Hash(),
"root", header.Root.String(),
Expand Down Expand Up @@ -308,6 +308,11 @@ func (s *ProofSubmitter) CancelProof(ctx context.Context, blockID *big.Int) erro
return s.proofProducer.Cancel(ctx, blockID)
}

// Tier returns the proof tier of the current proof submitter.
func (s *ProofSubmitter) Tier() uint16 {
return s.proofProducer.Tier()
}

// uint16ToBytes converts an uint16 to bytes.
func uint16ToBytes(i uint16) []byte {
b := make([]byte, 2)
Expand Down
2 changes: 1 addition & 1 deletion prover/proof_submitter/proof_submitter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ func (s *ProofSubmitterTestSuite) SetupTest() {
s.validProofCh = make(chan *proofProducer.ProofWithHeader, 1024)
s.invalidProofCh = make(chan *proofProducer.ProofWithHeader, 1024)

s.validProofSubmitter, err = NewValidProofSubmitter(
s.validProofSubmitter, err = New(
s.RpcClient,
&proofProducer.OptimisticProofProducer{},
s.validProofCh,
Expand Down
Loading

0 comments on commit 422728d

Please sign in to comment.