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

RESTful governance endpoints #1357

Merged
merged 12 commits into from
Jun 27, 2018
2 changes: 1 addition & 1 deletion .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ jobs:
for pkg in $(go list github.com/cosmos/cosmos-sdk/... | grep -v /vendor/ | grep -v github.com/cosmos/cosmos-sdk/cmd/gaia/cli_test | circleci tests split --split-by=timings); do
id=$(basename "$pkg")

go test -timeout 5m -race -coverprofile=/tmp/workspace/profiles/$id.out -covermode=atomic "$pkg"
go test -timeout 8m -race -coverprofile=/tmp/workspace/profiles/$id.out -covermode=atomic "$pkg"
done
- persist_to_workspace:
root: /tmp/workspace
Expand Down
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ BREAKING CHANGES
* Removed MsgChangePubKey from auth
* Removed setPubKey from account mapper
* Removed GetMemo from Tx (it is still on StdTx)
* Gov module REST endpoints changed to be more RESTful
* [cli] rearranged commands under subcommands
* [stake] remove Tick and add EndBlocker
* [stake] introduce concept of unbonding for delegations and validators
Expand Down
138 changes: 134 additions & 4 deletions client/lcd/lcd_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -475,6 +475,10 @@ func TestDeposit(t *testing.T) {
// query proposal
proposal = getProposal(t, port, proposalID)
assert.True(t, proposal.TotalDeposit.IsEqual(sdk.Coins{sdk.NewCoin("steak", 10)}))

// query deposit
deposit := getDeposit(t, port, proposalID, addr)
assert.True(t, deposit.Amount.IsEqual(sdk.Coins{sdk.NewCoin("steak", 10)}))
}

