|
1 | 1 | package context |
2 | 2 |
|
3 | 3 | import ( |
4 | | - "fmt" |
5 | | - "io" |
6 | | - |
7 | 4 | "github.com/pkg/errors" |
8 | 5 |
|
9 | | - abci "github.com/tendermint/tendermint/abci/types" |
10 | | - ctypes "github.com/tendermint/tendermint/rpc/core/types" |
| 6 | + sdk "github.com/cosmos/cosmos-sdk/types" |
11 | 7 | ) |
12 | 8 |
|
13 | | -// TODO: This should get deleted eventually, and perhaps |
14 | | -// ctypes.ResultBroadcastTx be stripped of unused fields, and |
15 | | -// ctypes.ResultBroadcastTxCommit returned for tendermint RPC BroadcastTxSync. |
16 | | -// |
17 | | -// The motivation is that we want a unified type to return, and the better |
18 | | -// option is the one that can hold CheckTx/DeliverTx responses optionally. |
19 | | -func resultBroadcastTxToCommit(res *ctypes.ResultBroadcastTx) *ctypes.ResultBroadcastTxCommit { |
20 | | - return &ctypes.ResultBroadcastTxCommit{ |
21 | | - Hash: res.Hash, |
22 | | - // NOTE: other fields are unused for async. |
23 | | - } |
24 | | -} |
25 | | - |
26 | 9 | // BroadcastTx broadcasts a transactions either synchronously or asynchronously |
27 | 10 | // based on the context parameters. The result of the broadcast is parsed into |
28 | 11 | // an intermediate structure which is logged if the context has a logger |
29 | 12 | // defined. |
30 | | -func (ctx CLIContext) BroadcastTx(txBytes []byte) (*ctypes.ResultBroadcastTxCommit, error) { |
| 13 | +func (ctx CLIContext) BroadcastTx(txBytes []byte) (res sdk.TxResponse, err error) { |
31 | 14 | if ctx.Async { |
32 | | - res, err := ctx.broadcastTxAsync(txBytes) |
33 | | - if err != nil { |
34 | | - return nil, err |
| 15 | + if res, err = ctx.BroadcastTxAsync(txBytes); err != nil { |
| 16 | + return |
35 | 17 | } |
| 18 | + return |
| 19 | + } |
36 | 20 |
|
37 | | - resCommit := resultBroadcastTxToCommit(res) |
38 | | - return resCommit, err |
| 21 | + if res, err = ctx.BroadcastTxAndAwaitCommit(txBytes); err != nil { |
| 22 | + return |
39 | 23 | } |
40 | 24 |
|
41 | | - return ctx.broadcastTxCommit(txBytes) |
| 25 | + return |
42 | 26 | } |
43 | 27 |
|
44 | 28 | // BroadcastTxAndAwaitCommit broadcasts transaction bytes to a Tendermint node |
45 | 29 | // and waits for a commit. |
46 | | -func (ctx CLIContext) BroadcastTxAndAwaitCommit(tx []byte) (*ctypes.ResultBroadcastTxCommit, error) { |
| 30 | +func (ctx CLIContext) BroadcastTxAndAwaitCommit(tx []byte) (sdk.TxResponse, error) { |
47 | 31 | node, err := ctx.GetNode() |
48 | 32 | if err != nil { |
49 | | - return nil, err |
| 33 | + return sdk.TxResponse{}, err |
50 | 34 | } |
51 | 35 |
|
52 | 36 | res, err := node.BroadcastTxCommit(tx) |
53 | 37 | if err != nil { |
54 | | - return res, err |
| 38 | + return sdk.NewResponseFormatBroadcastTxCommit(res), err |
55 | 39 | } |
56 | 40 |
|
57 | 41 | if !res.CheckTx.IsOK() { |
58 | | - return res, errors.Errorf(res.CheckTx.Log) |
| 42 | + return sdk.NewResponseFormatBroadcastTxCommit(res), errors.Errorf(res.CheckTx.Log) |
59 | 43 | } |
60 | 44 |
|
61 | 45 | if !res.DeliverTx.IsOK() { |
62 | | - return res, errors.Errorf(res.DeliverTx.Log) |
| 46 | + return sdk.NewResponseFormatBroadcastTxCommit(res), errors.Errorf(res.DeliverTx.Log) |
63 | 47 | } |
64 | 48 |
|
65 | | - return res, err |
| 49 | + return sdk.NewResponseFormatBroadcastTxCommit(res), err |
66 | 50 | } |
67 | 51 |
|
68 | | -// BroadcastTxSync broadcasts transaction bytes to a Tendermint node |
69 | | -// synchronously. |
70 | | -func (ctx CLIContext) BroadcastTxSync(tx []byte) (*ctypes.ResultBroadcastTx, error) { |
| 52 | +// BroadcastTxSync broadcasts transaction bytes to a Tendermint node synchronously. |
| 53 | +func (ctx CLIContext) BroadcastTxSync(tx []byte) (sdk.TxResponse, error) { |
71 | 54 | node, err := ctx.GetNode() |
72 | 55 | if err != nil { |
73 | | - return nil, err |
| 56 | + return sdk.TxResponse{}, err |
74 | 57 | } |
75 | 58 |
|
76 | 59 | res, err := node.BroadcastTxSync(tx) |
77 | 60 | if err != nil { |
78 | | - return res, err |
| 61 | + return sdk.NewResponseFormatBroadcastTx(res), err |
79 | 62 | } |
80 | 63 |
|
81 | | - return res, err |
| 64 | + return sdk.NewResponseFormatBroadcastTx(res), err |
82 | 65 | } |
83 | 66 |
|
84 | | -// BroadcastTxAsync broadcasts transaction bytes to a Tendermint node |
85 | | -// asynchronously. |
86 | | -func (ctx CLIContext) BroadcastTxAsync(tx []byte) (*ctypes.ResultBroadcastTx, error) { |
| 67 | +// BroadcastTxAsync broadcasts transaction bytes to a Tendermint node asynchronously. |
| 68 | +func (ctx CLIContext) BroadcastTxAsync(tx []byte) (sdk.TxResponse, error) { |
87 | 69 | node, err := ctx.GetNode() |
88 | 70 | if err != nil { |
89 | | - return nil, err |
| 71 | + return sdk.TxResponse{}, err |
90 | 72 | } |
91 | 73 |
|
92 | 74 | res, err := node.BroadcastTxAsync(tx) |
93 | 75 | if err != nil { |
94 | | - return res, err |
95 | | - } |
96 | | - |
97 | | - return res, err |
98 | | -} |
99 | | - |
100 | | -func (ctx CLIContext) broadcastTxAsync(txBytes []byte) (*ctypes.ResultBroadcastTx, error) { |
101 | | - res, err := ctx.BroadcastTxAsync(txBytes) |
102 | | - if err != nil { |
103 | | - return res, err |
104 | | - } |
105 | | - |
106 | | - if ctx.Output != nil { |
107 | | - if ctx.OutputFormat == "json" { |
108 | | - type toJSON struct { |
109 | | - TxHash string |
110 | | - } |
111 | | - |
112 | | - resJSON := toJSON{res.Hash.String()} |
113 | | - bz, err := ctx.Codec.MarshalJSON(resJSON) |
114 | | - if err != nil { |
115 | | - return res, err |
116 | | - } |
117 | | - |
118 | | - ctx.Output.Write(bz) |
119 | | - io.WriteString(ctx.Output, "\n") |
120 | | - } else { |
121 | | - io.WriteString(ctx.Output, fmt.Sprintf("async tx sent (tx hash: %s)\n", res.Hash)) |
122 | | - } |
123 | | - } |
124 | | - |
125 | | - return res, nil |
126 | | -} |
127 | | - |
128 | | -func (ctx CLIContext) broadcastTxCommit(txBytes []byte) (*ctypes.ResultBroadcastTxCommit, error) { |
129 | | - res, err := ctx.BroadcastTxAndAwaitCommit(txBytes) |
130 | | - if err != nil { |
131 | | - return res, err |
132 | | - } |
133 | | - |
134 | | - if ctx.OutputFormat == "json" { |
135 | | - // Since JSON is intended for automated scripts, always include response in |
136 | | - // JSON mode. |
137 | | - type toJSON struct { |
138 | | - Height int64 |
139 | | - TxHash string |
140 | | - Response abci.ResponseDeliverTx |
141 | | - } |
142 | | - |
143 | | - if ctx.Output != nil { |
144 | | - resJSON := toJSON{res.Height, res.Hash.String(), res.DeliverTx} |
145 | | - bz, err := ctx.Codec.MarshalJSON(resJSON) |
146 | | - if err != nil { |
147 | | - return res, err |
148 | | - } |
149 | | - |
150 | | - ctx.Output.Write(bz) |
151 | | - io.WriteString(ctx.Output, "\n") |
152 | | - } |
153 | | - |
154 | | - return res, nil |
155 | | - } |
156 | | - |
157 | | - if ctx.Output != nil { |
158 | | - resStr := fmt.Sprintf("Committed at block %d (tx hash: %s)\n", res.Height, res.Hash.String()) |
159 | | - |
160 | | - if ctx.PrintResponse { |
161 | | - resStr = fmt.Sprintf("Committed at block %d (tx hash: %s, response: %+v)\n", |
162 | | - res.Height, res.Hash.String(), res.DeliverTx, |
163 | | - ) |
164 | | - } |
165 | | - |
166 | | - io.WriteString(ctx.Output, resStr) |
| 76 | + return sdk.NewResponseFormatBroadcastTx(res), err |
167 | 77 | } |
168 | 78 |
|
169 | | - return res, nil |
| 79 | + return sdk.NewResponseFormatBroadcastTx(res), err |
170 | 80 | } |
0 commit comments