Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adjust covenant emulator to babylon with new params #10

Merged
merged 2 commits into from
Sep 4, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 7 additions & 6 deletions clientcontroller/babylon.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,21 +111,22 @@ func (bc *BabylonController) QueryStakingParamsByVersion(version uint32) (*types
}
covenantPks = append(covenantPks, covPk)
}
slashingAddress, err := btcutil.DecodeAddress(stakingParamRes.Params.SlashingAddress, bc.btcParams)
if err != nil {
return nil, err
}

return &types.StakingParams{
ComfirmationTimeBlocks: ckptParamRes.Params.BtcConfirmationDepth,
FinalizationTimeoutBlocks: ckptParamRes.Params.CheckpointFinalizationTimeout,
MinSlashingTxFeeSat: btcutil.Amount(stakingParamRes.Params.MinSlashingTxFeeSat),
CovenantPks: covenantPks,
SlashingAddress: slashingAddress,
SlashingPkScript: stakingParamRes.Params.SlashingPkScript,
CovenantQuorum: stakingParamRes.Params.CovenantQuorum,
SlashingRate: stakingParamRes.Params.SlashingRate,
MinComissionRate: stakingParamRes.Params.MinCommissionRate,
MinUnbondingTime: stakingParamRes.Params.MinUnbondingTime,
MinUnbondingTime: stakingParamRes.Params.MinUnbondingTimeBlocks,
UnbondingFee: btcutil.Amount(stakingParamRes.Params.UnbondingFeeSat),
MinStakingTime: uint16(stakingParamRes.Params.MinStakingTimeBlocks),
MaxStakingTime: uint16(stakingParamRes.Params.MaxStakingTimeBlocks),
MinStakingValue: btcutil.Amount(stakingParamRes.Params.MinStakingValueSat),
MaxStakingValue: btcutil.Amount(stakingParamRes.Params.MaxStakingValueSat),
}, nil
}

Expand Down
44 changes: 39 additions & 5 deletions covenant/covenant.go
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,30 @@ func (ce *CovenantEmulator) AddCovenantSignatures(btcDels []*types.Delegation) (
continue
}

// 4. decode staking tx and slashing tx from the delegation
// 4. check whether the:
RafilxTenfen marked this conversation as resolved.
Show resolved Hide resolved
// - staking time is within the min and max staking time
// - staking value is within the min and max staking value
stakingTime := btcDel.GetStakingTime()

if stakingTime < params.MinStakingTime || stakingTime > params.MaxStakingTime {
ce.logger.Error("invalid staking time",
zap.Uint16("min_staking_time", params.MinStakingTime),
zap.Uint16("max_staking_time", params.MaxStakingTime),
zap.Uint16("got_staking_time", stakingTime),
)
continue
}

if btcDel.TotalSat < uint64(params.MinStakingValue) || btcDel.TotalSat > uint64(params.MaxStakingValue) {
ce.logger.Error("invalid staking value",
zap.Uint64("min_staking_value", uint64(params.MinStakingValue)),
zap.Uint64("max_staking_value", uint64(params.MaxStakingValue)),
zap.Uint64("got_staking_value", btcDel.TotalSat),
)
continue
}

