Skip to content

Commit

Permalink
fix
Browse files Browse the repository at this point in the history
  • Loading branch information
colinlyguo committed Dec 5, 2023
1 parent a8ea304 commit 654b6b4
Show file tree
Hide file tree
Showing 26 changed files with 612 additions and 458 deletions.
6 changes: 3 additions & 3 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 @@ -51,7 +51,7 @@ can change this port thru modify `config.json`

2. `/withdrawals`
```
// @Summary get all l2 withdrawals under given address
// @Summary get all L2 withdrawals under given address
// @Accept plain
// @Produce plain
// @Param address query string true "wallet address"
Expand All @@ -63,7 +63,7 @@ can change this port thru modify `config.json`

3. `/claimablewithdrawals`
```
// @Summary get all l2 claimable withdrawals under given address
// @Summary get all L2 claimable withdrawals under given address
// @Accept plain
// @Produce plain
// @Param address query string true "wallet address"
Expand Down
10 changes: 6 additions & 4 deletions bridge-history-api/abi/backend_abi.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ package backendabi
import (
"math/big"

"github.com/ethereum/go-ethereum/accounts/abi"
"github.com/ethereum/go-ethereum/accounts/abi/bind"
"github.com/ethereum/go-ethereum/common"
"github.com/scroll-tech/go-ethereum/accounts/abi"
"github.com/scroll-tech/go-ethereum/accounts/abi/bind"
"github.com/scroll-tech/go-ethereum/common"
)

