Skip to content

Commit

Permalink
chore: Remove dependency on finality-provider (#13)
Browse files Browse the repository at this point in the history
  • Loading branch information
gitferry authored Jan 24, 2024
1 parent a5153a0 commit 2ae37d9
Show file tree
Hide file tree
Showing 8 changed files with 115 additions and 429 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,7 @@ RPCAddr = http://127.0.0.1:26657
GRPCAddr = https://127.0.0.1:9090

# Name of the key in the keyring to use for signing transactions
Key = <finality-provider-key-name>
Key = <covenant-emulator-key-name>

# Type of keyring to use,
# supported backends - (os|file|kwallet|pass|test|memory)
Expand Down
125 changes: 24 additions & 101 deletions clientcontroller/babylon.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,14 @@ import (
"fmt"
"time"

sdkmath "cosmossdk.io/math"
"github.com/btcsuite/btcd/btcec/v2"
stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types"

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"
finalitytypes "github.com/babylonchain/babylon/x/finality/types"
bbnclient "github.com/babylonchain/rpc-client/client"
"github.com/btcsuite/btcd/btcec/v2/schnorr"
"github.com/btcsuite/btcd/btcutil"
Expand Down Expand Up @@ -126,6 +127,7 @@ func (bc *BabylonController) QueryStakingParams() (*types.StakingParams, error)
SlashingAddress: slashingAddress,
CovenantQuorum: stakingParamRes.Params.CovenantQuorum,
SlashingRate: stakingParamRes.Params.SlashingRate,
MinComissionRate: stakingParamRes.Params.MinCommissionRate,
MinUnbondingTime: stakingParamRes.Params.MinUnbondingTime,
}, nil
}
Expand Down Expand Up @@ -175,6 +177,10 @@ func (bc *BabylonController) QueryPendingDelegations(limit uint64) ([]*types.Del
return bc.queryDelegationsWithStatus(btcstakingtypes.BTCDelegationStatus_PENDING, limit)
}

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

// queryDelegationsWithStatus queries BTC delegations that need a Covenant signature
// with the given status (either pending or unbonding)
// it is only used when the program is running in Covenant mode
Expand All @@ -196,75 +202,6 @@ func (bc *BabylonController) queryDelegationsWithStatus(status btcstakingtypes.B
return dels, nil
}

func (bc *BabylonController) getNDelegations(
fpBtcPk *bbntypes.BIP340PubKey,
startKey []byte,
n uint64,
) ([]*types.Delegation, []byte, error) {
pagination := &sdkquery.PageRequest{
Key: startKey,
Limit: n,
}

res, err := bc.bbnClient.QueryClient.FinalityProviderDelegations(fpBtcPk.MarshalHex(), pagination)

if err != nil {
return nil, nil, fmt.Errorf("failed to query BTC delegations: %v", err)
}

var delegations []*types.Delegation

for _, dels := range res.BtcDelegatorDelegations {
for _, d := range dels.Dels {
delegations = append(delegations, ConvertDelegationType(d))
}
}

var nextKey []byte

if res.Pagination != nil && res.Pagination.NextKey != nil {
nextKey = res.Pagination.NextKey
}

return delegations, nextKey, nil
}

func (bc *BabylonController) getNFinalityProviderDelegationsMatchingCriteria(
fpBtcPk *bbntypes.BIP340PubKey,
n uint64,
match func(*types.Delegation) bool,
) ([]*types.Delegation, error) {
batchSize := 100
var delegations []*types.Delegation
var startKey []byte

for {
dels, nextKey, err := bc.getNDelegations(fpBtcPk, startKey, uint64(batchSize))
if err != nil {
return nil, err
}

for _, del := range dels {
if match(del) {
delegations = append(delegations, del)
}
}

if len(delegations) >= int(n) || len(nextKey) == 0 {
break
}

startKey = nextKey
}

if len(delegations) > int(n) {
// only return requested number of delegations
return delegations[:n], nil
} else {
return delegations, nil
}
}

func getContextWithCancel(timeout time.Duration) (context.Context, context.CancelFunc) {
ctx, cancel := context.WithTimeout(context.Background(), timeout)
return ctx, cancel
Expand Down Expand Up @@ -425,6 +362,23 @@ func (bc *BabylonController) CreateBTCDelegation(
return &types.TxResponse{TxHash: res.TxHash}, nil
}

// Register a finality provider to Babylon
// Currently this is only used for e2e tests, probably does not need to add it into the interface
func (bc *BabylonController) RegisterFinalityProvider(
bbnPubKey *secp256k1.PubKey, btcPubKey *bbntypes.BIP340PubKey, commission *sdkmath.LegacyDec,
description *stakingtypes.Description, pop *btcstakingtypes.ProofOfPossession) (*provider.RelayerTxResponse, error) {
registerMsg := &btcstakingtypes.MsgCreateFinalityProvider{
Signer: bc.mustGetTxSigner(),
Commission: commission,
BabylonPk: bbnPubKey,
BtcPk: btcPubKey,
Description: description,
Pop: pop,
}

return bc.reliablySendMsgs([]sdk.Msg{registerMsg})
}

// 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) {
Expand Down Expand Up @@ -492,34 +446,3 @@ 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) QueryFinalityProviderDelegations(fpBtcPk *bbntypes.BIP340PubKey, max uint64) ([]*types.Delegation, error) {
return bc.getNFinalityProviderDelegationsMatchingCriteria(
fpBtcPk,
max,
// fitlering function which always returns true as we want all delegations
func(*types.Delegation) bool { return true },
)
}

