Skip to content

Commit

Permalink
feat: parameters upgrade (#31)
Browse files Browse the repository at this point in the history
* Update to babylon with new paramas
  • Loading branch information
KonradStaniec authored Sep 4, 2024
1 parent 4530202 commit dac0bb6
Show file tree
Hide file tree
Showing 11 changed files with 130 additions and 86 deletions.
54 changes: 42 additions & 12 deletions babylonclient/babyloncontroller.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,13 +100,18 @@ func NewBabylonController(
}

type StakingTrackerResponse struct {
SlashingAddress btcutil.Address
SlashingPkScript []byte
SlashingRate sdkmath.LegacyDec
MinComissionRate sdkmath.LegacyDec
CovenantPks []*btcec.PublicKey
CovenantQuruomThreshold uint32
MinSlashingFee btcutil.Amount
MinUnbodningTime uint16
MinUnbondingTime uint16
UnbondingFee btcutil.Amount
MinStakingTime uint16
MaxStakingTime uint16
MinStakingValue btcutil.Amount
MaxStakingValue btcutil.Amount
}

type FinalityProviderInfo struct {
Expand Down Expand Up @@ -175,18 +180,23 @@ func (bc *BabylonController) Params() (*StakingParams, error) {

minUnbondingTime := sdkmath.Max[uint16](
uint16(bccParams.CheckpointFinalizationTimeout),
stakingTrackerParams.MinUnbodningTime,
stakingTrackerParams.MinUnbondingTime,
)

return &StakingParams{
ConfirmationTimeBlocks: uint32(bccParams.BtcConfirmationDepth),
FinalizationTimeoutBlocks: uint32(bccParams.CheckpointFinalizationTimeout),
SlashingAddress: stakingTrackerParams.SlashingAddress,
SlashingPkScript: stakingTrackerParams.SlashingPkScript,
CovenantPks: stakingTrackerParams.CovenantPks,
MinSlashingTxFeeSat: stakingTrackerParams.MinSlashingFee,
SlashingRate: stakingTrackerParams.SlashingRate,
CovenantQuruomThreshold: stakingTrackerParams.CovenantQuruomThreshold,
MinUnbondingTime: minUnbondingTime,
UnbondingFee: stakingTrackerParams.UnbondingFee,
MinStakingTime: stakingTrackerParams.MinStakingTime,
MaxStakingTime: stakingTrackerParams.MaxStakingTime,
MinStakingValue: stakingTrackerParams.MinStakingValue,
MaxStakingValue: stakingTrackerParams.MaxStakingValue,
}, nil
}

Expand Down Expand Up @@ -446,11 +456,6 @@ func (bc *BabylonController) QueryStakingTracker() (*StakingTrackerResponse, err
return nil, err
}

slashingAddress, err := btcutil.DecodeAddress(response.Params.SlashingAddress, bc.btcParams)
if err != nil {
return nil, err
}

// check this early than covenant config makes sense, so that rest of the
// code can assume that:
// 1. covenant quorum is less or equal to number of covenant pks
Expand All @@ -473,18 +478,43 @@ func (bc *BabylonController) QueryStakingTracker() (*StakingTrackerResponse, err
covenantPks = append(covenantPks, covenantBtcPk)
}

if response.Params.MinUnbondingTime > math.MaxUint16 {
if response.Params.MinUnbondingTimeBlocks > math.MaxUint16 {
return nil, fmt.Errorf("min unbonding time is bigger than uint16: %w", ErrInvalidValueReceivedFromBabylonNode)
}

if response.Params.MinStakingTimeBlocks > math.MaxUint16 {
return nil, fmt.Errorf("min staking time is bigger than uint16: %w", ErrInvalidValueReceivedFromBabylonNode)
}

if response.Params.MaxStakingTimeBlocks > math.MaxUint16 {
return nil, fmt.Errorf("max staking time is bigger than uint16: %w", ErrInvalidValueReceivedFromBabylonNode)
}

if response.Params.MinStakingValueSat < 0 {
return nil, fmt.Errorf("min staking value is negative: %w", ErrInvalidValueReceivedFromBabylonNode)
}

if response.Params.MaxStakingValueSat < 0 {
return nil, fmt.Errorf("max staking value is negative: %w", ErrInvalidValueReceivedFromBabylonNode)
}

if response.Params.UnbondingFeeSat < 0 {
return nil, fmt.Errorf("unbonding fee is negative: %w", ErrInvalidValueReceivedFromBabylonNode)
}

return &StakingTrackerResponse{
SlashingAddress: slashingAddress,
SlashingPkScript: response.Params.SlashingPkScript,
SlashingRate: response.Params.SlashingRate,
MinComissionRate: response.Params.MinCommissionRate,
CovenantPks: covenantPks,
MinSlashingFee: btcutil.Amount(response.Params.MinSlashingTxFeeSat),
CovenantQuruomThreshold: response.Params.CovenantQuorum,
MinUnbodningTime: uint16(response.Params.MinUnbondingTime),
MinUnbondingTime: uint16(response.Params.MinUnbondingTimeBlocks),
UnbondingFee: btcutil.Amount(response.Params.UnbondingFeeSat),
MinStakingTime: uint16(response.Params.MinStakingTimeBlocks),
MaxStakingTime: uint16(response.Params.MaxStakingTimeBlocks),
MinStakingValue: btcutil.Amount(response.Params.MinStakingValueSat),
MaxStakingValue: btcutil.Amount(response.Params.MaxStakingValueSat),
}, nil
}

Expand Down
28 changes: 25 additions & 3 deletions babylonclient/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"github.com/btcsuite/btcd/btcutil"
"github.com/btcsuite/btcd/chaincfg"
"github.com/btcsuite/btcd/chaincfg/chainhash"
"github.com/btcsuite/btcd/txscript"
"github.com/cosmos/cosmos-sdk/crypto/keys/secp256k1"
sdk "github.com/cosmos/cosmos-sdk/types"
pv "github.com/cosmos/relayer/v2/relayer/provider"
Expand All @@ -27,8 +28,8 @@ type StakingParams struct {
// Bitcoin public key of the current covenant
CovenantPks []*btcec.PublicKey

// Address to which slashing transactions are sent
SlashingAddress btcutil.Address
// PkScript that must be inserted in the slashing output of the slashing transaction
SlashingPkScript []byte

// The rate at which the staked funds will be slashed, expressed as a decimal.
SlashingRate sdkmath.LegacyDec
Expand All @@ -38,6 +39,21 @@ type StakingParams struct {

// Minimum unbonding time required by bayblon
MinUnbondingTime uint16

// Fee required by unbonding transaction
UnbondingFee btcutil.Amount

// Minimum staking time required by bayblon
MinStakingTime uint16

// Maximum staking time required by bayblon
MaxStakingTime uint16

// Minimum staking value required by bayblon
MinStakingValue btcutil.Amount

// Maximum staking value required by bayblon
MaxStakingValue btcutil.Amount
}

// SingleKeyCosmosKeyring represents a keyring that supports only one pritvate/public key pair
Expand Down Expand Up @@ -154,6 +170,12 @@ func GetMockClient() *MockBabylonClient {

slashingAddress, _ := btcutil.NewAddressPubKey(covenantPk.PubKey().SerializeCompressed(), &chaincfg.SimNetParams)

slashingPkScript, err := txscript.PayToAddrScript(slashingAddress)

if err != nil {
panic(err)
}

fpBtcPrivKey, err := btcec.NewPrivateKey()
if err != nil {
panic(err)
Expand All @@ -170,7 +192,7 @@ func GetMockClient() *MockBabylonClient {
FinalizationTimeoutBlocks: 5,
MinSlashingTxFeeSat: btcutil.Amount(1000),
CovenantPks: []*btcec.PublicKey{covenantPk.PubKey()},
SlashingAddress: slashingAddress,
SlashingPkScript: slashingPkScript,
SlashingRate: sdkmath.LegacyNewDecWithPrec(1, 1), // 1 * 10^{-1} = 0.1
},
babylonKey: priv,
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,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/babylonlabs-io/babylon v0.9.0
github.com/babylonlabs-io/babylon v0.9.3-0.20240904123958-b1e255a85e76
github.com/babylonlabs-io/networks/parameters v0.2.2
github.com/btcsuite/btcd v0.24.2
github.com/btcsuite/btcd/btcec/v2 v2.3.2
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/babylonlabs-io/babylon v0.9.0 h1:dHZ9wUrI5XLaO4UIwJRgiCdnzFdi5yv7dpibbu6TDv0=
github.com/babylonlabs-io/babylon v0.9.0/go.mod h1:t7B4e+ooD2oYvAxkegtNKDL9bXe+vU29a8xnCQh+UKo=
github.com/babylonlabs-io/babylon v0.9.3-0.20240904123958-b1e255a85e76 h1:JOdd2H+bOYPyKYNyCcjT2lW7H76A4Pbfstk7n/Vo2Vc=
github.com/babylonlabs-io/babylon v0.9.3-0.20240904123958-b1e255a85e76/go.mod h1:9VUUAwVaalXiDdPZT65SPoawKWpp6ple6tBr8Vw0NI8=
github.com/babylonlabs-io/networks/parameters v0.2.2 h1:TCu39fZvjX5f6ZZrjhYe54M6wWxglNewuKu56yE+zrc=
github.com/babylonlabs-io/networks/parameters v0.2.2/go.mod h1:iEJVOzaLsE33vpP7J4u+CRGfkSIfErUAwRmgCFCBpyI=
github.com/benbjohnson/clock v1.1.0/go.mod h1:J11/hYXuz8f4ySSvYwY0FKfm+ezbsZBKZxNJlLklBHA=
Expand Down
6 changes: 4 additions & 2 deletions itest/babylon_node_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ func NewBabylonNodeHandler(
covenantPk1 *btcec.PublicKey,
covenantPk2 *btcec.PublicKey,
covenantPk3 *btcec.PublicKey,
slashingAddress string,
slashingPkScript string,
baseHeaderHex string,
) (*BabylonNodeHandler, error) {
testDir, err := baseDirBabylondir()
Expand All @@ -132,7 +132,9 @@ func NewBabylonNodeHandler(
"--btc-finalization-timeout=4",
"--btc-confirmation-depth=2",
"--btc-network=regtest",
fmt.Sprintf("--slashing-address=%s", slashingAddress),
"--min-staking-time-blocks=200",
"--min-staking-amount-sat=10000",
fmt.Sprintf("--slashing-pk-script=%s", slashingPkScript),
fmt.Sprintf("--btc-base-header=%s", baseHeaderHex),
"--additional-sender-account",
fmt.Sprintf("--covenant-quorum=%s", quorumString),
Expand Down
36 changes: 17 additions & 19 deletions itest/e2e_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -246,13 +246,16 @@ func StartManager(
require.NoError(t, err)
baseHeaderHex := hex.EncodeToString(buff.Bytes())

pkScript, err := txscript.PayToAddrScript(minerAddressDecoded)
require.NoError(t, err)

bh, err := NewBabylonNodeHandler(
quorum,
coventantPrivKeys[0].PubKey(),
coventantPrivKeys[1].PubKey(),
coventantPrivKeys[2].PubKey(),
// all slashings will be sent back to wallet
minerAddressDecoded.EncodeAddress(),
hex.EncodeToString(pkScript),
baseHeaderHex,
)
require.NoError(t, err)
Expand Down Expand Up @@ -806,7 +809,7 @@ func (tm *TestManager) sendWatchedStakingTx(
slashingTx, err := staking.BuildSlashingTxFromStakingTxStrict(
tx,
uint32(stakingOutputIdx),
params.SlashingAddress,
params.SlashingPkScript,
testStakingData.StakerKey,
unbondingTme,
int64(params.MinSlashingTxFeeSat)+10,
Expand Down Expand Up @@ -838,7 +841,7 @@ func (tm *TestManager) sendWatchedStakingTx(
serializedSlashingTx, err := utils.SerializeBtcTransaction(slashingTx)
require.NoError(t, err)
// Build unbonding related data
unbondingFee := btcutil.Amount(1000)
unbondingFee := params.UnbondingFee
unbondingAmount := btcutil.Amount(testStakingData.StakingAmount) - unbondingFee

unbondingInfo, err := staking.BuildUnbondingInfo(
Expand All @@ -862,7 +865,7 @@ func (tm *TestManager) sendWatchedStakingTx(
slashUnbondingTx, err := staking.BuildSlashingTxFromStakingTxStrict(
unbondingTx,
0,
params.SlashingAddress,
params.SlashingPkScript,
testStakingData.StakerKey,
unbondingTme,
int64(params.MinSlashingTxFeeSat)+10,
Expand Down Expand Up @@ -1103,9 +1106,8 @@ func TestStakingFailures(t *testing.T) {
cl := tm.Sa.BabylonController()
params, err := cl.Params()
require.NoError(t, err)
stakingTime := uint16(staker.GetMinStakingTime(params))

testStakingData := tm.getTestStakingData(t, tm.WalletPubKey, stakingTime, 10000, 1)
testStakingData := tm.getTestStakingData(t, tm.WalletPubKey, params.MinStakingTime, 10000, 1)
fpKey := hex.EncodeToString(schnorr.SerializePubKey(testStakingData.FinalityProviderBtcKeys[0]))

tm.createAndRegisterFinalityProviders(t, testStakingData)
Expand Down Expand Up @@ -1143,9 +1145,8 @@ func TestSendingStakingTransaction(t *testing.T) {
cl := tm.Sa.BabylonController()
params, err := cl.Params()
require.NoError(t, err)
stakingTime := uint16(staker.GetMinStakingTime(params))

testStakingData := tm.getTestStakingData(t, tm.WalletPubKey, stakingTime, 10000, 1)
testStakingData := tm.getTestStakingData(t, tm.WalletPubKey, params.MinStakingTime, 10000, 1)

hashed, err := chainhash.NewHash(datagen.GenRandomByteArray(r, 32))
require.NoError(t, err)
Expand Down Expand Up @@ -1218,7 +1219,7 @@ func TestMultipleWithdrawableStakingTransactions(t *testing.T) {
cl := tm.Sa.BabylonController()
params, err := cl.Params()
require.NoError(t, err)
minStakingTime := uint16(staker.GetMinStakingTime(params))
minStakingTime := params.MinStakingTime
stakingTime1 := minStakingTime
stakingTime2 := minStakingTime + 4
stakingTime3 := minStakingTime + 1
Expand Down Expand Up @@ -1284,8 +1285,8 @@ func TestSendingWatchedStakingTransaction(t *testing.T) {
cl := tm.Sa.BabylonController()
params, err := cl.Params()
require.NoError(t, err)
stakingTime := uint16(staker.GetMinStakingTime(params))
testStakingData := tm.getTestStakingData(t, tm.WalletPubKey, stakingTime, 10000, 1)

testStakingData := tm.getTestStakingData(t, tm.WalletPubKey, params.MinStakingTime, 10000, 1)

tm.createAndRegisterFinalityProviders(t, testStakingData)

Expand All @@ -1306,8 +1307,8 @@ func TestRestartingTxNotDeepEnough(t *testing.T) {
cl := tm.Sa.BabylonController()
params, err := cl.Params()
require.NoError(t, err)
stakingTime := uint16(staker.GetMinStakingTime(params))
testStakingData := tm.getTestStakingData(t, tm.WalletPubKey, stakingTime, 10000, 1)

testStakingData := tm.getTestStakingData(t, tm.WalletPubKey, params.MinStakingTime, 10000, 1)

tm.createAndRegisterFinalityProviders(t, testStakingData)
txHash := tm.sendStakingTxBTC(t, testStakingData)
Expand All @@ -1331,9 +1332,8 @@ func TestRestartingTxNotOnBabylon(t *testing.T) {
cl := tm.Sa.BabylonController()
params, err := cl.Params()
require.NoError(t, err)
stakingTime := uint16(staker.GetMinStakingTime(params))

testStakingData1 := tm.getTestStakingData(t, tm.WalletPubKey, stakingTime, 10000, 1)
testStakingData1 := tm.getTestStakingData(t, tm.WalletPubKey, params.MinStakingTime, 10000, 1)
testStakingData2 := testStakingData1.withStakingAmout(11000)

tm.createAndRegisterFinalityProviders(t, testStakingData1)
Expand Down Expand Up @@ -1615,10 +1615,9 @@ func TestSendingStakingTransaction_Restaking(t *testing.T) {
cl := tm.Sa.BabylonController()
params, err := cl.Params()
require.NoError(t, err)
stakingTime := uint16(staker.GetMinStakingTime(params))

// restaked to 5 finality providers
testStakingData := tm.getTestStakingData(t, tm.WalletPubKey, stakingTime, 10000, 5)
testStakingData := tm.getTestStakingData(t, tm.WalletPubKey, params.MinStakingTime, 10000, 5)

hashed, err := chainhash.NewHash(datagen.GenRandomByteArray(r, 32))
require.NoError(t, err)
Expand Down Expand Up @@ -1656,9 +1655,8 @@ func TestRecoverAfterRestartDuringWithdrawal(t *testing.T) {
cl := tm.Sa.BabylonController()
params, err := cl.Params()
require.NoError(t, err)
stakingTime := uint16(staker.GetMinStakingTime(params))

testStakingData := tm.getTestStakingData(t, tm.WalletPubKey, stakingTime, 10000, 1)
testStakingData := tm.getTestStakingData(t, tm.WalletPubKey, params.MinStakingTime, 10000, 1)

hashed, err := chainhash.NewHash(datagen.GenRandomByteArray(r, 32))
require.NoError(t, err)
Expand Down
8 changes: 2 additions & 6 deletions staker/babylontypes.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,17 +56,13 @@ func (app *StakerApp) buildOwnedDelegation(
}).Fatalf("Failed to build delegation data for already confirmed staking transaction")
}

// TODO: Option to use custom fee rate, as estimator uses pretty big value for fee
// in case of estimation failure (25 sat/byte)
unbondingTxFeeRatePerKb := btcutil.Amount(app.feeEstimator.EstimateFeePerKb())

undelegationDesc, err := createUndelegationData(
storedTx,
externalData.stakerPublicKey,
externalData.babylonParams.CovenantPks,
externalData.babylonParams.CovenantQuruomThreshold,
externalData.babylonParams.SlashingAddress,
unbondingTxFeeRatePerKb,
externalData.babylonParams.SlashingPkScript,
externalData.babylonParams.UnbondingFee,
// TODO: Possiblity to customize finalization time
uint16(externalData.babylonParams.MinUnbondingTime)+1,
app.getSlashingFee(externalData.babylonParams.MinSlashingTxFeeSat),
Expand Down
Loading

0 comments on commit dac0bb6

Please sign in to comment.