From f9436a3133637b28941b3d5baf645dab1b08ab4c Mon Sep 17 00:00:00 2001 From: icodezjb Date: Tue, 26 Sep 2017 16:28:20 +0800 Subject: [PATCH] support list-accounts and list-assets --- blockchain/account/accounts.go | 25 ++++++++++-------- blockchain/asset/asset.go | 14 +++++++++++ blockchain/query.go | 57 ++++++++++-------------------------------- cmd/bytomcli/main.go | 23 +++++++++++------ 4 files changed, 56 insertions(+), 63 deletions(-) diff --git a/blockchain/account/accounts.go b/blockchain/account/accounts.go index 9da7ddd0..cec6f3c4 100644 --- a/blockchain/account/accounts.go +++ b/blockchain/account/accounts.go @@ -326,16 +326,19 @@ func (m *Manager) nextIndex(ctx context.Context) (uint64, error) { return n, nil } -/* -func tagsToNullString(tags map[string]interface{}) (*stdsql.NullString, error) { - var tagsJSON []byte - if len(tags) != 0 { - var err error - tagsJSON, err = json.Marshal(tags) - if err != nil { - return nil, errors.Wrap(err) + +func (m *Manager) QueryAll(ctx context.Context) (interface{}, error){ + ret := make([]interface{},0) + + iter := m.db.Iterator() + for iter.Next() { + value := string(iter.Value()) + if value[:3] == "acc"{ + continue } + ret = append(ret,value) + //log.Printf(ctx,"%s\t", value) } - return &stdsql.NullString{String: string(tagsJSON), Valid: len(tagsJSON) > 0}, nil -} -*/ + + return ret,nil +} \ No newline at end of file diff --git a/blockchain/asset/asset.go b/blockchain/asset/asset.go index 64912ab7..d9c4fa40 100644 --- a/blockchain/asset/asset.go +++ b/blockchain/asset/asset.go @@ -18,6 +18,7 @@ import ( "github.com/bytom/protocol" "github.com/bytom/protocol/bc" "github.com/bytom/protocol/vm/vmutil" + //"github.com/bytom/log" ) const maxAssetCache = 1000 @@ -271,6 +272,19 @@ func (reg *Registry) FindByAlias(ctx context.Context, alias string) (*Asset, err } +func (reg *Registry) QueryAll(ctx context.Context) (interface{}, error){ + ret := make([]interface{},0) + + iter := reg.db.Iterator() + for iter.Next() { + value := string(iter.Value()) + ret = append(ret,value) + //log.Printf(ctx,"%s\t", value) + } + + return ret,nil +} + // insertAsset adds the asset to the database. If the asset has a client token, // and there already exists an asset with that client token, insertAsset will // lookup and return the existing asset instead. diff --git a/blockchain/query.go b/blockchain/query.go index 39060c30..623d893f 100644 --- a/blockchain/query.go +++ b/blockchain/query.go @@ -8,63 +8,32 @@ import ( "github.com/bytom/blockchain/query/filter" "github.com/bytom/errors" "github.com/bytom/net/http/httpjson" + //"github.com/bytom/log" ) const ( defGenericPageSize = 100 ) -// listAccounts is an http handler for listing accounts matching -// an index or an ad-hoc filter. + // // POST /list-accounts -func (bcr *BlockchainReactor) listAccounts(ctx context.Context, in requestQuery) (page, error) { - limit := in.PageSize - if limit == 0 { - limit = defGenericPageSize - } - after := in.After -/* - // Use the filter engine for querying account tags. - accounts, after, err := bcr.indexer.Accounts(ctx, in.Filter, in.FilterParams, after, limit) - if err != nil { - return page{}, errors.Wrap(err, "running acc query") - } -*/ - // Pull in the accounts by the IDs - out := in - out.After = after - return page{ -// Items: httpjson.Array(accounts), -// LastPage: len(accounts) < limit, - Next: out, - }, nil +func (bcr *BlockchainReactor) listAccounts(ctx context.Context, in requestQuery) interface{} { + + response,_ := bcr.accounts.QueryAll(ctx) + + return response + } -// listAssets is an http handler for listing assets matching -// an index or an ad-hoc filter. + // // POST /list-assets -func (bcr *BlockchainReactor) listAssets(ctx context.Context, in requestQuery) (page, error) { - limit := in.PageSize - if limit == 0 { - limit = defGenericPageSize - } - after := in.After +func (bcr *BlockchainReactor) listAssets(ctx context.Context, in requestQuery) interface{} { -/* // Use the query engine for querying asset tags. - assets, after, err := bcr.indexer.Assets(ctx, in.Filter, in.FilterParams, after, limit) - if err != nil { - return page{}, errors.Wrap(err, "running asset query") - } -*/ - out := in - out.After = after - return page{ -// Items: httpjson.Array(assets), -// LastPage: len(assets) < limit, - Next: out, - }, nil + response,_ := bcr.assets.QueryAll(ctx) + + return response } // POST /list-balances diff --git a/cmd/bytomcli/main.go b/cmd/bytomcli/main.go index 30f3b480..f9457803 100644 --- a/cmd/bytomcli/main.go +++ b/cmd/bytomcli/main.go @@ -543,10 +543,14 @@ func listAccounts(client *rpc.Client, args []string) { Aliases []string `json:"aliases,omitempty"` } var in requestQuery - after := in.After - out := in - out.After = after - client.Call(context.Background(), "/list-accounts", &[]requestQuery{in}, nil) + + + responses := make([]interface{}, 1) + + client.Call(context.Background(), "/list-accounts", in, &responses) + for i,item := range responses{ + fmt.Println(i,"-----",item) + } } func listAssets(client *rpc.Client, args []string) { @@ -568,10 +572,13 @@ func listAssets(client *rpc.Client, args []string) { Aliases []string `json:"aliases,omitempty"` } var in requestQuery - after := in.After - out := in - out.After = after - client.Call(context.Background(), "/list-assets", &[]requestQuery{in}, nil) + responses := make([]interface{}, 1) + + client.Call(context.Background(), "/list-assets", in, &responses) + for i,item := range responses{ + fmt.Println(i,"-----",item) + } + } func listTxFeeds(client *rpc.Client, args []string) { -- 2.11.0