From 0200c9ab6babd739dd3476f71e6ddb6f482f58df Mon Sep 17 00:00:00 2001 From: Hleb Albau Date: Mon, 22 Oct 2018 22:34:16 +0300 Subject: [PATCH] #57 [RPC]Fix small finding --- cosmos/poc/app/app.go | 15 ------------ cosmos/poc/app/rpc.go | 37 +++++++++++++++++++++++++++++ cosmos/poc/cyberdproxy/proxy/get.go | 17 +++++++++---- 3 files changed, 50 insertions(+), 19 deletions(-) create mode 100644 cosmos/poc/app/rpc.go diff --git a/cosmos/poc/app/app.go b/cosmos/poc/app/app.go index aaf80967..70dc1f22 100644 --- a/cosmos/poc/app/app.go +++ b/cosmos/poc/app/app.go @@ -174,18 +174,3 @@ func (app *CyberdApp) appHash() []byte { } return app.latestRankHash } - -func (app *CyberdApp) Search(cid string, page, perPage int) ([]RankedCid, int, error) { - if perPage == 0 { - perPage = 100 - } - result, totalSize, err := app.memStorage.GetCidRankedLinks(Cid(cid), page, perPage) - if err != nil { - return nil, totalSize, err - } - return result, totalSize, nil -} - -func (app *CyberdApp) Account(address sdk.AccAddress) auth.Account { - return app.accStorage.GetAccount(app.NewContext(true, abci.Header{}), address) -} diff --git a/cosmos/poc/app/rpc.go b/cosmos/poc/app/rpc.go new file mode 100644 index 00000000..9b9dfcec --- /dev/null +++ b/cosmos/poc/app/rpc.go @@ -0,0 +1,37 @@ +package app + +import ( + sdk "github.com/cosmos/cosmos-sdk/types" + "github.com/cosmos/cosmos-sdk/x/auth" + . "github.com/cybercongress/cyberd/cosmos/poc/app/storage" + abci "github.com/tendermint/tendermint/abci/types" +) + +func (app *CyberdApp) Search(cid string, page, perPage int) ([]RankedCid, int, error) { + if perPage == 0 { + perPage = 100 + } + result, totalSize, err := app.memStorage.GetCidRankedLinks(Cid(cid), page, perPage) + if err != nil { + return nil, totalSize, err + } + return result, totalSize, nil +} + +func (app *CyberdApp) Account(address sdk.AccAddress) auth.Account { + + acc := app.accStorage.GetAccount(app.NewContext(true, abci.Header{}), address) + + if acc != nil { + return acc + } + + // no acc in chain, assume new one, so balance 0 and seq 0 + // acc number -1 is for addresses not in chain + return &auth.BaseAccount{ + Address: address, + Sequence: 0, + Coins: make(sdk.Coins, 0), + AccountNumber: -1, + } +} diff --git a/cosmos/poc/cyberdproxy/proxy/get.go b/cosmos/poc/cyberdproxy/proxy/get.go index 83d70668..0312c6f8 100644 --- a/cosmos/poc/cyberdproxy/proxy/get.go +++ b/cosmos/poc/cyberdproxy/proxy/get.go @@ -1,6 +1,9 @@ package proxy -import "net/http" +import ( + "errors" + "net/http" +) func GetHandlerFn(ctx ProxyContext, endpoint string) func(http.ResponseWriter, *http.Request) { @@ -24,12 +27,18 @@ func GetWithParamHandlerFn(ctx ProxyContext, endpoint string, param string) func w.Header().Set("Content-Type", "application/json") - paramValue, err := getSingleParamValue(param, r) - if err != nil { - HandleError(err, w) + addresses, ok := r.URL.Query()[param] + + if !ok || addresses == nil { + HandleError(errors.New("Cannot find param "+param), w) return } + paramValue := "" + if addresses != nil && len(addresses[0]) == 1 { + paramValue = addresses[0] + } + resp, err := ctx.Get(endpoint + "?" + param + "=\"" + paramValue + "\"") if err != nil { HandleError(err, w)