Skip to content

Commit

Permalink
a commit
Browse files Browse the repository at this point in the history
  • Loading branch information
colinlyguo committed Aug 1, 2023
1 parent 1e9c8fd commit c0fc89b
Show file tree
Hide file tree
Showing 9 changed files with 303 additions and 136 deletions.
2 changes: 2 additions & 0 deletions bridge/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/deckarep/golang-set v1.8.0 // indirect
github.com/go-ole/go-ole v1.2.6 // indirect
github.com/go-resty/resty/v2 v2.7.0 // indirect
github.com/go-stack/stack v1.8.1 // indirect
github.com/golang/snappy v0.0.5-0.20220116011046-fa5810519dcb // indirect
github.com/google/uuid v1.3.0 // indirect
Expand Down Expand Up @@ -53,6 +54,7 @@ require (
github.com/xrash/smetrics v0.0.0-20201216005158-039620a65673 // indirect
github.com/yusufpapurcu/wmi v1.2.2 // indirect
golang.org/x/crypto v0.11.0 // indirect
golang.org/x/net v0.10.0 // indirect
golang.org/x/sys v0.10.0 // indirect
golang.org/x/time v0.3.0 // indirect
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c // indirect
Expand Down
13 changes: 13 additions & 0 deletions bridge/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ3
github.com/davecgh/go-spew v0.0.0-20171005155431-ecdeabc65495/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/dchest/blake512 v1.0.0/go.mod h1:FV1x7xPPLWukZlpDpWQ88rF/SFwZ5qbskrzhLMB92JI=
github.com/deckarep/golang-set v1.8.0 h1:sk9/l/KqpunDwP7pSjUg0keiOOLEnOBHzykLrsPppp4=
github.com/deckarep/golang-set v1.8.0/go.mod h1:5nI87KwE7wgsBU1F4GKAw2Qod7p5kyS383rP6+o6qqo=
github.com/edsrzf/mmap-go v1.0.0 h1:CEBF7HpRnUCSJgGUb5h1Gm7e3VkmVDrR8lvWVLtrOFw=
Expand All @@ -28,6 +29,8 @@ github.com/fsnotify/fsnotify v1.4.7/go.mod h1:jwhsz4b93w/PPRr/qN1Yymfu8t87LnFCMo
github.com/gballet/go-libpcsclite v0.0.0-20190607065134-2772fd86a8ff h1:tY80oXqGNY4FhTFhk+o9oFHGINQ/+vhlm8HFzi6znCI=
github.com/go-ole/go-ole v1.2.6 h1:/Fpf6oFPoeFik9ty7siob0G6Ke8QvQEuVcuChpwXzpY=
github.com/go-ole/go-ole v1.2.6/go.mod h1:pprOEPIfldk/42T2oK7lQ4v4JSDwmV0As9GaiUsvbm0=
github.com/go-resty/resty/v2 v2.7.0 h1:me+K9p3uhSmXtrBZ4k9jcEAfJmuC8IivWHwaLZwPrFY=
github.com/go-resty/resty/v2 v2.7.0/go.mod h1:9PWDzw47qPphMRFfhsyk0NnSgvluHcljSMVIq3w7q0I=
github.com/go-stack/stack v1.8.1 h1:ntEHSVwIt7PNXNpgPmVfMrNhLtgjlmnZha2kOpuRiDw=
github.com/go-stack/stack v1.8.1/go.mod h1:dcoOX6HbPZSZptuspn9bctJ+N/CnF5gGygcUP3XYfe4=
github.com/golang/protobuf v1.2.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
Expand Down Expand Up @@ -70,6 +73,7 @@ github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE=
github.com/leanovate/gopter v0.2.9 h1:fQjYxZaynp97ozCzfOyOuAGOU4aU/z37zf/tOujFk7c=
github.com/leanovate/gopter v0.2.9/go.mod h1:U2L/78B+KVFIx2VmW6onHJQzXtFb+p5y3y2Sh+Jxxv8=
github.com/mattn/go-colorable v0.1.13 h1:fFA4WZxdEF4tXPZVKMLwD8oUnCTTo08duU7wxecdEvA=
github.com/mattn/go-colorable v0.1.13/go.mod h1:7S9/ev0klgBDR4GtXTXX8a3vIGJpMovkB8vQcUbaXHg=
github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM=
Expand Down Expand Up @@ -118,6 +122,7 @@ github.com/smartystreets/goconvey v1.8.0 h1:Oi49ha/2MURE0WexF052Z0m+BNSGirfjg5RL
github.com/smartystreets/goconvey v1.8.0/go.mod h1:EdX8jtrTIj26jmjCOVNMVSIYAtgexqXKHOXW2Dx9JLg=
github.com/status-im/keycard-go v0.2.0 h1:QDLFswOQu1r5jsycloeQh3bVU8n/NatHHaZobtDnDzA=
github.com/status-im/keycard-go v0.2.0/go.mod h1:wlp8ZLbsmrF6g6WjugPAx+IzoLrkdf9+mHxBEeo3Hbg=
github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo=
github.com/stretchr/testify v1.8.3 h1:RP3t2pwF7cMEbC1dqtB6poj3niw/9gnV4Cjg5oW5gtY=
github.com/stretchr/testify v1.8.3/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
github.com/syndtr/goleveldb v1.0.1-0.20210819022825-2ae1ddf74ef7 h1:epCh84lMvA70Z7CTTCmYQn2CKbY8j86K7/FAIr141uY=
Expand All @@ -141,6 +146,9 @@ golang.org/x/crypto v0.11.0/go.mod h1:xgJhtzW8F9jGdVFWZESrid1U1bjeNy4zgy5cRr/CIi
golang.org/x/net v0.0.0-20180906233101-161cd47e91fd/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
golang.org/x/net v0.0.0-20190311183353-d8887717615a/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
golang.org/x/net v0.0.0-20211029224645-99673261e6eb/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y=
golang.org/x/net v0.10.0 h1:X2//UzNDwYmtCLn7To6G58Wr6f5ahEAQgKNzv9Y951M=
golang.org/x/net v0.10.0/go.mod h1:0qNGK6F8kojg2nk9dLZ2mShWaEBan6FAoqfSigmmuDg=
golang.org/x/sync v0.0.0-20180314180146-1d60e4601c6f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20210220032951-036812b2e83c/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.3.0 h1:ftCYgMx6zT/asHUrPw8BLLscYtGznsLAnjq5RH9P66E=
Expand All @@ -149,15 +157,20 @@ golang.org/x/sys v0.0.0-20180909124046-d0be0721c37e/go.mod h1:STP8DvDyc/dI5b8T5h
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20190916202348-b4ddaad3f8a3/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210423082822-04245dca01da/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.2.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.10.0 h1:SqMFp9UcQJZa+pmYuAKjd9xq1f0j5rLcDIk0mj4qAsA=
golang.org/x/sys v0.10.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/text v0.11.0 h1:LAntKIrcmeSKERyiOh0XMV39LXS8IE9UL2yP7+f5ij4=
golang.org/x/time v0.3.0 h1:rg5rLMjNzMS1RkNLzCG38eapWhnYLFYXDXj2gOlr8j4=
golang.org/x/time v0.3.0/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
golang.org/x/tools v0.0.0-20190328211700-ab21143f2384/go.mod h1:LCzVGOaR6xXOjkQ3onu1FJEFr0SW1gC7cKk1uF8kGRs=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk=
Expand Down
116 changes: 116 additions & 0 deletions prover/client/client.go
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
}
56 changes: 56 additions & 0 deletions prover/client/types.go
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?
}
3 changes: 2 additions & 1 deletion prover/cmd/app/app.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package app

