Skip to content

Commit

Permalink
feat(prover-stats-api): add prover stats API (#635)
Browse files Browse the repository at this point in the history
Co-authored-by: xinran chen <lawliet@xinran-m1x.local>
Co-authored-by: georgehao <haohongfan@gmail.com>
Co-authored-by: HAOYUatHZ <37070449+HAOYUatHZ@users.noreply.github.com>
  • Loading branch information
4 people committed Jul 20, 2023
1 parent 2d6a195 commit e3b451c
Show file tree
Hide file tree
Showing 31 changed files with 2,230 additions and 42 deletions.
80 changes: 80 additions & 0 deletions .github/workflows/prover_stats_api.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
name: ProverStatsAPI

on:
push:
branches:
- main
- staging
- develop
- alpha
paths:
- 'prover-stats-api/**'
- '.github/workflows/prover_stats_api.yml'
pull_request:
types:
- opened
- reopened
- synchronize
- ready_for_review
paths:
- 'prover-stats-api/**'
- '.github/workflows/prover_stats_api.yml'

defaults:
run:
working-directory: 'prover-stats-api'

jobs:
check:
if: github.event.pull_request.draft == false
runs-on: ubuntu-latest
steps:
- name: Install Go
uses: actions/setup-go@v2
with:
go-version: 1.19.x
- name: Checkout code
uses: actions/checkout@v2
- name: Lint
run: |
rm -rf $HOME/.cache/golangci-lint
make lint
test:
if: github.event.pull_request.draft == false
runs-on: ubuntu-latest
steps:
- name: Install Go
uses: actions/setup-go@v2
with:
go-version: 1.19.x
- name: Checkout code
uses: actions/checkout@v2
- name: Test
run: |
make test
- name: Upload coverage reports to Codecov
uses: codecov/codecov-action@v3
env:
CODECOV_TOKEN: ${{ secrets.CODECOV_TOKEN }}
with:
flags: prover-stats-api
goimports-lint:
if: github.event.pull_request.draft == false
runs-on: ubuntu-latest
steps:
- name: Install Go
uses: actions/setup-go@v2
with:
go-version: 1.19.x
- name: Checkout code
uses: actions/checkout@v2
- name: Install goimports
run: go install golang.org/x/tools/cmd/goimports
- run: goimports -local scroll-tech/prover-stats-api/ -w .
- run: go mod tidy
# If there are any diffs from goimports or go mod tidy, fail.
- name: Verify no changes from goimports and go mod tidy
run: |
if [ -n "$(git status --porcelain)" ]; then
exit 1
fi
2 changes: 1 addition & 1 deletion common/version/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"runtime/debug"
)

var tag = "v4.0.23"
var tag = "v4.0.24"

