From 9142caa8ee0f01b06dd8db032725cf8d93c0b73e Mon Sep 17 00:00:00 2001 From: Federico Kunze Date: Mon, 20 Aug 2018 15:54:21 +0200 Subject: [PATCH 01/11] Added gaia-lite staking endpoints for params and pool --- x/stake/client/rest/query.go | 72 ++++++++++++++++++++++++++++++++++++ x/stake/types/params.go | 19 ++++++++++ x/stake/types/pool.go | 19 ++++++++++ 3 files changed, 110 insertions(+) diff --git a/x/stake/client/rest/query.go b/x/stake/client/rest/query.go index 036a3b451144..a040c7541c0a 100644 --- a/x/stake/client/rest/query.go +++ b/x/stake/client/rest/query.go @@ -68,6 +68,18 @@ func registerQueryRoutes(cliCtx context.CLIContext, r *mux.Router, cdc *wire.Cod validatorHandlerFn(cliCtx, cdc), ).Methods("GET") + // Get the current state of the staking pool + r.HandleFunc( + "/stake/pool", + poolHandlerFn(cliCtx, cdc), + ).Methods("GET") + + // Get the current staking parameter values + r.HandleFunc( + "/stake/parameters", + paramsHandlerFn(cliCtx, cdc), + ).Methods("GET") + } // already resolve the rational shares to not handle this in the client @@ -554,3 +566,63 @@ func validatorHandlerFn(cliCtx context.CLIContext, cdc *wire.Codec) http.Handler w.Write(output) } } + +// HTTP request handler to query the pool information +func poolHandlerFn(cliCtx context.CLIContext, cdc *wire.Codec) http.HandlerFunc { + return func(w http.ResponseWriter, r *http.Request) { + key := stake.PoolKey + + res, err := cliCtx.QueryStore(key, storeName) + if err != nil { + w.WriteHeader(http.StatusInternalServerError) + w.Write([]byte(fmt.Sprintf("couldn't query pool. Error: %s", err.Error()))) + return + } + + pool, err := types.UnmarshalPool(cdc, res) + if err != nil { + w.WriteHeader(http.StatusInternalServerError) + w.Write([]byte(err.Error())) + return + } + + output, err := cdc.MarshalJSON(pool) + if err != nil { + w.WriteHeader(http.StatusInternalServerError) + w.Write([]byte(err.Error())) + return + } + + w.Write(output) + } +} + +// HTTP request handler to query the staking params values +func paramsHandlerFn(cliCtx context.CLIContext, cdc *wire.Codec) http.HandlerFunc { + return func(w http.ResponseWriter, r *http.Request) { + key := stake.ParamKey + + res, err := cliCtx.QueryStore(key, storeName) + if err != nil { + w.WriteHeader(http.StatusInternalServerError) + w.Write([]byte(fmt.Sprintf("couldn't query parameters. Error: %s", err.Error()))) + return + } + + params, err := types.UnmarshalParams(cdc, res) + if err != nil { + w.WriteHeader(http.StatusInternalServerError) + w.Write([]byte(err.Error())) + return + } + + output, err := cdc.MarshalJSON(params) + if err != nil { + w.WriteHeader(http.StatusInternalServerError) + w.Write([]byte(err.Error())) + return + } + + w.Write(output) + } +} diff --git a/x/stake/types/params.go b/x/stake/types/params.go index f297f3105963..bc50c63906e9 100644 --- a/x/stake/types/params.go +++ b/x/stake/types/params.go @@ -5,6 +5,7 @@ import ( "time" sdk "github.com/cosmos/cosmos-sdk/types" + "github.com/cosmos/cosmos-sdk/wire" ) // defaultUnbondingTime reflects three weeks in seconds as the default @@ -43,3 +44,21 @@ func DefaultParams() Params { BondDenom: "steak", } } + +// unmarshal the current staking params value from store key or panic +func MustUnmarshalParams(cdc *wire.Codec, value []byte) Params { + params, err := UnmarshalParams(cdc, value) + if err != nil { + panic(err) + } + return params +} + +// unmarshal the current staking params value from store key +func UnmarshalParams(cdc *wire.Codec, value []byte) (params Params, err error) { + err = cdc.UnmarshalBinary(value, ¶ms) + if err != nil { + return + } + return +} diff --git a/x/stake/types/pool.go b/x/stake/types/pool.go index d59c1ed251ad..2715f57d9914 100644 --- a/x/stake/types/pool.go +++ b/x/stake/types/pool.go @@ -6,6 +6,7 @@ import ( "time" sdk "github.com/cosmos/cosmos-sdk/types" + "github.com/cosmos/cosmos-sdk/wire" ) // Pool - dynamic parameters of the current state @@ -123,3 +124,21 @@ func (p Pool) NextInflation(params Params) (inflation sdk.Dec) { return inflation } + +// unmarshal the current pool value from store key or panics +func MustUnmarshalPool(cdc *wire.Codec, value []byte) Pool { + pool, err := UnmarshalPool(cdc, value) + if err != nil { + panic(err) + } + return pool +} + +// unmarshal the current pool value from store key +func UnmarshalPool(cdc *wire.Codec, value []byte) (pool Pool, err error) { + err = cdc.UnmarshalBinary(value, &pool) + if err != nil { + return + } + return +} From 995494212a8345187dbe38e64aca59964045b74d Mon Sep 17 00:00:00 2001 From: Federico Kunze Date: Mon, 20 Aug 2018 16:30:51 +0200 Subject: [PATCH 02/11] Tests for params and pool query --- client/lcd/lcd_test.go | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/client/lcd/lcd_test.go b/client/lcd/lcd_test.go index 09c0eddd1116..eb45bc2e3105 100644 --- a/client/lcd/lcd_test.go +++ b/client/lcd/lcd_test.go @@ -353,6 +353,35 @@ func TestTxs(t *testing.T) { require.Equal(t, resultTx.Height, indexedTxs[0].Height) } +func TestPoolQuery(t *testing.T) { + _, password := "test", "1234567890" + addr, _ := CreateAddr(t, "test", password, GetKeyBase(t)) + cleanup, _, port := InitializeTestLCD(t, 1, []sdk.AccAddress{addr}) + defer cleanup() + + res, body := Request(t, port, "GET", "/stake/pool", nil) + require.Equal(t, http.StatusOK, res.StatusCode, body) + require.NotNil(t, body) +} + +func TestParamsQuery(t *testing.T) { + _, password := "test", "1234567890" + addr, _ := CreateAddr(t, "test", password, GetKeyBase(t)) + cleanup, _, port := InitializeTestLCD(t, 1, []sdk.AccAddress{addr}) + defer cleanup() + + defaultParams := stake.DefaultParams() + + res, body := Request(t, port, "GET", "/stake/parameters", nil) + require.Equal(t, http.StatusOK, res.StatusCode, body) + + var params stake.Params + err := cdc.UnmarshalJSON([]byte(body), ¶ms) + require.Nil(t, err) + + require.True(t, defaultParams.Equal(params)) +} + func TestValidatorsQuery(t *testing.T) { cleanup, pks, port := InitializeTestLCD(t, 1, []sdk.AccAddress{}) defer cleanup() From 0f8c4c3e87062cb6ff65b3f014ca9e7388cd4cbe Mon Sep 17 00:00:00 2001 From: Federico Kunze Date: Mon, 20 Aug 2018 16:53:30 +0200 Subject: [PATCH 03/11] Added CLI cmds for params and pool query --- cmd/gaia/cmd/gaiacli/main.go | 2 + x/stake/client/cli/query.go | 78 ++++++++++++++++++++++++++++++++++++ x/stake/types/params.go | 16 ++++++++ x/stake/types/pool.go | 18 ++++++++- 4 files changed, 113 insertions(+), 1 deletion(-) diff --git a/cmd/gaia/cmd/gaiacli/main.go b/cmd/gaia/cmd/gaiacli/main.go index 9c4d67b8c4a8..d30cdcc3a3e7 100644 --- a/cmd/gaia/cmd/gaiacli/main.go +++ b/cmd/gaia/cmd/gaiacli/main.go @@ -88,6 +88,8 @@ func main() { stakecmd.GetCmdQueryValidators("stake", cdc), stakecmd.GetCmdQueryDelegation("stake", cdc), stakecmd.GetCmdQueryDelegations("stake", cdc), + stakecmd.GetCmdQueryParams("stake", cdc), + stakecmd.GetCmdQueryPool("stake", cdc), slashingcmd.GetCmdQuerySigningInfo("slashing", cdc), )...) stakeCmd.AddCommand( diff --git a/x/stake/client/cli/query.go b/x/stake/client/cli/query.go index dc2c8e30d279..90c4a3c8dfcc 100644 --- a/x/stake/client/cli/query.go +++ b/x/stake/client/cli/query.go @@ -417,3 +417,81 @@ func GetCmdQueryRedelegations(storeName string, cdc *wire.Codec) *cobra.Command return cmd } + +// GetCmdQueryPool implements the pool query command. +func GetCmdQueryPool(storeName string, cdc *wire.Codec) *cobra.Command { + cmd := &cobra.Command{ + Use: "pool", + Short: "Query the current staking pool values", + Args: cobra.ExactArgs(0), + RunE: func(cmd *cobra.Command, args []string) error { + key := stake.PoolKey + cliCtx := context.NewCLIContext().WithCodec(cdc) + + res, err := cliCtx.QueryStore(key, storeName) + if err != nil { + return err + } + + pool := types.MustUnmarshalPool(cdc, res) + + switch viper.Get(cli.OutputFlag) { + case "text": + human := pool.HumanReadableString() + + fmt.Println(human) + + case "json": + // parse out the pool + output, err := wire.MarshalJSONIndent(cdc, pool) + if err != nil { + return err + } + + fmt.Println(string(output)) + } + return nil + }, + } + + return cmd +} + +// GetCmdQueryPool implements the params query command. +func GetCmdQueryParams(storeName string, cdc *wire.Codec) *cobra.Command { + cmd := &cobra.Command{ + Use: "parameters", + Short: "Query the current staking parameters information", + Args: cobra.ExactArgs(0), + RunE: func(cmd *cobra.Command, args []string) error { + key := stake.ParamKey + cliCtx := context.NewCLIContext().WithCodec(cdc) + + res, err := cliCtx.QueryStore(key, storeName) + if err != nil { + return err + } + + params := types.MustUnmarshalParams(cdc, res) + + switch viper.Get(cli.OutputFlag) { + case "text": + human := params.HumanReadableString() + + fmt.Println(human) + + case "json": + // parse out the params + output, err := wire.MarshalJSONIndent(cdc, params) + if err != nil { + return err + } + + fmt.Println(string(output)) + } + return nil + }, + } + + return cmd +} diff --git a/x/stake/types/params.go b/x/stake/types/params.go index bc50c63906e9..ca4849b40158 100644 --- a/x/stake/types/params.go +++ b/x/stake/types/params.go @@ -2,6 +2,7 @@ package types import ( "bytes" + "fmt" "time" sdk "github.com/cosmos/cosmos-sdk/types" @@ -45,6 +46,21 @@ func DefaultParams() Params { } } +// HumanReadableString returns a human readable string representation of the +// parameters. +func (p Params) HumanReadableString() string { + + resp := "Pool \n" + resp += fmt.Sprintf("Maximum Annual Inflation Rate Change: %s\n", p.InflationRateChange) + resp += fmt.Sprintf("Max Inflation Rate: %s\n", p.InflationMax) + resp += fmt.Sprintf("Min Inflation Tate: %s\n", p.InflationMin) + resp += fmt.Sprintf("Bonded Token Goal (%): %v\n", p.GoalBonded) + resp += fmt.Sprintf("Unbonding Time: %s\n", p.UnbondingTime) + resp += fmt.Sprintf("Max Validators: %v\n", p.MaxValidators) + resp += fmt.Sprintf("Bonded Coin Denomination: %d\n", p.BondDenom) + return resp +} + // unmarshal the current staking params value from store key or panic func MustUnmarshalParams(cdc *wire.Codec, value []byte) Params { params, err := UnmarshalParams(cdc, value) diff --git a/x/stake/types/pool.go b/x/stake/types/pool.go index 2715f57d9914..b01ed8e9a919 100644 --- a/x/stake/types/pool.go +++ b/x/stake/types/pool.go @@ -13,7 +13,7 @@ import ( type Pool struct { LooseTokens sdk.Dec `json:"loose_tokens"` // tokens which are not bonded in a validator BondedTokens sdk.Dec `json:"bonded_tokens"` // reserve of bonded tokens - InflationLastTime time.Time `json:"inflation_last_time"` // block which the last inflation was processed // TODO make time + InflationLastTime time.Time `json:"inflation_last_time"` // block which the last inflation was processed Inflation sdk.Dec `json:"inflation"` // current annual inflation rate DateLastCommissionReset int64 `json:"date_last_commission_reset"` // unix timestamp for last commission accounting reset (daily) @@ -125,6 +125,22 @@ func (p Pool) NextInflation(params Params) (inflation sdk.Dec) { return inflation } +// HumanReadableString returns a human readable string representation of a +// pool. +func (p Pool) HumanReadableString() string { + + resp := "Pool \n" + resp += fmt.Sprintf("Loose Tokens: %s\n", p.LooseTokens) + resp += fmt.Sprintf("Bonded Tokens: %s\n", p.BondedTokens) + resp += fmt.Sprintf("Token Supply: %s\n", p.TokenSupply()) + resp += fmt.Sprintf("Bonded Ratio: %v\n", p.BondedRatio()) + resp += fmt.Sprintf("Previous Inflation Block: %s\n", p.InflationLastTime) + resp += fmt.Sprintf("Inflation: %v\n", p.Inflation) + resp += fmt.Sprintf("Date of Last Commission Reset: %d\n", p.DateLastCommissionReset) + resp += fmt.Sprintf("Previous Bonded Shares: %v\n", p.PrevBondedShares) + return resp +} + // unmarshal the current pool value from store key or panics func MustUnmarshalPool(cdc *wire.Codec, value []byte) Pool { pool, err := UnmarshalPool(cdc, value) From 8f331c4c1210bab18b34db03c72400159f1d7a53 Mon Sep 17 00:00:00 2001 From: Federico Kunze Date: Mon, 20 Aug 2018 17:06:02 +0200 Subject: [PATCH 04/11] Added cli tests --- cmd/gaia/cli_test/cli_test.go | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/cmd/gaia/cli_test/cli_test.go b/cmd/gaia/cli_test/cli_test.go index 4f041d8485f7..0c26544e1139 100644 --- a/cmd/gaia/cli_test/cli_test.go +++ b/cmd/gaia/cli_test/cli_test.go @@ -150,6 +150,14 @@ func TestGaiaCLICreateValidator(t *testing.T) { */ validator = executeGetValidator(t, fmt.Sprintf("gaiacli stake validator %s --output=json %v", barAddr, flags)) require.Equal(t, "1.0000000000", validator.Tokens.String()) + + pool := executeGetPool(t, fmt.Sprintf("gaiacli stake pool --output=json %v", flags)) + require.NotNil(t, pool) + + defaultParams := stake.DefaultParams() + + params := executeGetPool(t, fmt.Sprintf("gaiacli stake parameters --output=json %v", flags)) + require.True(t, defaultParams.Equal(params)) } func TestGaiaCLISubmitProposal(t *testing.T) { @@ -328,6 +336,9 @@ func executeGetAccount(t *testing.T, cmdStr string) auth.BaseAccount { return acc } +//___________________________________________________________________________________ +// stake + func executeGetValidator(t *testing.T, cmdStr string) stake.Validator { out := tests.ExecuteT(t, cmdStr, "") var validator stake.Validator @@ -337,6 +348,27 @@ func executeGetValidator(t *testing.T, cmdStr string) stake.Validator { return validator } +func executeGetPool(t *testing.T, cmdStr string) stake.Pool { + out := tests.ExecuteT(t, cmdStr, "") + var pool stake.Pool + cdc := app.MakeCodec() + err := cdc.UnmarshalJSON([]byte(out), &pool) + require.NoError(t, err, "out %v\n, err %v", out, err) + return pool +} + +func executeGetParams(t *testing.T, cmdStr string) stake.Params { + out := tests.ExecuteT(t, cmdStr, "") + var params stake.Params + cdc := app.MakeCodec() + err := cdc.UnmarshalJSON([]byte(out), ¶ms) + require.NoError(t, err, "out %v\n, err %v", out, err) + return params +} + +//___________________________________________________________________________________ +// gov + func executeGetProposal(t *testing.T, cmdStr string) gov.Proposal { out := tests.ExecuteT(t, cmdStr, "") var proposal gov.Proposal From 8aa7a7cbbef668741da24e516f7424ec9f9f6188 Mon Sep 17 00:00:00 2001 From: Federico Kunze Date: Mon, 20 Aug 2018 17:17:21 +0200 Subject: [PATCH 05/11] Fixed string formatting --- x/stake/types/params.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/x/stake/types/params.go b/x/stake/types/params.go index ca4849b40158..bad4251cb319 100644 --- a/x/stake/types/params.go +++ b/x/stake/types/params.go @@ -54,10 +54,10 @@ func (p Params) HumanReadableString() string { resp += fmt.Sprintf("Maximum Annual Inflation Rate Change: %s\n", p.InflationRateChange) resp += fmt.Sprintf("Max Inflation Rate: %s\n", p.InflationMax) resp += fmt.Sprintf("Min Inflation Tate: %s\n", p.InflationMin) - resp += fmt.Sprintf("Bonded Token Goal (%): %v\n", p.GoalBonded) + resp += fmt.Sprintf("Bonded Token Goal (%s): %s\n", "s", p.GoalBonded) resp += fmt.Sprintf("Unbonding Time: %s\n", p.UnbondingTime) - resp += fmt.Sprintf("Max Validators: %v\n", p.MaxValidators) - resp += fmt.Sprintf("Bonded Coin Denomination: %d\n", p.BondDenom) + resp += fmt.Sprintf("Max Validators: %d: \n", p.MaxValidators) + resp += fmt.Sprintf("Bonded Coin Denomination: %s\n", p.BondDenom) return resp } From d7fa2e7e7818a7938fab300dbbc82faafcf31b6d Mon Sep 17 00:00:00 2001 From: Federico Kunze Date: Mon, 20 Aug 2018 17:20:18 +0200 Subject: [PATCH 06/11] fix cli_test bug --- cmd/gaia/cli_test/cli_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cmd/gaia/cli_test/cli_test.go b/cmd/gaia/cli_test/cli_test.go index 0c26544e1139..15be899f67a6 100644 --- a/cmd/gaia/cli_test/cli_test.go +++ b/cmd/gaia/cli_test/cli_test.go @@ -156,7 +156,7 @@ func TestGaiaCLICreateValidator(t *testing.T) { defaultParams := stake.DefaultParams() - params := executeGetPool(t, fmt.Sprintf("gaiacli stake parameters --output=json %v", flags)) + params := executeGetParams(t, fmt.Sprintf("gaiacli stake parameters --output=json %v", flags)) require.True(t, defaultParams.Equal(params)) } From 46167f9d02c40bdb730b6e38c78f176561da2900 Mon Sep 17 00:00:00 2001 From: Federico Kunze Date: Mon, 20 Aug 2018 17:44:43 +0200 Subject: [PATCH 07/11] Updated client docs --- docs/sdk/clients.md | 39 +++++++++++++++++++++++++++++++++++---- 1 file changed, 35 insertions(+), 4 deletions(-) diff --git a/docs/sdk/clients.md b/docs/sdk/clients.md index 17190d90a3e4..665d0bbab73c 100644 --- a/docs/sdk/clients.md +++ b/docs/sdk/clients.md @@ -17,16 +17,12 @@ There are three types of key representations that are used: - `cosmosaccaddr` - - Derived from account keys generated by `gaiacli keys add` - Used to receive funds - e.g. `cosmosaccaddr15h6vd5f0wqps26zjlwrc6chah08ryu4hzzdwhc` - - `cosmosaccpub` - - Derived from account keys generated by `gaiacli keys add` - e.g. `cosmosaccpub1zcjduc3q7fu03jnlu2xpl75s2nkt7krm6grh4cc5aqth73v0zwmea25wj2hsqhlqzm` - - `cosmosvalpub` - Generated when the node is created with `gaiad init`. - Get this value with `gaiad tendermint show-validator` @@ -156,6 +152,41 @@ gaiacli stake delegation \ --chain-id=gaia-7005 ``` +### Query Parameters + +You can get the current parameters that define high level settings for staking: + +``` +gaiacli stake parameters +``` + +With the above command you will get the values for: + +- Maximum and minumum Inflation rate +- Maximum annual change in inflation rate, +- Goal of bonded tokens (%) +- Unbonding time +- Maximum numbers of validators +- Coin denomination for staking + +All this values can be updated though a `governance` process by submitting a parameter change `proposal`. + +### Query Pool + +A staking `Pool` defines the dynamic parameters of the current state. You can query them with the following command: + +``` +gaiacli stake pool +``` + +With the `pool` command you will get the values for: + +- Loose and bonded tokens +- Token supply +- Current anual inflation and the block in which the last inflation was processed +- Last recorded bonded shares + + ## Light Client Daemon ::: tip Note From 162511052d09d8c2011836e8a837dc87d63e196b Mon Sep 17 00:00:00 2001 From: Federico Kunze Date: Mon, 20 Aug 2018 18:13:43 +0200 Subject: [PATCH 08/11] Update PENDING and examples --- PENDING.md | 2 ++ docs/sdk/clients.md | 4 ++-- examples/basecoin/cmd/basecli/main.go | 2 ++ 3 files changed, 6 insertions(+), 2 deletions(-) diff --git a/PENDING.md b/PENDING.md index dafd653fe4a3..24a50fda8e72 100644 --- a/PENDING.md +++ b/PENDING.md @@ -45,6 +45,8 @@ BREAKING CHANGES * [x/stake] \#1901 Validator type's Owner field renamed to Operator; Validator's GetOwner() renamed accordingly to comply with the SDK's Validator interface. FEATURES +* [lcd] Endpoints to query staking pool and params +* [cli] Cmds to query staking pool and params * [lcd] Can now query governance proposals by ProposalStatus * [x/mock/simulation] Randomized simulation framework * Modules specify invariants and operations, preferably in an x/[module]/simulation package diff --git a/docs/sdk/clients.md b/docs/sdk/clients.md index 665d0bbab73c..8614ce8a8fad 100644 --- a/docs/sdk/clients.md +++ b/docs/sdk/clients.md @@ -152,7 +152,7 @@ gaiacli stake delegation \ --chain-id=gaia-7005 ``` -### Query Parameters +#### Query Parameters You can get the current parameters that define high level settings for staking: @@ -171,7 +171,7 @@ With the above command you will get the values for: All this values can be updated though a `governance` process by submitting a parameter change `proposal`. -### Query Pool +#### Query Pool A staking `Pool` defines the dynamic parameters of the current state. You can query them with the following command: diff --git a/examples/basecoin/cmd/basecli/main.go b/examples/basecoin/cmd/basecli/main.go index 82a283515769..4274c970e041 100644 --- a/examples/basecoin/cmd/basecli/main.go +++ b/examples/basecoin/cmd/basecli/main.go @@ -63,6 +63,8 @@ func main() { stakecmd.GetCmdEditValidator(cdc), stakecmd.GetCmdDelegate(cdc), stakecmd.GetCmdUnbond("stake", cdc), + stakecmd.GetCmdQueryPool("stake", cdc), + stakecmd.GetCmdQueryParams("stake", cdc), )...) // add proxy, version and key info From d519ca26e040239cbd95dcaf6bf4e398b85cada2 Mon Sep 17 00:00:00 2001 From: Federico Kunze Date: Mon, 20 Aug 2018 19:42:38 +0200 Subject: [PATCH 09/11] Merged tests into a single one --- client/lcd/lcd_test.go | 22 ++++++++++++---------- 1 file changed, 12 insertions(+), 10 deletions(-) diff --git a/client/lcd/lcd_test.go b/client/lcd/lcd_test.go index eb45bc2e3105..9b4a6e4dc8b9 100644 --- a/client/lcd/lcd_test.go +++ b/client/lcd/lcd_test.go @@ -353,7 +353,7 @@ func TestTxs(t *testing.T) { require.Equal(t, resultTx.Height, indexedTxs[0].Height) } -func TestPoolQuery(t *testing.T) { +func TestPoolParamsQuery(t *testing.T) { _, password := "test", "1234567890" addr, _ := CreateAddr(t, "test", password, GetKeyBase(t)) cleanup, _, port := InitializeTestLCD(t, 1, []sdk.AccAddress{addr}) @@ -362,23 +362,25 @@ func TestPoolQuery(t *testing.T) { res, body := Request(t, port, "GET", "/stake/pool", nil) require.Equal(t, http.StatusOK, res.StatusCode, body) require.NotNil(t, body) -} -func TestParamsQuery(t *testing.T) { - _, password := "test", "1234567890" - addr, _ := CreateAddr(t, "test", password, GetKeyBase(t)) - cleanup, _, port := InitializeTestLCD(t, 1, []sdk.AccAddress{addr}) - defer cleanup() + initialPool := stake.InitialPool() + initialPool.LooseTokens = initialPool.LooseTokens.Add(sdk.NewDec(100)) + + var pool stake.Pool + err := cdc.UnmarshalJSON([]byte(body), &pool) + require.Nil(t, err) + require.Equal(t, initialPool.DateLastCommissionReset, pool.DateLastCommissionReset) + require.Equal(t, initialPool.PrevBondedShares, pool.PrevBondedShares) + // require.Equal(t, initialPool, pool) defaultParams := stake.DefaultParams() - res, body := Request(t, port, "GET", "/stake/parameters", nil) + res, body = Request(t, port, "GET", "/stake/parameters", nil) require.Equal(t, http.StatusOK, res.StatusCode, body) var params stake.Params - err := cdc.UnmarshalJSON([]byte(body), ¶ms) + err = cdc.UnmarshalJSON([]byte(body), ¶ms) require.Nil(t, err) - require.True(t, defaultParams.Equal(params)) } From 685c9db90cbc6b7d038537c90903387369d73533 Mon Sep 17 00:00:00 2001 From: Federico Kunze Date: Tue, 21 Aug 2018 16:04:22 +0200 Subject: [PATCH 10/11] Tested pool logic correctly --- client/lcd/lcd_test.go | 31 ++++++++++++++++++------------- 1 file changed, 18 insertions(+), 13 deletions(-) diff --git a/client/lcd/lcd_test.go b/client/lcd/lcd_test.go index 9b4a6e4dc8b9..fbbf6a6cfff7 100644 --- a/client/lcd/lcd_test.go +++ b/client/lcd/lcd_test.go @@ -359,29 +359,34 @@ func TestPoolParamsQuery(t *testing.T) { cleanup, _, port := InitializeTestLCD(t, 1, []sdk.AccAddress{addr}) defer cleanup() - res, body := Request(t, port, "GET", "/stake/pool", nil) + defaultParams := stake.DefaultParams() + + res, body := Request(t, port, "GET", "/stake/parameters", nil) + require.Equal(t, http.StatusOK, res.StatusCode, body) + + var params stake.Params + err := cdc.UnmarshalJSON([]byte(body), ¶ms) + require.Nil(t, err) + require.True(t, defaultParams.Equal(params)) + + res, body = Request(t, port, "GET", "/stake/pool", nil) require.Equal(t, http.StatusOK, res.StatusCode, body) require.NotNil(t, body) initialPool := stake.InitialPool() initialPool.LooseTokens = initialPool.LooseTokens.Add(sdk.NewDec(100)) + initialPool.BondedTokens = initialPool.BondedTokens.Add(sdk.NewDec(100)) // Delegate tx on GaiaAppGenState + initialPool.LooseTokens = initialPool.LooseTokens.Add(sdk.NewDec(int64(50))) // freeFermionsAcc = 50 on GaiaAppGenState var pool stake.Pool - err := cdc.UnmarshalJSON([]byte(body), &pool) + err = cdc.UnmarshalJSON([]byte(body), &pool) require.Nil(t, err) require.Equal(t, initialPool.DateLastCommissionReset, pool.DateLastCommissionReset) require.Equal(t, initialPool.PrevBondedShares, pool.PrevBondedShares) - // require.Equal(t, initialPool, pool) - - defaultParams := stake.DefaultParams() - - res, body = Request(t, port, "GET", "/stake/parameters", nil) - require.Equal(t, http.StatusOK, res.StatusCode, body) - - var params stake.Params - err = cdc.UnmarshalJSON([]byte(body), ¶ms) - require.Nil(t, err) - require.True(t, defaultParams.Equal(params)) + require.Equal(t, initialPool.BondedTokens, pool.BondedTokens) + require.Equal(t, initialPool.NextInflation(params), pool.Inflation) + initialPool = initialPool.ProcessProvisions(params) // provisions are added to the pool every hour + require.Equal(t, initialPool.LooseTokens, pool.LooseTokens) } func TestValidatorsQuery(t *testing.T) { From 5ba1134571bf593a126193231798c792109d20a3 Mon Sep 17 00:00:00 2001 From: Federico Kunze Date: Tue, 21 Aug 2018 16:55:12 +0200 Subject: [PATCH 11/11] Updated cli_test --- cmd/gaia/cli_test/cli_test.go | 17 ++++++++++++----- examples/basecoin/cmd/basecli/main.go | 12 ++++++++++-- 2 files changed, 22 insertions(+), 7 deletions(-) diff --git a/cmd/gaia/cli_test/cli_test.go b/cmd/gaia/cli_test/cli_test.go index 15be899f67a6..47a1949dd26d 100644 --- a/cmd/gaia/cli_test/cli_test.go +++ b/cmd/gaia/cli_test/cli_test.go @@ -117,6 +117,11 @@ func TestGaiaCLICreateValidator(t *testing.T) { fooAcc := executeGetAccount(t, fmt.Sprintf("gaiacli account %s %v", fooAddr, flags)) require.Equal(t, int64(40), fooAcc.GetCoins().AmountOf("steak").Int64()) + defaultParams := stake.DefaultParams() + initialPool := stake.InitialPool() + initialPool.BondedTokens = initialPool.BondedTokens.Add(sdk.NewDec(100)) // Delegate tx on GaiaAppGenState + initialPool = initialPool.ProcessProvisions(defaultParams) // provisions are added to the pool every hour + // create validator cvStr := fmt.Sprintf("gaiacli stake create-validator %v", flags) cvStr += fmt.Sprintf(" --from=%s", "bar") @@ -124,6 +129,8 @@ func TestGaiaCLICreateValidator(t *testing.T) { cvStr += fmt.Sprintf(" --amount=%v", "2steak") cvStr += fmt.Sprintf(" --moniker=%v", "bar-vally") + initialPool.BondedTokens = initialPool.BondedTokens.Add(sdk.NewDec(1)) + executeWrite(t, cvStr, app.DefaultKeyPass) tests.WaitForNextNBlocksTM(2, port) @@ -151,13 +158,13 @@ func TestGaiaCLICreateValidator(t *testing.T) { validator = executeGetValidator(t, fmt.Sprintf("gaiacli stake validator %s --output=json %v", barAddr, flags)) require.Equal(t, "1.0000000000", validator.Tokens.String()) - pool := executeGetPool(t, fmt.Sprintf("gaiacli stake pool --output=json %v", flags)) - require.NotNil(t, pool) - - defaultParams := stake.DefaultParams() - params := executeGetParams(t, fmt.Sprintf("gaiacli stake parameters --output=json %v", flags)) require.True(t, defaultParams.Equal(params)) + + pool := executeGetPool(t, fmt.Sprintf("gaiacli stake pool --output=json %v", flags)) + require.Equal(t, initialPool.DateLastCommissionReset, pool.DateLastCommissionReset) + require.Equal(t, initialPool.PrevBondedShares, pool.PrevBondedShares) + require.Equal(t, initialPool.BondedTokens, pool.BondedTokens) } func TestGaiaCLISubmitProposal(t *testing.T) { diff --git a/examples/basecoin/cmd/basecli/main.go b/examples/basecoin/cmd/basecli/main.go index 4274c970e041..15bfd8035b32 100644 --- a/examples/basecoin/cmd/basecli/main.go +++ b/examples/basecoin/cmd/basecli/main.go @@ -14,6 +14,7 @@ import ( authcmd "github.com/cosmos/cosmos-sdk/x/auth/client/cli" bankcmd "github.com/cosmos/cosmos-sdk/x/bank/client/cli" ibccmd "github.com/cosmos/cosmos-sdk/x/ibc/client/cli" + slashingcmd "github.com/cosmos/cosmos-sdk/x/slashing/client/cli" stakecmd "github.com/cosmos/cosmos-sdk/x/stake/client/cli" "github.com/spf13/cobra" "github.com/tendermint/tendermint/libs/cli" @@ -51,6 +52,13 @@ func main() { stakecmd.GetCmdQueryValidators("stake", cdc), stakecmd.GetCmdQueryDelegation("stake", cdc), stakecmd.GetCmdQueryDelegations("stake", cdc), + stakecmd.GetCmdQueryPool("stake", cdc), + stakecmd.GetCmdQueryParams("stake", cdc), + stakecmd.GetCmdQueryUnbondingDelegation("stake", cdc), + stakecmd.GetCmdQueryUnbondingDelegations("stake", cdc), + stakecmd.GetCmdQueryRedelegation("stake", cdc), + stakecmd.GetCmdQueryRedelegations("stake", cdc), + slashingcmd.GetCmdQuerySigningInfo("slashing", cdc), authcmd.GetAccountCmd("acc", cdc, types.GetAccountDecoder(cdc)), )...) @@ -63,8 +71,8 @@ func main() { stakecmd.GetCmdEditValidator(cdc), stakecmd.GetCmdDelegate(cdc), stakecmd.GetCmdUnbond("stake", cdc), - stakecmd.GetCmdQueryPool("stake", cdc), - stakecmd.GetCmdQueryParams("stake", cdc), + stakecmd.GetCmdRedelegate("stake", cdc), + slashingcmd.GetCmdUnrevoke(cdc), )...) // add proxy, version and key info