Skip to content

Commit 0a1e64c

Browse files
ChuhanJinJJVincentJJcolinlyguo0xmountaintop
authored
feat(bridge-history-api):add bridge-history-api (#459)
Co-authored-by: vincent <[email protected]> Co-authored-by: colinlyguo <[email protected]> Co-authored-by: HAOYUatHZ <[email protected]>
1 parent 58a10d9 commit 0a1e64c

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

58 files changed

+4153
-173
lines changed
+73
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
name: BridgeHistoryApi
2+
3+
on:
4+
push:
5+
branches:
6+
- main
7+
- staging
8+
- develop
9+
- alpha
10+
paths:
11+
- 'bridge-history-api/**'
12+
- '.github/workflows/bridge_history_api.yml'
13+
pull_request:
14+
branches:
15+
- main
16+
- staging
17+
- develop
18+
- alpha
19+
paths:
20+
- 'bridge-history-api/**'
21+
- '.github/workflows/bridge_history_api.yml'
22+
23+
defaults:
24+
run:
25+
working-directory: 'bridge-history-api'
26+
27+
jobs:
28+
check:
29+
# runs-on: ubuntu-latest
30+
# steps:
31+
# - name: Install Go
32+
# uses: actions/setup-go@v2
33+
# with:
34+
# go-version: 1.20.x
35+
# - name: Checkout code
36+
# uses: actions/checkout@v2
37+
# - name: Lint
38+
# run: |
39+
# rm -rf $HOME/.cache/golangci-lint
40+
# make lint
41+
test:
42+
runs-on: ubuntu-latest
43+
steps:
44+
- name: Install Go
45+
uses: actions/setup-go@v2
46+
with:
47+
go-version: 1.20.x
48+
- name: Checkout code
49+
uses: actions/checkout@v2
50+
- name: Test
51+
run: |
52+
go get ./...
53+
make test
54+
goimports-lint:
55+
runs-on: ubuntu-latest
56+
steps:
57+
- name: Install Go
58+
uses: actions/setup-go@v2
59+
with:
60+
go-version: 1.20.x
61+
- name: Checkout code
62+
uses: actions/checkout@v2
63+
- name: Install goimports
64+
run: go install golang.org/x/tools/cmd/goimports
65+
- run: goimports -local scroll-tech/bridge-history-api/ -w .
66+
- run: go mod tidy
67+
# If there are any diffs from goimports or go mod tidy, fail.
68+
- name: Verify no changes from goimports and go mod tidy
69+
run: |
70+
if [ -n "$(git status --porcelain)" ]; then
71+
exit 1
72+
fi
73+

.github/workflows/docker.yaml

+18
Original file line numberDiff line numberDiff line change
@@ -63,3 +63,21 @@ jobs:
6363
tags: scrolltech/rollup-relayer:${{github.ref_name}}
6464
# cache-from: type=gha,scope=${{ github.workflow }}
6565
# cache-to: type=gha,scope=${{ github.workflow }}
66+
- name: Build and push bridgehistoryapi-cross-msg-fetcher docker
67+
uses: docker/build-push-action@v2
68+
with:
69+
context: .
70+
file: ./build/dockerfiles/bridgehistoryapi-cross-msg-fetcher.Dockerfile
71+
push: true
72+
tags: scrolltech/bridgehistoryapi-cross-msg-fetcher:${{github.ref_name}}
73+
# cache-from: type=gha,scope=${{ github.workflow }}
74+
# cache-to: type=gha,scope=${{ github.workflow }}
75+
- name: Build and push bridgehistoryapi-server docker
76+
uses: docker/build-push-action@v2
77+
with:
78+
context: .
79+
file: ./build/dockerfiles/bridgehistoryapi-server.Dockerfile
80+
push: true
81+
tags: scrolltech/bridgehistoryapi-server:${{github.ref_name}}
82+
# cache-from: type=gha,scope=${{ github.workflow }}
83+
# cache-to: type=gha,scope=${{ github.workflow }}

Makefile

+1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ lint: ## The code's format and security checks.
1313
make -C coordinator lint
1414
make -C database lint
1515
make -C roller lint
16+
make -C bridge-history-api lint
1617

1718
update: ## update dependencies
1819
go work sync

bridge-history-api/.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/build/bin

bridge-history-api/Makefile

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
.PHONY: lint
2+
REPO_ROOT_DIR=./..
3+
IMAGE_VERSION=latest
4+
5+
lint: ## Lint the files - used for CI
6+
GOBIN=$(PWD)/build/bin go run ../build/lint.go
7+
8+
test:
9+
go test -v -race -coverprofile=coverage.txt -covermode=atomic -p 1 $(PWD)/...
10+
11+
bridgehistoryapi-db-cli:
12+
go build -o $(PWD)/build/bin/bridgehistoryapi-db-cli ./cmd/db_cli
13+
14+
bridgehistoryapi-cross-msg-fetcher:
15+
go build -o $(PWD)/build/bin/bridgehistoryapi-cross-msg-fetcher ./cmd/cross_msg_fetcher
16+
17+
bridgehistoryapi-server:
18+
go build -o $(PWD)/build/bin/bridgehistoryapi-server ./cmd/backend_server
19+
20+
db-docker:
21+
docker run --name bridgehistoryapi-history-db -p 5444:5432 -e POSTGRES_PASSWORD=123456 -e POSTGRES_DB=test -d postgres
22+
23+
bridgehistoryapi-docker:
24+
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
25+
DOCKER_BUILDKIT=1 docker build -t scrolltech/bridgehistoryapi-server:${IMAGE_VERSION} ${REPO_ROOT_DIR}/ -f ${REPO_ROOT_DIR}/build/dockerfiles/bridgehistoryapi-server.Dockerfile
26+
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

bridge-history-api/README.md

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# bridge-history-api

bridge-history-api/abi/backend_abi.go

+327
Large diffs are not rendered by default.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
package app
2+
3+
import (
4+
"fmt"
5+
"os"
6+
7+
"github.com/ethereum/go-ethereum/log"
8+
"github.com/kataras/iris/v12"
9+
"github.com/kataras/iris/v12/mvc"
10+
"github.com/urfave/cli/v2"
11+
12+
"bridge-history-api/config"
13+
"bridge-history-api/controller"
14+
"bridge-history-api/db"
15+
"bridge-history-api/service"
16+
cutils "bridge-history-api/utils"
17+
)
18+
19+
var (
20+
app *cli.App
21+
)
22+
23+
var database db.OrmFactory
24+
25+
func pong(ctx iris.Context) {
26+
ctx.WriteString("pong")
27+
}
28+
29+
func setupQueryByAddressHandler(backend_app *mvc.Application) {
30+
// Register Dependencies.
31+
backend_app.Register(
32+
database,
33+
service.NewHistoryService,
34+
)
35+
36+
// Register Controllers.
37+
backend_app.Handle(new(controller.QueryAddressController))
38+
}
39+
40+
func setupQueryByHashHandler(backend_app *mvc.Application) {
41+
backend_app.Register(
42+
database,
43+
service.NewHistoryService,
44+
)
45+
backend_app.Handle(new(controller.QueryHashController))
46+
}
47+
48+
func init() {
49+
app = cli.NewApp()
50+
51+
app.Action = action
52+
app.Name = "Scroll Bridge History Web Service"
53+
app.Usage = "The Scroll Bridge History Web Service"
54+
app.Flags = append(app.Flags, cutils.CommonFlags...)
55+
app.Commands = []*cli.Command{}
56+
57+
app.Before = func(ctx *cli.Context) error {
58+
return cutils.LogSetup(ctx)
59+
}
60+
}
61+
62+
func action(ctx *cli.Context) error {
63+
// Load config file.
64+
cfgFile := ctx.String(cutils.ConfigFileFlag.Name)
65+
cfg, err := config.NewConfig(cfgFile)
66+
if err != nil {
67+
log.Crit("failed to load config file", "config file", cfgFile, "error", err)
68+
}
69+
database, err = db.NewOrmFactory(cfg)
70+
if err != nil {
71+
log.Crit("can not connect to database", "err", err)
72+
}
73+
defer database.Close()
74+
bridgeApp := iris.New()
75+
bridgeApp.Get("/ping", pong).Describe("healthcheck")
76+
77+
mvc.Configure(bridgeApp.Party("/api/txs"), setupQueryByAddressHandler)
78+
mvc.Configure(bridgeApp.Party("/api/txsbyhashes"), setupQueryByHashHandler)
79+
80+
// TODO: make debug mode configurable
81+
err = bridgeApp.Listen(":8080", iris.WithLogLevel("debug"))
82+
if err != nil {
83+
log.Crit("can not start server", "err", err)
84+
}
85+
86+
return nil
87+
}
88+
89+
// Run event watcher cmd instance.
90+
func Run() {
91+
if err := app.Run(os.Args); err != nil {
92+
_, _ = fmt.Fprintln(os.Stderr, err)
93+
os.Exit(1)
94+
}
95+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package main
2+
3+
import "bridge-history-api/cmd/backend_server/app"
4+
5+
func main() {
6+
app.Run()
7+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
package app
2+
3+
import (
4+
"context"
5+
"fmt"
6+
"os"
7+
"os/signal"
8+
9+
"github.com/ethereum/go-ethereum/common"
10+
"github.com/ethereum/go-ethereum/ethclient"
11+
"github.com/ethereum/go-ethereum/log"
12+
"github.com/urfave/cli/v2"
13+
14+
"bridge-history-api/config"
15+
"bridge-history-api/cross_msg"
16+
"bridge-history-api/db"
17+
cutils "bridge-history-api/utils"
18+
)
19+
20+
var (
21+
app *cli.App
22+
)
23+
24+
func init() {
25+
app = cli.NewApp()
26+
27+
app.Action = action
28+
app.Name = "Scroll Bridge History API"
29+
app.Usage = "The Scroll Bridge Web Backend"
30+
app.Flags = append(app.Flags, cutils.CommonFlags...)
31+
app.Commands = []*cli.Command{}
32+
33+
app.Before = func(ctx *cli.Context) error {
34+
return cutils.LogSetup(ctx)
35+
}
36+
}
37+
38+
func action(ctx *cli.Context) error {
39+
// Load config file.
40+
cfgFile := ctx.String(cutils.ConfigFileFlag.Name)
41+
cfg, err := config.NewConfig(cfgFile)
42+
if err != nil {
43+
log.Crit("failed to load config file", "config file", cfgFile, "error", err)
44+
}
45+
subCtx, cancel := context.WithCancel(ctx.Context)
46+
defer cancel()
47+
48+
l1client, err := ethclient.Dial(cfg.L1.Endpoint)
49+
if err != nil {
50+
log.Crit("failed to connect l1 geth", "config file", cfgFile, "error", err)
51+
}
52+
l2client, err := ethclient.Dial(cfg.L2.Endpoint)
53+
if err != nil {
54+
log.Crit("failed to connect l2 geth", "config file", cfgFile, "error", err)
55+
}
56+
db, err := db.NewOrmFactory(cfg)
57+
if err != nil {
58+
log.Crit("failed to connect to db", "config file", cfgFile, "error", err)
59+
}
60+
61+
l1worker := &cross_msg.FetchEventWorker{F: cross_msg.L1FetchAndSaveEvents, G: cross_msg.GetLatestL1ProcessedHeight, Name: "L1 events fetch Worker"}
62+
63+
l2worker := &cross_msg.FetchEventWorker{F: cross_msg.L2FetchAndSaveEvents, G: cross_msg.GetLatestL2ProcessedHeight, Name: "L2 events fetch Worker"}
64+
65+
l1AddressList := []common.Address{
66+
common.HexToAddress(cfg.L1.CustomERC20GatewayAddr),
67+
common.HexToAddress(cfg.L1.ERC721GatewayAddr),
68+
common.HexToAddress(cfg.L1.ERC1155GatewayAddr),
69+
common.HexToAddress(cfg.L1.MessengerAddr),
70+
common.HexToAddress(cfg.L1.ETHGatewayAddr),
71+
common.HexToAddress(cfg.L1.StandardERC20Gateway),
72+
common.HexToAddress(cfg.L1.WETHGatewayAddr),
73+
}
74+
75+
l2AddressList := []common.Address{
76+
common.HexToAddress(cfg.L2.CustomERC20GatewayAddr),
77+
common.HexToAddress(cfg.L2.ERC721GatewayAddr),
78+
common.HexToAddress(cfg.L2.ERC1155GatewayAddr),
79+
common.HexToAddress(cfg.L2.MessengerAddr),
80+
common.HexToAddress(cfg.L2.ETHGatewayAddr),
81+
common.HexToAddress(cfg.L2.StandardERC20Gateway),
82+
common.HexToAddress(cfg.L2.WETHGatewayAddr),
83+
}
84+
85+
l1crossMsgFetcher, err := cross_msg.NewCrossMsgFetcher(subCtx, cfg.L1, db, l1client, l1worker, l1AddressList, cross_msg.L1ReorgHandling)
86+
if err != nil {
87+
log.Crit("failed to create l1 cross message fetcher", "error", err)
88+
}
89+
90+
go l1crossMsgFetcher.Start()
91+
defer l1crossMsgFetcher.Stop()
92+
93+
l2crossMsgFetcher, err := cross_msg.NewCrossMsgFetcher(subCtx, cfg.L2, db, l2client, l2worker, l2AddressList, cross_msg.L2ReorgHandling)
94+
if err != nil {
95+
log.Crit("failed to create l2 cross message fetcher", "error", err)
96+
}
97+
98+
go l2crossMsgFetcher.Start()
99+
defer l2crossMsgFetcher.Stop()
100+
101+
// Catch CTRL-C to ensure a graceful shutdown.
102+
interrupt := make(chan os.Signal, 1)
103+
signal.Notify(interrupt, os.Interrupt)
104+
105+
// Wait until the interrupt signal is received from an OS signal.
106+
<-interrupt
107+
108+
return nil
109+
}
110+
111+
// Run event watcher cmd instance.
112+
func Run() {
113+
if err := app.Run(os.Args); err != nil {
114+
_, _ = fmt.Fprintln(os.Stderr, err)
115+
os.Exit(1)
116+
}
117+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package main
2+
3+
import "bridge-history-api/cmd/cross_msg_fetcher/app"
4+
5+
func main() {
6+
app.Run()
7+
}

0 commit comments

Comments
 (0)