Skip to content

Commit

Permalink
feat: new bridge-history apis
Browse files Browse the repository at this point in the history
  • Loading branch information
colinlyguo committed Dec 6, 2023
1 parent 144c7ed commit f011fd5
Show file tree
Hide file tree
Showing 53 changed files with 2,848 additions and 3,837 deletions.
6 changes: 3 additions & 3 deletions bridge-history-api/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ test:
bridgehistoryapi-db-cli:
go build -o $(PWD)/build/bin/bridgehistoryapi-db-cli ./cmd/db_cli

bridgehistoryapi-cross-msg-fetcher:
go build -o $(PWD)/build/bin/bridgehistoryapi-cross-msg-fetcher ./cmd/cross_msg_fetcher
bridgehistoryapi-cross-message-fetcher:
go build -o $(PWD)/build/bin/bridgehistoryapi-cross-message-fetcher ./cmd/cross_message_fetcher

bridgehistoryapi-server:
go build -o $(PWD)/build/bin/bridgehistoryapi-server ./cmd/backend_server
Expand All @@ -21,6 +21,6 @@ db-docker:
docker run --name bridgehistoryapi-history-db -p 5444:5432 -e POSTGRES_PASSWORD=123456 -e POSTGRES_DB=test -d postgres

bridgehistoryapi-docker:
DOCKER_BUILDKIT=1 docker build -t scrolltech/bridgehistoryapi-cross-msg-fetcher:${IMAGE_VERSION} ${REPO_ROOT_DIR}/ -f ${REPO_ROOT_DIR}/build/dockerfiles/bridgehistoryapi-cross-msg-fetcher.Dockerfile
DOCKER_BUILDKIT=1 docker build -t scrolltech/bridgehistoryapi-cross-message-fetcher:${IMAGE_VERSION} ${REPO_ROOT_DIR}/ -f ${REPO_ROOT_DIR}/build/dockerfiles/bridgehistoryapi-cross-message-fetcher.Dockerfile
DOCKER_BUILDKIT=1 docker build -t scrolltech/bridgehistoryapi-server:${IMAGE_VERSION} ${REPO_ROOT_DIR}/ -f ${REPO_ROOT_DIR}/build/dockerfiles/bridgehistoryapi-server.Dockerfile
DOCKER_BUILDKIT=1 docker build -t scrolltech/bridgehistoryapi-db-cli:${IMAGE_VERSION} ${REPO_ROOT_DIR}/ -f ${REPO_ROOT_DIR}/build/dockerfiles/bridgehistoryapi-db-cli.Dockerfile
30 changes: 16 additions & 14 deletions bridge-history-api/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ Provide init, show version, rollback, check status services of DB

### bridgehistoryapi-cross-msg-fetcher

Fetch the transactions from both l1 and l2
Fetch the transactions from both L1 and L2
```
cd ./bridge-history-api
make bridgehistoryapi-cross-msg-fetcher
Expand Down Expand Up @@ -49,34 +49,36 @@ can change this port thru modify `config.json`
// @Router /api/txs [get]
```

2. `/txsbyhashes`
2. `/withdrawals`
```
// @Summary get txs by given tx hashes
// @Summary get all L2 withdrawals under given address
// @Accept plain
// @Produce plain
// @Param hashes query string array true "array of hashes list"
// @Success 200
// @Router /api/txsbyhashes [post]
// @Param address query string true "wallet address"
// @Param page_size query int true "page size"
// @Param page query int true "page"
// @Success 200
// @Router /api/withdrawals [get]
```

3. `/claimable`
3. `/claimablewithdrawals`
```
// @Summary get all claimable txs under given address
// @Summary get all L2 claimable withdrawals under given address
// @Accept plain
// @Produce plain
// @Param address query string true "wallet address"
// @Param page_size query int true "page size"
// @Param page query int true "page"
// @Success 200
// @Router /api/claimable [get]
// @Router /api/claimablewithdrawals [get]
```