var commit = func() string {
if info, ok := debug.ReadBuildInfo(); ok {
Expand Down
1 change: 1 addition & 0 deletions go.work
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,5 @@ use (
./database
./roller
./tests/integration-test
./prover-stats-api
)
87 changes: 46 additions & 41 deletions go.work.sum

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions prover-stats-api/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/build/bin
16 changes: 16 additions & 0 deletions prover-stats-api/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
.PHONY: clean build test

build:
GOBIN=$(PWD)/build/bin go build -o $(PWD)/build/bin/prover-stats-api ./cmd

clean: ## Empty out the bin folder
@rm -rf build/bin

swag:
swag init -g ./cmd/main.go

test:
go test -v $(PWD)/...

lint: ## Lint the files - used for CI
GOBIN=$(PWD)/build/bin go run ../build/lint.go
30 changes: 30 additions & 0 deletions prover-stats-api/README
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# prover-stats-api

## how to get the prover-stats-api docs

### 1. start the prover-stats-api server

```
cd ./prover-stats-api
go build -o prover-stats ./cmd/
./prover-stats --config=./conf/config.json
```

you will get server run log
```
Listening and serving HTTP on :8990
```

### 2. browse the documents

open this documents in your browser
```
http://localhost:8990/swagger/index.html
```

## how to update the prover-stats-api docs

```
cd ./prover-stats-api
make swag
```
96 changes: 96 additions & 0 deletions prover-stats-api/cmd/app/app.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
package app

import (
"context"
"fmt"
"os"
"os/signal"

"github.com/gin-gonic/gin"
"github.com/scroll-tech/go-ethereum/log"
"github.com/urfave/cli/v2"

"scroll-tech/common/database"
"scroll-tech/common/metrics"
"scroll-tech/common/utils"
"scroll-tech/common/version"

"scroll-tech/prover-stats-api/internal/config"
"scroll-tech/prover-stats-api/internal/controller"
"scroll-tech/prover-stats-api/internal/route"
)

var app *cli.App

func init() {
// Set up prover-stats-api info.
app = cli.NewApp()
app.Action = action
app.Name = "Prover Stats API"
app.Usage = "The Scroll L2 ZK Prover Stats API"
app.Version = version.Version
app.Flags = append(app.Flags, utils.CommonFlags...)
app.Flags = append(app.Flags, apiFlags...)
app.Before = func(ctx *cli.Context) error {
return utils.LogSetup(ctx)
}
}

func action(ctx *cli.Context) error {
// Load config file.
cfgFile := ctx.String(utils.ConfigFileFlag.Name)
cfg, err := config.NewConfig(cfgFile)
if err != nil {
log.Crit("failed to load config file", "config file", cfgFile, "error", err)
}

// init db handler
db, err := database.InitDB(cfg.DBConfig)
if err != nil {
log.Crit("failed to init db connection", "err", err)
}
defer func() {
if err = database.CloseDB(db); err != nil {
log.Error("can not close ormFactory", "error", err)
}
}()

subCtx, cancel := context.WithCancel(ctx.Context)
defer func() {
cancel()
}()

// Start metrics server.
metrics.Serve(subCtx, ctx)

// init Prover Stats API
port := ctx.String(httpPortFlag.Name)

router := gin.Default()
controller.InitController(db)
route.Route(router, cfg)

go func() {
if runServerErr := router.Run(fmt.Sprintf(":%s", port)); runServerErr != nil {
log.Crit("run http server failure", "error", runServerErr)
}
}()

// Catch CTRL-C to ensure a graceful shutdown.
interrupt := make(chan os.Signal, 1)
signal.Notify(interrupt, os.Interrupt)

// Wait until the interrupt signal is received from an OS signal.
<-interrupt

return nil
}

// Run run prover-stats-api.
func Run() {
// RunApp the prover-stats-api.
if err := app.Run(os.Args); err != nil {
_, _ = fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
}
15 changes: 15 additions & 0 deletions prover-stats-api/cmd/app/flags.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package app

import "github.com/urfave/cli/v2"

var (
apiFlags = []cli.Flag{
&httpPortFlag,
}
// httpPortFlag set http.port.
httpPortFlag = cli.IntFlag{
Name: "http.port",
Usage: "HTTP server listening port",
Value: 8990,
}
)
24 changes: 24 additions & 0 deletions prover-stats-api/cmd/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package main

import (
"scroll-tech/prover-stats-api/cmd/app"
_ "scroll-tech/prover-stats-api/docs"
)

// @title Scroll Prover Stats API
// @version 1.0
// @description This is an API server for Provers.

// @contact.name Prover Stats API Support
// @contact.email Be Pending

// @license.name Apache 2.0
// @license.url http://www.apache.org/licenses/LICENSE-2.0.html

// @host localhost:8990
// @BasePath /api/v1

// @securityDefinitions.basic BasicAuth
func main() {
app.Run()
}
12 changes: 12 additions & 0 deletions prover-stats-api/conf/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"db_config": {
"driver_name": "postgres",
"dsn": "postgres://localhost/scroll?sslmode=disable",
"maxOpenNum": 200,
"maxIdleNum": 20
},
"auth": {
"secret": "prover secret key",
"token_expire_duration": 3600
}
}
Loading

0 comments on commit e3b451c

Please sign in to comment.