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: Redelegation endpoint and Querier #2336

Closed
wants to merge 19 commits into from
Closed
Show file tree
Hide file tree
Changes from 18 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
BUG FIXES

* Tendermint
- Fix unbounded consensus WAL growth
- Fix unbounded consensus WAL growth

## 0.24.1

Expand Down
5 changes: 3 additions & 2 deletions PENDING.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ BREAKING CHANGES
* [simulation] \#2162 Added back correct supply invariants
* [x/slashing] \#2430 Simulate more slashes, check if validator is jailed before jailing
* [x/stake] \#2393 Removed `CompleteUnbonding` and `CompleteRedelegation` Msg types, and instead added unbonding/redelegation queues to endblocker

* SDK
* [core] \#2219 Update to Tendermint 0.24.0
* Validator set updates delayed by one block
Expand Down Expand Up @@ -75,7 +75,7 @@ BREAKING CHANGES
* Mempool now won't build too large blocks, or too computationally expensive blocks
* Maximum tx sizes and gas are now removed, and are implicitly the blocks maximums
* ABCI validators no longer send the pubkey. The pubkey is only sent in validator updates
* Validator set changes are now delayed by one block
* Validator set changes are now delayed by one block
* Block header now includes the next validator sets hash
* BFT time is implemented
* Secp256k1 signature format has changed
Expand All @@ -85,6 +85,7 @@ BREAKING CHANGES
FEATURES

