-
Notifications
You must be signed in to change notification settings - Fork 606
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
1e9c8fd
commit c0fc89b
Showing
9 changed files
with
303 additions
and
136 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,116 @@ | ||
package client | ||
|
||
import ( | ||
"context" | ||
"encoding/json" | ||
"fmt" | ||
"time" | ||
|
||
"github.com/go-resty/resty/v2" | ||
|
||
"scroll-tech/prover/config" | ||
) | ||
|
||
// CoordinatorClient is a client used for interacting with the Coordinator service. | ||
type CoordinatorClient struct { | ||
client *resty.Client | ||
} | ||
|
||
// NewCoordinatorClient constructs a new CoordinatorClient. | ||
func NewCoordinatorClient(cfg *config.CoordinatorConfig) (*CoordinatorClient, error) { | ||
if cfg.BaseURL == "" { | ||
return nil, fmt.Errorf("base URL is not specified") | ||
} | ||
|
||
client := resty.New(). | ||
SetTimeout(time.Duration(cfg.Timeout) * time.Second). | ||
SetBaseURL(cfg.BaseURL) | ||
|
||
return &CoordinatorClient{ | ||
client: client, | ||
}, nil | ||
} | ||
|
||
// Login sends login request to the coordinator. | ||
func (c *CoordinatorClient) Login(ctx context.Context, req *ProverLoginRequest) (*ProverLoginResponse, error) { | ||
resp, err := c.client.R(). | ||
SetHeader("Content-Type", "application/json"). | ||
SetBody(req). | ||
Post("/api/login") | ||
|
||
if err != nil { | ||
return nil, fmt.Errorf("login failed: %v", err) | ||
} | ||
|
||
if resp.StatusCode() != 200 { | ||
return nil, fmt.Errorf("failed to login, status code: %v", resp.StatusCode()) | ||
} | ||
|
||
var result ProverLoginResponse | ||
err = json.Unmarshal(resp.Body(), &result) | ||
if err != nil { | ||
return nil, fmt.Errorf("failed to parse login response: %v", err) | ||
} | ||
|
||
// Check errcode in the response | ||
if result.ErrCode != 200 { | ||
return nil, fmt.Errorf("failed to login, error code: %v, error message: %v", result.ErrCode, result.ErrMsg) | ||
} | ||
|
||
// Store JWT token for future requests | ||
c.client.SetAuthToken(result.Data.Token) | ||
|
||
return &result, nil | ||
} | ||
|
||
// ProverTasks sends a request to the coordinator to get prover tasks. | ||
func (c *CoordinatorClient) ProverTasks(ctx context.Context, req *ProverTasksRequest) (*ProverTasksResponse, error) { | ||
resp, err := c.client.R(). | ||
SetHeader("Content-Type", "application/json"). | ||
SetBody(req). | ||
Post("/api/prover_tasks") | ||
|
||
if err != nil { | ||
return nil, fmt.Errorf("request for ProverTasks failed: %v", err) | ||
} | ||
|
||
var result ProverTasksResponse | ||
err = json.Unmarshal(resp.Body(), &result) | ||
if err != nil { | ||
return nil, fmt.Errorf("failed to parse ProverTasks response: %v", err) | ||
} | ||
|
||
if result.ErrCode != 200 { | ||
return nil, fmt.Errorf("error code: %v, error message: %v", result.ErrCode, result.ErrMsg) | ||
} | ||
|
||
return &result, nil | ||
} | ||
|
||
// SubmitProof sends a request to the coordinator to submit proof. | ||
func (c *CoordinatorClient) SubmitProof(ctx context.Context, req *SubmitProofRequest) (*SubmitProofResponse, error) { | ||
resp, err := c.client.R(). | ||
SetHeader("Content-Type", "application/json"). | ||
SetBody(req). | ||
Post("/coordinator/v1/submit_proof") | ||
|
||
if err != nil { | ||
return nil, fmt.Errorf("submit proof request failed: %v", err) | ||
} | ||
|
||
if resp.StatusCode() != 200 { | ||
return nil, fmt.Errorf("failed to submit proof, status code: %v", resp.StatusCode()) | ||
} | ||
|
||
var result SubmitProofResponse | ||
err = json.Unmarshal(resp.Body(), &result) | ||
if err != nil { | ||
return nil, fmt.Errorf("failed to parse submit proof response: %v", err) | ||
} | ||
|
||
if result.ErrCode != 200 { | ||
return nil, fmt.Errorf("error code: %v, error message: %v", result.ErrCode, result.ErrMsg) | ||
} | ||
|
||
return &result, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
package client | ||
|
||
import ( | ||
"scroll-tech/common/types/message" | ||
) | ||
|
||
// ProverLoginRequest defines the request structure for login API | ||
type ProverLoginRequest struct { | ||
PublicKey string `json:"public_key"` | ||
ProverName string `json:"prover_name"` | ||
} | ||
|
||
// ProverLoginResponse defines the response structure for login API | ||
type ProverLoginResponse struct { | ||
ErrCode int `json:"errcode"` | ||
ErrMsg string `json:"errmsg"` | ||
Data *struct { | ||
Time string `json:"time"` | ||
Token string `json:"token"` | ||
} `json:"data"` | ||
} | ||
|
||
// ProverTasksRequest defines the request structure for ProverTasks API | ||
type ProverTasksRequest struct { | ||
ProverVersion int `json:"prover_version"` | ||
ProverHeight int `json:"prover_height"` | ||
ProofType int `json:"proof_type,omitempty"` | ||
} | ||
|
||
// ProverTasksResponse defines the response structure for ProverTasks API | ||
type ProverTasksResponse struct { | ||
ErrCode int `json:"errcode"` | ||
ErrMsg string `json:"errmsg"` | ||
Data *struct { | ||
TaskID string `json:"task_id"` | ||
ProofType message.ProofType `json:"proof_type"` | ||
ProofData string `json:"proof_data"` | ||
} `json:"data"` | ||
} | ||
|
||
// SubmitProofRequest defines the request structure for the SubmitProof API. | ||
type SubmitProofRequest struct { | ||
TaskID string `json:"task_id"` | ||
Status int `json:"status"` | ||
Error string `json:"error"` | ||
ProofType int `json:"proof_type"` | ||
Signature string `json:"signature"` | ||
Proof string `json:"proof"` | ||
} | ||
|
||
// SubmitProofResponse defines the response structure for the SubmitProof API. | ||
type SubmitProofResponse struct { | ||
ErrCode int `json:"errcode"` | ||
ErrMsg string `json:"errmsg"` | ||
Data string `json:"data"` // todo: remove it? | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.