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

Haoyang/lcd implementation #66

Merged
merged 5 commits into from
Aug 22, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
2 changes: 1 addition & 1 deletion client/context/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ func (ctx CLIContext) WithCert(cert tendermintLite.Certifier) CLIContext {
return ctx
}

// WithCert - return a copy of the context with an updated ClientMgr
// WithClientMgr - return a copy of the context with an updated ClientMgr
func (ctx CLIContext) WithClientMgr(clientMgr *ClientManager) CLIContext {
ctx.ClientMgr = clientMgr
return ctx
Expand Down
3 changes: 2 additions & 1 deletion client/context/loadbalancing.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,15 @@ import (
"github.com/pkg/errors"
)

// This is a manager of a set of rpc clients to full nodes.
// ClientManager is a manager of a set of rpc clients to full nodes.
// This manager can do load balancing upon these rpc clients.
type ClientManager struct {
clients []rpcclient.Client
currentIndex int
mutex sync.RWMutex
}

// NewClientManager create a new ClientManager
func NewClientManager(nodeURIs string) (*ClientManager,error) {
if nodeURIs != "" {
nodeURLArray := strings.Split(nodeURIs, ",")
Expand Down
7 changes: 6 additions & 1 deletion client/context/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -284,6 +284,7 @@ func (ctx CLIContext) ensureBroadcastTx(txBytes []byte) error {
return nil
}

// nolint: gocyclo
// query performs a query from a Tendermint node with the provided store name
// and path.
func (ctx CLIContext) query(path string, key common.HexBytes) (res []byte, err error) {
Expand Down Expand Up @@ -312,8 +313,12 @@ func (ctx CLIContext) query(path string, key common.HexBytes) (res []byte, err e
return resp.Value,nil
}

// TODO: Later we consider to return error for missing valid certifier to verify data from untrusted node
if ctx.Cert == nil {
return resp.Value,errors.Errorf("missing valid certifier to verify data from untrusted node")
if ctx.Logger != nil {
io.WriteString(ctx.Logger, fmt.Sprintf("Missing valid certifier to verify data from untrusted node\n"))
}
return resp.Value, nil
}

// AppHash for height H is in header H+1
Expand Down
30 changes: 12 additions & 18 deletions client/httputils/httputils.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,33 +5,27 @@ import (
"net/http"
)

// Create error http response
// NewError create error http response
func NewError(ctx *gin.Context, errCode int, err error) {
errorResponse := httpError{
errorResponse := HTTPError{
API: "2.0",
Code: errCode,
ErrMsg: err.Error(),
}
ctx.JSON(errCode, errorResponse)
}

// Create normal http response
func NormalResponse(ctx *gin.Context, data interface{}) {
response := httpResponse{
API: "2.0",
Code: 0,
Result: data,
if err != nil {
errorResponse.ErrMsg = err.Error()
}
ctx.JSON(http.StatusOK, response)

ctx.JSON(errCode, errorResponse)
}

type httpResponse struct {
API string `json:"rest api" example:"2.0"`
Code int `json:"code" example:"0"`
Result interface{} `json:"result"`
// NormalResponse create normal http response
func NormalResponse(ctx *gin.Context, data []byte) {
ctx.Status(http.StatusOK)
ctx.Writer.Write(data)
}

type httpError struct {
// HTTPError is http response with error
type HTTPError struct {
API string `json:"rest api" example:"2.0"`
Code int `json:"code" example:"500"`
ErrMsg string `json:"error message"`
Expand Down
38 changes: 23 additions & 15 deletions client/keys/add.go
Original file line number Diff line number Diff line change
Expand Up @@ -239,37 +239,39 @@ func AddNewKeyRequestHandler(w http.ResponseWriter, r *http.Request) {
w.Write(bz)
}

// Handler of adding new key in swagger rest server
// AddNewKeyRequest is the handler of adding new key in swagger rest server
// nolint: gocyclo
func AddNewKeyRequest(gtx *gin.Context) {
var kb keys.Keybase
var m NewKeyBody

kb, err := GetKeyBase()
body, err := ioutil.ReadAll(gtx.Request.Body)
if err != nil {
httputils.NewError(gtx, http.StatusInternalServerError, err)
return
}

if err := gtx.BindJSON(&m); err != nil {
httputils.NewError(gtx, http.StatusBadRequest, err)
return
}
err = json.Unmarshal(body, &m)
if err != nil {
httputils.NewError(gtx, http.StatusBadRequest, err)
return
}

if len(m.Name) < 1 || len(m.Name) > 16 {
httputils.NewError(gtx, http.StatusBadRequest, fmt.Errorf("account name length should not be longer than 16"))
return
}
for _, char := range []rune(m.Name) {
if !syntax.IsWordChar(char) {
httputils.NewError(gtx, http.StatusBadRequest, fmt.Errorf("account name should not contains any char beyond [0-9A-Za-z]"))
httputils.NewError(gtx, http.StatusBadRequest, fmt.Errorf("account name should not contains any char beyond [_0-9A-Za-z]"))
return
}
}
if len(m.Password) < 8 || len(m.Password) > 16 {
httputils.NewError(gtx, http.StatusBadRequest, fmt.Errorf("account password length should be between 8 and 16"))
httputils.NewError(gtx, http.StatusBadRequest, fmt.Errorf("account password length should be no less than 8 and no greater than 16"))
return
}

kb, err := GetKeyBase()
if err != nil {
httputils.NewError(gtx, http.StatusInternalServerError, err)
return
}

Expand All @@ -282,7 +284,7 @@ func AddNewKeyRequest(gtx *gin.Context) {

for _, i := range infos {
if i.GetName() == m.Name {
httputils.NewError(gtx, http.StatusConflict, fmt.Errorf("Account with name %s already exists.", m.Name))
httputils.NewError(gtx, http.StatusConflict, fmt.Errorf("account with name %s already exists", m.Name))
return
}
}
Expand All @@ -306,7 +308,13 @@ func AddNewKeyRequest(gtx *gin.Context) {

keyOutput.Seed = seed

httputils.NormalResponse(gtx, keyOutput)
bz, err := json.Marshal(keyOutput)
if err != nil {
httputils.NewError(gtx, http.StatusInternalServerError, err)
return
}

httputils.NormalResponse(gtx, bz)

}

Expand All @@ -333,12 +341,12 @@ func SeedRequestHandler(w http.ResponseWriter, r *http.Request) {
w.Write([]byte(seed))
}

// Handler of creating seed in swagger rest server
// SeedRequest is the handler of creating seed in swagger rest server
func SeedRequest(gtx *gin.Context) {

algo := keys.SigningAlgo("secp256k1")

seed := getSeed(algo)

httputils.NormalResponse(gtx, seed)
httputils.NormalResponse(gtx, []byte(seed))
}
4 changes: 2 additions & 2 deletions client/keys/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ func DeleteKeyRequestHandler(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(200)
}

// Handler of deleting specified key in swagger rest server
// DeleteKeyRequest is the handler of deleting specified key in swagger rest server
func DeleteKeyRequest(gtx *gin.Context) {
name := gtx.Param("name")
var kb keys.Keybase
Expand All @@ -117,5 +117,5 @@ func DeleteKeyRequest(gtx *gin.Context) {
return
}

httputils.NormalResponse(gtx, "success")
httputils.NormalResponse(gtx, []byte("success"))
}
9 changes: 7 additions & 2 deletions client/keys/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ func QueryKeysRequestHandler(w http.ResponseWriter, r *http.Request) {
w.Write(output)
}

// Handler of listing all keys in swagger rest server
// DeleteKeyRequest is the handler of listing all keys in swagger rest server
func QueryKeysRequest(gtx *gin.Context) {
kb, err := GetKeyBase()
if err != nil {
Expand All @@ -92,5 +92,10 @@ func QueryKeysRequest(gtx *gin.Context) {
httputils.NewError(gtx, http.StatusInternalServerError, err)
return
}
httputils.NormalResponse(gtx, keysOutput)
output, err := json.MarshalIndent(keysOutput, "", " ")
if err != nil {
httputils.NewError(gtx, http.StatusInternalServerError, err)
return
}
httputils.NormalResponse(gtx, output)
}
2 changes: 1 addition & 1 deletion client/keys/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ func RegisterRoutes(r *mux.Router) {
r.HandleFunc("/keys/{name}", DeleteKeyRequestHandler).Methods("DELETE")
}

// resgister swagger REST routes
// RegisterSwaggerRoutes - Central function to define key management related routes that get registered by the main application
func RegisterSwaggerRoutes(routerGroup *gin.RouterGroup) {
routerGroup.GET("/keys", QueryKeysRequest)
routerGroup.POST("/keys", AddNewKeyRequest)
Expand Down
9 changes: 7 additions & 2 deletions client/keys/show.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ func GetKeyRequestHandler(w http.ResponseWriter, r *http.Request) {
w.Write(output)
}

// Handler of getting specified key in swagger rest server
// GetKeyRequest is the handler of getting specified key in swagger rest server
func GetKeyRequest(gtx *gin.Context) {
name := gtx.Param("name")

Expand All @@ -84,5 +84,10 @@ func GetKeyRequest(gtx *gin.Context) {
httputils.NewError(gtx, http.StatusInternalServerError, err)
return
}
httputils.NormalResponse(gtx, keyOutput)
output, err := json.MarshalIndent(keyOutput, "", " ")
if err != nil {
httputils.NewError(gtx, http.StatusInternalServerError, err)
return
}
httputils.NormalResponse(gtx, output)
}
4 changes: 2 additions & 2 deletions client/keys/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ func UpdateKeyRequestHandler(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(200)
}

// Handler of updating specified key in swagger rest server
// UpdateKeyRequest is the handler of updating specified key in swagger rest server
func UpdateKeyRequest(gtx *gin.Context) {
name := gtx.Param("name")
var kb keys.Keybase
Expand Down Expand Up @@ -128,5 +128,5 @@ func UpdateKeyRequest(gtx *gin.Context) {
return
}

httputils.NormalResponse(gtx, "success")
httputils.NormalResponse(gtx, []byte("success"))
}
Loading