func TestVote(t *testing.T) {
Expand Down Expand Up @@ -515,6 +519,75 @@ func TestVote(t *testing.T) {
assert.Equal(t, gov.VoteOptionToString(gov.OptionYes), vote.Option)
}

func TestProposalsQuery(t *testing.T) {
name, password := "test", "1234567890"
name2, password := "test2", "1234567890"
addr, seed := CreateAddr(t, "test", password, GetKB(t))
addr2, seed2 := CreateAddr(t, "test2", password, GetKB(t))
cleanup, _, port := InitializeTestLCD(t, 1, []sdk.Address{addr, addr2})
defer cleanup()

// Addr1 proposes (and deposits) proposals #1 and #2
resultTx := doSubmitProposal(t, port, seed, name, password, addr)
var proposalID1 int64
cdc.UnmarshalBinaryBare(resultTx.DeliverTx.GetData(), &proposalID1)
tests.WaitForHeight(resultTx.Height+1, port)
resultTx = doSubmitProposal(t, port, seed, name, password, addr)
var proposalID2 int64
cdc.UnmarshalBinaryBare(resultTx.DeliverTx.GetData(), &proposalID2)
tests.WaitForHeight(resultTx.Height+1, port)

// Addr2 proposes (and deposits) proposals #3
resultTx = doSubmitProposal(t, port, seed2, name2, password, addr2)
var proposalID3 int64
cdc.UnmarshalBinaryBare(resultTx.DeliverTx.GetData(), &proposalID3)
tests.WaitForHeight(resultTx.Height+1, port)

// Addr2 deposits on proposals #2 & #3
resultTx = doDeposit(t, port, seed2, name2, password, addr2, proposalID2)
tests.WaitForHeight(resultTx.Height+1, port)
resultTx = doDeposit(t, port, seed2, name2, password, addr2, proposalID3)
tests.WaitForHeight(resultTx.Height+1, port)

// Addr1 votes on proposals #2 & #3
resultTx = doVote(t, port, seed, name, password, addr, proposalID2)
tests.WaitForHeight(resultTx.Height+1, port)
resultTx = doVote(t, port, seed, name, password, addr, proposalID3)
tests.WaitForHeight(resultTx.Height+1, port)

// Addr2 votes on proposal #3
resultTx = doVote(t, port, seed2, name2, password, addr2, proposalID3)
tests.WaitForHeight(resultTx.Height+1, port)

// Test query all proposals
proposals := getProposalsAll(t, port)
assert.Equal(t, proposalID1, (proposals[0]).ProposalID)
assert.Equal(t, proposalID2, (proposals[1]).ProposalID)
assert.Equal(t, proposalID3, (proposals[2]).ProposalID)

// Test query deposited by addr1
proposals = getProposalsFilterDepositer(t, port, addr)
assert.Equal(t, proposalID1, (proposals[0]).ProposalID)

// Test query deposited by addr2
proposals = getProposalsFilterDepositer(t, port, addr2)
assert.Equal(t, proposalID2, (proposals[0]).ProposalID)
assert.Equal(t, proposalID3, (proposals[1]).ProposalID)

// Test query voted by addr1
proposals = getProposalsFilterVoter(t, port, addr)
assert.Equal(t, proposalID2, (proposals[0]).ProposalID)
assert.Equal(t, proposalID3, (proposals[1]).ProposalID)

// Test query voted by addr2
proposals = getProposalsFilterVoter(t, port, addr2)
assert.Equal(t, proposalID3, (proposals[0]).ProposalID)

// Test query voted and deposited by addr1
proposals = getProposalsFilterVoterDepositer(t, port, addr, addr)
assert.Equal(t, proposalID2, (proposals[0]).ProposalID)
}

//_____________________________________________________________________________
// get the account to get the sequence
func getAccount(t *testing.T, port string, addr sdk.Address) auth.Account {
Expand Down Expand Up @@ -766,16 +839,73 @@ func getProposal(t *testing.T, port string, proposalID int64) gov.ProposalRest {
return proposal
}

func getDeposit(t *testing.T, port string, proposalID int64, depositerAddr sdk.Address) gov.DepositRest {
bechDepositerAddr := sdk.MustBech32ifyAcc(depositerAddr)
res, body := Request(t, port, "GET", fmt.Sprintf("/gov/proposals/%d/deposits/%s", proposalID, bechDepositerAddr), nil)
require.Equal(t, http.StatusOK, res.StatusCode, body)
var deposit gov.DepositRest
err := cdc.UnmarshalJSON([]byte(body), &deposit)
require.Nil(t, err)
return deposit
}

func getVote(t *testing.T, port string, proposalID int64, voterAddr sdk.Address) gov.VoteRest {
bechVoterAddr := sdk.MustBech32ifyAcc(voterAddr)
res, body := Request(t, port, "GET", fmt.Sprintf("/gov/votes/%d/%s", proposalID, bechVoterAddr), nil)
res, body := Request(t, port, "GET", fmt.Sprintf("/gov/proposals/%d/votes/%s", proposalID, bechVoterAddr), nil)
require.Equal(t, http.StatusOK, res.StatusCode, body)
var vote gov.VoteRest
err := cdc.UnmarshalJSON([]byte(body), &vote)
require.Nil(t, err)
return vote
}

func getProposalsAll(t *testing.T, port string) []gov.ProposalRest {
res, body := Request(t, port, "GET", "/gov/proposals", nil)
require.Equal(t, http.StatusOK, res.StatusCode, body)

var proposals []gov.ProposalRest
err := cdc.UnmarshalJSON([]byte(body), &proposals)
require.Nil(t, err)
return proposals
}

func getProposalsFilterDepositer(t *testing.T, port string, depositerAddr sdk.Address) []gov.ProposalRest {
bechDepositerAddr := sdk.MustBech32ifyAcc(depositerAddr)

res, body := Request(t, port, "GET", fmt.Sprintf("/gov/proposals?depositer=%s", bechDepositerAddr), nil)
require.Equal(t, http.StatusOK, res.StatusCode, body)

var proposals []gov.ProposalRest
err := cdc.UnmarshalJSON([]byte(body), &proposals)
require.Nil(t, err)
return proposals
}

func getProposalsFilterVoter(t *testing.T, port string, voterAddr sdk.Address) []gov.ProposalRest {
bechVoterAddr := sdk.MustBech32ifyAcc(voterAddr)

res, body := Request(t, port, "GET", fmt.Sprintf("/gov/proposals?voter=%s", bechVoterAddr), nil)
require.Equal(t, http.StatusOK, res.StatusCode, body)

var proposals []gov.ProposalRest
err := cdc.UnmarshalJSON([]byte(body), &proposals)
require.Nil(t, err)
return proposals
}

func getProposalsFilterVoterDepositer(t *testing.T, port string, voterAddr sdk.Address, depositerAddr sdk.Address) []gov.ProposalRest {
bechVoterAddr := sdk.MustBech32ifyAcc(voterAddr)
bechDepositerAddr := sdk.MustBech32ifyAcc(depositerAddr)

res, body := Request(t, port, "GET", fmt.Sprintf("/gov/proposals?depositer=%s&voter=%s", bechDepositerAddr, bechVoterAddr), nil)
require.Equal(t, http.StatusOK, res.StatusCode, body)

var proposals []gov.ProposalRest
err := cdc.UnmarshalJSON([]byte(body), &proposals)
require.Nil(t, err)
return proposals
}

func doSubmitProposal(t *testing.T, port, seed, name, password string, proposerAddr sdk.Address) (resultTx ctypes.ResultBroadcastTxCommit) {
// get the account to get the sequence
acc := getAccount(t, port, proposerAddr)
Expand All @@ -802,7 +932,7 @@ func doSubmitProposal(t *testing.T, port, seed, name, password string, proposerA
"gas": 100000
}
}`, bechProposerAddr, name, password, chainID, accnum, sequence))
res, body := Request(t, port, "POST", "/gov/submitproposal", jsonStr)
res, body := Request(t, port, "POST", "/gov/proposals", jsonStr)
fmt.Println(res)
require.Equal(t, http.StatusOK, res.StatusCode, body)

Expand Down Expand Up @@ -837,7 +967,7 @@ func doDeposit(t *testing.T, port, seed, name, password string, proposerAddr sdk
"gas": 100000
}
}`, bechProposerAddr, proposalID, name, password, chainID, accnum, sequence))
res, body := Request(t, port, "POST", "/gov/deposit", jsonStr)
res, body := Request(t, port, "POST", fmt.Sprintf("/gov/proposals/%d/deposits", proposalID), jsonStr)
fmt.Println(res)
require.Equal(t, http.StatusOK, res.StatusCode, body)

Expand Down Expand Up @@ -872,7 +1002,7 @@ func doVote(t *testing.T, port, seed, name, password string, proposerAddr sdk.Ad
"gas": 100000
}
}`, bechProposerAddr, proposalID, name, password, chainID, accnum, sequence))
res, body := Request(t, port, "POST", "/gov/vote", jsonStr)
res, body := Request(t, port, "POST", fmt.Sprintf("/gov/proposals/%d/votes", proposalID), jsonStr)
fmt.Println(res)
require.Equal(t, http.StatusOK, res.StatusCode, body)

Expand Down
Loading