* Gaia REST API (`gaiacli advanced rest-server`)
* [gaia-lite] [\#2182](https://github.com/cosmos/cosmos-sdk/issues/2182) Added redelegation endpoint
* [gaia-lite] Endpoints to query staking pool and params
* [gaia-lite] [\#2110](https://github.com/cosmos/cosmos-sdk/issues/2110) Add support for `simulate=true` requests query argument to endpoints that send txs to run simulations of transactions
* [gaia-lite] [\#966](https://github.com/cosmos/cosmos-sdk/issues/966) Add support for `generate_only=true` query argument to generate offline unsigned transactions
Expand Down
75 changes: 48 additions & 27 deletions client/lcd/lcd_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,13 @@ import (
"time"

"github.com/cosmos/cosmos-sdk/client/tx"
p2p "github.com/tendermint/tendermint/p2p"

"github.com/spf13/viper"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

cryptoKeys "github.com/cosmos/cosmos-sdk/crypto/keys"
p2p "github.com/tendermint/tendermint/p2p"
ctypes "github.com/tendermint/tendermint/rpc/core/types"

client "github.com/cosmos/cosmos-sdk/client"
Expand Down Expand Up @@ -555,17 +555,17 @@ func TestBonding(t *testing.T) {

bondedValidators := getDelegatorValidators(t, port, addr)
require.Len(t, bondedValidators, 1)

require.Equal(t, operAddrs[0], bondedValidators[0].OperatorAddr)
require.Equal(t, validator.DelegatorShares.Add(amt).String(), bondedValidators[0].DelegatorShares.String())

bondedValidator := getDelegatorValidator(t, port, addr, operAddrs[0])
require.Equal(t, operAddrs[0], bondedValidator.OperatorAddr)

//////////////////////
// testing unbonding
// test unbonding

// create unbond TX
resultTx = doBeginUnbonding(t, port, seed, name, password, addr, operAddrs[0], 60)
resultTx = doBeginUnbonding(t, port, seed, name, password, addr, operAddrs[0], 30)
tests.WaitForHeight(resultTx.Height+1, port)

require.Equal(t, uint32(0), resultTx.CheckTx.Code)
Expand All @@ -577,33 +577,49 @@ func TestBonding(t *testing.T) {
require.Equal(t, int64(40), coins.AmountOf("steak").Int64())

unbonding := getUndelegation(t, port, addr, operAddrs[0])
require.Equal(t, "60", unbonding.Balance.Amount.String())
require.Equal(t, "30", unbonding.Balance.Amount.String())

// test redelegation
resultTx = doBeginRedelegation(t, port, seed, name, password, addr, operAddrs[0], operAddrs[1], 30)
tests.WaitForHeight(resultTx.Height+1, port)

require.Equal(t, uint32(0), resultTx.CheckTx.Code)
require.Equal(t, uint32(0), resultTx.DeliverTx.Code)

redelegation := getRedelegation(t, port, addr, operAddrs[0], operAddrs[1])
require.Equal(t, operAddrs[0].String(), redelegation.ValidatorSrcAddr.String())
require.Equal(t, operAddrs[1].String(), redelegation.ValidatorDstAddr.String())
require.Equal(t, "30", redelegation.Balance.Amount.String())

// test that the bond to the validatorDst has increased
delTo := getDelegation(t, port, addr, operAddrs[1])
require.Equal(t, sdk.NewDec(30), delTo.GetShares())

// test delegator summary
summary = getDelegationSummary(t, port, addr)

require.Len(t, summary.Delegations, 0, "Delegation summary holds all delegations")
require.Len(t, summary.Delegations, 1, "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())
assert.Len(t, summary.Redelegations, 1, "Delegation summary holds all redelegations")

bondedValidators = getDelegatorValidators(t, port, addr)
require.Len(t, bondedValidators, 0, "There's no delegation as the user withdraw all funds")
require.Len(t, bondedValidators, 1, "There's a delegation as the user only withdraw half of the funds")

// TODO Undonding status not currently implemented
// require.Equal(t, sdk.Unbonding, bondedValidators[0].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")

// query txs
txs := getBondingTxs(t, port, addr, "")
assert.Len(t, txs, 2, "All Txs found")
require.Len(t, txs, 3, "All Txs found")

txs = getBondingTxs(t, port, addr, "bond")
assert.Len(t, txs, 1, "All bonding txs found")
require.Len(t, txs, 1, "All bonding txs found")

txs = getBondingTxs(t, port, addr, "unbond")
assert.Len(t, txs, 1, "All unbonding txs found")
require.Len(t, txs, 1, "All unbonding txs found")

txs = getBondingTxs(t, port, addr, "redelegate")
require.Len(t, txs, 1, "All redelegation txs found")
}

func TestSubmitProposal(t *testing.T) {
Expand Down Expand Up @@ -955,11 +971,22 @@ func getUndelegation(t *testing.T, port string, delegatorAddr sdk.AccAddress, va
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)
var unbond stake.UnbondingDelegation
err := cdc.UnmarshalJSON([]byte(body), &unbond)
require.Nil(t, err)

return unbond
}

func getRedelegation(t *testing.T, port string, delegatorAddr sdk.AccAddress, validatorSrcAddr, validatorDstAddr sdk.ValAddress) stake.Redelegation {
res, body := Request(t, port, "GET", fmt.Sprintf("/stake/delegators/%s/redelegations/validator_from/%s/validator_to/%s", delegatorAddr, validatorSrcAddr, validatorDstAddr), nil)
require.Equal(t, http.StatusOK, res.StatusCode, body)

var red stake.Redelegation
err := cdc.UnmarshalJSON([]byte(body), &red)
require.Nil(t, err)

return unbondings
return red
}

func getDelegationSummary(t *testing.T, port string, delegatorAddr sdk.AccAddress) stake.DelegationSummary {
Expand Down Expand Up @@ -1033,9 +1060,7 @@ func doDelegate(t *testing.T, port, seed, name, password string,
}
],
"begin_unbondings": [],
"complete_unbondings": [],
"begin_redelegates": [],
"complete_redelegates": [],
"base_req": {
"name": "%s",
"password": "%s",
Expand Down Expand Up @@ -1072,9 +1097,7 @@ func doBeginUnbonding(t *testing.T, port, seed, name, password string,
"shares": "%d"
}
],
"complete_unbondings": [],
"begin_redelegates": [],
"complete_redelegates": [],
"base_req": {
"name": "%s",
"password": "%s",
Expand All @@ -1095,7 +1118,7 @@ func doBeginUnbonding(t *testing.T, port, seed, name, password string,
}

func doBeginRedelegation(t *testing.T, port, seed, name, password string,
delAddr sdk.AccAddress, valSrcAddr, valDstAddr sdk.ValAddress) (resultTx ctypes.ResultBroadcastTxCommit) {
delAddr sdk.AccAddress, valSrcAddr, valDstAddr sdk.ValAddress, amount int64) (resultTx ctypes.ResultBroadcastTxCommit) {

acc := getAccount(t, port, delAddr)
accnum := acc.GetAccountNumber()
Expand All @@ -1106,24 +1129,22 @@ func doBeginRedelegation(t *testing.T, port, seed, name, password string,
jsonStr := []byte(fmt.Sprintf(`{
"delegations": [],
"begin_unbondings": [],
"complete_unbondings": [],
"begin_redelegates": [
{
"delegator_addr": "%s",
"validator_src_addr": "%s",
"validator_dst_addr": "%s",
"shares": "30"
"shares": "%d"
}
],
"complete_redelegates": [],
"base_req": {
"name": "%s",
"password": "%s",
"chain_id": "%s",
"account_number":"%d",
"sequence":"%d"
}
}`, delAddr, valSrcAddr, valDstAddr, name, password, chainID, accnum, sequence))
}`, delAddr, valSrcAddr, valDstAddr, amount, name, password, chainID, accnum, sequence))

res, body := Request(t, port, "POST", fmt.Sprintf("/stake/delegators/%s/delegations", delAddr), jsonStr)
require.Equal(t, http.StatusOK, res.StatusCode, body)
Expand Down
Loading