Skip to content
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

chore: pull main to feat 457 #465

Merged
merged 103 commits into from
Jan 31, 2025
Merged
Changes from 1 commit
Commits
Show all changes
103 commits
Select commit Hold shift + click to select a range
2207f55
integration: set up `x/btcstkconsumer` module (#206)
SebastianElvis Mar 13, 2024
88f891c
F/consumer chain registry (#205)
maurolacy Mar 13, 2024
f4b88ab
F/consumer chain registry 2 (#209)
maurolacy Mar 13, 2024
873cfce
F/register codecs (#210)
maurolacy Mar 15, 2024
7928f66
F/consumer FP registry (#216)
maurolacy Mar 20, 2024
a4d244c
F/consumer fp registry 2 (#220)
maurolacy Mar 20, 2024
65657d1
handling restaked BTC delegations to consumer FPs (#222)
SebastianElvis Mar 22, 2024
0c5d6aa
F/consumer chain cli (#221)
maurolacy Mar 22, 2024
76efc0d
Update CHANGELOG
Mar 22, 2024
ce164ea
fix: only calculating Babylon FPs for FP set rotation (#223)
SebastianElvis Mar 25, 2024
5c15e74
Update CHANGELOG
Mar 25, 2024
b98b20e
fix
SebastianElvis Apr 2, 2024
0a6e9f8
R/consumer chain 2 (#224)
maurolacy Apr 3, 2024
c94b5f5
linting validateRestakedFPs
SebastianElvis Apr 16, 2024
4c1e0d5
proto: define IBC packet formats for PoS integration (#229)
SebastianElvis Apr 22, 2024
56b21f4
zoneconcierge: let zoneconcierge hold a pointer to btcstaking module …
SebastianElvis Apr 22, 2024
4601ff5
tests: Add pos-integration E2E Tests (#225)
gusin13 Apr 22, 2024
27f7072
chore: fix some proto formats for IBC packets (#234)
SebastianElvis Apr 25, 2024
78120b3
chore: rename chain to consumer for PoS integration (#233)
SebastianElvis Apr 25, 2024
038943a
btcstkconsumer: separate IBC packet for provider / consumer (#237)
SebastianElvis Apr 30, 2024
8f134e1
feat: Store btcstaking events in kv store and send IBC packet (#235)
gusin13 Apr 30, 2024
1ed7149
tests: btcstaking events e2e tests (#240)
gusin13 May 16, 2024
61ef003
release v0.2.0 rc0 (#245)
SebastianElvis May 17, 2024
49663ec
cd: Allows base/consumer-chain-support branch to run push_docker job …
hiepmai-babylonchain May 20, 2024
5f8413b
btcstkconsumer: RPC functions for BTC staking consumer module (#247)
SebastianElvis May 20, 2024
5701ec9
Fix/babylon update (#253)
maurolacy May 28, 2024
a0ec504
tests: Add undelegation and fp voting power test cases (#254)
gusin13 May 29, 2024
5d00627
feat: Add `cosmwasm_2_0` to `wasmCapabilities` (#255)
lesterli Jun 13, 2024
db66211
feat: Add Grpc querier (#257)
lesterli Jun 18, 2024
265678f
chore: remove aarch64 wasm (#261)
lesterli Jun 20, 2024
fdd9040
Fix rebase error
Jul 8, 2024
e57fefc
Fix rebase error / Simplify testutil btcstaking helpers
Jul 8, 2024
253dab4
fix: panic to merge proto files (#264)
lesterli Jul 5, 2024
e7658c2
U/contracts 0.7 (#270)
maurolacy Jul 10, 2024
0a646a3
Fix: Add stker_addr to active btc delegation
Jul 10, 2024
a6a6fc7
Update contracts from rebase branch
Jul 11, 2024
6f1b119
Fix: btc staking integration rebase errors
Jul 12, 2024
8531b8b
Fix: ExecCmd vs ExecTxCmd inconsistency
Jul 12, 2024
6c9ffa0
Fix in passing: docker warning
Jul 12, 2024
d1afabf
Fix syntax
Jul 12, 2024
d63dc74
proto-lint
Jul 12, 2024
33db0a6
proto-gen
Jul 12, 2024
b924ff0
Sanitize code path to avoid gosec warning
Jul 12, 2024
735e248
Increase Go min version to 1.22
Jul 12, 2024
fead9dc
Increase go version in CI
Jul 12, 2024
b83688b
Increase Go version in babylon container
Jul 12, 2024
30f2a15
Workaround consumer and babylon FP creation for staking integration t…
Jul 13, 2024
d1c5b08
Revert "Workaround consumer and babylon FP creation for staking integ…
Jul 15, 2024
a0e86bc
Comment pop.Verify out
Jul 15, 2024
dbf026c
fix fp gen
SebastianElvis Jul 15, 2024
5cdf5bb
fix gosec
SebastianElvis Jul 15, 2024
3d8f190
chore: rename del Babylon address in e2e (#273)
SebastianElvis Jul 17, 2024
6ecfdf4
Merge branch 'dev' into try-rebase-dev
SebastianElvis Jul 30, 2024
ce31233
fix compile
SebastianElvis Jul 30, 2024
d77e0cb
fix all tests
SebastianElvis Jul 30, 2024
74a24c9
Merge pull request #1 from babylonlabs-io/try-rebase-dev
SebastianElvis Aug 1, 2024
bd458da
feat: Automatic consumer registration (#11)
gusin13 Aug 14, 2024
20c5405
Merge branch 'dev' into merge-dev
SebastianElvis Aug 16, 2024
b03a1d8
fix e2e
SebastianElvis Aug 16, 2024
6bcadbc
Merge pull request #13 from babylonlabs-io/merge-dev-1
SebastianElvis Aug 17, 2024
81ee356
Add Consumer packet data defs / gens (#15)
maurolacy Aug 28, 2024
7942dae
tests: bcd setup in e2e (#57)
gusin13 Sep 11, 2024
35a5a1b
feat: Babylon FP cascaded slashing (#69)
gusin13 Sep 17, 2024
d6b39e8
Merge branch 'main' into try-rebase
SebastianElvis Sep 19, 2024
59a2486
fix compile
SebastianElvis Sep 19, 2024
747ca8e
feat(ADR-025): Enable jailing and unjailing (#80)
gitferry Sep 19, 2024
063241e
fix compile/tests
SebastianElvis Sep 20, 2024
bca42f1
accommodate timestamped pub rand for e2e
SebastianElvis Sep 20, 2024
0e661b3
Merge pull request #83 from babylonlabs-io/try-rebase
SebastianElvis Sep 23, 2024
a6f728b
Merge branch 'main' into base/consumer-chain-support
SebastianElvis Sep 23, 2024
a98269d
feat: Consumer FP cascaded slashing (#85)
gusin13 Sep 25, 2024
3520b05
f/Better IBC transfer tests (#128)
maurolacy Oct 4, 2024
8ee2c85
chore: gh sync workflow from main to base (#133)
gusin13 Oct 15, 2024
d10dd40
Merge branch 'main' into rebase-devnet-5
SebastianElvis Nov 14, 2024
540d59f
fix conflicts
SebastianElvis Nov 14, 2024
1be1c2b
fix compile
SebastianElvis Nov 15, 2024
098751b
Merge branch 'main' into rebase-devnet-5
SebastianElvis Nov 18, 2024
579d66c
fix tests and disable v1 upgrade e2e
SebastianElvis Nov 20, 2024
3aa1afc
Merge pull request #266 from babylonlabs-io/rebase-devnet-5
SebastianElvis Nov 20, 2024
ad3e100
F/consumer rewards e2e (#301)
maurolacy Nov 27, 2024
4898d8d
zoneconcierge: permissioned integration (#291)
SebastianElvis Dec 3, 2024
9300461
feat: add the slashing msg handler for op consumer fp (#327)
lesterli Dec 7, 2024
72654c8
e2e: remove e2e related to BTC staking integration (#339)
SebastianElvis Dec 13, 2024
e901f1c
fix: remove unused deps in `.proto` files (#355)
lesterli Dec 18, 2024
92ed24c
Merge branch 'main' into merge-main-to-base-2025
SebastianElvis Jan 13, 2025
e2651ab
fix compile and tests
SebastianElvis Jan 13, 2025
c06b2fe
fix lint
SebastianElvis Jan 13, 2025
abeef54
Merge pull request #399 from babylonlabs-io/merge-main-to-base-2025
filippos47 Jan 14, 2025
cbfb705
Merge branch 'main' into merge-base-2025-2
SebastianElvis Jan 14, 2025
9fa494b
Merge pull request #403 from babylonlabs-io/merge-base-2025-2
filippos47 Jan 14, 2025
873f123
fix: fix comments to the base branch (#418)
SebastianElvis Jan 17, 2025
eaf20a1
fix: fix comments to the base branch 2 (#424)
SebastianElvis Jan 22, 2025
3221f11
Merge branch 'main' into merge-main-to-base-0122
SebastianElvis Jan 22, 2025
3273ea8
Merge pull request #432 from babylonlabs-io/merge-main-to-base-0122
filippos47 Jan 22, 2025
10905e7
Merge pull request #402 from babylonlabs-io/base/consumer-chain-support
filippos47 Jan 23, 2025
2b8c528
chore: remove `cosmos/relayer` dependency (#429)
Lazar955 Jan 23, 2025
abc2272
chore: fix flaky test `FuzzCreateBTCDelegationWithParamsFromBtcHeight…
KonradStaniec Jan 27, 2025
1038c81
fix: swagger doc gen incentive v1 (#443)
RafilxTenfen Jan 27, 2025
367f8c0
Reject forks if the first header is already known (#445)
KonradStaniec Jan 27, 2025
02612f9
backport changelog update (#454)
KonradStaniec Jan 28, 2025
83af4ad
chore: update cosmos provider to expose public functions (#458)
RafilxTenfen Jan 29, 2025
70b63e6
Merge branch 'main' of github.com:babylonlabs-io/babylon into feat/rm…
RafilxTenfen Jan 31, 2025
58e9e40
fix: lint
RafilxTenfen Jan 31, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
feat: Store btcstaking events in kv store and send IBC packet (#235)
gusin13 authored and Mauro Lacy committed Jul 8, 2024
commit 8f134e1947625a1dfb33b81366dc93e722b0aff1
31 changes: 23 additions & 8 deletions proto/babylon/btcstaking/v1/packet.proto
Original file line number Diff line number Diff line change
@@ -43,8 +43,8 @@ message NewFinalityProvider {
// commission defines the commission rate of the finality provider.
// It forms as a string converted from "cosmossdk.io/math.LegacyDec"
string commission = 2;
// babylon_pk is the Babylon secp256k1 PK of this finality provider
cosmos.crypto.secp256k1.PubKey babylon_pk = 3;
// addr is the bech32 address identifier of the finality provider.
string addr = 3;
// btc_pk_hex is the Bitcoin secp256k1 PK of this finality provider
// the PK follows encoding in BIP-340 spec in hex format
string btc_pk_hex = 4;
@@ -53,24 +53,22 @@ message NewFinalityProvider {
// master_pub_rand is the master public randomness of the finality provider
// encoded as a base58 string
string master_pub_rand = 6;
// registered_epoch is the epoch when this finality provider is registered
uint64 registered_epoch = 7;
// slashed_babylon_height indicates the Babylon height when
// the finality provider is slashed.
// if it's 0 then the finality provider is not slashed
uint64 slashed_babylon_height = 8;
uint64 slashed_babylon_height = 7;
// slashed_btc_height indicates the BTC height when
// the finality provider is slashed.
// if it's 0 then the finality provider is not slashed
uint64 slashed_btc_height = 9;
uint64 slashed_btc_height = 8;
// consumer_id is the ID of the consumer the finality provider is operating on.
// If it's missing / empty, it's assumed the finality provider is operating in Babylon.
string consumer_id = 10;
string consumer_id = 9;
}

// ActiveBTCDelegation is an IBC packet sent from Babylon to consumer
// upon a BTC delegation newly receives covenant signatures and thus becomes active
message ActiveBTCDelegation {
message ActiveBTCDelegation {
// btc_pk_hex is the Bitcoin secp256k1 PK of this BTC delegation
// the PK follows encoding in BIP-340 spec in hex format
string btc_pk_hex = 1;
@@ -157,3 +155,20 @@ message UnbondedBTCDelegation {
// It proves that the BTC delegator wants to unbond
bytes unbonding_tx_sig = 2;
}

// BTCStakingConsumerEvent defines the structure for storing BTC staking events
// which will be later sent to consumer chains as IBC packets.
message BTCStakingConsumerEvent {
// oneof event represents a mutually exclusive choice among different event types.
// Only one event type can be set within each BTCStakingEvent message instance.
oneof event {
// NewFinalityProvider represents the event of a new finality provider being created.
NewFinalityProvider new_fp = 1;
// ActiveBTCDelegation represents the event of an active BTC delegation.
ActiveBTCDelegation active_del = 2;
// SlashedBTCDelegation represents the event of a slashed BTC delegation.
SlashedBTCDelegation slashed_del = 3;
// UnbondedBTCDelegation represents the event of an unbonded BTC delegation.
UnbondedBTCDelegation unbonded_del = 4;
}
}
19 changes: 17 additions & 2 deletions test/e2e/btc_staking_e2e_test.go
Original file line number Diff line number Diff line change
@@ -79,7 +79,22 @@ func (s *BTCStakingTestSuite) Test1CreateFinalityProviderAndDelegation() {
nonValidatorNode, err := chainA.GetNodeAtIndex(2)
s.NoError(err)

cacheFP = s.CreateNodeFP(nonValidatorNode)
//cacheFP = s.CreateNodeFP(nonValidatorNode)
/*
create a random finality provider on Babylon
*/
// NOTE: we use the node's secret key as Babylon secret key for the finality provider
fp, err := datagen.GenRandomCustomFinalityProvider(r, fpBTCSK, nonValidatorNode.SecretKey, "")
s.NoError(err)
nonValidatorNode.CreateFinalityProvider("val", fp.BtcPk, fp.Pop, fp.Description.Moniker, fp.Description.Identity, fp.Description.Website, fp.Description.SecurityContact, fp.Description.Details, fp.Commission)

// wait for a block so that above txs take effect
nonValidatorNode.WaitForNextBlock()

// query the existence of finality provider and assert equivalence
actualFps := nonValidatorNode.QueryFinalityProviders()
s.Len(actualFps, 1)
s.equalFinalityProviderResp(fp, actualFps[0])

/*
create a random BTC delegation under this finality provider
@@ -854,7 +869,7 @@ func (s *BTCStakingTestSuite) CreateNodeFP(node *chain.NodeConfig) (newFP *bstyp
nodeAddr, err := sdk.AccAddressFromBech32(node.PublicAddress)
s.NoError(err)

newFP, err = datagen.GenRandomFinalityProviderWithBTCBabylonSKs(r, fpBTCSK, nodeAddr)
newFP, err = datagen.GenRandomFinalityProviderWithBTCBabylonSKs(r, fpBTCSK, nodeAddr, "")
s.NoError(err)
node.CreateFinalityProvider(newFP.Addr, newFP.BtcPk, newFP.Pop, newFP.Description.Moniker, newFP.Description.Identity, newFP.Description.Website, newFP.Description.SecurityContact, newFP.Description.Details, newFP.Commission)

43 changes: 4 additions & 39 deletions test/e2e/btc_staking_integration_e2e_test.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,8 @@
package e2e

import (
"errors"
"fmt"
"math"
"time"

"github.com/babylonchain/babylon/crypto/eots"
"github.com/babylonchain/babylon/test/e2e/configurer"
"github.com/babylonchain/babylon/test/e2e/configurer/chain"
"github.com/babylonchain/babylon/test/e2e/initialization"
@@ -15,7 +11,6 @@ import (
btcctypes "github.com/babylonchain/babylon/x/btccheckpoint/types"
bstypes "github.com/babylonchain/babylon/x/btcstaking/types"
bsctypes "github.com/babylonchain/babylon/x/btcstkconsumer/types"
ckpttypes "github.com/babylonchain/babylon/x/checkpointing/types"
"github.com/btcsuite/btcd/btcec/v2"
"github.com/btcsuite/btcd/btcutil"
"github.com/btcsuite/btcd/wire"
@@ -144,12 +139,10 @@ func (s *BTCStakingIntegrationTestSuite) createVerifyConsumerFP(babylonNode *cha
*/
// NOTE: we use the node's secret key as Babylon secret key for the finality provider
czFpBTCSK, _, _ := datagen.GenRandomBTCKeyPair(r)
sr, _, err := eots.NewMasterRandPair(r)
s.NoError(err)
czFp, err := datagen.GenRandomCustomFinalityProvider(r, czFpBTCSK, babylonNode.SecretKey, sr)
czFp, err := datagen.GenRandomCustomFinalityProvider(r, czFpBTCSK, babylonNode.SecretKey, consumerId)
s.NoError(err)
babylonNode.CreateConsumerFinalityProvider(
czFp.BabylonPk, czFp.BtcPk, czFp.Pop, czFp.MasterPubRand, consumerId, czFp.Description.Moniker,
czFp.BabylonPk, czFp.BtcPk, czFp.Pop, consumerId, czFp.Description.Moniker,
czFp.Description.Identity, czFp.Description.Website, czFp.Description.SecurityContact,
czFp.Description.Details, czFp.Commission,
)
@@ -177,11 +170,9 @@ func (s *BTCStakingIntegrationTestSuite) createVerifyBabylonFP(babylonNode *chai
*/
// NOTE: we use the node's secret key as Babylon secret key for the finality provider
babylonFpBTCSK, _, _ := datagen.GenRandomBTCKeyPair(r)
sr, _, err := eots.NewMasterRandPair(r)
s.NoError(err)
babylonFp, err := datagen.GenRandomCustomFinalityProvider(r, babylonFpBTCSK, babylonNode.SecretKey, sr)
babylonFp, err := datagen.GenRandomCustomFinalityProvider(r, babylonFpBTCSK, babylonNode.SecretKey, "")
s.NoError(err)
babylonNode.CreateFinalityProvider(babylonFp.BabylonPk, babylonFp.BtcPk, babylonFp.Pop, babylonFp.MasterPubRand, babylonFp.Description.Moniker, babylonFp.Description.Identity, babylonFp.Description.Website, babylonFp.Description.SecurityContact, babylonFp.Description.Details, babylonFp.Commission)
babylonNode.CreateFinalityProvider(babylonFp.BabylonPk, babylonFp.BtcPk, babylonFp.Pop, babylonFp.Description.Moniker, babylonFp.Description.Identity, babylonFp.Description.Website, babylonFp.Description.SecurityContact, babylonFp.Description.Details, babylonFp.Commission)

// wait for a block so that above txs take effect
babylonNode.WaitForNextBlock()
@@ -202,32 +193,6 @@ func (s *BTCStakingIntegrationTestSuite) createVerifyBabylonFP(babylonNode *chai

// helper function: create a Babylon delegation and restake to Babylon and consumer finality providers
func (s *BTCStakingIntegrationTestSuite) createBabylonDelegation(nonValidatorNode *chain.NodeConfig, babylonFp *bstypes.FinalityProviderResponse, consumerFp *bsctypes.FinalityProviderResponse) (*btcec.PublicKey, string) {
// finalise epochs until the registered epoch of the finality provider
// so that the finality provider can receive BTC delegations
// TODO: it is assumed here that babylonFp is registered after consumerFp so
// if we finalize registered epoch of babylonFp the other would also get finalized
// ideally we should get registered epoch of each restaked fp and finalize it.
var (
startEpoch = uint64(1)
endEpoch = babylonFp.RegisteredEpoch
)
// wait until the end epoch is sealed
s.Eventually(func() bool {
ch, _ := nonValidatorNode.QueryCurrentHeight()
ce, _ := nonValidatorNode.QueryCurrentEpoch()
fmt.Println("current height", ch)
fmt.Println("current epoch", ce)
resp, err := nonValidatorNode.QueryRawCheckpoint(endEpoch)
if err != nil {
if !errors.Is(err, ckpttypes.ErrCkptDoesNotExist) {
return false
}
}
return resp.Status == ckpttypes.Sealed
}, time.Minute, time.Second*5)
// finalise these epochs
nonValidatorNode.FinalizeSealedEpochs(startEpoch, endEpoch)

/*
create a random BTC delegation restaking to Babylon and consumer finality providers
*/
4 changes: 2 additions & 2 deletions test/e2e/configurer/chain/commands_btcstaking_integration.go
Original file line number Diff line number Diff line change
@@ -21,7 +21,7 @@ func (n *NodeConfig) RegisterConsumer(consumerId, consumerName, consumerDesc str
n.LogActionF("successfully registered consumer on babylon")
}

func (n *NodeConfig) CreateConsumerFinalityProvider(babylonPK *secp256k1.PubKey, btcPK *bbn.BIP340PubKey, pop *bstypes.ProofOfPossession, masterPubRand, consumerId, moniker, identity, website, securityContract, details string, commission *sdkmath.LegacyDec) {
func (n *NodeConfig) CreateConsumerFinalityProvider(babylonPK *secp256k1.PubKey, btcPK *bbn.BIP340PubKey, pop *bstypes.ProofOfPossession, consumerId, moniker, identity, website, securityContract, details string, commission *sdkmath.LegacyDec) {
n.LogActionF("creating consumer finality provider")

// get babylon PK hex
@@ -35,7 +35,7 @@ func (n *NodeConfig) CreateConsumerFinalityProvider(babylonPK *secp256k1.PubKey,
require.NoError(n.t, err)

cmd := []string{
"babylond", "tx", "btcstaking", "create-finality-provider", babylonPKHex, btcPKHex, popHex, masterPubRand, "--from=val", "--consumer-id", consumerId, "--moniker", moniker, "--identity", identity, "--website", website, "--security-contact", securityContract, "--details", details, "--commission-rate", commission.String(),
"babylond", "tx", "btcstaking", "create-finality-provider", babylonPKHex, btcPKHex, popHex, "--from=val", "--consumer-id", consumerId, "--moniker", moniker, "--identity", identity, "--website", website, "--security-contact", securityContract, "--details", details, "--commission-rate", commission.String(),
}
_, _, err = n.containerManager.ExecTxCmd(n.t, n.chainId, n.Name, cmd)
require.NoError(n.t, err)
18 changes: 12 additions & 6 deletions testutil/datagen/btcstaking.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package datagen

import (
cryptotypes "github.com/cosmos/cosmos-sdk/crypto/types"
"math/rand"
"testing"

@@ -26,12 +27,12 @@ const (
)

func GenRandomFinalityProvider(r *rand.Rand) (*bstypes.FinalityProvider, error) {
// key pairs
// BTC key pairs
btcSK, _, err := GenRandomBTCKeyPair(r)
if err != nil {
return nil, err
}
return GenRandomFinalityProviderWithBTCSK(r, btcSK)
return GenRandomFinalityProviderWithBTCSK(r, btcSK, "")
}

func CreateNFinalityProviders(r *rand.Rand, t *testing.T, n int) []*bstypes.FinalityProvider {
@@ -44,8 +45,8 @@ func CreateNFinalityProviders(r *rand.Rand, t *testing.T, n int) []*bstypes.Fina
return fps
}

func GenRandomFinalityProviderWithBTCSK(r *rand.Rand, btcSK *btcec.PrivateKey) (*bstypes.FinalityProvider, error) {
return GenRandomFinalityProviderWithBTCBabylonSKs(r, btcSK, GenRandomAccount().GetAddress())
func GenRandomFinalityProviderWithBTCSK(r *rand.Rand, btcSK *btcec.PrivateKey, consumerID string) (*bstypes.FinalityProvider, error) {
return GenRandomFinalityProviderWithBTCBabylonSKs(r, btcSK, GenRandomAccount().GetAddress(), consumerID)
}

func GenRandomCommission(r *rand.Rand) sdkmath.LegacyDec {
@@ -56,7 +57,7 @@ func GenRandomDescription(r *rand.Rand) *stakingtypes.Description {
return &stakingtypes.Description{Moniker: GenRandomHexStr(r, 10)}
}

func GenRandomFinalityProviderWithBTCBabylonSKs(r *rand.Rand, btcSK *btcec.PrivateKey, fpAddr sdk.AccAddress) (*bstypes.FinalityProvider, error) {
func GenRandomFinalityProviderWithBTCBabylonSKs(r *rand.Rand, btcSK *btcec.PrivateKey, fpAddr sdk.AccAddress, consumerID string) (*bstypes.FinalityProvider, error) {
// commission
commission := GenRandomCommission(r)
// description
@@ -75,10 +76,15 @@ func GenRandomFinalityProviderWithBTCBabylonSKs(r *rand.Rand, btcSK *btcec.Priva
BtcPk: bip340PK,
Addr: fpAddr.String(),
Pop: pop,
ConsumerId: "", // TODO: parameterise
ConsumerId: consumerID,
}, nil
}

func GenRandomCustomFinalityProvider(r *rand.Rand, btcSK *btcec.PrivateKey, bbnSK cryptotypes.PrivKey, consumerID string) (*bstypes.FinalityProvider, error) {
fpAddr := sdk.MustAccAddressFromBech32(bbnSK.PubKey().Address().String())
return GenRandomFinalityProviderWithBTCBabylonSKs(r, btcSK, fpAddr, consumerID)
}

// TODO: accomodate presign unbonding flow
func GenRandomBTCDelegation(
r *rand.Rand,
21 changes: 21 additions & 0 deletions x/btcstaking/keeper/btc_consumer_delegations.go
Original file line number Diff line number Diff line change
@@ -92,6 +92,27 @@ func (k Keeper) GetBTCConsumerDelegatorDelegationsResponses(
return btcDels, pageRes, nil
}

// SetConsumerFinalityProvider checks conditions and sets a new finality provider for a consumer.
func (k Keeper) SetConsumerFinalityProvider(ctx context.Context, fp *bstypes.FinalityProvider, consumerID string) error {
// check if the finality provider already exists to prevent duplicates
if k.bscKeeper.HasConsumerFinalityProvider(ctx, fp.BtcPk) {
return bstypes.ErrFpRegistered
}
// verify that the consumer is registered within the btcstkconsumer module
if !k.bscKeeper.IsConsumerRegistered(ctx, consumerID) {
return bstypes.ErrConsumerIDNotRegistered
}
// set the finality provider in btcstkconsumer module
k.bscKeeper.SetConsumerFinalityProvider(ctx, fp)

// record the event in btc staking consumer event store
if err := k.AddBTCStakingConsumerEvent(ctx, consumerID, bstypes.CreateNewFinalityProviderEvent(fp)); err != nil {
return err
}

return nil
}

// getBTCConsumerDelegatorDelegationIndex gets the BTC delegation index with a given BTC PK under a given finality provider
func (k Keeper) getBTCConsumerDelegatorDelegationIndex(ctx context.Context, fpBTCPK *bbn.BIP340PubKey, delBTCPK *bbn.BIP340PubKey) *bstypes.BTCDelegatorDelegationIndex {
// ensure the finality provider exists
56 changes: 50 additions & 6 deletions x/btcstaking/keeper/btc_delegations.go
Original file line number Diff line number Diff line change
@@ -108,6 +108,21 @@ func (k Keeper) addCovenantSigsToBTCDelegation(
activeEvent := types.NewEventPowerDistUpdateWithBTCDel(event)
btcTip := k.btclcKeeper.GetTipInfo(ctx)
k.addPowerDistUpdateEvent(ctx, btcTip.Height, activeEvent)

// get consumer ids of only non-Babylon finality providers
restakedFPConsumerIDs, err := k.restakedFPConsumerIDs(ctx, btcDel.FpBtcPkList)
if err != nil {
panic(fmt.Errorf("failed to get consumer ids for the restaked BTC delegation: %w", err))
}
consumerEvent, err := types.CreateActiveBTCDelegationEvent(btcDel)
if err != nil {
panic(fmt.Errorf("failed to create active BTC delegation event: %w", err))
}
for _, consumerID := range restakedFPConsumerIDs {
if err := k.AddBTCStakingConsumerEvent(ctx, consumerID, consumerEvent); err != nil {
panic(fmt.Errorf("failed to add active BTC delegation event: %w", err))
}
}
}
}

@@ -137,6 +152,22 @@ func (k Keeper) btcUndelegate(
unbondedEvent := types.NewEventPowerDistUpdateWithBTCDel(event)
btcTip := k.btclcKeeper.GetTipInfo(ctx)
k.addPowerDistUpdateEvent(ctx, btcTip.Height, unbondedEvent)

// get consumer ids of only non-Babylon finality providers
restakedFPConsumerIDs, err := k.restakedFPConsumerIDs(ctx, btcDel.FpBtcPkList)
if err != nil {
panic(fmt.Errorf("failed to get consumer ids for the restaked BTC delegation: %w", err))
}
// create consumer event for unbonded BTC delegation and add it to the consumer's event store
consumerEvent, err := types.CreateUnbondedBTCDelegationEvent(btcDel)
if err != nil {
panic(fmt.Errorf("failed to create unbonded BTC delegation event: %w", err))
}
for _, consumerID := range restakedFPConsumerIDs {
if err = k.AddBTCStakingConsumerEvent(ctx, consumerID, consumerEvent); err != nil {
panic(fmt.Errorf("failed to add active BTC delegation event: %w", err))
}
}
}

func (k Keeper) setBTCDelegation(ctx context.Context, btcDel *types.BTCDelegation) {
@@ -150,10 +181,8 @@ func (k Keeper) setBTCDelegation(ctx context.Context, btcDel *types.BTCDelegatio
// one of them is a Babylon finality provider. It also checks whether the BTC stake is
// restaked to FPs of consumer chains
func (k Keeper) validateRestakedFPs(ctx context.Context, fpBTCPKs []bbn.BIP340PubKey) (bool, error) {
var (
restakedToBabylon = false
restakedToConsumers = false
)
restakedToBabylon := false
restakedToConsumers := false

for _, fpBTCPK := range fpBTCPKs {
// find the fp and determine whether it's Babylon fp or consumer chain fp
@@ -162,7 +191,6 @@ func (k Keeper) validateRestakedFPs(ctx context.Context, fpBTCPKs []bbn.BIP340Pu
if fp.IsSlashed() {
return false, types.ErrFpAlreadySlashed
}
// ensure the finality provider's registered epoch is finalised
restakedToBabylon = true
continue
} else if consumerID, err := k.bscKeeper.GetConsumerOfFinalityProvider(ctx, &fpBTCPK); err == nil {
@@ -174,7 +202,6 @@ func (k Keeper) validateRestakedFPs(ctx context.Context, fpBTCPKs []bbn.BIP340Pu
if fp.IsSlashed() {
return false, types.ErrFpAlreadySlashed
}
// ensure the finality provider's registered epoch is finalised
restakedToConsumers = true
continue
} else {
@@ -188,6 +215,23 @@ func (k Keeper) validateRestakedFPs(ctx context.Context, fpBTCPKs []bbn.BIP340Pu
return restakedToConsumers, nil
}

// restakedFPConsumerIDs returns the consumer IDs of non-Babylon finality providers
func (k Keeper) restakedFPConsumerIDs(ctx context.Context, fpBTCPKs []bbn.BIP340PubKey) ([]string, error) {
var consumerIDs []string

for _, fpBTCPK := range fpBTCPKs {
if _, err := k.GetFinalityProvider(ctx, fpBTCPK); err == nil {
continue
} else if consumerID, err := k.bscKeeper.GetConsumerOfFinalityProvider(ctx, &fpBTCPK); err == nil {
consumerIDs = append(consumerIDs, consumerID)
} else {
return nil, types.ErrFpNotFound.Wrapf("finality provider pk %s is not found", fpBTCPK.MarshalHex())
}
}

return consumerIDs, nil
}

// GetBTCDelegation gets the BTC delegation with a given staking tx hash
func (k Keeper) GetBTCDelegation(ctx context.Context, stakingTxHashStr string) (*types.BTCDelegation, error) {
// decode staking tx hash string
Loading