Skip to content

Commit 82191df

Browse files
feat: Add support for stake expansion (#127)
* chore: initial add btc stk exp sig * chore: add stk exp to the parsed sig request * fix: add missing funding output to stake expansion * chore: add TODO verifier * chore: add missing stk expansion fields * chore: add details for stk exp signing err * update based on latest changes * address TODO * update validations * fix build * add chlog * update tools pkg * update Dockerfiles go version * update test * fix bbn node startup * fix tst * update babylon dep to v3 snapshot * update babylon dep to v3 snapshot in tools * changes based on review comments * add basic validations * update bbn snapshot * update stake expansion sig * add e2e test * update bbn snapshot * go mod tidy * changes based on review comments * revert go mod change --------- Co-authored-by: tom <[email protected]>
1 parent ce0331d commit 82191df

33 files changed

+3116
-889
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/)
4040
### Improvements
4141

4242
* [#125](https://github.com/babylonlabs-io/covenant-emulator/pull/125) Update config defaults for gas adjustment and cov batch sigs.
43+
* [#127](https://github.com/babylonlabs-io/covenant-emulator/pull/127) Add Stake Expansion support
4344

4445
## v0.15.1
4546

Dockerfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
FROM golang:1.23 AS builder
1+
FROM golang:1.23.10 AS builder
22

33
# hadolint ignore=DL3008
44
RUN apt-get update && apt-get install --no-install-recommends -y ca-certificates make git bash gcc curl jq && rm -rf /var/lib/apt/lists/*

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
BUILDDIR ?= $(CURDIR)/build
22
TOOLS_DIR := tools
33

4-
BABYLON_PKG := github.com/babylonlabs-io/babylon/cmd/babylond
4+
BABYLON_PKG := github.com/babylonlabs-io/babylon/v3/cmd/babylond
55

66
GO_BIN := ${GOPATH}/bin
77

clientcontroller/babylon.go

Lines changed: 88 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,12 @@ import (
1010
"github.com/btcsuite/btcd/btcec/v2"
1111
stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types"
1212

13-
"github.com/babylonlabs-io/babylon/client/babylonclient"
14-
bbnclient "github.com/babylonlabs-io/babylon/client/client"
15-
bbntypes "github.com/babylonlabs-io/babylon/types"
16-
btcctypes "github.com/babylonlabs-io/babylon/x/btccheckpoint/types"
17-
btclctypes "github.com/babylonlabs-io/babylon/x/btclightclient/types"
18-
btcstakingtypes "github.com/babylonlabs-io/babylon/x/btcstaking/types"
13+
"github.com/babylonlabs-io/babylon/v3/client/babylonclient"
14+
bbnclient "github.com/babylonlabs-io/babylon/v3/client/client"
15+
bbntypes "github.com/babylonlabs-io/babylon/v3/types"
16+
btcctypes "github.com/babylonlabs-io/babylon/v3/x/btccheckpoint/types"
17+
btclctypes "github.com/babylonlabs-io/babylon/v3/x/btclightclient/types"
18+
btcstakingtypes "github.com/babylonlabs-io/babylon/v3/x/btcstaking/types"
1919
"github.com/btcsuite/btcd/btcutil"
2020
"github.com/btcsuite/btcd/chaincfg"
2121
sdkclient "github.com/cosmos/cosmos-sdk/client"
@@ -165,14 +165,22 @@ func (bc *BabylonController) SubmitCovenantSigs(covSigs []*types.CovenantSigs) (
165165
msgs := make([]sdk.Msg, 0, len(covSigs))
166166
for _, covSig := range covSigs {
167167
bip340UnbondingSig := bbntypes.NewBIP340SignatureFromBTCSig(covSig.UnbondingSig)
168-
msgs = append(msgs, &btcstakingtypes.MsgAddCovenantSigs{
168+
msg := &btcstakingtypes.MsgAddCovenantSigs{
169169
Signer: bc.mustGetTxSigner(),
170170
Pk: bbntypes.NewBIP340PubKeyFromBTCPK(covSig.PublicKey),
171171
StakingTxHash: covSig.StakingTxHash.String(),
172172
SlashingTxSigs: covSig.SlashingSigs,
173173
UnbondingTxSig: bip340UnbondingSig,
174174
SlashingUnbondingTxSigs: covSig.SlashingUnbondingSigs,
175-
})
175+
StakeExpansionTxSig: nil,
176+
}
177+
178+
if covSig.StkExpSig != nil {
179+
stkExpSig := bbntypes.NewBIP340SignatureFromBTCSig(covSig.StkExpSig)
180+
msg.StakeExpansionTxSig = stkExpSig
181+
}
182+
183+
msgs = append(msgs, msg)
176184
}
177185
res, err := bc.reliablySendMsgs(msgs)
178186
if err != nil {
@@ -198,6 +206,15 @@ func (bc *BabylonController) QueryVerifiedDelegations(limit uint64) ([]*types.De
198206
return bc.queryDelegationsWithStatus(btcstakingtypes.BTCDelegationStatus_VERIFIED, limit, nil)
199207
}
200208

209+
// QueryBTCDelegation queries the BTC delegation by the tx hash
210+
func (bc *BabylonController) QueryBTCDelegation(stakingTxHashHex string) (*types.Delegation, error) {
211+
resp, err := bc.bbnClient.QueryClient.BTCDelegation(stakingTxHashHex)
212+
if err != nil {
213+
return nil, fmt.Errorf("failed to query BTC delegation %s: %v", stakingTxHashHex, err)
214+
}
215+
return DelegationRespToDelegation(resp.BtcDelegation)
216+
}
217+
201218
// queryDelegationsWithStatus queries BTC delegations that need a Covenant signature
202219
// with the given status (either pending or unbonding)
203220
// it is only used when the program is running in Covenant mode
@@ -308,7 +325,7 @@ func DelegationRespToDelegation(del *btcstakingtypes.BTCDelegationResponse) (*ty
308325
return nil, fmt.Errorf("total sat (%d) is larger than the maximum int64", del.TotalSat)
309326
}
310327

311-
return &types.Delegation{
328+
respDel := &types.Delegation{
312329
BtcPk: del.BtcPk.MustToBTCPK(),
313330
FpBtcPks: fpBtcPks,
314331
TotalSat: btcutil.Amount(del.TotalSat),
@@ -322,7 +339,17 @@ func DelegationRespToDelegation(del *btcstakingtypes.BTCDelegationResponse) (*ty
322339
UnbondingTime: uint16(del.UnbondingTime),
323340
BtcUndelegation: undelegation,
324341
ParamsVersion: del.ParamsVersion,
325-
}, nil
342+
StakeExpansion: nil,
343+
}
344+
345+
if del.StkExp != nil {
346+
respDel.StakeExpansion = &types.DelegationStakeExpansion{
347+
PreviousStakingTxHashHex: del.StkExp.PreviousStakingTxHashHex,
348+
OtherFundingTxOutHex: del.StkExp.OtherFundingTxOutHex,
349+
}
350+
}
351+
352+
return respDel, nil
326353
}
327354

328355
func UndelegationRespToUndelegation(undel *btcstakingtypes.BTCUndelegationResponse) (*types.Undelegation, error) {
@@ -426,6 +453,57 @@ func (bc *BabylonController) CreateBTCDelegation(
426453
return &types.TxResponse{TxHash: res.TxHash}, nil
427454
}
428455

456+
// CreateStakeExpansionDelegation creates a BTC stake expansion delegation using MsgBtcStakeExpand
457+
// Currently this is only used for e2e tests, probably does not need to add it into the interface
458+
func (bc *BabylonController) CreateStakeExpansionDelegation(
459+
delBtcPk *bbntypes.BIP340PubKey,
460+
fpPks []*btcec.PublicKey,
461+
pop *btcstakingtypes.ProofOfPossessionBTC,
462+
stakingTime uint32,
463+
stakingValue int64,
464+
stakingTxInfo *btcctypes.TransactionInfo,
465+
slashingTx *btcstakingtypes.BTCSlashingTx,
466+
delSlashingSig *bbntypes.BIP340Signature,
467+
unbondingTx []byte,
468+
unbondingTime uint32,
469+
unbondingValue int64,
470+
unbondingSlashingTx *btcstakingtypes.BTCSlashingTx,
471+
delUnbondingSlashingSig *bbntypes.BIP340Signature,
472+
previousStakingTxHash string,
473+
fundingTx []byte,
474+
) (*types.TxResponse, error) {
475+
fpBtcPks := make([]bbntypes.BIP340PubKey, 0, len(fpPks))
476+
for _, v := range fpPks {
477+
fpBtcPks = append(fpBtcPks, *bbntypes.NewBIP340PubKeyFromBTCPK(v))
478+
}
479+
480+
msg := &btcstakingtypes.MsgBtcStakeExpand{
481+
StakerAddr: bc.mustGetTxSigner(),
482+
Pop: pop,
483+
BtcPk: delBtcPk,
484+
FpBtcPkList: fpBtcPks,
485+
StakingTime: stakingTime,
486+
StakingValue: stakingValue,
487+
StakingTx: stakingTxInfo.Transaction,
488+
SlashingTx: slashingTx,
489+
DelegatorSlashingSig: delSlashingSig,
490+
UnbondingTx: unbondingTx,
491+
UnbondingTime: unbondingTime,
492+
UnbondingValue: unbondingValue,
493+
UnbondingSlashingTx: unbondingSlashingTx,
494+
DelegatorUnbondingSlashingSig: delUnbondingSlashingSig,
495+
PreviousStakingTxHash: previousStakingTxHash,
496+
FundingTx: fundingTx,
497+
}
498+
499+
res, err := bc.reliablySendMsg(msg)
500+
if err != nil {
501+
return nil, err
502+
}
503+
504+
return &types.TxResponse{TxHash: res.TxHash}, nil
505+
}
506+
429507
// Register a finality provider to Babylon
430508
// Currently this is only used for e2e tests, probably does not need to add it into the interface
431509
func (bc *BabylonController) RegisterFinalityProvider(

clientcontroller/babylon_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@ import (
44
"math/rand"
55
"testing"
66

7-
"github.com/babylonlabs-io/babylon/testutil/datagen"
8-
bbntypes "github.com/babylonlabs-io/babylon/types"
9-
"github.com/babylonlabs-io/babylon/x/btcstaking/types"
7+
"github.com/babylonlabs-io/babylon/v3/testutil/datagen"
8+
bbntypes "github.com/babylonlabs-io/babylon/v3/types"
9+
"github.com/babylonlabs-io/babylon/v3/x/btcstaking/types"
1010
"github.com/babylonlabs-io/covenant-emulator/clientcontroller"
1111
"github.com/babylonlabs-io/covenant-emulator/testutil"
1212
"github.com/stretchr/testify/require"

clientcontroller/interface.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,9 @@ type (
2525
// QueryPendingDelegations queries BTC delegations that are in status of pending
2626
QueryPendingDelegations(limit uint64, filter FilterFn) ([]*types.Delegation, error)
2727

28+
// QueryBTCDelegation queries the BTC delegation by the tx hash
29+
QueryBTCDelegation(stakingTxHashHex string) (*types.Delegation, error)
30+
2831
QueryStakingParamsByVersion(version uint32) (*types.StakingParams, error)
2932

3033
Close() error

clientcontroller/retry_utils.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ package clientcontroller
22

33
import (
44
sdkErr "cosmossdk.io/errors"
5-
btcstakingtypes "github.com/babylonlabs-io/babylon/x/btcstaking/types"
5+
btcstakingtypes "github.com/babylonlabs-io/babylon/v3/x/btcstaking/types"
66
)
77

88
// these errors are considered unrecoverable because these indicate

cmd/covd/key.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import (
44
"encoding/json"
55
"fmt"
66

7-
"github.com/babylonlabs-io/babylon/types"
7+
"github.com/babylonlabs-io/babylon/v3/types"
88
"github.com/btcsuite/btcd/btcec/v2"
99
"github.com/jessevdk/go-flags"
1010

config/babylon.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ package config
33
import (
44
"time"
55

6-
bbncfg "github.com/babylonlabs-io/babylon/client/config"
6+
bbncfg "github.com/babylonlabs-io/babylon/v3/client/config"
77
)
88

99
type BBNConfig struct {

covenant-signer/Dockerfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
FROM golang:1.23.1-alpine AS builder
1+
FROM golang:1.23.10-alpine AS builder
22

33
# Use muslc for static libs
44
ARG BUILD_TAGS="muslc"

0 commit comments

Comments
 (0)