Skip to content

Commit

Permalink
Merge PR #3400: power reduction for Tendermint
Browse files Browse the repository at this point in the history
* add uncompiled power functionality

* fix some compile errors

* Power -> TendermintPower

* tests rename GetTendermintPower

* test fix

* working

* fix delegation tests

* fix slash tests

* staking/keeper tests passing

* docs reversion

* debuggin workin

* x/staking test pass

* fix gov tests

* fix x/slashing tests

* working distribution test fixes

* fix distribution tests

* lint

* fix lcd tests

* fix gov test

* lint

* CLI fixes, rm stakingTypes

* typos

* working cli fixes

* cli test fix

* cli tests fixed

* testnet creation modification

* typo

* pending

* Sanitize Dec.Roundint64 (#3475)

* merge fixes

* @cwgoes comments

* fix tests

* change power reduction to 10^-6

* option to turn off minting for LCD tests
  • Loading branch information
rigelrozanski authored Feb 6, 2019
1 parent df94f52 commit 52f2ec7
Show file tree
Hide file tree
Showing 56 changed files with 1,158 additions and 954 deletions.
2 changes: 2 additions & 0 deletions PENDING.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ BREAKING CHANGES
* [\#3457](https://github.com/cosmos/cosmos-sdk/issues/3457) Changed governance tally validatorGovInfo to use sdk.Int power instead of sdk.Dec

* SDK
* \#2513 Tendermint updates are adjusted by 10^-6 relative to staking tokens,
* [\#3487](https://github.com/cosmos/cosmos-sdk/pull/3487) Move HTTP/REST utilities out of client/utils into a new dedicated client/rest package.
* [\#3490](https://github.com/cosmos/cosmos-sdk/issues/3490) ReadRESTReq() returns bool to avoid callers to write error responses twice.
* [\#3502](https://github.com/cosmos/cosmos-sdk/pull/3502) Fixes issue when comparing genesis states
Expand Down Expand Up @@ -79,6 +80,7 @@ IMPROVEMENTS
* [\#2605] x/params add subkey accessing
* [\#2986](https://github.com/cosmos/cosmos-sdk/pull/2986) Store Refactor
* \#3435 Test that store implementations do not allow nil values
* \#2509 Sanitize all usage of Dec.RoundInt64()
* [\#556](https://github.com/cosmos/cosmos-sdk/issues/556) Increase `BaseApp`
test coverage.

Expand Down
166 changes: 91 additions & 75 deletions client/lcd/lcd_test.go

Large diffs are not rendered by default.

102 changes: 57 additions & 45 deletions client/lcd/test_helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ import (
"strings"
"testing"

"github.com/spf13/viper"
"github.com/stretchr/testify/require"

"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/client/keys"
"github.com/cosmos/cosmos-sdk/client/rest"
Expand All @@ -23,26 +26,22 @@ import (
gapp "github.com/cosmos/cosmos-sdk/cmd/gaia/app"
"github.com/cosmos/cosmos-sdk/codec"
crkeys "github.com/cosmos/cosmos-sdk/crypto/keys"
cryptoKeys "github.com/cosmos/cosmos-sdk/crypto/keys"
"github.com/cosmos/cosmos-sdk/server"
"github.com/cosmos/cosmos-sdk/tests"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/x/auth"
authRest "github.com/cosmos/cosmos-sdk/x/auth/client/rest"
authrest "github.com/cosmos/cosmos-sdk/x/auth/client/rest"
txbuilder "github.com/cosmos/cosmos-sdk/x/auth/client/txbuilder"
bankRest "github.com/cosmos/cosmos-sdk/x/bank/client/rest"
bankrest "github.com/cosmos/cosmos-sdk/x/bank/client/rest"
distr "github.com/cosmos/cosmos-sdk/x/distribution"
distrrest "github.com/cosmos/cosmos-sdk/x/distribution/client/rest"
"github.com/cosmos/cosmos-sdk/x/gov"
govRest "github.com/cosmos/cosmos-sdk/x/gov/client/rest"
govrest "github.com/cosmos/cosmos-sdk/x/gov/client/rest"
gcutils "github.com/cosmos/cosmos-sdk/x/gov/client/utils"
"github.com/cosmos/cosmos-sdk/x/slashing"
slashingRest "github.com/cosmos/cosmos-sdk/x/slashing/client/rest"
slashingrest "github.com/cosmos/cosmos-sdk/x/slashing/client/rest"
"github.com/cosmos/cosmos-sdk/x/staking"
stakingRest "github.com/cosmos/cosmos-sdk/x/staking/client/rest"
stakingTypes "github.com/cosmos/cosmos-sdk/x/staking/types"

"github.com/spf13/viper"
"github.com/stretchr/testify/require"
stakingrest "github.com/cosmos/cosmos-sdk/x/staking/client/rest"

abci "github.com/tendermint/tendermint/abci/types"
tmcfg "github.com/tendermint/tendermint/config"
Expand All @@ -59,9 +58,6 @@ import (
ctypes "github.com/tendermint/tendermint/rpc/core/types"
tmrpc "github.com/tendermint/tendermint/rpc/lib/server"
tmtypes "github.com/tendermint/tendermint/types"

distr "github.com/cosmos/cosmos-sdk/x/distribution"
distrRest "github.com/cosmos/cosmos-sdk/x/distribution/client/rest"
)

// makePathname creates a unique pathname for each test. It will panic if it
Expand Down Expand Up @@ -202,9 +198,8 @@ func (b AddrSeedSlice) Swap(i, j int) {
// their respective sockets where nValidators is the total number of validators
// and initAddrs are the accounts to initialize with some steak tokens. It
// returns a cleanup function, a set of validator public keys, and a port.
func InitializeTestLCD(
t *testing.T, nValidators int, initAddrs []sdk.AccAddress,
) (cleanup func(), valConsPubKeys []crypto.PubKey, valOperAddrs []sdk.ValAddress, port string) {
func InitializeTestLCD(t *testing.T, nValidators int, initAddrs []sdk.AccAddress, minting bool) (
cleanup func(), valConsPubKeys []crypto.PubKey, valOperAddrs []sdk.ValAddress, port string) {

if nValidators < 1 {
panic("InitializeTestLCD must use at least one validator")
Expand Down Expand Up @@ -239,16 +234,19 @@ func InitializeTestLCD(
operPrivKey := secp256k1.GenPrivKey()
operAddr := operPrivKey.PubKey().Address()
pubKey := privVal.GetPubKey()
delegation := 100

power := int64(100)
if i > 0 {
pubKey = ed25519.GenPrivKey().PubKey()
delegation = 1
power = 1
}
startTokens := staking.TokensFromTendermintPower(power)

msg := staking.NewMsgCreateValidator(
sdk.ValAddress(operAddr),
pubKey,
sdk.NewCoin(stakingTypes.DefaultBondDenom, sdk.NewInt(int64(delegation))),
staking.Description{Moniker: fmt.Sprintf("validator-%d", i+1)},
sdk.NewCoin(staking.DefaultBondDenom, startTokens),
staking.NewDescription(fmt.Sprintf("validator-%d", i+1), "", "", ""),
staking.NewCommissionMsg(sdk.ZeroDec(), sdk.ZeroDec(), sdk.ZeroDec()),
)
stdSignMsg := txbuilder.StdSignMsg{
Expand All @@ -260,12 +258,14 @@ func InitializeTestLCD(
tx := auth.NewStdTx([]sdk.Msg{msg}, auth.StdFee{}, []auth.StdSignature{{Signature: sig, PubKey: operPrivKey.PubKey()}}, "")
txBytes, err := cdc.MarshalJSON(tx)
require.Nil(t, err)

genTxs = append(genTxs, txBytes)
valConsPubKeys = append(valConsPubKeys, pubKey)
valOperAddrs = append(valOperAddrs, sdk.ValAddress(operAddr))

accAuth := auth.NewBaseAccountWithAddress(sdk.AccAddress(operAddr))
accAuth.Coins = sdk.Coins{sdk.NewInt64Coin(stakingTypes.DefaultBondDenom, 150)}
accTokens := staking.TokensFromTendermintPower(150)
accAuth.Coins = sdk.Coins{sdk.NewCoin(staking.DefaultBondDenom, accTokens)}
accs = append(accs, gapp.NewGenesisAccount(&accAuth))
}

Expand All @@ -279,15 +279,21 @@ func InitializeTestLCD(
// add some tokens to init accounts
for _, addr := range initAddrs {
accAuth := auth.NewBaseAccountWithAddress(addr)
accAuth.Coins = sdk.Coins{sdk.NewInt64Coin(stakingTypes.DefaultBondDenom, 100)}
accTokens := staking.TokensFromTendermintPower(100)
accAuth.Coins = sdk.Coins{sdk.NewCoin(staking.DefaultBondDenom, accTokens)}
acc := gapp.NewGenesisAccount(&accAuth)
genesisState.Accounts = append(genesisState.Accounts, acc)
genesisState.StakingData.Pool.NotBondedTokens = genesisState.StakingData.Pool.NotBondedTokens.Add(sdk.NewInt(100))
genesisState.StakingData.Pool.NotBondedTokens = genesisState.StakingData.Pool.NotBondedTokens.Add(accTokens)
}

inflationMin := sdk.MustNewDecFromStr("10000.0")
inflationMin := sdk.ZeroDec()
if minting {
inflationMin = sdk.MustNewDecFromStr("10000.0")
genesisState.MintData.Params.InflationMax = sdk.MustNewDecFromStr("15000.0")
} else {
genesisState.MintData.Params.InflationMax = inflationMin
}
genesisState.MintData.Minter.Inflation = inflationMin
genesisState.MintData.Params.InflationMax = sdk.MustNewDecFromStr("15000.0")
genesisState.MintData.Params.InflationMin = inflationMin

appState, err := codec.MarshalJSONIndent(cdc, genesisState)
Expand Down Expand Up @@ -335,6 +341,7 @@ func startTM(
tmcfg *tmcfg.Config, logger log.Logger, genDoc *tmtypes.GenesisDoc,
privVal tmtypes.PrivValidator, app abci.Application,
) (*nm.Node, error) {

genDocProvider := func() (*tmtypes.GenesisDoc, error) { return genDoc, nil }
dbProvider := func(*nm.DBContext) (dbm.DB, error) { return dbm.NewMemDB(), nil }
nodeKey, err := p2p.LoadOrGenNodeKey(tmcfg.NodeKeyFile())
Expand Down Expand Up @@ -384,12 +391,12 @@ func registerRoutes(rs *RestServer) {
keys.RegisterRoutes(rs.Mux, rs.CliCtx.Indent)
rpc.RegisterRoutes(rs.CliCtx, rs.Mux)
tx.RegisterRoutes(rs.CliCtx, rs.Mux, rs.Cdc)
authRest.RegisterRoutes(rs.CliCtx, rs.Mux, rs.Cdc, auth.StoreKey)
bankRest.RegisterRoutes(rs.CliCtx, rs.Mux, rs.Cdc, rs.KeyBase)
distrRest.RegisterRoutes(rs.CliCtx, rs.Mux, rs.Cdc, distr.StoreKey)
stakingRest.RegisterRoutes(rs.CliCtx, rs.Mux, rs.Cdc, rs.KeyBase)
slashingRest.RegisterRoutes(rs.CliCtx, rs.Mux, rs.Cdc, rs.KeyBase)
govRest.RegisterRoutes(rs.CliCtx, rs.Mux, rs.Cdc)
authrest.RegisterRoutes(rs.CliCtx, rs.Mux, rs.Cdc, auth.StoreKey)
bankrest.RegisterRoutes(rs.CliCtx, rs.Mux, rs.Cdc, rs.KeyBase)
distrrest.RegisterRoutes(rs.CliCtx, rs.Mux, rs.Cdc, distr.StoreKey)
stakingrest.RegisterRoutes(rs.CliCtx, rs.Mux, rs.Cdc, rs.KeyBase)
slashingrest.RegisterRoutes(rs.CliCtx, rs.Mux, rs.Cdc, rs.KeyBase)
govrest.RegisterRoutes(rs.CliCtx, rs.Mux, rs.Cdc)
}

// Request makes a test LCD test request. It returns a response object and a
Expand Down Expand Up @@ -678,7 +685,7 @@ func doTransferWithGas(
kb := client.MockKeyBase()

receiveInfo, _, err := kb.CreateMnemonic(
"receive_address", cryptoKeys.English, gapp.DefaultKeyPass, cryptoKeys.SigningAlgo("secp256k1"),
"receive_address", crkeys.English, gapp.DefaultKeyPass, crkeys.SigningAlgo("secp256k1"),
)
require.Nil(t, err)

Expand All @@ -700,7 +707,7 @@ func doTransferWithGas(
)

sr := rest.SendReq{
Amount: sdk.Coins{sdk.NewInt64Coin(stakingTypes.DefaultBondDenom, 1)},
Amount: sdk.Coins{sdk.NewInt64Coin(staking.DefaultBondDenom, 1)},
BaseReq: baseReq,
}

Expand All @@ -720,7 +727,7 @@ func doTransferWithGasAccAuto(
kb := client.MockKeyBase()

receiveInfo, _, err := kb.CreateMnemonic(
"receive_address", cryptoKeys.English, gapp.DefaultKeyPass, cryptoKeys.SigningAlgo("secp256k1"),
"receive_address", crkeys.English, gapp.DefaultKeyPass, crkeys.SigningAlgo("secp256k1"),
)
require.Nil(t, err)

Expand All @@ -733,7 +740,7 @@ func doTransferWithGasAccAuto(
)

sr := rest.SendReq{
Amount: sdk.Coins{sdk.NewInt64Coin(stakingTypes.DefaultBondDenom, 1)},
Amount: sdk.Coins{sdk.NewInt64Coin(staking.DefaultBondDenom, 1)},
BaseReq: baseReq,
}

Expand All @@ -755,7 +762,8 @@ type sendReq struct {

// POST /staking/delegators/{delegatorAddr}/delegations Submit delegation
func doDelegate(t *testing.T, port, name, password string,
delAddr sdk.AccAddress, valAddr sdk.ValAddress, amount int64, fees sdk.Coins) (resultTx sdk.TxResponse) {
delAddr sdk.AccAddress, valAddr sdk.ValAddress, amount sdk.Int, fees sdk.Coins) (resultTx sdk.TxResponse) {

acc := getAccount(t, port, delAddr)
accnum := acc.GetAccountNumber()
sequence := acc.GetSequence()
Expand All @@ -765,7 +773,7 @@ func doDelegate(t *testing.T, port, name, password string,
BaseReq: baseReq,
DelegatorAddr: delAddr,
ValidatorAddr: valAddr,
Delegation: sdk.NewInt64Coin(stakingTypes.DefaultBondDenom, amount),
Delegation: sdk.NewCoin(staking.DefaultBondDenom, amount),
}
req, err := cdc.MarshalJSON(msg)
require.NoError(t, err)
Expand All @@ -788,7 +796,7 @@ type msgDelegationsInput struct {

// POST /staking/delegators/{delegatorAddr}/delegations Submit delegation
func doUndelegate(t *testing.T, port, name, password string,
delAddr sdk.AccAddress, valAddr sdk.ValAddress, amount int64, fees sdk.Coins) (resultTx sdk.TxResponse) {
delAddr sdk.AccAddress, valAddr sdk.ValAddress, amount sdk.Int, fees sdk.Coins) (resultTx sdk.TxResponse) {

acc := getAccount(t, port, delAddr)
accnum := acc.GetAccountNumber()
Expand All @@ -799,7 +807,7 @@ func doUndelegate(t *testing.T, port, name, password string,
BaseReq: baseReq,
DelegatorAddr: delAddr,
ValidatorAddr: valAddr,
SharesAmount: sdk.NewDec(amount),
SharesAmount: sdk.NewDecFromInt(amount),
}
req, err := cdc.MarshalJSON(msg)
require.NoError(t, err)
Expand All @@ -823,7 +831,8 @@ type msgUndelegateInput struct {

// POST /staking/delegators/{delegatorAddr}/delegations Submit delegation
func doBeginRedelegation(t *testing.T, port, name, password string,
delAddr sdk.AccAddress, valSrcAddr, valDstAddr sdk.ValAddress, amount int64, fees sdk.Coins) (resultTx sdk.TxResponse) {
delAddr sdk.AccAddress, valSrcAddr, valDstAddr sdk.ValAddress, amount sdk.Int,
fees sdk.Coins) (resultTx sdk.TxResponse) {

acc := getAccount(t, port, delAddr)
accnum := acc.GetAccountNumber()
Expand All @@ -837,7 +846,7 @@ func doBeginRedelegation(t *testing.T, port, name, password string,
DelegatorAddr: delAddr,
ValidatorSrcAddr: valSrcAddr,
ValidatorDstAddr: valDstAddr,
SharesAmount: sdk.NewDec(amount),
SharesAmount: sdk.NewDecFromInt(amount),
}
req, err := cdc.MarshalJSON(msg)
require.NoError(t, err)
Expand Down Expand Up @@ -1056,7 +1065,9 @@ func getStakingParams(t *testing.T, port string) staking.Params {
// ICS 22 - Gov
// ----------------------------------------------------------------------
// POST /gov/proposals Submit a proposal
func doSubmitProposal(t *testing.T, port, seed, name, password string, proposerAddr sdk.AccAddress, amount int64, fees sdk.Coins) (resultTx sdk.TxResponse) {
func doSubmitProposal(t *testing.T, port, seed, name, password string, proposerAddr sdk.AccAddress,
amount sdk.Int, fees sdk.Coins) (resultTx sdk.TxResponse) {

acc := getAccount(t, port, proposerAddr)
accnum := acc.GetAccountNumber()
sequence := acc.GetSequence()
Expand All @@ -1068,7 +1079,7 @@ func doSubmitProposal(t *testing.T, port, seed, name, password string, proposerA
Description: "test",
ProposalType: "Text",
Proposer: proposerAddr,
InitialDeposit: sdk.Coins{sdk.NewCoin(stakingTypes.DefaultBondDenom, sdk.NewInt(amount))},
InitialDeposit: sdk.Coins{sdk.NewCoin(staking.DefaultBondDenom, amount)},
BaseReq: baseReq,
}

Expand Down Expand Up @@ -1151,7 +1162,8 @@ func getProposalsFilterStatus(t *testing.T, port string, status gov.ProposalStat
}

// POST /gov/proposals/{proposalId}/deposits Deposit tokens to a proposal
func doDeposit(t *testing.T, port, seed, name, password string, proposerAddr sdk.AccAddress, proposalID uint64, amount int64, fees sdk.Coins) (resultTx sdk.TxResponse) {
func doDeposit(t *testing.T, port, seed, name, password string, proposerAddr sdk.AccAddress, proposalID uint64,
amount sdk.Int, fees sdk.Coins) (resultTx sdk.TxResponse) {

acc := getAccount(t, port, proposerAddr)
accnum := acc.GetAccountNumber()
Expand All @@ -1161,7 +1173,7 @@ func doDeposit(t *testing.T, port, seed, name, password string, proposerAddr sdk

dr := rest.DepositReq{
Depositor: proposerAddr,
Amount: sdk.Coins{sdk.NewCoin(stakingTypes.DefaultBondDenom, sdk.NewInt(amount))},
Amount: sdk.Coins{sdk.NewCoin(staking.DefaultBondDenom, amount)},
BaseReq: baseReq,
}

Expand Down
6 changes: 2 additions & 4 deletions cmd/gaia/app/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,12 @@ import (
"github.com/cosmos/cosmos-sdk/x/mint"
"github.com/cosmos/cosmos-sdk/x/slashing"
"github.com/cosmos/cosmos-sdk/x/staking"
stakingTypes "github.com/cosmos/cosmos-sdk/x/staking/types"
)

var (
// bonded tokens given to genesis validators/accounts
freeFermionVal = int64(100)
freeFermionsAcc = sdk.NewInt(150)
bondDenom = stakingTypes.DefaultBondDenom
freeFermionsAcc = staking.TokensFromTendermintPower(150)
bondDenom = staking.DefaultBondDenom
)

// State to Unmarshal
Expand Down
9 changes: 4 additions & 5 deletions cmd/gaia/app/genesis_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ import (
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/x/auth"
"github.com/cosmos/cosmos-sdk/x/staking"
stakingTypes "github.com/cosmos/cosmos-sdk/x/staking/types"
)

var (
Expand Down Expand Up @@ -104,7 +103,7 @@ func TestGaiaAppGenState(t *testing.T) {

func makeMsg(name string, pk crypto.PubKey) auth.StdTx {
desc := staking.NewDescription(name, "", "", "")
comm := stakingTypes.CommissionMsg{}
comm := staking.CommissionMsg{}
msg := staking.NewMsgCreateValidator(sdk.ValAddress(pk.Address()), pk, sdk.NewInt64Coin(bondDenom,
50), desc, comm)
return auth.NewStdTx([]sdk.Msg{msg}, auth.StdFee{}, nil, "")
Expand All @@ -131,7 +130,7 @@ func TestGaiaGenesisValidation(t *testing.T) {

// require bonded + jailed validator fails validation
genesisState = makeGenesisState(t, genTxs)
val1 := stakingTypes.NewValidator(addr1, pk1, stakingTypes.Description{Moniker: "test #2"})
val1 := staking.NewValidator(addr1, pk1, staking.NewDescription("test #2", "", "", ""))
val1.Jailed = true
val1.Status = sdk.Bonded
genesisState.StakingData.Validators = append(genesisState.StakingData.Validators, val1)
Expand All @@ -141,7 +140,7 @@ func TestGaiaGenesisValidation(t *testing.T) {
// require duplicate validator fails validation
val1.Jailed = false
genesisState = makeGenesisState(t, genTxs)
val2 := stakingTypes.NewValidator(addr1, pk1, stakingTypes.Description{Moniker: "test #3"})
val2 := staking.NewValidator(addr1, pk1, staking.NewDescription("test #3", "", "", ""))
genesisState.StakingData.Validators = append(genesisState.StakingData.Validators, val1)
genesisState.StakingData.Validators = append(genesisState.StakingData.Validators, val2)
err = GaiaValidateGenesisState(genesisState)
Expand All @@ -152,7 +151,7 @@ func TestNewDefaultGenesisAccount(t *testing.T) {
addr := secp256k1.GenPrivKeySecp256k1([]byte("")).PubKey().Address()
acc := NewDefaultGenesisAccount(sdk.AccAddress(addr))
require.Equal(t, sdk.NewInt(1000), acc.Coins.AmountOf("footoken"))
require.Equal(t, sdk.NewInt(150), acc.Coins.AmountOf(bondDenom))
require.Equal(t, staking.TokensFromTendermintPower(150), acc.Coins.AmountOf(bondDenom))
}

func TestGenesisStateSanitize(t *testing.T) {
Expand Down
Loading

0 comments on commit 52f2ec7

Please sign in to comment.