Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

R4R: Add unit tests and add valueable PRs from cosmos-sdk #758

Merged
merged 12 commits into from
Dec 5, 2018
2 changes: 2 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
PACKAGES_NOSIMULATION=$(shell go list ./... | grep -v '/simulation' | grep -v '/prometheus' | grep -v '/clitest' | grep -v '/lcd' | grep -v '/protobuf')
PACKAGES_MODULES=$(shell go list ./... | grep 'modules')
PACKAGES_TYPES=$(shell go list ./... | grep 'types')
PACKAGES_SIMTEST=$(shell go list ./... | grep '/simulation')

all: get_tools get_vendor_deps install
Expand Down Expand Up @@ -119,6 +120,7 @@ test_sim: test_sim_modules test_sim_benchmark test_sim_iris_nondeterminism test_
test_unit:
#@go test $(PACKAGES_NOSIMULATION)
@go test $(PACKAGES_MODULES)
@go test $(PACKAGES_TYPES)

test_cli:
@go test -timeout 20m -count 1 -p 1 client/clitest/utils.go client/clitest/bank_test.go client/clitest/distribution_test.go client/clitest/gov_test.go client/clitest/iparam_test.go client/clitest/irismon_test.go client/clitest/record_test.go client/clitest/service_test.go client/clitest/stake_test.go
Expand Down
25 changes: 12 additions & 13 deletions app/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,14 @@ import (
tmtypes "github.com/tendermint/tendermint/types"
"github.com/irisnet/irishub/modules/arbitration"
"github.com/irisnet/irishub/modules/guardian"
stakeTypes "github.com/irisnet/irishub/modules/stake/types"
)

var (
Denom = "iris"
StakeDenom = Denom + "-" + types.Atto
FeeAmt = int64(100)
IrisCt = types.NewDefaultCoinType(Denom)
FreeFermionVal, _ = IrisCt.ConvertToMinCoin(fmt.Sprintf("%d%s", FeeAmt, Denom))
FreeFermionAcc, _ = IrisCt.ConvertToMinCoin(fmt.Sprintf("%d%s", int64(150), Denom))
IrisCt = types.NewDefaultCoinType(stakeTypes.StakeDenomName)
FreeFermionVal, _ = IrisCt.ConvertToMinCoin(fmt.Sprintf("%d%s", FeeAmt, stakeTypes.StakeDenomName))
FreeFermionAcc, _ = IrisCt.ConvertToMinCoin(fmt.Sprintf("%d%s", int64(150), stakeTypes.StakeDenomName))
)

