Skip to content

Commit

Permalink
feat(taiko-client): integrate new Raiko APIs (openAPI) (#17039)
Browse files Browse the repository at this point in the history
  • Loading branch information
YoGhurt111 authored May 20, 2024
1 parent ec98418 commit e36d5b7
Showing 1 changed file with 41 additions and 56 deletions.
97 changes: 41 additions & 56 deletions packages/taiko-client/prover/proof_producer/sgx_producer.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"bytes"
"context"
"encoding/json"
"errors"
"fmt"
"io"
"math/big"
Expand Down Expand Up @@ -37,47 +36,38 @@ type SGXProofProducer struct {
DummyProofProducer
}

// SGXRequestProofBody represents the JSON body for requesting the proof.
type SGXRequestProofBody struct {
JsonRPC string `json:"jsonrpc"` //nolint:revive,stylecheck
ID *big.Int `json:"id"`
Method string `json:"method"`
Params []*SGXRequestProofBodyParam `json:"params"`
// RaikoRequestProofBody represents the JSON body for requesting the proof.
type RaikoRequestProofBody struct {
L2RPC string `json:"rpc"`
L1RPC string `json:"l1_rpc"`
L1BeaconRPC string `json:"beacon_rpc"`
Block *big.Int `json:"block_number"`
Prover string `json:"prover"`
Graffiti string `json:"graffiti"`
Type string `json:"proof_type"`
SGX *SGXRequestProofBodyParam `json:"sgx"`
RISC0 RISC0RequestProofBodyParam `json:"risc0"`
}

// SGXRequestProofBodyParam represents the JSON body of RequestProofBody's `param` field.
// SGXRequestProofBodyParam represents the JSON body of RaikoRequestProofBody's `sgx` field.
type SGXRequestProofBodyParam struct {
Type string `json:"proof_type"`
Block *big.Int `json:"block_number"`
L2RPC string `json:"rpc"`
L1RPC string `json:"l1_rpc"`
L1BeaconRPC string `json:"beacon_rpc"`
Prover string `json:"prover"`
Graffiti string `json:"graffiti"`
ProofParam *ProofParam `json:"sgx"`
}

// ProofParam represents the JSON body of SGXRequestProofBodyParam's `sgx` field.
type ProofParam struct {
Setup bool `json:"setup"`
Bootstrap bool `json:"bootstrap"`
Prove bool `json:"prove"`
}

// SGXRequestProofBodyResponse represents the JSON body of the response of the proof requests.
type SGXRequestProofBodyResponse struct {
JsonRPC string `json:"jsonrpc"` //nolint:revive,stylecheck
ID *big.Int `json:"id"`
Result *RaikoHostOutput `json:"result"`
Error *struct {
Code *big.Int `json:"code"`
Message string `json:"message"`
} `json:"error,omitempty"`
// RISC0RequestProofBodyParam represents the JSON body of RaikoRequestProofBody's `risc0` field.
type RISC0RequestProofBodyParam struct {
Bonsai bool `json:"bonsai"`
Snark bool `json:"snark"`
Profile bool `json:"profile"`
ExecutionPo2 *big.Int `json:"execution_po2"`
}

// RaikoHostOutput represents the JSON body of SGXRequestProofBodyResponse's `result` field.
type RaikoHostOutput struct {
Proof string `json:"proof"`
// RaikoRequestProofBodyResponse represents the JSON body of the response of the proof requests.
type RaikoRequestProofBodyResponse struct {
Proof string `json:"proof"` //nolint:revive,stylecheck
ErrorMessage string `json:"message"`
}

// RequestProof implements the ProofProducer interface.
Expand Down Expand Up @@ -161,33 +151,28 @@ func (s *SGXProofProducer) callProverDaemon(ctx context.Context, opts *ProofRequ
}

// requestProof sends a RPC request to proverd to try to get the requested proof.
func (s *SGXProofProducer) requestProof(opts *ProofRequestOptions) (*RaikoHostOutput, error) {
reqBody := SGXRequestProofBody{
JsonRPC: "2.0",
ID: common.Big1,
Method: "proof",
Params: []*SGXRequestProofBodyParam{{
Type: s.ProofType,
Block: opts.BlockID,
L2RPC: s.L2Endpoint,
L1RPC: s.L1Endpoint,
L1BeaconRPC: s.L1BeaconEndpoint,
Prover: opts.ProverAddress.Hex()[2:],
Graffiti: opts.Graffiti,
ProofParam: &ProofParam{
Setup: false,
Bootstrap: false,
Prove: true,
},
}},
func (s *SGXProofProducer) requestProof(opts *ProofRequestOptions) (*RaikoRequestProofBodyResponse, error) {
reqBody := RaikoRequestProofBody{
Type: s.ProofType,
Block: opts.BlockID,
L2RPC: s.L2Endpoint,
L1RPC: s.L1Endpoint,
L1BeaconRPC: s.L1BeaconEndpoint,
Prover: opts.ProverAddress.Hex()[2:],
Graffiti: opts.Graffiti,
SGX: &SGXRequestProofBodyParam{
Setup: false,
Bootstrap: false,
Prove: true,
},
}

jsonValue, err := json.Marshal(reqBody)
if err != nil {
return nil, err
}

res, err := http.Post(s.RaikoHostEndpoint, "application/json", bytes.NewBuffer(jsonValue))
res, err := http.Post(s.RaikoHostEndpoint+"/v1/proof", "application/json", bytes.NewBuffer(jsonValue))
if err != nil {
return nil, err
}
Expand All @@ -202,16 +187,16 @@ func (s *SGXProofProducer) requestProof(opts *ProofRequestOptions) (*RaikoHostOu
return nil, err
}

var output SGXRequestProofBodyResponse
var output RaikoRequestProofBodyResponse
if err := json.Unmarshal(resBytes, &output); err != nil {
return nil, err
}

if output.Error != nil {
return nil, errors.New(output.Error.Message)
if len(output.ErrorMessage) > 0 {
return nil, fmt.Errorf("failed to get proof, msg: %s", output.ErrorMessage)
}

return output.Result, nil
return &output, nil
}

// Tier implements the ProofProducer interface.
Expand Down

0 comments on commit e36d5b7

Please sign in to comment.