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

chore: add pagination to queryDelegationsWithStatus #96

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 4 commits
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/)
* [#83](https://github.com/babylonlabs-io/covenant-emulator/pull/83) covenant-signer: remove go.mod
* [#95](https://github.com/babylonlabs-io/covenant-emulator/pull/95) removed local signer option
as the covenant emulator should only connect to a remote signer
* [#96](https://github.com/babylonlabs-io/covenant-emulator/pull/96) add pagination to `queryDelegationsWithStatus`

## v0.11.3

Expand Down
42 changes: 30 additions & 12 deletions clientcontroller/babylon.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,10 @@ import (
"github.com/babylonlabs-io/covenant-emulator/types"
)

var _ ClientController = &BabylonController{}
var (
_ ClientController = &BabylonController{}
MaxPaginationLimit = uint64(1000)
)

type BabylonController struct {
bbnClient *bbnclient.Client
Expand Down Expand Up @@ -198,24 +201,39 @@ func (bc *BabylonController) QueryVerifiedDelegations(limit uint64) ([]*types.De
// 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
func (bc *BabylonController) queryDelegationsWithStatus(status btcstakingtypes.BTCDelegationStatus, limit uint64) ([]*types.Delegation, error) {
func (bc *BabylonController) queryDelegationsWithStatus(status btcstakingtypes.BTCDelegationStatus, delsLimit uint64) ([]*types.Delegation, error) {
pgLimit := min(MaxPaginationLimit, delsLimit)
pagination := &sdkquery.PageRequest{
Limit: limit,
Limit: pgLimit,
}

res, err := bc.bbnClient.QueryClient.BTCDelegations(status, pagination)
if err != nil {
return nil, fmt.Errorf("failed to query BTC delegations: %v", err)
}
dels := make([]*types.Delegation, 0, delsLimit)
indexDels := uint64(0)

dels := make([]*types.Delegation, 0, len(res.BtcDelegations))
for _, delResp := range res.BtcDelegations {
del, err := DelegationRespToDelegation(delResp)
for indexDels < delsLimit {
res, err := bc.bbnClient.QueryClient.BTCDelegations(status, pagination)
if err != nil {
return nil, err
return nil, fmt.Errorf("failed to query BTC delegations: %v", err)
}

for _, delResp := range res.BtcDelegations {
del, err := DelegationRespToDelegation(delResp)
if err != nil {
return nil, err
}

dels = append(dels, del)
RafilxTenfen marked this conversation as resolved.
Show resolved Hide resolved
indexDels++

if indexDels == delsLimit {
return dels, nil
}
}

dels = append(dels, del)
if uint64(len(res.BtcDelegations)) != pgLimit {
return dels, nil
}
pagination.Key = res.Pagination.NextKey
}

return dels, nil
Expand Down
7 changes: 7 additions & 0 deletions covenant-signer/keystore/cosmos/keyringcontroller.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/crypto/keyring"
sdksecp256k1 "github.com/cosmos/cosmos-sdk/crypto/keys/secp256k1"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/go-bip39"
)

Expand All @@ -18,6 +19,7 @@ const (

type ChainKeyInfo struct {
Name string
Address sdk.AccAddress
Mnemonic string
PublicKey *btcec.PublicKey
PrivateKey *btcec.PrivateKey
Expand Down Expand Up @@ -98,13 +100,18 @@ func (kc *ChainKeyringController) CreateChainKey(passphrase, hdPath string) (*Ch
return nil, err
}

addr, err := record.GetAddress()
if err != nil {
return nil, err
}
privKey := record.GetLocal().PrivKey.GetCachedValue()

switch v := privKey.(type) {
case *sdksecp256k1.PrivKey:
sk, pk := btcec.PrivKeyFromBytes(v.Key)
return &ChainKeyInfo{
Name: kc.keyName,
Address: addr,
PublicKey: pk,
PrivateKey: sk,
Mnemonic: mnemonic,
Expand Down
10 changes: 6 additions & 4 deletions itest/babylon_node_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,16 @@ type babylonNode struct {
cmd *exec.Cmd
pidFile string
dataDir string
nodeHome string
chainID string
slashingAddr string
covenantPk *types.BIP340PubKey
}

func newBabylonNode(dataDir string, cmd *exec.Cmd, chainID string, slashingAddr string, covenantPk *types.BIP340PubKey) *babylonNode {
func newBabylonNode(dataDir, nodeHome string, cmd *exec.Cmd, chainID string, slashingAddr string, covenantPk *types.BIP340PubKey) *babylonNode {
return &babylonNode{
dataDir: dataDir,
nodeHome: nodeHome,
cmd: cmd,
chainID: chainID,
slashingAddr: slashingAddr,
Expand Down Expand Up @@ -122,7 +124,7 @@ func NewBabylonNodeHandler(t *testing.T, covenantPk *types.BIP340PubKey) *Babylo
}
}()

nodeDataDir := filepath.Join(testDir, "node0", "babylond")
nodeHome := filepath.Join(testDir, "node0", "babylond")

slashingAddr := "SZtRT4BySL3o4efdGLh3k7Kny8GAnsBrSW"
decodedAddr, err := btcutil.DecodeAddress(slashingAddr, &chaincfg.SimNetParams)
Expand Down Expand Up @@ -164,14 +166,14 @@ func NewBabylonNodeHandler(t *testing.T, covenantPk *types.BIP340PubKey) *Babylo
startCmd := exec.Command(
"babylond",
"start",
fmt.Sprintf("--home=%s", nodeDataDir),
fmt.Sprintf("--home=%s", nodeHome),
"--log_level=debug",
)

startCmd.Stdout = f

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

Expand Down
18 changes: 18 additions & 0 deletions itest/e2e_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"testing"
"time"

"github.com/babylonlabs-io/covenant-emulator/clientcontroller"
"github.com/stretchr/testify/require"
)

Expand Down Expand Up @@ -48,3 +49,20 @@ func TestCovenantEmulatorLifeCycle(t *testing.T) {
require.NoError(t, err)
require.Empty(t, res)
}

func TestQueryPendingDelegations(t *testing.T) {
tm, btcPks := StartManagerWithFinalityProvider(t, 1)
defer tm.Stop(t)

// manually sets the pg to a low value
clientcontroller.MaxPaginationLimit = 2

numDels := 3
for i := 0; i < numDels; i++ {
_ = tm.InsertBTCDelegation(t, btcPks, stakingTime, stakingAmount, false)
}

dels, err := tm.CovBBNClient.QueryPendingDelegations(uint64(numDels))
require.NoError(t, err)
require.Len(t, dels, numDels)
}
19 changes: 19 additions & 0 deletions itest/test_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"fmt"
"math/rand"
"os"
"os/exec"
"sync"
"testing"
"time"
Expand Down Expand Up @@ -169,6 +170,7 @@ func StartManager(t *testing.T) *TestManager {
}

tm.WaitForServicesStart(t)
tm.SendToAddr(t, keyInfo.Address.String(), "100000ubbn")

return tm
}
Expand All @@ -187,6 +189,23 @@ func (tm *TestManager) WaitForServicesStart(t *testing.T) {
t.Logf("Babylon node is started")
}

func (tm *TestManager) SendToAddr(t *testing.T, toAddr, amount string) {
sendTx := exec.Command(
"babylond",
"tx",
"bank",
"send",
"node0",
toAddr,
amount,
"--keyring-backend=test",
"--chain-id=chain-test",
fmt.Sprintf("--home=%s", tm.BabylonHandler.babylonNode.nodeHome),
)
err := sendTx.Start()
require.NoError(t, err)
}

func StartManagerWithFinalityProvider(t *testing.T, n int) (*TestManager, []*btcec.PublicKey) {
tm := StartManager(t)

Expand Down