Skip to content

Commit

Permalink
refactor QueryFinalityProviderHasPower
Browse files Browse the repository at this point in the history
  • Loading branch information
Bourne Zhang committed Aug 28, 2024
1 parent 8e840c3 commit 15c6e06
Showing 1 changed file with 8 additions and 24 deletions.
32 changes: 8 additions & 24 deletions clientcontroller/opstackl2/consumer.go
Original file line number Diff line number Diff line change
Expand Up @@ -267,49 +267,33 @@ func (cc *OPStackL2ConsumerController) SubmitBatchFinalitySigs(
// TODO: see this issue https://github.com/babylonlabs-io/finality-provider/issues/390 for more details
func (cc *OPStackL2ConsumerController) QueryFinalityProviderHasPower(fpPk *btcec.PublicKey, blockHeight uint64) (bool, error) {
fpBtcPkHex := bbntypes.NewBIP340PubKeyFromBTCPK(fpPk).MarshalHex()
var nextKey []byte = nil
var nextKey []byte

hasActiveDelegation := false
for {
pagination := &sdkquerytypes.PageRequest{
Key: nextKey,
Limit: 100,
}
// queries the BTCStaking module for all delegations of a finality provider
resp, err := cc.bbnClient.QueryClient.FinalityProviderDelegations(fpBtcPkHex, pagination)
resp, err := cc.bbnClient.QueryClient.FinalityProviderDelegations(fpBtcPkHex, &sdkquerytypes.PageRequest{Key: nextKey, Limit: 100})
if err != nil {
return false, err
}

for _, btcDels := range resp.BtcDelegatorDelegations {
// early return if the delegation is active
if hasActiveDelegation {
break
}
for _, btcDel := range btcDels.Dels {
activate, delErr := cc.isDelegationActive(btcDel)
if delErr != nil {
active, err := cc.isDelegationActive(btcDel)
if err != nil {
continue
}
if activate {
hasActiveDelegation = true
break
if active {
return true, nil
}
}
}

// early return if the delegation is active
if hasActiveDelegation {
break
}

if resp.Pagination == nil || resp.Pagination.NextKey == nil {
break
}
nextKey = resp.Pagination.NextKey
}

return hasActiveDelegation, nil

return false, nil
}

// QueryLatestFinalizedBlock returns the finalized L2 block from a RPC call
Expand Down

0 comments on commit 15c6e06

Please sign in to comment.