Skip to content

Commit

Permalink
chore: Remove dependency on covenant-emulator (#212)
Browse files Browse the repository at this point in the history
  • Loading branch information
gitferry authored Jan 22, 2024
1 parent 20979c2 commit d9a390f
Show file tree
Hide file tree
Showing 9 changed files with 369 additions and 175 deletions.
100 changes: 93 additions & 7 deletions clientcontroller/babylon.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"time"

"github.com/btcsuite/btcd/btcec/v2"
"github.com/btcsuite/btcd/btcutil"

"cosmossdk.io/math"
bbntypes "github.com/babylonchain/babylon/types"
Expand Down Expand Up @@ -360,7 +361,10 @@ func (bc *BabylonController) Close() error {
return bc.bbnClient.Stop()
}

// Currently this is only used for e2e tests, probably does not need to add it into the interface
/*
Implementations for e2e tests only
*/

func (bc *BabylonController) CreateBTCDelegation(
delBabylonPk *secp256k1.PubKey,
delBtcPk *bbntypes.BIP340PubKey,
Expand Down Expand Up @@ -407,8 +411,6 @@ func (bc *BabylonController) CreateBTCDelegation(
return &types.TxResponse{TxHash: res.TxHash}, 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) {
msg := &btclctypes.MsgInsertHeaders{
Signer: bc.mustGetTxSigner(),
Expand All @@ -423,8 +425,6 @@ func (bc *BabylonController) InsertBtcBlockHeaders(headers []bbntypes.BTCHeaderB
return res, nil
}

// QueryFinalityProvider queries finality providers
// Currently this is only used for e2e tests, probably does not need to add this into the interface
func (bc *BabylonController) QueryFinalityProviders() ([]*btcstakingtypes.FinalityProvider, error) {
var fps []*btcstakingtypes.FinalityProvider
pagination := &sdkquery.PageRequest{
Expand All @@ -447,7 +447,6 @@ func (bc *BabylonController) QueryFinalityProviders() ([]*btcstakingtypes.Finali
return fps, nil
}

// Currently this is only used for e2e tests, probably does not need to add this into the interface
func (bc *BabylonController) QueryBtcLightClientTip() (*btclctypes.BTCHeaderInfo, error) {
res, err := bc.bbnClient.QueryClient.BTCHeaderChainTip()
if err != nil {
Expand All @@ -457,7 +456,6 @@ func (bc *BabylonController) QueryBtcLightClientTip() (*btclctypes.BTCHeaderInfo
return res.Header, nil
}

// Currently this is only used for e2e tests, probably does not need to add this into the interface
func (bc *BabylonController) QueryVotesAtHeight(height uint64) ([]bbntypes.BIP340PubKey, error) {
res, err := bc.bbnClient.QueryClient.VotesAtHeight(height)
if err != nil {
Expand All @@ -466,3 +464,91 @@ func (bc *BabylonController) QueryVotesAtHeight(height uint64) ([]bbntypes.BIP34

return res.BtcPks, nil
}

func (bc *BabylonController) QueryPendingDelegations(limit uint64) ([]*btcstakingtypes.BTCDelegation, error) {
return bc.queryDelegationsWithStatus(btcstakingtypes.BTCDelegationStatus_PENDING, limit)
}

func (bc *BabylonController) QueryActiveDelegations(limit uint64) ([]*btcstakingtypes.BTCDelegation, error) {
return bc.queryDelegationsWithStatus(btcstakingtypes.BTCDelegationStatus_ACTIVE, limit)
}

// queryDelegationsWithStatus queries BTC delegations
// with the given status (either pending or unbonding)
// it is only used when the program is running in Covenant mode
func (bc *BabylonController) queryDelegationsWithStatus(status btcstakingtypes.BTCDelegationStatus, limit uint64) ([]*btcstakingtypes.BTCDelegation, error) {
pagination := &sdkquery.PageRequest{
Limit: limit,
}

res, err := bc.bbnClient.QueryClient.BTCDelegations(status, pagination)
if err != nil {
return nil, fmt.Errorf("failed to query BTC delegations: %v", err)
}

return res.BtcDelegations, nil
}

func (bc *BabylonController) QueryStakingParams() (*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()
if err != nil {
return nil, fmt.Errorf("failed to query staking params: %v", err)
}

covenantPks := make([]*btcec.PublicKey, 0, len(stakingParamRes.Params.CovenantPks))
for _, pk := range stakingParamRes.Params.CovenantPks {
covPk, err := pk.ToBTCPK()
if err != nil {
return nil, fmt.Errorf("invalid covenant public key")
}
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,
CovenantQuorum: stakingParamRes.Params.CovenantQuorum,
SlashingRate: stakingParamRes.Params.SlashingRate,
MinUnbondingTime: stakingParamRes.Params.MinUnbondingTime,
}, nil
}

func (bc *BabylonController) SubmitCovenantSigs(
covPk *btcec.PublicKey,
stakingTxHash string,
slashingSigs [][]byte,
unbondingSig *schnorr.Signature,
unbondingSlashingSigs [][]byte,
) (*types.TxResponse, error) {
bip340UnbondingSig := bbntypes.NewBIP340SignatureFromBTCSig(unbondingSig)

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

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

return &types.TxResponse{TxHash: res.TxHash, Events: res.Events}, nil
}
1 change: 0 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ require (
cosmossdk.io/math v1.2.0
github.com/avast/retry-go/v4 v4.5.1
github.com/babylonchain/babylon v0.8.0-rc.0
github.com/babylonchain/covenant-emulator v0.0.0-20240122065420-24422ab54cb6
github.com/babylonchain/rpc-client v0.7.0-rc0.0.20240121104942-d8a42431b1c2
github.com/btcsuite/btcd v0.23.5-0.20230711222809-7faa9b266231
github.com/btcsuite/btcd/btcec/v2 v2.3.2
Expand Down
2 changes: 0 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -280,8 +280,6 @@ github.com/aws/aws-sdk-go v1.44.312/go.mod h1:aVsgQcEevwlmQ7qHE9I3h+dtQgpqhFB+i8
github.com/aws/aws-sdk-go-v2 v0.18.0/go.mod h1:JWVYvqSMppoMJC0x5wdwiImzgXTI9FuZwxzkQq9wy+g=
github.com/babylonchain/babylon v0.8.0-rc.0 h1:zl2pyCpdvj+9tVuQ23qg/JqoywIbThwaxkdJcL5dGcM=
github.com/babylonchain/babylon v0.8.0-rc.0/go.mod h1:UJMehldQROFooqS5rXMd6avJSjwIsc0jpL+uKoOsTls=
github.com/babylonchain/covenant-emulator v0.0.0-20240122065420-24422ab54cb6 h1:NMgDiBBuA0xfcS8LTaP2Lpca9j6qWJnMfbQ2u1HYj2k=
github.com/babylonchain/covenant-emulator v0.0.0-20240122065420-24422ab54cb6/go.mod h1:Rz51BAn7ym3Nf53FZ0R+pJXMj5GPwD0alVqDR+f+Rjw=
github.com/babylonchain/rpc-client v0.7.0-rc0.0.20240121104942-d8a42431b1c2 h1:FWsneAs2tSact+VKJuwVtX6xjj10OtthA1np4AG2CQ4=
github.com/babylonchain/rpc-client v0.7.0-rc0.0.20240121104942-d8a42431b1c2/go.mod h1:3WkXcOZX2m0VvM4K6KshfiSQeTz3sSCjVw+NQ6Ayx2U=
github.com/benbjohnson/clock v1.1.0/go.mod h1:J11/hYXuz8f4ySSvYwY0FKfm+ezbsZBKZxNJlLklBHA=
Expand Down
36 changes: 16 additions & 20 deletions itest/babylon_node_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,28 +8,23 @@ import (
"os/exec"
"path/filepath"
"runtime"
"strings"
"testing"

"github.com/babylonchain/babylon/types"
"github.com/stretchr/testify/require"
)

type babylonNode struct {
cmd *exec.Cmd
pidFile string
dataDir string
chainID string
slashingAddr string
covenantPk *types.BIP340PubKey
cmd *exec.Cmd
pidFile string
dataDir string
}

func newBabylonNode(dataDir string, cmd *exec.Cmd, chainID string, slashingAddr string, covenantPk *types.BIP340PubKey) *babylonNode {
func newBabylonNode(dataDir string, cmd *exec.Cmd) *babylonNode {
return &babylonNode{
dataDir: dataDir,
cmd: cmd,
chainID: chainID,
slashingAddr: slashingAddr,
covenantPk: covenantPk,
dataDir: dataDir,
cmd: cmd,
}
}

Expand Down Expand Up @@ -107,7 +102,7 @@ type BabylonNodeHandler struct {
babylonNode *babylonNode
}

func NewBabylonNodeHandler(t *testing.T, covenantPk *types.BIP340PubKey) *BabylonNodeHandler {
func NewBabylonNodeHandler(t *testing.T, covenantQuorum int, covenantPks []*types.BIP340PubKey) *BabylonNodeHandler {
testDir, err := baseDir("zBabylonTest")
require.NoError(t, err)
defer func() {
Expand All @@ -121,6 +116,11 @@ func NewBabylonNodeHandler(t *testing.T, covenantPk *types.BIP340PubKey) *Babylo

slashingAddr := "SZtRT4BySL3o4efdGLh3k7Kny8GAnsBrSW"

var covenantPksStr []string
for _, pk := range covenantPks {
covenantPksStr = append(covenantPksStr, pk.MarshalHex())
}

initTestnetCmd := exec.Command(
"babylond",
"testnet",
Expand All @@ -131,8 +131,8 @@ func NewBabylonNodeHandler(t *testing.T, covenantPk *types.BIP340PubKey) *Babylo
"--chain-id=chain-test",
"--additional-sender-account",
fmt.Sprintf("--slashing-address=%s", slashingAddr),
fmt.Sprintf("--covenant-pks=%s", covenantPk.MarshalHex()),
"--covenant-quorum=1",
fmt.Sprintf("--covenant-quorum=%d", covenantQuorum),
fmt.Sprintf("--covenant-pks=%s", strings.Join(covenantPksStr, ",")),
)

var stderr bytes.Buffer
Expand All @@ -157,7 +157,7 @@ func NewBabylonNodeHandler(t *testing.T, covenantPk *types.BIP340PubKey) *Babylo
startCmd.Stdout = f

return &BabylonNodeHandler{
babylonNode: newBabylonNode(testDir, startCmd, chainID, slashingAddr, covenantPk),
babylonNode: newBabylonNode(testDir, startCmd),
}
}

Expand All @@ -182,7 +182,3 @@ func (w *BabylonNodeHandler) GetNodeDataDir() string {
dir := filepath.Join(w.babylonNode.dataDir, "node0", "babylond")
return dir
}

func (w *BabylonNodeHandler) GetCovenantPk() *types.BIP340PubKey {
return w.babylonNode.covenantPk
}
59 changes: 32 additions & 27 deletions itest/e2e_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,19 +31,20 @@ func TestFinalityProviderLifeCycle(t *testing.T) {

fpIns := fpInsList[0]

params := tm.GetParams(t)

// check the public randomness is committed
tm.WaitForFpPubRandCommitted(t, fpIns)

// send a BTC delegation
_ = tm.InsertBTCDelegation(t, []*btcec.PublicKey{fpIns.MustGetBtcPk()}, stakingTime, stakingAmount, params)
_ = tm.InsertBTCDelegation(t, []*btcec.PublicKey{fpIns.MustGetBtcPk()}, stakingTime, stakingAmount)

// check the BTC delegation is pending
_ = tm.WaitForNPendingDels(t, 1)
dels := tm.WaitForNPendingDels(t, 1)

// send covenant sigs
tm.InsertCovenantSigForDelegation(t, dels[0])

// check the BTC delegation is active
_ = tm.WaitForFpNActiveDels(t, fpIns.GetBtcPkBIP340(), 1)
_ = tm.WaitForNActiveDels(t, 1)

// check the last voted block is finalized
lastVotedHeight := tm.WaitForFpVoteCast(t, fpIns)
Expand All @@ -60,19 +61,20 @@ func TestDoubleSigning(t *testing.T) {

fpIns := fpInsList[0]

params := tm.GetParams(t)

// check the public randomness is committed
tm.WaitForFpPubRandCommitted(t, fpIns)

// send a BTC delegation
_ = tm.InsertBTCDelegation(t, []*btcec.PublicKey{fpIns.MustGetBtcPk()}, stakingTime, stakingAmount, params)
_ = tm.InsertBTCDelegation(t, []*btcec.PublicKey{fpIns.MustGetBtcPk()}, stakingTime, stakingAmount)

// check the BTC delegation is pending
_ = tm.WaitForNPendingDels(t, 1)
dels := tm.WaitForNPendingDels(t, 1)

// send covenant sigs
tm.InsertCovenantSigForDelegation(t, dels[0])

// check the BTC delegation is active
_ = tm.WaitForFpNActiveDels(t, fpIns.GetBtcPkBIP340(), 1)
_ = tm.WaitForNActiveDels(t, 1)

// check the last voted block is finalized
lastVotedHeight := tm.WaitForFpVoteCast(t, fpIns)
Expand Down Expand Up @@ -111,30 +113,31 @@ func TestMultipleFinalityProviders(t *testing.T) {
tm, fpInstances := StartManagerWithFinalityProvider(t, n)
defer tm.Stop(t)

params := tm.GetParams(t)

// submit BTC delegations for each finality-provider
for _, fpIns := range fpInstances {
tm.Wg.Add(1)
go func(fpi *service.FinalityProviderInstance) {
defer tm.Wg.Done()
// check the public randomness is committed
tm.WaitForFpPubRandCommitted(t, fpi)

// send a BTC delegation
_ = tm.InsertBTCDelegation(t, []*btcec.PublicKey{fpi.MustGetBtcPk()}, stakingTime, stakingAmount, params)
_ = tm.InsertBTCDelegation(t, []*btcec.PublicKey{fpi.MustGetBtcPk()}, stakingTime, stakingAmount)
}(fpIns)
}
tm.Wg.Wait()

for _, fpIns := range fpInstances {
tm.Wg.Add(1)
go func(fpi *service.FinalityProviderInstance) {
defer tm.Wg.Done()
_ = tm.WaitForFpNActiveDels(t, fpi.GetBtcPkBIP340(), 1)
}(fpIns)
// check the BTC delegations are pending
dels := tm.WaitForNPendingDels(t, n)
require.Equal(t, n, len(dels))

// send covenant sigs to each of the delegations
for _, d := range dels {
// send covenant sigs
tm.InsertCovenantSigForDelegation(t, d)
}
tm.Wg.Wait()

// check the BTC delegations are active
_ = tm.WaitForNActiveDels(t, n)

// check there's a block finalized
_ = tm.WaitForNFinalizedBlocks(t, 1)
Expand All @@ -147,18 +150,20 @@ func TestFastSync(t *testing.T) {

fpIns := fpInsList[0]

params := tm.GetParams(t)

// check the public randomness is committed
tm.WaitForFpPubRandCommitted(t, fpIns)

// send a BTC delegation
_ = tm.InsertBTCDelegation(t, []*btcec.PublicKey{fpIns.MustGetBtcPk()}, stakingTime, stakingAmount, params)
_ = tm.InsertBTCDelegation(t, []*btcec.PublicKey{fpIns.MustGetBtcPk()}, stakingTime, stakingAmount)

// check the BTC delegation is pending
_ = tm.WaitForNPendingDels(t, 1)
dels := tm.WaitForNPendingDels(t, 1)

_ = tm.WaitForFpNActiveDels(t, fpIns.GetBtcPkBIP340(), 1)
// send covenant sigs
tm.InsertCovenantSigForDelegation(t, dels[0])

// check the BTC delegation is active
_ = tm.WaitForNActiveDels(t, 1)

// check the last voted block is finalized
lastVotedHeight := tm.WaitForFpVoteCast(t, fpIns)
Expand All @@ -180,7 +185,7 @@ func TestFastSync(t *testing.T) {
t.Logf("the latest finalized block is at %v", finalizedHeight)

// check if the fast sync works by checking if the gap is not more than 1
currentHeaderRes, err := tm.FPBBNClient.QueryBestBlock()
currentHeaderRes, err := tm.BBNClient.QueryBestBlock()
currentHeight := currentHeaderRes.Height
t.Logf("the current block is at %v", currentHeight)
require.NoError(t, err)
Expand Down
Loading

0 comments on commit d9a390f

Please sign in to comment.