// 5. decode staking tx and slashing tx from the delegation
stakingTx, slashingTx, err := decodeDelegationTransactions(btcDel, params, &ce.config.BTCNetParams)
if err != nil {
ce.logger.Error("invalid delegation",
Expand All @@ -169,7 +192,7 @@ func (ce *CovenantEmulator) AddCovenantSignatures(btcDels []*types.Delegation) (
continue
}

// 5. decode unbonding tx and slash unbonding tx from the undelegation
// 6. decode unbonding tx and slash unbonding tx from the undelegation
unbondingTx, slashUnbondingTx, err := decodeUndelegationTransactions(btcDel, params, &ce.config.BTCNetParams)
if err != nil {
ce.logger.Error("invalid undelegation",
Expand All @@ -181,7 +204,18 @@ func (ce *CovenantEmulator) AddCovenantSignatures(btcDels []*types.Delegation) (
continue
}

// 6. sign covenant staking sigs
// 7. Check unbonding fee
unbondingFee := stakingTx.TxOut[btcDel.StakingOutputIdx].Value - unbondingTx.TxOut[0].Value

if unbondingFee != int64(params.UnbondingFee) {
ce.logger.Error("invalid unbonding fee",
zap.Int64("expected_unbonding_fee", int64(params.UnbondingFee)),
zap.Int64("got_unbonding_fee", unbondingFee),
)
continue
}

// 8. sign covenant staking sigs
// record metrics
startSignTime := time.Now()
metricsTimeKeeper.SetPreviousSignStart(&startSignTime)
Expand Down Expand Up @@ -393,7 +427,7 @@ func decodeDelegationTransactions(del *types.Delegation, params *types.StakingPa
del.StakingOutputIdx,
int64(params.MinSlashingTxFeeSat),
params.SlashingRate,
params.SlashingAddress,
params.SlashingPkScript,
del.BtcPk,
uint16(del.UnbondingTime),
btcNet,
Expand Down Expand Up @@ -423,7 +457,7 @@ func decodeUndelegationTransactions(del *types.Delegation, params *types.Staking
0,
int64(params.MinSlashingTxFeeSat),
params.SlashingRate,
params.SlashingAddress,
params.SlashingPkScript,
del.BtcPk,
uint16(del.UnbondingTime),
btcNet,
Expand Down
10 changes: 5 additions & 5 deletions covenant/covenant_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,8 @@ func FuzzAddCovenantSig(f *testing.F) {
// generate BTC delegation
delSK, delPK, err := datagen.GenRandomBTCKeyPair(r)
require.NoError(t, err)
stakingTimeBlocks := uint16(5)
stakingValue := int64(2 * 10e8)
stakingTimeBlocks := uint16(testutil.RandRange(r, int(params.MinStakingTime), int(params.MaxStakingTime)))
stakingValue := int64(testutil.RandRange(r, int(params.MinStakingValue), int(params.MaxStakingValue)))
unbondingTime := uint16(params.MinimumUnbondingTime()) + 1
fpNum := datagen.RandomInt(r, 5) + 1
fpPks := testutil.GenBtcPublicKeys(r, t, int(fpNum))
Expand All @@ -73,7 +73,7 @@ func FuzzAddCovenantSig(f *testing.F) {
params.CovenantQuorum,
stakingTimeBlocks,
stakingValue,
params.SlashingAddress.String(),
params.SlashingPkScript,
params.SlashingRate,
unbondingTime,
)
Expand Down Expand Up @@ -112,7 +112,7 @@ func FuzzAddCovenantSig(f *testing.F) {
}

// generate undelegation
unbondingValue := int64(btcDel.TotalSat) - 1000
unbondingValue := int64(btcDel.TotalSat) - int64(params.UnbondingFee)

stakingTxHash := testInfo.StakingTx.TxHash()
testUnbondingInfo := datagen.GenBTCUnbondingSlashingInfo(
Expand All @@ -126,7 +126,7 @@ func FuzzAddCovenantSig(f *testing.F) {
wire.NewOutPoint(&stakingTxHash, 0),
unbondingTime,
unbondingValue,
params.SlashingAddress.String(),
params.SlashingPkScript,
params.SlashingRate,
unbondingTime,
)
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/babylonlabs-io/babylon v0.9.0
github.com/babylonlabs-io/babylon v0.9.3-0.20240904061947-c170f5d15db0
github.com/btcsuite/btcd v0.24.2
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 @@ -273,8 +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/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.20240904061947-c170f5d15db0 h1:xw1zphFUosCd7Ib/5YXTX5QE+jnJMgAwrlvPK7SWlDQ=
github.com/babylonlabs-io/babylon v0.9.3-0.20240904061947-c170f5d15db0/go.mod h1:9VUUAwVaalXiDdPZT65SPoawKWpp6ple6tBr8Vw0NI8=
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
13 changes: 12 additions & 1 deletion itest/babylon_node_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package e2etest

import (
"bytes"
"encoding/hex"
"fmt"
"log"
"os"
Expand All @@ -11,6 +12,10 @@ import (
"testing"

"github.com/babylonlabs-io/babylon/types"
"github.com/btcsuite/btcd/btcutil"
"github.com/btcsuite/btcd/chaincfg"
"github.com/btcsuite/btcd/txscript"

"github.com/stretchr/testify/require"
)

Expand Down Expand Up @@ -120,6 +125,10 @@ func NewBabylonNodeHandler(t *testing.T, covenantPk *types.BIP340PubKey) *Babylo
nodeDataDir := filepath.Join(testDir, "node0", "babylond")

slashingAddr := "SZtRT4BySL3o4efdGLh3k7Kny8GAnsBrSW"
decodedAddr, err := btcutil.DecodeAddress(slashingAddr, &chaincfg.SimNetParams)
require.NoError(t, err)
pkScript, err := txscript.PayToAddrScript(decodedAddr)
require.NoError(t, err)

initTestnetCmd := exec.Command(
"babylond",
Expand All @@ -130,7 +139,9 @@ func NewBabylonNodeHandler(t *testing.T, covenantPk *types.BIP340PubKey) *Babylo
"--keyring-backend=test",
"--chain-id=chain-test",
"--additional-sender-account",
fmt.Sprintf("--slashing-address=%s", slashingAddr),
"--min-staking-time-blocks=100",
"--min-staking-amount-sat=10000",
fmt.Sprintf("--slashing-pk-script=%s", hex.EncodeToString(pkScript)),
fmt.Sprintf("--covenant-pks=%s", covenantPk.MarshalHex()),
"--covenant-quorum=1",
)
Expand Down
12 changes: 6 additions & 6 deletions itest/test_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,9 @@ type TestDelegationData struct {
DelegatorSig *bbntypes.BIP340Signature
FpPks []*btcec.PublicKey

SlashingAddr string
StakingTime uint16
StakingAmount int64
SlashingPkScript []byte
StakingTime uint16
StakingAmount int64
}

type testFinalityProviderData struct {
Expand Down Expand Up @@ -242,7 +242,7 @@ func (tm *TestManager) InsertBTCDelegation(t *testing.T, fpPks []*btcec.PublicKe
params.CovenantQuorum,
stakingTime,
stakingAmount,
params.SlashingAddress.String(),
params.SlashingPkScript,
params.SlashingRate,
unbondingTime,
)
Expand Down Expand Up @@ -305,7 +305,7 @@ func (tm *TestManager) InsertBTCDelegation(t *testing.T, fpPks []*btcec.PublicKe
wire.NewOutPoint(&stakingTxHash, 1),
unbondingTime,
unbondingValue,
params.SlashingAddress.String(),
params.SlashingPkScript,
params.SlashingRate,
unbondingTime,
)
Expand Down Expand Up @@ -353,7 +353,7 @@ func (tm *TestManager) InsertBTCDelegation(t *testing.T, fpPks []*btcec.PublicKe
SlashingTx: testStakingInfo.SlashingTx,
StakingTxInfo: txInfo,
DelegatorSig: delegatorSig,
SlashingAddr: params.SlashingAddress.String(),
SlashingPkScript: params.SlashingPkScript,
StakingTime: stakingTime,
StakingAmount: stakingAmount,
}
Expand Down
27 changes: 18 additions & 9 deletions testutil/datagen.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package testutil

import (
"encoding/hex"
"math"
"math/rand"
"testing"
"time"
Expand Down Expand Up @@ -50,6 +51,10 @@ func GenValidSlashingRate(r *rand.Rand) sdkmath.LegacyDec {
return sdkmath.LegacyNewDecWithPrec(int64(datagen.RandomInt(r, 41)+10), 2)
}

func RandRange(r *rand.Rand, min, max int) int {
return rand.Intn(max+1-min) + min
}

func GenRandomParams(r *rand.Rand, t *testing.T) *types.StakingParams {
covThreshold := datagen.RandomInt(r, 5) + 1
covNum := covThreshold * 2
Expand All @@ -62,15 +67,23 @@ func GenRandomParams(r *rand.Rand, t *testing.T) *types.StakingParams {

slashingAddr, err := datagen.GenRandomBTCAddress(r, &chaincfg.SimNetParams)
require.NoError(t, err)
slashingPkScript, err := txscript.PayToAddrScript(slashingAddr)
require.NoError(t, err)

return &types.StakingParams{
ComfirmationTimeBlocks: 10,
FinalizationTimeoutBlocks: 100,
MinUnbondingTime: 100,
MinSlashingTxFeeSat: 1,
CovenantPks: covenantPks,
SlashingAddress: slashingAddr,
SlashingPkScript: slashingPkScript,
CovenantQuorum: uint32(covThreshold),
SlashingRate: GenValidSlashingRate(r),
UnbondingFee: btcutil.Amount(1000),
MinStakingTime: 10,
MaxStakingTime: math.MaxUint16,
MinStakingValue: btcutil.Amount(10000),
MaxStakingValue: btcutil.Amount(10 * 10e8),
}
}

Expand All @@ -95,7 +108,7 @@ func GenBTCStakingSlashingInfo(
covenantQuorum uint32,
stakingTimeBlocks uint16,
stakingValue int64,
slashingAddress string,
slashingPkScript []byte,
slashingRate sdkmath.LegacyDec,
slashingChangeLockTime uint16,
) *TestStakingSlashingInfo {
Expand All @@ -114,7 +127,7 @@ func GenBTCStakingSlashingInfo(
covenantQuorum,
stakingTimeBlocks,
stakingValue,
slashingAddress,
slashingPkScript,
slashingRate,
slashingChangeLockTime,
)
Expand All @@ -140,7 +153,7 @@ func GenBTCStakingSlashingInfoWithOutPoint(
covenantQuorum uint32,
stakingTimeBlocks uint16,
stakingValue int64,
slashingAddress string,
slashingPkScript []byte,
slashingRate sdkmath.LegacyDec,
slashingChangeLockTime uint16,
) *TestStakingSlashingInfo {
Expand Down Expand Up @@ -170,14 +183,10 @@ func GenBTCStakingSlashingInfoWithOutPoint(
tx.AddTxIn(txIn)
tx.AddTxOut(stakingInfo.StakingOutput)

// construct slashing tx
slashingAddrBtc, err := btcutil.DecodeAddress(slashingAddress, btcNet)
require.NoError(t, err)

slashingMsgTx, err := btcstaking.BuildSlashingTxFromStakingTxStrict(
tx,
uint32(1),
slashingAddrBtc,
slashingPkScript,
stakerSK.PubKey(),
slashingChangeLockTime,
2000,
Expand Down
2 changes: 1 addition & 1 deletion tools/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ go 1.21

toolchain go1.21.4

require github.com/babylonlabs-io/babylon v0.9.0
require github.com/babylonlabs-io/babylon v0.9.3-0.20240904061947-c170f5d15db0

require (
cloud.google.com/go v0.112.0 // indirect
Expand Down
4 changes: 2 additions & 2 deletions tools/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -268,8 +268,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.20240904061947-c170f5d15db0 h1:xw1zphFUosCd7Ib/5YXTX5QE+jnJMgAwrlvPK7SWlDQ=
github.com/babylonlabs-io/babylon v0.9.3-0.20240904061947-c170f5d15db0/go.mod h1:9VUUAwVaalXiDdPZT65SPoawKWpp6ple6tBr8Vw0NI8=
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
19 changes: 17 additions & 2 deletions types/params.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ type StakingParams struct {
// Bitcoin public keys of the covenant committee
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

// Minimum number of signatures needed for the covenant multisignature
CovenantQuorum uint32
Expand All @@ -32,6 +32,21 @@ type StakingParams struct {

// The minimum time for unbonding transaction timelock in BTC blocks
MinUnbondingTime uint32

// 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
}

// MinimumUnbondingTime returns the minimum unbonding time. It is the bigger value from:
Expand Down
Loading