Skip to content

Commit

Permalink
feat: Covenant handles pre-signed unbonding txs (#180)
Browse files Browse the repository at this point in the history
  • Loading branch information
vitsalis authored Dec 14, 2023
1 parent b964419 commit f73962d
Show file tree
Hide file tree
Showing 14 changed files with 184 additions and 443 deletions.
1 change: 0 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,6 @@ $ make build
```

The above will lead to a build directory having the following structure:

```bash
$ ls build
├── eotsd
Expand Down
97 changes: 29 additions & 68 deletions clientcontroller/babylon.go
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ func (bc *BabylonController) CommitPubRandList(
ValBtcPk: bbntypes.NewBIP340PubKeyFromBTCPK(valPk),
StartHeight: startHeight,
PubRandList: schnorrPubRandList,
Sig: &bip340Sig,
Sig: bip340Sig,
}

res, err := bc.reliablySendMsg(msg)
Expand All @@ -216,40 +216,19 @@ func (bc *BabylonController) CommitPubRandList(
func (bc *BabylonController) SubmitCovenantSigs(
covPk *btcec.PublicKey,
stakingTxHash string,
sigs [][]byte,
) (*types.TxResponse, error) {

msg := &btcstakingtypes.MsgAddCovenantSig{
Signer: bc.mustGetTxSigner(),
Pk: bbntypes.NewBIP340PubKeyFromBTCPK(covPk),
StakingTxHash: stakingTxHash,
Sigs: sigs,
}

res, err := bc.reliablySendMsg(msg)
if err != nil {
return nil, err
}

return &types.TxResponse{TxHash: res.TxHash, Events: res.Events}, nil
}

// SubmitCovenantUnbondingSigs submits the Covenant signatures via a MsgAddCovenantUnbondingSigs to Babylon if the daemon runs in Covenant mode
// it returns tx hash and error
func (bc *BabylonController) SubmitCovenantUnbondingSigs(
covPk *btcec.PublicKey,
stakingTxHash string,
slashingSigs [][]byte,
unbondingSig *schnorr.Signature,
slashUnbondingSigs [][]byte,
unbondingSlashingSigs [][]byte,
) (*types.TxResponse, error) {
bip340UnbondingSig := bbntypes.NewBIP340SignatureFromBTCSig(unbondingSig)

msg := &btcstakingtypes.MsgAddCovenantUnbondingSigs{
msg := &btcstakingtypes.MsgAddCovenantSigs{
Signer: bc.mustGetTxSigner(),
Pk: bbntypes.NewBIP340PubKeyFromBTCPK(covPk),
StakingTxHash: stakingTxHash,
UnbondingTxSig: &bip340UnbondingSig,
SlashingUnbondingTxSigs: slashUnbondingSigs,
SlashingTxSigs: slashingSigs,
UnbondingTxSig: bip340UnbondingSig,
SlashingUnbondingTxSigs: unbondingSlashingSigs,
}

res, err := bc.reliablySendMsg(msg)
Expand Down Expand Up @@ -308,10 +287,6 @@ func (bc *BabylonController) QueryPendingDelegations(limit uint64) ([]*types.Del
return bc.queryDelegationsWithStatus(btcstakingtypes.BTCDelegationStatus_PENDING, limit)
}

func (bc *BabylonController) QueryUnbondingDelegations(limit uint64) ([]*types.Delegation, error) {
return bc.queryDelegationsWithStatus(btcstakingtypes.BTCDelegationStatus_UNBONDING, limit)
}

// queryDelegationsWithStatus queries BTC delegations that need a Covenant signature
// with the given status (either pending or unbonding)
// it is only used when the program is running in Covenant mode
Expand Down Expand Up @@ -643,6 +618,7 @@ func ConvertUndelegationType(undel *btcstakingtypes.BTCUndelegation) *types.Unde
CovenantSlashingSigs: covenantSlashingSigs,
CovenantUnbondingSigs: covenantUnbondingSigs,
UnbondingTime: undel.UnbondingTime,
DelegatorUnbondingSig: undel.DelegatorUnbondingSig,
}
}

Expand All @@ -656,23 +632,33 @@ func (bc *BabylonController) CreateBTCDelegation(
stakingValue int64,
stakingTxInfo *btcctypes.TransactionInfo,
slashingTx *btcstakingtypes.BTCSlashingTx,
delSig *bbntypes.BIP340Signature,
delSlashingSig *bbntypes.BIP340Signature,
unbondingTx []byte,
unbondingTime uint32,
unbondingValue int64,
unbondingSlashingTx *btcstakingtypes.BTCSlashingTx,
delUnbondingSlashingSig *bbntypes.BIP340Signature,
) (*types.TxResponse, error) {
valBtcPks := make([]bbntypes.BIP340PubKey, 0, len(valPks))
for _, v := range valPks {
valBtcPks = append(valBtcPks, *bbntypes.NewBIP340PubKeyFromBTCPK(v))
}
msg := &btcstakingtypes.MsgCreateBTCDelegation{
Signer: bc.mustGetTxSigner(),
BabylonPk: delBabylonPk,
BtcPk: delBtcPk,
ValBtcPkList: valBtcPks,
Pop: pop,
StakingTime: stakingTime,
StakingValue: stakingValue,
StakingTx: stakingTxInfo,
SlashingTx: slashingTx,
DelegatorSig: delSig,
Signer: bc.mustGetTxSigner(),
BabylonPk: delBabylonPk,
Pop: pop,
BtcPk: delBtcPk,
ValBtcPkList: valBtcPks,
StakingTime: stakingTime,
StakingValue: stakingValue,
StakingTx: stakingTxInfo,
SlashingTx: slashingTx,
DelegatorSlashingSig: delSlashingSig,
UnbondingTx: unbondingTx,
UnbondingTime: unbondingTime,
UnbondingValue: unbondingValue,
UnbondingSlashingTx: unbondingSlashingTx,
DelegatorUnbondingSlashingSig: delUnbondingSlashingSig,
}

res, err := bc.reliablySendMsg(msg)
Expand All @@ -683,31 +669,6 @@ func (bc *BabylonController) CreateBTCDelegation(
return &types.TxResponse{TxHash: res.TxHash}, nil
}

// Currently this is only used for e2e tests, probably does not need to add this into the interface
func (bc *BabylonController) CreateBTCUndelegation(
unbondingTx []byte,
unbondingTime uint32,
unbondingValue int64,
slashingTx *btcstakingtypes.BTCSlashingTx,
delSig *bbntypes.BIP340Signature,
) (*provider.RelayerTxResponse, error) {
msg := &btcstakingtypes.MsgBTCUndelegate{
Signer: bc.mustGetTxSigner(),
UnbondingTx: unbondingTx,
UnbondingTime: unbondingTime,
UnbondingValue: unbondingValue,
SlashingTx: slashingTx,
DelegatorSlashingSig: delSig,
}

res, err := bc.reliablySendMsg(msg)
if err != nil {
return nil, err
}

return res, nil
}

// Insert BTC block header using rpc client
// Currently this is only used for e2e tests, probably does not need to add it into the interface
func (bc *BabylonController) InsertBtcBlockHeaders(headers []bbntypes.BTCHeaderBytes) (*provider.RelayerTxResponse, error) {
Expand Down
15 changes: 2 additions & 13 deletions clientcontroller/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,22 +79,11 @@ type CovenantAPIs interface {
// SubmitCovenantSigs submits Covenant signatures to the consumer chain, each corresponding to
// a validator that the delegation is (re-)staked to
// it returns tx hash and error
SubmitCovenantSigs(covPk *btcec.PublicKey, stakingTxHash string, sigs [][]byte) (*types.TxResponse, error)

// SubmitCovenantUnbondingSigs submits the Covenant signatures for undelegation to the consumer chain
// it returns tx hash and error
SubmitCovenantUnbondingSigs(
covPk *btcec.PublicKey,
stakingTxHash string,
unbondingSig *schnorr.Signature,
slashUnbondingSigs [][]byte,
) (*types.TxResponse, error)
SubmitCovenantSigs(covPk *btcec.PublicKey, stakingTxHash string,
sigs [][]byte, unbondingSig *schnorr.Signature, unbondingSlashingSigs [][]byte) (*types.TxResponse, error)

// QueryPendingDelegations queries BTC delegations that are in status of pending
QueryPendingDelegations(limit uint64) ([]*types.Delegation, error)

// QueryUnbondingDelegations queries BTC delegations that are in status of unbonding
QueryUnbondingDelegations(limit uint64) ([]*types.Delegation, error)
}

func NewClientController(chainName string, bbnConfig *config.BBNConfig, netParams *chaincfg.Params, logger *zap.Logger) (ClientController, error) {
Expand Down
Loading

0 comments on commit f73962d

Please sign in to comment.