import (
"context"
"fmt"
"os"
"os/signal"
Expand Down Expand Up @@ -42,7 +43,7 @@ func action(ctx *cli.Context) error {
}

// Create prover
r, err := prover.NewProver(cfg)
r, err := prover.NewProver(context.Background(), cfg)
if err != nil {
return err
}
Expand Down
1 change: 0 additions & 1 deletion prover/cmd/app/mock_app.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,6 @@ func (r *ProverApp) MockConfig(store bool, wsURL string) error {
if err != nil {
return err
}
cfg.CoordinatorURL = wsURL
r.Config = cfg

if !store {
Expand Down
5 changes: 4 additions & 1 deletion prover/config.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,13 @@
"prover_name": "my_prover",
"keystore_path": "keystore.json",
"keystore_password": "prover-pwd",
"coordinator_url": "ws://localhost:8391",
"db_path": "bbolt_db",
"core": {
"params_path": "params",
"seed_path": "seed"
},
"coordinator": {
"base_url": "https://coordinator/v1",
"timeout": 30
}
}
24 changes: 17 additions & 7 deletions prover/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package config

import (
"encoding/json"
"errors"
"os"
"path/filepath"

Expand All @@ -12,13 +13,13 @@ import (

// Config loads prover configuration items.
type Config struct {
ProverName string `json:"prover_name"`
KeystorePath string `json:"keystore_path"`
KeystorePassword string `json:"keystore_password"`
CoordinatorURL string `json:"coordinator_url"`
TraceEndpoint string `json:"trace_endpoint"`
Core *ProverCoreConfig `json:"core"`
DBPath string `json:"db_path"`
ProverName string `json:"prover_name"`
KeystorePath string `json:"keystore_path"`
KeystorePassword string `json:"keystore_password"`
TraceEndpoint string `json:"trace_endpoint"`
Core *ProverCoreConfig `json:"core"`
DBPath string `json:"db_path"`
Coordinator *CoordinatorConfig `json:"coordinator"`
}

// ProverCoreConfig load zk prover config.
Expand All @@ -29,6 +30,12 @@ type ProverCoreConfig struct {
DumpDir string `json:"dump_dir,omitempty"`
}

// CoordinatorConfig represents the configuration for the Coordinator client.
type CoordinatorConfig struct {
Timeout int `json:"timeout"`
BaseURL string `json:"base_url"`
}

// NewConfig returns a new instance of Config.
func NewConfig(file string) (*Config, error) {
buf, err := os.ReadFile(filepath.Clean(file))
Expand All @@ -46,5 +53,8 @@ func NewConfig(file string) (*Config, error) {
return nil, err
}
}
if cfg.Coordinator == nil || cfg.Coordinator.BaseURL == "" {
return nil, errors.New("missing coordinator config or base_url in configuration")
}
return cfg, nil
}
Loading

0 comments on commit c0fc89b

Please sign in to comment.