Skip to content

Commit

Permalink
refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
colinlyguo committed Dec 6, 2023
1 parent bdbddc3 commit 0803dd9
Show file tree
Hide file tree
Showing 41 changed files with 144 additions and 947 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,12 @@ import (
"github.com/scroll-tech/go-ethereum/log"
"github.com/urfave/cli/v2"

"bridge-history-api/config"
"bridge-history-api/internal/controller"
"bridge-history-api/internal/route"
"bridge-history-api/observability"
"bridge-history-api/utils"
"scroll-tech/bridge-history-api/internal/config"
"scroll-tech/bridge-history-api/internal/controller/api"
"scroll-tech/bridge-history-api/internal/route"
"scroll-tech/common/database"
"scroll-tech/common/observability"
"scroll-tech/common/utils"
)

var (
Expand Down Expand Up @@ -43,12 +44,12 @@ func action(ctx *cli.Context) error {
if err != nil {
log.Crit("failed to load config file", "config file", cfgFile, "error", err)
}
db, err := utils.InitDB(cfg.DB)
db, err := database.InitDB(cfg.DB)
if err != nil {
log.Crit("failed to init db", "err", err)
}
defer func() {
if deferErr := utils.CloseDB(db); deferErr != nil {
if deferErr := database.CloseDB(db); deferErr != nil {
log.Error("failed to close db", "err", err)
}
}()
Expand All @@ -57,7 +58,7 @@ func action(ctx *cli.Context) error {
Password: cfg.Redis.Password,
DB: cfg.Redis.DB,
})
controller.InitController(db, redis)
api.InitController(db, redis)

router := gin.Default()
registry := prometheus.DefaultRegisterer
Expand Down
7 changes: 7 additions & 0 deletions bridge-history-api/cmd/api/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package main

import "scroll-tech/bridge-history-api/cmd/api/app"

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

This file was deleted.

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

This file was deleted.

2 changes: 1 addition & 1 deletion bridge-history-api/cmd/db_cli/app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (

"github.com/urfave/cli/v2"

"bridge-history-api/utils"
"scroll-tech/common/utils"
)

var (
Expand Down
11 changes: 6 additions & 5 deletions bridge-history-api/cmd/db_cli/app/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,10 @@ import (
"github.com/urfave/cli/v2"
"gorm.io/gorm"

"bridge-history-api/config"
"bridge-history-api/orm/migrate"
"bridge-history-api/utils"
"scroll-tech/bridge-history-api/internal/config"
"scroll-tech/bridge-history-api/internal/orm/migrate"
"scroll-tech/common/database"
"scroll-tech/common/utils"
)

func getConfig(ctx *cli.Context) (*config.Config, error) {
Expand All @@ -19,8 +20,8 @@ func getConfig(ctx *cli.Context) (*config.Config, error) {
return dbCfg, nil
}

func initDB(dbCfg *config.DBConfig) (*gorm.DB, error) {
return utils.InitDB(dbCfg)
func initDB(dbCfg *database.Config) (*gorm.DB, error) {
return database.InitDB(dbCfg)
}

// resetDB clean or reset database.
Expand Down
2 changes: 1 addition & 1 deletion bridge-history-api/cmd/db_cli/main.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package main

import "bridge-history-api/cmd/db_cli/app"
import "scroll-tech/bridge-history-api/cmd/db_cli/app"

func main() {
app.Run()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,10 @@ import (
"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"
"scroll-tech/bridge-history-api/internal/config"
"scroll-tech/bridge-history-api/internal/controller/fetcher"
"scroll-tech/common/database"
"scroll-tech/common/utils"
)

var (
Expand Down Expand Up @@ -52,26 +53,26 @@ func action(ctx *cli.Context) error {
log.Crit("failed to connect to L2 geth", "endpoint", cfg.L2.Endpoint, "err", err)
}

db, err := utils.InitDB(cfg.DB)
db, err := database.InitDB(cfg.DB)
if err != nil {
log.Crit("failed to init db", "err", err)
}
defer func() {
if deferErr := utils.CloseDB(db); deferErr != nil {
if deferErr := database.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)
l1MessageFetcher, err := fetcher.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)
l2MessageFetcher, err := fetcher.NewL2MessageFetcher(subCtx, cfg.L2, db, l2Client)
if err != nil {
log.Crit("failed to create L2 cross message fetcher", "error", err)
}
Expand Down
7 changes: 7 additions & 0 deletions bridge-history-api/cmd/fetcher/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package main

import "scroll-tech/bridge-history-api/cmd/fetcher/app"

func main() {
app.Run()
}
File renamed without changes.
2 changes: 1 addition & 1 deletion bridge-history-api/go.mod
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
module bridge-history-api
module scroll-tech/bridge-history-api

go 1.19

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import (
"encoding/json"
"os"
"path/filepath"

"scroll-tech/common/database"
)

// LayerConfig is the configuration of Layer1/Layer2
Expand All @@ -29,14 +31,6 @@ type LayerConfig struct {
MessageQueueAddr string `json:"MessageQueueAddr"`
}

// DBConfig db config
type DBConfig struct {
DSN string `json:"dsn"`
DriverName string `json:"driverName"`
MaxOpenNum int `json:"maxOpenNum"`
MaxIdleNum int `json:"maxIdleNum"`
}

// RedisConfig redis config
type RedisConfig struct {
Address string `json:"address"`
Expand All @@ -51,11 +45,11 @@ type ServerConfig struct {

// Config is the configuration of the bridge history backend
type Config struct {
L1 *LayerConfig `json:"L1"`
L2 *LayerConfig `json:"L2"`
DB *DBConfig `json:"db"`
Redis *RedisConfig `json:"redis"`
Server *ServerConfig `json:"server"`
L1 *LayerConfig `json:"L1"`
L2 *LayerConfig `json:"L2"`
DB *database.Config `json:"db"`
Redis *RedisConfig `json:"redis"`
Server *ServerConfig `json:"server"`
}

// NewConfig returns a new instance of Config.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package controller
package api

import (
"sync"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package controller
package api

import (
"context"
Expand All @@ -14,8 +14,8 @@ import (
"golang.org/x/sync/singleflight"
"gorm.io/gorm"

"bridge-history-api/internal/logic"
"bridge-history-api/internal/types"
"scroll-tech/bridge-history-api/internal/logic"
"scroll-tech/bridge-history-api/internal/types"
)

const (
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package controller
package api

import (
"sync"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package eventfetcher
package fetcher

import (
"context"
Expand All @@ -12,11 +12,11 @@ import (
"github.com/scroll-tech/go-ethereum/log"
"gorm.io/gorm"

backendabi "bridge-history-api/abi"
"bridge-history-api/config"
"bridge-history-api/crossmessage/logic"
"bridge-history-api/orm"
"bridge-history-api/utils"
backendabi "scroll-tech/bridge-history-api/abi"
"scroll-tech/bridge-history-api/internal/config"
"scroll-tech/bridge-history-api/internal/logic"
"scroll-tech/bridge-history-api/internal/orm"
"scroll-tech/bridge-history-api/internal/utils"
)

// L1MessageFetcher fetches cross message events from L1 and saves them to database.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package eventfetcher
package fetcher

import (
"context"
Expand All @@ -14,12 +14,11 @@ import (
"github.com/scroll-tech/go-ethereum/rpc"
"gorm.io/gorm"

backendabi "bridge-history-api/abi"
"bridge-history-api/config"
"bridge-history-api/crossmessage/controller/messageproof"
"bridge-history-api/crossmessage/logic"
"bridge-history-api/orm"
"bridge-history-api/utils"
backendabi "scroll-tech/bridge-history-api/abi"
"scroll-tech/bridge-history-api/internal/config"
"scroll-tech/bridge-history-api/internal/logic"
"scroll-tech/bridge-history-api/internal/orm"
"scroll-tech/bridge-history-api/internal/utils"
)

// L2MessageFetcher fetches cross message events from L2 and saves them to database.
Expand Down Expand Up @@ -215,7 +214,7 @@ func (c *L2MessageFetcher) doFetchAndSaveEvents(ctx context.Context, from uint64
}

func (c *L2MessageFetcher) updateL2WithdrawMessageProofs(ctx context.Context, l2WithdrawMessages []*orm.CrossMessage) error {
withdrawTrie := messageproof.NewWithdrawTrie()
withdrawTrie := utils.NewWithdrawTrie()
message, err := c.crossMessageOrm.GetLatestL2Withdrawal(ctx)
if err != nil {
log.Error("failed to get latest L2 sent message event", "err", err)
Expand Down
6 changes: 3 additions & 3 deletions bridge-history-api/internal/logic/history_logic.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ import (
"github.com/scroll-tech/go-ethereum/common"
"gorm.io/gorm"

"bridge-history-api/internal/types"
"bridge-history-api/orm"
"scroll-tech/bridge-history-api/internal/orm"
"scroll-tech/bridge-history-api/internal/types"
)

// HistoryLogic services.
Expand Down Expand Up @@ -85,7 +85,7 @@ func getTxHistoryInfo(message *orm.CrossMessage) *types.TxHistoryInfo {
L1Token: message.L1TokenAddress,
L2Token: message.L2TokenAddress,
IsL1: orm.MessageType(message.MessageType) == orm.MessageTypeL1SentMessage,
TxStatus: orm.TxStatusType(message.TxStatus),
TxStatus: message.TxStatus,
CreatedAt: &message.CreatedAt,
}
if txHistory.IsL1 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ import (
"github.com/scroll-tech/go-ethereum/ethclient"
"github.com/scroll-tech/go-ethereum/log"

backendabi "bridge-history-api/abi"
"bridge-history-api/orm"
"bridge-history-api/utils"
backendabi "scroll-tech/bridge-history-api/abi"
"scroll-tech/bridge-history-api/internal/orm"
"scroll-tech/bridge-history-api/internal/utils"
)

// ParseL1CrossChainEventLogs parses L1 watched cross chain events.
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
15 changes: 8 additions & 7 deletions bridge-history-api/internal/route/route.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ import (
"github.com/gin-gonic/gin"
"github.com/prometheus/client_golang/prometheus"

"bridge-history-api/config"
"bridge-history-api/internal/controller"
"bridge-history-api/observability"
"scroll-tech/bridge-history-api/internal/config"
"scroll-tech/bridge-history-api/internal/controller/api"
"scroll-tech/common/observability"
)

// Route routes the APIs
Expand All @@ -25,9 +25,10 @@ func Route(router *gin.Engine, conf *config.Config, reg prometheus.Registerer) {
observability.Use(router, "bridge_history_api", reg)

r := router.Group("api/")
r.GET("/txs", controller.HistoryCtrler.GetTxsByAddress)
r.GET("/withdrawals", controller.HistoryCtrler.GetL2WithdrawalsByAddress)
r.GET("/claimablewithdrawals", controller.HistoryCtrler.GetL2ClaimableWithdrawalsByAddress)

r.POST("/txsbyhashes", controller.HistoryCtrler.PostQueryTxsByHashes)
r.GET("/txs", api.HistoryCtrler.GetTxsByAddress)
r.GET("/withdrawals", api.HistoryCtrler.GetL2WithdrawalsByAddress)
r.GET("/claimablewithdrawals", api.HistoryCtrler.GetL2ClaimableWithdrawalsByAddress)

r.POST("/txsbyhashes", api.HistoryCtrler.PostQueryTxsByHashes)
}
24 changes: 11 additions & 13 deletions bridge-history-api/internal/types/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@ import (
"time"

"github.com/gin-gonic/gin"

"bridge-history-api/orm"
)

const (
Expand Down Expand Up @@ -71,17 +69,17 @@ type UserClaimInfo struct {

// TxHistoryInfo the schema of tx history infos
type TxHistoryInfo struct {
Hash string `json:"hash"`
MsgHash string `json:"msgHash"`
Amount string `json:"amount"`
IsL1 bool `json:"isL1"`
L1Token string `json:"l1Token"`
L2Token string `json:"l2Token"`
BlockNumber uint64 `json:"blockNumber"`
TxStatus orm.TxStatusType `json:"txStatus"`
FinalizeTx *Finalized `json:"finalizeTx"`
ClaimInfo *UserClaimInfo `json:"claimInfo"`
CreatedAt *time.Time `json:"createdTime"`
Hash string `json:"hash"`
MsgHash string `json:"msgHash"`
Amount string `json:"amount"`
IsL1 bool `json:"isL1"`
L1Token string `json:"l1Token"`
L2Token string `json:"l2Token"`
BlockNumber uint64 `json:"blockNumber"`
TxStatus int `json:"txStatus"`
FinalizeTx *Finalized `json:"finalizeTx"`
ClaimInfo *UserClaimInfo `json:"claimInfo"`
CreatedAt *time.Time `json:"createdTime"`
}

// RenderJSON renders response with json
Expand Down
Loading

0 comments on commit 0803dd9

Please sign in to comment.