Skip to content

Commit

Permalink
chore: Bump babylon with checking of slashing time lock (#6)
Browse files Browse the repository at this point in the history
  • Loading branch information
gitferry authored Jan 9, 2024
1 parent 99c47b7 commit 2586ba2
Show file tree
Hide file tree
Showing 10 changed files with 45 additions and 35 deletions.
2 changes: 1 addition & 1 deletion clientcontroller/babylon.go
Original file line number Diff line number Diff line change
Expand Up @@ -324,6 +324,7 @@ func ConvertDelegationType(del *btcstakingtypes.BTCDelegation) *types.Delegation
StakingTxHex: stakingTxHex,
SlashingTxHex: slashingTxHex,
CovenantSigs: covenantSigs,
UnbondingTime: del.UnbondingTime,
BtcUndelegation: undelegation,
}
}
Expand Down Expand Up @@ -373,7 +374,6 @@ func ConvertUndelegationType(undel *btcstakingtypes.BTCUndelegation) *types.Unde
SlashingTxHex: slashingTxHex,
CovenantSlashingSigs: covenantSlashingSigs,
CovenantUnbondingSigs: covenantUnbondingSigs,
UnbondingTime: undel.UnbondingTime,
DelegatorUnbondingSig: undel.DelegatorUnbondingSig,
}
}
Expand Down
8 changes: 6 additions & 2 deletions covenant/covenant.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ func (ce *CovenantEmulator) AddCovenantSignatures(btcDel *types.Delegation) (*ty
// which is larger value from:
// - MinUnbondingTime
// - CheckpointFinalizationTimeout
unbondingTime := btcDel.BtcUndelegation.UnbondingTime
unbondingTime := btcDel.UnbondingTime
minUnbondingTime := ce.params.MinUnbondingTime
if unbondingTime <= minUnbondingTime {
return nil, fmt.Errorf("unbonding time %d must be larger than %d",
Expand Down Expand Up @@ -158,6 +158,8 @@ func (ce *CovenantEmulator) AddCovenantSignatures(btcDel *types.Delegation) (*ty
int64(ce.params.MinSlashingTxFeeSat),
ce.params.SlashingRate,
ce.params.SlashingAddress,
btcDel.BtcPk,
uint16(unbondingTime),
&ce.config.BTCNetParams,
); err != nil {
return nil, fmt.Errorf("invalid txs in the delegation: %w", err)
Expand All @@ -179,7 +181,7 @@ func (ce *CovenantEmulator) AddCovenantSignatures(btcDel *types.Delegation) (*ty
btcDel.FpBtcPks,
ce.params.CovenantPks,
ce.params.CovenantQuorum,
uint16(btcDel.BtcUndelegation.UnbondingTime),
uint16(unbondingTime),
btcutil.Amount(unbondingMsgTx.TxOut[0].Value),
&ce.config.BTCNetParams,
)
Expand All @@ -194,6 +196,8 @@ func (ce *CovenantEmulator) AddCovenantSignatures(btcDel *types.Delegation) (*ty
int64(ce.params.MinSlashingTxFeeSat),
ce.params.SlashingRate,
ce.params.SlashingAddress,
btcDel.BtcPk,
uint16(unbondingTime),
&ce.config.BTCNetParams,
)
if err != nil {
Expand Down
11 changes: 5 additions & 6 deletions covenant/covenant_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,12 +55,11 @@ func FuzzAddCovenantSig(f *testing.F) {
require.NoError(t, err)

// generate BTC delegation
changeAddr, err := datagen.GenRandomBTCAddress(r, &chaincfg.SimNetParams)
require.NoError(t, err)
delSK, delPK, err := datagen.GenRandomBTCKeyPair(r)
require.NoError(t, err)
stakingTimeBlocks := uint16(5)
stakingValue := int64(2 * 10e8)
unbondingTime := uint16(params.MinimumUnbondingTime()) + 1
fpNum := datagen.RandomInt(r, 5) + 1
fpPks := testutil.GenBtcPublicKeys(r, t, int(fpNum))
testInfo := datagen.GenBTCStakingSlashingInfo(
Expand All @@ -74,8 +73,8 @@ func FuzzAddCovenantSig(f *testing.F) {
stakingTimeBlocks,
stakingValue,
params.SlashingAddress.String(),
changeAddr.String(),
params.SlashingRate,
unbondingTime,
)
stakingTxBytes, err := bbntypes.SerializeBTCTx(testInfo.StakingTx)
require.NoError(t, err)
Expand All @@ -86,6 +85,7 @@ func FuzzAddCovenantSig(f *testing.F) {
StartHeight: startHeight, // not relevant here
EndHeight: startHeight + uint64(stakingTimeBlocks),
TotalSat: uint64(stakingValue),
UnbondingTime: uint32(unbondingTime),
StakingTxHex: hex.EncodeToString(stakingTxBytes),
StakingOutputIdx: 0,
SlashingTxHex: testInfo.SlashingTx.ToHexStr(),
Expand All @@ -108,7 +108,6 @@ func FuzzAddCovenantSig(f *testing.F) {
}

// generate undelegation
unbondingTime := uint16(params.FinalizationTimeoutBlocks) + 1
unbondingValue := int64(btcDel.TotalSat) - 1000

stakingTxHash := testInfo.StakingTx.TxHash()
Expand All @@ -123,8 +122,9 @@ func FuzzAddCovenantSig(f *testing.F) {
wire.NewOutPoint(&stakingTxHash, 0),
unbondingTime,
unbondingValue,
params.SlashingAddress.String(), changeAddr.String(),
params.SlashingAddress.String(),
params.SlashingRate,
unbondingTime,
)
require.NoError(t, err)
// random signer
Expand All @@ -137,7 +137,6 @@ func FuzzAddCovenantSig(f *testing.F) {
require.NoError(t, err)
undel := &types.Undelegation{
UnbondingTxHex: hex.EncodeToString(serializedUnbondingTx),
UnbondingTime: uint32(unbondingTime),
SlashingTxHex: testUnbondingInfo.SlashingTx.ToHexStr(),
}
btcDel.BtcUndelegation = undel
Expand Down
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ require (
cosmossdk.io/math v1.2.0
github.com/avast/retry-go/v4 v4.5.1
github.com/babylonchain/babylon v0.7.2
github.com/babylonchain/finality-provider v0.0.0-20231222112306-5891f21cd080
github.com/babylonchain/finality-provider v0.0.0-20240105110631-3971eeaefe64
github.com/babylonchain/rpc-client v0.7.0
github.com/btcsuite/btcd v0.23.5-0.20230711222809-7faa9b266231
github.com/btcsuite/btcd/btcec/v2 v2.3.2
Expand Down Expand Up @@ -239,7 +239,7 @@ require (
)

replace (
github.com/babylonchain/babylon => github.com/babylonchain/babylon-private v0.0.0-20231224153404-79decc6a6866
github.com/babylonchain/babylon => github.com/babylonchain/babylon-private v0.0.0-20240105083612-dd4e4b1c0598
github.com/babylonchain/rpc-client => github.com/babylonchain/rpc-client-private v0.7.0-rc0.0.20231214053715-9de58555773a
github.com/gogo/protobuf => github.com/regen-network/protobuf v1.3.3-alpha.regen.1
github.com/syndtr/goleveldb => github.com/syndtr/goleveldb v1.0.1-0.20210819022825-2ae1ddf74ef7
Expand Down
8 changes: 4 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -279,10 +279,10 @@ 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-private v0.0.0-20231224153404-79decc6a6866 h1:PhcQKkrFX2F+NJHgzQUquLAB3hjvZ5ojEnilf/lZycI=
github.com/babylonchain/babylon-private v0.0.0-20231224153404-79decc6a6866/go.mod h1:qiavPBxKkbMeTXg0vaLpMw97Xw8Nb3kpzZJ0ptiYcss=
github.com/babylonchain/finality-provider v0.0.0-20231222112306-5891f21cd080 h1:p04I5UnuiaUALu+xioFxIR+VwBH1olAPSfS9aKY9yI4=
github.com/babylonchain/finality-provider v0.0.0-20231222112306-5891f21cd080/go.mod h1:81SxlAFLZWfCfmi+vqISIgSnRy5ehWyPBw8StWbrTB4=
github.com/babylonchain/babylon-private v0.0.0-20240105083612-dd4e4b1c0598 h1:ysEA08WGHlMm2pIoXgTt1O9YAW4QjBQXg3TM0CSn21g=
github.com/babylonchain/babylon-private v0.0.0-20240105083612-dd4e4b1c0598/go.mod h1:qiavPBxKkbMeTXg0vaLpMw97Xw8Nb3kpzZJ0ptiYcss=
github.com/babylonchain/finality-provider v0.0.0-20240105110631-3971eeaefe64 h1:5KXqgroUxfP7VPOPEhFGb3kra3mWXwX+cG4fYvjesMg=
github.com/babylonchain/finality-provider v0.0.0-20240105110631-3971eeaefe64/go.mod h1:FupoqbNfunaQUBgknLIyKvlM1+VUXBgkAwDveIlr7lM=
github.com/babylonchain/rpc-client-private v0.7.0-rc0.0.20231214053715-9de58555773a h1:IwntCUOlcsczQfl4P72ckqGf09coMdM42LvAG1aM+mg=
github.com/babylonchain/rpc-client-private v0.7.0-rc0.0.20231214053715-9de58555773a/go.mod h1:dnUVCa5yHq2giiSpBpXx7W3CbJuuMabvQi3hG81AAtM=
github.com/benbjohnson/clock v1.1.0/go.mod h1:J11/hYXuz8f4ySSvYwY0FKfm+ezbsZBKZxNJlLklBHA=
Expand Down
30 changes: 17 additions & 13 deletions itest/test_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ type TestManager struct {
EOTSClient *client.EOTSManagerGRpcClient
FPBBNClient *fpcc.BabylonController
CovBBNClient *covcc.BabylonController
StakingParams *types.StakingParams
baseDir string
}

Expand All @@ -79,7 +80,6 @@ type TestDelegationData struct {
FpPks []*btcec.PublicKey

SlashingAddr string
ChangeAddr string
StakingTime uint16
StakingAmount int64
}
Expand Down Expand Up @@ -151,9 +151,12 @@ func StartManager(t *testing.T) *TestManager {
func (tm *TestManager) WaitForServicesStart(t *testing.T) {
// wait for Babylon node starts
require.Eventually(t, func() bool {
_, err := tm.FPBBNClient.QueryStakingParams()

return err == nil
params, err := tm.CovBBNClient.QueryStakingParams()
if err != nil {
return false
}
tm.StakingParams = params
return true
}, eventuallyWaitTimeOut, eventuallyPollTime)

t.Logf("Babylon node is started")
Expand Down Expand Up @@ -244,7 +247,11 @@ func (tm *TestManager) WaitForFpRegistered(t *testing.T, bbnPk *secp256k1.PubKey

func (tm *TestManager) WaitForFpPubRandCommitted(t *testing.T, fpIns *service.FinalityProviderInstance) {
require.Eventually(t, func() bool {
return fpIns.GetLastCommittedHeight() > 0
lastCommittedHeight, err := fpIns.GetLastCommittedHeight()
if err != nil {
return false
}
return lastCommittedHeight > 0
}, eventuallyWaitTimeOut, eventuallyPollTime)

t.Logf("public randomness is successfully committed")
Expand Down Expand Up @@ -274,7 +281,7 @@ func (tm *TestManager) WaitForFpNActiveDels(t *testing.T, btcPk *bbntypes.BIP340
var dels []*types.Delegation
currentBtcTip, err := tm.FPBBNClient.QueryBtcLightClientTip()
require.NoError(t, err)
params, err := tm.FPBBNClient.QueryStakingParams()
params, err := tm.CovBBNClient.QueryStakingParams()
require.NoError(t, err)
require.Eventually(t, func() bool {
dels, err = tm.CovBBNClient.QueryFinalityProviderDelegations(btcPk, 1000)
Expand Down Expand Up @@ -368,9 +375,7 @@ func (tm *TestManager) InsertBTCDelegation(t *testing.T, fpPks []*btcec.PublicKe
delBtcPrivKey, delBtcPubKey, err := datagen.GenRandomBTCKeyPair(r)
require.NoError(t, err)

changeAddress, err := datagen.GenRandomBTCAddress(r, btcNetworkParams)
require.NoError(t, err)

unbondingTime := uint16(tm.StakingParams.MinimumUnbondingTime()) + 1
testStakingInfo := datagen.GenBTCStakingSlashingInfo(
r,
t,
Expand All @@ -381,8 +386,9 @@ func (tm *TestManager) InsertBTCDelegation(t *testing.T, fpPks []*btcec.PublicKe
params.CovenantQuorum,
stakingTime,
stakingAmount,
params.SlashingAddress.String(), changeAddress.String(),
params.SlashingAddress.String(),
params.SlashingRate,
unbondingTime,
)

// delegator Babylon key pairs
Expand Down Expand Up @@ -431,7 +437,6 @@ func (tm *TestManager) InsertBTCDelegation(t *testing.T, fpPks []*btcec.PublicKe
)
require.NoError(t, err)

unbondingTime := uint16(params.FinalizationTimeoutBlocks) + 1
unbondingValue := stakingAmount - 1000
stakingTxHash := testStakingInfo.StakingTx.TxHash()

Expand All @@ -447,8 +452,8 @@ func (tm *TestManager) InsertBTCDelegation(t *testing.T, fpPks []*btcec.PublicKe
unbondingTime,
unbondingValue,
params.SlashingAddress.String(),
changeAddress.String(),
params.SlashingRate,
unbondingTime,
)

unbondingTxMsg := testUnbondingInfo.UnbondingTx
Expand Down Expand Up @@ -498,7 +503,6 @@ func (tm *TestManager) InsertBTCDelegation(t *testing.T, fpPks []*btcec.PublicKe
StakingTxInfo: txInfo,
DelegatorSig: delegatorSig,
SlashingAddr: params.SlashingAddress.String(),
ChangeAddr: changeAddress.String(),
StakingTime: stakingTime,
StakingAmount: stakingAmount,
}
Expand Down
1 change: 1 addition & 0 deletions testutil/datagen.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ func GenRandomParams(r *rand.Rand, t *testing.T) *types.StakingParams {
return &types.StakingParams{
ComfirmationTimeBlocks: 10,
FinalizationTimeoutBlocks: 100,
MinUnbondingTime: 100,
MinSlashingTxFeeSat: 1,
CovenantPks: covenantPks,
SlashingAddress: slashingAddr,
Expand Down
4 changes: 2 additions & 2 deletions tools/go.mod
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
module github.com/babylonchain/finality-provider/tools
module github.com/babylonchain/covenant-emulator/tools

go 1.21

Expand Down Expand Up @@ -212,7 +212,7 @@ require (
)

replace (
github.com/babylonchain/babylon => github.com/babylonchain/babylon-private v0.0.0-20231224153404-79decc6a6866
github.com/babylonchain/babylon => github.com/babylonchain/babylon-private v0.0.0-20240105083612-dd4e4b1c0598

// Downgraded to stable version see: https://github.com/cosmos/cosmos-sdk/pull/14952
github.com/syndtr/goleveldb => github.com/syndtr/goleveldb v1.0.1-0.20210819022825-2ae1ddf74ef7
Expand Down
4 changes: 2 additions & 2 deletions tools/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -272,8 +272,8 @@ github.com/aws/aws-sdk-go v1.44.122/go.mod h1:y4AeaBuwd2Lk+GepC1E9v0qOiTws0MIWAX
github.com/aws/aws-sdk-go v1.44.224 h1:09CiaaF35nRmxrzWZ2uRq5v6Ghg/d2RiPjZnSgtt+RQ=
github.com/aws/aws-sdk-go v1.44.224/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-private v0.0.0-20231224153404-79decc6a6866 h1:PhcQKkrFX2F+NJHgzQUquLAB3hjvZ5ojEnilf/lZycI=
github.com/babylonchain/babylon-private v0.0.0-20231224153404-79decc6a6866/go.mod h1:qiavPBxKkbMeTXg0vaLpMw97Xw8Nb3kpzZJ0ptiYcss=
github.com/babylonchain/babylon-private v0.0.0-20240105083612-dd4e4b1c0598 h1:ysEA08WGHlMm2pIoXgTt1O9YAW4QjBQXg3TM0CSn21g=
github.com/babylonchain/babylon-private v0.0.0-20240105083612-dd4e4b1c0598/go.mod h1:qiavPBxKkbMeTXg0vaLpMw97Xw8Nb3kpzZJ0ptiYcss=
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
8 changes: 5 additions & 3 deletions types/delegation.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
package types

import (
bbn "github.com/babylonchain/babylon/types"
"math"

bbn "github.com/babylonchain/babylon/types"

"github.com/btcsuite/btcd/btcec/v2"
"github.com/btcsuite/btcd/btcec/v2/schnorr"
)
Expand All @@ -29,6 +30,9 @@ type Delegation struct {
StakingOutputIdx uint32
// The hex string of the slashing tx
SlashingTxHex string
// UnbondingTime describes how long the funds will be locked either in unbonding output
// or slashing change output
UnbondingTime uint32
// The signatures on the slashing tx
// by the covenants (i.e., SKs corresponding to covenant_pks in params)
// It will be a part of the witness for the staking tx output.
Expand Down Expand Up @@ -57,8 +61,6 @@ func (d *Delegation) GetStakingTime() uint16 {

// Undelegation signalizes that the delegation is being undelegated
type Undelegation struct {
// How long the funds will be locked in the unbonding output
UnbondingTime uint32
// The hex string of the transaction which will transfer the funds from staking
// output to unbonding output. Unbonding output will usually have lower timelock
// than staking output.
Expand Down

0 comments on commit 2586ba2

Please sign in to comment.