Skip to content

Commit

Permalink
feat(taiko-client): add RaikoZKVMHostEndpoint and rename (#17926)
Browse files Browse the repository at this point in the history
Co-authored-by: maskpp <maskpp266@gmail.com>
  • Loading branch information
YoGhurt111 and mask-pp authored Aug 16, 2024
1 parent fdec8db commit 0838f79
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 34 deletions.
7 changes: 7 additions & 0 deletions packages/taiko-client/cmd/flags/prover.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,12 @@ var (
Category: proverCategory,
EnvVars: []string{"RAIKO_HOST"},
}
RaikoZKVMHostEndpoint = &cli.StringFlag{
Name: "raiko.host.zkvm",
Usage: "RPC endpoint of a Raiko ZKVM host service",
Category: proverCategory,
EnvVars: []string{"RAIKO_HOST_ZKVM"},
}
RaikoJWTPath = &cli.StringFlag{
Name: "raiko.jwtPath",
Usage: "Path to a JWT secret for the Raiko service",
Expand Down Expand Up @@ -220,4 +226,5 @@ var ProverFlags = MergeFlags(CommonFlags, []cli.Flag{
L2NodeVersion,
BlockConfirmations,
RaikoRequestTimeout,
RaikoZKVMHostEndpoint,
}, TxmgrFlags)
2 changes: 2 additions & 0 deletions packages/taiko-client/prover/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ type Config struct {
Allowance *big.Int
GuardianProverHealthCheckServerEndpoint *url.URL
RaikoHostEndpoint string
RaikoZKVMHostEndpoint string
RaikoJWT string
RaikoRequestTimeout time.Duration
L1NodeVersion string
Expand Down Expand Up @@ -143,6 +144,7 @@ func NewConfigFromCliContext(c *cli.Context) (*Config, error) {
ProverSetAddress: common.HexToAddress(c.String(flags.ProverSetAddress.Name)),
L1ProverPrivKey: l1ProverPrivKey,
RaikoHostEndpoint: c.String(flags.RaikoHostEndpoint.Name),
RaikoZKVMHostEndpoint: c.String(flags.RaikoZKVMHostEndpoint.Name),
RaikoJWT: common.Bytes2Hex(jwtSecret),
RaikoRequestTimeout: c.Duration(flags.RaikoRequestTimeout.Name),
StartingBlockID: startingBlockID,
Expand Down
16 changes: 6 additions & 10 deletions packages/taiko-client/prover/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,16 +115,12 @@ func (p *Prover) initProofSubmitters(
RaikoRequestTimeout: p.cfg.RaikoRequestTimeout,
}
case encoding.TierZkVMRisc0ID:
// TODO: Temporarily use SgxAndZKvmProofProducer to generate zkvm proofs
producer = &proofProducer.SgxAndZKvmProofProducer{
ZKProofType: proofProducer.ZKProofTypeR0,
SGX: proofProducer.SGXProofProducer{
RaikoHostEndpoint: p.cfg.RaikoHostEndpoint,
JWT: p.cfg.RaikoJWT,
ProofType: proofProducer.ProofTypeSgx,
Dummy: p.cfg.Dummy,
RaikoRequestTimeout: p.cfg.RaikoRequestTimeout,
},
producer = &proofProducer.ZKvmProofProducer{
ZKProofType: proofProducer.ZKProofTypeR0,
RaikoHostEndpoint: p.cfg.RaikoZKVMHostEndpoint,
JWT: p.cfg.RaikoJWT,
Dummy: p.cfg.Dummy,
RaikoRequestTimeout: p.cfg.RaikoRequestTimeout,
}
case encoding.TierGuardianMinorityID:
producer = proofProducer.NewGuardianProofProducer(encoding.TierGuardianMinorityID, p.cfg.EnableLivenessBondProof)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,30 +46,35 @@ type ProofDataV2 struct {
Quote string `json:"quote"`
}

// SgxAndZKvmProofProducer generates a ZK proof for the given block.
type SgxAndZKvmProofProducer struct {
ZKProofType string // ZK Proof type
SGX SGXProofProducer
// ZKvmProofProducer generates a ZK proof for the given block.
type ZKvmProofProducer struct {
ZKProofType string // ZK Proof type
RaikoHostEndpoint string
RaikoRequestTimeout time.Duration
JWT string // JWT provided by Raiko
Dummy bool
DummyProofProducer
}

// RequestProof implements the ProofProducer interface.
func (s *SgxAndZKvmProofProducer) RequestProof(
func (s *ZKvmProofProducer) RequestProof(
ctx context.Context,
opts *ProofRequestOptions,
blockID *big.Int,
meta metadata.TaikoBlockMetaData,
header *types.Header,
) (*ProofWithHeader, error) {
log.Info(
"Request sgx and zk proof from raiko-host service",
"Request zk proof from raiko-host service",
"blockID", blockID,
"coinbase", meta.GetCoinbase(),
"height", header.Number,
"hash", header.Hash(),
"zk type", s.ZKProofType,
)

if s.SGX.Dummy {
return s.SGX.DummyProofProducer.RequestProof(opts, blockID, meta, header, s.Tier())
if s.Dummy {
return s.DummyProofProducer.RequestProof(opts, blockID, meta, header, s.Tier())
}

proof, err := s.callProverDaemon(ctx, opts)
Expand All @@ -89,26 +94,26 @@ func (s *SgxAndZKvmProofProducer) RequestProof(
}, nil
}

func (s *SgxAndZKvmProofProducer) RequestCancel(
func (s *ZKvmProofProducer) RequestCancel(
ctx context.Context,
opts *ProofRequestOptions,
) error {
return s.requestCancel(ctx, opts)
}

// callProverDaemon keeps polling the proverd service to get the requested proof.
func (s *SgxAndZKvmProofProducer) callProverDaemon(ctx context.Context, opts *ProofRequestOptions) ([]byte, error) {
func (s *ZKvmProofProducer) callProverDaemon(ctx context.Context, opts *ProofRequestOptions) ([]byte, error) {
var (
proof []byte
start = time.Now()
)

zkCtx, zkCancel := rpc.CtxWithTimeoutOrDefault(ctx, s.SGX.RaikoRequestTimeout)
zkCtx, zkCancel := rpc.CtxWithTimeoutOrDefault(ctx, s.RaikoRequestTimeout)
defer zkCancel()

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

Expand All @@ -123,14 +128,14 @@ func (s *SgxAndZKvmProofProducer) callProverDaemon(ctx context.Context, opts *Pr
"Proof generated",
"height", opts.BlockID,
"time", time.Since(start),
"producer", "SgxAndZKvmProofProducer",
"producer", "ZKvmProofProducer",
)

return proof, nil
}

// requestProof sends a RPC request to proverd to try to get the requested proof.
func (s *SgxAndZKvmProofProducer) requestProof(
func (s *ZKvmProofProducer) requestProof(
ctx context.Context,
opts *ProofRequestOptions,
) (*RaikoRequestProofBodyResponseV2, error) {
Expand All @@ -154,13 +159,13 @@ func (s *SgxAndZKvmProofProducer) requestProof(
return nil, err
}

req, err := http.NewRequestWithContext(ctx, "POST", s.SGX.RaikoHostEndpoint+"/v2/proof", bytes.NewBuffer(jsonValue))
req, err := http.NewRequestWithContext(ctx, "POST", s.RaikoHostEndpoint+"/v2/proof", bytes.NewBuffer(jsonValue))
if err != nil {
return nil, err
}
req.Header.Set("Content-Type", "application/json")
if len(s.SGX.JWT) > 0 {
req.Header.Set("Authorization", "Bearer "+base64.StdEncoding.EncodeToString([]byte(s.SGX.JWT)))
if len(s.JWT) > 0 {
req.Header.Set("Authorization", "Bearer "+base64.StdEncoding.EncodeToString([]byte(s.JWT)))
}

res, err := client.Do(req)
Expand Down Expand Up @@ -190,7 +195,7 @@ func (s *SgxAndZKvmProofProducer) requestProof(
return &output, nil
}

func (s *SgxAndZKvmProofProducer) requestCancel(
func (s *ZKvmProofProducer) requestCancel(
ctx context.Context,
opts *ProofRequestOptions,
) error {
Expand All @@ -217,15 +222,15 @@ func (s *SgxAndZKvmProofProducer) requestCancel(
req, err := http.NewRequestWithContext(
ctx,
"POST",
s.SGX.RaikoHostEndpoint+"/v2/proof/cancel",
s.RaikoHostEndpoint+"/v2/proof/cancel",
bytes.NewBuffer(jsonValue),
)
if err != nil {
return err
}
req.Header.Set("Content-Type", "application/json")
if len(s.SGX.JWT) > 0 {
req.Header.Set("Authorization", "Bearer "+base64.StdEncoding.EncodeToString([]byte(s.SGX.JWT)))
if len(s.JWT) > 0 {
req.Header.Set("Authorization", "Bearer "+base64.StdEncoding.EncodeToString([]byte(s.JWT)))
}

res, err := client.Do(req)
Expand All @@ -242,7 +247,9 @@ func (s *SgxAndZKvmProofProducer) requestCancel(
}

// Tier implements the ProofProducer interface.
func (s *SgxAndZKvmProofProducer) Tier() uint16 {
// TODO: Temporarily use TierZkVMRisc0ID instead of TierSgxAndZkVMID
return encoding.TierZkVMRisc0ID
func (s *ZKvmProofProducer) Tier() uint16 {
switch s.ZKProofType {
default:
return encoding.TierZkVMRisc0ID
}
}

0 comments on commit 0838f79

Please sign in to comment.