Skip to content

Commit

Permalink
btcstaking: accomodate message formats and handlers for new staking tx (
Browse files Browse the repository at this point in the history
  • Loading branch information
SebastianElvis authored Nov 23, 2023
1 parent 9cf49a9 commit c31e470
Show file tree
Hide file tree
Showing 40 changed files with 577 additions and 643 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ require (
cosmossdk.io/api v0.7.2
cosmossdk.io/client/v2 v2.0.0-beta.1
cosmossdk.io/core v0.11.0
cosmossdk.io/depinject v1.0.0-alpha.4
cosmossdk.io/errors v1.0.0
cosmossdk.io/log v1.2.1
cosmossdk.io/math v1.2.0
Expand Down Expand Up @@ -134,7 +135,6 @@ require (
cloud.google.com/go/iam v1.1.3 // indirect
cloud.google.com/go/storage v1.30.1 // indirect
cosmossdk.io/collections v0.4.0 // indirect
cosmossdk.io/depinject v1.0.0-alpha.4 // indirect
cosmossdk.io/x/nft v0.1.0 // indirect
github.com/Azure/go-ansiterm v0.0.0-20230124172434-306776ec8161 // indirect
github.com/DataDog/zstd v1.5.5 // indirect
Expand Down
23 changes: 11 additions & 12 deletions proto/babylon/btcstaking/v1/btcstaking.proto
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ message BTCDelegation {
// by the covenant (i.e., SK corresponding to covenant_pk in params)
// It will be a part of the witness for the staking tx output.
// TODO: change to a set of (covenant PK, covenant adaptor signature) tuples
// over each restaked BTC validator (i.e., a matrix of tuples)
bytes covenant_sig = 12 [ (gogoproto.customtype) = "github.com/babylonchain/babylon/types.BIP340Signature" ];

// if this object is present it menans that staker requested undelegation, and whole
Expand Down Expand Up @@ -121,14 +122,12 @@ message BTCUndelegation {
// covenant_slashing_sig is the signature on the slashing tx
// by the covenant (i.e., SK corresponding to covenant_pk in params)
// It must be provided after processing undelagate message by Babylon
// TODO: change to a set of (covenant PK, covenant adaptor signature) tuples
// TODO: change to a matrix of (covenant PK, covenant adaptor signature) tuples
bytes covenant_slashing_sig = 5 [ (gogoproto.customtype) = "github.com/babylonchain/babylon/types.BIP340Signature" ];
// covenant_unbonding_sig is the signature on the unbonding tx
// by the covenant (i.e., SK corresponding to covenant_pk in params)
// It must be provided after processing undelagate message by Babylon and after
// validator sig will be provided by validator
// TODO: change to a set of (covenant PK, covenant Schnorr signature) tuples
bytes covenant_unbonding_sig = 6 [ (gogoproto.customtype) = "github.com/babylonchain/babylon/types.BIP340Signature" ];
// covenant_unbonding_sig_list is the list of signatures on the unbonding tx
// by covenant members
// It must be provided after processing undelagate message by Babylon
repeated SignatureInfo covenant_unbonding_sig_list = 6;
}


Expand All @@ -139,11 +138,11 @@ message BTCUndelegationInfo {
// than staking output.
bytes unbonding_tx = 1;

// covenant_unbonding_sig is the signature on the unbonding tx
// by the covenant (i.e., SK corresponding to covenant_pk in params)
// it will be nil if covenant didn't sign it yet
// TODO: change to a set of (covenant PK, covenant Schnorr signature) tuples
bytes covenant_unbonding_sig = 2 [ (gogoproto.customtype) = "github.com/babylonchain/babylon/types.BIP340Signature" ];
// covenant_unbonding_sig_list is the list of signatures on the unbonding tx
// by covenant members
// It must be provided after processing undelagate message by Babylon and after
// validator sig will be provided by validator
repeated SignatureInfo covenant_unbonding_sig_list = 2;
}

// BTCDelegatorDelegations is a collection of BTC delegations from the same delegator.
Expand Down
35 changes: 14 additions & 21 deletions proto/babylon/btcstaking/v1/tx.proto
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,8 @@ message MsgCreateBTCDelegation {
cosmos.crypto.secp256k1.PubKey babylon_pk = 2;
// pop is the proof of possession of babylon_pk and btc_pk
ProofOfPossession pop = 3;
// staker_btc_pk is the Bitcoin secp256k1 PK of the BTC staker
bytes staker_btc_pk = 4 [ (gogoproto.customtype) = "github.com/babylonchain/babylon/types.BIP340PubKey" ];
// btc_pk is the Bitcoin secp256k1 PK of the BTC delegator
bytes btc_pk = 4 [ (gogoproto.customtype) = "github.com/babylonchain/babylon/types.BIP340PubKey" ];
// val_btc_pk_list is the list of Bitcoin secp256k1 PKs of the BTC validators, if there is more than one
// validator pk it means that delegation is re-staked
repeated bytes val_btc_pk_list = 5 [ (gogoproto.customtype) = "github.com/babylonchain/babylon/types.BIP340PubKey" ];
Expand Down Expand Up @@ -116,18 +116,15 @@ message MsgAddCovenantSig {
option (cosmos.msg.v1.signer) = "signer";

string signer = 1;
// val_pk is the Bitcoin secp256k1 PK of the BTC validator
// the PK follows encoding in BIP-340 spec
bytes val_pk = 2 [ (gogoproto.customtype) = "github.com/babylonchain/babylon/types.BIP340PubKey" ];
// del_pk is the Bitcoin secp256k1 PK of the BTC delegation
// the PK follows encoding in BIP-340 spec
bytes del_pk = 3 [ (gogoproto.customtype) = "github.com/babylonchain/babylon/types.BIP340PubKey" ];
// pk is the BTC public key of the covenant member
bytes pk = 2 [ (gogoproto.customtype) = "github.com/babylonchain/babylon/types.BIP340PubKey" ];
// staking_tx_hash is the hash of the staking tx.
// (val_pk, del_pk, staking_tx_hash) uniquely identifies a BTC delegation
string staking_tx_hash = 4;
// It uniquely identifies a BTC delegation
string staking_tx_hash = 3;
// sig is the signature of the covenant
// the signature follows encoding in BIP-340 spec
bytes sig = 5 [ (gogoproto.customtype) = "github.com/babylonchain/babylon/types.BIP340Signature" ];
// TODO: change to adaptor signature
bytes sig = 4 [ (gogoproto.customtype) = "github.com/babylonchain/babylon/types.BIP340Signature" ];
}
// MsgAddCovenantSigResponse is the response for MsgAddCovenantSig
message MsgAddCovenantSigResponse {}
Expand Down Expand Up @@ -156,22 +153,18 @@ message MsgAddCovenantUnbondingSigs {
option (cosmos.msg.v1.signer) = "signer";

string signer = 1;
// val_pk is the Bitcoin secp256k1 PK of the BTC validator
// the PK follows encoding in BIP-340 spec
bytes val_pk = 2 [ (gogoproto.customtype) = "github.com/babylonchain/babylon/types.BIP340PubKey" ];
// del_pk is the Bitcoin secp256k1 PK of the BTC delegation
// the PK follows encoding in BIP-340 spec
bytes del_pk = 3 [ (gogoproto.customtype) = "github.com/babylonchain/babylon/types.BIP340PubKey" ];
// pk is the BTC public key of the covenant member
bytes pk = 2 [ (gogoproto.customtype) = "github.com/babylonchain/babylon/types.BIP340PubKey" ];
// staking_tx_hash is the hash of the staking tx.
// (val_pk, del_pk, staking_tx_hash) uniquely identifies a BTC delegation
string staking_tx_hash = 4;
string staking_tx_hash = 3;
// unbonding_tx_sig is the signature of the covenant on the unbonding tx submitted to babylon
// the signature follows encoding in BIP-340 spec
bytes unbonding_tx_sig = 5 [ (gogoproto.customtype) = "github.com/babylonchain/babylon/types.BIP340Signature" ];
bytes unbonding_tx_sig = 4 [ (gogoproto.customtype) = "github.com/babylonchain/babylon/types.BIP340Signature" ];
// slashing_unbonding_tx_sig is the signature of the covenant on slashing tx corresponding to unbodning tx submitted to babylon
// the signature follows encoding in BIP-340 spec
bytes slashing_unbonding_tx_sig = 6 [ (gogoproto.customtype) = "github.com/babylonchain/babylon/types.BIP340Signature" ];

// TODO: change to adaptor signature
bytes slashing_unbonding_tx_sig = 5 [ (gogoproto.customtype) = "github.com/babylonchain/babylon/types.BIP340Signature" ];
}
// MsgAddCovenantSigResponse is the response for MsgAddCovenantSig
message MsgAddCovenantUnbondingSigsResponse {}
2 changes: 1 addition & 1 deletion proto/babylon/checkpointing/v1/checkpoint.proto
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ message BlsSig {

// epoch_num defines the epoch number that the BLS sig is signed on
uint64 epoch_num = 1;
// last_commit_hash defines the 'AppHash' that the BLS sig is signed on
// app_hash defines the 'AppHash' that the BLS sig is signed on
bytes app_hash = 2 [ (gogoproto.customtype) = "AppHash" ];
bytes bls_sig = 3
[ (gogoproto.customtype) =
Expand Down
2 changes: 1 addition & 1 deletion proto/babylon/epoching/v1/epoching.proto
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ message Epoch {
// It will be used for proving a block is in an epoch
bytes app_hash_root = 5;
// sealer_header is the 2nd header of the next epoch
// This validator set has generated a BLS multisig on `last_commit_hash` of
// This validator set has generated a BLS multisig on `app_hash` of
// the sealer header
tendermint.types.Header sealer_header = 6;
}
Expand Down
12 changes: 6 additions & 6 deletions proto/babylon/finality/v1/finality.proto
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ import "gogoproto/gogo.proto";
message IndexedBlock {
// height is the height of the block
uint64 height = 1;
// last_commit_hash is the last_commit_hash of the block
bytes last_commit_hash = 2;
// app_hash is the AppHash of the block
bytes app_hash = 2;
// finalized indicates whether the IndexedBlock is finalised by 2/3
// BTC validators or not
bool finalized = 3;
Expand All @@ -25,10 +25,10 @@ message Evidence {
uint64 block_height = 2;
// pub_rand is the public randomness the BTC validator has committed to
bytes pub_rand = 3 [ (gogoproto.customtype) = "github.com/babylonchain/babylon/types.SchnorrPubRand" ];
// canonical_last_commit_hash is the last_commit_hash of the canonical block
bytes canonical_last_commit_hash = 4;
// fork_last_commit_hash is the last_commit_hash of the fork block
bytes fork_last_commit_hash = 5;
// canonical_app_hash is the AppHash of the canonical block
bytes canonical_app_hash = 4;
// fork_app_hash is the AppHash of the fork block
bytes fork_app_hash = 5;
// canonical_finality_sig is the finality signature to the canonical block
// where finality signature is an EOTS signature, i.e.,
// the `s` in a Schnorr signature `(r, s)`
Expand Down
4 changes: 2 additions & 2 deletions proto/babylon/finality/v1/tx.proto
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ message MsgAddFinalitySig {
bytes val_btc_pk = 2 [ (gogoproto.customtype) = "github.com/babylonchain/babylon/types.BIP340PubKey" ];
// block_height is the height of the voted block
uint64 block_height = 3;
// block_last_commit_hash is the last_commit_hash of the voted block
bytes block_last_commit_hash = 4;
// block_app_hash is the AppHash of the voted block
bytes block_app_hash = 4;
// finality_sig is the finality signature to this block
// where finality signature is an EOTS signature, i.e.,
// the `s` in a Schnorr signature `(r, s)`
Expand Down
6 changes: 3 additions & 3 deletions proto/babylon/zoneconcierge/v1/zoneconcierge.proto
Original file line number Diff line number Diff line change
Expand Up @@ -94,14 +94,14 @@ message FinalizedChainInfo {
// - Metadata of this epoch, which includes the sealer header
// - Raw checkpoint of this epoch
// The verifier can perform the following verification rules:
// - The raw checkpoint's `last_commit_hash` is same as in the sealer header
// - The raw checkpoint's `app_hash` is same as in the sealer header
// - More than 1/3 (in voting power) validators in the validator set of this
// epoch have signed `last_commit_hash` of the sealer header
// epoch have signed `app_hash` of the sealer header
// - The epoch medatata is committed to the `app_hash` of the sealer header
// - The validator set is committed to the `app_hash` of the sealer header
message ProofEpochSealed {
// validator_set is the validator set of the sealed epoch
// This validator set has generated a BLS multisig on `last_commit_hash` of
// This validator set has generated a BLS multisig on `app_hash` of
// the sealer header
repeated babylon.checkpointing.v1.ValidatorWithBlsKey validator_set = 1;
// proof_epoch_info is the Merkle proof that the epoch's metadata is committed
Expand Down
15 changes: 7 additions & 8 deletions test/e2e/btc_staking_e2e_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,7 @@ func (s *BTCStakingTestSuite) Test2SubmitCovenantSignature() {
net,
)
s.NoError(err)
nonValidatorNode.AddCovenantSig(btcVal.BtcPk, bbn.NewBIP340PubKeyFromBTCPK(delBTCPK), stakingTxHash, covenantSig)
nonValidatorNode.AddCovenantSig(&params.CovenantPks[0], stakingTxHash, covenantSig)

// wait for a block so that above txs take effect
nonValidatorNode.WaitForNextBlock()
Expand All @@ -264,8 +264,8 @@ func (s *BTCStakingTestSuite) Test2SubmitCovenantSignature() {
s.NoError(err)
activeBTCVals := nonValidatorNode.QueryActiveBTCValidatorsAtHeight(activatedHeight)
s.Len(activeBTCVals, 1)
s.Equal(activeBTCVals[0].VotingPower, activeDels.VotingPower(currentBtcTip.Height, initialization.BabylonBtcFinalizationPeriod))
s.Equal(activeBTCVals[0].VotingPower, activeDel.VotingPower(currentBtcTip.Height, initialization.BabylonBtcFinalizationPeriod))
s.Equal(activeBTCVals[0].VotingPower, activeDels.VotingPower(currentBtcTip.Height, initialization.BabylonBtcFinalizationPeriod, params.CovenantQuorum))
s.Equal(activeBTCVals[0].VotingPower, activeDel.VotingPower(currentBtcTip.Height, initialization.BabylonBtcFinalizationPeriod, params.CovenantQuorum))
}

// Test2CommitPublicRandomnessAndSubmitFinalitySignature is an end-to-end
Expand Down Expand Up @@ -519,7 +519,7 @@ func (s *BTCStakingTestSuite) Test6SubmitUnbondingSignatures() {
delegation := delegatorDelegations.Dels[0]

s.NotNil(delegation.BtcUndelegation)
s.Nil(delegation.BtcUndelegation.CovenantUnbondingSig)
s.Empty(delegation.BtcUndelegation.CovenantUnbondingSigList)
s.Nil(delegation.BtcUndelegation.CovenantSlashingSig)

// params for covenantPk and slashing address
Expand Down Expand Up @@ -593,8 +593,7 @@ func (s *BTCStakingTestSuite) Test6SubmitUnbondingSignatures() {
)
s.NoError(err)
nonValidatorNode.AddCovenantUnbondingSigs(
btcVal.BtcPk,
bbn.NewBIP340PubKeyFromBTCPK(delBTCPK), stakingTxHash, &covenantUnbondingSig, covenantSlashingSig)
&params.CovenantPks[0], stakingTxHash, &covenantUnbondingSig, covenantSlashingSig)
nonValidatorNode.WaitForNextBlock()
nonValidatorNode.WaitForNextBlock()

Expand All @@ -603,12 +602,12 @@ func (s *BTCStakingTestSuite) Test6SubmitUnbondingSignatures() {
s.Len(allDelegationsWithSigs, 1)
delegationWithSigs := allDelegationsWithSigs[0].Dels[0]
s.NotNil(delegationWithSigs.BtcUndelegation)
s.NotNil(delegationWithSigs.BtcUndelegation.CovenantUnbondingSig)
s.NotEmpty(delegationWithSigs.BtcUndelegation.CovenantUnbondingSigList)
s.NotNil(delegationWithSigs.BtcUndelegation.CovenantSlashingSig)
btcTip, err := nonValidatorNode.QueryTip()
s.NoError(err)
s.Equal(
bstypes.BTCDelegationStatus_UNBONDED,
delegationWithSigs.GetStatus(btcTip.Height, initialization.BabylonBtcFinalizationPeriod),
delegationWithSigs.GetStatus(btcTip.Height, initialization.BabylonBtcFinalizationPeriod, params.CovenantQuorum),
)
}
18 changes: 8 additions & 10 deletions test/e2e/configurer/chain/commands_btcstaking.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@ package chain

import (
"encoding/hex"
"strconv"

"github.com/btcsuite/btcd/btcutil"
"github.com/btcsuite/btcd/wire"
"strconv"

"cosmossdk.io/math"
sdkmath "cosmossdk.io/math"
Expand Down Expand Up @@ -80,14 +81,13 @@ func (n *NodeConfig) CreateBTCDelegation(
n.LogActionF("successfully created BTC delegation")
}

func (n *NodeConfig) AddCovenantSig(valPK *bbn.BIP340PubKey, delPK *bbn.BIP340PubKey, stakingTxHash string, sig *bbn.BIP340Signature) {
func (n *NodeConfig) AddCovenantSig(covPK *bbn.BIP340PubKey, stakingTxHash string, sig *bbn.BIP340Signature) {
n.LogActionF("adding covenant signature")

valPKHex := valPK.MarshalHex()
delPKHex := delPK.MarshalHex()
covPKHex := covPK.MarshalHex()
sigHex := sig.ToHexStr()

cmd := []string{"babylond", "tx", "btcstaking", "add-covenant-sig", valPKHex, delPKHex, stakingTxHash, sigHex, "--from=val"}
cmd := []string{"babylond", "tx", "btcstaking", "add-covenant-sig", covPKHex, stakingTxHash, sigHex, "--from=val"}
_, _, err := n.containerManager.ExecTxCmd(n.t, n.chainId, n.Name, cmd)
require.NoError(n.t, err)
n.LogActionF("successfully added covenant sig")
Expand Down Expand Up @@ -181,19 +181,17 @@ func (n *NodeConfig) AddValidatorUnbondingSig(valPK *bbn.BIP340PubKey, delPK *bb
}

func (n *NodeConfig) AddCovenantUnbondingSigs(
valPK *bbn.BIP340PubKey,
delPK *bbn.BIP340PubKey,
covPK *bbn.BIP340PubKey,
stakingTxHash string,
unbondingTxSig *bbn.BIP340Signature,
slashUnbondingTxSig *bbn.BIP340Signature) {
n.LogActionF("adding validator signature")

valPKHex := valPK.MarshalHex()
delPKHex := delPK.MarshalHex()
covPKHex := covPK.MarshalHex()
unbondingTxSigHex := unbondingTxSig.ToHexStr()
slashUnbondingTxSigHex := slashUnbondingTxSig.ToHexStr()

cmd := []string{"babylond", "tx", "btcstaking", "add-covenant-unbonding-sigs", valPKHex, delPKHex, stakingTxHash, unbondingTxSigHex, slashUnbondingTxSigHex, "--from=val"}
cmd := []string{"babylond", "tx", "btcstaking", "add-covenant-unbonding-sigs", covPKHex, stakingTxHash, unbondingTxSigHex, slashUnbondingTxSigHex, "--from=val"}
_, _, err := n.containerManager.ExecTxCmd(n.t, n.chainId, n.Name, cmd)
require.NoError(n.t, err)
n.LogActionF("successfully added covenant unbonding sigs")
Expand Down
Loading

0 comments on commit c31e470

Please sign in to comment.