// 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) {
ctx, cancel := getContextWithCancel(bc.cfg.Timeout)
defer cancel()

clientCtx := sdkclient.Context{Client: bc.bbnClient.RPCClient}

queryClient := finalitytypes.NewQueryClient(clientCtx)

// query all the unsigned delegations
queryRequest := &finalitytypes.QueryVotesAtHeightRequest{
Height: height,
}
res, err := queryClient.VotesAtHeight(ctx, queryRequest)
if err != nil {
return nil, fmt.Errorf("failed to query BTC delegations: %w", err)
}

return res.BtcPks, nil
}
2 changes: 0 additions & 2 deletions 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/finality-provider v0.0.0-20240122055808-e6ec67113cd3
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 Expand Up @@ -210,7 +209,6 @@ require (
github.com/zondax/ledger-go v0.14.3 // indirect
go.etcd.io/bbolt v1.3.8 // indirect
go.opencensus.io v0.24.0 // indirect
go.uber.org/atomic v1.10.0 // indirect
go.uber.org/multierr v1.11.0 // indirect
golang.org/x/crypto v0.17.0 // indirect
golang.org/x/exp v0.0.0-20231006140011-7918f672742d // indirect
Expand Down
4 changes: 0 additions & 4 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/finality-provider v0.0.0-20240122055808-e6ec67113cd3 h1:DyEAnimHfqIzCIbh//5o5ptV+n8BKhbli2IiEQjiew0=
github.com/babylonchain/finality-provider v0.0.0-20240122055808-e6ec67113cd3/go.mod h1:Y1kf/JbrAii51B6BcFR80bUKXnfnxsNP1fVo8oc1nqk=
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 Expand Up @@ -1184,8 +1182,6 @@ go.uber.org/atomic v1.3.2/go.mod h1:gD2HeocX3+yG+ygLZcrzQJaqmWj9AIm7n08wl/qW/PE=
go.uber.org/atomic v1.4.0/go.mod h1:gD2HeocX3+yG+ygLZcrzQJaqmWj9AIm7n08wl/qW/PE=
go.uber.org/atomic v1.5.0/go.mod h1:sABNBOSYdrvTF6hTgEIbc7YasKWGhgEQZyfxyTvoXHQ=
go.uber.org/atomic v1.7.0/go.mod h1:fEN4uk6kAWBTFdckzkM89CLk9XfWZrxpCo0nPH17wJc=
go.uber.org/atomic v1.10.0 h1:9qC72Qh0+3MqyJbAn8YU5xVq1frD8bn3JtD2oXtafVQ=
go.uber.org/atomic v1.10.0/go.mod h1:LUxbIzbOniOlMKjJjyPfpl4v+PKK2cNJn91OQbhoJI0=
go.uber.org/goleak v1.1.10/go.mod h1:8a7PlsEVH3e/a/GLqe5IIrQx6GzcnRmZEufDUTk4A7A=
go.uber.org/goleak v1.2.0 h1:xqgm/S+aQvhWFTtR0XK3Jvg7z8kGV8P4X14IzwN3Eqk=
go.uber.org/goleak v1.2.0/go.mod h1:XJYK+MuIchqpmGmUSAzotztawfKvYLUIgg7guXrwVUo=
Expand Down
24 changes: 5 additions & 19 deletions itest/e2e_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,41 +5,27 @@ package e2etest

import (
"testing"

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

var (
stakingTime = uint16(100)
stakingAmount = int64(20000)
)

// TestCovenantEmulatorLifeCycleWithFinalityProvider tests the whole life cycle of a finality-provider
// TestCovenantEmulatorLifeCycle tests the whole life cycle of a finality-provider
// creation -> registration -> randomness commitment ->
// activation with BTC delegation and Covenant sig ->
// vote submission -> block finalization
func TestCovenantEmulatorLifeCycleWithFinalityProvider(t *testing.T) {
tm, fpInsList := StartManagerWithFinalityProvider(t, 1)
func TestCovenantEmulatorLifeCycle(t *testing.T) {
tm, btcPks := StartManagerWithFinalityProvider(t, 1)
defer tm.Stop(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, btcPks, stakingTime, stakingAmount)

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

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

// check the last voted block is finalized
lastVotedHeight := tm.WaitForFpVoteCast(t, fpIns)
tm.CheckBlockFinalization(t, lastVotedHeight, 1)
t.Logf("the block at height %v is finalized", lastVotedHeight)
_ = tm.WaitForNActiveDels(t, 1)
}
49 changes: 0 additions & 49 deletions itest/eotsmanager_handler.go

This file was deleted.

Loading

0 comments on commit 2ae37d9

Please sign in to comment.