-
Notifications
You must be signed in to change notification settings - Fork 3.6k
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: Fedekunze/1939 tests for staking lcd #2000
Changes from 5 commits
91f059d
0f93dd5
6eb7f41
38d34ef
cf6d3d2
927f150
0619fbb
210aa50
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -385,14 +385,15 @@ 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() | ||
|
||
validator1Owner := sdk.AccAddress(pks[0].Address()) | ||
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,40 +410,69 @@ 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) | ||
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 | ||
|
||
// 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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Let's remove any unnecessary commented out code. |
||
// 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 | ||
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()) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ditto |
||
|
||
// 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) | ||
|
||
// resultTx = doBeginUnbonding(t, port, seed, name, password, addr, validator1Owner) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ditto |
||
// 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 | ||
|
@@ -806,7 +836,35 @@ func getBondingTxs(t *testing.T, port string, delegatorAddr sdk.AccAddress, quer | |
return txs | ||
} | ||
|
||
func doDelegate(t *testing.T, port, seed, name, password string, delegatorAddr, validatorAddr sdk.AccAddress) (resultTx ctypes.ResultBroadcastTxCommit) { | ||
func getDelegatorValidators(t *testing.T, port string, delegatorAddr sdk.AccAddress) []stake.BechValidator { | ||
|
||
var res *http.Response | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could we improve the spacing a bit to help the legibility? |
||
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)) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do we need these print statements? |
||
return bondedValidators | ||
} | ||
|
||
func getDelegatorValidator(t *testing.T, port string, delegatorAddr sdk.AccAddress, validatorAddr sdk.AccAddress) stake.BechValidator { | ||
|
||
var res *http.Response | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ditto |
||
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, amount int64) (resultTx ctypes.ResultBroadcastTxCommit) { | ||
// get the account to get the sequence | ||
acc := getAccount(t, port, delegatorAddr) | ||
accnum := acc.GetAccountNumber() | ||
|
@@ -826,14 +884,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) | ||
|
||
|
@@ -845,7 +903,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) | ||
|
@@ -867,13 +925,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) | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do we actually need the comments |
||
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 | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The convention is we use
require
's I believe 👍