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 4, 2023
1 parent 144c7ed commit f09f43c
Show file tree
Hide file tree
Showing 42 changed files with 1,998 additions and 3,321 deletions.
28 changes: 15 additions & 13 deletions bridge-history-api/README.md
Original file line number Diff line number Diff line change
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]
```
373 changes: 127 additions & 246 deletions bridge-history-api/abi/backend_abi.go

Large diffs are not rendered by default.

12 changes: 8 additions & 4 deletions bridge-history-api/cmd/backend_server/app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (

"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/urfave/cli/v2"

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
92 changes: 10 additions & 82 deletions bridge-history-api/cmd/cross_msg_fetcher/app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,12 @@ import (
"os"
"os/signal"

"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/ethclient"
"github.com/ethereum/go-ethereum/log"
"github.com/urfave/cli/v2"

"bridge-history-api/config"
"bridge-history-api/crossmsg"
"bridge-history-api/crossmsg/messageproof"
"bridge-history-api/orm"
"bridge-history-api/crossmessage/controller/messagefetcher"
"bridge-history-api/utils"
)

Expand All @@ -37,7 +34,6 @@ func init() {
}

func action(ctx *cli.Context) error {
// Load config file.
cfgFile := ctx.String(utils.ConfigFileFlag.Name)
cfg, err := config.NewConfig(cfgFile)
if err != nil {
Expand All @@ -46,13 +42,14 @@ func action(ctx *cli.Context) error {
subCtx, cancel := context.WithCancel(ctx.Context)
defer cancel()

l1client, err := ethclient.Dial(cfg.L1.Endpoint)
l1Client, err := ethclient.Dial(cfg.L1.Endpoint)
if err != nil {
log.Crit("failed to connect l1 geth", "config file", cfgFile, "error", err)
log.Crit("failed to connect to l1 geth", "endpoint", cfg.L1.Endpoint, "err", err)
}
l2client, err := ethclient.Dial(cfg.L2.Endpoint)

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

db, err := utils.InitDB(cfg.DB)
Expand All @@ -68,86 +65,17 @@ func action(ctx *cli.Context) error {
log.Crit("failed to connect to db", "config file", cfgFile, "error", err)
}

l1worker := &crossmsg.FetchEventWorker{F: crossmsg.L1FetchAndSaveEvents, G: crossmsg.GetLatestL1ProcessedHeight, Name: "L1 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),
common.HexToAddress(cfg.L1.ERC721GatewayAddr),
common.HexToAddress(cfg.L1.ERC1155GatewayAddr),
common.HexToAddress(cfg.L1.MessengerAddr),
common.HexToAddress(cfg.L1.ETHGatewayAddr),
common.HexToAddress(cfg.L1.StandardERC20Gateway),
common.HexToAddress(cfg.L1.WETHGatewayAddr),
}

if cfg.L1.USDCGatewayAddr != "" {
l1AddressList = append(l1AddressList, common.HexToAddress(cfg.L1.USDCGatewayAddr))
}

if cfg.L1.LIDOGatewayAddr != "" {
l1AddressList = append(l1AddressList, common.HexToAddress(cfg.L1.LIDOGatewayAddr))
}

if cfg.L2.DAIGatewayAddr != "" {
l1AddressList = append(l1AddressList, common.HexToAddress(cfg.L1.DAIGatewayAddr))
}

l2AddressList := []common.Address{
common.HexToAddress(cfg.L2.CustomERC20GatewayAddr),
common.HexToAddress(cfg.L2.ERC721GatewayAddr),
common.HexToAddress(cfg.L2.ERC1155GatewayAddr),
common.HexToAddress(cfg.L2.MessengerAddr),
common.HexToAddress(cfg.L2.ETHGatewayAddr),
common.HexToAddress(cfg.L2.StandardERC20Gateway),
common.HexToAddress(cfg.L2.WETHGatewayAddr),
}

if cfg.L2.USDCGatewayAddr != "" {
l2AddressList = append(l2AddressList, common.HexToAddress(cfg.L2.USDCGatewayAddr))
}

if cfg.L2.LIDOGatewayAddr != "" {
l2AddressList = append(l2AddressList, common.HexToAddress(cfg.L2.LIDOGatewayAddr))
}

if cfg.L2.DAIGatewayAddr != "" {
l2AddressList = append(l2AddressList, common.HexToAddress(cfg.L2.DAIGatewayAddr))
}

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

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

l2crossMsgFetcher, err := crossmsg.NewMsgFetcher(subCtx, cfg.L2, db, l2client, l2worker, l2AddressList, crossmsg.L2ReorgHandling)
l2MessageFetcher, err := messagefetcher.NewL2MessageFetcher(subCtx, cfg.L2, db, l2Client)
if err != nil {
log.Crit("failed to create l2 cross message fetcher", "error", err)
}

go l2crossMsgFetcher.Start()
defer l2crossMsgFetcher.Stop()

CrossMsgOrm := orm.NewCrossMsg(db)

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

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

// Proof updater and batch fetcher
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()
go l2MessageFetcher.Start()

// Catch CTRL-C to ensure a graceful shutdown.
interrupt := make(chan os.Signal, 1)
Expand Down
23 changes: 16 additions & 7 deletions bridge-history-api/config.json
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
{
"batchInfoFetcher": {
"batchIndexStartBlock": 9091265,
"ScrollChainAddr": "0x1799c3Df650caB9DFBb228C971016707D8f8721D"
},
"l1": {
"chain_id": 1,
"confirmation": 64,
"endpoint": "https://rpc.ankr.com/eth",
"startHeight": 18310747,
"blockTime": 10,
"fetchLimit": 30,
"MessengerAddr": "0x7318152B19c3c97c886D5ee6C2525E62ce8e2abA",
"ETHGatewayAddr": "0xd165b42d857eae2915625819464a2a1f91E5d0A5",
"WETHGatewayAddr": "0xb0255e4C1a919619D1CafBA51021d638c4F71b89",
Expand All @@ -17,13 +15,18 @@
"ERC1155GatewayAddr": "0xCeE721789FAA05c7F4463efB664520656aB7C7d5",
"USDCGatewayAddr": "0x37ba659D6CC380D12Fb96567CC52FC8e1DF4E334",
"LIDOGatewayAddr": "0x892dDB2899325aBBA1fD00FDA8249B40Cbbc33F9",
"DAIGatewayAddr": "0xD8dD7787f89c7E6243AD32E0d0cCf460243C8130"
"DAIGatewayAddr": "0xD8dD7787f89c7E6243AD32E0d0cCf460243C8130",
"ScrollChainAddr": "0x1799c3Df650caB9DFBb228C971016707D8f8721D",
"GatewayRouterAddr": "0xF8B1378579659D8F7EE5f3C929c2f3E332E41Fd6",
"MessageQueueAddr": "0xF0B2293F5D834eAe920c6974D50957A1732de763"
},
"l2": {
"chain_id": 534352,
"confirmation": 1,
"endpoint": "http://mainnet-l2geth-internal-1.mainnet.scroll.tech:8545",
"blockTime": 3,
"startHeight": 0,
"blockTime": 3,
"fetchLimit": 100,
"MessengerAddr": "0xda7c91Ed60DACD28Cb97B180108958c9ACC7698a",
"ETHGatewayAddr": "0x567671187b5FFbcDFe0B6EcF3e56C05508a31A87",
"WETHGatewayAddr": "0x3b03aE2F27d62E0B2b6740CA20Fc07Af4338B791",
Expand All @@ -33,14 +36,20 @@
"ERC1155GatewayAddr": "0xfF14870512e42BFb85a9B7bEfDc06e9aB5A37269",
"USDCGatewayAddr": "0x97D5799CDC8eE2A7452913d7548c7cEE285719FA",
"LIDOGatewayAddr": "0xE9c5C9f67ec7B773fC76440845751F657bb953FF",
"DAIGatewayAddr": "0xC5034eB8F682b73F93C9246aa95A8eBbF82793aA"
"DAIGatewayAddr": "0xC5034eB8F682b73F93C9246aa95A8eBbF82793aA",
"GatewayRouterAddr": "0x4C0926FF5252A435FD19e10ED15e5a249Ba19d79"
},
"db": {
"dsn": "postgres://postgres:1234@localhost:5444/test?sslmode=disable",
"driverName": "postgres",
"maxOpenNum": 200,
"maxIdleNum": 20
},
"redis": {
"address": "localhost:6379",
"password": "",
"db": 0
},
"server": {
"hostPort": "20006"
}
Expand Down
55 changes: 28 additions & 27 deletions bridge-history-api/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,38 +6,42 @@ import (
"path/filepath"
)

// BatchInfoFetcherConfig is the configuration of BatchInfoFetcher
type BatchInfoFetcherConfig struct {
BatchIndexStartBlock uint64 `json:"batchIndexStartBlock"`
ScrollChainAddr string `json:"ScrollChainAddr"`
}

// DBConfig db config
type DBConfig struct {
// data source name
DSN string `json:"dsn"`
DriverName string `json:"driverName"`

MaxOpenNum int `json:"maxOpenNum"`
MaxIdleNum int `json:"maxIdleNum"`
}

// LayerConfig is the configuration of Layer1/Layer2
type LayerConfig struct {
ChainID uint64 `json:"chain_id"`
Confirmation uint64 `json:"confirmation"`
Endpoint string `json:"endpoint"`
StartHeight uint64 `json:"startHeight"`
BlockTime int64 `json:"blockTime"`
FetchLimit uint64 `json:"fetchLimit"`
MessengerAddr string `json:"MessengerAddr"`
ETHGatewayAddr string `json:"ETHGatewayAddr"`
StandardERC20Gateway string `json:"StandardERC20Gateway"`
CustomERC20GatewayAddr string `json:"CustomERC20GatewayAddr"`
WETHGatewayAddr string `json:"WETHGatewayAddr"`
DAIGatewayAddr string `json:"DAIGatewayAddr"`
USDCGatewayAddr string `json:"USDCGatewayAddr"`
LIDOGatewayAddr string `json:"LIDOGatewayAddr"`
DAIGatewayAddr string `json:"DAIGatewayAddr"`
StandardERC20Gateway string `json:"StandardERC20Gateway"`
ERC721GatewayAddr string `json:"ERC721GatewayAddr"`
ERC1155GatewayAddr string `json:"ERC1155GatewayAddr"`
CustomERC20GatewayAddr string `json:"CustomERC20GatewayAddr"`
ScrollChainAddr string `json:"ScrollChainAddr"`
GatewayRouterAddr string `json:"GatewayRouterAddr"`
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"`
Password string `json:"password"`
DB int `json:"db"`
}

// ServerConfig is the configuration of the bridge history backend server port
Expand All @@ -47,14 +51,11 @@ type ServerConfig struct {

// Config is the configuration of the bridge history backend
type Config struct {
// chain config
L1 *LayerConfig `json:"l1"`
L2 *LayerConfig `json:"l2"`

// data source name
DB *DBConfig `json:"db"`
Server *ServerConfig `json:"server"`
BatchInfoFetcher *BatchInfoFetcherConfig `json:"batchInfoFetcher"`
L1 *LayerConfig `json:"l1"`
L2 *LayerConfig `json:"l2"`
DB *DBConfig `json:"db"`
Redis *RedisConfig `json:"redis"`
Server *ServerConfig `json:"server"`
}

// NewConfig returns a new instance of Config.
Expand Down
Loading

0 comments on commit f09f43c

Please sign in to comment.