OSDN Git Service

add txfeed
[bytom/bytom.git] / blockchain / txfeeds.go
1 package blockchain
2
3 import (
4         "context"
5 //      "fmt"
6 //      "math"
7
8         "github.com/bytom/blockchain/query"
9         "github.com/bytom/blockchain/txfeed"
10         "github.com/bytom/errors"
11         "github.com/bytom/log"
12         "github.com/bytom/net/http/httpjson"
13 )
14
15 // POST /create-txfeed
16 func (a *BlockchainReactor) createTxFeed(ctx context.Context, in struct {
17         Alias  string
18         Filter string
19
20         // ClientToken is the application's unique token for the txfeed. Every txfeed
21         // should have a unique client token. The client token is used to ensure
22         // idempotency of create txfeed requests. Duplicate create txfeed requests
23         // with the same client_token will only create one txfeed.
24         ClientToken string `json:"client_token"`
25 }) (*txfeed.TxFeed, error) {
26         log.Printf(ctx,"-------createTxFeed-------")
27 //      after := fmt.Sprintf("%d:%d-%d", a.chain.Height(), math.MaxInt32, uint64(math.MaxInt64))
28 //      return a.txFeeds.Create(ctx, in.Alias, in.Filter, after, in.ClientToken)
29         return nil,nil
30 }
31
32 // POST /get-transaction-feed
33 func (a *BlockchainReactor) getTxFeed(ctx context.Context, in struct {
34         ID    string `json:"id,omitempty"`
35         Alias string `json:"alias,omitempty"`
36 }) (*txfeed.TxFeed, error) {
37         log.Printf(ctx,"-------getTxFeed-------")
38 //      return a.txFeeds.Find(ctx, in.ID, in.Alias)
39         return nil,nil
40 }
41
42 // POST /delete-transaction-feed
43 func (a *BlockchainReactor) deleteTxFeed(ctx context.Context, in struct {
44         ID    string `json:"id,omitempty"`
45         Alias string `json:"alias,omitempty"`
46 }) error {
47         log.Printf(ctx,"-------deleteTxFeed-------")
48 //      return a.txFeeds.Delete(ctx, in.ID, in.Alias)
49         return nil
50 }
51
52 // POST /update-transaction-feed
53 func (a *BlockchainReactor) updateTxFeed(ctx context.Context, in struct {
54         ID    string `json:"id,omitempty"`
55         Alias string `json:"alias,omitempty"`
56         Prev  string `json:"previous_after"`
57         After string `json:"after"`
58 }) (*txfeed.TxFeed, error) {
59         log.Printf(ctx,"-------updateTxFeed-------")
60         // TODO(tessr): Consider moving this function into the txfeed package.
61         // (It's currently outside the txfeed package to avoid a dependecy cycle
62         // between txfeed and query.)
63         bad, err := txAfterIsBefore(in.After, in.Prev)
64         if err != nil {
65                 return nil, err
66         }
67
68         if bad {
69                 return nil, errors.WithDetail(httpjson.ErrBadRequest, "new After cannot be before Prev")
70         }
71 //      return a.txFeeds.Update(ctx, in.ID, in.Alias, in.After, in.Prev)
72         return nil,nil
73 }
74
75 // txAfterIsBefore returns true if a is before b. It returns an error if either
76 // a or b are not valid query.TxAfters.
77 func txAfterIsBefore(a, b string) (bool, error) {
78         aAfter, err := query.DecodeTxAfter(a)
79         if err != nil {
80                 return false, err
81         }
82
83         bAfter, err := query.DecodeTxAfter(b)
84         if err != nil {
85                 return false, err
86         }
87
88         return aAfter.FromBlockHeight < bAfter.FromBlockHeight ||
89                 (aAfter.FromBlockHeight == bAfter.FromBlockHeight &&
90                         aAfter.FromPosition < bAfter.FromPosition), nil
91 }
92