From 91f059d428c662dd22e365e2b485cc914c4a388b Mon Sep 17 00:00:00 2001 From: Federico Kunze Date: Mon, 13 Aug 2018 13:41:49 +0200 Subject: [PATCH 1/7] Added tests for Delegator Validators routes --- client/lcd/lcd_test.go | 45 ++++++++++++++++++++++++++++++++++++ x/stake/client/rest/query.go | 13 +++++++++++ 2 files changed, 58 insertions(+) diff --git a/client/lcd/lcd_test.go b/client/lcd/lcd_test.go index b8909e226a02..d43286d52b87 100644 --- a/client/lcd/lcd_test.go +++ b/client/lcd/lcd_test.go @@ -389,6 +389,7 @@ func TestBonding(t *testing.T) { defer cleanup() validator1Owner := sdk.AccAddress(pks[0].Address()) + validator := getValidator(t, port, validator1Owner) // create bond TX resultTx := doDelegate(t, port, seed, name, password, addr, validator1Owner) @@ -408,6 +409,14 @@ func TestBonding(t *testing.T) { bond := getDelegation(t, port, addr, validator1Owner) require.Equal(t, "60.0000000000", bond.Shares) + bondedValidators := getDelegatorValidators(t, port, addr) + require.Len(t, bondedValidators, 1) + require.Equal(t, validator1Owner, bondedValidators[0].Owner) + require.Equal(t, validator.DelegatorShares.Add(sdk.NewRat(60)).FloatString(), bondedValidators[0].DelegatorShares.FloatString()) + + bondedValidator := getDelegatorValidator(t, port, addr, validator1Owner) + require.Equal(t, validator1Owner, bondedValidator.Owner) + ////////////////////// // testing unbonding @@ -443,6 +452,14 @@ func TestBonding(t *testing.T) { assert.Len(t, summary.UnbondingDelegations, 1, "Delegation summary holds all unbonding-delegations") assert.Equal(t, "30", summary.UnbondingDelegations[0].Balance.Amount.String()) + // TODO Unbonding BondStatus is not currently implemented + // bondedValidators = getDelegatorValidators(t, port, addr) + // require.Len(t, bondedValidators, 1) + // require.Equal(t, sdk.Unbonding, bondedValidators[0].Status) + // + // bondedValidator = getDelegatorValidator(t, port, addr, validator1Owner) + // require.Equal(t, sdk.Unbonding, bondedValidator.Status) + // TODO add redelegation, need more complex capabilities such to mock context and // TODO check summary for redelegation // assert.Len(t, summary.Redelegations, 1, "Delegation summary holds all redelegations") @@ -805,6 +822,34 @@ func getBondingTxs(t *testing.T, port string, delegatorAddr sdk.AccAddress, quer return txs } +func getDelegatorValidators(t *testing.T, port string, delegatorAddr sdk.AccAddress) []stake.BechValidator { + + var res *http.Response + var body string + res, body = Request(t, port, "GET", fmt.Sprintf("/stake/delegators/%s/validators", delegatorAddr), nil) + + require.Equal(t, http.StatusOK, res.StatusCode, body) + var bondedValidators []stake.BechValidator + err := cdc.UnmarshalJSON([]byte(body), &bondedValidators) + require.Nil(t, err) + fmt.Println(fmt.Sprintf("–––> DelegatorVals: %v", bondedValidators)) + return bondedValidators +} + +func getDelegatorValidator(t *testing.T, port string, delegatorAddr sdk.AccAddress, validatorAddr sdk.AccAddress) stake.BechValidator { + + var res *http.Response + var body string + res, body = Request(t, port, "GET", fmt.Sprintf("/stake/delegators/%s/validators/%s", delegatorAddr, validatorAddr), nil) + + require.Equal(t, http.StatusOK, res.StatusCode, body) + var bondedValidator stake.BechValidator + err := cdc.UnmarshalJSON([]byte(body), &bondedValidator) + require.Nil(t, err) + fmt.Println(fmt.Sprintf("–––> DelegatorVal: %v", bondedValidator)) + return bondedValidator +} + func doDelegate(t *testing.T, port, seed, name, password string, delegatorAddr, validatorAddr sdk.AccAddress) (resultTx ctypes.ResultBroadcastTxCommit) { // get the account to get the sequence acc := getAccount(t, port, delegatorAddr) diff --git a/x/stake/client/rest/query.go b/x/stake/client/rest/query.go index ac660f98f66b..8d15d08d25dc 100644 --- a/x/stake/client/rest/query.go +++ b/x/stake/client/rest/query.go @@ -32,6 +32,18 @@ func registerQueryRoutes(cliCtx context.CLIContext, r *mux.Router, cdc *wire.Cod delegatorTxsHandlerFn(cliCtx, cdc), ).Methods("GET") + // GET /stake/delegators/{delegatorAddr}/validators // Query all validators that a delegator is bonded to + r.HandleFunc( + "/stake/delegators/{delegatorAddr}/validators", + delegatorValidatorsHandlerFn(cliCtx, cdc), + ).Methods("GET") + + // GET /stake/delegators/{delegatorAddr}/validators/{validatorAddr} // Query a validator that a delegator is bonded to + r.HandleFunc( + "/stake/delegators/{delegatorAddr}/validators/{validatorAddr}", + delegatorValidatorHandlerFn(cliCtx, cdc), + ).Methods("GET") + // GET /stake/delegators/{delegatorAddr}/delegations/{validatorAddr} // Query a delegation between a delegator and a validator r.HandleFunc( "/stake/delegators/{delegatorAddr}/delegations/{validatorAddr}", @@ -55,6 +67,7 @@ func registerQueryRoutes(cliCtx context.CLIContext, r *mux.Router, cdc *wire.Cod "/stake/validators/{addr}", validatorHandlerFn(cliCtx, cdc), ).Methods("GET") + } // already resolve the rational shares to not handle this in the client From 0f93dd59612face28fad6c024241cdfb5213aab3 Mon Sep 17 00:00:00 2001 From: Federico Kunze Date: Mon, 13 Aug 2018 14:37:47 +0200 Subject: [PATCH 2/7] Updated tests for undelegations --- client/lcd/lcd_test.go | 57 ++++++++++++++++++++++++++---------------- 1 file changed, 35 insertions(+), 22 deletions(-) diff --git a/client/lcd/lcd_test.go b/client/lcd/lcd_test.go index d43286d52b87..72454e1dc847 100644 --- a/client/lcd/lcd_test.go +++ b/client/lcd/lcd_test.go @@ -384,7 +384,7 @@ func TestValidatorQuery(t *testing.T) { func TestBonding(t *testing.T) { name, password, denom := "test", "1234567890", "steak" - addr, seed := CreateAddr(t, "test", password, GetKeyBase(t)) + addr, seed := CreateAddr(t, name, password, GetKeyBase(t)) cleanup, pks, port := InitializeTestLCD(t, 1, []sdk.AccAddress{addr}) defer cleanup() @@ -392,7 +392,7 @@ func TestBonding(t *testing.T) { validator := getValidator(t, port, validator1Owner) // create bond TX - resultTx := doDelegate(t, port, seed, name, password, addr, validator1Owner) + resultTx := doDelegate(t, port, seed, name, password, addr, validator1Owner, 60) tests.WaitForHeight(resultTx.Height+1, port) // check if tx was committed @@ -409,6 +409,12 @@ func TestBonding(t *testing.T) { bond := getDelegation(t, port, addr, validator1Owner) require.Equal(t, "60.0000000000", bond.Shares) + // query summary + summary := getDelegationSummary(t, port, addr) + assert.Len(t, summary.Delegations, 1, "Delegation summary holds all delegations") + assert.Equal(t, "60.0000000000", summary.Delegations[0].Shares) + assert.Len(t, summary.UnbondingDelegations, 0, "Delegation summary holds all unbonding-delegations") + bondedValidators := getDelegatorValidators(t, port, addr) require.Len(t, bondedValidators, 1) require.Equal(t, validator1Owner, bondedValidators[0].Owner) @@ -421,17 +427,17 @@ func TestBonding(t *testing.T) { // testing unbonding // create unbond TX - resultTx = doBeginUnbonding(t, port, seed, name, password, addr, validator1Owner) + resultTx = doBeginUnbonding(t, port, seed, name, password, addr, validator1Owner, 60) tests.WaitForHeight(resultTx.Height+1, port) - // query validator - bond = getDelegation(t, port, addr, validator1Owner) - require.Equal(t, "30.0000000000", bond.Shares) - // check if tx was committed require.Equal(t, uint32(0), resultTx.CheckTx.Code) require.Equal(t, uint32(0), resultTx.DeliverTx.Code) + // // query validator + // bond = getDelegation(t, port, addr, validator1Owner) + // require.Equal(t, "0.0000000000", bond.Shares) + // should the sender should have not received any coins as the unbonding has only just begun // query sender acc = getAccount(t, port, addr) @@ -439,22 +445,29 @@ func TestBonding(t *testing.T) { require.Equal(t, int64(40), coins.AmountOf("steak").Int64()) // query unbonding delegation - validatorAddr := sdk.AccAddress(pks[0].Address()) - unbondings := getUndelegations(t, port, addr, validatorAddr) + unbondings := getUndelegations(t, port, addr, validator1Owner) assert.Len(t, unbondings, 1, "Unbondings holds all unbonding-delegations") - assert.Equal(t, "30", unbondings[0].Balance.Amount.String()) + assert.Equal(t, "60", unbondings[0].Balance.Amount.String()) // query summary - summary := getDelegationSummary(t, port, addr) + summary = getDelegationSummary(t, port, addr) - assert.Len(t, summary.Delegations, 1, "Delegation summary holds all delegations") - assert.Equal(t, "30.0000000000", summary.Delegations[0].Shares) + assert.Len(t, summary.Delegations, 0, "Delegation summary holds all delegations") + // assert.Equal(t, "0.0000000000", summary.Delegations[0].Shares) assert.Len(t, summary.UnbondingDelegations, 1, "Delegation summary holds all unbonding-delegations") - assert.Equal(t, "30", summary.UnbondingDelegations[0].Balance.Amount.String()) + assert.Equal(t, "60", summary.UnbondingDelegations[0].Balance.Amount.String()) + + // validator still has bonded shares from the delegator + bondedValidators = getDelegatorValidators(t, port, addr) + require.Len(t, bondedValidators, 0) - // TODO Unbonding BondStatus is not currently implemented + // resultTx = doBeginUnbonding(t, port, seed, name, password, addr, validator1Owner) + // tests.WaitForHeight(resultTx.Height+1, port) + // // bondedValidators = getDelegatorValidators(t, port, addr) - // require.Len(t, bondedValidators, 1) + // require.Len(t, bondedValidators, 0) + + // TODO Undonding status not currently implemented // require.Equal(t, sdk.Unbonding, bondedValidators[0].Status) // // bondedValidator = getDelegatorValidator(t, port, addr, validator1Owner) @@ -850,7 +863,7 @@ func getDelegatorValidator(t *testing.T, port string, delegatorAddr sdk.AccAddre return bondedValidator } -func doDelegate(t *testing.T, port, seed, name, password string, delegatorAddr, validatorAddr sdk.AccAddress) (resultTx ctypes.ResultBroadcastTxCommit) { +func doDelegate(t *testing.T, port, seed, name, password string, delegatorAddr, validatorAddr sdk.AccAddress, amount int64) (resultTx ctypes.ResultBroadcastTxCommit) { // get the account to get the sequence acc := getAccount(t, port, delegatorAddr) accnum := acc.GetAccountNumber() @@ -870,14 +883,14 @@ func doDelegate(t *testing.T, port, seed, name, password string, delegatorAddr, { "delegator_addr": "%s", "validator_addr": "%s", - "delegation": { "denom": "%s", "amount": "60" } + "delegation": { "denom": "%s", "amount": "%d" } } ], "begin_unbondings": [], "complete_unbondings": [], "begin_redelegates": [], "complete_redelegates": [] - }`, name, password, accnum, sequence, chainID, delegatorAddr, validatorAddr, "steak")) + }`, name, password, accnum, sequence, chainID, delegatorAddr, validatorAddr, "steak", amount)) res, body := Request(t, port, "POST", fmt.Sprintf("/stake/delegators/%s/delegations", delegatorAddr), jsonStr) require.Equal(t, http.StatusOK, res.StatusCode, body) @@ -889,7 +902,7 @@ func doDelegate(t *testing.T, port, seed, name, password string, delegatorAddr, } func doBeginUnbonding(t *testing.T, port, seed, name, password string, - delegatorAddr, validatorAddr sdk.AccAddress) (resultTx ctypes.ResultBroadcastTxCommit) { + delegatorAddr, validatorAddr sdk.AccAddress, amount int64) (resultTx ctypes.ResultBroadcastTxCommit) { // get the account to get the sequence acc := getAccount(t, port, delegatorAddr) @@ -911,13 +924,13 @@ func doBeginUnbonding(t *testing.T, port, seed, name, password string, { "delegator_addr": "%s", "validator_addr": "%s", - "shares": "30" + "shares": "%d" } ], "complete_unbondings": [], "begin_redelegates": [], "complete_redelegates": [] - }`, name, password, accnum, sequence, chainID, delegatorAddr, validatorAddr)) + }`, name, password, accnum, sequence, chainID, delegatorAddr, validatorAddr, amount)) res, body := Request(t, port, "POST", fmt.Sprintf("/stake/delegators/%s/delegations", delegatorAddr), jsonStr) require.Equal(t, http.StatusOK, res.StatusCode, body) From 6eb7f41bdff8479cec2012c16807b2cf736af57c Mon Sep 17 00:00:00 2001 From: Federico Kunze Date: Mon, 13 Aug 2018 14:51:49 +0200 Subject: [PATCH 3/7] Updated Gaia-lite docs --- docs/clients/lcd-rest-api.yaml | 43 ++++++++++++++++++++++++++ docs/light/api.md | 56 ++++++++++++++++++++++++++++++++++ docs/light/specification.md | 8 +++++ 3 files changed, 107 insertions(+) diff --git a/docs/clients/lcd-rest-api.yaml b/docs/clients/lcd-rest-api.yaml index 063e3a55b857..3b8349f4bd59 100644 --- a/docs/clients/lcd-rest-api.yaml +++ b/docs/clients/lcd-rest-api.yaml @@ -493,6 +493,49 @@ paths: 500: description: Internal Server Error + /stake/delegators/{delegatorAddr}/validators: + parameters: + - in: path + name: delegatorAddr + description: Bech32 AccAddress of Delegator + required: true + type: string + get: + summary: Query all validators that a delegator is bonded to + tags: + - stake + produces: + - application/json + responses: + 200: + description: OK + 404: + description: Not Found + + /stake/delegators/{delegatorAddr}/validators/{validatorAddr}: + parameters: + - in: path + name: delegatorAddr + description: Bech32 AccAddress of Delegator + required: true + type: string + - in: path + name: validatorAddr + description: Bech32 ValAddress of Delegator + required: true + type: string + get: + summary: Query a validator that a delegator is bonded to + tags: + - stake + produces: + - application/json + responses: + 200: + description: OK + 404: + description: Not Found + /stake/delegators/{delegatorAddr}/txs: parameters: - in: path diff --git a/docs/light/api.md b/docs/light/api.md index 41abed5da6ef..d4e66eaf152e 100644 --- a/docs/light/api.md +++ b/docs/light/api.md @@ -479,6 +479,62 @@ Returns on error: } ``` +### /stake/delegators/{delegatorAddr}/validators - GET + +url: /stake/delegators/{delegatorAddr}/validators + +Functionality: Query all validators that a delegator is bonded to. + +Returns on success: + +```json +{ + "rest api":"2.0", + "code":200, + "error":"", + "result":{} +} +``` + +Returns on failure: + +```json +{ + "rest api":"2.0", + "code":500, + "error":"TODO", + "result":{} +} +``` + +### /stake/delegators/{delegatorAddr}/validators/{validatorAddr} - GET + +url: /stake/delegators/{delegatorAddr}/validators/{validatorAddr} + +Functionality: Query a validator that a delegator is bonded to + +Returns on success: + +```json +{ + "rest api":"2.0", + "code":200, + "error":"", + "result":{} +} +``` + +Returns on failure: + +```json +{ + "rest api":"2.0", + "code":500, + "error":"TODO", + "result":{} +} +``` + ### /stake/delegators/{delegatorAddr}/txs - GET url: /stake/delegators/{delegatorAddr}/txs diff --git a/docs/light/specification.md b/docs/light/specification.md index d8897b2444a0..15f36b014460 100644 --- a/docs/light/specification.md +++ b/docs/light/specification.md @@ -323,6 +323,14 @@ return KeyOutput{ TODO +### [/stake/delegators/{delegatorAddr}/validators](api.md#stakedelegatorsdelegatorAddrvalidators---get) + +TODO + +### [/stake/delegators/{delegatorAddr}/validators/{validatorAddr}](api.md#stakedelegatorsdelegatorAddrvalidatorsvalidatorAddr---get) + +TODO + ### [/stake/delegators/{delegatorAddr}/txs](api.md#stakedelegatorsdelegatorAddrtxs---get) TODO From cf6d3d2ca81804a7a40aef4cb85029858ab10ac1 Mon Sep 17 00:00:00 2001 From: Federico Kunze Date: Mon, 13 Aug 2018 14:57:51 +0200 Subject: [PATCH 4/7] Updated PENDING.md --- PENDING.md | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/PENDING.md b/PENDING.md index 4ca3e6df5465..be173d531140 100644 --- a/PENDING.md +++ b/PENDING.md @@ -1,15 +1,15 @@ -## v0.24.0 PENDING +## v0.24.0 PENDING ^--- PENDING wasn't purged on sdk v0.23.0 release. BREAKING CHANGES -* Update to tendermint v0.23.0. This involves removing crypto.Pubkey, +* Update to tendermint v0.23.0. This involves removing crypto.Pubkey, maintaining a validator address to pubkey map, and using time.Time instead of int64 for time. [SDK PR](https://github.com/cosmos/cosmos-sdk/pull/1927) ## PENDING BREAKING CHANGES * API - - \#1880 [x/stake] changed the endpoints to be more REST-ful + - \#1880 and \#2000 [x/stake] changed the endpoints to be more REST-ful * Update to tendermint v0.22.5. This involves changing all of the cryptography imports. [Ref](https://github.com/tendermint/tendermint/pull/1966) * [baseapp] Msgs are no longer run on CheckTx, removed `ctx.IsCheckTx()` * [x/gov] CLI flag changed from `proposalID` to `proposal-id` @@ -32,7 +32,7 @@ BREAKING CHANGES * `gaiacli gov submit-proposal --proposer` * `gaiacli gov deposit --depositer` * `gaiacli gov vote --voter` -* [x/gov] Added tags sub-package, changed tags to use dash-case +* [x/gov] Added tags sub-package, changed tags to use dash-case * [x/gov] Governance parameters are now stored in globalparams store * [lcd] \#1866 Updated lcd /slashing/signing_info endpoint to take cosmosvalpub instead of cosmosvaladdr * [types] sdk.NewCoin now takes sdk.Int, sdk.NewInt64Coin takes int64 @@ -46,7 +46,7 @@ FEATURES * Modules can test random combinations of their own operations * Applications can integrate operations and invariants from modules together for an integrated simulation * [baseapp] Initialize validator set on ResponseInitChain -* [cosmos-sdk-cli] Added support for cosmos-sdk-cli tool under cosmos-sdk/cmd +* [cosmos-sdk-cli] Added support for cosmos-sdk-cli tool under cosmos-sdk/cmd * This allows SDK users to initialize a new project repository. * [tests] Remotenet commands for AWS (awsnet) * [networks] Added ansible scripts to upgrade seed nodes on a network @@ -62,7 +62,7 @@ IMPROVEMENTS * [cli] Improve error messages for all txs when the account doesn't exist * [tools] Remove `rm -rf vendor/` from `make get_vendor_deps` * [x/auth] Recover ErrorOutOfGas panic in order to set sdk.Result attributes correctly -* [x/stake] Add revoked to human-readable validator +* [x/stake] Add revoked to human-readable validator * [spec] \#967 Inflation and distribution specs drastically improved * [tests] Add tests to example apps in docs * [x/gov] Votes on a proposal can now be queried @@ -70,8 +70,9 @@ IMPROVEMENTS * [tests] Fixes ansible scripts to work with AWS too * [tests] \#1806 CLI tests are now behind the build flag 'cli_test', so go test works on a new repo * [x/gov] Initial governance parameters can now be set in the genesis file -* [x/stake] \#1815 Sped up the processing of `EditValidator` txs. +* [x/stake] \#1815 Sped up the processing of `EditValidator` txs. * [server] \#1930 Transactions indexer indexes all tags by default. +* [x/stake] \#2000 Added tests for new staking endpoints BUG FIXES * \#1666 Add intra-tx counter to the genesis validators @@ -83,7 +84,7 @@ BUG FIXES * \#1828 Force user to specify amount on create-validator command by removing default * \#1839 Fixed bug where intra-tx counter wasn't set correctly for genesis validators * [staking] [#1858](https://github.com/cosmos/cosmos-sdk/pull/1858) Fixed bug where the cliff validator was not be updated correctly -* [tests] \#1675 Fix non-deterministic `test_cover` +* [tests] \#1675 Fix non-deterministic `test_cover` * [client] \#1551: Refactored `CoreContext` * Renamed `CoreContext` to `QueryContext` * Removed all tx related fields and logic (building & signing) to separate From 927f150b86def72dbc777329f48025e957742bf8 Mon Sep 17 00:00:00 2001 From: Federico Kunze Date: Mon, 13 Aug 2018 16:58:22 +0200 Subject: [PATCH 5/7] Updated comments --- client/lcd/lcd_test.go | 46 ++++++++++-------------------------- x/stake/client/rest/query.go | 16 ++++++------- 2 files changed, 21 insertions(+), 41 deletions(-) diff --git a/client/lcd/lcd_test.go b/client/lcd/lcd_test.go index 3abac902c213..90b683926fe3 100644 --- a/client/lcd/lcd_test.go +++ b/client/lcd/lcd_test.go @@ -412,9 +412,10 @@ func TestBonding(t *testing.T) { // query summary summary := getDelegationSummary(t, port, addr) - assert.Len(t, summary.Delegations, 1, "Delegation summary holds all delegations") - assert.Equal(t, "60.0000000000", summary.Delegations[0].Shares) - assert.Len(t, summary.UnbondingDelegations, 0, "Delegation summary holds all unbonding-delegations") + + require.Len(t, summary.Delegations, 1, "Delegation summary holds all delegations") + require.Equal(t, "60.0000000000", summary.Delegations[0].Shares) + require.Len(t, summary.UnbondingDelegations, 0, "Delegation summary holds all unbonding-delegations") bondedValidators := getDelegatorValidators(t, port, addr) require.Len(t, bondedValidators, 1) @@ -435,44 +436,29 @@ func TestBonding(t *testing.T) { require.Equal(t, uint32(0), resultTx.CheckTx.Code) require.Equal(t, uint32(0), resultTx.DeliverTx.Code) - // // query validator - // bond = getDelegation(t, port, addr, validator1Owner) - // require.Equal(t, "0.0000000000", bond.Shares) - // should the sender should have not received any coins as the unbonding has only just begun - // query sender acc = getAccount(t, port, addr) coins = acc.GetCoins() require.Equal(t, int64(40), coins.AmountOf("steak").Int64()) // query unbonding delegation unbondings := getUndelegations(t, port, addr, validator1Owner) - assert.Len(t, unbondings, 1, "Unbondings holds all unbonding-delegations") - assert.Equal(t, "60", unbondings[0].Balance.Amount.String()) + require.Len(t, unbondings, 1, "Unbondings holds all unbonding-delegations") + require.Equal(t, "60", unbondings[0].Balance.Amount.String()) // query summary summary = getDelegationSummary(t, port, addr) - assert.Len(t, summary.Delegations, 0, "Delegation summary holds all delegations") - // assert.Equal(t, "0.0000000000", summary.Delegations[0].Shares) - assert.Len(t, summary.UnbondingDelegations, 1, "Delegation summary holds all unbonding-delegations") - assert.Equal(t, "60", summary.UnbondingDelegations[0].Balance.Amount.String()) + require.Len(t, summary.Delegations, 0, "Delegation summary holds all delegations") + require.Len(t, summary.UnbondingDelegations, 1, "Delegation summary holds all unbonding-delegations") + require.Equal(t, "60", summary.UnbondingDelegations[0].Balance.Amount.String()) // validator still has bonded shares from the delegator bondedValidators = getDelegatorValidators(t, port, addr) require.Len(t, bondedValidators, 0) - // resultTx = doBeginUnbonding(t, port, seed, name, password, addr, validator1Owner) - // tests.WaitForHeight(resultTx.Height+1, port) - // - // bondedValidators = getDelegatorValidators(t, port, addr) - // require.Len(t, bondedValidators, 0) - // TODO Undonding status not currently implemented // require.Equal(t, sdk.Unbonding, bondedValidators[0].Status) - // - // bondedValidator = getDelegatorValidator(t, port, addr, validator1Owner) - // require.Equal(t, sdk.Unbonding, bondedValidator.Status) // TODO add redelegation, need more complex capabilities such to mock context and // TODO check summary for redelegation @@ -846,7 +832,6 @@ func getDelegatorValidators(t *testing.T, port string, delegatorAddr sdk.AccAddr var bondedValidators []stake.BechValidator err := cdc.UnmarshalJSON([]byte(body), &bondedValidators) require.Nil(t, err) - fmt.Println(fmt.Sprintf("–––> DelegatorVals: %v", bondedValidators)) return bondedValidators } @@ -860,19 +845,17 @@ func getDelegatorValidator(t *testing.T, port string, delegatorAddr sdk.AccAddre var bondedValidator stake.BechValidator err := cdc.UnmarshalJSON([]byte(body), &bondedValidator) require.Nil(t, err) - fmt.Println(fmt.Sprintf("–––> DelegatorVal: %v", bondedValidator)) return bondedValidator } func doDelegate(t *testing.T, port, seed, name, password string, delegatorAddr, validatorAddr sdk.AccAddress, amount int64) (resultTx ctypes.ResultBroadcastTxCommit) { - // get the account to get the sequence + acc := getAccount(t, port, delegatorAddr) accnum := acc.GetAccountNumber() sequence := acc.GetSequence() chainID := viper.GetString(client.FlagChainID) - // send jsonStr := []byte(fmt.Sprintf(`{ "name": "%s", "password": "%s", @@ -892,6 +875,7 @@ func doDelegate(t *testing.T, port, seed, name, password string, delegatorAddr, "begin_redelegates": [], "complete_redelegates": [] }`, name, password, accnum, sequence, chainID, delegatorAddr, validatorAddr, "steak", amount)) + res, body := Request(t, port, "POST", fmt.Sprintf("/stake/delegators/%s/delegations", delegatorAddr), jsonStr) require.Equal(t, http.StatusOK, res.StatusCode, body) @@ -905,14 +889,12 @@ func doDelegate(t *testing.T, port, seed, name, password string, delegatorAddr, func doBeginUnbonding(t *testing.T, port, seed, name, password string, delegatorAddr, validatorAddr sdk.AccAddress, amount int64) (resultTx ctypes.ResultBroadcastTxCommit) { - // get the account to get the sequence acc := getAccount(t, port, delegatorAddr) accnum := acc.GetAccountNumber() sequence := acc.GetSequence() chainID := viper.GetString(client.FlagChainID) - // send jsonStr := []byte(fmt.Sprintf(`{ "name": "%s", "password": "%s", @@ -932,6 +914,7 @@ func doBeginUnbonding(t *testing.T, port, seed, name, password string, "begin_redelegates": [], "complete_redelegates": [] }`, name, password, accnum, sequence, chainID, delegatorAddr, validatorAddr, amount)) + res, body := Request(t, port, "POST", fmt.Sprintf("/stake/delegators/%s/delegations", delegatorAddr), jsonStr) require.Equal(t, http.StatusOK, res.StatusCode, body) @@ -945,14 +928,12 @@ func doBeginUnbonding(t *testing.T, port, seed, name, password string, func doBeginRedelegation(t *testing.T, port, seed, name, password string, delegatorAddr, validatorSrcAddr, validatorDstAddr sdk.AccAddress) (resultTx ctypes.ResultBroadcastTxCommit) { - // get the account to get the sequence acc := getAccount(t, port, delegatorAddr) accnum := acc.GetAccountNumber() sequence := acc.GetSequence() chainID := viper.GetString(client.FlagChainID) - // send jsonStr := []byte(fmt.Sprintf(`{ "name": "%s", "password": "%s", @@ -973,6 +954,7 @@ func doBeginRedelegation(t *testing.T, port, seed, name, password string, ], "complete_redelegates": [] }`, name, password, accnum, sequence, chainID, delegatorAddr, validatorSrcAddr, validatorDstAddr)) + res, body := Request(t, port, "POST", fmt.Sprintf("/stake/delegators/%s/delegations", delegatorAddr), jsonStr) require.Equal(t, http.StatusOK, res.StatusCode, body) @@ -984,7 +966,6 @@ func doBeginRedelegation(t *testing.T, port, seed, name, password string, } func getValidators(t *testing.T, port string) []stake.BechValidator { - // get the account to get the sequence res, body := Request(t, port, "GET", "/stake/validators", nil) require.Equal(t, http.StatusOK, res.StatusCode, body) var validators []stake.BechValidator @@ -994,7 +975,6 @@ func getValidators(t *testing.T, port string) []stake.BechValidator { } func getValidator(t *testing.T, port string, validatorAddr sdk.AccAddress) stake.BechValidator { - // get the account to get the sequence res, body := Request(t, port, "GET", fmt.Sprintf("/stake/validators/%s", validatorAddr.String()), nil) require.Equal(t, http.StatusOK, res.StatusCode, body) var validator stake.BechValidator diff --git a/x/stake/client/rest/query.go b/x/stake/client/rest/query.go index 8d15d08d25dc..1741f88234b7 100644 --- a/x/stake/client/rest/query.go +++ b/x/stake/client/rest/query.go @@ -20,49 +20,49 @@ const storeName = "stake" func registerQueryRoutes(cliCtx context.CLIContext, r *mux.Router, cdc *wire.Codec) { - // GET /stake/delegators/{delegatorAddr} // Get all delegations (delegation, undelegation and redelegation) from a delegator + // Get all delegations (delegation, undelegation and redelegation) from a delegator r.HandleFunc( "/stake/delegators/{delegatorAddr}", delegatorHandlerFn(cliCtx, cdc), ).Methods("GET") - // GET /stake/delegators/{delegatorAddr}/txs?type= // Get all staking txs (i.e msgs) from a delegator + // Get all staking txs (i.e msgs) from a delegator r.HandleFunc( "/stake/delegators/{delegatorAddr}/txs", delegatorTxsHandlerFn(cliCtx, cdc), ).Methods("GET") - // GET /stake/delegators/{delegatorAddr}/validators // Query all validators that a delegator is bonded to + // Query all validators that a delegator is bonded to r.HandleFunc( "/stake/delegators/{delegatorAddr}/validators", delegatorValidatorsHandlerFn(cliCtx, cdc), ).Methods("GET") - // GET /stake/delegators/{delegatorAddr}/validators/{validatorAddr} // Query a validator that a delegator is bonded to + // Query a validator that a delegator is bonded to r.HandleFunc( "/stake/delegators/{delegatorAddr}/validators/{validatorAddr}", delegatorValidatorHandlerFn(cliCtx, cdc), ).Methods("GET") - // GET /stake/delegators/{delegatorAddr}/delegations/{validatorAddr} // Query a delegation between a delegator and a validator + // Query a delegation between a delegator and a validator r.HandleFunc( "/stake/delegators/{delegatorAddr}/delegations/{validatorAddr}", delegationHandlerFn(cliCtx, cdc), ).Methods("GET") - // GET /stake/delegators/{delegatorAddr}/unbonding_delegations/{validatorAddr} // Query all unbonding_delegations between a delegator and a validator + // Query all unbonding_delegations between a delegator and a validator r.HandleFunc( "/stake/delegators/{delegatorAddr}/unbonding_delegations/{validatorAddr}", unbondingDelegationsHandlerFn(cliCtx, cdc), ).Methods("GET") - // GET /stake/validators/ + // Get all validators r.HandleFunc( "/stake/validators", validatorsHandlerFn(cliCtx, cdc), ).Methods("GET") - // GET /stake/validators/{addr} + // Get a single validator info r.HandleFunc( "/stake/validators/{addr}", validatorHandlerFn(cliCtx, cdc), From 0619fbb2cbceb519c15d51df1359a24b8883680d Mon Sep 17 00:00:00 2001 From: Federico Kunze Date: Mon, 13 Aug 2018 17:33:07 +0200 Subject: [PATCH 6/7] Deleted more comments --- client/lcd/lcd_test.go | 22 ++++------------------ x/stake/client/rest/utils.go | 4 ---- 2 files changed, 4 insertions(+), 22 deletions(-) diff --git a/client/lcd/lcd_test.go b/client/lcd/lcd_test.go index 90b683926fe3..86fd89261f20 100644 --- a/client/lcd/lcd_test.go +++ b/client/lcd/lcd_test.go @@ -392,25 +392,20 @@ func TestBonding(t *testing.T) { validator1Owner := sdk.AccAddress(pks[0].Address()) validator := getValidator(t, port, validator1Owner) - // create bond TX resultTx := doDelegate(t, port, seed, name, password, addr, validator1Owner, 60) tests.WaitForHeight(resultTx.Height+1, port) - // check if tx was committed require.Equal(t, uint32(0), resultTx.CheckTx.Code) require.Equal(t, uint32(0), resultTx.DeliverTx.Code) - // query sender acc := getAccount(t, port, addr) coins := acc.GetCoins() require.Equal(t, int64(40), coins.AmountOf(denom).Int64()) - // query validator bond := getDelegation(t, port, addr, validator1Owner) require.Equal(t, "60.0000000000", bond.Shares) - // query summary summary := getDelegationSummary(t, port, addr) require.Len(t, summary.Delegations, 1, "Delegation summary holds all delegations") @@ -428,34 +423,29 @@ func TestBonding(t *testing.T) { ////////////////////// // testing unbonding - // create unbond TX resultTx = doBeginUnbonding(t, port, seed, name, password, addr, validator1Owner, 60) tests.WaitForHeight(resultTx.Height+1, port) - // check if tx was committed require.Equal(t, uint32(0), resultTx.CheckTx.Code) require.Equal(t, uint32(0), resultTx.DeliverTx.Code) - // should the sender should have not received any coins as the unbonding has only just begun + // sender should have not received any coins as the unbonding has only just begun acc = getAccount(t, port, addr) coins = acc.GetCoins() require.Equal(t, int64(40), coins.AmountOf("steak").Int64()) - // query unbonding delegation unbondings := getUndelegations(t, port, addr, validator1Owner) require.Len(t, unbondings, 1, "Unbondings holds all unbonding-delegations") require.Equal(t, "60", unbondings[0].Balance.Amount.String()) - // query summary summary = getDelegationSummary(t, port, addr) require.Len(t, summary.Delegations, 0, "Delegation summary holds all delegations") require.Len(t, summary.UnbondingDelegations, 1, "Delegation summary holds all unbonding-delegations") require.Equal(t, "60", summary.UnbondingDelegations[0].Balance.Amount.String()) - // validator still has bonded shares from the delegator bondedValidators = getDelegatorValidators(t, port, addr) - require.Len(t, bondedValidators, 0) + require.Len(t, bondedValidators, 0, "There's no delegation as the user withdraw all funds") // TODO Undonding status not currently implemented // require.Equal(t, sdk.Unbonding, bondedValidators[0].Status) @@ -774,7 +764,6 @@ func getSigningInfo(t *testing.T, port string, validatorPubKey string) slashing. func getDelegation(t *testing.T, port string, delegatorAddr, validatorAddr sdk.AccAddress) rest.DelegationWithoutRat { - // get the account to get the sequence res, body := Request(t, port, "GET", fmt.Sprintf("/stake/delegators/%s/delegations/%s", delegatorAddr, validatorAddr), nil) require.Equal(t, http.StatusOK, res.StatusCode, body) var bond rest.DelegationWithoutRat @@ -785,7 +774,6 @@ func getDelegation(t *testing.T, port string, delegatorAddr, validatorAddr sdk.A func getUndelegations(t *testing.T, port string, delegatorAddr, validatorAddr sdk.AccAddress) []stake.UnbondingDelegation { - // get the account to get the sequence res, body := Request(t, port, "GET", fmt.Sprintf("/stake/delegators/%s/unbonding_delegations/%s", delegatorAddr, validatorAddr), nil) require.Equal(t, http.StatusOK, res.StatusCode, body) var unbondings []stake.UnbondingDelegation @@ -796,7 +784,6 @@ func getUndelegations(t *testing.T, port string, delegatorAddr, validatorAddr sd func getDelegationSummary(t *testing.T, port string, delegatorAddr sdk.AccAddress) rest.DelegationSummary { - // get the account to get the sequence res, body := Request(t, port, "GET", fmt.Sprintf("/stake/delegators/%s", delegatorAddr), nil) require.Equal(t, http.StatusOK, res.StatusCode, body) var summary rest.DelegationSummary @@ -807,7 +794,6 @@ func getDelegationSummary(t *testing.T, port string, delegatorAddr sdk.AccAddres func getBondingTxs(t *testing.T, port string, delegatorAddr sdk.AccAddress, query string) []tx.Info { - // get the account to get the sequence var res *http.Response var body string if len(query) > 0 { @@ -1072,7 +1058,7 @@ func getProposalsFilterStatus(t *testing.T, port string, status gov.ProposalStat } func doSubmitProposal(t *testing.T, port, seed, name, password string, proposerAddr sdk.AccAddress) (resultTx ctypes.ResultBroadcastTxCommit) { - // get the account to get the sequence + acc := getAccount(t, port, proposerAddr) accnum := acc.GetAccountNumber() sequence := acc.GetSequence() @@ -1106,7 +1092,7 @@ func doSubmitProposal(t *testing.T, port, seed, name, password string, proposerA } func doDeposit(t *testing.T, port, seed, name, password string, proposerAddr sdk.AccAddress, proposalID int64) (resultTx ctypes.ResultBroadcastTxCommit) { - // get the account to get the sequence + acc := getAccount(t, port, proposerAddr) accnum := acc.GetAccountNumber() sequence := acc.GetSequence() diff --git a/x/stake/client/rest/utils.go b/x/stake/client/rest/utils.go index 86e714662861..da00eda0bd56 100644 --- a/x/stake/client/rest/utils.go +++ b/x/stake/client/rest/utils.go @@ -29,7 +29,6 @@ func contains(stringSlice []string, txType string) bool { func getDelegatorValidator(cliCtx context.CLIContext, cdc *wire.Codec, delegatorAddr sdk.AccAddress, validatorAccAddr sdk.AccAddress) ( validator types.BechValidator, httpStatusCode int, errMsg string, err error) { - // check if the delegator is bonded or redelegated to the validator keyDel := stake.GetDelegationKey(delegatorAddr, validatorAccAddr) res, err := cliCtx.QueryStore(keyDel, storeName) @@ -46,7 +45,6 @@ func getDelegatorValidator(cliCtx context.CLIContext, cdc *wire.Codec, delegator return types.BechValidator{}, http.StatusInternalServerError, "Error: ", err } if len(kvs) == 0 { - // the query will return empty if there are no delegations return types.BechValidator{}, http.StatusNoContent, "", nil } @@ -65,7 +63,6 @@ func getDelegatorDelegations(cliCtx context.CLIContext, cdc *wire.Codec, delegat return DelegationWithoutRat{}, http.StatusInternalServerError, "couldn't query delegation. Error: ", err } - // the query will return empty if there is no data for this record if len(marshalledDelegation) == 0 { return DelegationWithoutRat{}, http.StatusNoContent, "", nil } @@ -93,7 +90,6 @@ func getDelegatorUndelegations(cliCtx context.CLIContext, cdc *wire.Codec, deleg return types.UnbondingDelegation{}, http.StatusInternalServerError, "couldn't query unbonding-delegation. Error: ", err } - // the query will return empty if there is no data for this record if len(marshalledUnbondingDelegation) == 0 { return types.UnbondingDelegation{}, http.StatusNoContent, "", nil } From 210aa50721a499e40f6bb2a19e4e8b6159ecc081 Mon Sep 17 00:00:00 2001 From: Federico Kunze Date: Mon, 13 Aug 2018 17:52:43 +0200 Subject: [PATCH 7/7] Add spacing --- client/lcd/lcd_test.go | 37 ++++++++++++++++++++----------------- 1 file changed, 20 insertions(+), 17 deletions(-) diff --git a/client/lcd/lcd_test.go b/client/lcd/lcd_test.go index 86fd89261f20..9b8c5e439719 100644 --- a/client/lcd/lcd_test.go +++ b/client/lcd/lcd_test.go @@ -763,83 +763,87 @@ func getSigningInfo(t *testing.T, port string, validatorPubKey string) slashing. // ============= Stake Module ================ func getDelegation(t *testing.T, port string, delegatorAddr, validatorAddr sdk.AccAddress) rest.DelegationWithoutRat { - res, body := Request(t, port, "GET", fmt.Sprintf("/stake/delegators/%s/delegations/%s", delegatorAddr, validatorAddr), nil) require.Equal(t, http.StatusOK, res.StatusCode, body) + var bond rest.DelegationWithoutRat + err := cdc.UnmarshalJSON([]byte(body), &bond) require.Nil(t, err) + return bond } func getUndelegations(t *testing.T, port string, delegatorAddr, validatorAddr sdk.AccAddress) []stake.UnbondingDelegation { - res, body := Request(t, port, "GET", fmt.Sprintf("/stake/delegators/%s/unbonding_delegations/%s", delegatorAddr, validatorAddr), nil) require.Equal(t, http.StatusOK, res.StatusCode, body) + var unbondings []stake.UnbondingDelegation + err := cdc.UnmarshalJSON([]byte(body), &unbondings) require.Nil(t, err) + return unbondings } func getDelegationSummary(t *testing.T, port string, delegatorAddr sdk.AccAddress) rest.DelegationSummary { - res, body := Request(t, port, "GET", fmt.Sprintf("/stake/delegators/%s", delegatorAddr), nil) require.Equal(t, http.StatusOK, res.StatusCode, body) + var summary rest.DelegationSummary + err := cdc.UnmarshalJSON([]byte(body), &summary) require.Nil(t, err) + return summary } func getBondingTxs(t *testing.T, port string, delegatorAddr sdk.AccAddress, query string) []tx.Info { - var res *http.Response var body string + if len(query) > 0 { res, body = Request(t, port, "GET", fmt.Sprintf("/stake/delegators/%s/txs?type=%s", delegatorAddr, query), nil) } else { res, body = Request(t, port, "GET", fmt.Sprintf("/stake/delegators/%s/txs", delegatorAddr), nil) } require.Equal(t, http.StatusOK, res.StatusCode, body) + var txs []tx.Info + err := cdc.UnmarshalJSON([]byte(body), &txs) require.Nil(t, err) + return txs } func getDelegatorValidators(t *testing.T, port string, delegatorAddr sdk.AccAddress) []stake.BechValidator { - - var res *http.Response - var body string - res, body = Request(t, port, "GET", fmt.Sprintf("/stake/delegators/%s/validators", delegatorAddr), nil) - + res, body := Request(t, port, "GET", fmt.Sprintf("/stake/delegators/%s/validators", delegatorAddr), nil) require.Equal(t, http.StatusOK, res.StatusCode, body) + var bondedValidators []stake.BechValidator + err := cdc.UnmarshalJSON([]byte(body), &bondedValidators) require.Nil(t, err) + return bondedValidators } func getDelegatorValidator(t *testing.T, port string, delegatorAddr sdk.AccAddress, validatorAddr sdk.AccAddress) stake.BechValidator { - - var res *http.Response - var body string - res, body = Request(t, port, "GET", fmt.Sprintf("/stake/delegators/%s/validators/%s", delegatorAddr, validatorAddr), nil) - + res, body := Request(t, port, "GET", fmt.Sprintf("/stake/delegators/%s/validators/%s", delegatorAddr, validatorAddr), nil) require.Equal(t, http.StatusOK, res.StatusCode, body) + var bondedValidator stake.BechValidator err := cdc.UnmarshalJSON([]byte(body), &bondedValidator) require.Nil(t, err) + return bondedValidator } func doDelegate(t *testing.T, port, seed, name, password string, delegatorAddr, validatorAddr sdk.AccAddress, amount int64) (resultTx ctypes.ResultBroadcastTxCommit) { - acc := getAccount(t, port, delegatorAddr) accnum := acc.GetAccountNumber() sequence := acc.GetSequence() - chainID := viper.GetString(client.FlagChainID) jsonStr := []byte(fmt.Sprintf(`{ @@ -878,7 +882,6 @@ func doBeginUnbonding(t *testing.T, port, seed, name, password string, acc := getAccount(t, port, delegatorAddr) accnum := acc.GetAccountNumber() sequence := acc.GetSequence() - chainID := viper.GetString(client.FlagChainID) jsonStr := []byte(fmt.Sprintf(`{