OSDN Git Service

Merge pull request #34 from icodezjb/demo
[bytom/bytom.git] / blockchain / query.go
1 package blockchain
2
3 import (
4         "context"
5         "math"
6
7         "github.com/bytom/blockchain/query"
8         "github.com/bytom/blockchain/query/filter"
9         "github.com/bytom/errors"
10         "github.com/bytom/net/http/httpjson"
11         //"github.com/bytom/log"
12 )
13
14 const (
15         defGenericPageSize = 100
16 )
17
18
19 //
20 // POST /list-accounts
21 func (bcr *BlockchainReactor) listAccounts(ctx context.Context, in requestQuery) interface{} {
22
23         response,_ := bcr.accounts.QueryAll(ctx)
24
25         return response
26
27 }
28
29
30 //
31 // POST /list-assets
32 func (bcr *BlockchainReactor) listAssets(ctx context.Context, in requestQuery) interface{} {
33
34         response,_ := bcr.assets.QueryAll(ctx)
35
36         return response
37 }
38
39 // POST /list-balances
40 func (bcr *BlockchainReactor) listBalances(ctx context.Context, in requestQuery) (result page, err error) {
41         var sumBy []filter.Field
42
43         // Since an empty SumBy yields a meaningless result, we'll provide a
44         // sensible default here.
45         if len(in.SumBy) == 0 {
46                 in.SumBy = []string{"asset_alias", "asset_id"}
47         }
48
49         for _, field := range in.SumBy {
50                 f, err := filter.ParseField(field)
51                 if err != nil {
52                         return result, err
53                 }
54                 sumBy = append(sumBy, f)
55         }
56
57         timestampMS := in.TimestampMS
58         if timestampMS == 0 {
59                 timestampMS = math.MaxInt64
60         } else if timestampMS > math.MaxInt64 {
61                 return result, errors.WithDetail(httpjson.ErrBadRequest, "timestamp is too large")
62         }
63
64         // TODO(jackson): paginate this endpoint.
65 /*      balances, err := bcr.indexer.Balances(ctx, in.Filter, in.FilterParams, sumBy, timestampMS)
66         if err != nil {
67                 return result, err
68         }
69 */
70 //      result.Items = httpjson.Array(balances)
71         result.LastPage = true
72         result.Next = in
73         return result, nil
74 }
75
76 // listTransactions is an http handler for listing transactions matching
77 // an index or an ad-hoc filter.
78 //
79 // POST /list-transactions
80 func (bcr *BlockchainReactor) listTransactions(ctx context.Context, in requestQuery) (result page, err error) {
81         var c context.CancelFunc
82         timeout := in.Timeout.Duration
83         if timeout != 0 {
84                 ctx, c = context.WithTimeout(ctx, timeout)
85                 defer c()
86         }
87
88         limit := in.PageSize
89         if limit == 0 {
90                 limit = defGenericPageSize
91         }
92
93         endTimeMS := in.EndTimeMS
94         if endTimeMS == 0 {
95                 endTimeMS = math.MaxInt64
96         } else if endTimeMS > math.MaxInt64 {
97                 return result, errors.WithDetail(httpjson.ErrBadRequest, "end timestamp is too large")
98         }
99
100         // Either parse the provided `after` or look one up for the time range.
101 //      var after query.TxAfter
102         if in.After != "" {
103                 _, err = query.DecodeTxAfter(in.After)
104                 if err != nil {
105                         return result, errors.Wrap(err, "decoding `after`")
106                 }
107         } else {
108 /*              after, err = bcr.indexer.LookupTxAfter(ctx, in.StartTimeMS, endTimeMS)
109                 if err != nil {
110                         return result, err
111                 }
112 */
113         }
114
115 /*      txns, nextAfter, err := bcr.indexer.Transactions(ctx, in.Filter, in.FilterParams, after, limit, in.AscLongPoll)
116         if err != nil {
117                 return result, errors.Wrap(err, "running tx query")
118         }
119 */
120         out := in
121 //      out.After = nextAfter.String()
122         return page{
123 //              Items:    httpjson.Array(txns),
124 //              LastPage: len(txns) < limit,
125                 Next:     out,
126         }, nil
127 }
128
129 // listTxFeeds is an http handler for listing txfeeds. It does not take a filter.
130 //
131 // POST /list-transaction-feeds
132 func (bcr *BlockchainReactor) listTxFeeds(ctx context.Context, in requestQuery) (page, error) {
133         limit := in.PageSize
134         if limit == 0 {
135                 limit = defGenericPageSize
136         }
137
138         after := in.After
139
140 /*      txfeeds, after, err := bcr.txFeeds.Query(ctx, after, limit)
141         if err != nil {
142                 return page{}, errors.Wrap(err, "running txfeed query")
143         }
144 */
145         out := in
146         out.After = after
147         return page{
148 //              Items:    httpjson.Array(txfeeds),
149 //              LastPage: len(txfeeds) < limit,
150                 Next:     out,
151         }, nil
152 }
153
154 // POST /list-unspent-outputs
155 func (bcr *BlockchainReactor) listUnspentOutputs(ctx context.Context, in requestQuery) (result page, err error) {
156         limit := in.PageSize
157         if limit == 0 {
158                 limit = defGenericPageSize
159         }
160
161 //      var after *query.OutputsAfter
162         if in.After != "" {
163                 _, err = query.DecodeOutputsAfter(in.After)
164                 if err != nil {
165                         return result, errors.Wrap(err, "decoding `after`")
166                 }
167         }
168
169         timestampMS := in.TimestampMS
170         if timestampMS == 0 {
171                 timestampMS = math.MaxInt64
172         } else if timestampMS > math.MaxInt64 {
173                 return result, errors.WithDetail(httpjson.ErrBadRequest, "timestamp is too large")
174         }
175 /*      outputs, nextAfter, err := bcr.indexer.Outputs(ctx, in.Filter, in.FilterParams, timestampMS, after, limit)
176         if err != nil {
177                 return result, errors.Wrap(err, "querying outputs")
178         }
179 */
180         outQuery := in
181 //      outQuery.After = nextAfter.String()
182         return page{
183 //              Items:    httpjson.Array(outputs),
184 //              LastPage: len(outputs) < limit,
185                 Next:     outQuery,
186         }, nil
187 }