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.

224 lines
7.1 KiB

  1. package eventbus
  2. import (
  3. "context"
  4. "fmt"
  5. "strings"
  6. abci "github.com/tendermint/tendermint/abci/types"
  7. tmpubsub "github.com/tendermint/tendermint/internal/pubsub"
  8. tmquery "github.com/tendermint/tendermint/internal/pubsub/query"
  9. "github.com/tendermint/tendermint/libs/log"
  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(l, tmpubsub.BufferCapacity(0))
  29. b := &EventBus{pubsub: pubsub}
  30. b.BaseService = *service.NewBaseService(logger, "EventBus", b)
  31. return b
  32. }
  33. func (b *EventBus) OnStart(ctx context.Context) error {
  34. return b.pubsub.Start(ctx)
  35. }
  36. func (b *EventBus) OnStop() {}
  37. func (b *EventBus) NumClients() int {
  38. return b.pubsub.NumClients()
  39. }
  40. func (b *EventBus) NumClientSubscriptions(clientID string) int {
  41. return b.pubsub.NumClientSubscriptions(clientID)
  42. }
  43. // Deprecated: Use SubscribeWithArgs instead.
  44. func (b *EventBus) Subscribe(ctx context.Context,
  45. clientID string, query *tmquery.Query, capacities ...int) (Subscription, error) {
  46. return b.pubsub.Subscribe(ctx, clientID, query, capacities...)
  47. }
  48. func (b *EventBus) SubscribeWithArgs(ctx context.Context, args tmpubsub.SubscribeArgs) (Subscription, error) {
  49. return b.pubsub.SubscribeWithArgs(ctx, args)
  50. }
  51. func (b *EventBus) Unsubscribe(ctx context.Context, args tmpubsub.UnsubscribeArgs) error {
  52. return b.pubsub.Unsubscribe(ctx, args)
  53. }
  54. func (b *EventBus) UnsubscribeAll(ctx context.Context, subscriber string) error {
  55. return b.pubsub.UnsubscribeAll(ctx, subscriber)
  56. }
  57. func (b *EventBus) Observe(ctx context.Context, observe func(tmpubsub.Message) error, queries ...*tmquery.Query) error {
  58. return b.pubsub.Observe(ctx, observe, queries...)
  59. }
  60. func (b *EventBus) Publish(ctx context.Context, eventValue string, eventData types.EventData) error {
  61. tokens := strings.Split(types.EventTypeKey, ".")
  62. event := abci.Event{
  63. Type: tokens[0],
  64. Attributes: []abci.EventAttribute{
  65. {
  66. Key: tokens[1],
  67. Value: eventValue,
  68. },
  69. },
  70. }
  71. return b.pubsub.PublishWithEvents(ctx, eventData, []abci.Event{event})
  72. }
  73. func (b *EventBus) PublishEventNewBlock(ctx context.Context, data types.EventDataNewBlock) error {
  74. events := data.ResultFinalizeBlock.Events
  75. // add Tendermint-reserved new block event
  76. events = append(events, types.EventNewBlock)
  77. return b.pubsub.PublishWithEvents(ctx, data, events)
  78. }
  79. func (b *EventBus) PublishEventNewBlockHeader(ctx context.Context, data types.EventDataNewBlockHeader) error {
  80. // no explicit deadline for publishing events
  81. events := data.ResultFinalizeBlock.Events
  82. // add Tendermint-reserved new block header event
  83. events = append(events, types.EventNewBlockHeader)
  84. return b.pubsub.PublishWithEvents(ctx, data, events)
  85. }
  86. func (b *EventBus) PublishEventNewEvidence(ctx context.Context, evidence types.EventDataNewEvidence) error {
  87. return b.Publish(ctx, types.EventNewEvidenceValue, evidence)
  88. }
  89. func (b *EventBus) PublishEventVote(ctx context.Context, data types.EventDataVote) error {
  90. return b.Publish(ctx, types.EventVoteValue, data)
  91. }
  92. func (b *EventBus) PublishEventValidBlock(ctx context.Context, data types.EventDataRoundState) error {
  93. return b.Publish(ctx, types.EventValidBlockValue, data)
  94. }
  95. func (b *EventBus) PublishEventBlockSyncStatus(ctx context.Context, data types.EventDataBlockSyncStatus) error {
  96. return b.Publish(ctx, types.EventBlockSyncStatusValue, data)
  97. }
  98. func (b *EventBus) PublishEventStateSyncStatus(ctx context.Context, data types.EventDataStateSyncStatus) error {
  99. return b.Publish(ctx, types.EventStateSyncStatusValue, data)
  100. }
  101. // PublishEventTx publishes tx event with events from Result. Note it will add
  102. // predefined keys (EventTypeKey, TxHashKey). Existing events with the same keys
  103. // will be overwritten.
  104. func (b *EventBus) PublishEventTx(ctx context.Context, data types.EventDataTx) error {
  105. events := data.Result.Events
  106. // add Tendermint-reserved events
  107. events = append(events, types.EventTx)
  108. tokens := strings.Split(types.TxHashKey, ".")
  109. events = append(events, abci.Event{
  110. Type: tokens[0],
  111. Attributes: []abci.EventAttribute{
  112. {
  113. Key: tokens[1],
  114. Value: fmt.Sprintf("%X", types.Tx(data.Tx).Hash()),
  115. },
  116. },
  117. })
  118. tokens = strings.Split(types.TxHeightKey, ".")
  119. events = append(events, abci.Event{
  120. Type: tokens[0],
  121. Attributes: []abci.EventAttribute{
  122. {
  123. Key: tokens[1],
  124. Value: fmt.Sprintf("%d", data.Height),
  125. },
  126. },
  127. })
  128. return b.pubsub.PublishWithEvents(ctx, data, events)
  129. }
  130. func (b *EventBus) PublishEventNewRoundStep(ctx context.Context, data types.EventDataRoundState) error {
  131. return b.Publish(ctx, types.EventNewRoundStepValue, data)
  132. }
  133. func (b *EventBus) PublishEventTimeoutPropose(ctx context.Context, data types.EventDataRoundState) error {
  134. return b.Publish(ctx, types.EventTimeoutProposeValue, data)
  135. }
  136. func (b *EventBus) PublishEventTimeoutWait(ctx context.Context, data types.EventDataRoundState) error {
  137. return b.Publish(ctx, types.EventTimeoutWaitValue, data)
  138. }
  139. func (b *EventBus) PublishEventNewRound(ctx context.Context, data types.EventDataNewRound) error {
  140. return b.Publish(ctx, types.EventNewRoundValue, data)
  141. }
  142. func (b *EventBus) PublishEventCompleteProposal(ctx context.Context, data types.EventDataCompleteProposal) error {
  143. return b.Publish(ctx, types.EventCompleteProposalValue, data)
  144. }
  145. func (b *EventBus) PublishEventPolka(ctx context.Context, data types.EventDataRoundState) error {
  146. return b.Publish(ctx, types.EventPolkaValue, data)
  147. }
  148. func (b *EventBus) PublishEventRelock(ctx context.Context, data types.EventDataRoundState) error {
  149. return b.Publish(ctx, types.EventRelockValue, data)
  150. }
  151. func (b *EventBus) PublishEventLock(ctx context.Context, data types.EventDataRoundState) error {
  152. return b.Publish(ctx, types.EventLockValue, data)
  153. }
  154. func (b *EventBus) PublishEventValidatorSetUpdates(ctx context.Context, data types.EventDataValidatorSetUpdates) error {
  155. return b.Publish(ctx, types.EventValidatorSetUpdatesValue, data)
  156. }
  157. //-----------------------------------------------------------------------------
  158. // NopEventBus implements a types.BlockEventPublisher that discards all events.
  159. type NopEventBus struct{}
  160. func (NopEventBus) PublishEventNewBlock(context.Context, types.EventDataNewBlock) error {
  161. return nil
  162. }
  163. func (NopEventBus) PublishEventNewBlockHeader(context.Context, types.EventDataNewBlockHeader) error {
  164. return nil
  165. }
  166. func (NopEventBus) PublishEventNewEvidence(context.Context, types.EventDataNewEvidence) error {
  167. return nil
  168. }
  169. func (NopEventBus) PublishEventTx(context.Context, types.EventDataTx) error {
  170. return nil
  171. }
  172. func (NopEventBus) PublishEventValidatorSetUpdates(context.Context, types.EventDataValidatorSetUpdates) error {
  173. return nil
  174. }