Skip to content

Commit

Permalink
feat: Add commission as cli flag (#109)
Browse files Browse the repository at this point in the history
  • Loading branch information
gitferry authored Sep 21, 2023
1 parent b288764 commit 8caa336
Show file tree
Hide file tree
Showing 22 changed files with 371 additions and 252 deletions.
6 changes: 3 additions & 3 deletions clientcontroller/babylon.go
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ func (bc *BabylonController) GetStakingParams() (*StakingParams, error) {
MinSlashingTxFeeSat: btcutil.Amount(stakingParamRes.Params.MinSlashingTxFeeSat),
JuryPk: juryPk,
SlashingAddress: stakingParamRes.Params.SlashingAddress,
MinComissionRate: stakingParamRes.Params.MinCommissionRate,
MinCommissionRate: stakingParamRes.Params.MinCommissionRate,
}, nil
}

Expand Down Expand Up @@ -268,7 +268,7 @@ func (bc *BabylonController) RegisterValidator(
bbnPubKey *secp256k1.PubKey,
btcPubKey *bbntypes.BIP340PubKey,
pop *btcstakingtypes.ProofOfPossession,
commission sdkTypes.Dec,
commission *sdkTypes.Dec,
description *sttypes.Description,
) (*provider.RelayerTxResponse, error) {

Expand All @@ -277,7 +277,7 @@ func (bc *BabylonController) RegisterValidator(
BabylonPk: bbnPubKey,
BtcPk: btcPubKey,
Pop: pop,
Commission: &commission,
Commission: commission,
Description: description,
}

Expand Down
6 changes: 3 additions & 3 deletions clientcontroller/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@ type StakingParams struct {
// Address to which slashing transactions are sent
SlashingAddress string

// Minimum comission required by babylon
MinComissionRate sdkTypes.Dec
// Minimum commission required by babylon
MinCommissionRate sdkTypes.Dec
}

// TODO replace babylon types with general ones
Expand All @@ -50,7 +50,7 @@ type ClientController interface {
bbnPubKey *secp256k1.PubKey,
btcPubKey *bbntypes.BIP340PubKey,
pop *btcstakingtypes.ProofOfPossession,
commission sdkTypes.Dec,
commission *sdkTypes.Dec,
description *stakingtypes.Description,
) (*provider.RelayerTxResponse, error)
// CommitPubRandList commits a list of Schnorr public randomness via a MsgCommitPubRand to Babylon
Expand Down
16 changes: 15 additions & 1 deletion cmd/valcli/daemoncmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"fmt"
"strconv"

"cosmossdk.io/math"
"github.com/babylonchain/babylon/x/checkpointing/types"
stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types"
"github.com/urfave/cli"
Expand Down Expand Up @@ -45,6 +46,8 @@ const (
websiteFlag = "website"
securityContractFlag = "security-contract"
detailsFlag = "details"

commissionRateFlag = "commission"
)

var (
Expand Down Expand Up @@ -100,6 +103,11 @@ var createValDaemonCmd = cli.Command{
Usage: "The unique name of the validator key",
Required: true,
},
cli.StringFlag{
Name: commissionRateFlag,
Usage: "The commission rate for the validator, e.g., 0.05",
Value: "0.05",
},
cli.StringFlag{
Name: monikerFlag,
Usage: "A human-readable name for the validator",
Expand Down Expand Up @@ -132,6 +140,12 @@ var createValDaemonCmd = cli.Command{
func createValDaemon(ctx *cli.Context) error {
daemonAddress := ctx.String(valdDaemonAddressFlag)
keyName := ctx.String(keyNameFlag)

commissionRate, err := math.LegacyNewDecFromStr(ctx.String(commissionRateFlag))
if err != nil {
return err
}

description, err := getDesciptionFromContext(ctx)
if err != nil {
return err
Expand All @@ -143,7 +157,7 @@ func createValDaemon(ctx *cli.Context) error {
}
defer cleanUp()

info, err := client.CreateValidator(context.Background(), keyName, &description)
info, err := client.CreateValidator(context.Background(), keyName, &description, &commissionRate)

if err != nil {
return err
Expand Down
20 changes: 19 additions & 1 deletion cmd/valcli/validators.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"encoding/hex"
"fmt"

"cosmossdk.io/math"
"github.com/urfave/cli"

"github.com/babylonchain/btc-validator/proto"
Expand Down Expand Up @@ -56,6 +57,11 @@ var createValCmd = cli.Command{
Name: keyringDirFlag,
Usage: "The directory where the keyring is stored",
},
cli.StringFlag{
Name: commissionRateFlag,
Usage: "The commission rate for the validator, e.g., 0.05",
Value: "0.05",
},
cli.StringFlag{
Name: monikerFlag,
Usage: "A human-readable name for the validator",
Expand Down Expand Up @@ -103,6 +109,11 @@ func createVal(ctx *cli.Context) error {
return err
}

commissionRate, err := math.LegacyNewDecFromStr(ctx.String(commissionRateFlag))
if err != nil {
return err
}

if krController.ValidatorKeyNameTaken() {
return fmt.Errorf("the key name %s is taken", krController.GetKeyName())
}
Expand All @@ -112,11 +123,18 @@ func createVal(ctx *cli.Context) error {
return err
}

validator, err := krController.CreateBTCValidator(&description)
btcPk, bbnPk, err := krController.CreateValidatorKeys()
if err != nil {
return err
}

pop, err := krController.CreatePop()
if err != nil {
return err
}

validator := val.NewStoreValidator(bbnPk, btcPk, krController.GetKeyName(), pop, &description, &commissionRate)

vs, err := getValStoreFromCtx(ctx)
if err != nil {
return err
Expand Down
6 changes: 3 additions & 3 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,15 @@ module github.com/babylonchain/btc-validator
go 1.20

require (
cosmossdk.io/errors v1.0.0-beta.7
cosmossdk.io/math v1.0.1
github.com/avast/retry-go/v4 v4.3.3
github.com/babylonchain/babylon v0.7.1
github.com/btcsuite/btcd v0.23.5-0.20230228185050-38331963bddd
github.com/btcsuite/btcd/btcec/v2 v2.3.2
github.com/btcsuite/btcd/btcutil v1.1.3
github.com/btcsuite/btcd/chaincfg/chainhash v1.0.2
github.com/cometbft/cometbft v0.37.2
github.com/cosmos/cosmos-proto v1.0.0-beta.2
github.com/cosmos/cosmos-sdk v0.47.3
github.com/cosmos/go-bip39 v1.0.0
github.com/cosmos/gogoproto v1.4.10
Expand Down Expand Up @@ -42,8 +43,8 @@ require (
cosmossdk.io/api v0.3.1 // indirect
cosmossdk.io/core v0.5.1 // indirect
cosmossdk.io/depinject v1.0.0-alpha.3 // indirect
cosmossdk.io/errors v1.0.0-beta.7 // indirect
cosmossdk.io/log v1.1.0 // indirect
cosmossdk.io/math v1.0.1 // indirect
cosmossdk.io/tools/rosetta v0.2.1 // indirect
filippo.io/edwards25519 v1.0.0 // indirect
github.com/99designs/go-keychain v0.0.0-20191008050251-8e49817e8af4 // indirect
Expand Down Expand Up @@ -83,7 +84,6 @@ require (
github.com/coreos/go-systemd v0.0.0-20190719114852-fd7a80b32e1f // indirect
github.com/coreos/go-systemd/v22 v22.5.0 // indirect
github.com/cosmos/btcutil v1.0.5 // indirect
github.com/cosmos/cosmos-proto v1.0.0-beta.2 // indirect
github.com/cosmos/gogogateway v1.2.0 // indirect
github.com/cosmos/iavl v0.20.0 // indirect
github.com/cosmos/ibc-go/v7 v7.2.0 // indirect
Expand Down
8 changes: 1 addition & 7 deletions itest/e2e_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ import (
"github.com/stretchr/testify/require"

"github.com/babylonchain/btc-validator/clientcontroller"
"github.com/babylonchain/btc-validator/proto"
"github.com/babylonchain/btc-validator/service"
"github.com/babylonchain/btc-validator/types"
"github.com/babylonchain/btc-validator/val"
Expand Down Expand Up @@ -221,12 +220,7 @@ func TestDoubleSigning(t *testing.T) {
require.NoError(t, err)
require.True(t, localKey.Key.Equals(&extractedKey.Key) || localKey.Key.Negate().Equals(&extractedKey.Key))

// try to submit another signature and should get error due to being slashed already
_, _, err = valIns.TestSubmitFinalitySignatureAndExtractPrivKey(b)
require.ErrorIs(t, err, types.ErrValidatorSlashed)

tm.WaitForValStopped(t, valIns.GetBabylonPk())
require.Equal(t, proto.ValidatorStatus_SLASHED, valIns.GetStatus())
t.Logf("the equivocation attack is successful")
}

func getBtcPrivKey(kr keyring.Keyring, keyName val.KeyName) (*btcec.PrivateKey, error) {
Expand Down
47 changes: 42 additions & 5 deletions itest/test_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"os"
"path/filepath"
"strconv"
"strings"
"sync"
"testing"
"time"
Expand All @@ -20,13 +21,14 @@ import (
"github.com/btcsuite/btcd/chaincfg/chainhash"
"github.com/btcsuite/btcd/wire"
"github.com/cosmos/cosmos-sdk/crypto/keys/secp256k1"
sdktypes "github.com/cosmos/cosmos-sdk/types"
stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types"
"github.com/cosmos/relayer/v2/relayer/provider"
"github.com/sirupsen/logrus"
"github.com/stretchr/testify/require"

"github.com/babylonchain/btc-validator/clientcontroller"
"github.com/babylonchain/btc-validator/service"
"github.com/babylonchain/btc-validator/testutil"
"github.com/babylonchain/btc-validator/types"
"github.com/babylonchain/btc-validator/valcfg"
)
Expand Down Expand Up @@ -80,6 +82,7 @@ func StartManager(t *testing.T, isJury bool) *TestManager {
bc, err := clientcontroller.NewBabylonController(bh.GetNodeDataDir(), cfg.BabylonConfig, logger)
// making sure the fee is sufficient
cfg.BabylonConfig.GasAdjustment = 1.5
cfg.BabylonConfig.GasPrices = "0.1ubbn"
require.NoError(t, err)

valApp, err := service.NewValidatorAppFromConfig(cfg, logger, bc)
Expand Down Expand Up @@ -113,19 +116,48 @@ func StartManagerWithValidator(t *testing.T, n int, isJury bool) *TestManager {
tm := StartManager(t, isJury)
app := tm.Va

var newValName = "test-val-"
var (
valNamePrefix = "test-val-"
monikerPrefix = "moniker-"
)
for i := 0; i < n; i++ {
newValName += strconv.Itoa(i)
_, err := app.CreateValidator(newValName, testutil.EmptyDescription())
valName := valNamePrefix + strconv.Itoa(i)
moniker := monikerPrefix + strconv.Itoa(i)
commission := sdktypes.OneDec()
_, err := app.CreateValidator(valName, newDescription(moniker), &commission)
require.NoError(t, err)
_, bbnPk, err := app.RegisterValidator(newValName)
_, bbnPk, err := app.RegisterValidator(valName)
require.NoError(t, err)
err = app.StartHandlingValidator(bbnPk)
require.NoError(t, err)
valIns, err := app.GetValidatorInstance(bbnPk)
require.NoError(t, err)
require.True(t, valIns.IsRunning())
require.NoError(t, err)

// check validators on Babylon side
require.Eventually(t, func() bool {
vals, err := tm.BabylonClient.QueryValidators()
if err != nil {
t.Logf("failed to query validtors from Babylon %s", err.Error())
return false
}

if len(vals) != i+1 {
return false
}

for _, v := range vals {
if !strings.Contains(v.Description.Moniker, monikerPrefix) {
return false
}
if !v.Commission.Equal(sdktypes.OneDec()) {
return false
}
}

return true
}, eventuallyWaitTimeOut, eventuallyPollTime)
}

require.Equal(t, n, len(app.ListValidatorInstances()))
Expand Down Expand Up @@ -527,3 +559,8 @@ func tempDirWithName(name string) (string, error) {

return tempName, nil
}

func newDescription(moniker string) *stakingtypes.Description {
dec := stakingtypes.NewDescription(moniker, "", "", "", "")
return &dec
}
Loading

0 comments on commit 8caa336

Please sign in to comment.