Skip to content

Commit 85e6448

Browse files
committed
ulxly: refactoring agglayer bridge service; fix typos
1 parent 217547c commit 85e6448

13 files changed

+129
-34
lines changed

cmd/ulxly/bridge_service/agglayer/deposit.go renamed to cmd/ulxly/bridge_service/agglayer/bridge.go

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -36,11 +36,10 @@ type BridgeResponse struct {
3636

3737
// TODO: REVIEW THESE INFORMATION USED IN THE LEGACY BRIDGE SERVICE
3838
// ClaimTxHash string `json:"claim_tx_hash"`
39-
// ReadyForClaim bool `json:"ready_for_claim"`
4039
// GlobalIndex string `json:"global_index"`
4140
}
4241

43-
func (r *BridgeResponse) ToDeposit() *bridge_service.Deposit {
42+
func (r *BridgeResponse) ToDeposit(isReadyForClaim bool) *bridge_service.Deposit {
4443
d := &bridge_service.Deposit{}
4544
d.BlockNum = r.BlockNum
4645

@@ -51,10 +50,10 @@ func (r *BridgeResponse) ToDeposit() *bridge_service.Deposit {
5150
d.Amount.SetString(r.Amount, 10)
5251

5352
d.TxHash = common.HexToHash(r.TxHash)
54-
// if len(r.ClaimTxHash) > 0 {
55-
// claimTxHash := common.HexToHash(r.ClaimTxHash)
56-
// d.ClaimTxHash = &claimTxHash
57-
// }
53+
if len(r.BridgeHash) > 0 {
54+
claimTxHash := common.HexToHash(r.BridgeHash)
55+
d.ClaimTxHash = &claimTxHash
56+
}
5857

5958
d.OrigAddr = common.HexToAddress(r.OrigAddr)
6059
d.DestAddr = common.HexToAddress(r.DestAddr)
@@ -66,7 +65,7 @@ func (r *BridgeResponse) ToDeposit() *bridge_service.Deposit {
6665
d.DestNet = r.DestNet
6766
d.NetworkID = r.NetworkID
6867
d.DepositCnt = r.DepositCnt
69-
// d.ReadyForClaim = r.ReadyForClaim
68+
d.ReadyForClaim = isReadyForClaim
7069

7170
return d
7271
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package agglayer
2+
3+
type getInjectedL1InfoLeafResponse struct {
4+
BlockNum uint64 `json:"block_num"`
5+
BlockPos uint64 `json:"block_pos"`
6+
GlobalExitRoot string `json:"global_exit_root"`
7+
Hash string `json:"hash"`
8+
L1InfoTreeIndex uint64 `json:"l1_info_tree_index"`
9+
MainnetExitRoot string `json:"mainnet_exit_root"`
10+
PreviousBlockHash string `json:"previous_block_hash"`
11+
RollupExitRoot string `json:"rollup_exit_root"`
12+
Timestamp uint64 `json:"timestamp"`
13+
}

cmd/ulxly/bridge_service/agglayer/service.go

Lines changed: 94 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ import (
99
"github.com/rs/zerolog/log"
1010
)
1111

12+
const urlPath = "/bridge/v1"
13+
1214
type BridgeService struct {
1315
bridge_service.BridgeServiceBase
1416
httpClient *http.Client
@@ -23,19 +25,26 @@ func NewBridgeService(url string, insecure bool) (*BridgeService, error) {
2325
}
2426

2527
func (s *BridgeService) GetDeposit(depositNetwork, depositCount uint32) (*bridge_service.Deposit, error) {
26-
endpoint := fmt.Sprintf("%s/bridges?network_id=%d&deposit_count=%d", s.BridgeServiceBase.Url(), depositNetwork, depositCount)
27-
resp, respError, statusCode, err := httpjson.HTTPGetWithError[GetBridgeResponse, ErrorResponse](s.httpClient, endpoint)
28+
bridgeEndpoint := fmt.Sprintf("%s/%s/bridges?network_id=%d&deposi.t_count=%d", s.BridgeServiceBase.Url(), urlPath, depositNetwork, depositCount)
29+
bridgeResp, bridgeRespError, statusCode, err := httpjson.HTTPGetWithError[GetBridgeResponse, ErrorResponse](s.httpClient, bridgeEndpoint)
2830
if err != nil {
2931
return nil, err
3032
}
3133

3234
if statusCode != http.StatusOK {
3335
errMsg := "unable to retrieve bridge deposit"
34-
log.Warn().Int("code", statusCode).Str("message", respError.Error).Msgf("%s", errMsg)
36+
log.Warn().Int("code", statusCode).Str("message", bridgeRespError.Error).Msgf("%s", errMsg)
3537
return nil, bridge_service.ErrUnableToRetrieveDeposit
3638
}
3739

38-
deposit := resp.Bridges[0].ToDeposit()
40+
if len(bridgeResp.Bridges) == 0 {
41+
return nil, fmt.Errorf("no deposit found in the response")
42+
}
43+
44+
deposit, err := s.responseToDeposit(bridgeResp.Bridges[0])
45+
if err != nil {
46+
return nil, err
47+
}
3948

4049
return deposit, nil
4150
}
@@ -45,11 +54,11 @@ func (s *BridgeService) GetDeposits(destinationAddress string, offset, limit int
4554
pageNumber := offset/limit + 1
4655
skipItems := offset % limit
4756

48-
const endpointTemplate = "%s/bridges?from_address=%s&page_number=%d&page_size=%d"
57+
const endpointTemplate = "%s/%s/bridges?from_address=%s&page_number=%d&page_size=%d"
4958

5059
// loads all deposits when offset is exactly the size of a page or the first part of them when offset is not
5160
// exactly the size of a page
52-
endpoint := fmt.Sprintf(endpointTemplate, s.BridgeServiceBase.Url(), destinationAddress, pageNumber, pageSize)
61+
endpoint := fmt.Sprintf(endpointTemplate, s.BridgeServiceBase.Url(), urlPath, destinationAddress, pageNumber, pageSize)
5362
resp, respError, statusCode, err := httpjson.HTTPGetWithError[GetBridgeResponse, ErrorResponse](s.httpClient, endpoint)
5463
if err != nil {
5564
return nil, 0, err
@@ -68,7 +77,7 @@ func (s *BridgeService) GetDeposits(destinationAddress string, offset, limit int
6877
// this is needed because the API only supports pagination by page number and page size
6978
// and not by offset and limit
7079
if skipItems > 0 {
71-
endpoint := fmt.Sprintf(endpointTemplate, s.BridgeServiceBase.Url(), destinationAddress, pageNumber+1, pageSize)
80+
endpoint := fmt.Sprintf(endpointTemplate, s.BridgeServiceBase.Url(), urlPath, destinationAddress, pageNumber+1, pageSize)
7281
resp, respError, statusCode, err = httpjson.HTTPGetWithError[GetBridgeResponse, ErrorResponse](s.httpClient, endpoint)
7382
if err != nil {
7483
return nil, 0, err
@@ -89,8 +98,11 @@ func (s *BridgeService) GetDeposits(destinationAddress string, offset, limit int
8998
}
9099

91100
deposits := make([]bridge_service.Deposit, 0, len(bridgesResponses))
92-
for _, d := range bridgesResponses {
93-
deposit := d.ToDeposit()
101+
for _, bridgeResp := range bridgesResponses {
102+
deposit, err := s.responseToDeposit(bridgeResp)
103+
if err != nil {
104+
return nil, 0, err
105+
}
94106
deposits = append(deposits, *deposit)
95107
}
96108

@@ -99,30 +111,93 @@ func (s *BridgeService) GetDeposits(destinationAddress string, offset, limit int
99111
}
100112

101113
func (s *BridgeService) GetProof(depositNetwork, depositCount uint32) (*bridge_service.Proof, error) {
102-
l1InfoTreeIndexEndpoint := fmt.Sprintf("%s/l1-info-tree-index?network_id=%d&deposit_count=%d", s.BridgeServiceBase.Url(), depositNetwork, depositCount)
103-
l1InfoTreeIndex, l1InfoTreeIndexRespError, statusCode, err := httpjson.HTTPGetWithError[int, ErrorResponse](s.httpClient, l1InfoTreeIndexEndpoint)
114+
l1InfoTreeIndex, err := s.getL1InfoTreeIndex(depositNetwork, depositCount)
104115
if err != nil {
105116
return nil, err
106117
}
107118

108-
if statusCode != http.StatusOK {
109-
errMsg := "unable to retrieve l1 info tree index"
110-
log.Warn().Int("code", statusCode).Str("message", l1InfoTreeIndexRespError.Error).Msgf("%s", errMsg)
111-
return nil, fmt.Errorf(l1InfoTreeIndexRespError.Error)
112-
}
113-
114-
endpoint := fmt.Sprintf("%s/claim-proof?network_id=%d&leaf_index=%d&deposit_count=%d", s.BridgeServiceBase.Url(), depositNetwork, l1InfoTreeIndex, depositCount)
119+
endpoint := fmt.Sprintf("%s/%s/claim-proof?network_id=%d&leaf_index=%d&deposit_count=%d", s.BridgeServiceBase.Url(), urlPath, depositNetwork, l1InfoTreeIndex, depositCount)
115120
resp, respError, statusCode, err := httpjson.HTTPGetWithError[GetClaimProofResponse, ErrorResponse](s.httpClient, endpoint)
116121
if err != nil {
117122
return nil, err
118123
}
119124

120125
if statusCode != http.StatusOK {
121-
errMsg := "unable to retrieve bridge deposits"
126+
if statusCode == http.StatusNotFound {
127+
return nil, nil
128+
}
129+
errMsg := "unable to retrieve proof"
122130
log.Warn().Int("code", statusCode).Str("message", respError.Error).Msgf("%s", errMsg)
123131
return nil, fmt.Errorf(respError.Error)
124132
}
125133

126134
proof := resp.ToProof()
127135
return proof, nil
128136
}
137+
138+
func (s *BridgeService) getL1InfoLeaf(depositNetwork, l1InfoTreeIndex uint32) (*getInjectedL1InfoLeafResponse, error) {
139+
endpoint := fmt.Sprintf("%s/%s/injected-l1-info-leaf?network_id=%d&leaf_index=%d", s.BridgeServiceBase.Url(), urlPath, depositNetwork, l1InfoTreeIndex)
140+
resp, errorResp, statusCode, err := httpjson.HTTPGetWithError[*getInjectedL1InfoLeafResponse, ErrorResponse](s.httpClient, endpoint)
141+
if err != nil {
142+
return nil, err
143+
}
144+
145+
if statusCode != http.StatusOK {
146+
if statusCode == http.StatusNotFound {
147+
return nil, nil
148+
}
149+
errMsg := "unable to retrieve l1 info leaf"
150+
log.Warn().Int("code", statusCode).Str("message", errorResp.Error).Msgf("%s", errMsg)
151+
return nil, bridge_service.ErrUnableToRetrieveDeposit
152+
}
153+
154+
return resp, nil
155+
}
156+
157+
func (s *BridgeService) getL1InfoTreeIndex(depositNetwork, depositCount uint32) (*uint32, error) {
158+
l1InfoTreeIndexEndpoint := fmt.Sprintf("%s/%s/l1-info-tree-index?network_id=%d&deposit_count=%d", s.BridgeServiceBase.Url(), urlPath, depositNetwork, depositCount)
159+
l1InfoTreeIndex, l1InfoTreeIndexRespError, statusCode, err := httpjson.HTTPGetWithError[uint32, ErrorResponse](s.httpClient, l1InfoTreeIndexEndpoint)
160+
if err != nil {
161+
return nil, err
162+
}
163+
164+
if statusCode != http.StatusOK {
165+
if statusCode == http.StatusNotFound {
166+
return nil, nil
167+
}
168+
errMsg := "unable to retrieve l1 info tree index"
169+
log.Warn().Int("code", statusCode).Str("message", l1InfoTreeIndexRespError.Error).Msgf("%s", errMsg)
170+
return nil, fmt.Errorf(l1InfoTreeIndexRespError.Error)
171+
}
172+
173+
return &l1InfoTreeIndex, nil
174+
}
175+
176+
func (s *BridgeService) responseToDeposit(bridgeResp BridgeResponse) (*bridge_service.Deposit, error) {
177+
depositNetwork := bridgeResp.OrigNet
178+
depositCount := bridgeResp.DepositCnt
179+
180+
proof, err := s.GetProof(depositNetwork, depositCount)
181+
if err != nil {
182+
return nil, err
183+
}
184+
185+
isReadyForClaim := false
186+
if proof == nil {
187+
l1InfoTreeIndex, err := s.getL1InfoTreeIndex(depositNetwork, depositCount)
188+
if err != nil {
189+
return nil, err
190+
}
191+
if l1InfoTreeIndex == nil {
192+
l1InfoLeaf, err := s.getL1InfoLeaf(depositNetwork, *l1InfoTreeIndex)
193+
if err != nil {
194+
return nil, err
195+
}
196+
isReadyForClaim = l1InfoLeaf != nil
197+
}
198+
}
199+
200+
deposit := bridgeResp.ToDeposit(isReadyForClaim)
201+
202+
return deposit, nil
203+
}

cmd/ulxly/bridge_service/httpjson/httpjson.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ func HTTPGetWithError[T any, TError any](client *http.Client, url string) (obj T
5757
}
5858
defer res.Body.Close()
5959

60-
if res.StatusCode != http.StatusOK {
60+
if res.StatusCode == http.StatusOK {
6161
err = json.Unmarshal(body, &obj)
6262
} else {
6363
err = json.Unmarshal(body, &objError)

cmd/ulxly/bridge_service/legacy/deposit.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,6 @@ func (r *DepositResponse) ToDeposit() (*bridge_service.Deposit, error) {
6868
}
6969

7070
type GetDepositsResponse struct {
71-
Deposits []DepositResponse `json:"deposit"`
71+
Deposits []DepositResponse `json:"deposits"`
7272
Total int `json:"total_cnt,string"`
7373
}

doc/polycli_ulxly_bridge.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ The command also inherits flags from parent commands.
3636
--gas-limit uint force a gas limit when sending a transaction
3737
--gas-price string the gas price to be used
3838
--insecure skip TLS certificate verification
39+
--legacy force usage of legacy bridge service
3940
--pretty-logs Should logs be in pretty format or JSON (default true)
4041
--private-key string the hex encoded private key to be used when sending the tx
4142
--rpc-url string the URL of the RPC to send the transaction

doc/polycli_ulxly_bridge_asset.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,7 @@ The command also inherits flags from parent commands.
110110
--gas-limit uint force a gas limit when sending a transaction
111111
--gas-price string the gas price to be used
112112
--insecure skip TLS certificate verification
113+
--legacy force usage of legacy bridge service
113114
--pretty-logs Should logs be in pretty format or JSON (default true)
114115
--private-key string the hex encoded private key to be used when sending the tx
115116
--rpc-url string the URL of the RPC to send the transaction

doc/polycli_ulxly_bridge_message.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,7 @@ The command also inherits flags from parent commands.
107107
--gas-limit uint force a gas limit when sending a transaction
108108
--gas-price string the gas price to be used
109109
--insecure skip TLS certificate verification
110+
--legacy force usage of legacy bridge service
110111
--pretty-logs Should logs be in pretty format or JSON (default true)
111112
--private-key string the hex encoded private key to be used when sending the tx
112113
--rpc-url string the URL of the RPC to send the transaction

doc/polycli_ulxly_bridge_weth.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ The command also inherits flags from parent commands.
7676
--gas-limit uint force a gas limit when sending a transaction
7777
--gas-price string the gas price to be used
7878
--insecure skip TLS certificate verification
79+
--legacy force usage of legacy bridge service
7980
--pretty-logs Should logs be in pretty format or JSON (default true)
8081
--private-key string the hex encoded private key to be used when sending the tx
8182
--rpc-url string the URL of the RPC to send the transaction

doc/polycli_ulxly_claim-everything.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ The command also inherits flags from parent commands.
3737
--gas-limit uint force a gas limit when sending a transaction
3838
--gas-price string the gas price to be used
3939
--insecure skip TLS certificate verification
40+
--legacy force usage of legacy bridge service
4041
--pretty-logs Should logs be in pretty format or JSON (default true)
4142
--private-key string the hex encoded private key to be used when sending the tx
4243
--rpc-url string the URL of the RPC to send the transaction

0 commit comments

Comments
 (0)