Skip to content

Commit 6209058

Browse files
author
HaoyangLiu
authored
Merge pull request #758 from HaoyangLiu/haoyang/develop
R4R: Add unit tests and add valueable PRs from cosmos-sdk
2 parents 7c89dab + 6044b45 commit 6209058

File tree

127 files changed

+12888
-176
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

127 files changed

+12888
-176
lines changed

Makefile

+2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
PACKAGES_NOSIMULATION=$(shell go list ./... | grep -v '/simulation' | grep -v '/prometheus' | grep -v '/clitest' | grep -v '/lcd' | grep -v '/protobuf')
22
PACKAGES_MODULES=$(shell go list ./... | grep 'modules')
3+
PACKAGES_TYPES=$(shell go list ./... | grep 'types')
34
PACKAGES_SIMTEST=$(shell go list ./... | grep '/simulation')
45

56
all: get_tools get_vendor_deps install
@@ -116,6 +117,7 @@ test_sim: test_sim_modules test_sim_benchmark test_sim_iris_nondeterminism test_
116117
test_unit:
117118
#@go test $(PACKAGES_NOSIMULATION)
118119
@go test $(PACKAGES_MODULES)
120+
@go test $(PACKAGES_TYPES)
119121

120122
test_cli:
121123
@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

app/genesis.go

+12-13
Original file line numberDiff line numberDiff line change
@@ -25,15 +25,14 @@ import (
2525
tmtypes "github.com/tendermint/tendermint/types"
2626
"github.com/irisnet/irishub/modules/arbitration"
2727
"github.com/irisnet/irishub/modules/guardian"
28+
stakeTypes "github.com/irisnet/irishub/modules/stake/types"
2829
)
2930

3031
var (
31-
Denom = "iris"
32-
StakeDenom = Denom + "-" + types.Atto
3332
FeeAmt = int64(100)
34-
IrisCt = types.NewDefaultCoinType(Denom)
35-
FreeFermionVal, _ = IrisCt.ConvertToMinCoin(fmt.Sprintf("%d%s", FeeAmt, Denom))
36-
FreeFermionAcc, _ = IrisCt.ConvertToMinCoin(fmt.Sprintf("%d%s", int64(150), Denom))
33+
IrisCt = types.NewDefaultCoinType(stakeTypes.StakeDenomName)
34+
FreeFermionVal, _ = IrisCt.ConvertToMinCoin(fmt.Sprintf("%d%s", FeeAmt, stakeTypes.StakeDenomName))
35+
FreeFermionAcc, _ = IrisCt.ConvertToMinCoin(fmt.Sprintf("%d%s", int64(150), stakeTypes.StakeDenomName))
3736
)
3837

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

