Skip to content

Commit

Permalink
chore: update to BTC multisig delegation (#167)
Browse files Browse the repository at this point in the history
* chore: update to BTC multisig delegation

* fix: addr staker bbn in WatchStaking and PoP BTC

* chore: add log for e2e test run '-'

* fix: use staker address the same as the signer of the msg

* chore: remove debug log

* chore: removed unused args in docker

* chore: add comment for Key of bbn conf

* chore: removed signer from delegationDataToMsg

* chore: update key bbnconfig description

* chore: add validation check for bbn address be the same in params and in config

* chore: update babylon version to latest dev with btc multisig
  • Loading branch information
RafilxTenfen authored Jun 19, 2024
1 parent f326479 commit d88515b
Show file tree
Hide file tree
Showing 20 changed files with 234 additions and 308 deletions.
10 changes: 1 addition & 9 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,11 +1,5 @@
FROM golang:1.21 as builder

# Version to build. Default is the Git HEAD.
ARG VERSION="HEAD"
ARG LEDGER_ENABLED="false"
ARG COSMOS_BUILD_OPTIONS=""


# Install cli tools for building and final image
RUN apt-get update && apt-get install -y make git bash gcc curl jq

Expand All @@ -20,16 +14,14 @@ COPY ./ /go/src/github.com/babylonchain/btc-staker/

RUN BUILD_TAGS=netgo \
LDFLAGS="-w -s" \
make build --trace
make build

# FINAL IMAGE
FROM debian:bookworm-slim AS run

RUN addgroup --gid 1138 --system btcstaker && adduser --uid 1138 --system --home /home/btcstaker btcstaker

RUN apt-get update && apt-get install -y bash curl jq wget


COPY --from=builder /go/src/github.com/babylonchain/btc-staker/go.mod /tmp
RUN WASMVM_VERSION=$(grep github.com/CosmWasm/wasmvm /tmp/go.mod | cut -d' ' -f2) && \
wget https://github.com/CosmWasm/wasmvm/releases/download/$WASMVM_VERSION/libwasmvm.$(uname -m).so \
Expand Down
19 changes: 7 additions & 12 deletions babylonclient/babyloncontroller.go
Original file line number Diff line number Diff line change
Expand Up @@ -273,7 +273,7 @@ type DelegationData struct {
FinalityProvidersBtcPks []*btcec.PublicKey
SlashingTransaction *wire.MsgTx
SlashingTransactionSig *schnorr.Signature
BabylonPk *secp256k1.PubKey
BabylonStakerAddr sdk.AccAddress
StakerBtcPk *btcec.PublicKey
BabylonPop *stakerdb.ProofOfPossession
Ud *UndelegationData
Expand Down Expand Up @@ -308,7 +308,7 @@ type DelegationInfo struct {
UndelegationInfo *UndelegationInfo
}

func delegationDataToMsg(signer string, dg *DelegationData) (*btcstypes.MsgCreateBTCDelegation, error) {
func delegationDataToMsg(dg *DelegationData) (*btcstypes.MsgCreateBTCDelegation, error) {
if dg == nil {
return nil, fmt.Errorf("nil delegation data")
}
Expand Down Expand Up @@ -365,13 +365,11 @@ func delegationDataToMsg(signer string, dg *DelegationData) (*btcstypes.MsgCreat
slashUnbondingTxSig := bbntypes.NewBIP340SignatureFromBTCSig(dg.Ud.SlashUnbondingTransactionSig)

return &btcstypes.MsgCreateBTCDelegation{
Signer: signer,
BabylonPk: dg.BabylonPk,
Pop: &btcstypes.ProofOfPossession{
// Note: this should be always safe conversion as we received data from our db
// Note: this should be always safe conversion as we received data from our db
StakerAddr: dg.BabylonStakerAddr.String(),
Pop: &btcstypes.ProofOfPossessionBTC{
BtcSigType: btcstypes.BTCSigType(dg.BabylonPop.BtcSigType),
BabylonSig: dg.BabylonPop.BabylonSigOverBtcPk,
BtcSig: dg.BabylonPop.BtcSigOverBabylonSig,
BtcSig: dg.BabylonPop.BtcSigOverBabylonAddr,
},
BtcPk: bbntypes.NewBIP340PubKeyFromBTCPK(dg.StakerBtcPk),
FpBtcPkList: fpPksList,
Expand Down Expand Up @@ -408,10 +406,7 @@ func (bc *BabylonController) reliablySendMsgs(
// TODO: for now return sdk.TxResponse, it will ease up debugging/testing
// ultimately we should create our own type ate
func (bc *BabylonController) Delegate(dg *DelegationData) (*pv.RelayerTxResponse, error) {
signer := bc.getTxSigner()

delegateMsg, err := delegationDataToMsg(signer, dg)

delegateMsg, err := delegationDataToMsg(dg)
if err != nil {
return nil, err
}
Expand Down
3 changes: 1 addition & 2 deletions babylonclient/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,8 +98,7 @@ func (m *MockBabylonClient) GetPubKey() *secp256k1.PubKey {
}

func (m *MockBabylonClient) Delegate(dg *DelegationData) (*pv.RelayerTxResponse, error) {
msg, err := delegationDataToMsg("signer", dg)

msg, err := delegationDataToMsg(dg)
if err != nil {
return nil, err
}
Expand Down
28 changes: 12 additions & 16 deletions babylonclient/pop.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
btcstypes "github.com/babylonchain/babylon/x/btcstaking/types"
"github.com/btcsuite/btcd/btcec/v2"
"github.com/btcsuite/btcd/chaincfg"
"github.com/cosmos/cosmos-sdk/crypto/keys/secp256k1"
sdk "github.com/cosmos/cosmos-sdk/types"
)

type BabylonBtcPopType int
Expand All @@ -19,20 +19,18 @@ const (
)

type BabylonPop struct {
popType BabylonBtcPopType
BabylonEcdsaSigOverBtcPk []byte
BtcSig []byte
popType BabylonBtcPopType
BtcSig []byte
}

func NewBabylonPop(t BabylonBtcPopType, babylonSig []byte, btcSig []byte) (*BabylonPop, error) {
if len(babylonSig) == 0 || len(btcSig) == 0 {
func NewBabylonPop(t BabylonBtcPopType, btcSigOverBbnAddr []byte) (*BabylonPop, error) {
if len(btcSigOverBbnAddr) == 0 {
return nil, fmt.Errorf("cannot create BabylonPop with empty signatures")
}

return &BabylonPop{
popType: t,
BabylonEcdsaSigOverBtcPk: babylonSig,
BtcSig: btcSig,
popType: t,
BtcSig: btcSigOverBbnAddr,
}, nil
}

Expand Down Expand Up @@ -66,26 +64,25 @@ func (pop *BabylonPop) PopTypeNum() uint32 {
return uint32(pop.popType)
}

func (pop *BabylonPop) ToBtcStakingPop() (*btcstypes.ProofOfPossession, error) {
func (pop *BabylonPop) ToBtcStakingPop() (*btcstypes.ProofOfPossessionBTC, error) {
popType, err := NewBTCSigType(pop.popType)

if err != nil {
return nil, err
}

return &btcstypes.ProofOfPossession{
return &btcstypes.ProofOfPossessionBTC{
BtcSigType: popType,
BabylonSig: pop.BabylonEcdsaSigOverBtcPk,
BtcSig: pop.BtcSig,
}, nil
}

func (pop *BabylonPop) ValidatePop(
babylonPk *secp256k1.PubKey,
bbnAddr sdk.AccAddress,
btcPk *btcec.PublicKey,
net *chaincfg.Params,
) error {
if babylonPk == nil || btcPk == nil || net == nil {
if btcPk == nil || net == nil {
return fmt.Errorf("cannot validate pop with nil parameters")
}

Expand All @@ -96,9 +93,8 @@ func (pop *BabylonPop) ValidatePop(
}

btcPkBabylonFormat := bbn.NewBIP340PubKeyFromBTCPK(btcPk)

return bPop.Verify(
babylonPk,
bbnAddr,
btcPkBabylonFormat,
net,
)
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ require (
cosmossdk.io/errors v1.0.1
cosmossdk.io/math v1.3.0
github.com/avast/retry-go/v4 v4.5.1
github.com/babylonchain/babylon v0.8.6-0.20240610115448-58c28046b75d
github.com/babylonchain/babylon v0.8.6-0.20240619103849-013f733e9537
github.com/btcsuite/btcd v0.24.0
github.com/btcsuite/btcd/btcec/v2 v2.3.2
github.com/btcsuite/btcd/btcutil v1.1.5
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -285,8 +285,8 @@ github.com/aws/aws-sdk-go v1.44.122/go.mod h1:y4AeaBuwd2Lk+GepC1E9v0qOiTws0MIWAX
github.com/aws/aws-sdk-go v1.44.312 h1:llrElfzeqG/YOLFFKjg1xNpZCFJ2xraIi3PqSuP+95k=
github.com/aws/aws-sdk-go v1.44.312/go.mod h1:aVsgQcEevwlmQ7qHE9I3h+dtQgpqhFB+i8Phjh7fkwI=
github.com/aws/aws-sdk-go-v2 v0.18.0/go.mod h1:JWVYvqSMppoMJC0x5wdwiImzgXTI9FuZwxzkQq9wy+g=
github.com/babylonchain/babylon v0.8.6-0.20240610115448-58c28046b75d h1:hpzxnNRLnJlM6oadyuCwwyIMczC0DN5lz6gp4PE8sQo=
github.com/babylonchain/babylon v0.8.6-0.20240610115448-58c28046b75d/go.mod h1:YFALTW+Kp/b5jSDoA7Z70RggJjAedlmQTrpdeU8c3hY=
github.com/babylonchain/babylon v0.8.6-0.20240619103849-013f733e9537 h1:jbUK7ooiB8Syfrn3bFxueCS3pxbA8ezx3hG+OaEEkrE=
github.com/babylonchain/babylon v0.8.6-0.20240619103849-013f733e9537/go.mod h1:YFALTW+Kp/b5jSDoA7Z70RggJjAedlmQTrpdeU8c3hY=
github.com/benbjohnson/clock v1.1.0/go.mod h1:J11/hYXuz8f4ySSvYwY0FKfm+ezbsZBKZxNJlLklBHA=
github.com/beorn7/perks v0.0.0-20180321164747-3a771d992973/go.mod h1:Dwedo/Wpr24TaqPxmxbtue+5NUziq4I4S80YR8gNf3Q=
github.com/beorn7/perks v1.0.0/go.mod h1:KWe93zE9D1o94FZ5RNwFwVgaQK1VOXiVxmqh+CedLV8=
Expand Down
37 changes: 16 additions & 21 deletions itest/e2e_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ import (
"github.com/btcsuite/btcd/txscript"
"github.com/btcsuite/btcd/wire"
"github.com/cosmos/cosmos-sdk/crypto/keys/secp256k1"
sdk "github.com/cosmos/cosmos-sdk/types"
sdkquerytypes "github.com/cosmos/cosmos-sdk/types/query"
sttypes "github.com/cosmos/cosmos-sdk/x/staking/types"
"github.com/lightningnetwork/lnd/kvdb"
Expand Down Expand Up @@ -150,8 +151,7 @@ type TestManager struct {

type testStakingData struct {
StakerKey *btcec.PublicKey
StakerBabylonPrivKey *secp256k1.PrivKey
StakerBabylonPubKey *secp256k1.PubKey
StakerBabylonAddr sdk.AccAddress
FinalityProviderBabylonPrivKeys []*secp256k1.PrivKey
FinalityProviderBabylonPublicKeys []*secp256k1.PubKey
FinalityProviderBtcPrivKeys []*btcec.PrivateKey
Expand Down Expand Up @@ -182,13 +182,11 @@ func (tm *TestManager) getTestStakingData(
fpBBNPKs = append(fpBBNPKs, fpBBNPK)
}

stakerBabylonPrivKey := secp256k1.GenPrivKey()
stakerBabylonPubKey := stakerBabylonPrivKey.PubKey().(*secp256k1.PubKey)

return &testStakingData{
StakerKey: stakerKey,
StakerBabylonPrivKey: stakerBabylonPrivKey,
StakerBabylonPubKey: stakerBabylonPubKey,
StakerKey: stakerKey,
// the staker babylon addr needs to be the same one that is going to sign
// the transaction in the end
StakerBabylonAddr: tm.BabylonClient.GetKeyAddress(),
FinalityProviderBabylonPrivKeys: fpBBNSKs,
FinalityProviderBabylonPublicKeys: fpBBNPKs,
FinalityProviderBtcPrivKeys: fpBTCSKs,
Expand Down Expand Up @@ -604,6 +602,7 @@ func (tm *TestManager) createAndRegisterFinalityProviders(t *testing.T, testStak
},
pop,
)
require.NoError(t, err)

require.Eventually(t, func() bool {
resp2, err := tm.BabylonClient.QueryFinalityProviders(100, 0)
Expand Down Expand Up @@ -648,7 +647,7 @@ func (tm *TestManager) mineBlock(t *testing.T) *wire.MsgBlock {
return header
}

func (tm *TestManager) sendStakingTx(t *testing.T, testStakingData *testStakingData) *chainhash.Hash {
func (tm *TestManager) sendStakingTxBTC(t *testing.T, testStakingData *testStakingData) *chainhash.Hash {
fpBTCPKs := []string{}
for i := 0; i < testStakingData.GetNumRestakedFPs(); i++ {
fpBTCPK := hex.EncodeToString(schnorr.SerializePubKey(testStakingData.FinalityProviderBtcKeys[i]))
Expand Down Expand Up @@ -740,7 +739,6 @@ func (tm *TestManager) sendWatchedStakingTx(
btcutil.Amount(testStakingData.StakingAmount),
regtestParams,
)

require.NoError(t, err)

err = tm.Sa.Wallet().UnlockWallet(20)
Expand Down Expand Up @@ -787,7 +785,6 @@ func (tm *TestManager) sendWatchedStakingTx(
require.NoError(t, err)

stakingTxSlashingPathInfo, err := stakingInfo.SlashingPathSpendInfo()

require.NoError(t, err)

slashSig, err := staking.SignTxWithOneScriptSpendInputFromScript(
Expand All @@ -796,7 +793,6 @@ func (tm *TestManager) sendWatchedStakingTx(
tm.WalletPrivKey,
stakingTxSlashingPathInfo.RevealedLeaf.Script,
)

require.NoError(t, err)

serializedStakingTx, err := utils.SerializeBtcTransaction(tx)
Expand Down Expand Up @@ -851,8 +847,8 @@ func (tm *TestManager) sendWatchedStakingTx(

// TODO: Update pop when new version will be ready, for now using schnorr as we don't have
// easy way to generate bip322 sig on backend side
pop, err := btcstypes.NewPoP(
testStakingData.StakerBabylonPrivKey,
pop, err := btcstypes.NewPoPBTC(
testStakingData.StakerBabylonAddr,
tm.WalletPrivKey,
)
require.NoError(t, err)
Expand All @@ -871,9 +867,8 @@ func (tm *TestManager) sendWatchedStakingTx(
fpBTCPKs,
hex.EncodeToString(serializedSlashingTx),
hex.EncodeToString(slashSig.Serialize()),
hex.EncodeToString(testStakingData.StakerBabylonPubKey.Key),
testStakingData.StakerBabylonAddr.String(),
tm.MinerAddr.String(),
hex.EncodeToString(pop.BabylonSig),
hex.EncodeToString(pop.BtcSig),
hex.EncodeToString(serializedUnbondingTx),
hex.EncodeToString(serializedSlashUnbondingTx),
Expand Down Expand Up @@ -1113,7 +1108,7 @@ func TestSendingStakingTransaction(t *testing.T) {

tm.createAndRegisterFinalityProviders(t, testStakingData)

txHash := tm.sendStakingTx(t, testStakingData)
txHash := tm.sendStakingTxBTC(t, testStakingData)

go tm.mineNEmptyBlocks(t, params.ConfirmationTimeBlocks, true)
tm.waitForStakingTxState(t, txHash, proto.TransactionState_SENT_TO_BABYLON)
Expand Down Expand Up @@ -1265,7 +1260,7 @@ func TestRestartingTxNotDeepEnough(t *testing.T) {
testStakingData := tm.getTestStakingData(t, tm.WalletPrivKey.PubKey(), stakingTime, 10000, 1)

tm.createAndRegisterFinalityProviders(t, testStakingData)
txHash := tm.sendStakingTx(t, testStakingData)
txHash := tm.sendStakingTxBTC(t, testStakingData)

// restart app when tx is not deep enough
tm.RestartApp(t)
Expand Down Expand Up @@ -1334,7 +1329,7 @@ func TestStakingUnbonding(t *testing.T) {

tm.createAndRegisterFinalityProviders(t, testStakingData)

txHash := tm.sendStakingTx(t, testStakingData)
txHash := tm.sendStakingTxBTC(t, testStakingData)

go tm.mineNEmptyBlocks(t, params.ConfirmationTimeBlocks, true)
tm.waitForStakingTxState(t, txHash, proto.TransactionState_SENT_TO_BABYLON)
Expand Down Expand Up @@ -1405,7 +1400,7 @@ func TestUnbondingRestartWaitingForSignatures(t *testing.T) {

tm.createAndRegisterFinalityProviders(t, testStakingData)

txHash := tm.sendStakingTx(t, testStakingData)
txHash := tm.sendStakingTxBTC(t, testStakingData)

go tm.mineNEmptyBlocks(t, params.ConfirmationTimeBlocks, true)
tm.waitForStakingTxState(t, txHash, proto.TransactionState_SENT_TO_BABYLON)
Expand Down Expand Up @@ -1558,7 +1553,7 @@ func TestSendingStakingTransaction_Restaking(t *testing.T) {

tm.createAndRegisterFinalityProviders(t, testStakingData)

txHash := tm.sendStakingTx(t, testStakingData)
txHash := tm.sendStakingTxBTC(t, testStakingData)

go tm.mineNEmptyBlocks(t, params.ConfirmationTimeBlocks, true)
tm.waitForStakingTxState(t, txHash, proto.TransactionState_SENT_TO_BABYLON)
Expand Down
Loading

0 comments on commit d88515b

Please sign in to comment.