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

Extra APIs #563

Merged
merged 1 commit into from
Nov 15, 2020
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
41 changes: 41 additions & 0 deletions app/rpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,28 @@ func (app *CyberdApp) Search(cid string, page, perPage int) ([]RankedCid, int, e
return result, size, nil
}

func (app *CyberdApp) Backlinks(cid string, page, perPage int) ([]RankedCid, int, error) {

ctx := app.RpcContext()
cidNumber, exists := app.cidNumKeeper.GetCidNumber(ctx, link.Cid(cid))
if !exists || cidNumber > app.rankStateKeeper.GetLastCidNum() {
return nil, 0, errors.New("no such cid found")
}

rankedCidNumbers, size, err := app.rankStateKeeper.Backlinks(cidNumber, page, perPage)

if err != nil {
return nil, size, err
}

result := make([]RankedCid, 0, len(rankedCidNumbers))
for _, c := range rankedCidNumbers {
result = append(result, RankedCid{Cid: app.cidNumKeeper.GetCid(ctx, c.GetNumber()), Rank: c.GetRank()})
}

return result, size, nil
}

func (app *CyberdApp) Top(page, perPage int) ([]RankedCid, int, error) {

ctx := app.RpcContext()
Expand Down Expand Up @@ -88,6 +110,25 @@ func (app *CyberdApp) AccountBandwidth(address sdk.AccAddress) bw.AccountBandwid
return app.bandwidthMeter.GetCurrentAccBandwidth(app.RpcContext(), address)
}

func (app *CyberdApp) AccountLinks(address sdk.AccAddress, page, perPage int) ([]link.Link, int, error) {
ctx := app.RpcContext()

acc := app.accountKeeper.GetAccount(ctx, address)

if acc != nil {
accNumber := cbd.AccNumber(acc.GetAccountNumber())
links, total, _ := app.rankStateKeeper.Accounts(uint64(accNumber), page, perPage)

result := make([]link.Link, 0, len(links))
for j, c := range links {
result = append(result, link.Link{From: app.cidNumKeeper.GetCid(ctx, j), To: app.cidNumKeeper.GetCid(ctx, c)})
}
return result, total, nil
} else {
return nil, 0, nil
}
}

