Skip to content

Commit

Permalink
add withdraw root check
Browse files Browse the repository at this point in the history
  • Loading branch information
colinlyguo committed Dec 13, 2023
1 parent 5c1de26 commit 33ec1e1
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 32 deletions.
53 changes: 25 additions & 28 deletions bridge-history-api/conf/config.json
Original file line number Diff line number Diff line change
@@ -1,40 +1,37 @@
{
"L1": {
"confirmation": 2,
"endpoint": "https://rpc.ankr.com/eth",
"startHeight": 18318215,
"endpoint": "L1-URL",
"startHeight": 4655367,
"blockTime": 12,
"fetchLimit": 32,
"MessengerAddr": "0x6774Bcbd5ceCeF1336b5300fb5186a12DDD8b367",
"ETHGatewayAddr": "0x7F2b8C31F88B6006c382775eea88297Ec1e3E905",
"WETHGatewayAddr": "0x7AC440cAe8EB6328de4fA621163a792c1EA9D4fE",
"StandardERC20GatewayAddr": "0xD8A791fE2bE73eb6E6cF1eb0cb3F36adC9B3F8f9",
"CustomERC20GatewayAddr": "0xb2b10a289A229415a124EFDeF310C10cb004B6ff",
"ERC721GatewayAddr": "0x6260aF48e8948617b8FA17F4e5CEa2d21D21554B",
"ERC1155GatewayAddr": "0xb94f7F6ABcb811c5Ac709dE14E37590fcCd975B6",
"USDCGatewayAddr": "0xf1AF3b23DE0A5Ca3CAb7261cb0061C0D779A5c7B",
"LIDOGatewayAddr": "0x6625C6332c9F91F2D27c304E729B86db87A3f504",
"DAIGatewayAddr": "0x67260A8B73C5B77B55c1805218A42A7A6F98F515",
"ScrollChainAddr": "0xa13BAF47339d63B743e7Da8741db5456DAc1E556",
"GatewayRouterAddr": "0xF8B1378579659D8F7EE5f3C929c2f3E332E41Fd6",
"MessageQueueAddr": "0x0d7E906BD9cAFa154b048cFa766Cc1E54E39AF9B"
"MessengerAddr": "0x50c7d3e7f7c656493D1D76aaa1a836CedfCBB16A",
"ETHGatewayAddr": "0x8A54A2347Da2562917304141ab67324615e9866d",
"WETHGatewayAddr": "0x3dA0BF44814cfC678376b3311838272158211695",
"StandardERC20GatewayAddr": "0x65D123d6389b900d954677c26327bfc1C3e88A13",
"CustomERC20GatewayAddr": "0x31C994F2017E71b82fd4D8118F140c81215bbb37",
"ERC721GatewayAddr": "0xEF27A5E63aa3f1B8312f744b9b4DcEB910Ba77AC",
"ERC1155GatewayAddr": "0xa5Df8530766A85936EE3E139dECE3bF081c83146",
"DAIGatewayAddr": "0x8b0B9c4e9f41b9bbDEfFee24F9f11C328093d248",
"ScrollChainAddr": "0x2D567EcE699Eabe5afCd141eDB7A4f2D0D6ce8a0",
"GatewayRouterAddr": "0x13FBE0D0e5552b8c9c4AE9e2435F38f37355998a",
"MessageQueueAddr": "0xF0B2293F5D834eAe920c6974D50957A1732de763"
},
"L2": {
"confirmation": 1,
"endpoint": "http://mainnet-l2geth-internal-1.mainnet.scroll.tech:8545",
"endpoint": "L2-URL",
"blockTime": 3,
"fetchLimit": 128,
"MessengerAddr": "0x781e90f1c8Fc4611c9b7497C3B47F99Ef6969CbC",
"ETHGatewayAddr": "0x6EA73e05AdC79974B931123675ea8F78FfdacDF0",
"WETHGatewayAddr": "0x7003E7B7186f0E6601203b99F7B8DECBfA391cf9",
"StandardERC20GatewayAddr": "0xE2b4795039517653c5Ae8C2A9BFdd783b48f447A",
"CustomERC20GatewayAddr": "0x64CCBE37c9A82D85A1F2E74649b7A42923067988",
"ERC721GatewayAddr": "0x7bC08E1c04fb41d75F1410363F0c5746Eae80582",
"ERC1155GatewayAddr": "0x62597Cc19703aF10B58feF87B0d5D29eFE263bcc",
"USDCGatewayAddr": "0x33B60d5Dd260d453cAC3782b0bDC01ce84672142",
"LIDOGatewayAddr": "0x8aE8f22226B9d789A36AC81474e633f8bE2856c9",
"DAIGatewayAddr": "0xaC78dff3A87b5b534e366A93E785a0ce8fA6Cc62",
"GatewayRouterAddr": "0x4C0926FF5252A435FD19e10ED15e5a249Ba19d79"
"MessengerAddr": "0xBa50f5340FB9F3Bd074bD638c9BE13eCB36E603d",
"ETHGatewayAddr": "0x91e8ADDFe1358aCa5314c644312d38237fC1101C",
"WETHGatewayAddr": "0x481B20A927206aF7A754dB8b904B052e2781ea27",
"StandardERC20GatewayAddr": "0xaDcA915971A336EA2f5b567e662F5bd74AEf9582",
"CustomERC20GatewayAddr": "0x058dec71E53079F9ED053F3a0bBca877F6f3eAcf",
"ERC721GatewayAddr": "0x179B9415194B67DC3c0b8760E075cD4415785c97",
"ERC1155GatewayAddr": "0xe17C9b9C66FAF07753cdB04316D09f52144612A5",
"DAIGatewayAddr": "0xbF28c28490988026Dca2396148DE50136A54534e",
"GatewayRouterAddr": "0x9aD3c5617eCAa556d6E166787A97081907171230",
"MessageQueueAddr": "0x5300000000000000000000000000000000000000"
},
"db": {
"dsn": "postgres://postgres:123456@localhost:5444/test?sslmode=disable",
Expand All @@ -47,4 +44,4 @@
"password": "",
"db": 0
}
}
}
14 changes: 12 additions & 2 deletions bridge-history-api/internal/controller/fetcher/l2_fetcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,7 @@ func (c *L2MessageFetcher) doFetchAndSaveEvents(ctx context.Context, from uint64

l2RelayedMessages = append(l2RelayedMessages, l2RevertedRelayedMessages...)

if err = c.updateL2WithdrawMessageProofs(ctx, l2WithdrawMessages); err != nil {
if err = c.updateL2WithdrawMessageProofs(ctx, l2WithdrawMessages, to); err != nil {
log.Error("failed to update withdraw message proofs", "err", err)
}

Expand All @@ -238,7 +238,7 @@ func (c *L2MessageFetcher) doFetchAndSaveEvents(ctx context.Context, from uint64
return nil
}

func (c *L2MessageFetcher) updateL2WithdrawMessageProofs(ctx context.Context, l2WithdrawMessages []*orm.CrossMessage) error {
func (c *L2MessageFetcher) updateL2WithdrawMessageProofs(ctx context.Context, l2WithdrawMessages []*orm.CrossMessage, endBlock uint64) error {
withdrawTrie := utils.NewWithdrawTrie()
message, err := c.crossMessageOrm.GetLatestL2Withdrawal(ctx)
if err != nil {
Expand All @@ -260,5 +260,15 @@ func (c *L2MessageFetcher) updateL2WithdrawMessageProofs(ctx context.Context, l2
for i, proof := range proofs {
l2WithdrawMessages[i].MerkleProof = proof
}
// Verify if local info is correct.
withdrawRoot, err := c.client.StorageAt(ctx, common.HexToAddress(c.cfg.MessageQueueAddr), common.Hash{}, new(big.Int).SetUint64(endBlock))
if err != nil {
log.Error("failed to get withdraw root", "number", endBlock, "error", err)
return fmt.Errorf("failed to get withdraw root: %v, number: %v", err, endBlock)
}
if common.BytesToHash(withdrawRoot) != withdrawTrie.MessageRoot() {
log.Error("withdraw root mismatch", "expected", common.BytesToHash(withdrawRoot).String(), "got", withdrawTrie.MessageRoot().String())
return fmt.Errorf("withdraw root mismatch. expected: %v, got: %v", common.BytesToHash(withdrawRoot), withdrawTrie.MessageRoot())
}
return nil
}
4 changes: 2 additions & 2 deletions bridge-history-api/internal/orm/cross_message.go
Original file line number Diff line number Diff line change
Expand Up @@ -340,7 +340,7 @@ func (c *CrossMessage) InsertOrUpdateL2RelayedMessagesOfL1Deposits(ctx context.C
db = db.Model(&CrossMessage{})
db = db.Clauses(clause.OnConflict{
Columns: []clause.Column{{Name: "message_hash"}},
DoUpdates: clause.AssignmentColumns([]string{"message_type, l2_block_number", "l2_tx_hash", "tx_status"}),
DoUpdates: clause.AssignmentColumns([]string{"message_type", "l2_block_number", "l2_tx_hash", "tx_status"}),
})
if err := db.Create(l2RelayedMessages).Error; err != nil {
return fmt.Errorf("failed to update L2 relayed message of L1 deposit, error: %w", err)
Expand Down Expand Up @@ -383,7 +383,7 @@ func (c *CrossMessage) InsertOrUpdateL1RelayedMessagesOfL2Withdrawals(ctx contex
db = db.Model(&CrossMessage{})
db = db.Clauses(clause.OnConflict{
Columns: []clause.Column{{Name: "message_hash"}},
DoUpdates: clause.AssignmentColumns([]string{"message_type, l1_block_number", "l1_tx_hash", "tx_status"}),
DoUpdates: clause.AssignmentColumns([]string{"message_type", "l1_block_number", "l1_tx_hash", "tx_status"}),
})
if err := db.Create(uniqueL1RelayedMessages).Error; err != nil {
return fmt.Errorf("failed to update L1 relayed message of L2 withdrawal, error: %w", err)
Expand Down

0 comments on commit 33ec1e1

Please sign in to comment.