Skip to content

Commit

Permalink
Merge pull request #281 from irisnet/feature/lcd
Browse files Browse the repository at this point in the history
Push code from lcd feature branch to develop branch
  • Loading branch information
wukongcheng authored Sep 21, 2018
2 parents d207fea + e8248c8 commit d502654
Show file tree
Hide file tree
Showing 19 changed files with 4,379 additions and 85 deletions.
5 changes: 2 additions & 3 deletions client/bank/lcd/rest.go
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
package lcd

import (
"github.com/cosmos/cosmos-sdk/crypto/keys"
"github.com/cosmos/cosmos-sdk/wire"
authcmd "github.com/cosmos/cosmos-sdk/x/auth/client/cli"
"github.com/gorilla/mux"
"github.com/irisnet/irishub/client/context"
)

// RegisterRoutes - Central function to define routes that get registered by the main application
func RegisterRoutes(cliCtx context.CLIContext, r *mux.Router, cdc *wire.Codec, kb keys.Keybase) {
r.HandleFunc("/bank/{address}/send", SendRequestHandlerFn(cdc, kb, cliCtx)).Methods("POST")
func RegisterRoutes(cliCtx context.CLIContext, r *mux.Router, cdc *wire.Codec) {
r.HandleFunc("/bank/{address}/send", SendRequestHandlerFn(cdc, cliCtx)).Methods("POST")
r.HandleFunc("/bank/accounts/{address}",
QueryAccountRequestHandlerFn("acc", cdc, authcmd.GetAccountDecoder(cdc), cliCtx)).Methods("GET")
r.HandleFunc("/bank/coin/{coin-type}",
Expand Down
7 changes: 3 additions & 4 deletions client/bank/lcd/sendtx.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package lcd

import (
"github.com/cosmos/cosmos-sdk/crypto/keys"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/wire"
"github.com/gorilla/mux"
Expand All @@ -12,14 +11,14 @@ import (
)

type sendBody struct {
Amount sdk.Coins `json:"amount"`
Amount string `json:"amount"`
Sender string `json:"sender"`
BaseTx context.BaseTx `json:"base_tx"`
}

// SendRequestHandlerFn - http request handler to send coins to a address
// nolint: gocyclo
func SendRequestHandlerFn(cdc *wire.Codec, kb keys.Keybase, cliCtx context.CLIContext) http.HandlerFunc {
func SendRequestHandlerFn(cdc *wire.Codec, cliCtx context.CLIContext) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
// collect data
vars := mux.Vars(r)
Expand All @@ -46,7 +45,7 @@ func SendRequestHandlerFn(cdc *wire.Codec, kb keys.Keybase, cliCtx context.CLICo
return
}

amount, err := cliCtx.ParseCoins(m.Amount.String())
amount, err := cliCtx.ParseCoins(m.Amount)
if err != nil {
utils.WriteErrorResponse(w, http.StatusBadRequest, err.Error())
return
Expand Down
5 changes: 3 additions & 2 deletions client/context/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,13 +94,14 @@ func createCertifier() tmlite.Certifier {
errMsg.WriteString("--node ")
}
if errMsg.Len() != 0 {
fmt.Printf("must specify these options: %s when --trust-node is false\n", errMsg.String())
fmt.Printf("Must specify these options: %s when --trust-node is false\n", errMsg.String())
os.Exit(1)
}

certifier, err := tmliteProxy.GetCertifier(chainID, home, nodeURI)
if err != nil {
panic(err)
fmt.Printf("Abort!! IRISLCD encountered fatal error in creating certifier: %s", err.Error())
os.Exit(1)
}

return certifier
Expand Down
6 changes: 3 additions & 3 deletions client/gov/cli/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ func GetCmdQueryProposal(storeName string, cdc *wire.Codec) *cobra.Command {
var proposal gov.Proposal
cdc.MustUnmarshalBinary(res, &proposal)

proposalResponse, err := govClient.ConvertProposalCoins(cliCtx, proposal)
proposalResponse, err := govClient.ConvertProposalToProposalOutput(cliCtx, proposal)
if err != nil {
return err
}
Expand Down Expand Up @@ -98,7 +98,7 @@ func GetCmdQueryProposals(storeName string, cdc *wire.Codec) *cobra.Command {
var maxProposalID int64
cdc.MustUnmarshalBinary(res, &maxProposalID)

matchingProposals := []govClient.TextProposalResponse{}
matchingProposals := []govClient.ProposalOutput{}

if latestProposalsIDs == 0 {
latestProposalsIDs = maxProposalID
Expand Down Expand Up @@ -133,7 +133,7 @@ func GetCmdQueryProposals(storeName string, cdc *wire.Codec) *cobra.Command {
}
}

proposalResponse, err := govClient.ConvertProposalCoins(cliCtx, proposal)
proposalResponse, err := govClient.ConvertProposalToProposalOutput(cliCtx, proposal)
if err != nil {
return err
}
Expand Down
16 changes: 8 additions & 8 deletions client/gov/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@ import (
)

// Deposit
type DepositResponse struct {
type DepositOutput struct {
Depositer sdk.AccAddress `json:"depositer"` // Address of the depositer
ProposalID int64 `json:"proposal_id"` // proposalID of the proposal
Amount []string `json:"amount"` // Deposit amount
}

type TextProposalResponse struct {
type ProposalOutput struct {
ProposalID int64 `json:"proposal_id"` // ID of the proposal
Title string `json:"title"` // Title of the proposal
Description string `json:"description"` // Description of the proposal
Expand All @@ -33,12 +33,12 @@ type KvPair struct {
V string `json:"value"`
}

func ConvertProposalCoins(cliCtx context.CLIContext, proposal gov.Proposal) (TextProposalResponse, error) {
func ConvertProposalToProposalOutput(cliCtx context.CLIContext, proposal gov.Proposal) (ProposalOutput, error) {
totalDeposit, err := cliCtx.ConvertCoinToMainUnit(proposal.GetTotalDeposit().String())
if err != nil {
return TextProposalResponse{}, err
return ProposalOutput{}, err
}
return TextProposalResponse{
return ProposalOutput{
ProposalID: proposal.GetProposalID(),
Title: proposal.GetTitle(),
Description: proposal.GetDescription(),
Expand All @@ -54,12 +54,12 @@ func ConvertProposalCoins(cliCtx context.CLIContext, proposal gov.Proposal) (Tex
}, nil
}

func ConvertDepositeCoins(cliCtx context.CLIContext, deposite gov.Deposit) (DepositResponse, error) {
func ConvertDepositToDepositOutput(cliCtx context.CLIContext, deposite gov.Deposit) (DepositOutput, error) {
amount, err := cliCtx.ConvertCoinToMainUnit(deposite.Amount.String())
if err != nil {
return DepositResponse{}, err
return DepositOutput{}, err
}
return DepositResponse{
return DepositOutput{
ProposalID: deposite.ProposalID,
Depositer: deposite.Depositer,
Amount: amount,
Expand Down
8 changes: 4 additions & 4 deletions client/gov/lcd/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ func queryProposalHandlerFn(cdc *wire.Codec, cliCtx context.CLIContext) http.Han

var proposal gov.Proposal
cdc.MustUnmarshalBinary(res, &proposal)
proposalResponse, err := govClient.ConvertProposalCoins(cliCtx, proposal)
proposalResponse, err := govClient.ConvertProposalToProposalOutput(cliCtx, proposal)
if err != nil {
utils.WriteErrorResponse(w, http.StatusInternalServerError, err.Error())
return
Expand Down Expand Up @@ -96,7 +96,7 @@ func queryDepositHandlerFn(cdc *wire.Codec, cliCtx context.CLIContext) http.Hand
var deposit gov.Deposit
cdc.MustUnmarshalBinary(res, &deposit)

depositeResponse, err := govClient.ConvertDepositeCoins(cliCtx, deposit)
depositeResponse, err := govClient.ConvertDepositToDepositOutput(cliCtx, deposit)
if err != nil {
utils.WriteErrorResponse(w, http.StatusInternalServerError, err.Error())
return
Expand Down Expand Up @@ -266,7 +266,7 @@ func queryProposalsWithParameterFn(cdc *wire.Codec, cliCtx context.CLIContext) h
var maxProposalID int64
cdc.MustUnmarshalBinary(res, &maxProposalID)

matchingProposals := []govClient.TextProposalResponse{}
matchingProposals := []govClient.ProposalOutput{}

for proposalID := int64(0); proposalID < maxProposalID; proposalID++ {
if voterAddr != nil {
Expand Down Expand Up @@ -296,7 +296,7 @@ func queryProposalsWithParameterFn(cdc *wire.Codec, cliCtx context.CLIContext) h
continue
}
}
proposalResponse, err := govClient.ConvertProposalCoins(cliCtx, proposal)
proposalResponse, err := govClient.ConvertProposalToProposalOutput(cliCtx, proposal)
if err != nil {
utils.WriteErrorResponse(w, http.StatusInternalServerError, err.Error())
return
Expand Down
8 changes: 4 additions & 4 deletions client/gov/lcd/sendtx.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,13 @@ type postProposalReq struct {
Description string `json:"description"` // Description of the proposal
ProposalType gov.ProposalKind `json:"proposal_type"` // Type of proposal. Initial set {PlainTextProposal, SoftwareUpgradeProposal}
Proposer string `json:"proposer"` // Address of the proposer
InitialDeposit sdk.Coins `json:"initial_deposit"` // Coins to add to the proposal's deposit
InitialDeposit string `json:"initial_deposit"` // Coins to add to the proposal's deposit
}

type depositReq struct {
BaseTx context.BaseTx `json:"base_tx"`
Depositer string `json:"depositer"` // Address of the depositer
Amount sdk.Coins `json:"amount"` // Coins to add to the proposal's deposit
Amount string `json:"amount"` // Coins to add to the proposal's deposit
}

type voteReq struct {
Expand Down Expand Up @@ -55,7 +55,7 @@ func postProposalHandlerFn(cdc *wire.Codec, cliCtx context.CLIContext) http.Hand
return
}

initDepositAmount, err := cliCtx.ParseCoins(req.InitialDeposit.String())
initDepositAmount, err := cliCtx.ParseCoins(req.InitialDeposit)
if err != nil {
utils.WriteErrorResponse(w, http.StatusBadRequest, err.Error())
return
Expand Down Expand Up @@ -104,7 +104,7 @@ func depositHandlerFn(cdc *wire.Codec, cliCtx context.CLIContext) http.HandlerFu
utils.WriteErrorResponse(w, http.StatusBadRequest, err.Error())
return
}
depositAmount, err := cliCtx.ParseCoins(req.Amount.String())
depositAmount, err := cliCtx.ParseCoins(req.Amount)
if err != nil {
utils.WriteErrorResponse(w, http.StatusBadRequest, err.Error())
return
Expand Down
21 changes: 9 additions & 12 deletions client/lcd/lcd.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,9 @@ import (
bankhandler "github.com/irisnet/irishub/client/bank/lcd"
"github.com/irisnet/irishub/client/context"
govhandler "github.com/irisnet/irishub/client/gov/lcd"
"github.com/irisnet/irishub/client/keys"
keyshandler "github.com/irisnet/irishub/client/keys/lcd"
//slashinghandler "github.com/irisnet/irishub/client/slashing/lcd"
//stakehandler "github.com/irisnet/irishub/client/stake/lcd"
slashinghandler "github.com/irisnet/irishub/client/slashing/lcd"
stakehandler "github.com/irisnet/irishub/client/stake/lcd"
rpchandler "github.com/irisnet/irishub/client/tendermint/rpc"
txhandler "github.com/irisnet/irishub/client/tendermint/tx"
"github.com/rakyll/statik/fs"
Expand All @@ -32,7 +31,7 @@ func ServeLCDStartCommand(cdc *wire.Codec) *cobra.Command {

cmd := &cobra.Command{
Use: "start",
Short: "Start irislcd (irishub light-client daemon), a local REST server with swagger-ui: http://localhost:1317/swagger-ui/",
Short: "Start IRISLCD (IRISHUB light-client daemon), a local REST server with swagger-ui: http://localhost:1317/swagger-ui/",
RunE: func(cmd *cobra.Command, args []string) error {
listenAddr := viper.GetString(flagListenAddr)
router := createHandler(cdc)
Expand All @@ -55,7 +54,7 @@ func ServeLCDStartCommand(cdc *wire.Codec) *cobra.Command {
return err
}

logger.Info("irislcd server started")
logger.Info("IRISLCD server started")

// wait forever and cleanup
cmn.TrapSignal(func() {
Expand All @@ -79,20 +78,18 @@ func ServeLCDStartCommand(cdc *wire.Codec) *cobra.Command {

func createHandler(cdc *wire.Codec) *mux.Router {
r := mux.NewRouter()
kb, err := keys.GetKeyBase()
if err != nil {
panic(err)
}

cliCtx := context.NewCLIContext().WithCodec(cdc).WithLogger(os.Stdout)

r.HandleFunc("/version", CLIVersionRequestHandler).Methods("GET")
r.HandleFunc("/node_version", NodeVersionRequestHandler(cliCtx)).Methods("GET")

keyshandler.RegisterRoutes(r)
bankhandler.RegisterRoutes(cliCtx, r, cdc, kb)
bankhandler.RegisterRoutes(cliCtx, r, cdc)
slashinghandler.RegisterRoutes(cliCtx, r, cdc)
stakehandler.RegisterRoutes(cliCtx, r, cdc)
govhandler.RegisterRoutes(cliCtx, r, cdc)
//slashinghandler.RegisterRoutes(cliCtx, r, cdc, kb)
//stakehandler.RegisterRoutes(cliCtx, r, cdc, kb)
// tendermint apis
rpchandler.RegisterRoutes(cliCtx, r, cdc)
txhandler.RegisterRoutes(cliCtx, r, cdc)
return r
Expand Down
Loading

0 comments on commit d502654

Please sign in to comment.