2019-05-16 08:25:32 -07:00
|
|
|
package cli
|
2019-03-27 11:38:35 -07:00
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
2020-07-06 14:48:54 -07:00
|
|
|
"github.com/spf13/pflag"
|
2019-03-27 11:38:35 -07:00
|
|
|
"github.com/stretchr/testify/require"
|
2019-04-04 07:36:39 -07:00
|
|
|
|
2020-07-08 10:57:45 -07:00
|
|
|
"github.com/cosmos/cosmos-sdk/client/flags"
|
2019-04-04 07:36:39 -07:00
|
|
|
sdk "github.com/cosmos/cosmos-sdk/types"
|
2019-03-27 11:38:35 -07:00
|
|
|
)
|
|
|
|
|
2020-07-06 14:48:54 -07:00
|
|
|
func TestPrepareConfigForTxCreateValidator(t *testing.T) {
|
|
|
|
chainID := "chainID"
|
|
|
|
ip := "1.1.1.1"
|
|
|
|
nodeID := "nodeID"
|
2020-01-09 06:04:28 -08:00
|
|
|
valPubKey, _ := sdk.GetPubKeyFromBech32(sdk.Bech32PubKeyTypeConsPub, "cosmosvalconspub1zcjduepq7jsrkl9fgqk0wj3ahmfr8pgxj6vakj2wzn656s8pehh0zhv2w5as5gd80a")
|
2020-07-08 10:57:45 -07:00
|
|
|
moniker := "DefaultMoniker"
|
2019-03-27 11:38:35 -07:00
|
|
|
|
2020-07-06 14:48:54 -07:00
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
fsModify func(fs *pflag.FlagSet)
|
|
|
|
expectedCfg TxCreateValidatorConfig
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "all defaults",
|
|
|
|
fsModify: func(fs *pflag.FlagSet) {
|
|
|
|
return
|
|
|
|
},
|
|
|
|
expectedCfg: TxCreateValidatorConfig{
|
|
|
|
IP: ip,
|
|
|
|
ChainID: chainID,
|
|
|
|
NodeID: nodeID,
|
|
|
|
TrustNode: true,
|
|
|
|
PubKey: sdk.MustBech32ifyPubKey(sdk.Bech32PubKeyTypeConsPub, valPubKey),
|
|
|
|
Moniker: moniker,
|
|
|
|
Amount: "100000000stake",
|
|
|
|
CommissionRate: "0.1",
|
|
|
|
CommissionMaxRate: "0.2",
|
|
|
|
CommissionMaxChangeRate: "0.01",
|
|
|
|
MinSelfDelegation: "1",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "Custom amount",
|
|
|
|
fsModify: func(fs *pflag.FlagSet) {
|
|
|
|
fs.Set(FlagAmount, "2000stake")
|
|
|
|
},
|
|
|
|
expectedCfg: TxCreateValidatorConfig{
|
|
|
|
IP: ip,
|
2020-07-08 10:57:45 -07:00
|
|
|
Moniker: moniker,
|
2020-07-06 14:48:54 -07:00
|
|
|
ChainID: chainID,
|
|
|
|
NodeID: nodeID,
|
|
|
|
TrustNode: true,
|
|
|
|
PubKey: sdk.MustBech32ifyPubKey(sdk.Bech32PubKeyTypeConsPub, valPubKey),
|
|
|
|
Amount: "2000stake",
|
|
|
|
CommissionRate: "0.1",
|
|
|
|
CommissionMaxRate: "0.2",
|
|
|
|
CommissionMaxChangeRate: "0.01",
|
|
|
|
MinSelfDelegation: "1",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "Custom commission rate",
|
|
|
|
fsModify: func(fs *pflag.FlagSet) {
|
|
|
|
fs.Set(FlagCommissionRate, "0.54")
|
|
|
|
},
|
|
|
|
expectedCfg: TxCreateValidatorConfig{
|
|
|
|
IP: ip,
|
2020-07-08 10:57:45 -07:00
|
|
|
Moniker: moniker,
|
2020-07-06 14:48:54 -07:00
|
|
|
ChainID: chainID,
|
|
|
|
NodeID: nodeID,
|
|
|
|
TrustNode: true,
|
|
|
|
PubKey: sdk.MustBech32ifyPubKey(sdk.Bech32PubKeyTypeConsPub, valPubKey),
|
|
|
|
Amount: "100000000stake",
|
|
|
|
CommissionRate: "0.54",
|
|
|
|
CommissionMaxRate: "0.2",
|
|
|
|
CommissionMaxChangeRate: "0.01",
|
|
|
|
MinSelfDelegation: "1",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "Custom commission max rate",
|
|
|
|
fsModify: func(fs *pflag.FlagSet) {
|
|
|
|
fs.Set(FlagCommissionMaxRate, "0.89")
|
|
|
|
},
|
|
|
|
expectedCfg: TxCreateValidatorConfig{
|
|
|
|
IP: ip,
|
2020-07-08 10:57:45 -07:00
|
|
|
Moniker: moniker,
|
2020-07-06 14:48:54 -07:00
|
|
|
ChainID: chainID,
|
|
|
|
NodeID: nodeID,
|
|
|
|
TrustNode: true,
|
|
|
|
PubKey: sdk.MustBech32ifyPubKey(sdk.Bech32PubKeyTypeConsPub, valPubKey),
|
|
|
|
Amount: "100000000stake",
|
|
|
|
CommissionRate: "0.1",
|
|
|
|
CommissionMaxRate: "0.89",
|
|
|
|
CommissionMaxChangeRate: "0.01",
|
|
|
|
MinSelfDelegation: "1",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "Custom commission max change rate",
|
|
|
|
fsModify: func(fs *pflag.FlagSet) {
|
|
|
|
fs.Set(FlagCommissionMaxChangeRate, "0.55")
|
|
|
|
},
|
|
|
|
expectedCfg: TxCreateValidatorConfig{
|
|
|
|
IP: ip,
|
2020-07-08 10:57:45 -07:00
|
|
|
Moniker: moniker,
|
2020-07-06 14:48:54 -07:00
|
|
|
ChainID: chainID,
|
|
|
|
NodeID: nodeID,
|
|
|
|
TrustNode: true,
|
|
|
|
PubKey: sdk.MustBech32ifyPubKey(sdk.Bech32PubKeyTypeConsPub, valPubKey),
|
|
|
|
Amount: "100000000stake",
|
|
|
|
CommissionRate: "0.1",
|
|
|
|
CommissionMaxRate: "0.2",
|
|
|
|
CommissionMaxChangeRate: "0.55",
|
|
|
|
MinSelfDelegation: "1",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "Custom min self delegations",
|
|
|
|
fsModify: func(fs *pflag.FlagSet) {
|
|
|
|
fs.Set(FlagMinSelfDelegation, "0.33")
|
|
|
|
},
|
|
|
|
expectedCfg: TxCreateValidatorConfig{
|
|
|
|
IP: ip,
|
2020-07-08 10:57:45 -07:00
|
|
|
Moniker: moniker,
|
2020-07-06 14:48:54 -07:00
|
|
|
ChainID: chainID,
|
|
|
|
NodeID: nodeID,
|
|
|
|
TrustNode: true,
|
|
|
|
PubKey: sdk.MustBech32ifyPubKey(sdk.Bech32PubKeyTypeConsPub, valPubKey),
|
|
|
|
Amount: "100000000stake",
|
|
|
|
CommissionRate: "0.1",
|
|
|
|
CommissionMaxRate: "0.2",
|
|
|
|
CommissionMaxChangeRate: "0.01",
|
|
|
|
MinSelfDelegation: "0.33",
|
|
|
|
},
|
|
|
|
},
|
2019-03-27 11:38:35 -07:00
|
|
|
}
|
|
|
|
|
2020-07-06 14:48:54 -07:00
|
|
|
for _, tc := range tests {
|
|
|
|
tc := tc
|
|
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
|
|
fs, _ := CreateValidatorMsgFlagSet(ip)
|
|
|
|
fs.String(flags.FlagName, "", "name of private key with which to sign the gentx")
|
|
|
|
|
|
|
|
tc.fsModify(fs)
|
|
|
|
|
2020-07-08 10:57:45 -07:00
|
|
|
cvCfg, err := PrepareConfigForTxCreateValidator(fs, moniker, nodeID, chainID, valPubKey)
|
2020-07-06 14:48:54 -07:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
require.Equal(t, tc.expectedCfg, cvCfg)
|
|
|
|
})
|
2019-03-27 11:38:35 -07:00
|
|
|
}
|
|
|
|
}
|