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: Staking Gaia-lite (ex LCD) refactor #1880

Merged
merged 53 commits into from
Aug 8, 2018
Merged
Show file tree
Hide file tree
Changes from 47 commits
Commits
Show all changes
53 commits
Select commit Hold shift + click to select a range
2f75f66
Updated some staking txs routes (bond, unbond, redelegate)
Jul 27, 2018
6f87e3f
LCD utils for validators
Jul 27, 2018
df3e021
Updated DelegatorTxs route
Jul 30, 2018
72553a2
Merge branch 'develop' of https://github.com/cosmos/cosmos-sdk into f…
Jul 30, 2018
5f5b07d
Updated endpoints
Jul 30, 2018
0174084
Moved staking LCD tests to module
Jul 30, 2018
3a26b46
Revert prev commit
Jul 30, 2018
efcc11b
remove logger output
faboweb Jul 31, 2018
8f5334a
linted
faboweb Jul 31, 2018
f0d2eb5
removed module test
faboweb Jul 31, 2018
bb540d4
gopgk update
faboweb Jul 31, 2018
88e1bd5
summary in place
faboweb Jul 31, 2018
dbc284d
simple test for summary
faboweb Jul 31, 2018
551cef2
remove rat from share output
faboweb Jul 31, 2018
deedd2c
added validator endpoint
faboweb Jul 31, 2018
a013f84
added unbonding delegations endpoint
faboweb Jul 31, 2018
35c4ff7
fixed wrongly using accaddress for validators
faboweb Jul 31, 2018
21a05d1
adding txs endpoint (not working)
faboweb Jul 31, 2018
ee87537
Refactor completed but redelegation txs
Jul 31, 2018
c3b29fa
removed some prints
faboweb Jul 31, 2018
e867c8c
Merge pull request #1880: Staking Gaia-lite (ex LCD) refactor
Jul 31, 2018
e57980e
Pull most recent updates
Jul 31, 2018
d7297b5
updated endpoints API docs
Jul 31, 2018
00baf3f
Updated docs with staking Gaia Lite routes
Aug 1, 2018
71bc4d1
Merge branch 'develop' of https://github.com/cosmos/cosmos-sdk into f…
Aug 1, 2018
0f4040c
deleted unused helpers file
Aug 1, 2018
20c490f
Fixed some linter errors
Aug 1, 2018
d6dc156
Reduced complexity of staking tx query
Aug 1, 2018
e2cab9f
More golinter fixes
Aug 1, 2018
84b3de7
Logic for delegatorValidators routes. Tests missing
Aug 1, 2018
eb611e1
Addressed Aleks comments
Aug 1, 2018
14ec625
Refactor handlers with util funcs
Aug 1, 2018
a7884db
More refactoring on handlers
Aug 1, 2018
120be9c
minor fix
Aug 1, 2018
e2064a4
addresses comments
faboweb Aug 2, 2018
7722435
added validator address to output
faboweb Aug 2, 2018
9ee3d47
Revert "added validator address to output"
faboweb Aug 2, 2018
c683089
do not add empty delegations et al
faboweb Aug 2, 2018
43f3ca8
reduced complexity
Aug 2, 2018
f53130f
updated comments
Aug 3, 2018
724aae6
reverted valaddr back to accaddr
faboweb Aug 3, 2018
8fe38e3
Merge branch 'fedekunze/lcd_refactor' of https://github.com/cosmos/co…
faboweb Aug 3, 2018
e0a8f04
fixed tests
faboweb Aug 3, 2018
71cc9ce
Merge from develop
Aug 7, 2018
a0e35c9
Merge branch 'fedekunze/lcd_refactor' of https://github.com/cosmos/co…
Aug 7, 2018
33f763f
Fixed conflicts
Aug 7, 2018
8abcdaa
Fixed Gopkg.lock
Aug 7, 2018
7030081
formatting
rigelrozanski Aug 8, 2018
a8261a3
addressed new comments by Rige
Aug 8, 2018
9ea4d6d
Deleted key comment
Aug 8, 2018
94c7507
Deleted comments for TODO in #1939
Aug 8, 2018
ab25efe
Merge branch 'develop' into fedekunze/lcd_refactor
cwgoes Aug 8, 2018
ccb3fcb
dep ensure -v -update
cwgoes Aug 8, 2018
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
3 changes: 1 addition & 2 deletions Gopkg.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions PENDING.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
## PENDING

BREAKING CHANGES
* API
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

++

