Skip to content

Commit

Permalink
chore: add logs for prover-proxy (#4)
Browse files Browse the repository at this point in the history
* chore: add logs for prover-proxy

* chore: add indent
  • Loading branch information
0xHansLee authored May 7, 2024
1 parent 73d9e11 commit 7fb7317
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 0 deletions.
3 changes: 3 additions & 0 deletions internal/ec2/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ func (c *Controller) StartIfNotRunning() error {
c.mu.Lock()
defer c.mu.Unlock()
if c.running {
log.Println("instance is already running")
return nil
}
for {
Expand All @@ -112,6 +113,7 @@ func (c *Controller) StartIfNotRunning() error {
time.Sleep(1 * time.Second)
}
_, err := c.client.StartInstances(&ec2.StartInstancesInput{InstanceIds: c.instanceIds()})
log.Printf("start instance (id: %s)", c.instanceId)
if err != nil {
log.Println(fmt.Errorf("failed to start ec2 instance %s: %w", c.instanceId, err))
return err
Expand All @@ -124,6 +126,7 @@ func (c *Controller) StopIfRunning() {
c.mu.Lock()
defer c.mu.Unlock()
if c.running {
log.Printf("stop instance (id: %s)", c.instanceId)
_, err := c.client.StopInstances(&ec2.StopInstancesInput{InstanceIds: c.instanceIds()})
if err == nil {
c.running = false
Expand Down
2 changes: 2 additions & 0 deletions internal/proof/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,10 +55,12 @@ func NewJsonRpcErrorFromErrorOrNil(err error) (rpcError *JsonRpcError) {
func (j *JsonRpcError) Error() string { return fmt.Sprintf("[%d] %s", j.Code, j.Message) }

func (d dialJsonRpcProverClient) Prove(traceString string) (*ProveResponse, error) {
log.Println("send request to generate proof to prover")
return send[ProveResponse](d.address, "prove", []any{traceString})
}

func (d dialJsonRpcProverClient) Spec() (*ProverSpecResponse, error) {
log.Println("send request of spec")
return send[ProverSpecResponse](d.address, "spec", nil)
}

Expand Down
3 changes: 3 additions & 0 deletions internal/proof/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"encoding/json"
"errors"
"fmt"
"log"
"net/http"
)

Expand Down Expand Up @@ -76,13 +77,15 @@ func (s *Server) serveJsonRpc(writer http.ResponseWriter, httpRequest *http.Requ
func (s *Server) callMethod(method string, params interface{}) (any, error) {
switch method {
case "prove":
log.Println("prove requested")
p := params.([]any)
traceString, traceStringOk := p[0].(string)
if !traceStringOk {
return nil, errors.New("failed to read traceString parameter")
}
return s.service.Prove(traceString)
case "spec":
log.Println("spec requested")
return s.service.Spec()
default:
return nil, fmt.Errorf("unsupported method %s", method)
Expand Down
3 changes: 3 additions & 0 deletions internal/proof/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ func NewService(disk *DiskRepository, ec2 *ec2.Controller) *Service {

func (s *Service) Prove(traceString string) (*ProveResponse, error) {
id, blockNumber := computeId(traceString), readBlockNumber(traceString)
log.Printf("request prove for block number %s to prover", blockNumber)
if proof := s.disk.Find(id); proof != nil {
return newProofResponseFromFileProof(proof)
}
Expand Down Expand Up @@ -79,6 +80,7 @@ func (s *Service) Prove(traceString string) (*ProveResponse, error) {
}

func (s *Service) Spec() (*ProverSpecResponse, error) {
log.Println("request spec to prover")
return withClient(s, func(c ProverClient) (*ProverSpecResponse, error) { return c.Spec() })
}

Expand All @@ -89,6 +91,7 @@ func (s *Service) Close() {
func withClient[R interface{}](s *Service, callback func(c ProverClient) (*R, error)) (*R, error) {
defer func() {
if len(s.inProgressProof) == 0 {
log.Println("there is no proof in progress. shut down if it is running.")
s.ec2.StopIfRunning()
}
}()
Expand Down

0 comments on commit 7fb7317

Please sign in to comment.