Skip to content

Commit

Permalink
Merge pull request #77 from HaoyangLiu/haoyang/lcd-implementation-new
Browse files Browse the repository at this point in the history
Fix errors in test_cover and test_lint
  • Loading branch information
wukongcheng authored Aug 24, 2018
2 parents 79ec713 + 9409e73 commit 9ff57af
Show file tree
Hide file tree
Showing 5 changed files with 106 additions and 125 deletions.
3 changes: 3 additions & 0 deletions client/context/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -301,6 +301,9 @@ func (ctx CLIContext) proofVerify(path string, resp abci.ResponseQuery) error {
}

node, err := ctx.GetNode()
if err != nil {
return err
}
// AppHash for height H is in header H+1
commit, err := tendermintLiteProxy.GetCertifiedCommit(resp.Height+1, node, ctx.Certifier)
if err != nil {
Expand Down
55 changes: 23 additions & 32 deletions client/keys/add.go
Original file line number Diff line number Diff line change
Expand Up @@ -188,22 +188,13 @@ func AddNewKeyRequestHandler(w http.ResponseWriter, r *http.Request) {
return
}

if paramCheck(m) != nil {
w.WriteHeader(http.StatusBadRequest)
errCode, err := paramCheck(kb, m)
if err != nil {
w.WriteHeader(errCode)
w.Write([]byte(err.Error()))
return
}

// check if already exists
infos, err := kb.List()
for _, i := range infos {
if i.GetName() == m.Name {
w.WriteHeader(http.StatusConflict)
w.Write([]byte(fmt.Sprintf("Account with name %s already exists.", m.Name)))
return
}
}

// create account
seed := m.Seed
if seed == "" {
Expand Down Expand Up @@ -236,19 +227,32 @@ func AddNewKeyRequestHandler(w http.ResponseWriter, r *http.Request) {
}

// paramCheck performs add new key parameters checking
func paramCheck(m NewKeyBody) error {
func paramCheck(kb keys.Keybase, m NewKeyBody) (int, error) {
if len(m.Name) < 1 || len(m.Name) > 16 {
return fmt.Errorf("account name length should not be longer than 16")
return http.StatusBadRequest, fmt.Errorf("account name length should not be longer than 16")
}
for _, char := range []rune(m.Name) {
if !syntax.IsWordChar(char) {
return fmt.Errorf("account name should not contains any char beyond [_0-9A-Za-z]")
return http.StatusBadRequest, fmt.Errorf("account name should not contains any char beyond [_0-9A-Za-z]")
}
}
if len(m.Password) < 8 || len(m.Password) > 16 {
return fmt.Errorf("account password length should be no less than 8 and no greater than 16")
return http.StatusBadRequest, fmt.Errorf("account password length should be no less than 8 and no greater than 16")
}
return nil

// check if already exists
infos, err := kb.List()
if err != nil {
return http.StatusInternalServerError, err
}

for _, i := range infos {
if i.GetName() == m.Name {
return http.StatusConflict, fmt.Errorf("account with name %s already exists", m.Name)
}
}

return 0, nil
}

// AddNewKeyRequest is the handler of adding new key in swagger rest server
Expand All @@ -265,28 +269,15 @@ func AddNewKeyRequest(gtx *gin.Context) {
return
}

if paramCheck(m) != nil {
httputils.NewError(gtx, http.StatusBadRequest, err)
}

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

// check if already exists
infos, err := kb.List()
errCode, err := paramCheck(kb, m)
if err != nil {
httputils.NewError(gtx, http.StatusInternalServerError, err)
return
}

for _, i := range infos {
if i.GetName() == m.Name {
httputils.NewError(gtx, http.StatusConflict, fmt.Errorf("account with name %s already exists", m.Name))
return
}
httputils.NewError(gtx, errCode, err)
}

// create account
Expand Down
14 changes: 8 additions & 6 deletions client/lcd/lcd_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -340,7 +340,7 @@ func TestCoinSend(t *testing.T) {
someFakeAddr := sdk.AccAddress(bz)

// query empty
res, body := Request(t, port, "GET", fmt.Sprintf("/accounts/%s", someFakeAddr), nil)
res, body := Request(t, port, "GET", fmt.Sprintf("/bank/balance/%s", someFakeAddr), nil)
require.Equal(t, http.StatusNoContent, res.StatusCode, body)

acc := getAccount(t, port, addr)
Expand Down Expand Up @@ -382,7 +382,7 @@ func TestCoinSendSwaggerLCD(t *testing.T) {
someFakeAddr := sdk.AccAddress(bz)

// query empty
res, body := Request(t, port, "GET", fmt.Sprintf("/accounts/%s", someFakeAddr), nil)
res, body := Request(t, port, "GET", fmt.Sprintf("/bank/balance/%s", someFakeAddr), nil)
require.Equal(t, http.StatusNoContent, res.StatusCode, body)

acc := getAccount(t, port, addr)
Expand Down Expand Up @@ -934,7 +934,7 @@ func TestProposalsQuery(t *testing.T) {
//_____________________________________________________________________________
// get the account to get the sequence
func getAccount(t *testing.T, port string, addr sdk.AccAddress) auth.Account {
res, body := Request(t, port, "GET", fmt.Sprintf("/accounts/%s", addr), nil)
res, body := Request(t, port, "GET", fmt.Sprintf("/bank/balance/%s", addr), nil)
require.Equal(t, http.StatusOK, res.StatusCode, body)
var acc auth.Account
err := cdc.UnmarshalJSON([]byte(body), &acc)
Expand Down Expand Up @@ -967,9 +967,11 @@ func doSend(t *testing.T, port, seed, name, password string, addr sdk.AccAddress
"sequence":"%d",
"gas": "10000",
"amount":[%s],
"chain_id":"%s"
}`, name, password, accnum, sequence, coinbz, chainID))
res, body := Request(t, port, "POST", fmt.Sprintf("/accounts/%s/send", receiveAddr), jsonStr)
"chain_id":"%s",
"signed": true,
"to_address":"%s"
}`, name, password, accnum, sequence, coinbz, chainID, receiveAddr))
res, body := Request(t, port, "POST", "/bank/transfers", jsonStr)
require.Equal(t, http.StatusOK, res.StatusCode, body)

err = cdc.UnmarshalJSON([]byte(body), &resultTx)
Expand Down
2 changes: 1 addition & 1 deletion x/auth/client/rest/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import (
// register REST routes
func RegisterRoutes(cliCtx context.CLIContext, r *mux.Router, cdc *wire.Codec, storeName string) {
r.HandleFunc(
"/accounts/{address}",
"/bank/balance/{address}",
QueryAccountRequestHandlerFn(storeName, cdc, authcmd.GetAccountDecoder(cdc), cliCtx),
).Methods("GET")
}
Expand Down
Loading

0 comments on commit 9ff57af

Please sign in to comment.