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.

244 lines
7.2 KiB

  1. package eventbus
  2. import (
  3. "context"
  4. "errors"
  5. "fmt"
  6. "strings"
  7. abci "github.com/tendermint/tendermint/abci/types"
  8. "github.com/tendermint/tendermint/libs/log"
  9. tmpubsub "github.com/tendermint/tendermint/libs/pubsub"
  10. "github.com/tendermint/tendermint/libs/service"
  11. "github.com/tendermint/tendermint/types"
  12. )
  13. // Subscription is a proxy interface for a pubsub Subscription.
  14. type Subscription interface {
  15. ID() string
  16. Next(context.Context) (tmpubsub.Message, error)
  17. }
  18. // EventBus is a common bus for all events going through the system.
  19. // It is a type-aware wrapper around an underlying pubsub server.
  20. // All events should be published via the bus.
  21. type EventBus struct {
  22. service.BaseService
  23. pubsub *tmpubsub.Server
  24. }
  25. // NewDefault returns a new event bus with default options.
  26. func NewDefault(l log.Logger) *EventBus {
  27. logger := l.With("module", "eventbus")
  28. pubsub := tmpubsub.NewServer(tmpubsub.BufferCapacity(0),
  29. func(s *tmpubsub.Server) {
  30. s.Logger = logger
  31. })
  32. b := &EventBus{pubsub: pubsub}
  33. b.BaseService = *service.NewBaseService(logger, "EventBus", b)
  34. return b
  35. }
  36. func (b *EventBus) OnStart(ctx context.Context) error {
  37. return b.pubsub.Start(ctx)
  38. }
  39. func (b *EventBus) OnStop() {
  40. if err := b.pubsub.Stop(); err != nil {
  41. if !errors.Is(err, service.ErrAlreadyStopped) {
  42. b.pubsub.Logger.Error("error trying to stop eventBus", "error", err)
  43. }
  44. }
  45. }
  46. func (b *EventBus) NumClients() int {
  47. return b.pubsub.NumClients()
  48. }
  49. func (b *EventBus) NumClientSubscriptions(clientID string) int {
  50. return b.pubsub.NumClientSubscriptions(clientID)
  51. }
  52. // Deprecated: Use SubscribeWithArgs instead.
  53. func (b *EventBus) Subscribe(ctx context.Context,
  54. clientID string, query tmpubsub.Query, capacities ...int) (Subscription, error) {
  55. return b.pubsub.Subscribe(ctx, clientID, query, capacities...)
  56. }
  57. func (b *EventBus) SubscribeWithArgs(ctx context.Context, args tmpubsub.SubscribeArgs) (Subscription, error) {
  58. return b.pubsub.SubscribeWithArgs(ctx, args)
  59. }
  60. func (b *EventBus) Unsubscribe(ctx context.Context, args tmpubsub.UnsubscribeArgs) error {
  61. return b.pubsub.Unsubscribe(ctx, args)
  62. }
  63. func (b *EventBus) UnsubscribeAll(ctx context.Context, subscriber string) error {
  64. return b.pubsub.UnsubscribeAll(ctx, subscriber)
  65. }
  66. func (b *EventBus) Observe(ctx context.Context, observe func(tmpubsub.Message) error, queries ...tmpubsub.Query) error {
  67. return b.pubsub.Observe(ctx, observe, queries...)
  68. }
  69. func (b *EventBus) Publish(eventValue string, eventData types.TMEventData) error {
  70. // no explicit deadline for publishing events
  71. ctx := context.Background()
  72. tokens := strings.Split(types.EventTypeKey, ".")
  73. event := abci.Event{
  74. Type: tokens[0],
  75. Attributes: []abci.EventAttribute{
  76. {
  77. Key: tokens[1],
  78. Value: eventValue,
  79. },
  80. },
  81. }
  82. return b.pubsub.PublishWithEvents(ctx, eventData, []abci.Event{event})
  83. }
  84. func (b *EventBus) PublishEventNewBlock(data types.EventDataNewBlock) error {
  85. // no explicit deadline for publishing events
  86. ctx := context.Background()
  87. events := append(data.ResultBeginBlock.Events, data.ResultEndBlock.Events...)
  88. // add Tendermint-reserved new block event
  89. events = append(events, types.EventNewBlock)
  90. return b.pubsub.PublishWithEvents(ctx, data, events)
  91. }
  92. func (b *EventBus) PublishEventNewBlockHeader(data types.EventDataNewBlockHeader) error {
  93. // no explicit deadline for publishing events
  94. ctx := context.Background()
  95. events := append(data.ResultBeginBlock.Events, data.ResultEndBlock.Events...)
  96. // add Tendermint-reserved new block header event
  97. events = append(events, types.EventNewBlockHeader)
  98. return b.pubsub.PublishWithEvents(ctx, data, events)
  99. }
  100. func (b *EventBus) PublishEventNewEvidence(evidence types.EventDataNewEvidence) error {
  101. return b.Publish(types.EventNewEvidenceValue, evidence)
  102. }
  103. func (b *EventBus) PublishEventVote(data types.EventDataVote) error {
  104. return b.Publish(types.EventVoteValue, data)
  105. }
  106. func (b *EventBus) PublishEventValidBlock(data types.EventDataRoundState) error {
  107. return b.Publish(types.EventValidBlockValue, data)
  108. }
  109. func (b *EventBus) PublishEventBlockSyncStatus(data types.EventDataBlockSyncStatus) error {
  110. return b.Publish(types.EventBlockSyncStatusValue, data)
  111. }
  112. func (b *EventBus) PublishEventStateSyncStatus(data types.EventDataStateSyncStatus) error {
  113. return b.Publish(types.EventStateSyncStatusValue, data)
  114. }
  115. // PublishEventTx publishes tx event with events from Result. Note it will add
  116. // predefined keys (EventTypeKey, TxHashKey). Existing events with the same keys
  117. // will be overwritten.
  118. func (b *EventBus) PublishEventTx(data types.EventDataTx) error {
  119. // no explicit deadline for publishing events
  120. ctx := context.Background()
  121. events := data.Result.Events
  122. // add Tendermint-reserved events
  123. events = append(events, types.EventTx)
  124. tokens := strings.Split(types.TxHashKey, ".")
  125. events = append(events, abci.Event{
  126. Type: tokens[0],
  127. Attributes: []abci.EventAttribute{
  128. {
  129. Key: tokens[1],
  130. Value: fmt.Sprintf("%X", types.Tx(data.Tx).Hash()),
  131. },
  132. },
  133. })
  134. tokens = strings.Split(types.TxHeightKey, ".")
  135. events = append(events, abci.Event{
  136. Type: tokens[0],
  137. Attributes: []abci.EventAttribute{
  138. {
  139. Key: tokens[1],
  140. Value: fmt.Sprintf("%d", data.Height),
  141. },
  142. },
  143. })
  144. return b.pubsub.PublishWithEvents(ctx, data, events)
  145. }
  146. func (b *EventBus) PublishEventNewRoundStep(data types.EventDataRoundState) error {
  147. return b.Publish(types.EventNewRoundStepValue, data)
  148. }
  149. func (b *EventBus) PublishEventTimeoutPropose(data types.EventDataRoundState) error {
  150. return b.Publish(types.EventTimeoutProposeValue, data)
  151. }
  152. func (b *EventBus) PublishEventTimeoutWait(data types.EventDataRoundState) error {
  153. return b.Publish(types.EventTimeoutWaitValue, data)
  154. }
  155. func (b *EventBus) PublishEventNewRound(data types.EventDataNewRound) error {
  156. return b.Publish(types.EventNewRoundValue, data)
  157. }
  158. func (b *EventBus) PublishEventCompleteProposal(data types.EventDataCompleteProposal) error {
  159. return b.Publish(types.EventCompleteProposalValue, data)
  160. }
  161. func (b *EventBus) PublishEventPolka(data types.EventDataRoundState) error {
  162. return b.Publish(types.EventPolkaValue, data)
  163. }
  164. func (b *EventBus) PublishEventUnlock(data types.EventDataRoundState) error {
  165. return b.Publish(types.EventUnlockValue, data)
  166. }
  167. func (b *EventBus) PublishEventRelock(data types.EventDataRoundState) error {
  168. return b.Publish(types.EventRelockValue, data)
  169. }
  170. func (b *EventBus) PublishEventLock(data types.EventDataRoundState) error {
  171. return b.Publish(types.EventLockValue, data)
  172. }
  173. func (b *EventBus) PublishEventValidatorSetUpdates(data types.EventDataValidatorSetUpdates) error {
  174. return b.Publish(types.EventValidatorSetUpdatesValue, data)
  175. }
  176. //-----------------------------------------------------------------------------
  177. // NopEventBus implements a types.BlockEventPublisher that discards all events.
  178. type NopEventBus struct{}
  179. func (NopEventBus) PublishEventNewBlock(types.EventDataNewBlock) error {
  180. return nil
  181. }
  182. func (NopEventBus) PublishEventNewBlockHeader(types.EventDataNewBlockHeader) error {
  183. return nil
  184. }
  185. func (NopEventBus) PublishEventNewEvidence(types.EventDataNewEvidence) error {
  186. return nil
  187. }
  188. func (NopEventBus) PublishEventTx(types.EventDataTx) error {
  189. return nil
  190. }
  191. func (NopEventBus) PublishEventValidatorSetUpdates(types.EventDataValidatorSetUpdates) error {
  192. return nil
  193. }