cosmos-sdk/x/gov/client/rest/rest.go

521 lines
14 KiB
Go
Raw Normal View History

2018-06-21 17:19:14 -07:00
package rest
import (
"fmt"
"net/http"
"strconv"
"github.com/cosmos/cosmos-sdk/client/context"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/wire"
"github.com/cosmos/cosmos-sdk/x/gov"
2018-08-06 11:11:30 -07:00
2018-06-21 17:19:14 -07:00
"github.com/gorilla/mux"
"github.com/pkg/errors"
)
// REST Variable names
// nolint
const (
RestProposalID = "proposal-id"
RestDepositer = "depositer"
RestVoter = "voter"
RestProposalStatus = "status"
2018-08-04 22:56:48 -07:00
RestNumLatest = "latest"
storeName = "gov"
2018-06-21 17:19:14 -07:00
)
// RegisterRoutes - Central function to define routes that get registered by the main application
2018-08-06 11:11:30 -07:00
func RegisterRoutes(cliCtx context.CLIContext, r *mux.Router, cdc *wire.Codec) {
r.HandleFunc("/gov/proposals", postProposalHandlerFn(cdc, cliCtx)).Methods("POST")
r.HandleFunc(fmt.Sprintf("/gov/proposals/{%s}/deposits", RestProposalID), depositHandlerFn(cdc, cliCtx)).Methods("POST")
r.HandleFunc(fmt.Sprintf("/gov/proposals/{%s}/votes", RestProposalID), voteHandlerFn(cdc, cliCtx)).Methods("POST")
r.HandleFunc(fmt.Sprintf("/gov/proposals/{%s}", RestProposalID), queryProposalHandlerFn(cdc)).Methods("GET")
r.HandleFunc(fmt.Sprintf("/gov/proposals/{%s}/deposits/{%s}", RestProposalID, RestDepositer), queryDepositHandlerFn(cdc)).Methods("GET")
r.HandleFunc(fmt.Sprintf("/gov/proposals/{%s}/votes/{%s}", RestProposalID, RestVoter), queryVoteHandlerFn(cdc)).Methods("GET")
r.HandleFunc(fmt.Sprintf("/gov/proposals/{%s}/votes", RestProposalID), queryVotesOnProposalHandlerFn(cdc)).Methods("GET")
r.HandleFunc("/gov/proposals", queryProposalsWithParameterFn(cdc)).Methods("GET")
2018-06-21 17:19:14 -07:00
}
type postProposalReq struct {
2018-07-10 17:59:07 -07:00
BaseReq baseReq `json:"base_req"`
Title string `json:"title"` // Title of the proposal
Description string `json:"description"` // Description of the proposal
ProposalType gov.ProposalKind `json:"proposal_type"` // Type of proposal. Initial set {PlainTextProposal, SoftwareUpgradeProposal}
Proposer sdk.AccAddress `json:"proposer"` // Address of the proposer
InitialDeposit sdk.Coins `json:"initial_deposit"` // Coins to add to the proposal's deposit
2018-06-21 17:19:14 -07:00
}
type depositReq struct {
2018-07-10 17:59:07 -07:00
BaseReq baseReq `json:"base_req"`
Depositer sdk.AccAddress `json:"depositer"` // Address of the depositer
Amount sdk.Coins `json:"amount"` // Coins to add to the proposal's deposit
2018-06-21 17:19:14 -07:00
}
type voteReq struct {
2018-07-10 17:59:07 -07:00
BaseReq baseReq `json:"base_req"`
Voter sdk.AccAddress `json:"voter"` // address of the voter
Option gov.VoteOption `json:"option"` // option from OptionSet chosen by the voter
2018-06-21 17:19:14 -07:00
}
2018-08-06 11:11:30 -07:00
func postProposalHandlerFn(cdc *wire.Codec, cliCtx context.CLIContext) http.HandlerFunc {
2018-06-21 17:19:14 -07:00
return func(w http.ResponseWriter, r *http.Request) {
var req postProposalReq
err := buildReq(w, r, cdc, &req)
2018-06-21 17:19:14 -07:00
if err != nil {
return
}
if !req.BaseReq.baseReqValidate(w) {
return
}
// create the message
2018-07-10 17:59:07 -07:00
msg := gov.NewMsgSubmitProposal(req.Title, req.Description, req.ProposalType, req.Proposer, req.InitialDeposit)
2018-06-21 17:19:14 -07:00
err = msg.ValidateBasic()
if err != nil {
writeErr(&w, http.StatusBadRequest, err.Error())
return
}
2018-08-06 11:11:30 -07:00
signAndBuild(w, cliCtx, req.BaseReq, msg, cdc)
2018-06-21 17:19:14 -07:00
}
}
2018-08-06 11:11:30 -07:00
func depositHandlerFn(cdc *wire.Codec, cliCtx context.CLIContext) http.HandlerFunc {
2018-06-21 17:19:14 -07:00
return func(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
strProposalID := vars[RestProposalID]
if len(strProposalID) == 0 {
w.WriteHeader(http.StatusBadRequest)
err := errors.New("proposalId required but not specified")
w.Write([]byte(err.Error()))
2018-08-06 11:11:30 -07:00
return
}
proposalID, err := strconv.ParseInt(strProposalID, 10, 64)
if err != nil {
err := errors.Errorf("proposalID [%d] is not positive", proposalID)
w.Write([]byte(err.Error()))
2018-08-06 11:11:30 -07:00
return
}
2018-06-21 17:19:14 -07:00
var req depositReq
err = buildReq(w, r, cdc, &req)
2018-06-21 17:19:14 -07:00
if err != nil {
return
}
if !req.BaseReq.baseReqValidate(w) {
return
}
// create the message
2018-07-10 17:59:07 -07:00
msg := gov.NewMsgDeposit(req.Depositer, proposalID, req.Amount)
2018-06-21 17:19:14 -07:00
err = msg.ValidateBasic()
if err != nil {
writeErr(&w, http.StatusBadRequest, err.Error())
return
}
2018-08-06 11:11:30 -07:00
signAndBuild(w, cliCtx, req.BaseReq, msg, cdc)
2018-06-21 17:19:14 -07:00
}
}
2018-08-06 11:11:30 -07:00
func voteHandlerFn(cdc *wire.Codec, cliCtx context.CLIContext) http.HandlerFunc {
2018-06-21 17:19:14 -07:00
return func(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
strProposalID := vars[RestProposalID]
if len(strProposalID) == 0 {
w.WriteHeader(http.StatusBadRequest)
err := errors.New("proposalId required but not specified")
w.Write([]byte(err.Error()))
2018-08-06 11:11:30 -07:00
return
}
proposalID, err := strconv.ParseInt(strProposalID, 10, 64)
if err != nil {
err := errors.Errorf("proposalID [%d] is not positive", proposalID)
w.Write([]byte(err.Error()))
return
}
2018-06-21 17:19:14 -07:00
var req voteReq
err = buildReq(w, r, cdc, &req)
2018-06-21 17:19:14 -07:00
if err != nil {
return
}
if !req.BaseReq.baseReqValidate(w) {
return
}
// create the message
2018-07-10 17:59:07 -07:00
msg := gov.NewMsgVote(req.Voter, proposalID, req.Option)
2018-06-21 17:19:14 -07:00
err = msg.ValidateBasic()
if err != nil {
writeErr(&w, http.StatusBadRequest, err.Error())
return
}
2018-08-06 11:11:30 -07:00
signAndBuild(w, cliCtx, req.BaseReq, msg, cdc)
2018-06-21 17:19:14 -07:00
}
}
func queryProposalHandlerFn(cdc *wire.Codec) http.HandlerFunc {
2018-06-21 17:19:14 -07:00
return func(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
strProposalID := vars[RestProposalID]
2018-06-21 17:19:14 -07:00
if len(strProposalID) == 0 {
w.WriteHeader(http.StatusBadRequest)
err := errors.New("proposalId required but not specified")
w.Write([]byte(err.Error()))
2018-08-06 11:11:30 -07:00
2018-06-21 17:19:14 -07:00
return
}
proposalID, err := strconv.ParseInt(strProposalID, 10, 64)
if err != nil {
err := errors.Errorf("proposalID [%d] is not positive", proposalID)
w.Write([]byte(err.Error()))
2018-08-06 11:11:30 -07:00
2018-06-21 17:19:14 -07:00
return
}
2018-08-06 11:11:30 -07:00
cliCtx := context.NewCLIContext().WithCodec(cdc)
2018-06-21 17:19:14 -07:00
2018-08-04 22:56:48 -07:00
params := gov.QueryProposalParams{
ProposalID: proposalID,
}
res, err := cliCtx.QueryWithData("custom/gov/proposal", cdc.MustMarshalBinary(params))
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
2018-06-21 17:19:14 -07:00
w.Write([]byte(err.Error()))
2018-08-06 11:11:30 -07:00
2018-06-21 17:19:14 -07:00
return
}
var proposal gov.Proposal
cdc.MustUnmarshalBinary(res, &proposal)
2018-08-06 11:11:30 -07:00
2018-07-10 17:59:07 -07:00
output, err := wire.MarshalJSONIndent(cdc, proposal)
2018-06-21 17:19:14 -07:00
if err != nil {
w.WriteHeader(http.StatusBadRequest)
w.Write([]byte(err.Error()))
2018-08-06 11:11:30 -07:00
2018-06-21 17:19:14 -07:00
return
}
2018-08-06 11:11:30 -07:00
2018-06-21 17:19:14 -07:00
w.Write(output)
}
}
func queryDepositHandlerFn(cdc *wire.Codec) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
strProposalID := vars[RestProposalID]
bechDepositerAddr := vars[RestDepositer]
if len(strProposalID) == 0 {
w.WriteHeader(http.StatusBadRequest)
err := errors.New("proposalId required but not specified")
w.Write([]byte(err.Error()))
2018-08-06 11:11:30 -07:00
return
}
proposalID, err := strconv.ParseInt(strProposalID, 10, 64)
if err != nil {
w.WriteHeader(http.StatusBadRequest)
err := errors.Errorf("proposalID [%d] is not positive", proposalID)
w.Write([]byte(err.Error()))
2018-08-06 11:11:30 -07:00
return
}
if len(bechDepositerAddr) == 0 {
w.WriteHeader(http.StatusBadRequest)
err := errors.New("depositer address required but not specified")
w.Write([]byte(err.Error()))
2018-08-06 11:11:30 -07:00
return
}
2018-07-09 16:06:05 -07:00
depositerAddr, err := sdk.AccAddressFromBech32(bechDepositerAddr)
if err != nil {
w.WriteHeader(http.StatusBadRequest)
err := errors.Errorf("'%s' needs to be bech32 encoded", RestDepositer)
w.Write([]byte(err.Error()))
2018-08-06 11:11:30 -07:00
return
}
2018-08-06 11:11:30 -07:00
cliCtx := context.NewCLIContext().WithCodec(cdc)
2018-08-04 22:56:48 -07:00
params := gov.QueryDepositParams{
ProposalID: proposalID,
Depositer: depositerAddr,
}
2018-08-06 11:11:30 -07:00
2018-08-04 22:56:48 -07:00
res, err := cliCtx.QueryWithData("custom/gov/deposit", cdc.MustMarshalBinary(params))
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte(err.Error()))
2018-08-06 11:11:30 -07:00
return
}
var deposit gov.Deposit
cdc.MustUnmarshalBinary(res, &deposit)
2018-08-06 11:11:30 -07:00
2018-07-10 17:59:07 -07:00
output, err := wire.MarshalJSONIndent(cdc, deposit)
if err != nil {
w.WriteHeader(http.StatusBadRequest)
w.Write([]byte(err.Error()))
2018-08-06 11:11:30 -07:00
return
}
2018-08-04 22:56:48 -07:00
if deposit.Empty() {
res, err := cliCtx.QueryWithData("custom/gov/proposal", cdc.MustMarshalBinary(gov.QueryProposalParams{params.ProposalID}))
if err != nil || len(res) == 0 {
w.WriteHeader(http.StatusNotFound)
err := errors.Errorf("proposalID [%d] does not exist", proposalID)
w.Write([]byte(err.Error()))
return
}
w.WriteHeader(http.StatusNotFound)
err = errors.Errorf("depositer [%s] did not deposit on proposalID [%d]", bechDepositerAddr, proposalID)
w.Write([]byte(err.Error()))
return
}
w.Write(output)
}
}
func queryVoteHandlerFn(cdc *wire.Codec) http.HandlerFunc {
2018-06-21 17:19:14 -07:00
return func(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
strProposalID := vars[RestProposalID]
2018-06-21 17:19:14 -07:00
bechVoterAddr := vars[RestVoter]
if len(strProposalID) == 0 {
w.WriteHeader(http.StatusBadRequest)
err := errors.New("proposalId required but not specified")
w.Write([]byte(err.Error()))
return
}
proposalID, err := strconv.ParseInt(strProposalID, 10, 64)
if err != nil {
w.WriteHeader(http.StatusBadRequest)
2018-06-21 17:19:14 -07:00
err := errors.Errorf("proposalID [%s] is not positive", proposalID)
w.Write([]byte(err.Error()))
2018-08-06 11:11:30 -07:00
2018-06-21 17:19:14 -07:00
return
}
if len(bechVoterAddr) == 0 {
w.WriteHeader(http.StatusBadRequest)
err := errors.New("voter address required but not specified")
w.Write([]byte(err.Error()))
return
}
2018-07-09 16:06:05 -07:00
voterAddr, err := sdk.AccAddressFromBech32(bechVoterAddr)
2018-06-21 17:19:14 -07:00
if err != nil {
w.WriteHeader(http.StatusBadRequest)
err := errors.Errorf("'%s' needs to be bech32 encoded", RestVoter)
w.Write([]byte(err.Error()))
2018-08-06 11:11:30 -07:00
2018-06-21 17:19:14 -07:00
return
}
2018-08-06 11:11:30 -07:00
cliCtx := context.NewCLIContext().WithCodec(cdc)
2018-06-21 17:19:14 -07:00
2018-08-04 22:56:48 -07:00
params := gov.QueryVoteParams{
Voter: voterAddr,
ProposalID: proposalID,
}
2018-08-06 11:11:30 -07:00
2018-08-04 22:56:48 -07:00
res, err := cliCtx.QueryWithData("custom/gov/vote", cdc.MustMarshalBinary(params))
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
2018-06-21 17:19:14 -07:00
w.Write([]byte(err.Error()))
2018-08-06 11:11:30 -07:00
2018-06-21 17:19:14 -07:00
return
}
var vote gov.Vote
cdc.MustUnmarshalBinary(res, &vote)
2018-08-06 11:11:30 -07:00
2018-07-10 17:59:07 -07:00
output, err := wire.MarshalJSONIndent(cdc, vote)
2018-06-21 17:19:14 -07:00
if err != nil {
w.WriteHeader(http.StatusBadRequest)
w.Write([]byte(err.Error()))
2018-08-06 11:11:30 -07:00
2018-06-21 17:19:14 -07:00
return
}
2018-08-04 22:56:48 -07:00
if vote.Empty() {
res, err := cliCtx.QueryWithData("custom/gov/proposal", cdc.MustMarshalBinary(gov.QueryProposalParams{params.ProposalID}))
if err != nil || len(res) == 0 {
w.WriteHeader(http.StatusNotFound)
err := errors.Errorf("proposalID [%d] does not exist", proposalID)
w.Write([]byte(err.Error()))
return
}
w.WriteHeader(http.StatusNotFound)
err = errors.Errorf("voter [%s] did not deposit on proposalID [%d]", bechVoterAddr, proposalID)
w.Write([]byte(err.Error()))
return
}
2018-06-21 17:19:14 -07:00
w.Write(output)
}
}
// nolint: gocyclo
// todo: Split this functionality into helper functions to remove the above
func queryVotesOnProposalHandlerFn(cdc *wire.Codec) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
strProposalID := vars[RestProposalID]
if len(strProposalID) == 0 {
w.WriteHeader(http.StatusBadRequest)
err := errors.New("proposalId required but not specified")
w.Write([]byte(err.Error()))
2018-08-06 11:11:30 -07:00
return
}
proposalID, err := strconv.ParseInt(strProposalID, 10, 64)
if err != nil {
w.WriteHeader(http.StatusBadRequest)
err := errors.Errorf("proposalID [%s] is not positive", proposalID)
w.Write([]byte(err.Error()))
2018-08-06 11:11:30 -07:00
return
}
2018-08-06 11:11:30 -07:00
cliCtx := context.NewCLIContext().WithCodec(cdc)
2018-08-04 22:56:48 -07:00
params := gov.QueryVotesParams{
ProposalID: proposalID,
}
2018-08-04 22:56:48 -07:00
res, err := cliCtx.QueryWithData("custom/gov/votes", cdc.MustMarshalBinary(params))
if err != nil {
2018-08-04 22:56:48 -07:00
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte(err.Error()))
return
}
var votes []gov.Vote
2018-08-04 22:56:48 -07:00
cdc.MustUnmarshalBinary(res, &votes)
output, err := wire.MarshalJSONIndent(cdc, votes)
if err != nil {
w.WriteHeader(http.StatusBadRequest)
w.Write([]byte(err.Error()))
2018-08-06 11:11:30 -07:00
return
}
2018-08-06 11:11:30 -07:00
w.Write(output)
}
}
2018-07-09 16:16:43 -07:00
// nolint: gocyclo
// todo: Split this functionality into helper functions to remove the above
func queryProposalsWithParameterFn(cdc *wire.Codec) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
bechVoterAddr := r.URL.Query().Get(RestVoter)
bechDepositerAddr := r.URL.Query().Get(RestDepositer)
strProposalStatus := r.URL.Query().Get(RestProposalStatus)
2018-08-04 22:56:48 -07:00
strNumLatest := r.URL.Query().Get(RestNumLatest)
var err error
2018-07-06 00:06:53 -07:00
var voterAddr sdk.AccAddress
var depositerAddr sdk.AccAddress
var proposalStatus gov.ProposalStatus
2018-08-04 22:56:48 -07:00
var numLatest int64
if len(bechVoterAddr) != 0 {
2018-07-09 16:06:05 -07:00
voterAddr, err = sdk.AccAddressFromBech32(bechVoterAddr)
if err != nil {
w.WriteHeader(http.StatusBadRequest)
err := errors.Errorf("'%s' needs to be bech32 encoded", RestVoter)
w.Write([]byte(err.Error()))
return
}
}
if len(bechDepositerAddr) != 0 {
2018-07-09 16:06:05 -07:00
depositerAddr, err = sdk.AccAddressFromBech32(bechDepositerAddr)
if err != nil {
w.WriteHeader(http.StatusBadRequest)
err := errors.Errorf("'%s' needs to be bech32 encoded", RestDepositer)
w.Write([]byte(err.Error()))
2018-08-06 11:11:30 -07:00
return
}
}
if len(strProposalStatus) != 0 {
proposalStatus, err = gov.ProposalStatusFromString(strProposalStatus)
if err != nil {
w.WriteHeader(http.StatusBadRequest)
err := errors.Errorf("'%s' is not a valid Proposal Status", strProposalStatus)
w.Write([]byte(err.Error()))
2018-08-06 11:11:30 -07:00
return
}
}
2018-08-04 22:56:48 -07:00
if len(strNumLatest) != 0 {
numLatest, err = strconv.ParseInt(strNumLatest, 10, 64)
if err != nil {
w.WriteHeader(http.StatusBadRequest)
err := errors.Errorf("'%s' is not a valid int64", strNumLatest)
w.Write([]byte(err.Error()))
return
}
}
2018-08-06 11:11:30 -07:00
cliCtx := context.NewCLIContext().WithCodec(cdc)
2018-08-04 22:56:48 -07:00
params := gov.QueryProposalsParams{
Depositer: depositerAddr,
Voter: voterAddr,
ProposalStatus: proposalStatus,
NumLatestProposals: numLatest,
}
res, err := cliCtx.QueryWithData("custom/gov/proposals", cdc.MustMarshalBinary(params))
if err != nil {
2018-08-04 22:56:48 -07:00
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte(err.Error()))
2018-08-06 11:11:30 -07:00
return
}
2018-08-06 11:11:30 -07:00
2018-08-04 22:56:48 -07:00
var matchingProposals []gov.Proposal
cdc.MustUnmarshalBinary(res, &matchingProposals)
output, err := wire.MarshalJSONIndent(cdc, matchingProposals)
if err != nil {
w.WriteHeader(http.StatusBadRequest)
w.Write([]byte(err.Error()))
2018-08-06 11:11:30 -07:00
return
}
2018-08-06 11:11:30 -07:00
w.Write(output)
}
}