Skip to content

Commit

Permalink
adapt version delegation params
Browse files Browse the repository at this point in the history
  • Loading branch information
gitferry committed Apr 4, 2024
1 parent 6cb25a9 commit 8794cae
Show file tree
Hide file tree
Showing 13 changed files with 127 additions and 140 deletions.
9 changes: 5 additions & 4 deletions clientcontroller/babylon.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@ import (
"github.com/btcsuite/btcd/btcec/v2"
stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types"

bbnclient "github.com/babylonchain/babylon/client/client"
bbntypes "github.com/babylonchain/babylon/types"
btcctypes "github.com/babylonchain/babylon/x/btccheckpoint/types"
btclctypes "github.com/babylonchain/babylon/x/btclightclient/types"
btcstakingtypes "github.com/babylonchain/babylon/x/btcstaking/types"
bbnclient "github.com/babylonchain/rpc-client/client"
"github.com/btcsuite/btcd/btcutil"
"github.com/btcsuite/btcd/chaincfg"
sdkclient "github.com/cosmos/cosmos-sdk/client"
Expand Down Expand Up @@ -91,17 +91,17 @@ func (bc *BabylonController) GetKeyAddress() sdk.AccAddress {
return addr
}

func (bc *BabylonController) QueryStakingParams() (*types.StakingParams, error) {
func (bc *BabylonController) QueryStakingParamsByVersion(version uint32) (*types.StakingParams, error) {
// query btc checkpoint params
ckptParamRes, err := bc.bbnClient.QueryClient.BTCCheckpointParams()
if err != nil {
return nil, fmt.Errorf("failed to query params of the btccheckpoint module: %v", err)
}

// query btc staking params
stakingParamRes, err := bc.bbnClient.QueryClient.BTCStakingParams()
stakingParamRes, err := bc.bbnClient.QueryClient.BTCStakingParamsByVersion(version)
if err != nil {
return nil, fmt.Errorf("failed to query staking params: %v", err)
return nil, fmt.Errorf("failed to query staking params with version %d: %v", version, err)
}

covenantPks := make([]*btcec.PublicKey, 0, len(stakingParamRes.Params.CovenantPks))
Expand Down Expand Up @@ -259,6 +259,7 @@ func DelegationRespToDelegation(del *btcstakingtypes.BTCDelegationResponse) (*ty
CovenantSigs: covenantSigs,
UnbondingTime: del.UnbondingTime,
BtcUndelegation: undelegation,
ParamsVersion: del.ParamsVersion,
}, nil
}

Expand Down
3 changes: 2 additions & 1 deletion clientcontroller/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package clientcontroller

import (
"fmt"

"github.com/btcsuite/btcd/chaincfg"
"go.uber.org/zap"

Expand All @@ -22,7 +23,7 @@ type ClientController interface {
// QueryPendingDelegations queries BTC delegations that are in status of pending
QueryPendingDelegations(limit uint64) ([]*types.Delegation, error)

QueryStakingParams() (*types.StakingParams, error)
QueryStakingParamsByVersion(version uint32) (*types.StakingParams, error)

Close() error
}
Expand Down
2 changes: 1 addition & 1 deletion config/babylon.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package config
import (
"time"

bbncfg "github.com/babylonchain/rpc-client/config"
bbncfg "github.com/babylonchain/babylon/client/config"
)

type BBNConfig struct {
Expand Down
51 changes: 20 additions & 31 deletions covenant/covenant.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,6 @@ type CovenantEmulator struct {
kc *keyring.ChainKeyringController

config *covcfg.Config
params *types.StakingParams
logger *zap.Logger

// input is used to pass passphrase to the keyring
Expand Down Expand Up @@ -109,16 +108,6 @@ func (ce *CovenantEmulator) PublicKeyStr() string {
return hex.EncodeToString(schnorr.SerializePubKey(ce.pk))
}

func (ce *CovenantEmulator) UpdateParams() error {
params, err := ce.getParamsWithRetry()
if err != nil {
return err
}
ce.params = params

return nil
}

// AddCovenantSignatures adds a Covenant signature on the given Bitcoin delegation and submits it to Babylon
// TODO: break this function into smaller components
func (ce *CovenantEmulator) AddCovenantSignatures(btcDels []*types.Delegation) (*types.TxResponse, error) {
Expand All @@ -136,8 +125,14 @@ func (ce *CovenantEmulator) AddCovenantSignatures(btcDels []*types.Delegation) (
return nil, fmt.Errorf("empty undelegation")
}

// 1. get the params matched to the delegation version
params, err := ce.getParamsByVersionWithRetry(btcDel.ParamsVersion)
if err != nil {
return nil, fmt.Errorf("failed to get staking params with version %d: %w", btcDel.ParamsVersion, err)
}

// 1. the quorum is already achieved, skip sending more sigs
if btcDel.HasCovenantQuorum(ce.params.CovenantQuorum) {
if btcDel.HasCovenantQuorum(params.CovenantQuorum) {
return nil, nil
}

Expand All @@ -146,7 +141,7 @@ func (ce *CovenantEmulator) AddCovenantSignatures(btcDels []*types.Delegation) (
// - MinUnbondingTime
// - CheckpointFinalizationTimeout
unbondingTime := btcDel.UnbondingTime
minUnbondingTime := ce.params.MinUnbondingTime
minUnbondingTime := params.MinUnbondingTime
if unbondingTime <= minUnbondingTime {
return nil, fmt.Errorf("unbonding time %d must be larger than %d",
unbondingTime, minUnbondingTime)
Expand All @@ -172,9 +167,9 @@ func (ce *CovenantEmulator) AddCovenantSignatures(btcDels []*types.Delegation) (
slashingMsgTx,
stakingMsgTx,
btcDel.StakingOutputIdx,
int64(ce.params.MinSlashingTxFeeSat),
ce.params.SlashingRate,
ce.params.SlashingAddress,
int64(params.MinSlashingTxFeeSat),
params.SlashingRate,
params.SlashingAddress,
btcDel.BtcPk,
uint16(unbondingTime),
&ce.config.BTCNetParams,
Expand All @@ -196,8 +191,8 @@ func (ce *CovenantEmulator) AddCovenantSignatures(btcDels []*types.Delegation) (
unbondingInfo, err := btcstaking.BuildUnbondingInfo(
btcDel.BtcPk,
btcDel.FpBtcPks,
ce.params.CovenantPks,
ce.params.CovenantQuorum,
params.CovenantPks,
params.CovenantQuorum,
uint16(unbondingTime),
btcutil.Amount(unbondingMsgTx.TxOut[0].Value),
&ce.config.BTCNetParams,
Expand All @@ -210,9 +205,9 @@ func (ce *CovenantEmulator) AddCovenantSignatures(btcDels []*types.Delegation) (
unbondingSlashingMsgTx,
unbondingMsgTx,
0,
int64(ce.params.MinSlashingTxFeeSat),
ce.params.SlashingRate,
ce.params.SlashingAddress,
int64(params.MinSlashingTxFeeSat),
params.SlashingRate,
params.SlashingAddress,
btcDel.BtcPk,
uint16(unbondingTime),
&ce.config.BTCNetParams,
Expand All @@ -234,8 +229,8 @@ func (ce *CovenantEmulator) AddCovenantSignatures(btcDels []*types.Delegation) (
stakingInfo, err := btcstaking.BuildStakingInfo(
btcDel.BtcPk,
btcDel.FpBtcPks,
ce.params.CovenantPks,
ce.params.CovenantQuorum,
params.CovenantPks,
params.CovenantQuorum,
btcDel.GetStakingTime(),
btcutil.Amount(btcDel.TotalSat),
&ce.config.BTCNetParams,
Expand Down Expand Up @@ -405,12 +400,6 @@ func (ce *CovenantEmulator) covenantSigSubmissionLoop() {
for {
select {
case <-covenantSigTicker.C:
// 0. Update slashing address in case it is changed upon governance proposal
if err := ce.UpdateParams(); err != nil {
ce.logger.Debug("failed to get staking params", zap.Error(err))
continue
}

// 1. Get all pending delegations
dels, err := ce.cc.QueryPendingDelegations(limit)
if err != nil {
Expand Down Expand Up @@ -487,14 +476,14 @@ func CreateCovenantKey(keyringDir, chainID, keyName, backend, passphrase, hdPath
return krController.CreateChainKey(passphrase, hdPath)
}

func (ce *CovenantEmulator) getParamsWithRetry() (*types.StakingParams, error) {
func (ce *CovenantEmulator) getParamsByVersionWithRetry(version uint32) (*types.StakingParams, error) {
var (
params *types.StakingParams
err error
)

if err := retry.Do(func() error {
params, err = ce.cc.QueryStakingParams()
params, err = ce.cc.QueryStakingParamsByVersion(version)
if err != nil {
return err
}
Expand Down
3 changes: 0 additions & 3 deletions covenant/covenant_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,6 @@ func FuzzAddCovenantSig(f *testing.F) {
ce, err := covenant.NewCovenantEmulator(&covenantConfig, mockClientController, passphrase, zap.NewNop())
require.NoError(t, err)

err = ce.UpdateParams()
require.NoError(t, err)

numDels := datagen.RandomInt(r, 3) + 1
covSigsSet := make([]*types.CovenantSigs, 0, numDels)
btcDels := make([]*types.Delegation, 0, numDels)
Expand Down
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +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.20240314161103-c2c92d903a48
github.com/babylonchain/rpc-client v0.8.0-rc.0.0.20240315010507-4e4e08fc5420
github.com/babylonchain/babylon v0.8.6-0.20240403114330-1ee4d649d955
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 Expand Up @@ -182,6 +181,7 @@ require (
github.com/prometheus/common v0.47.0 // indirect
github.com/prometheus/procfs v0.12.0 // indirect
github.com/rcrowley/go-metrics v0.0.0-20201227073835-cf1acfcdf475 // indirect
github.com/rivo/uniseg v0.4.4 // indirect
github.com/rogpeppe/go-internal v1.12.0 // indirect
github.com/rs/cors v1.8.3 // indirect
github.com/rs/zerolog v1.32.0 // indirect
Expand Down
6 changes: 2 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -273,10 +273,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.20240314161103-c2c92d903a48 h1:STSRSyxNmDGnGpwGrmvu0Y7MRUdWf6+jHLwRc0Y3gcY=
github.com/babylonchain/babylon v0.8.6-0.20240314161103-c2c92d903a48/go.mod h1:jR1b+5mA7BkRrXfd/PMHwk7W/RpoeQtunvjal+tKeHY=
github.com/babylonchain/rpc-client v0.8.0-rc.0.0.20240315010507-4e4e08fc5420 h1:5RMocqFkrpGtFbe1OMelSmEiikmIslbQEarTa2OoeQ4=
github.com/babylonchain/rpc-client v0.8.0-rc.0.0.20240315010507-4e4e08fc5420/go.mod h1:OqW4bUDGVuqGjZ9RQjcTOTjLaz6V50S/po0oUCbA0m8=
github.com/babylonchain/babylon v0.8.6-0.20240403114330-1ee4d649d955 h1:7XtfE9n9STrZq3j1B9qk41a+LQgSR4vwOIKd4QD2jtg=
github.com/babylonchain/babylon v0.8.6-0.20240403114330-1ee4d649d955/go.mod h1:lfeASLNJgcUsX7LEns3HRUv0k+MjzcB2q2AMasfz38M=
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
2 changes: 1 addition & 1 deletion itest/test_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ func StartManager(t *testing.T) *TestManager {
func (tm *TestManager) WaitForServicesStart(t *testing.T) {
// wait for Babylon node starts
require.Eventually(t, func() bool {
params, err := tm.CovBBNClient.QueryStakingParams()
params, err := tm.CovBBNClient.QueryStakingParamsByVersion(0)
if err != nil {
return false
}
Expand Down
12 changes: 6 additions & 6 deletions testutil/mocks/babylon.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion testutil/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ func PrepareMockedClientController(t *testing.T, params *types.StakingParams) *m
mockClientController := mocks.NewMockClientController(ctl)

mockClientController.EXPECT().Close().Return(nil).AnyTimes()
mockClientController.EXPECT().QueryStakingParams().Return(params, nil).AnyTimes()
mockClientController.EXPECT().QueryStakingParamsByVersion(gomock.Any()).Return(params, nil).AnyTimes()

return mockClientController
}
Loading

0 comments on commit 8794cae

Please sign in to comment.