- \#1880 [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`
Expand Down
121 changes: 104 additions & 17 deletions client/lcd/lcd_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@ import (
"regexp"
"testing"

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

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

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

Expand All @@ -27,6 +27,7 @@ import (
"github.com/cosmos/cosmos-sdk/x/gov"
"github.com/cosmos/cosmos-sdk/x/slashing"
"github.com/cosmos/cosmos-sdk/x/stake"
"github.com/cosmos/cosmos-sdk/x/stake/client/rest"
)

func init() {
Expand Down Expand Up @@ -316,13 +317,7 @@ func TestTxs(t *testing.T) {
res, body = Request(t, port, "GET", fmt.Sprintf("/txs/%s", resultTx.Hash), nil)
require.Equal(t, http.StatusOK, res.StatusCode, body)

type txInfo struct {
Hash common.HexBytes `json:"hash"`
Height int64 `json:"height"`
Tx sdk.Tx `json:"tx"`
Result abci.ResponseDeliverTx `json:"result"`
}
var indexedTxs []txInfo
var indexedTxs []tx.Info

// check if tx is queryable
res, body = Request(t, port, "GET", fmt.Sprintf("/txs?tag=tx.hash='%s'", resultTx.Hash), nil)
Expand Down Expand Up @@ -374,6 +369,19 @@ func TestValidatorsQuery(t *testing.T) {
require.True(t, foundVal, "pkBech %v, owner %v", pkBech, validators[0].Owner)
}

func TestValidatorQuery(t *testing.T) {
cleanup, pks, port := InitializeTestLCD(t, 1, []sdk.AccAddress{})
defer cleanup()
require.Equal(t, 1, len(pks))

validator1Owner := sdk.AccAddress(pks[0].Address())

validator := getValidator(t, port, validator1Owner)
bech32ValAddress, err := sdk.Bech32ifyValPub(pks[0])
require.NoError(t, err)
assert.Equal(t, validator.PubKey, bech32ValAddress, "The returned validator does not hold the correct data")
}

func TestBonding(t *testing.T) {
name, password, denom := "test", "1234567890", "steak"
addr, seed := CreateAddr(t, "test", password, GetKeyBase(t))
Expand All @@ -398,7 +406,7 @@ func TestBonding(t *testing.T) {

// query validator
bond := getDelegation(t, port, addr, validator1Owner)
require.Equal(t, "60/1", bond.Shares.String())
require.Equal(t, "60.0000000000", bond.Shares)

//////////////////////
// testing unbonding
Expand All @@ -409,7 +417,7 @@ func TestBonding(t *testing.T) {

// query validator
bond = getDelegation(t, port, addr, validator1Owner)
require.Equal(t, "30/1", bond.Shares.String())
require.Equal(t, "30.0000000000", bond.Shares)

// check if tx was committed
require.Equal(t, uint32(0), resultTx.CheckTx.Code)
Expand All @@ -421,7 +429,33 @@ func TestBonding(t *testing.T) {
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)
assert.Len(t, unbondings, 1, "Unbondings holds all unbonding-delegations")
assert.Equal(t, "30", unbondings[0].Balance.Amount.String())

// query summary
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.UnbondingDelegations, 1, "Delegation summary holds all unbonding-delegations")
assert.Equal(t, "30", summary.UnbondingDelegations[0].Balance.Amount.String())

// 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")

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

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

func TestSubmitProposal(t *testing.T) {
Expand Down Expand Up @@ -719,17 +753,58 @@ func getSigningInfo(t *testing.T, port string, validatorPubKey string) slashing.
return signingInfo
}

func getDelegation(t *testing.T, port string, delegatorAddr, validatorAddr sdk.AccAddress) stake.Delegation {
// ============= Stake Module ================

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/%s/delegation/%s", delegatorAddr, validatorAddr), nil)
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 stake.Delegation
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 {

// 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
err := cdc.UnmarshalJSON([]byte(body), &unbondings)
require.Nil(t, err)
return unbondings
}

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
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 {

// get the account to get the sequence
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 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)
Expand Down Expand Up @@ -758,7 +833,7 @@ func doDelegate(t *testing.T, port, seed, name, password string, delegatorAddr,
"begin_redelegates": [],
"complete_redelegates": []
}`, name, password, accnum, sequence, chainID, delegatorAddr, validatorAddr, "steak"))
res, body := Request(t, port, "POST", "/stake/delegations", jsonStr)
res, body := Request(t, port, "POST", fmt.Sprintf("/stake/delegators/%s/delegations", delegatorAddr), jsonStr)
require.Equal(t, http.StatusOK, res.StatusCode, body)

var results []ctypes.ResultBroadcastTxCommit
Expand Down Expand Up @@ -798,7 +873,7 @@ func doBeginUnbonding(t *testing.T, port, seed, name, password string,
"begin_redelegates": [],
"complete_redelegates": []
}`, name, password, accnum, sequence, chainID, delegatorAddr, validatorAddr))
res, body := Request(t, port, "POST", "/stake/delegations", jsonStr)
res, body := Request(t, port, "POST", fmt.Sprintf("/stake/delegators/%s/delegations", delegatorAddr), jsonStr)
require.Equal(t, http.StatusOK, res.StatusCode, body)

var results []ctypes.ResultBroadcastTxCommit
Expand Down Expand Up @@ -839,7 +914,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", "/stake/delegations", jsonStr)
res, body := Request(t, port, "POST", fmt.Sprintf("/stake/delegators/%s/delegations", delegatorAddr), jsonStr)
require.Equal(t, http.StatusOK, res.StatusCode, body)

var results []ctypes.ResultBroadcastTxCommit
Expand All @@ -859,6 +934,18 @@ func getValidators(t *testing.T, port string) []stake.BechValidator {
return validators
}

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
err := cdc.UnmarshalJSON([]byte(body), &validator)
require.Nil(t, err)
return validator
}

// ============= Governance Module ================

func getProposal(t *testing.T, port string, proposalID int64) gov.Proposal {
res, body := Request(t, port, "GET", fmt.Sprintf("/gov/proposals/%d", proposalID), nil)
require.Equal(t, http.StatusOK, res.StatusCode, body)
Expand Down
3 changes: 2 additions & 1 deletion client/lcd/test_helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ func InitializeTestLCD(t *testing.T, nValidators int, initAddrs []sdk.AccAddress
config.TxIndex.IndexAllTags = true

logger := log.NewTMLogger(log.NewSyncWriter(os.Stdout))
logger = log.NewFilter(logger, log.AllowDebug())
logger = log.NewFilter(logger, log.AllowError())

privValidatorFile := config.PrivValidatorFile()
privVal := pvm.LoadOrGenFilePV(privValidatorFile)
Expand Down Expand Up @@ -256,6 +256,7 @@ func Request(t *testing.T, port, method, path string, payload []byte) (*http.Res
res *http.Response
)
url := fmt.Sprintf("http://localhost:%v%v", port, path)
fmt.Println("REQUEST " + method + " " + url)

req, err := http.NewRequest(method, url, bytes.NewBuffer(payload))
require.Nil(t, err)
Expand Down
2 changes: 1 addition & 1 deletion client/tx/broadcast.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
"github.com/cosmos/cosmos-sdk/client/context"
)

// Tx Broadcast Body
// BroadcastTxBody Tx Broadcast Body
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

revert addition

type BroadcastTxBody struct {
TxBytes string `json:"tx"`
}
Expand Down
10 changes: 5 additions & 5 deletions client/tx/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,23 +75,23 @@ func queryTx(cdc *wire.Codec, cliCtx context.CLIContext, hashHexStr string, trus
return wire.MarshalJSONIndent(cdc, info)
}

func formatTxResult(cdc *wire.Codec, res *ctypes.ResultTx) (txInfo, error) {
func formatTxResult(cdc *wire.Codec, res *ctypes.ResultTx) (Info, error) {
// TODO: verify the proof if requested
tx, err := parseTx(cdc, res.Tx)
if err != nil {
return txInfo{}, err
return Info{}, err
}

return txInfo{
return Info{
Hash: res.Hash,
Height: res.Height,
Tx: tx,
Result: res.TxResult,
}, nil
}

// txInfo is used to prepare info to display
type txInfo struct {
// Info is used to prepare info to display
type Info struct {
Hash common.HexBytes `json:"hash"`
Height int64 `json:"height"`
Tx sdk.Tx `json:"tx"`
Expand Down
11 changes: 6 additions & 5 deletions client/tx/search.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ const (
flagAny = "any"
)

// default client command to search through tagged transactions
// Default client command to search through tagged transactions
func SearchTxCmd(cdc *wire.Codec) *cobra.Command {
cmd := &cobra.Command{
Use: "txs",
Expand Down Expand Up @@ -57,7 +57,7 @@ func SearchTxCmd(cdc *wire.Codec) *cobra.Command {
return cmd
}

func searchTxs(cliCtx context.CLIContext, cdc *wire.Codec, tags []string) ([]txInfo, error) {
func searchTxs(cliCtx context.CLIContext, cdc *wire.Codec, tags []string) ([]Info, error) {
if len(tags) == 0 {
return nil, errors.New("must declare at least one tag to search")
}
Expand All @@ -81,17 +81,18 @@ func searchTxs(cliCtx context.CLIContext, cdc *wire.Codec, tags []string) ([]txI
return nil, err
}

info, err := formatTxResults(cdc, res.Txs)
info, err := FormatTxResults(cdc, res.Txs)
if err != nil {
return nil, err
}

return info, nil
}

func formatTxResults(cdc *wire.Codec, res []*ctypes.ResultTx) ([]txInfo, error) {
// Parse the indexed txs into an array of Info
func FormatTxResults(cdc *wire.Codec, res []*ctypes.ResultTx) ([]Info, error) {
var err error
out := make([]txInfo, len(res))
out := make([]Info, len(res))
for i := range res {
out[i], err = formatTxResult(cdc, res[i])
if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion client/tx/sign.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
keys "github.com/cosmos/cosmos-sdk/crypto/keys"
)

// REST request body
// REST request body for signed txs
// TODO does this need to be exposed?
type SignTxBody struct {
Name string `json:"name"`
Expand Down
3 changes: 2 additions & 1 deletion cmd/cosmos-sdk-cli/cmd/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,11 @@ import (
"os"
"strings"

"path/filepath"

"github.com/cosmos/cosmos-sdk/version"
"github.com/spf13/cobra"
tmversion "github.com/tendermint/tendermint/version"
"path/filepath"
)

var remoteBasecoinPath = "github.com/cosmos/cosmos-sdk/examples/basecoin"
Expand Down
Loading