Skip to content

Commit

Permalink
chore: update FP as signer (#178)
Browse files Browse the repository at this point in the history
* chore: update FP as signer

* fix: set delay to 600, sometimes was failing to not enough time for block to be produce to get params

* fix: set command to multi-send in bank

* chore: removed print

* fix: use bank send if there is only one addr

* chore: remove space

* chore: update to use babylond version v0.9.0-rc.2

* chore: removed unused global params example
  • Loading branch information
RafilxTenfen authored Jul 1, 2024
1 parent 3eab822 commit 55abcea
Show file tree
Hide file tree
Showing 10 changed files with 181 additions and 99 deletions.
42 changes: 27 additions & 15 deletions babylonclient/babyloncontroller.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ var (
// TODO: Maybe configurable?
RtyAttNum = uint(5)
RtyAtt = retry.Attempts(RtyAttNum)
RtyDel = retry.Delay(time.Millisecond * 400)
RtyDel = retry.Delay(time.Millisecond * 600)
RtyErr = retry.LastErrorOnly(true)
)

Expand Down Expand Up @@ -110,8 +110,8 @@ type StakingTrackerResponse struct {
}

type FinalityProviderInfo struct {
BabylonPk secp256k1.PubKey
BtcPk btcec.PublicKey
BabylonAddr sdk.AccAddress
BtcPk btcec.PublicKey
}

type FinalityProvidersClientResponse struct {
Expand Down Expand Up @@ -535,11 +535,15 @@ func (bc *BabylonController) QueryFinalityProviders(
if err != nil {
return nil, fmt.Errorf("query finality providers error: %w", err)
}
fpBabylonPk := finalityProvider.BabylonPk

fpAddr, err := sdk.AccAddressFromBech32(finalityProvider.Addr)
if err != nil {
return nil, fmt.Errorf("query finality providers error transform address: %s - %w", finalityProvider.Addr, err)
}

fpInfo := FinalityProviderInfo{
BabylonPk: *fpBabylonPk,
BtcPk: *fpBtcKey,
BabylonAddr: fpAddr,
BtcPk: *fpBtcKey,
}

finalityProviders = append(finalityProviders, fpInfo)
Expand Down Expand Up @@ -598,15 +602,19 @@ func (bc *BabylonController) QueryFinalityProvider(btcPubKey *btcec.PublicKey) (
}

btcPk, err := response.FinalityProvider.BtcPk.ToBTCPK()

if err != nil {
return nil, fmt.Errorf("received malformed btc pk in babylon response: %w", err)
}

fpAddr, err := sdk.AccAddressFromBech32(response.FinalityProvider.Addr)
if err != nil {
return nil, fmt.Errorf("received malformed fp addr in babylon response: %s - %w", response.FinalityProvider.Addr, err)
}

return &FinalityProviderClientResponse{
FinalityProvider: FinalityProviderInfo{
BabylonPk: *response.FinalityProvider.BabylonPk,
BtcPk: *btcPk,
BabylonAddr: fpAddr,
BtcPk: *btcPk,
},
}, nil
}
Expand Down Expand Up @@ -668,22 +676,26 @@ func chainToChainBytes(chain []*wire.BlockHeader) []bbntypes.BTCHeaderBytes {
// RegisterFinalityProvider registers a BTC finality provider via a MsgCreateFinalityProvider to Babylon
// it returns tx hash and error
func (bc *BabylonController) RegisterFinalityProvider(
bbnPubKey *secp256k1.PubKey,
fpAddr sdk.AccAddress,
fpPrivKeyBBN *secp256k1.PrivKey,
btcPubKey *bbntypes.BIP340PubKey,
commission *sdkmath.LegacyDec,
description *sttypes.Description,
pop *btcstypes.ProofOfPossession,
) (*pv.RelayerTxResponse, error) {
pop *btcstypes.ProofOfPossessionBTC,
) error {
registerMsg := &btcstypes.MsgCreateFinalityProvider{
Signer: bc.getTxSigner(),
Addr: fpAddr.String(),
Commission: commission,
BabylonPk: bbnPubKey,
BtcPk: btcPubKey,
Description: description,
Pop: pop,
}

return bc.reliablySendMsgs([]sdk.Msg{registerMsg})
fpPrivKeyBBN.PubKey()
relayerMsgs := bbnclient.ToProviderMsgs([]sdk.Msg{registerMsg})

_, err := bc.bbnClient.SendMessageWithSigner(context.Background(), fpAddr, fpPrivKeyBBN, relayerMsgs)
return err
}

func (bc *BabylonController) QueryDelegationInfo(stakingTxHash *chainhash.Hash) (*DelegationInfo, error) {
Expand Down
8 changes: 3 additions & 5 deletions babylonclient/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"

sdkmath "cosmossdk.io/math"
"github.com/babylonchain/babylon/testutil/datagen"
"github.com/babylonchain/babylon/x/btcstaking/types"
"github.com/btcsuite/btcd/btcec/v2"
"github.com/btcsuite/btcd/btcutil"
Expand Down Expand Up @@ -158,12 +159,9 @@ func GetMockClient() *MockBabylonClient {
panic(err)
}

fpBabylonPrivKey := secp256k1.GenPrivKey()
fpBabylonPubKey := fpBabylonPrivKey.PubKey().(*secp256k1.PubKey)

vi := FinalityProviderInfo{
BabylonPk: *fpBabylonPubKey,
BtcPk: *fpBtcPrivKey.PubKey(),
BabylonAddr: datagen.GenRandomAccount().GetAddress(),
BtcPk: *fpBtcPrivKey.PubKey(),
}

return &MockBabylonClient{
Expand Down
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ require (
cosmossdk.io/errors v1.0.1
cosmossdk.io/math v1.3.0
github.com/avast/retry-go/v4 v4.5.1
github.com/babylonchain/babylon v0.9.0-rc.1
github.com/babylonchain/babylon v0.9.0-rc.2
github.com/babylonchain/networks/parameters v0.2.1
github.com/btcsuite/btcd v0.24.0
github.com/btcsuite/btcd v0.24.2
github.com/btcsuite/btcd/btcec/v2 v2.3.2
github.com/btcsuite/btcd/btcutil v1.1.5
github.com/btcsuite/btcd/btcutil/psbt v1.1.8
Expand Down
8 changes: 4 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -285,8 +285,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/babylonchain/babylon v0.9.0-rc.1 h1:mZYKQVHVKFUA2xaEAzJloB1kyePHvZECJPgm3p9TYas=
github.com/babylonchain/babylon v0.9.0-rc.1/go.mod h1:YFALTW+Kp/b5jSDoA7Z70RggJjAedlmQTrpdeU8c3hY=
github.com/babylonchain/babylon v0.9.0-rc.2 h1:cnkVdfJgwXEarzQrqJ2otONuEl+Xe22bN6bXyoenBR4=
github.com/babylonchain/babylon v0.9.0-rc.2/go.mod h1:QTjpnEAEReQofIpZikCQXUZxSkdK0TrWAUbgxOSF9yA=
github.com/babylonchain/networks/parameters v0.2.1 h1:OKHiCnwL/UdVN17cMwCrHz/bAjO/USauLiPyNlnVl6E=
github.com/babylonchain/networks/parameters v0.2.1/go.mod h1:nejhvrL7Iwh5Vunvkg7pnomQZlHnyNzOY9lQaDp6tOA=
github.com/benbjohnson/clock v1.1.0/go.mod h1:J11/hYXuz8f4ySSvYwY0FKfm+ezbsZBKZxNJlLklBHA=
Expand All @@ -310,8 +310,8 @@ github.com/btcsuite/btcd v0.22.0-beta.0.20220207191057-4dc4ff7963b4/go.mod h1:7a
github.com/btcsuite/btcd v0.23.1/go.mod h1:0QJIIN1wwIXF/3G/m87gIwGniDMDQqjVn4SZgnFpsYY=
github.com/btcsuite/btcd v0.23.3/go.mod h1:0QJIIN1wwIXF/3G/m87gIwGniDMDQqjVn4SZgnFpsYY=
github.com/btcsuite/btcd v0.23.5-0.20231215221805-96c9fd8078fd/go.mod h1:nm3Bko6zh6bWP60UxwoT5LzdGJsQJaPo6HjduXq9p6A=
github.com/btcsuite/btcd v0.24.0 h1:gL3uHE/IaFj6fcZSu03SvqPMSx7s/dPzfpG/atRwWdo=
github.com/btcsuite/btcd v0.24.0/go.mod h1:K4IDc1593s8jKXIF7yS7yCTSxrknB9z0STzc2j6XgE4=
github.com/btcsuite/btcd v0.24.2 h1:aLmxPguqxza+4ag8R1I2nnJjSu2iFn/kqtHTIImswcY=
github.com/btcsuite/btcd v0.24.2/go.mod h1:5C8ChTkl5ejr3WHj8tkQSCmydiMEPB0ZhQhehpq7Dgg=
github.com/btcsuite/btcd/btcec/v2 v2.1.0/go.mod h1:2VzYrv4Gm4apmbVVsSq5bqf1Ec8v56E48Vt0Y/umPgA=
github.com/btcsuite/btcd/btcec/v2 v2.1.1/go.mod h1:ctjw4H1kknNJmRN4iP1R7bTQ+v3GJkZBd6mui8ZsAZE=
github.com/btcsuite/btcd/btcec/v2 v2.1.3/go.mod h1:ctjw4H1kknNJmRN4iP1R7bTQ+v3GJkZBd6mui8ZsAZE=
Expand Down
143 changes: 103 additions & 40 deletions itest/babylon_node_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,43 +14,27 @@ import (
"github.com/btcsuite/btcd/btcec/v2"
)

func baseDirBabylondir() (string, error) {
tempPath := os.TempDir()

tempName, err := os.MkdirTemp(tempPath, "zBabylonTestStaker")
if err != nil {
return "", err
}

err = os.Chmod(tempName, 0755)

if err != nil {
return "", err
}

return tempName, nil
}

type babylonNode struct {
cmd *exec.Cmd
pidFile string
dataDir string
type BabylonNode struct {
cmd *exec.Cmd
pidFile string
DataDir string
WalletName string
}

func newBabylonNode(dataDir string, cmd *exec.Cmd) *babylonNode {
return &babylonNode{
dataDir: dataDir,
cmd: cmd,
func newBabylonNode(dataDir, walletName string, cmd *exec.Cmd) *BabylonNode {
return &BabylonNode{
DataDir: dataDir,
cmd: cmd,
WalletName: walletName,
}
}

func (n *babylonNode) start() error {
func (n *BabylonNode) start() error {
if err := n.cmd.Start(); err != nil {
return err
}

pid, err := os.Create(filepath.Join(n.dataDir,
fmt.Sprintf("%s.pid", "config")))
pid, err := os.Create(filepath.Join(n.DataDir, fmt.Sprintf("%s.pid", "config")))
if err != nil {
return err
}
Expand All @@ -67,7 +51,7 @@ func (n *babylonNode) start() error {
return nil
}

func (n *babylonNode) stop() (err error) {
func (n *BabylonNode) stop() (err error) {
if n.cmd == nil || n.cmd.Process == nil {
// return if not properly initialized
// or error starting the process
Expand All @@ -84,7 +68,7 @@ func (n *babylonNode) stop() (err error) {
return n.cmd.Process.Signal(os.Interrupt)
}

func (n *babylonNode) cleanup() error {
func (n *BabylonNode) cleanup() error {
if n.pidFile != "" {
if err := os.Remove(n.pidFile); err != nil {
log.Printf("unable to remove file %s: %v", n.pidFile,
Expand All @@ -93,7 +77,7 @@ func (n *babylonNode) cleanup() error {
}

dirs := []string{
n.dataDir,
n.DataDir,
}
var err error
for _, dir := range dirs {
Expand All @@ -104,7 +88,7 @@ func (n *babylonNode) cleanup() error {
return nil
}

func (n *babylonNode) shutdown() error {
func (n *BabylonNode) shutdown() error {
if err := n.stop(); err != nil {
return err
}
Expand All @@ -115,7 +99,7 @@ func (n *babylonNode) shutdown() error {
}

type BabylonNodeHandler struct {
babylonNode *babylonNode
BabylonNode *BabylonNode
}

func NewBabylonNodeHandler(
Expand All @@ -136,6 +120,7 @@ func NewBabylonNodeHandler(
pubBabylon2 := types.NewBIP340PubKeyFromBTCPK(covenantPk2)
pubBabylon3 := types.NewBIP340PubKeyFromBTCPK(covenantPk3)

walletName := "node0"
initTestnetCmd := exec.Command(
"babylond",
"testnet",
Expand Down Expand Up @@ -166,10 +151,9 @@ func NewBabylonNodeHandler(
return nil, err
}

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

f, err := os.Create(filepath.Join(testDir, "babylon.log"))

if err != nil {
return nil, err
}
Expand All @@ -184,28 +168,107 @@ func NewBabylonNodeHandler(
startCmd.Stdout = f

return &BabylonNodeHandler{
babylonNode: newBabylonNode(testDir, startCmd),
BabylonNode: newBabylonNode(testDir, walletName, startCmd),
}, nil
}

func (w *BabylonNodeHandler) Start() error {
if err := w.babylonNode.start(); err != nil {
if err := w.BabylonNode.start(); err != nil {
// try to cleanup after start error, but return original error
_ = w.babylonNode.cleanup()
_ = w.BabylonNode.cleanup()
return err
}
return nil
}

func (w *BabylonNodeHandler) Stop() error {
if err := w.babylonNode.shutdown(); err != nil {
if err := w.BabylonNode.shutdown(); err != nil {
return err
}

return nil
}

func (w *BabylonNodeHandler) GetNodeDataDir() string {
dir := filepath.Join(w.babylonNode.dataDir, "node0", "babylond")
return w.BabylonNode.GetNodeDataDir()
}

// GetNodeDataDir returns the home path of the babylon node.
func (n *BabylonNode) GetNodeDataDir() string {
dir := filepath.Join(n.DataDir, n.WalletName, "babylond")
return dir
}

// TxBankSend send transaction to a address from the node address.
func (n *BabylonNode) TxBankSend(addr, coins string) error {
flags := []string{
"tx",
"bank",
"send",
n.WalletName,
addr, coins,
"--keyring-backend=test",
fmt.Sprintf("--home=%s", n.GetNodeDataDir()),
"--log_level=debug",
"--chain-id=chain-test",
"-b=sync", "--yes", "--gas-prices=10ubbn",
}

cmd := exec.Command("babylond", flags...)
_, err := cmd.Output()
if err != nil {
return err
}
return nil
}

// TxBankMultiSend send transaction to multiple addresses from the node address.
func (n *BabylonNode) TxBankMultiSend(coins string, addresses ...string) error {
// babylond tx bank multi-send [from_key_or_address] [to_address_1 to_address_2 ...] [amount] [flags]
switch len(addresses) {
case 0:
return nil
case 1:
return n.TxBankSend(addresses[0], coins)
default:
flags := []string{
"tx",
"bank",
"multi-send",
n.WalletName,
}
flags = append(flags, addresses...)
flags = append(flags,
coins,
"--keyring-backend=test",
fmt.Sprintf("--home=%s", n.GetNodeDataDir()),
"--log_level=debug",
"--chain-id=chain-test",
"-b=sync", "--yes", "--gas-prices=10ubbn",
)

cmd := exec.Command("babylond", flags...)
_, err := cmd.Output()
if err != nil {
return err
}
return nil
}
}

func baseDirBabylondir() (string, error) {
tempPath := os.TempDir()

tempName, err := os.MkdirTemp(tempPath, "zBabylonTestStaker")
if err != nil {
return "", err
}

err = os.Chmod(tempName, 0755)

if err != nil {
return "", err
}

return tempName, nil
}
Loading

0 comments on commit 55abcea

Please sign in to comment.