OSDN Git Service

add btm descriptor in log & Add issue amout as a argment
[bytom/bytom.git] / blockchain / assets.go
1 package blockchain
2
3 import (
4         "context"
5         "sync"
6
7         "github.com/bytom/blockchain/asset"
8         "github.com/bytom/crypto/ed25519/chainkd"
9         "github.com/bytom/net/http/httpjson"
10         "github.com/bytom/net/http/reqid"
11         "github.com/bytom/log"
12 )
13
14 // POST /create-asset
15 func (a *BlockchainReactor) createAsset(ctx context.Context, ins []struct {
16         Alias      string
17         RootXPubs  []chainkd.XPub `json:"root_xpubs"`
18         Quorum     int
19         Definition map[string]interface{}
20         Tags       map[string]interface{}
21
22         // ClientToken is the application's unique token for the asset. Every asset
23         // should have a unique client token. The client token is used to ensure
24         // idempotency of create asset requests. Duplicate create asset requests
25         // with the same client_token will only create one asset.
26         ClientToken string `json:"client_token"`
27 }) ([]interface{}, error) {
28         responses := make([]interface{}, len(ins))
29         var wg sync.WaitGroup
30         wg.Add(len(responses))
31
32         for i := range responses {
33                 go func(i int) {
34                         subctx := reqid.NewSubContext(ctx, reqid.New())
35                         defer wg.Done()
36                         defer batchRecover(subctx, &responses[i])
37
38                         a, err := a.assets.Define(
39                                 subctx,
40                                 ins[i].RootXPubs,
41                                 ins[i].Quorum,
42                                 ins[i].Definition,
43                                 ins[i].Alias,
44                                 ins[i].Tags,
45                                 ins[i].ClientToken,
46                         )
47                         if err != nil {
48                                 responses[i] = err
49                                 return
50                         }
51                         aa, err := asset.Annotated(a)
52                         log.Printf(ctx, "-------createAsset-----Annotated asset:%v", aa)
53                         if err != nil {
54                                 responses[i] = err
55                                 return
56                         }
57                         responses[i] = aa
58                 }(i)
59         }
60
61         wg.Wait()
62         log.Printf(ctx, "-------createAsset-----%v", responses)
63         return responses, nil
64 }
65
66 // POST /update-asset-tags
67 func (a *BlockchainReactor) updateAssetTags(ctx context.Context, ins []struct {
68         ID    *string
69         Alias *string
70         Tags  map[string]interface{} `json:"tags"`
71 }) interface{} {
72         log.Printf(ctx,"------updateAssetTags-----")
73         responses := make([]interface{}, len(ins))
74         var wg sync.WaitGroup
75         wg.Add(len(responses))
76
77         for i := range responses {
78                 go func(i int) {
79                         subctx := reqid.NewSubContext(ctx, reqid.New())
80                         defer wg.Done()
81                         defer batchRecover(subctx, &responses[i])
82
83                         err := a.assets.UpdateTags(subctx, ins[i].ID, ins[i].Alias, ins[i].Tags)
84                         if err != nil {
85                                 responses[i] = err
86                         } else {
87                                 responses[i] = httpjson.DefaultResponse
88                         }
89                 }(i)
90         }
91
92         wg.Wait()
93         return responses
94 }
95