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.

112 lines
3.1 KiB

  1. package proxy
  2. import (
  3. "github.com/pkg/errors"
  4. cmn "github.com/tendermint/tmlibs/common"
  5. )
  6. //-----------------------------
  7. // Tendermint's interface to the application consists of multiple connections
  8. type AppConns interface {
  9. cmn.Service
  10. Mempool() AppConnMempool
  11. Consensus() AppConnConsensus
  12. Query() AppConnQuery
  13. }
  14. func NewAppConns(clientCreator ClientCreator, handshaker Handshaker) AppConns {
  15. return NewMultiAppConn(clientCreator, handshaker)
  16. }
  17. //-----------------------------
  18. // multiAppConn implements AppConns
  19. type Handshaker interface {
  20. Handshake(AppConns) error
  21. }
  22. // a multiAppConn is made of a few appConns (mempool, consensus, query)
  23. // and manages their underlying abci clients, including the handshake
  24. // which ensures the app and tendermint are synced.
  25. // TODO: on app restart, clients must reboot together
  26. type multiAppConn struct {
  27. cmn.BaseService
  28. handshaker Handshaker
  29. mempoolConn *appConnMempool
  30. consensusConn *appConnConsensus
  31. queryConn *appConnQuery
  32. clientCreator ClientCreator
  33. }
  34. // Make all necessary abci connections to the application
  35. func NewMultiAppConn(clientCreator ClientCreator, handshaker Handshaker) *multiAppConn {
  36. multiAppConn := &multiAppConn{
  37. handshaker: handshaker,
  38. clientCreator: clientCreator,
  39. }
  40. multiAppConn.BaseService = *cmn.NewBaseService(nil, "multiAppConn", multiAppConn)
  41. return multiAppConn
  42. }
  43. // Returns the mempool connection
  44. func (app *multiAppConn) Mempool() AppConnMempool {
  45. return app.mempoolConn
  46. }
  47. // Returns the consensus Connection
  48. func (app *multiAppConn) Consensus() AppConnConsensus {
  49. return app.consensusConn
  50. }
  51. // Returns the query Connection
  52. func (app *multiAppConn) Query() AppConnQuery {
  53. return app.queryConn
  54. }
  55. func (app *multiAppConn) OnStart() error {
  56. // query connection
  57. querycli, err := app.clientCreator.NewABCIClient()
  58. if err != nil {
  59. return errors.Wrap(err, "Error creating ABCI client (query connection)")
  60. }
  61. querycli.SetLogger(app.Logger.With("module", "abci-client", "connection", "query"))
  62. if err := querycli.Start(); err != nil {
  63. return errors.Wrap(err, "Error starting ABCI client (query connection)")
  64. }
  65. app.queryConn = NewAppConnQuery(querycli)
  66. // mempool connection
  67. memcli, err := app.clientCreator.NewABCIClient()
  68. if err != nil {
  69. return errors.Wrap(err, "Error creating ABCI client (mempool connection)")
  70. }
  71. memcli.SetLogger(app.Logger.With("module", "abci-client", "connection", "mempool"))
  72. if err := memcli.Start(); err != nil {
  73. return errors.Wrap(err, "Error starting ABCI client (mempool connection)")
  74. }
  75. app.mempoolConn = NewAppConnMempool(memcli)
  76. // consensus connection
  77. concli, err := app.clientCreator.NewABCIClient()
  78. if err != nil {
  79. return errors.Wrap(err, "Error creating ABCI client (consensus connection)")
  80. }
  81. concli.SetLogger(app.Logger.With("module", "abci-client", "connection", "consensus"))
  82. if err := concli.Start(); err != nil {
  83. return errors.Wrap(err, "Error starting ABCI client (consensus connection)")
  84. }
  85. app.consensusConn = NewAppConnConsensus(concli)
  86. // ensure app is synced to the latest state
  87. if app.handshaker != nil {
  88. return app.handshaker.Handshake(app)
  89. }
  90. return nil
  91. }