Skip to content

Commit

Permalink
resolve comments
Browse files Browse the repository at this point in the history
  • Loading branch information
JJVincentJJ committed Jun 24, 2023
1 parent 2cc5c00 commit 279895c
Show file tree
Hide file tree
Showing 10 changed files with 26 additions and 32 deletions.
6 changes: 3 additions & 3 deletions bridge-history-api/cmd/cross_msg_fetcher/app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,9 +111,9 @@ func action(ctx *cli.Context) error {

// Proof updater and batch fetcher
l2msgProofUpdater := message_proof.NewMsgProofUpdater(subCtx, l1client, cfg.L1.Confirmation, cfg.BatchInfoFetcher.BatchIndexStartBlock, db)
l2BatchFetcher := cross_msg.NewBatchInfoFetcher(subCtx, common.HexToAddress(cfg.BatchInfoFetcher.ScrollChainAddr), cfg.BatchInfoFetcher.BatchIndexStartBlock, cfg.L1.Confirmation, int(cfg.L1.BlockTime), l1client, db, l2msgProofUpdater)
go l2BatchFetcher.Start()
defer l2BatchFetcher.Stop()
batchFetcher := cross_msg.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()

// Catch CTRL-C to ensure a graceful shutdown.
interrupt := make(chan os.Signal, 1)
Expand Down
11 changes: 6 additions & 5 deletions bridge-history-api/cross_msg/batch_info_fetcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (

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

type BatchInfoFetcher struct {
Expand Down Expand Up @@ -38,7 +39,7 @@ func NewBatchInfoFetcher(ctx context.Context, scrollChainAddr common.Address, ba

func (b *BatchInfoFetcher) Start() {
log.Info("BatchInfoFetcher Start")
// Fetch batch info at begining
// Fetch batch info at beginning
// Then start msg proof updater after db have some bridge batch
err := b.fetchBatchInfo()
if err != nil {
Expand Down Expand Up @@ -70,7 +71,7 @@ func (b *BatchInfoFetcher) Stop() {
}

func (b *BatchInfoFetcher) fetchBatchInfo() error {
number, err := b.client.BlockNumber(b.ctx)
number, err := utils.GetSafeBlockNumber(b.ctx, b.client, b.confirmation)
if err != nil {
log.Error("Can not get latest block number: ", "err", err)
return err
Expand All @@ -86,12 +87,12 @@ func (b *BatchInfoFetcher) fetchBatchInfo() error {
} else {
startHeight = latestBatch.CommitHeight + 1
}
for from := startHeight; number >= from+b.confirmation; from += uint64(fetchLimit) {
for from := startHeight; number >= from; from += uint64(fetchLimit) {
to := from + uint64(fetchLimit) - 1
// number - confirmation can never less than 0 since the for loop condition
// but watch out the overflow
if to > number-b.confirmation {
to = number - b.confirmation
if to > number {
to = number
}
// filter logs to fetch batches
err = FetchAndSaveBatchIndex(b.ctx, b.client, b.db, int64(from), int64(to), b.scrollChainAddr)
Expand Down
2 changes: 1 addition & 1 deletion bridge-history-api/cross_msg/fetch_missing_event.go
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,7 @@ func FetchAndSaveBatchIndex(ctx context.Context, client *ethclient.Client, datab
log.Error("FetchAndSaveBatchIndex: Failed to begin db transaction", "err", err)
return err
}
err = database.BatchInsertBridgeBatchDBTx(dbTx, bridgeBatches)
err = database.BatchInsertRollupBatchDBTx(dbTx, bridgeBatches)
if err != nil {
dbTx.Rollback()
log.Crit("FetchAndSaveBatchIndex: Failed to insert batch commit msg event logs", "err", err)
Expand Down
11 changes: 5 additions & 6 deletions bridge-history-api/cross_msg/message_proof/msg_proof_updater.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,13 @@ import (

type MsgProofUpdater struct {
ctx context.Context
client *ethclient.Client
db db.OrmFactory
withdrawTrie *WithdrawTrie
}

func NewMsgProofUpdater(ctx context.Context, client *ethclient.Client, confirmations uint64, startBlock uint64, db db.OrmFactory) *MsgProofUpdater {
return &MsgProofUpdater{
ctx: ctx,
client: client,
db: db,
withdrawTrie: NewWithdrawTrie(),
}
Expand Down Expand Up @@ -65,7 +63,7 @@ func (m *MsgProofUpdater) Start() {
batch, err := m.db.GetBridgeBatchByIndex(i)
if err != nil {
log.Error("MsgProofUpdater: Can not get RollupBatch: ", "err", err, "index", i)
continue
break
}
// get all l2 messages in this batch
msgs, proofs, err := m.appendL2Messages(batch.StartBlockNumber, batch.EndBlockNumber)
Expand Down Expand Up @@ -196,8 +194,9 @@ func (m *MsgProofUpdater) updateMsgProof(msgs []*orm.L2SentMsg, proofs [][]byte,
}

for i, msg := range msgs {
log.Debug("updateMsgProof", "msgHash", msg.MsgHash, "batchIndex", batchIndex, "proof", common.Bytes2Hex(proofs[i]))
if dbTxErr := m.db.UpdateL2MessageProofInDBTx(m.ctx, dbTx, msg.MsgHash, common.Bytes2Hex(proofs[i]), batchIndex); dbTxErr != nil {
proofHex := common.Bytes2Hex(proofs[i])
log.Debug("updateMsgProof", "msgHash", msg.MsgHash, "batchIndex", batchIndex, "proof", proofHex)
if dbTxErr := m.db.UpdateL2MessageProofInDBTx(m.ctx, dbTx, msg.MsgHash, proofHex, batchIndex); dbTxErr != nil {
if err := dbTx.Rollback(); err != nil {
log.Error("dbTx.Rollback()", "err", err)
}
Expand Down Expand Up @@ -230,7 +229,7 @@ func (m *MsgProofUpdater) appendL2Messages(firstBlock, lastBlock uint64) ([]*orm
// double check whether nonce is matched
if messages[0].Nonce != m.withdrawTrie.NextMessageNonce {
log.Error("L2 message nonce mismatch", "expected", m.withdrawTrie.NextMessageNonce, "found", messages[0].Nonce)
return messages, msgProofs, fmt.Errorf("l2 message nonce mismatch, expected: %v, found: %v", messages[0].Nonce, m.withdrawTrie.NextMessageNonce)
return messages, msgProofs, fmt.Errorf("l2 message nonce mismatch, expected: %v, found: %v", m.withdrawTrie.NextMessageNonce, messages[0].Nonce)
}

var hashes []common.Hash
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ create table l2_sent_msg
msg_hash VARCHAR NOT NULL,
height BIGINT NOT NULL,
nonce BIGINT NOT NULL,
batch_index BIGINT DEFAULT 0,
msg_proof TEXT DEFAULT '',
msg_data TEXT DEFAULT '',
batch_index BIGINT NOT NULL DEFAULT 0,
msg_proof TEXT NOT NULL DEFAULT '',
msg_data TEXT NOT NULL DEFAULT '',
is_deleted BOOLEAN NOT NULL DEFAULT FALSE,
created_at TIMESTAMP(0) NOT NULL DEFAULT CURRENT_TIMESTAMP,
updated_at TIMESTAMP(0) NOT NULL DEFAULT CURRENT_TIMESTAMP,
Expand Down
6 changes: 3 additions & 3 deletions bridge-history-api/db/orm/batch.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ func NewBridgeBatchOrm(db *sqlx.DB) BridgeBatchOrm {
return &bridgeBatchOrm{db: db}
}

func (b *bridgeBatchOrm) BatchInsertBridgeBatchDBTx(dbTx *sqlx.Tx, batches []*RollupBatch) error {
func (b *bridgeBatchOrm) BatchInsertRollupBatchDBTx(dbTx *sqlx.Tx, batches []*RollupBatch) error {
if len(batches) == 0 {
return nil
}
Expand All @@ -46,12 +46,12 @@ func (b *bridgeBatchOrm) BatchInsertBridgeBatchDBTx(dbTx *sqlx.Tx, batches []*Ro
return err
}
if exists {
return fmt.Errorf("BatchInsertBridgeBatchDBTx: batch index %v already exists at height %v", msg.BatchIndex, msg.CommitHeight)
return fmt.Errorf("BatchInsertRollupBatchDBTx: batch index %v already exists at height %v", msg.BatchIndex, msg.CommitHeight)
}
}
_, err = dbTx.NamedExec(`insert into bridge_batch(commit_height, batch_index, batch_hash, start_block_number, end_block_number) values(:commit_height, :batch_index, :batch_hash, :start_block_number, :end_block_number);`, messageMaps)
if err != nil {
log.Error("BatchInsertBridgeBatchDBTx: failed to insert batch event msgs", "err", err)
log.Error("BatchInsertRollupBatchDBTx: failed to insert batch event msgs", "err", err)
return err
}
return nil
Expand Down
3 changes: 1 addition & 2 deletions bridge-history-api/db/orm/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,11 +114,10 @@ type L2SentMsgOrm interface {
UpdateL2MessageProofInDBTx(ctx context.Context, dbTx *sqlx.Tx, msgHash string, proof string, batch_index uint64) error
GetLatestL2SentMsgBatchIndex() (int64, error)
DeleteL2SentMsgAfterHeightDBTx(dbTx *sqlx.Tx, height int64) error
ResetL2SentMsgL1HashAfterHeightDBTx(dbTx *sqlx.Tx, height int64) error
}

type BridgeBatchOrm interface {
GetLatestBridgeBatch() (*RollupBatch, error)
GetBridgeBatchByIndex(index uint64) (*RollupBatch, error)
BatchInsertBridgeBatchDBTx(dbTx *sqlx.Tx, messages []*RollupBatch) error
BatchInsertRollupBatchDBTx(dbTx *sqlx.Tx, messages []*RollupBatch) error
}
5 changes: 0 additions & 5 deletions bridge-history-api/db/orm/l2_sent_msg.go
Original file line number Diff line number Diff line change
Expand Up @@ -157,8 +157,3 @@ func (l *l2SentMsgOrm) DeleteL2SentMsgAfterHeightDBTx(dbTx *sqlx.Tx, height int6
_, err := dbTx.Exec(`UPDATE l2_sent_msg SET is_deleted = true WHERE height > $1;`, height)
return err
}

func (l *l2SentMsgOrm) ResetL2SentMsgL1HashAfterHeightDBTx(dbTx *sqlx.Tx, height int64) error {
_, err := dbTx.Exec(`UPDATE l2_sent_msg SET layer1_hash = '' WHERE height > $1 AND NOT is_deleted;`, height)
return err
}
2 changes: 1 addition & 1 deletion bridge-history-api/service/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ func GetCrossTxClaimInfo(msgHash string, db db.OrmFactory) *UserClaimInfo {
From: l2sentMsg.Sender,
To: l2sentMsg.Target,
Value: l2sentMsg.Value,
Nonce: strconv.FormatUint(l2sentMsg.Nonce, 10),
Nonce: strconv.FormatUint(l2sentMsg.Nonce, 16),
Message: l2sentMsg.MsgData,
Proof: l2sentMsg.MsgProof,
BatchHash: batch.BatchHash,
Expand Down
6 changes: 3 additions & 3 deletions bridge-history-api/utils/parse_event.go
Original file line number Diff line number Diff line change
Expand Up @@ -278,22 +278,22 @@ func ParseBatchInfoFromScrollChain(ctx context.Context, client *ethclient.Client
log.Warn("Failed to get commit Batch tx receipt or the tx is still pending", "err", err)
return bridgeBatches, err
}
indices, startBlcocks, endBlocks, err := GetBatchRangeFromCalldataV1(commitTx.Data())
indices, startBlocks, endBlocks, err := GetBatchRangeFromCalldataV1(commitTx.Data())
if err != nil {
log.Warn("Failed to get batch range from calldata", "hash", commitTx.Hash().Hex(), "height", vlog.BlockNumber)
return bridgeBatches, err
}
cache[vlog.TxHash.Hex()] = CachedParsedTxCalldata{
CallDataIndex: 0,
BatchIndices: indices,
StartBlocks: startBlcocks,
StartBlocks: startBlocks,
EndBlocks: endBlocks,
}
bridgeBatches = append(bridgeBatches, &orm.RollupBatch{
CommitHeight: vlog.BlockNumber,
BatchIndex: indices[0],
BatchHash: event.BatchHash.Hex(),
StartBlockNumber: startBlcocks[0],
StartBlockNumber: startBlocks[0],
EndBlockNumber: endBlocks[0],
})

Expand Down

0 comments on commit 279895c

Please sign in to comment.