You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

195 lines
5.0 KiB

7 years ago
7 years ago
7 years ago
8 years ago
8 years ago
7 years ago
  1. package mock
  2. import (
  3. abci "github.com/tendermint/abci/types"
  4. data "github.com/tendermint/go-wire/data"
  5. "github.com/tendermint/tendermint/rpc/client"
  6. ctypes "github.com/tendermint/tendermint/rpc/core/types"
  7. "github.com/tendermint/tendermint/types"
  8. "github.com/tendermint/tendermint/version"
  9. )
  10. // ABCIApp will send all abci related request to the named app,
  11. // so you can test app behavior from a client without needing
  12. // an entire tendermint node
  13. type ABCIApp struct {
  14. App abci.Application
  15. }
  16. func (a ABCIApp) _assertABCIClient() client.ABCIClient {
  17. return a
  18. }
  19. func (a ABCIApp) ABCIInfo() (*ctypes.ResultABCIInfo, error) {
  20. return &ctypes.ResultABCIInfo{a.App.Info(abci.RequestInfo{version.Version})}, nil
  21. }
  22. func (a ABCIApp) ABCIQuery(path string, data data.Bytes, prove bool) (*ctypes.ResultABCIQuery, error) {
  23. q := a.App.Query(abci.RequestQuery{data, path, 0, prove})
  24. return &ctypes.ResultABCIQuery{q.Result()}, nil
  25. }
  26. func (a ABCIApp) BroadcastTxCommit(tx types.Tx) (*ctypes.ResultBroadcastTxCommit, error) {
  27. res := ctypes.ResultBroadcastTxCommit{}
  28. res.CheckTx = a.App.CheckTx(tx)
  29. if !res.CheckTx.IsOK() {
  30. return &res, nil
  31. }
  32. res.DeliverTx = a.App.DeliverTx(tx)
  33. return &res, nil
  34. }
  35. func (a ABCIApp) BroadcastTxAsync(tx types.Tx) (*ctypes.ResultBroadcastTx, error) {
  36. c := a.App.CheckTx(tx)
  37. // and this gets written in a background thread...
  38. if c.IsOK() {
  39. go func() { a.App.DeliverTx(tx) }()
  40. }
  41. return &ctypes.ResultBroadcastTx{c.Code, c.Data, c.Log, tx.Hash()}, nil
  42. }
  43. func (a ABCIApp) BroadcastTxSync(tx types.Tx) (*ctypes.ResultBroadcastTx, error) {
  44. c := a.App.CheckTx(tx)
  45. // and this gets written in a background thread...
  46. if c.IsOK() {
  47. go func() { a.App.DeliverTx(tx) }()
  48. }
  49. return &ctypes.ResultBroadcastTx{c.Code, c.Data, c.Log, tx.Hash()}, nil
  50. }
  51. // ABCIMock will send all abci related request to the named app,
  52. // so you can test app behavior from a client without needing
  53. // an entire tendermint node
  54. type ABCIMock struct {
  55. Info Call
  56. Query Call
  57. BroadcastCommit Call
  58. Broadcast Call
  59. }
  60. func (m ABCIMock) _assertABCIClient() client.ABCIClient {
  61. return m
  62. }
  63. func (m ABCIMock) ABCIInfo() (*ctypes.ResultABCIInfo, error) {
  64. res, err := m.Info.GetResponse(nil)
  65. if err != nil {
  66. return nil, err
  67. }
  68. return &ctypes.ResultABCIInfo{res.(abci.ResponseInfo)}, nil
  69. }
  70. func (m ABCIMock) ABCIQuery(path string, data data.Bytes, prove bool) (*ctypes.ResultABCIQuery, error) {
  71. res, err := m.Query.GetResponse(QueryArgs{path, data, prove})
  72. if err != nil {
  73. return nil, err
  74. }
  75. resQuery := res.(abci.ResponseQuery)
  76. return &ctypes.ResultABCIQuery{resQuery.Result()}, nil
  77. }
  78. func (m ABCIMock) BroadcastTxCommit(tx types.Tx) (*ctypes.ResultBroadcastTxCommit, error) {
  79. res, err := m.BroadcastCommit.GetResponse(tx)
  80. if err != nil {
  81. return nil, err
  82. }
  83. return res.(*ctypes.ResultBroadcastTxCommit), nil
  84. }
  85. func (m ABCIMock) BroadcastTxAsync(tx types.Tx) (*ctypes.ResultBroadcastTx, error) {
  86. res, err := m.Broadcast.GetResponse(tx)
  87. if err != nil {
  88. return nil, err
  89. }
  90. return res.(*ctypes.ResultBroadcastTx), nil
  91. }
  92. func (m ABCIMock) BroadcastTxSync(tx types.Tx) (*ctypes.ResultBroadcastTx, error) {
  93. res, err := m.Broadcast.GetResponse(tx)
  94. if err != nil {
  95. return nil, err
  96. }
  97. return res.(*ctypes.ResultBroadcastTx), nil
  98. }
  99. // ABCIRecorder can wrap another type (ABCIApp, ABCIMock, or Client)
  100. // and record all ABCI related calls.
  101. type ABCIRecorder struct {
  102. Client client.ABCIClient
  103. Calls []Call
  104. }
  105. func NewABCIRecorder(client client.ABCIClient) *ABCIRecorder {
  106. return &ABCIRecorder{
  107. Client: client,
  108. Calls: []Call{},
  109. }
  110. }
  111. func (r *ABCIRecorder) _assertABCIClient() client.ABCIClient {
  112. return r
  113. }
  114. type QueryArgs struct {
  115. Path string
  116. Data data.Bytes
  117. Prove bool
  118. }
  119. func (r *ABCIRecorder) addCall(call Call) {
  120. r.Calls = append(r.Calls, call)
  121. }
  122. func (r *ABCIRecorder) ABCIInfo() (*ctypes.ResultABCIInfo, error) {
  123. res, err := r.Client.ABCIInfo()
  124. r.addCall(Call{
  125. Name: "abci_info",
  126. Response: res,
  127. Error: err,
  128. })
  129. return res, err
  130. }
  131. func (r *ABCIRecorder) ABCIQuery(path string, data data.Bytes, prove bool) (*ctypes.ResultABCIQuery, error) {
  132. res, err := r.Client.ABCIQuery(path, data, prove)
  133. r.addCall(Call{
  134. Name: "abci_query",
  135. Args: QueryArgs{path, data, prove},
  136. Response: res,
  137. Error: err,
  138. })
  139. return res, err
  140. }
  141. func (r *ABCIRecorder) BroadcastTxCommit(tx types.Tx) (*ctypes.ResultBroadcastTxCommit, error) {
  142. res, err := r.Client.BroadcastTxCommit(tx)
  143. r.addCall(Call{
  144. Name: "broadcast_tx_commit",
  145. Args: tx,
  146. Response: res,
  147. Error: err,
  148. })
  149. return res, err
  150. }
  151. func (r *ABCIRecorder) BroadcastTxAsync(tx types.Tx) (*ctypes.ResultBroadcastTx, error) {
  152. res, err := r.Client.BroadcastTxAsync(tx)
  153. r.addCall(Call{
  154. Name: "broadcast_tx_async",
  155. Args: tx,
  156. Response: res,
  157. Error: err,
  158. })
  159. return res, err
  160. }
  161. func (r *ABCIRecorder) BroadcastTxSync(tx types.Tx) (*ctypes.ResultBroadcastTx, error) {
  162. res, err := r.Client.BroadcastTxSync(tx)
  163. r.addCall(Call{
  164. Name: "broadcast_tx_sync",
  165. Args: tx,
  166. Response: res,
  167. Error: err,
  168. })
  169. return res, err
  170. }