func (app *CyberdApp) IsLinkExist(from link.Cid, to link.Cid, address sdk.AccAddress) bool {

ctx := app.RpcContext()
Expand Down
32 changes: 32 additions & 0 deletions cmd/cyberd/rpc/account_links.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package rpc

import (
"github.com/cosmos/cosmos-sdk/types"

"github.com/cybercongress/go-cyber/x/link"

rpctypes "github.com/tendermint/tendermint/rpc/jsonrpc/types"
)

type ResultAccountLinks struct {
Links []link.Link `json:"links"`
TotalCount int `json:"total"`
Page int `json:"page"`
PerPage int `json:"perPage"`
}

func AccountLinks(ctx *rpctypes.Context, address string, page, perPage int) (*ResultAccountLinks, error) {
if perPage == 0 {
perPage = 100
}

accAddress, err := types.AccAddressFromBech32(address)
if err != nil {
return nil, err
}

links, totalSize, err := cyberdApp.AccountLinks(accAddress, page, perPage)
return &ResultAccountLinks{links, totalSize, page, perPage}, err
}


1 change: 0 additions & 1 deletion cmd/cyberd/rpc/account_test.go

This file was deleted.

32 changes: 2 additions & 30 deletions cmd/cyberd/rpc/link.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
package rpc

import (
"encoding/json"
"github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/x/auth"
"github.com/cybercongress/go-cyber/x/link"
rpctypes "github.com/tendermint/tendermint/rpc/jsonrpc/types"

"github.com/cybercongress/go-cyber/x/link"
)

func IsLinkExist(ctx *rpctypes.Context, from string, to string, address string) (bool, error) {
Expand All @@ -21,30 +20,3 @@ func IsLinkExist(ctx *rpctypes.Context, from string, to string, address string)

return cyberdApp.IsLinkExist(link.Cid(from), link.Cid(to), accAddress), nil
}

type LinkRequest struct {
Fee auth.StdFee `json:"fee"`
Msgs []link.Msg `json:"msgs"`
Signatures []Signature `json:"signatures"`
Memo string `json:"memo"`
}

func (r LinkRequest) GetFee() auth.StdFee { return r.Fee }
func (r LinkRequest) GetSignatures() []Signature { return r.Signatures }
func (r LinkRequest) GetMemo() string { return r.Memo }
func (r LinkRequest) GetMsgs() []types.Msg {
msgs := make([]types.Msg, 0, len(r.Msgs))
for _, msg := range r.Msgs {
msgs = append(msgs, msg)
}
return msgs
}

func UnmarshalLinkRequestFn(reqBytes []byte) (TxRequest, error) {
var req LinkRequest
err := json.Unmarshal(reqBytes, &req)
if err != nil {
return nil, err
}
return req, nil
}
4 changes: 3 additions & 1 deletion cmd/cyberd/rpc/rank.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package rpc

import (
rpctypes "github.com/tendermint/tendermint/rpc/jsonrpc/types"

"github.com/cybercongress/go-cyber/merkle"
)

Expand All @@ -9,7 +11,7 @@ type RankAndProofResult struct {
Rank float64 `amino:"unsafe" json:"rank"`
}

func Rank(cid string, proof bool) (*RankAndProofResult, error) {
func Rank(ctx *rpctypes.Context, cid string, proof bool) (*RankAndProofResult, error) {
rankValue, proofs, err := cyberdApp.Rank(cid, proof)
return &RankAndProofResult{proofs, rankValue}, err
}
10 changes: 2 additions & 8 deletions cmd/cyberd/rpc/routes.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,22 +15,16 @@ func SetCyberdApp(cApp *app.CyberdApp) {

var Routes = map[string]*rpcserver.RPCFunc{
"search": rpcserver.NewRPCFunc(Search, "cid,page,perPage"),
"backlinks": rpcserver.NewRPCFunc(Backlinks, "cid,page,perPage"),
"top": rpcserver.NewRPCFunc(Top, "page,perPage"),
"rank": rpcserver.NewRPCFunc(Rank, "cid,proof"),
"account": rpcserver.NewRPCFunc(Account, "address"),
"account_bandwidth": rpcserver.NewRPCFunc(AccountBandwidth, "address"),
"account_links": rpcserver.NewRPCFunc(AccountLinks, "address,page,perPage"),
"is_link_exist": rpcserver.NewRPCFunc(IsLinkExist, "from,to,address"),
"current_bandwidth_price": rpcserver.NewRPCFunc(CurrentBandwidthPrice, ""),
"current_network_load": rpcserver.NewRPCFunc(CurrentNetworkLoad, ""),
"index_stats": rpcserver.NewRPCFunc(IndexStats, ""),

// TODO remove this for euler-6 release
"staking/validators": rpcserver.NewRPCFunc(StakingValidators, "page,limit,status"),
"staking/pool": rpcserver.NewRPCFunc(StakingPool, ""),

// TODO remove this for euler-6 release
"submit_signed_link": rpcserver.NewRPCFunc(SignedMsgHandler(UnmarshalLinkRequestFn), "data"),
"submit_signed_send": rpcserver.NewRPCFunc(SignedMsgHandler(UnmarshalSendRequestFn), "data"),
}

func init() {
Expand Down
9 changes: 9 additions & 0 deletions cmd/cyberd/rpc/search.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,12 @@ func Search(ctx *rpctypes.Context, cid string, page, perPage int) (*ResultSearch
links, totalSize, err := cyberdApp.Search(cid, page, perPage)
return &ResultSearch{links, totalSize, page, perPage}, err
}

func Backlinks(ctx *rpctypes.Context, cid string, page, perPage int) (*ResultSearch, error) {
if perPage == 0 {
perPage = 100
}
links, totalSize, err := cyberdApp.Backlinks(cid, page, perPage)
return &ResultSearch{links, totalSize, page, perPage}, err
}

46 changes: 0 additions & 46 deletions cmd/cyberd/rpc/staking.go

This file was deleted.

35 changes: 0 additions & 35 deletions cmd/cyberd/rpc/tokens.go

This file was deleted.

2 changes: 2 additions & 0 deletions x/rank/exported/exported.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ type StateKeeper interface {
EndBlocker(sdk.Context, log.Logger)

Search(cidNumber link.CidNumber, page, perPage int) ([]types.RankedCidNumber, int, error)
Backlinks(cidNumber link.CidNumber, page, perPage int) ([]types.RankedCidNumber, int, error)
Accounts(account uint64, page, perPage int) (map[link.CidNumber]link.CidNumber, int, error)
Top(page, perPage int) ([]types.RankedCidNumber, int, error)

GetRankValue(link.CidNumber) float64
Expand Down
8 changes: 8 additions & 0 deletions x/rank/internal/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -152,10 +152,18 @@ func (s *StateKeeper) Search(cidNumber link.CidNumber, page, perPage int) ([]typ
return s.index.Search(cidNumber, page, perPage)
}

func (s *StateKeeper) Backlinks(cidNumber link.CidNumber, page, perPage int) ([]types.RankedCidNumber, int, error) {
return s.index.Backlinks(cidNumber, page, perPage)
}

func (s *StateKeeper) Top(page, perPage int) ([]types.RankedCidNumber, int, error) {
return s.index.Top(page, perPage)
}

func (s *StateKeeper) Accounts(account uint64, page, perPage int) (map[link.CidNumber]link.CidNumber, int, error) {
return s.index.Accounts(account, page, perPage)
}

func (s *StateKeeper) GetRankValue(cidNumber link.CidNumber) float64 {
return s.index.GetRankValue(cidNumber)
}
Expand Down
Loading