Skip to content

Commit

Permalink
refactor(bridge-history-api): fix go lint (#638)
Browse files Browse the repository at this point in the history
Co-authored-by: vincent <419436363@qq.com>
Co-authored-by: colinlyguo <colinlyguo@scroll.io>
Co-authored-by: HAOYUatHZ <37070449+HAOYUatHZ@users.noreply.github.com>
  • Loading branch information
4 people committed Jul 18, 2023
1 parent ecd3a61 commit 577cc90
Show file tree
Hide file tree
Showing 27 changed files with 263 additions and 100 deletions.
28 changes: 14 additions & 14 deletions .github/workflows/bridge_history_api.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,20 +25,20 @@ defaults:
working-directory: 'bridge-history-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
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
Expand Down
29 changes: 18 additions & 11 deletions bridge-history-api/cmd/backend_server/app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,37 +24,40 @@ var (
var database db.OrmFactory

func pong(ctx iris.Context) {
ctx.WriteString("pong")
_, err := ctx.WriteString("pong")
if err != nil {
log.Error("failed to write pong", "err", err)
}
}

func setupQueryByAddressHandler(backend_app *mvc.Application) {
func setupQueryByAddressHandler(backendApp *mvc.Application) {
// Register Dependencies.
backend_app.Register(
backendApp.Register(
database,
service.NewHistoryService,
)

// Register Controllers.
backend_app.Handle(new(controller.QueryAddressController))
backendApp.Handle(new(controller.QueryAddressController))
}

func setupQueryClaimableHandler(backend_app *mvc.Application) {
func setupQueryClaimableHandler(backendApp *mvc.Application) {
// Register Dependencies.
backend_app.Register(
backendApp.Register(
database,
service.NewHistoryService,
)

// Register Controllers.
backend_app.Handle(new(controller.QueryClaimableController))
backendApp.Handle(new(controller.QueryClaimableController))
}

func setupQueryByHashHandler(backend_app *mvc.Application) {
backend_app.Register(
func setupQueryByHashHandler(backendApp *mvc.Application) {
backendApp.Register(
database,
service.NewHistoryService,
)
backend_app.Handle(new(controller.QueryHashController))
backendApp.Handle(new(controller.QueryHashController))
}

func init() {
Expand Down Expand Up @@ -87,7 +90,11 @@ func action(ctx *cli.Context) error {
if err != nil {
log.Crit("can not connect to database", "err", err)
}
defer database.Close()
defer func() {
if err = database.Close(); err != nil {
log.Error("failed to close database", "err", err)
}
}()
bridgeApp := iris.New()
bridgeApp.UseRouter(corsOptions)
bridgeApp.Get("/ping", pong).Describe("healthcheck")
Expand Down
27 changes: 16 additions & 11 deletions bridge-history-api/cmd/cross_msg_fetcher/app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ import (
"github.com/urfave/cli/v2"

"bridge-history-api/config"
"bridge-history-api/cross_msg"
"bridge-history-api/cross_msg/message_proof"
"bridge-history-api/crossmsg"
"bridge-history-api/crossmsg/messageproof"
"bridge-history-api/db"
cutils "bridge-history-api/utils"
)
Expand Down Expand Up @@ -54,15 +54,20 @@ func action(ctx *cli.Context) error {
if err != nil {
log.Crit("failed to connect l2 geth", "config file", cfgFile, "error", err)
}

db, err := db.NewOrmFactory(cfg)
defer db.Close()
defer func() {
if deferErr := db.Close(); 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)
}

l1worker := &cross_msg.FetchEventWorker{F: cross_msg.L1FetchAndSaveEvents, G: cross_msg.GetLatestL1ProcessedHeight, Name: "L1 events fetch Worker"}
l1worker := &crossmsg.FetchEventWorker{F: crossmsg.L1FetchAndSaveEvents, G: crossmsg.GetLatestL1ProcessedHeight, Name: "L1 events fetch Worker"}

l2worker := &cross_msg.FetchEventWorker{F: cross_msg.L2FetchAndSaveEvents, G: cross_msg.GetLatestL2ProcessedHeight, Name: "L2 events fetch Worker"}
l2worker := &crossmsg.FetchEventWorker{F: crossmsg.L2FetchAndSaveEvents, G: crossmsg.GetLatestL2ProcessedHeight, Name: "L2 events fetch Worker"}

l1AddressList := []common.Address{
common.HexToAddress(cfg.L1.CustomERC20GatewayAddr),
Expand All @@ -84,15 +89,15 @@ func action(ctx *cli.Context) error {
common.HexToAddress(cfg.L2.WETHGatewayAddr),
}

l1crossMsgFetcher, err := cross_msg.NewCrossMsgFetcher(subCtx, cfg.L1, db, l1client, l1worker, l1AddressList, cross_msg.L1ReorgHandling)
l1crossMsgFetcher, err := crossmsg.NewMsgFetcher(subCtx, cfg.L1, db, l1client, l1worker, l1AddressList, crossmsg.L1ReorgHandling)
if err != nil {
log.Crit("failed to create l1 cross message fetcher", "error", err)
}

go l1crossMsgFetcher.Start()
defer l1crossMsgFetcher.Stop()

l2crossMsgFetcher, err := cross_msg.NewCrossMsgFetcher(subCtx, cfg.L2, db, l2client, l2worker, l2AddressList, cross_msg.L2ReorgHandling)
l2crossMsgFetcher, err := crossmsg.NewMsgFetcher(subCtx, cfg.L2, db, l2client, l2worker, l2AddressList, crossmsg.L2ReorgHandling)
if err != nil {
log.Crit("failed to create l2 cross message fetcher", "error", err)
}
Expand All @@ -101,17 +106,17 @@ func action(ctx *cli.Context) error {
defer l2crossMsgFetcher.Stop()

// BlockTimestamp fetcher for l1 and l2
l1BlockTimeFetcher := cross_msg.NewBlockTimestampFetcher(subCtx, cfg.L1.Confirmation, int(cfg.L1.BlockTime), l1client, db.UpdateL1BlockTimestamp, db.GetL1EarliestNoBlockTimestampHeight)
l1BlockTimeFetcher := crossmsg.NewBlockTimestampFetcher(subCtx, cfg.L1.Confirmation, int(cfg.L1.BlockTime), l1client, db.UpdateL1BlockTimestamp, db.GetL1EarliestNoBlockTimestampHeight)
go l1BlockTimeFetcher.Start()
defer l1BlockTimeFetcher.Stop()

l2BlockTimeFetcher := cross_msg.NewBlockTimestampFetcher(subCtx, cfg.L2.Confirmation, int(cfg.L2.BlockTime), l2client, db.UpdateL2BlockTimestamp, db.GetL2EarliestNoBlockTimestampHeight)
l2BlockTimeFetcher := crossmsg.NewBlockTimestampFetcher(subCtx, cfg.L2.Confirmation, int(cfg.L2.BlockTime), l2client, db.UpdateL2BlockTimestamp, db.GetL2EarliestNoBlockTimestampHeight)
go l2BlockTimeFetcher.Start()
defer l2BlockTimeFetcher.Stop()

// Proof updater and batch fetcher
l2msgProofUpdater := message_proof.NewMsgProofUpdater(subCtx, cfg.L1.Confirmation, cfg.BatchInfoFetcher.BatchIndexStartBlock, db)
batchFetcher := cross_msg.NewBatchInfoFetcher(subCtx, common.HexToAddress(cfg.BatchInfoFetcher.ScrollChainAddr), cfg.BatchInfoFetcher.BatchIndexStartBlock, cfg.L1.Confirmation, int(cfg.L1.BlockTime), l1client, db, l2msgProofUpdater)
l2msgProofUpdater := messageproof.NewMsgProofUpdater(subCtx, cfg.L1.Confirmation, cfg.BatchInfoFetcher.BatchIndexStartBlock, db)
batchFetcher := crossmsg.NewBatchInfoFetcher(subCtx, common.HexToAddress(cfg.BatchInfoFetcher.ScrollChainAddr), cfg.BatchInfoFetcher.BatchIndexStartBlock, cfg.L1.Confirmation, int(cfg.L1.BlockTime), l1client, db, l2msgProofUpdater)
go batchFetcher.Start()
defer batchFetcher.Stop()

Expand Down
3 changes: 3 additions & 0 deletions bridge-history-api/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"path/filepath"
)

// BatchInfoFetcherConfig is the configuration of BatchInfoFetcher
type BatchInfoFetcherConfig struct {
BatchIndexStartBlock uint64 `json:"batchIndexStartBlock"`
ScrollChainAddr string `json:"ScrollChainAddr"`
Expand All @@ -21,6 +22,7 @@ type DBConfig struct {
MaxIdleNum int `json:"maxIdleNum"`
}

// LayerConfig is the configuration of Layer1/Layer2
type LayerConfig struct {
Confirmation uint64 `json:"confirmation"`
Endpoint string `json:"endpoint"`
Expand All @@ -35,6 +37,7 @@ type LayerConfig struct {
CustomERC20GatewayAddr string `json:"CustomERC20GatewayAddr"`
}

// ServerConfig is the configuration of the bridge history backend server port
type ServerConfig struct {
HostPort string `json:"hostPort"`
}
Expand Down
6 changes: 6 additions & 0 deletions bridge-history-api/controller/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,22 @@ import (
"github.com/ethereum/go-ethereum/common"
)

// QueryAddressController contains the query by address service
type QueryAddressController struct {
Service service.HistoryService
}

// QueryHashController contains the query by hash service
type QueryHashController struct {
Service service.HistoryService
}

// QueryClaimableController contains the query claimable txs service
type QueryClaimableController struct {
Service service.HistoryService
}

// Get defines the http get method behavior for QueryClaimableController
func (c *QueryClaimableController) Get(req model.QueryByAddressRequest) (*model.QueryByAddressResponse, error) {
txs, total, err := c.Service.GetClaimableTxsByAddress(common.HexToAddress(req.Address), int64(req.Offset), int64(req.Limit))
if err != nil {
Expand All @@ -32,6 +36,7 @@ func (c *QueryClaimableController) Get(req model.QueryByAddressRequest) (*model.
}}, nil
}

// Get defines the http get method behavior for QueryAddressController
func (c *QueryAddressController) Get(req model.QueryByAddressRequest) (*model.QueryByAddressResponse, error) {
message, total, err := c.Service.GetTxsByAddress(common.HexToAddress(req.Address), int64(req.Offset), int64(req.Limit))
if err != nil {
Expand All @@ -45,6 +50,7 @@ func (c *QueryAddressController) Get(req model.QueryByAddressRequest) (*model.Qu
}}, nil
}

// Post defines the http post method behavior for QueryHashController
func (c *QueryHashController) Post(req model.QueryByHashRequest) (*model.QueryByHashResponse, error) {
result, err := c.Service.GetTxsByHashes(req.Txs)
if err != nil {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package cross_msg
package crossmsg

import (
"context"
Expand All @@ -8,11 +8,12 @@ import (
"github.com/ethereum/go-ethereum/ethclient"
"github.com/ethereum/go-ethereum/log"

"bridge-history-api/cross_msg/message_proof"
"bridge-history-api/crossmsg/messageproof"
"bridge-history-api/db"
"bridge-history-api/utils"
)

// BatchInfoFetcher fetches batch info from l1 chain and update db
type BatchInfoFetcher struct {
ctx context.Context
scrollChainAddr common.Address
Expand All @@ -21,10 +22,11 @@ type BatchInfoFetcher struct {
blockTimeInSec int
client *ethclient.Client
db db.OrmFactory
msgProofUpdater *message_proof.MsgProofUpdater
msgProofUpdater *messageproof.MsgProofUpdater
}

func NewBatchInfoFetcher(ctx context.Context, scrollChainAddr common.Address, batchInfoStartNumber uint64, confirmation uint64, blockTimeInSec int, client *ethclient.Client, db db.OrmFactory, msgProofUpdater *message_proof.MsgProofUpdater) *BatchInfoFetcher {
// NewBatchInfoFetcher creates a new BatchInfoFetcher instance
func NewBatchInfoFetcher(ctx context.Context, scrollChainAddr common.Address, batchInfoStartNumber uint64, confirmation uint64, blockTimeInSec int, client *ethclient.Client, db db.OrmFactory, msgProofUpdater *messageproof.MsgProofUpdater) *BatchInfoFetcher {
return &BatchInfoFetcher{
ctx: ctx,
scrollChainAddr: scrollChainAddr,
Expand All @@ -37,13 +39,14 @@ func NewBatchInfoFetcher(ctx context.Context, scrollChainAddr common.Address, ba
}
}

// Start the BatchInfoFetcher
func (b *BatchInfoFetcher) Start() {
log.Info("BatchInfoFetcher Start")
// Fetch batch info at beginning
// Then start msg proof updater after db have some bridge batch
err := b.fetchBatchInfo()
if err != nil {
log.Error("fetch batch info at begining failed: ", "err", err)
log.Error("fetch batch info at beginning failed: ", "err", err)
}

go b.msgProofUpdater.Start()
Expand All @@ -65,6 +68,7 @@ func (b *BatchInfoFetcher) Start() {
}()
}

// Stop the BatchInfoFetcher and call msg proof updater to stop
func (b *BatchInfoFetcher) Stop() {
log.Info("BatchInfoFetcher Stop")
b.msgProofUpdater.Stop()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package cross_msg
package crossmsg

import (
"context"
Expand All @@ -9,9 +9,13 @@ import (
"github.com/ethereum/go-ethereum/log"
)

// GetEarliestNoBlockTimestampHeightFunc is a function type that gets the earliest record without block timestamp from database
type GetEarliestNoBlockTimestampHeightFunc func() (uint64, error)

// UpdateBlockTimestampFunc is a function type that updates block timestamp into database
type UpdateBlockTimestampFunc func(height uint64, timestamp time.Time) error

// BlockTimestampFetcher fetches block timestamp from blockchain and saves them to database
type BlockTimestampFetcher struct {
ctx context.Context
confirmation uint64
Expand All @@ -21,6 +25,7 @@ type BlockTimestampFetcher struct {
getEarliestNoBlockTimestampHeightFunc GetEarliestNoBlockTimestampHeightFunc
}

// NewBlockTimestampFetcher creates a new BlockTimestampFetcher instance
func NewBlockTimestampFetcher(ctx context.Context, confirmation uint64, blockTimeInSec int, client *ethclient.Client, updateBlockTimestampFunc UpdateBlockTimestampFunc, getEarliestNoBlockTimestampHeightFunc GetEarliestNoBlockTimestampHeightFunc) *BlockTimestampFetcher {
return &BlockTimestampFetcher{
ctx: ctx,
Expand All @@ -32,6 +37,7 @@ func NewBlockTimestampFetcher(ctx context.Context, confirmation uint64, blockTim
}
}

// Start the BlockTimestampFetcher
func (b *BlockTimestampFetcher) Start() {
go func() {
tick := time.NewTicker(time.Duration(b.blockTimeInSec) * time.Second)
Expand Down Expand Up @@ -73,6 +79,7 @@ func (b *BlockTimestampFetcher) Start() {
}()
}

// Stop the BlockTimestampFetcher and log the info
func (b *BlockTimestampFetcher) Stop() {
log.Info("BlockTimestampFetcher Stop")
}
Loading

0 comments on commit 577cc90

Please sign in to comment.