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
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
38 changes: 32 additions & 6 deletions client/stake/cli/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ 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"
)
Expand Down Expand Up @@ -137,9 +138,7 @@ func GetCmdQueryValidatorUnbondingDelegations(queryRoute string, cdc *codec.Code
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 @@ -168,9 +167,7 @@ func GetCmdQueryValidatorRedelegations(queryRoute string, cdc *codec.Codec) *cob
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 @@ -291,6 +288,35 @@ 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-addr]",
Short: "Query all delegations made to one validator",
abelliumnt marked this conversation as resolved.
Show resolved Hide resolved
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
3 changes: 1 addition & 2 deletions client/stake/cli/sendtx.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import (
"github.com/irisnet/irishub/modules/stake"
"github.com/irisnet/irishub/modules/stake/types"

"github.com/irisnet/irishub/app"
"github.com/irisnet/irishub/client/context"
stakeClient "github.com/irisnet/irishub/client/stake"
"github.com/irisnet/irishub/client/utils"
Expand Down Expand Up @@ -299,7 +298,7 @@ func getShares(
return sharesAmount, errors.Errorf("shares amount must be positive number (ex. 123, 1.23456789)")
}

stakeTokenDenom, err := cliCtx.GetCoinType(app.Denom)
stakeTokenDenom, err := cliCtx.GetCoinType(types.StakeDenomName)
if err != nil {
panic(err)
}
Expand Down
11 changes: 11 additions & 0 deletions client/stake/lcd/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,12 @@ func registerQueryRoutes(cliCtx context.CLIContext, r *mux.Router, cdc *codec.Co
validatorHandlerFn(cliCtx, cdc),
).Methods("GET")

// Get all delegations to a validator
r.HandleFunc(
"/stake/validators/{validatorAddr}/delegations",
validatorDelegationsHandlerFn(cliCtx, cdc),
).Methods("GET")

// Get all unbonding delegations from a validator
r.HandleFunc(
"/stake/validators/{validatorAddr}/unbonding_delegations",
Expand Down Expand Up @@ -235,6 +241,11 @@ func validatorHandlerFn(cliCtx context.CLIContext, cdc *codec.Codec) http.Handle
return queryValidator(cliCtx, cdc, "custom/stake/validator")
}

// HTTP request handler to query all unbonding delegations from a validator
func validatorDelegationsHandlerFn(cliCtx context.CLIContext, cdc *codec.Codec) http.HandlerFunc {
return queryValidator(cliCtx, cdc, "custom/stake/validatorDelegations")
}

// HTTP request handler to query all unbonding delegations from a validator
func validatorUnbondingDelegationsHandlerFn(cliCtx context.CLIContext, cdc *codec.Codec) http.HandlerFunc {
return queryValidator(cliCtx, cdc, "custom/stake/validatorUnbondingDelegations")
Expand Down
Loading