146145
for _, acc := range genesisState.Accounts {
147-
// create the genesis account, give'm few iris-atto and a buncha token with there name
146+
// create the genesis account, give'm few stake token and a buncha token with there name
148147
for _, coin := range acc.Coins {
149148
coinName, err := types.GetCoinName(coin)
150149
if err != nil {
151150
panic(fmt.Sprintf("fatal error: failed pick out demon from coin: %s", coin))
152151
}
153-
if coinName != Denom {
152+
if coinName != stakeTypes.StakeDenomName {
154153
continue
155154
}
156155
stakeToken, err := IrisCt.ConvertToMinCoin(coin)
157156
if err != nil {
158-
panic(fmt.Sprintf("fatal error: failed to convert %s to stake token: %s", StakeDenom, coin))
157+
panic(fmt.Sprintf("fatal error: failed to convert %s to stake token: %s", stakeTypes.StakeDenom, coin))
159158
}
160159
stakeData.Pool.LooseTokens = stakeData.Pool.LooseTokens.
161160
Add(sdk.NewDecFromInt(stakeToken.Amount)) // increase the supply
@@ -306,7 +305,7 @@ func createStakeGenesisState() stake.GenesisState {
306305
Params: stake.Params{
307306
UnbondingTime: defaultUnbondingTime,
308307
MaxValidators: 100,
309-
BondDenom: StakeDenom,
308+
BondDenom: stakeTypes.StakeDenom,
310309
},
311310
}
312311
}
@@ -315,7 +314,7 @@ func createMintGenesisState() mint.GenesisState {
315314
return mint.GenesisState{
316315
Minter: mint.InitialMinter(),
317316
Params: mint.Params{
318-
MintDenom: StakeDenom,
317+
MintDenom: stakeTypes.StakeDenom,
319318
InflationRateChange: sdk.NewDecWithPrec(13, 2),
320319
InflationMax: sdk.NewDecWithPrec(20, 2),
321320
InflationMin: sdk.NewDecWithPrec(7, 2),
@@ -327,16 +326,16 @@ func createMintGenesisState() mint.GenesisState {
327326
// normalize stake token to mini-unit
328327
func normalizeNativeToken(coins []string) sdk.Coins {
329328
var accountCoins sdk.Coins
330-
nativeCoin := sdk.NewInt64Coin(StakeDenom, 0)
329+
nativeCoin := sdk.NewInt64Coin(stakeTypes.StakeDenom, 0)
331330
for _, coin := range coins {
332331
coinName, err := types.GetCoinName(coin)
333332
if err != nil {
334333
panic(fmt.Sprintf("fatal error: failed pick out demon from coin: %s", coin))
335334
}
336-
if coinName == Denom {
335+
if coinName == stakeTypes.StakeDenomName {
337336
normalizeNativeToken, err := IrisCt.ConvertToMinCoin(coin)
338337
if err != nil {
339-
panic(fmt.Sprintf("fatal error in converting %s to %s", coin, StakeDenom))
338+
panic(fmt.Sprintf("fatal error in converting %s to %s", coin, stakeTypes.StakeDenom))
340339
}
341340
nativeCoin = nativeCoin.Plus(normalizeNativeToken)
342341
} else {

app/sim_test.go

+16-11
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,12 @@ import (
2020
"github.com/tendermint/tendermint/libs/log"
2121

2222
"github.com/irisnet/irishub/modules/gov"
23-
banksim "github.com/irisnet/irishub/simulation/bank"
24-
govsim "github.com/irisnet/irishub/simulation/gov"
25-
"github.com/irisnet/irishub/simulation/mock/simulation"
26-
slashingsim "github.com/irisnet/irishub/simulation/slashing"
27-
stakesim "github.com/irisnet/irishub/simulation/stake"
23+
banksim "github.com/irisnet/irishub/modules/bank/simulation"
24+
govsim "github.com/irisnet/irishub/modules/gov/simulation"
25+
"github.com/irisnet/irishub/modules/mock/simulation"
26+
slashingsim "github.com/irisnet/irishub/modules/slashing/simulation"
27+
stakesim "github.com/irisnet/irishub/modules/stake/simulation"
28+
stakeTypes "github.com/irisnet/irishub/modules/stake/types"
2829
)
2930

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

52-
var genesisAccounts []GenesisAccount
53+
var genesisAccounts []GenesisFileAccount
5354

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

6465
// Randomly generate some genesis accounts
6566
for _, acc := range accs {
6667
coins := sdk.Coins{
6768
{
68-
Denom: "iris-atto",
69+
Denom: stakeTypes.StakeDenom,
6970
Amount: amount,
7071
},
7172
{
7273
Denom: stakeGenesis.Params.BondDenom,
7374
Amount: stakeAmount,
7475
},
7576
}
76-
genesisAccounts = append(genesisAccounts, GenesisAccount{
77+
var coinsStringArray []string
78+
for _, coin := range coins {
79+
coinsStringArray = append(coinsStringArray, coin.String())
80+
}
81+
genesisAccounts = append(genesisAccounts, GenesisFileAccount{
7782
Address: acc.Address,
78-
Coins: coins,
83+
Coins: coinsStringArray,
7984
})
8085
}
8186

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

116-
genesis := GenesisState{
121+
genesis := GenesisFileState{
117122
Accounts: genesisAccounts,
118123
StakeData: stakeGenesis,
119124
MintData: mintGenesis,

client/bank/cli/sign.go

+1
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ recommended to set such parameters manually.`,
4040
cmd.Flags().Bool(flagAppend, true, "Append the signature to the existing ones. If disabled, old signatures would be overwritten")
4141
cmd.Flags().Bool(flagPrintSigs, false, "Print the addresses that must sign the transaction and those who have already signed it, then exit")
4242
cmd.Flags().Bool(flagOffline, false, "Offline mode. Do not query local cache.")
43+
cmd.MarkFlagRequired(client.FlagChainID)
4344
return cmd
4445
}
4546

client/context/query.go

+9-11
Original file line numberDiff line numberDiff line change
@@ -2,28 +2,26 @@ package context
22

33
import (
44
"fmt"
5-
6-
sdk "github.com/irisnet/irishub/types"
7-
"github.com/irisnet/irishub/modules/auth"
8-
9-
"github.com/pkg/errors"
10-
5+
"io/ioutil"
6+
"net/http"
117
"strings"
128

13-
"github.com/irisnet/irishub/store"
149
"github.com/irisnet/irishub/app"
10+
"github.com/irisnet/irishub/modules/auth"
11+
stakeTypes "github.com/irisnet/irishub/modules/stake/types"
12+
"github.com/irisnet/irishub/store"
1513
"github.com/irisnet/irishub/types"
14+
sdk "github.com/irisnet/irishub/types"
15+
"github.com/pkg/errors"
1616
abci "github.com/tendermint/tendermint/abci/types"
17+
"github.com/tendermint/tendermint/crypto/merkle"
1718
cmn "github.com/tendermint/tendermint/libs/common"
1819
tmliteErr "github.com/tendermint/tendermint/lite/errors"
1920
tmliteProxy "github.com/tendermint/tendermint/lite/proxy"
20-
"github.com/tendermint/tendermint/crypto/merkle"
2121
rpcclient "github.com/tendermint/tendermint/rpc/client"
2222
tmclient "github.com/tendermint/tendermint/rpc/client"
2323
ctypes "github.com/tendermint/tendermint/rpc/core/types"
2424
tmtypes "github.com/tendermint/tendermint/types"
25-
"io/ioutil"
26-
"net/http"
2725
)
2826

2927
// GetNode returns an RPC client. If the context's client is not defined, an
@@ -284,7 +282,7 @@ func (cliCtx CLIContext) GetCoinType(coinName string) (types.CoinType, error) {
284282
if coinName == "" {
285283
return types.CoinType{}, fmt.Errorf("coin name is empty")
286284
}
287-
if coinName == app.Denom {
285+
if coinName == stakeTypes.StakeDenomName {
288286
coinType = app.IrisCt
289287
} else {
290288
key := types.CoinTypeKey(coinName)

client/distribution/utils.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@ package distribution
33
import (
44
sdk "github.com/irisnet/irishub/types"
55
"github.com/irisnet/irishub/modules/distribution"
6-
"github.com/irisnet/irishub/app"
76
"github.com/irisnet/irishub/client/context"
87
"github.com/irisnet/irishub/client/utils"
8+
stakeTypes "github.com/irisnet/irishub/modules/stake/types"
99
)
1010

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

2020
func ConvertToValidatorDistInfoOutput(cliCtx context.CLIContext, vdi distribution.ValidatorDistInfo) ValidatorDistInfoOutput {
2121
exRate := utils.ExRateFromStakeTokenToMainUnit(cliCtx)
22-
delPool := utils.ConvertDecToRat(vdi.DelPool.AmountOf(app.Denom+"-"+"atto")).Mul(exRate).FloatString() + app.Denom
23-
valCommission := utils.ConvertDecToRat(vdi.ValCommission.AmountOf(app.Denom+"-"+"atto")).Mul(exRate).FloatString() + app.Denom
22+
delPool := utils.ConvertDecToRat(vdi.DelPool.AmountOf(stakeTypes.StakeDenom)).Mul(exRate).FloatString() + stakeTypes.StakeDenomName
23+
valCommission := utils.ConvertDecToRat(vdi.ValCommission.AmountOf(stakeTypes.StakeDenom)).Mul(exRate).FloatString() + stakeTypes.StakeDenomName
2424
return ValidatorDistInfoOutput{
2525
OperatorAddr: vdi.OperatorAddr,
2626
FeePoolWithdrawalHeight: vdi.FeePoolWithdrawalHeight,

client/lcd/swaggerui/swagger.yaml

+24
Original file line numberDiff line numberDiff line change
@@ -1201,6 +1201,30 @@ paths:
12011201
description: Invalid validator address
12021202
500:
12031203
description: Internal Server Error
1204+
/stake/validators/{validatorAddr}/delegations:
1205+
parameters:
1206+
- in: path
1207+
name: validatorAddr
1208+
description: Bech32 OperatorAddress of validator
1209+
required: true
1210+
type: string
1211+
get:
1212+
summary: Get all delegations from a validator
1213+
tags:
1214+
- ICS21
1215+
produces:
1216+
- application/json
1217+
responses:
1218+
200:
1219+
description: OK
1220+
schema:
1221+
type: array
1222+
items:
1223+
$ref: "#/definitions/Delegation"
1224+
400:
1225+
description: Invalid validator address
1226+
500:
1227+
description: Internal Server Error
12041228
/stake/validators/{validatorAddr}/unbonding_delegations:
12051229
parameters:
12061230
- in: path

client/stake/cli/query.go

+42-13
Original file line numberDiff line numberDiff line change
@@ -11,16 +11,17 @@ import (
1111
sdk "github.com/irisnet/irishub/types"
1212
"github.com/irisnet/irishub/modules/stake"
1313
"github.com/irisnet/irishub/modules/stake/types"
14+
"github.com/irisnet/irishub/modules/stake/querier"
1415
"github.com/irisnet/irishub/client/context"
1516
stakeClient "github.com/irisnet/irishub/client/stake"
1617
)
1718

1819
// GetCmdQueryValidator implements the validator query command.
1920
func GetCmdQueryValidator(storeName string, cdc *codec.Codec) *cobra.Command {
2021
cmd := &cobra.Command{
21-
Use: "validator [owner-addr]",
22+
Use: "validator [validator-address]",
2223
Short: "Query a validator",
23-
Example: "iriscli stake validator <validator owner address>",
24+
Example: "iriscli stake validator <validator address>",
2425
Args: cobra.ExactArgs(1),
2526
RunE: func(cmd *cobra.Command, args []string) error {
2627
addr, err := sdk.ValAddressFromBech32(args[0])
@@ -128,18 +129,17 @@ func GetCmdQueryValidators(storeName string, cdc *codec.Codec) *cobra.Command {
128129
// GetCmdQueryValidatorUnbondingDelegations implements the query all unbonding delegatations from a validator command.
129130
func GetCmdQueryValidatorUnbondingDelegations(queryRoute string, cdc *codec.Codec) *cobra.Command {
130131
cmd := &cobra.Command{
131-
Use: "unbonding-delegations-from [operator-addr]",
132+
Use: "unbonding-delegations-from [validator-address]",
132133
Short: "Query all unbonding delegatations from a validator",
134+
Example: "iriscli stake unbonding-delegations-from <validator address>",
133135
Args: cobra.ExactArgs(1),
134136
RunE: func(cmd *cobra.Command, args []string) error {
135137
valAddr, err := sdk.ValAddressFromBech32(args[0])
136138
if err != nil {
137139
return err
138140
}
139141
cliCtx := context.NewCLIContext().WithCodec(cdc)
140-
params := stake.QueryValidatorParams{
141-
ValidatorAddr: valAddr,
142-
}
142+
params := querier.NewQueryValidatorParams(valAddr)
143143
bz, err := cdc.MarshalJSON(params)
144144
if err != nil {
145145
return err
@@ -159,18 +159,17 @@ func GetCmdQueryValidatorUnbondingDelegations(queryRoute string, cdc *codec.Code
159159
// GetCmdQueryValidatorRedelegations implements the query all redelegatations from a validator command.
160160
func GetCmdQueryValidatorRedelegations(queryRoute string, cdc *codec.Codec) *cobra.Command {
161161
cmd := &cobra.Command{
162-
Use: "redelegations-from [operator-addr]",
162+
Use: "redelegations-from [validator-address]",
163163
Short: "Query all outgoing redelegatations from a validator",
164+
Example: "iriscli stake redelegations-from <validator address>",
164165
Args: cobra.ExactArgs(1),
165166
RunE: func(cmd *cobra.Command, args []string) error {
166167
valAddr, err := sdk.ValAddressFromBech32(args[0])
167168
if err != nil {
168169
return err
169170
}
170171
cliCtx := context.NewCLIContext().WithCodec(cdc)
171-
params := stake.QueryValidatorParams{
172-
ValidatorAddr: valAddr,
173-
}
172+
params := querier.NewQueryValidatorParams(valAddr)
174173
bz, err := cdc.MarshalJSON(params)
175174
if err != nil {
176175
return err
@@ -250,7 +249,7 @@ func GetCmdQueryDelegation(storeName string, cdc *codec.Codec) *cobra.Command {
250249
// made from one delegator.
251250
func GetCmdQueryDelegations(storeName string, cdc *codec.Codec) *cobra.Command {
252251
cmd := &cobra.Command{
253-
Use: "delegations [delegator-addr]",
252+
Use: "delegations [delegator-address]",
254253
Short: "Query all delegations made from one delegator",
255254
Example: "iriscli stake delegations <delegator address>",
256255
Args: cobra.ExactArgs(1),
@@ -291,6 +290,36 @@ func GetCmdQueryDelegations(storeName string, cdc *codec.Codec) *cobra.Command {
291290
return cmd
292291
}
293292

293+
// GetCmdQueryValidatorDelegations implements the command to query all the
294+
// delegations to a specific validator.
295+
func GetCmdQueryValidatorDelegations(queryRoute string, cdc *codec.Codec) *cobra.Command {
296+
cmd := &cobra.Command{
297+
Use: "delegations-to [validator-address]",
298+
Short: "Query all delegations made to one validator",
299+
Example: "iriscli stake delegations-to <validator address>",
300+
Args: cobra.ExactArgs(1),
301+
RunE: func(cmd *cobra.Command, args []string) error {
302+
validatorAddr, err := sdk.ValAddressFromBech32(args[0])
303+
if err != nil {
304+
return err
305+
}
306+
params := querier.NewQueryValidatorParams(validatorAddr)
307+
bz, err := cdc.MarshalJSON(params)
308+
if err != nil {
309+
return err
310+
}
311+
cliCtx := context.NewCLIContext().WithCodec(cdc)
312+
res, err := cliCtx.QueryWithData(fmt.Sprintf("custom/%s/validatorDelegations", queryRoute), bz)
313+
if err != nil {
314+
return err
315+
}
316+
fmt.Println(string(res))
317+
return nil
318+
},
319+
}
320+
return cmd
321+
}
322+
294323
// GetCmdQueryUnbondingDelegation implements the command to query a single
295324
// unbonding-delegation record.
296325
func GetCmdQueryUnbondingDelegation(storeName string, cdc *codec.Codec) *cobra.Command {
@@ -355,7 +384,7 @@ func GetCmdQueryUnbondingDelegation(storeName string, cdc *codec.Codec) *cobra.C
355384
// unbonding-delegation records for a delegator.
356385
func GetCmdQueryUnbondingDelegations(storeName string, cdc *codec.Codec) *cobra.Command {
357386
cmd := &cobra.Command{
358-
Use: "unbonding-delegations [delegator-addr]",
387+
Use: "unbonding-delegations [delegator-address]",
359388
Short: "Query all unbonding-delegations records for one delegator",
360389
Example: "iriscli stake unbonding-delegation <delegator address>",
361390
Args: cobra.ExactArgs(1),
@@ -465,7 +494,7 @@ func GetCmdQueryRedelegation(storeName string, cdc *codec.Codec) *cobra.Command
465494
// redelegation records for a delegator.
466495
func GetCmdQueryRedelegations(storeName string, cdc *codec.Codec) *cobra.Command {
467496
cmd := &cobra.Command{
468-
Use: "redelegations [delegator-addr]",
497+
Use: "redelegations [delegator-address]",
469498
Short: "Query all redelegations records for one delegator",
470499
Example: "iriscli stake redelegations <delegator address>",
471500
Args: cobra.ExactArgs(1),

0 commit comments

Comments
 (0)