const (
Expand Down Expand Up @@ -144,18 +143,18 @@ func IrisAppGenState(cdc *codec.Codec, genDoc tmtypes.GenesisDoc, appGenTxs []js
}

for _, acc := range genesisState.Accounts {
// create the genesis account, give'm few iris-atto and a buncha token with there name
// create the genesis account, give'm few stake token and a buncha token with there name
for _, coin := range acc.Coins {
coinName, err := types.GetCoinName(coin)
if err != nil {
panic(fmt.Sprintf("fatal error: failed pick out demon from coin: %s", coin))
}
if coinName != Denom {
if coinName != stakeTypes.StakeDenomName {
continue
}
stakeToken, err := IrisCt.ConvertToMinCoin(coin)
if err != nil {
panic(fmt.Sprintf("fatal error: failed to convert %s to stake token: %s", StakeDenom, coin))
panic(fmt.Sprintf("fatal error: failed to convert %s to stake token: %s", stakeTypes.StakeDenom, coin))
}
stakeData.Pool.LooseTokens = stakeData.Pool.LooseTokens.
Add(sdk.NewDecFromInt(stakeToken.Amount)) // increase the supply
Expand Down Expand Up @@ -306,7 +305,7 @@ func createStakeGenesisState() stake.GenesisState {
Params: stake.Params{
UnbondingTime: defaultUnbondingTime,
MaxValidators: 100,
BondDenom: StakeDenom,
BondDenom: stakeTypes.StakeDenom,
},
}
}
Expand All @@ -315,7 +314,7 @@ func createMintGenesisState() mint.GenesisState {
return mint.GenesisState{
Minter: mint.InitialMinter(),
Params: mint.Params{
MintDenom: StakeDenom,
MintDenom: stakeTypes.StakeDenom,
InflationRateChange: sdk.NewDecWithPrec(13, 2),
InflationMax: sdk.NewDecWithPrec(20, 2),
InflationMin: sdk.NewDecWithPrec(7, 2),
Expand All @@ -327,16 +326,16 @@ func createMintGenesisState() mint.GenesisState {
// normalize stake token to mini-unit
func normalizeNativeToken(coins []string) sdk.Coins {
var accountCoins sdk.Coins
nativeCoin := sdk.NewInt64Coin(StakeDenom, 0)
nativeCoin := sdk.NewInt64Coin(stakeTypes.StakeDenom, 0)
for _, coin := range coins {
coinName, err := types.GetCoinName(coin)
if err != nil {
panic(fmt.Sprintf("fatal error: failed pick out demon from coin: %s", coin))
}
if coinName == Denom {
if coinName == stakeTypes.StakeDenomName {
normalizeNativeToken, err := IrisCt.ConvertToMinCoin(coin)
if err != nil {
panic(fmt.Sprintf("fatal error in converting %s to %s", coin, StakeDenom))
panic(fmt.Sprintf("fatal error in converting %s to %s", coin, stakeTypes.StakeDenom))
}
nativeCoin = nativeCoin.Plus(normalizeNativeToken)
} else {
Expand Down
27 changes: 16 additions & 11 deletions app/sim_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,12 @@ import (
"github.com/tendermint/tendermint/libs/log"

"github.com/irisnet/irishub/modules/gov"
banksim "github.com/irisnet/irishub/simulation/bank"
govsim "github.com/irisnet/irishub/simulation/gov"
"github.com/irisnet/irishub/simulation/mock/simulation"
slashingsim "github.com/irisnet/irishub/simulation/slashing"
stakesim "github.com/irisnet/irishub/simulation/stake"
banksim "github.com/irisnet/irishub/modules/bank/simulation"
govsim "github.com/irisnet/irishub/modules/gov/simulation"
"github.com/irisnet/irishub/modules/mock/simulation"
slashingsim "github.com/irisnet/irishub/modules/slashing/simulation"
stakesim "github.com/irisnet/irishub/modules/stake/simulation"
stakeTypes "github.com/irisnet/irishub/modules/stake/types"
)

var (
Expand All @@ -49,7 +50,7 @@ func appStateFn(r *rand.Rand, accs []simulation.Account) json.RawMessage {
stakeGenesis := stake.DefaultGenesisState()
fmt.Printf("Selected randomly generated staking parameters: %+v\n", stakeGenesis)

var genesisAccounts []GenesisAccount
var genesisAccounts []GenesisFileAccount

amount := sdk.NewIntWithDecimal(100, 18)
stakeAmount := sdk.NewIntWithDecimal(1, 2)
Expand All @@ -59,23 +60,27 @@ func appStateFn(r *rand.Rand, accs []simulation.Account) json.RawMessage {
if numInitiallyBonded > numAccs {
numInitiallyBonded = numAccs
}
fmt.Printf("Selected randomly generated parameters for simulated genesis: {amount of iris-atto per account: %v, initially bonded validators: %v}\n", amount, numInitiallyBonded)
fmt.Printf("Selected randomly generated parameters for simulated genesis: {amount of %s per account: %v, initially bonded validators: %v}\n", stakeTypes.StakeDenom, amount, numInitiallyBonded)

// Randomly generate some genesis accounts
for _, acc := range accs {
coins := sdk.Coins{
{
Denom: "iris-atto",
Denom: stakeTypes.StakeDenom,
Amount: amount,
},
{
Denom: stakeGenesis.Params.BondDenom,
Amount: stakeAmount,
},
}
genesisAccounts = append(genesisAccounts, GenesisAccount{
var coinsStringArray []string
for _, coin := range coins {
coinsStringArray = append(coinsStringArray, coin.String())
}
genesisAccounts = append(genesisAccounts, GenesisFileAccount{
Address: acc.Address,
Coins: coins,
Coins: coinsStringArray,
})
}

Expand Down Expand Up @@ -113,7 +118,7 @@ func appStateFn(r *rand.Rand, accs []simulation.Account) json.RawMessage {
stakeGenesis.Validators = validators
stakeGenesis.Bonds = delegations

genesis := GenesisState{
genesis := GenesisFileState{
Accounts: genesisAccounts,
StakeData: stakeGenesis,
MintData: mintGenesis,
Expand Down
1 change: 1 addition & 0 deletions client/bank/cli/sign.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ recommended to set such parameters manually.`,
cmd.Flags().Bool(flagAppend, true, "Append the signature to the existing ones. If disabled, old signatures would be overwritten")
cmd.Flags().Bool(flagPrintSigs, false, "Print the addresses that must sign the transaction and those who have already signed it, then exit")
cmd.Flags().Bool(flagOffline, false, "Offline mode. Do not query local cache.")
cmd.MarkFlagRequired(client.FlagChainID)
return cmd
}

Expand Down
20 changes: 9 additions & 11 deletions client/context/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,28 +2,26 @@ package context

import (
"fmt"

sdk "github.com/irisnet/irishub/types"
"github.com/irisnet/irishub/modules/auth"

"github.com/pkg/errors"

"io/ioutil"
"net/http"
"strings"

"github.com/irisnet/irishub/store"
"github.com/irisnet/irishub/app"
"github.com/irisnet/irishub/modules/auth"
stakeTypes "github.com/irisnet/irishub/modules/stake/types"
"github.com/irisnet/irishub/store"
"github.com/irisnet/irishub/types"
sdk "github.com/irisnet/irishub/types"
"github.com/pkg/errors"
abci "github.com/tendermint/tendermint/abci/types"
"github.com/tendermint/tendermint/crypto/merkle"
cmn "github.com/tendermint/tendermint/libs/common"
tmliteErr "github.com/tendermint/tendermint/lite/errors"
tmliteProxy "github.com/tendermint/tendermint/lite/proxy"
"github.com/tendermint/tendermint/crypto/merkle"
rpcclient "github.com/tendermint/tendermint/rpc/client"
tmclient "github.com/tendermint/tendermint/rpc/client"
ctypes "github.com/tendermint/tendermint/rpc/core/types"
tmtypes "github.com/tendermint/tendermint/types"
"io/ioutil"
"net/http"
)

// GetNode returns an RPC client. If the context's client is not defined, an
Expand Down Expand Up @@ -284,7 +282,7 @@ func (cliCtx CLIContext) GetCoinType(coinName string) (types.CoinType, error) {
if coinName == "" {
return types.CoinType{}, fmt.Errorf("coin name is empty")
}
if coinName == app.Denom {
if coinName == stakeTypes.StakeDenomName {
coinType = app.IrisCt
} else {
key := types.CoinTypeKey(coinName)
Expand Down
6 changes: 3 additions & 3 deletions client/distribution/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ package distribution
import (
sdk "github.com/irisnet/irishub/types"
"github.com/irisnet/irishub/modules/distribution"
"github.com/irisnet/irishub/app"
"github.com/irisnet/irishub/client/context"
"github.com/irisnet/irishub/client/utils"
stakeTypes "github.com/irisnet/irishub/modules/stake/types"
)

// distribution info for a particular validator
Expand All @@ -19,8 +19,8 @@ type ValidatorDistInfoOutput struct {

func ConvertToValidatorDistInfoOutput(cliCtx context.CLIContext, vdi distribution.ValidatorDistInfo) ValidatorDistInfoOutput {
exRate := utils.ExRateFromStakeTokenToMainUnit(cliCtx)
delPool := utils.ConvertDecToRat(vdi.DelPool.AmountOf(app.Denom+"-"+"atto")).Mul(exRate).FloatString() + app.Denom
valCommission := utils.ConvertDecToRat(vdi.ValCommission.AmountOf(app.Denom+"-"+"atto")).Mul(exRate).FloatString() + app.Denom
delPool := utils.ConvertDecToRat(vdi.DelPool.AmountOf(stakeTypes.StakeDenom)).Mul(exRate).FloatString() + stakeTypes.StakeDenomName
valCommission := utils.ConvertDecToRat(vdi.ValCommission.AmountOf(stakeTypes.StakeDenom)).Mul(exRate).FloatString() + stakeTypes.StakeDenomName
return ValidatorDistInfoOutput{
OperatorAddr: vdi.OperatorAddr,
FeePoolWithdrawalHeight: vdi.FeePoolWithdrawalHeight,
Expand Down
24 changes: 24 additions & 0 deletions client/lcd/swaggerui/swagger.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -1201,6 +1201,30 @@ paths:
description: Invalid validator address
500:
description: Internal Server Error
/stake/validators/{validatorAddr}/delegations:
parameters:
- in: path
name: validatorAddr
description: Bech32 OperatorAddress of validator
required: true
type: string
get:
summary: Get all delegations from a validator
tags:
- ICS21
produces:
- application/json
responses:
200:
description: OK
schema:
type: array
items:
$ref: "#/definitions/Delegation"
400:
description: Invalid validator address
500:
description: Internal Server Error
/stake/validators/{validatorAddr}/unbonding_delegations:
parameters:
- in: path
Expand Down
55 changes: 42 additions & 13 deletions client/stake/cli/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,17 @@ import (
sdk "github.com/irisnet/irishub/types"
"github.com/irisnet/irishub/modules/stake"
"github.com/irisnet/irishub/modules/stake/types"
"github.com/irisnet/irishub/modules/stake/querier"
"github.com/irisnet/irishub/client/context"
stakeClient "github.com/irisnet/irishub/client/stake"
)

// GetCmdQueryValidator implements the validator query command.
func GetCmdQueryValidator(storeName string, cdc *codec.Codec) *cobra.Command {
cmd := &cobra.Command{
Use: "validator [owner-addr]",
Use: "validator [validator-address]",
Short: "Query a validator",
Example: "iriscli stake validator <validator owner address>",
Example: "iriscli stake validator <validator address>",
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
addr, err := sdk.ValAddressFromBech32(args[0])
Expand Down Expand Up @@ -128,18 +129,17 @@ func GetCmdQueryValidators(storeName string, cdc *codec.Codec) *cobra.Command {
// GetCmdQueryValidatorUnbondingDelegations implements the query all unbonding delegatations from a validator command.
func GetCmdQueryValidatorUnbondingDelegations(queryRoute string, cdc *codec.Codec) *cobra.Command {
cmd := &cobra.Command{
Use: "unbonding-delegations-from [operator-addr]",
Use: "unbonding-delegations-from [validator-address]",
Short: "Query all unbonding delegatations from a validator",
Example: "iriscli stake unbonding-delegations-from <validator address>",
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
valAddr, err := sdk.ValAddressFromBech32(args[0])
if err != nil {
return err
}
cliCtx := context.NewCLIContext().WithCodec(cdc)
params := stake.QueryValidatorParams{
ValidatorAddr: valAddr,
}
params := querier.NewQueryValidatorParams(valAddr)
bz, err := cdc.MarshalJSON(params)
if err != nil {
return err
Expand All @@ -159,18 +159,17 @@ func GetCmdQueryValidatorUnbondingDelegations(queryRoute string, cdc *codec.Code
// GetCmdQueryValidatorRedelegations implements the query all redelegatations from a validator command.
func GetCmdQueryValidatorRedelegations(queryRoute string, cdc *codec.Codec) *cobra.Command {
cmd := &cobra.Command{
Use: "redelegations-from [operator-addr]",
Use: "redelegations-from [validator-address]",
Short: "Query all outgoing redelegatations from a validator",
Example: "iriscli stake redelegations-from <validator address>",
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
valAddr, err := sdk.ValAddressFromBech32(args[0])
if err != nil {
return err
}
cliCtx := context.NewCLIContext().WithCodec(cdc)
params := stake.QueryValidatorParams{
ValidatorAddr: valAddr,
}
params := querier.NewQueryValidatorParams(valAddr)
bz, err := cdc.MarshalJSON(params)
if err != nil {
return err
Expand Down Expand Up @@ -250,7 +249,7 @@ func GetCmdQueryDelegation(storeName string, cdc *codec.Codec) *cobra.Command {
// made from one delegator.
func GetCmdQueryDelegations(storeName string, cdc *codec.Codec) *cobra.Command {
cmd := &cobra.Command{
Use: "delegations [delegator-addr]",
Use: "delegations [delegator-address]",
Short: "Query all delegations made from one delegator",
Example: "iriscli stake delegations <delegator address>",
Args: cobra.ExactArgs(1),
Expand Down Expand Up @@ -291,6 +290,36 @@ func GetCmdQueryDelegations(storeName string, cdc *codec.Codec) *cobra.Command {
return cmd
}

// GetCmdQueryValidatorDelegations implements the command to query all the
// delegations to a specific validator.
func GetCmdQueryValidatorDelegations(queryRoute string, cdc *codec.Codec) *cobra.Command {
cmd := &cobra.Command{
Use: "delegations-to [validator-address]",
Short: "Query all delegations made to one validator",
abelliumnt marked this conversation as resolved.
Show resolved Hide resolved
Example: "iriscli stake delegations-to <validator address>",
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
validatorAddr, err := sdk.ValAddressFromBech32(args[0])
if err != nil {
return err
}
params := querier.NewQueryValidatorParams(validatorAddr)
bz, err := cdc.MarshalJSON(params)
if err != nil {
return err
}
cliCtx := context.NewCLIContext().WithCodec(cdc)
res, err := cliCtx.QueryWithData(fmt.Sprintf("custom/%s/validatorDelegations", queryRoute), bz)
if err != nil {
return err
}
fmt.Println(string(res))
return nil
},
}
return cmd
}

// GetCmdQueryUnbondingDelegation implements the command to query a single
// unbonding-delegation record.
func GetCmdQueryUnbondingDelegation(storeName string, cdc *codec.Codec) *cobra.Command {
Expand Down Expand Up @@ -355,7 +384,7 @@ func GetCmdQueryUnbondingDelegation(storeName string, cdc *codec.Codec) *cobra.C
// unbonding-delegation records for a delegator.
func GetCmdQueryUnbondingDelegations(storeName string, cdc *codec.Codec) *cobra.Command {
cmd := &cobra.Command{
Use: "unbonding-delegations [delegator-addr]",
Use: "unbonding-delegations [delegator-address]",
Short: "Query all unbonding-delegations records for one delegator",
Example: "iriscli stake unbonding-delegation <delegator address>",
Args: cobra.ExactArgs(1),
Expand Down Expand Up @@ -465,7 +494,7 @@ func GetCmdQueryRedelegation(storeName string, cdc *codec.Codec) *cobra.Command
// redelegation records for a delegator.
func GetCmdQueryRedelegations(storeName string, cdc *codec.Codec) *cobra.Command {
cmd := &cobra.Command{
Use: "redelegations [delegator-addr]",
Use: "redelegations [delegator-address]",
Short: "Query all redelegations records for one delegator",
Example: "iriscli stake redelegations <delegator address>",
Args: cobra.ExactArgs(1),
Expand Down
Loading