4. `/withdraw_root`
4. `/txsbyhashes`
```
// @Summary get withdraw_root of given batch index
// @Summary get txs by given tx hashes
// @Accept plain
// @Produce plain
// @Param batch_index query string true "batch_index"
// @Param hashes query string array true "array of hashes"
// @Success 200
// @Router /api/withdraw_root [get]
```
// @Router /api/txsbyhashes [post]
```
379 changes: 131 additions & 248 deletions bridge-history-api/abi/backend_abi.go

Large diffs are not rendered by default.

14 changes: 9 additions & 5 deletions bridge-history-api/cmd/backend_server/app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,10 @@ import (
"os"
"os/signal"

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

"bridge-history-api/config"
Expand Down Expand Up @@ -51,16 +52,19 @@ func action(ctx *cli.Context) error {
log.Error("failed to close db", "err", err)
}
}()
// init Prover Stats API
port := cfg.Server.HostPort
redis := redis.NewClient(&redis.Options{
Addr: cfg.Redis.Address,
Password: cfg.Redis.Password,
DB: cfg.Redis.DB,
})
controller.InitController(db, redis)

router := gin.Default()
controller.InitController(db)

registry := prometheus.DefaultRegisterer
route.Route(router, cfg, registry)

go func() {
port := cfg.Server.HostPort
if runServerErr := router.Run(fmt.Sprintf(":%s", port)); runServerErr != nil {
log.Crit("run http server failure", "error", runServerErr)
}
Expand Down
96 changes: 96 additions & 0 deletions bridge-history-api/cmd/cross_message_fetcher/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/scroll-tech/go-ethereum/ethclient"
"github.com/scroll-tech/go-ethereum/log"
"github.com/urfave/cli/v2"

"bridge-history-api/config"
"bridge-history-api/crossmessage/controller/eventfetcher"
"bridge-history-api/utils"
)

var (
app *cli.App
)

func init() {
app = cli.NewApp()

app.Action = action
app.Name = "Scroll Bridge History API"
app.Usage = "The Scroll Bridge Web Backend"
app.Flags = append(app.Flags, utils.CommonFlags...)
app.Commands = []*cli.Command{}

app.Before = func(ctx *cli.Context) error {
return utils.LogSetup(ctx)
}
}

func action(ctx *cli.Context) error {
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)
}
subCtx, cancel := context.WithCancel(ctx.Context)
defer cancel()

l1Client, err := ethclient.Dial(cfg.L1.Endpoint)
if err != nil {
log.Crit("failed to connect to L1 geth", "endpoint", cfg.L1.Endpoint, "err", err)
}

l2Client, err := ethclient.Dial(cfg.L2.Endpoint)
if err != nil {
log.Crit("failed to connect to L2 geth", "endpoint", cfg.L2.Endpoint, "err", err)
}

db, err := utils.InitDB(cfg.DB)
if err != nil {
log.Crit("failed to init db", "err", err)
}
defer func() {
if deferErr := utils.CloseDB(db); deferErr != nil {
log.Error("failed to close db", "err", err)
}
}()
if err != nil {
log.Crit("failed to connect to db", "config file", cfgFile, "error", err)
}

l1MessageFetcher, err := eventfetcher.NewL1MessageFetcher(subCtx, cfg.L1, db, l1Client)
if err != nil {
log.Crit("failed to create L1 cross message fetcher", "error", err)
}
go l1MessageFetcher.Start()

l2MessageFetcher, err := eventfetcher.NewL2MessageFetcher(subCtx, cfg.L2, db, l2Client)
if err != nil {
log.Crit("failed to create L2 cross message fetcher", "error", err)
}
go l2MessageFetcher.Start()

// 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 event watcher cmd instance.
func Run() {
if err := app.Run(os.Args); err != nil {
_, _ = fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
}
7 changes: 7 additions & 0 deletions bridge-history-api/cmd/cross_message_fetcher/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package main

import "bridge-history-api/cmd/cross_message_fetcher/app"

func main() {
app.Run()
}
168 changes: 0 additions & 168 deletions bridge-history-api/cmd/cross_msg_fetcher/app/app.go

This file was deleted.

7 changes: 0 additions & 7 deletions bridge-history-api/cmd/cross_msg_fetcher/main.go

This file was deleted.

Loading

0 comments on commit f011fd5

Please sign in to comment.