-
Notifications
You must be signed in to change notification settings - Fork 22
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Feat: impl QueryFinalityProviderHasPower in consumer chain #44
Merged
bournezjc
merged 10 commits into
base/consumer-chain-support
from
bourne/dev-consumer-chain-support
Aug 29, 2024
Merged
Changes from 9 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
9faac2a
add bbnclient in opl2 consumer; impl QueryFinalityProviderHasPower in…
05f331f
change cwclient to bbnclient to fetch the correct bbn block number
9269284
add base case for GetBlockHeightByL2Timestamp & QueryLatestFinalizedB…
1ba056d
revert QueryLatestFinalizedBlock
6a9fafe
make QueryLatestFinalizedBlock returns nil when initialized
22ed38c
change QueryFinalityProviderHasPower logic
1e09f7f
fix: missing to insert btc delegation for second consumer fp
lesterli 98d33e4
chore: no BTC delegation, no power
lesterli 2b3308e
refactor QueryFinalityProviderHasPower
ce55f75
refactor isDelegationActive
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,12 +6,15 @@ | |
"encoding/json" | ||
"fmt" | ||
"math" | ||
bbnclient "github.com/babylonlabs-io/babylon/client/client" | ||
btcstakingtypes "github.com/babylonlabs-io/babylon/x/btcstaking/types" | ||
sdkquerytypes "github.com/cosmos/cosmos-sdk/types/query" | ||
"math/big" | ||
|
||
sdkErr "cosmossdk.io/errors" | ||
wasmdparams "github.com/CosmWasm/wasmd/app/params" | ||
wasmtypes "github.com/CosmWasm/wasmd/x/wasm/types" | ||
bbnapp "github.com/babylonlabs-io/babylon/app" | ||
Check failure on line 17 in clientcontroller/opstackl2/consumer.go GitHub Actions / lint_test / build
Check failure on line 17 in clientcontroller/opstackl2/consumer.go GitHub Actions / lint_test / unit-tests
|
||
bbntypes "github.com/babylonlabs-io/babylon/types" | ||
fgclient "github.com/babylonlabs-io/finality-gadget/client" | ||
"github.com/babylonlabs-io/finality-provider/clientcontroller/api" | ||
|
@@ -41,6 +44,7 @@ | |
Cfg *fpcfg.OPStackL2Config | ||
CwClient *cwclient.Client | ||
opl2Client *ethclient.Client | ||
bbnClient *bbnclient.Client | ||
logger *zap.Logger | ||
} | ||
|
||
|
@@ -66,10 +70,26 @@ | |
return nil, fmt.Errorf("failed to create OPStack L2 client: %w", err) | ||
} | ||
|
||
bbnConfig := opl2Cfg.ToBBNConfig() | ||
babylonConfig := fpcfg.BBNConfigToBabylonConfig(&bbnConfig) | ||
|
||
if err := babylonConfig.Validate(); err != nil { | ||
return nil, fmt.Errorf("invalid config for Babylon client: %w", err) | ||
} | ||
|
||
bc, err := bbnclient.New( | ||
&babylonConfig, | ||
logger, | ||
) | ||
if err != nil { | ||
return nil, fmt.Errorf("failed to create Babylon client: %w", err) | ||
} | ||
|
||
return &OPStackL2ConsumerController{ | ||
opl2Cfg, | ||
cwClient, | ||
opl2Client, | ||
bc, | ||
logger, | ||
}, nil | ||
} | ||
|
@@ -248,7 +268,34 @@ | |
// Now we can simply hardcode the voting power to true | ||
// 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) { | ||
return true, nil | ||
fpBtcPkHex := bbntypes.NewBIP340PubKeyFromBTCPK(fpPk).MarshalHex() | ||
var nextKey []byte | ||
|
||
for { | ||
resp, err := cc.bbnClient.QueryClient.FinalityProviderDelegations(fpBtcPkHex, &sdkquerytypes.PageRequest{Key: nextKey, Limit: 100}) | ||
if err != nil { | ||
return false, err | ||
} | ||
|
||
for _, btcDels := range resp.BtcDelegatorDelegations { | ||
for _, btcDel := range btcDels.Dels { | ||
active, err := cc.isDelegationActive(btcDel) | ||
if err != nil { | ||
continue | ||
} | ||
if active { | ||
return true, nil | ||
} | ||
} | ||
} | ||
|
||
if resp.Pagination == nil || resp.Pagination.NextKey == nil { | ||
break | ||
} | ||
nextKey = resp.Pagination.NextKey | ||
} | ||
|
||
return false, nil | ||
} | ||
|
||
// QueryLatestFinalizedBlock returns the finalized L2 block from a RPC call | ||
|
@@ -259,6 +306,11 @@ | |
if err != nil { | ||
return nil, err | ||
} | ||
|
||
if l2Block.Number.Uint64() == 0 { | ||
return nil, nil | ||
} | ||
|
||
return &types.BlockInfo{ | ||
Height: l2Block.Number.Uint64(), | ||
Hash: l2Block.Hash().Bytes(), | ||
|
@@ -348,6 +400,10 @@ | |
if err != nil { | ||
return false, err | ||
} | ||
|
||
if l2Block == nil { | ||
return false, nil | ||
} | ||
if height > l2Block.Height { | ||
return false, nil | ||
} | ||
|
@@ -486,3 +542,32 @@ | |
cc.opl2Client.Close() | ||
return cc.CwClient.Stop() | ||
} | ||
|
||
func (cc *OPStackL2ConsumerController) isDelegationActive( | ||
btcDel *btcstakingtypes.BTCDelegationResponse, | ||
) (bool, error) { | ||
|
||
btcstakingParams, err := cc.bbnClient.QueryClient.BTCStakingParams() | ||
if err != nil { | ||
return false, err | ||
} | ||
|
||
covQuorum := btcstakingParams.GetParams().CovenantQuorum | ||
ud := btcDel.UndelegationResponse | ||
|
||
if len(ud.GetDelegatorUnbondingSigHex()) > 0 { | ||
return false, nil | ||
} | ||
|
||
if uint32(len(btcDel.CovenantSigs)) < covQuorum { | ||
return false, nil | ||
} | ||
if len(ud.CovenantUnbondingSigList) < int(covQuorum) { | ||
return false, nil | ||
} | ||
if len(ud.CovenantSlashingSigs) < int(covQuorum) { | ||
return false, nil | ||
} | ||
|
||
return true, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Since this never changes and the
isDelegationActive
function will be called upon every BTC delegation, how about we passbtcstakingParams
as an argument toisDelegationActive
rather than querying it everytime?