var (
Expand Down Expand Up @@ -154,10 +154,11 @@ var IL1MessageQueueMetaData = &bind.MetaData{
ABI: "[{\"anonymous\":false,\"inputs\":[{\"indexed\":false,\"internalType\":\"uint256\",\"name\":\"startIndex\",\"type\":\"uint256\"},{\"indexed\":false,\"internalType\":\"uint256\",\"name\":\"count\",\"type\":\"uint256\"},{\"indexed\":false,\"internalType\":\"uint256\",\"name\":\"skippedBitmap\",\"type\":\"uint256\"}],\"name\":\"DequeueTransaction\",\"type\":\"event\"},{\"anonymous\":false,\"inputs\":[{\"indexed\":false,\"internalType\":\"uint256\",\"name\":\"index\",\"type\":\"uint256\"}],\"name\":\"DropTransaction\",\"type\":\"event\"},{\"anonymous\":false,\"inputs\":[{\"indexed\":true,\"internalType\":\"address\",\"name\":\"sender\",\"type\":\"address\"},{\"indexed\":true,\"internalType\":\"address\",\"name\":\"target\",\"type\":\"address\"},{\"indexed\":false,\"internalType\":\"uint256\",\"name\":\"value\",\"type\":\"uint256\"},{\"indexed\":false,\"internalType\":\"uint64\",\"name\":\"queueIndex\",\"type\":\"uint64\"},{\"indexed\":false,\"internalType\":\"uint256\",\"name\":\"gasLimit\",\"type\":\"uint256\"},{\"indexed\":false,\"internalType\":\"bytes\",\"name\":\"data\",\"type\":\"bytes\"}],\"name\":\"QueueTransaction\",\"type\":\"event\"},{\"inputs\":[{\"internalType\":\"address\",\"name\":\"target\",\"type\":\"address\"},{\"internalType\":\"uint256\",\"name\":\"gasLimit\",\"type\":\"uint256\"},{\"internalType\":\"bytes\",\"name\":\"data\",\"type\":\"bytes\"}],\"name\":\"appendCrossDomainMessage\",\"outputs\":[],\"stateMutability\":\"nonpayable\",\"type\":\"function\"},{\"inputs\":[{\"internalType\":\"address\",\"name\":\"sender\",\"type\":\"address\"},{\"internalType\":\"address\",\"name\":\"target\",\"type\":\"address\"},{\"internalType\":\"uint256\",\"name\":\"value\",\"type\":\"uint256\"},{\"internalType\":\"uint256\",\"name\":\"gasLimit\",\"type\":\"uint256\"},{\"internalType\":\"bytes\",\"name\":\"data\",\"type\":\"bytes\"}],\"name\":\"appendEnforcedTransaction\",\"outputs\":[],\"stateMutability\":\"nonpayable\",\"type\":\"function\"},{\"inputs\":[{\"internalType\":\"bytes\",\"name\":\"_calldata\",\"type\":\"bytes\"}],\"name\":\"calculateIntrinsicGasFee\",\"outputs\":[{\"internalType\":\"uint256\",\"name\":\"\",\"type\":\"uint256\"}],\"stateMutability\":\"view\",\"type\":\"function\"},{\"inputs\":[{\"internalType\":\"address\",\"name\":\"sender\",\"type\":\"address\"},{\"internalType\":\"uint256\",\"name\":\"queueIndex\",\"type\":\"uint256\"},{\"internalType\":\"uint256\",\"name\":\"value\",\"type\":\"uint256\"},{\"internalType\":\"address\",\"name\":\"target\",\"type\":\"address\"},{\"internalType\":\"uint256\",\"name\":\"gasLimit\",\"type\":\"uint256\"},{\"internalType\":\"bytes\",\"name\":\"data\",\"type\":\"bytes\"}],\"name\":\"computeTransactionHash\",\"outputs\":[{\"internalType\":\"bytes32\",\"name\":\"\",\"type\":\"bytes32\"}],\"stateMutability\":\"view\",\"type\":\"function\"},{\"inputs\":[{\"internalType\":\"uint256\",\"name\":\"index\",\"type\":\"uint256\"}],\"name\":\"dropCrossDomainMessage\",\"outputs\":[],\"stateMutability\":\"nonpayable\",\"type\":\"function\"},{\"inputs\":[{\"internalType\":\"uint256\",\"name\":\"gasLimit\",\"type\":\"uint256\"}],\"name\":\"estimateCrossDomainMessageFee\",\"outputs\":[{\"internalType\":\"uint256\",\"name\":\"\",\"type\":\"uint256\"}],\"stateMutability\":\"view\",\"type\":\"function\"},{\"inputs\":[{\"internalType\":\"uint256\",\"name\":\"queueIndex\",\"type\":\"uint256\"}],\"name\":\"getCrossDomainMessage\",\"outputs\":[{\"internalType\":\"bytes32\",\"name\":\"\",\"type\":\"bytes32\"}],\"stateMutability\":\"view\",\"type\":\"function\"},{\"inputs\":[{\"internalType\":\"uint256\",\"name\":\"queueIndex\",\"type\":\"uint256\"}],\"name\":\"isMessageDropped\",\"outputs\":[{\"internalType\":\"bool\",\"name\":\"\",\"type\":\"bool\"}],\"stateMutability\":\"view\",\"type\":\"function\"},{\"inputs\":[{\"internalType\":\"uint256\",\"name\":\"queueIndex\",\"type\":\"uint256\"}],\"name\":\"isMessageSkipped\",\"outputs\":[{\"internalType\":\"bool\",\"name\":\"\",\"type\":\"bool\"}],\"stateMutability\":\"view\",\"type\":\"function\"},{\"inputs\":[],\"name\":\"nextCrossDomainMessageIndex\",\"outputs\":[{\"internalType\":\"uint256\",\"name\":\"\",\"type\":\"uint256\"}],\"stateMutability\":\"view\",\"type\":\"function\"},{\"inputs\":[],\"name\":\"pendingQueueIndex\",\"outputs\":[{\"internalType\":\"uint256\",\"name\":\"\",\"type\":\"uint256\"}],\"stateMutability\":\"view\",\"type\":\"function\"},{\"inputs\":[{\"internalType\":\"uint256\",\"name\":\"startIndex\",\"type\":\"uint256\"},{\"internalType\":\"uint256\",\"name\":\"count\",\"type\":\"uint256\"},{\"internalType\":\"uint256\",\"name\":\"skippedBitmap\",\"type\":\"uint256\"}],\"name\":\"popCrossDomainMessage\",\"outputs\":[],\"stateMutability\":\"nonpayable\",\"type\":\"function\"}]",
}

type DepositETH struct {
type ETHMessageEvent struct {
From common.Address
To common.Address
Amount *big.Int
Data []byte
}

type ERC20MessageEvent struct {
Expand All @@ -166,6 +167,7 @@ type ERC20MessageEvent struct {
From common.Address
To common.Address
Amount *big.Int
Data []byte
}

type ERC721MessageEvent struct {
Expand Down
2 changes: 1 addition & 1 deletion bridge-history-api/cmd/backend_server/app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +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
12 changes: 6 additions & 6 deletions bridge-history-api/cmd/cross_message_fetcher/app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ import (
"os"
"os/signal"

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

"bridge-history-api/config"
Expand Down Expand Up @@ -44,12 +44,12 @@ func action(ctx *cli.Context) error {

l1Client, err := ethclient.Dial(cfg.L1.Endpoint)
if err != nil {
log.Crit("failed to connect to l1 geth", "endpoint", cfg.L1.Endpoint, "err", err)
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)
log.Crit("failed to connect to L2 geth", "endpoint", cfg.L2.Endpoint, "err", err)
}

db, err := utils.InitDB(cfg.DB)
Expand All @@ -67,13 +67,13 @@ func action(ctx *cli.Context) error {

l1MessageFetcher, err := eventfetcher.NewL1MessageFetcher(subCtx, cfg.L1, db, l1Client)
if err != nil {
log.Crit("failed to create l1 cross message fetcher", "error", err)
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)
log.Crit("failed to create L2 cross message fetcher", "error", err)
}
go l2MessageFetcher.Start()

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

import (
"github.com/ethereum/go-ethereum/log"
"github.com/scroll-tech/go-ethereum/log"
"github.com/urfave/cli/v2"
"gorm.io/gorm"

Expand Down
6 changes: 3 additions & 3 deletions bridge-history-api/config.json
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
{
"l1": {
"L1": {
"chain_id": 1,
"confirmation": 64,
"endpoint": "https://rpc.ankr.com/eth",
"startHeight": 18310747,
"startHeight": 18334000,
"blockTime": 10,
"fetchLimit": 30,
"MessengerAddr": "0x6774Bcbd5ceCeF1336b5300fb5186a12DDD8b367",
Expand All @@ -20,7 +20,7 @@
"GatewayRouterAddr": "0xF8B1378579659D8F7EE5f3C929c2f3E332E41Fd6",
"MessageQueueAddr": "0x0d7E906BD9cAFa154b048cFa766Cc1E54E39AF9B"
},
"l2": {
"L2": {
"chain_id": 534352,
"confirmation": 1,
"endpoint": "http://mainnet-l2geth-internal-1.mainnet.scroll.tech:8545",
Expand Down
4 changes: 2 additions & 2 deletions bridge-history-api/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,8 @@ type ServerConfig struct {

// Config is the configuration of the bridge history backend
type Config struct {
L1 *LayerConfig `json:"l1"`
L2 *LayerConfig `json:"l2"`
L1 *LayerConfig `json:"L1"`
L2 *LayerConfig `json:"L2"`
DB *DBConfig `json:"db"`
Redis *RedisConfig `json:"redis"`
Server *ServerConfig `json:"server"`
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@ import (
"math/big"
"time"

geth "github.com/ethereum/go-ethereum"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/ethclient"
"github.com/ethereum/go-ethereum/log"
"github.com/scroll-tech/go-ethereum"
"github.com/scroll-tech/go-ethereum/common"
"github.com/scroll-tech/go-ethereum/core/types"
"github.com/scroll-tech/go-ethereum/ethclient"
"github.com/scroll-tech/go-ethereum/log"
"gorm.io/gorm"

backendabi "bridge-history-api/abi"
Expand Down Expand Up @@ -70,7 +70,7 @@ func NewL1MessageFetcher(ctx context.Context, cfg *config.LayerConfig, db *gorm.
}, nil
}

// Start starts the l1 message fetching process.
// Start starts the L1 message fetching process.
func (c *L1MessageFetcher) Start() {
tick := time.NewTicker(time.Duration(c.cfg.BlockTime) * time.Second)
go func() {
Expand Down Expand Up @@ -118,6 +118,7 @@ func (c *L1MessageFetcher) fetchAndSaveEvents(confirmation uint64) {
}

func (c *L1MessageFetcher) doFetchAndSaveEvents(ctx context.Context, from uint64, to uint64, addrList []common.Address) error {
log.Info("fetch and save L1 events", "from", from, "to", to)
var l1FailedGatewayRouterTxs []*orm.CrossMessage
blockTimestampsMap := make(map[uint64]uint64)
for number := from; number <= to; number++ {
Expand Down Expand Up @@ -164,7 +165,7 @@ func (c *L1MessageFetcher) doFetchAndSaveEvents(ctx context.Context, from uint64
}
}

query := geth.FilterQuery{
query := ethereum.FilterQuery{
FromBlock: new(big.Int).SetUint64(from), // inclusive
ToBlock: new(big.Int).SetUint64(to), // inclusive
Addresses: addrList,
Expand All @@ -187,49 +188,49 @@ func (c *L1MessageFetcher) doFetchAndSaveEvents(ctx context.Context, from uint64

logs, err := c.client.FilterLogs(ctx, query)
if err != nil {
log.Error("failed to filter l1 event logs", "from", from, "to", to, "err", err)
log.Error("failed to filter L1 event logs", "from", from, "to", to, "err", err)
return err
}
l1DepositMessages, l1RelayedMessages, err := logic.ParseL1CrossChainEventLogs(ctx, logs, blockTimestampsMap, c.client)
if err != nil {
log.Error("failed to parse l1 cross chain event logs", "from", from, "to", to, "err", err)
log.Error("failed to parse L1 cross chain event logs", "from", from, "to", to, "err", err)
return err
}
l1BatchEvents, err := logic.ParseL1BatchEventLogs(ctx, logs, blockTimestampsMap, c.client)
if err != nil {
log.Error("failed to parse l1 batch event logs", "from", from, "to", to, "err", err)
log.Error("failed to parse L1 batch event logs", "from", from, "to", to, "err", err)
return err
}
l1MessageQueueEvents, err := logic.ParseL1MessageQueueEventLogs(ctx, logs, blockTimestampsMap, c.client)
if err != nil {
log.Error("failed to parse l1 message queue event logs", "from", from, "to", to, "err", err)
log.Error("failed to parse L1 message queue event logs", "from", from, "to", to, "err", err)
return err
}
err = c.db.Transaction(func(tx *gorm.DB) error {
if txErr := c.crossMessageOrm.InsertOrUpdateL1Messages(ctx, l1DepositMessages, tx); txErr != nil {
log.Error("failed to insert l1 deposit messages", "from", from, "to", to, "err", txErr)
log.Error("failed to insert L1 deposit messages", "from", from, "to", to, "err", txErr)
return txErr
}
if txErr := c.crossMessageOrm.InsertOrUpdateL1RelayedMessagesOfL2Withdrawals(ctx, l1RelayedMessages, tx); txErr != nil {
log.Error("failed to update l1 relayed messages of l2 withdrawals", "from", from, "to", to, "err", txErr)
log.Error("failed to update L1 relayed messages of L2 withdrawals", "from", from, "to", to, "err", txErr)
return txErr
}
if txErr := c.batchEventOrm.InsertOrUpdateBatchEvents(ctx, l1BatchEvents, tx); txErr != nil {
log.Error("failed to insert or update batch events", "from", from, "to", to, "err", txErr)
return txErr
}
if txErr := c.crossMessageOrm.UpdateL1MessageQueueEventsInfo(ctx, l1MessageQueueEvents, tx); txErr != nil {
log.Error("failed to insert l1 message queue events", "from", from, "to", to, "err", txErr)
log.Error("failed to insert L1 message queue events", "from", from, "to", to, "err", txErr)
return txErr
}
if txErr := c.crossMessageOrm.InsertFailedMessages(ctx, l1FailedGatewayRouterTxs, tx); txErr != nil {
log.Error("failed to insert l1 failed gateway router transactions", "from", from, "to", to, "err", txErr)
log.Error("failed to insert L1 failed gateway router transactions", "from", from, "to", to, "err", txErr)
return txErr
}
return nil
})
if err != nil {
log.Error("failed to update db of l1 events", "from", from, "to", to, "err", err)
log.Error("failed to update db of L1 events", "from", from, "to", to, "err", err)
return err
}
if err = c.updateBatchIndexAndStatus(ctx); err != nil {
Expand All @@ -242,7 +243,7 @@ func (c *L1MessageFetcher) doFetchAndSaveEvents(ctx context.Context, from uint64
func (c *L1MessageFetcher) updateBatchIndexAndStatus(ctx context.Context) error {
latestMessageHeight, err := c.crossMessageOrm.GetLatestFinalizedL2WithdrawalBlockHeight(ctx)
if err != nil {
log.Error("failed to get latest finalized l2 sent message block height", "error", err)
log.Error("failed to get latest finalized L2 sent message block height", "error", err)
return err
}
batches, err := c.batchEventOrm.GetBatchesGEBlockHeight(ctx, latestMessageHeight+1)
Expand All @@ -252,7 +253,7 @@ func (c *L1MessageFetcher) updateBatchIndexAndStatus(ctx context.Context) error
}
for _, batch := range batches {
if err := c.crossMessageOrm.UpdateBatchStatusOfL2Withdrawals(ctx, batch.StartBlockNumber, batch.EndBlockNumber, batch.BatchIndex); err != nil {
log.Error("failed to update batch status of l2 sent messages", "start", batch.StartBlockNumber, "end", batch.EndBlockNumber, "index", batch.BatchIndex, "error", err)
log.Error("failed to update batch status of L2 sent messages", "start", batch.StartBlockNumber, "end", batch.EndBlockNumber, "index", batch.BatchIndex, "error", err)
return err
}
}
Expand Down
Loading

0 comments on commit 654b6b4

Please sign in to comment.