merged in master

This commit is contained in:
Sunny Aggarwal
2021-04-06 19:19:59 -04:00
264 changed files with 8719 additions and 17032 deletions
+20 -19
View File
@@ -42,11 +42,9 @@ func (s *IntegrationTestSuite) SetupSuite() {
s.T().Skip("skipping test in unit-tests mode.")
}
cfg := network.DefaultConfig()
cfg.NumValidators = 2
s.cfg = cfg
s.network = network.New(s.T(), cfg)
s.cfg = network.DefaultConfig()
s.cfg.NumValidators = 2
s.network = network.New(s.T(), s.cfg)
_, err := s.network.WaitForHeight(1)
s.Require().NoError(err)
@@ -82,17 +80,18 @@ func (s *IntegrationTestSuite) TearDownSuite() {
}
func (s *IntegrationTestSuite) TestNewCreateValidatorCmd() {
require := s.Require()
val := s.network.Validators[0]
consPrivKey := ed25519.GenPrivKey()
consPubKey, err := sdk.Bech32ifyPubKey(sdk.Bech32PubKeyTypeConsPub, consPrivKey.PubKey())
s.Require().NoError(err)
consPubKeyBz, err := s.cfg.Codec.MarshalInterfaceJSON(consPrivKey.PubKey())
require.NoError(err)
require.NotNil(consPubKeyBz)
info, _, err := val.ClientCtx.Keyring.NewMnemonic("NewValidator", keyring.English, sdk.FullFundraiserPath, keyring.DefaultBIP39Passphrase, hd.Secp256k1)
s.Require().NoError(err)
require.NoError(err)
newAddr := sdk.AccAddress(info.GetPubKey().Address())
_, err = banktestutil.MsgSendExec(
val.ClientCtx,
val.Address,
@@ -101,7 +100,7 @@ func (s *IntegrationTestSuite) TestNewCreateValidatorCmd() {
fmt.Sprintf("--%s=%s", flags.FlagBroadcastMode, flags.BroadcastBlock),
fmt.Sprintf("--%s=%s", flags.FlagFees, sdk.NewCoins(sdk.NewCoin(s.cfg.BondDenom, sdk.NewInt(10))).String()),
)
s.Require().NoError(err)
require.NoError(err)
testCases := []struct {
name string
@@ -131,7 +130,7 @@ func (s *IntegrationTestSuite) TestNewCreateValidatorCmd() {
{
"invalid transaction (missing pubkey)",
[]string{
fmt.Sprintf("--%s=100stake", cli.FlagAmount),
fmt.Sprintf("--%s=%dstake", cli.FlagAmount, 100),
fmt.Sprintf("--%s=AFAF00C4", cli.FlagIdentity),
fmt.Sprintf("--%s=https://newvalidator.io", cli.FlagWebsite),
fmt.Sprintf("--%s=contact@newvalidator.io", cli.FlagSecurityContact),
@@ -150,8 +149,8 @@ func (s *IntegrationTestSuite) TestNewCreateValidatorCmd() {
{
"invalid transaction (missing moniker)",
[]string{
fmt.Sprintf("--%s=%s", cli.FlagPubKey, consPubKey),
fmt.Sprintf("--%s=100stake", cli.FlagAmount),
fmt.Sprintf("--%s=%s", cli.FlagPubKey, consPubKeyBz),
fmt.Sprintf("--%s=%dstake", cli.FlagAmount, 100),
fmt.Sprintf("--%s=AFAF00C4", cli.FlagIdentity),
fmt.Sprintf("--%s=https://newvalidator.io", cli.FlagWebsite),
fmt.Sprintf("--%s=contact@newvalidator.io", cli.FlagSecurityContact),
@@ -170,8 +169,8 @@ func (s *IntegrationTestSuite) TestNewCreateValidatorCmd() {
{
"valid transaction",
[]string{
fmt.Sprintf("--%s=%s", cli.FlagPubKey, consPubKey),
fmt.Sprintf("--%s=100stake", cli.FlagAmount),
fmt.Sprintf("--%s=%s", cli.FlagPubKey, consPubKeyBz),
fmt.Sprintf("--%s=%dstake", cli.FlagAmount, 100),
fmt.Sprintf("--%s=NewValidator", cli.FlagMoniker),
fmt.Sprintf("--%s=AFAF00C4", cli.FlagIdentity),
fmt.Sprintf("--%s=https://newvalidator.io", cli.FlagWebsite),
@@ -199,13 +198,15 @@ func (s *IntegrationTestSuite) TestNewCreateValidatorCmd() {
out, err := clitestutil.ExecTestCLICmd(clientCtx, cmd, tc.args)
if tc.expectErr {
s.Require().Error(err)
require.Error(err)
} else {
s.Require().NoError(err, out.String())
s.Require().NoError(clientCtx.JSONMarshaler.UnmarshalJSON(out.Bytes(), tc.respType), out.String())
require.NoError(err, "test: %s\noutput: %s", tc.name, out.String())
err = clientCtx.JSONMarshaler.UnmarshalJSON(out.Bytes(), tc.respType)
require.NoError(err, out.String(), "test: %s, output\n:", tc.name, out.String())
txResp := tc.respType.(*sdk.TxResponse)
s.Require().Equal(tc.expectedCode, txResp.Code, out.String())
require.Equal(tc.expectedCode, txResp.Code,
"test: %s, output\n:", tc.name, out.String())
}
})
}
+11 -14
View File
@@ -58,8 +58,9 @@ func NewCreateValidatorCmd() *cobra.Command {
if err != nil {
return err
}
txf := tx.NewFactoryCLI(clientCtx, cmd.Flags()).WithTxConfig(clientCtx.TxConfig).WithAccountRetriever(clientCtx.AccountRetriever)
txf := tx.NewFactoryCLI(clientCtx, cmd.Flags()).
WithTxConfig(clientCtx.TxConfig).WithAccountRetriever(clientCtx.AccountRetriever)
txf, msg, err := newBuildCreateValidatorMsg(clientCtx, txf, cmd.Flags())
if err != nil {
return err
@@ -315,13 +316,16 @@ func newBuildCreateValidatorMsg(clientCtx client.Context, txf tx.Factory, fs *fl
}
valAddr := clientCtx.GetFromAddress()
pkStr, _ := fs.GetString(FlagPubKey)
pk, err := sdk.GetPubKeyFromBech32(sdk.Bech32PubKeyTypeConsPub, pkStr)
pkStr, err := fs.GetString(FlagPubKey)
if err != nil {
return txf, nil, err
}
var pk cryptotypes.PubKey
if err := clientCtx.JSONMarshaler.UnmarshalInterfaceJSON([]byte(pkStr), &pk); err != nil {
return txf, nil, err
}
moniker, _ := fs.GetString(FlagMoniker)
identity, _ := fs.GetString(FlagIdentity)
website, _ := fs.GetString(FlagWebsite)
@@ -417,7 +421,7 @@ type TxCreateValidatorConfig struct {
CommissionMaxChangeRate string
MinSelfDelegation string
PubKey string
PubKey cryptotypes.PubKey
IP string
Website string
@@ -489,7 +493,7 @@ func PrepareConfigForTxCreateValidator(flagSet *flag.FlagSet, moniker, nodeID, c
}
c.NodeID = nodeID
c.PubKey = sdk.MustBech32ifyPubKey(sdk.Bech32PubKeyTypeConsPub, valPubKey)
c.PubKey = valPubKey
c.Website = website
c.SecurityContact = securityContact
c.Details = details
@@ -530,13 +534,6 @@ func BuildCreateValidatorMsg(clientCtx client.Context, config TxCreateValidatorC
}
valAddr := clientCtx.GetFromAddress()
pkStr := config.PubKey
pk, err := sdk.GetPubKeyFromBech32(sdk.Bech32PubKeyTypeConsPub, pkStr)
if err != nil {
return txBldr, nil, err
}
description := types.NewDescription(
config.Moniker,
config.Identity,
@@ -564,7 +561,7 @@ func BuildCreateValidatorMsg(clientCtx client.Context, config TxCreateValidatorC
}
msg, err := types.NewMsgCreateValidator(
sdk.ValAddress(valAddr), pk, amount, description, commissionRates, minSelfDelegation,
sdk.ValAddress(valAddr), config.PubKey, amount, description, commissionRates, minSelfDelegation,
)
if err != nil {
return txBldr, msg, err
+27 -82
View File
@@ -7,15 +7,30 @@ import (
"github.com/stretchr/testify/require"
"github.com/cosmos/cosmos-sdk/client/flags"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/crypto/keys/ed25519"
)
func TestPrepareConfigForTxCreateValidator(t *testing.T) {
chainID := "chainID"
ip := "1.1.1.1"
nodeID := "nodeID"
valPubKey, _ := sdk.GetPubKeyFromBech32(sdk.Bech32PubKeyTypeConsPub, "cosmosvalconspub1zcjduepq7jsrkl9fgqk0wj3ahmfr8pgxj6vakj2wzn656s8pehh0zhv2w5as5gd80a")
privKey := ed25519.GenPrivKey()
valPubKey := privKey.PubKey()
moniker := "DefaultMoniker"
mkTxValCfg := func(amount, commission, commissionMax, commissionMaxChange, minSelfDelegation string) TxCreateValidatorConfig {
return TxCreateValidatorConfig{
IP: ip,
ChainID: chainID,
NodeID: nodeID,
PubKey: valPubKey,
Moniker: moniker,
Amount: amount,
CommissionRate: commission,
CommissionMaxRate: commissionMax,
CommissionMaxChangeRate: commissionMaxChange,
MinSelfDelegation: minSelfDelegation,
}
}
tests := []struct {
name string
@@ -27,108 +42,38 @@ func TestPrepareConfigForTxCreateValidator(t *testing.T) {
fsModify: func(fs *pflag.FlagSet) {
return
},
expectedCfg: TxCreateValidatorConfig{
IP: ip,
ChainID: chainID,
NodeID: nodeID,
PubKey: sdk.MustBech32ifyPubKey(sdk.Bech32PubKeyTypeConsPub, valPubKey),
Moniker: moniker,
Amount: defaultAmount,
CommissionRate: "0.1",
CommissionMaxRate: "0.2",
CommissionMaxChangeRate: "0.01",
MinSelfDelegation: "1",
},
},
{
expectedCfg: mkTxValCfg(defaultAmount, "0.1", "0.2", "0.01", "1"),
}, {
name: "Custom amount",
fsModify: func(fs *pflag.FlagSet) {
fs.Set(FlagAmount, "2000stake")
},
expectedCfg: TxCreateValidatorConfig{
IP: ip,
Moniker: moniker,
ChainID: chainID,
NodeID: nodeID,
PubKey: sdk.MustBech32ifyPubKey(sdk.Bech32PubKeyTypeConsPub, valPubKey),
Amount: "2000stake",
CommissionRate: "0.1",
CommissionMaxRate: "0.2",
CommissionMaxChangeRate: "0.01",
MinSelfDelegation: "1",
},
},
{
expectedCfg: mkTxValCfg("2000stake", "0.1", "0.2", "0.01", "1"),
}, {
name: "Custom commission rate",
fsModify: func(fs *pflag.FlagSet) {
fs.Set(FlagCommissionRate, "0.54")
},
expectedCfg: TxCreateValidatorConfig{
IP: ip,
Moniker: moniker,
ChainID: chainID,
NodeID: nodeID,
PubKey: sdk.MustBech32ifyPubKey(sdk.Bech32PubKeyTypeConsPub, valPubKey),
Amount: defaultAmount,
CommissionRate: "0.54",
CommissionMaxRate: "0.2",
CommissionMaxChangeRate: "0.01",
MinSelfDelegation: "1",
},
},
{
expectedCfg: mkTxValCfg(defaultAmount, "0.54", "0.2", "0.01", "1"),
}, {
name: "Custom commission max rate",
fsModify: func(fs *pflag.FlagSet) {
fs.Set(FlagCommissionMaxRate, "0.89")
},
expectedCfg: TxCreateValidatorConfig{
IP: ip,
Moniker: moniker,
ChainID: chainID,
NodeID: nodeID,
PubKey: sdk.MustBech32ifyPubKey(sdk.Bech32PubKeyTypeConsPub, valPubKey),
Amount: defaultAmount,
CommissionRate: "0.1",
CommissionMaxRate: "0.89",
CommissionMaxChangeRate: "0.01",
MinSelfDelegation: "1",
},
},
{
expectedCfg: mkTxValCfg(defaultAmount, "0.1", "0.89", "0.01", "1"),
}, {
name: "Custom commission max change rate",
fsModify: func(fs *pflag.FlagSet) {
fs.Set(FlagCommissionMaxChangeRate, "0.55")
},
expectedCfg: TxCreateValidatorConfig{
IP: ip,
Moniker: moniker,
ChainID: chainID,
NodeID: nodeID,
PubKey: sdk.MustBech32ifyPubKey(sdk.Bech32PubKeyTypeConsPub, valPubKey),
Amount: defaultAmount,
CommissionRate: "0.1",
CommissionMaxRate: "0.2",
CommissionMaxChangeRate: "0.55",
MinSelfDelegation: "1",
},
expectedCfg: mkTxValCfg(defaultAmount, "0.1", "0.2", "0.55", "1"),
},
{
name: "Custom min self delegations",
fsModify: func(fs *pflag.FlagSet) {
fs.Set(FlagMinSelfDelegation, "0.33")
},
expectedCfg: TxCreateValidatorConfig{
IP: ip,
Moniker: moniker,
ChainID: chainID,
NodeID: nodeID,
PubKey: sdk.MustBech32ifyPubKey(sdk.Bech32PubKeyTypeConsPub, valPubKey),
Amount: defaultAmount,
CommissionRate: "0.1",
CommissionMaxRate: "0.2",
CommissionMaxChangeRate: "0.01",
MinSelfDelegation: "0.33",
},
expectedCfg: mkTxValCfg(defaultAmount, "0.1", "0.2", "0.01", "0.33"),
},
}