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.

199 lines
5.8 KiB

  1. package types
  2. import (
  3. // for registering TMEventData as events.EventData
  4. . "github.com/tendermint/go-common"
  5. "github.com/tendermint/go-events"
  6. "github.com/tendermint/go-wire"
  7. tmsp "github.com/tendermint/tmsp/types"
  8. )
  9. // Functions to generate eventId strings
  10. // Reserved
  11. func EventStringBond() string { return "Bond" }
  12. func EventStringUnbond() string { return "Unbond" }
  13. func EventStringRebond() string { return "Rebond" }
  14. func EventStringDupeout() string { return "Dupeout" }
  15. func EventStringFork() string { return "Fork" }
  16. func EventStringTx(tx Tx) string { return Fmt("Tx:%X", tx.Hash()) }
  17. func EventStringNewBlock() string { return "NewBlock" }
  18. func EventStringNewBlockHeader() string { return "NewBlockHeader" }
  19. func EventStringNewRound() string { return "NewRound" }
  20. func EventStringNewRoundStep() string { return "NewRoundStep" }
  21. func EventStringTimeoutPropose() string { return "TimeoutPropose" }
  22. func EventStringCompleteProposal() string { return "CompleteProposal" }
  23. func EventStringPolka() string { return "Polka" }
  24. func EventStringUnlock() string { return "Unlock" }
  25. func EventStringLock() string { return "Lock" }
  26. func EventStringRelock() string { return "Relock" }
  27. func EventStringTimeoutWait() string { return "TimeoutWait" }
  28. func EventStringVote() string { return "Vote" }
  29. //----------------------------------------
  30. // implements events.EventData
  31. type TMEventData interface {
  32. events.EventData
  33. AssertIsTMEventData()
  34. }
  35. const (
  36. EventDataTypeNewBlock = byte(0x01)
  37. EventDataTypeFork = byte(0x02)
  38. EventDataTypeTx = byte(0x03)
  39. EventDataTypeNewBlockHeader = byte(0x04)
  40. EventDataTypeRoundState = byte(0x11)
  41. EventDataTypeVote = byte(0x12)
  42. )
  43. var _ = wire.RegisterInterface(
  44. struct{ TMEventData }{},
  45. wire.ConcreteType{EventDataNewBlock{}, EventDataTypeNewBlock},
  46. wire.ConcreteType{EventDataNewBlockHeader{}, EventDataTypeNewBlockHeader},
  47. // wire.ConcreteType{EventDataFork{}, EventDataTypeFork },
  48. wire.ConcreteType{EventDataTx{}, EventDataTypeTx},
  49. wire.ConcreteType{EventDataRoundState{}, EventDataTypeRoundState},
  50. wire.ConcreteType{EventDataVote{}, EventDataTypeVote},
  51. )
  52. // Most event messages are basic types (a block, a transaction)
  53. // but some (an input to a call tx or a receive) are more exotic
  54. type EventDataNewBlock struct {
  55. Block *Block `json:"block"`
  56. }
  57. // light weight event for benchmarking
  58. type EventDataNewBlockHeader struct {
  59. Header *Header `json:"header"`
  60. }
  61. // All txs fire EventDataTx
  62. type EventDataTx struct {
  63. Tx Tx `json:"tx"`
  64. Result []byte `json:"result"`
  65. Log string `json:"log"`
  66. Code tmsp.CodeType `json:"code"`
  67. Error string `json:"error"`
  68. }
  69. // NOTE: This goes into the replay WAL
  70. type EventDataRoundState struct {
  71. Height int `json:"height"`
  72. Round int `json:"round"`
  73. Step string `json:"step"`
  74. // private, not exposed to websockets
  75. RoundState interface{} `json:"-"`
  76. }
  77. type EventDataVote struct {
  78. Index int
  79. Address []byte
  80. Vote *Vote
  81. }
  82. func (_ EventDataNewBlock) AssertIsTMEventData() {}
  83. func (_ EventDataNewBlockHeader) AssertIsTMEventData() {}
  84. func (_ EventDataTx) AssertIsTMEventData() {}
  85. func (_ EventDataRoundState) AssertIsTMEventData() {}
  86. func (_ EventDataVote) AssertIsTMEventData() {}
  87. //----------------------------------------
  88. // Wrappers for type safety
  89. type Fireable interface {
  90. events.Fireable
  91. }
  92. type Eventable interface {
  93. SetEventSwitch(EventSwitch)
  94. }
  95. type EventSwitch interface {
  96. events.EventSwitch
  97. }
  98. type EventCache interface {
  99. Fireable
  100. Flush()
  101. }
  102. func NewEventSwitch() EventSwitch {
  103. return events.NewEventSwitch()
  104. }
  105. func NewEventCache(evsw EventSwitch) EventCache {
  106. return events.NewEventCache(evsw)
  107. }
  108. // All events should be based on this FireEvent to ensure they are TMEventData
  109. func fireEvent(fireable events.Fireable, event string, data TMEventData) {
  110. fireable.FireEvent(event, data)
  111. }
  112. func AddListenerForEvent(evsw EventSwitch, id, event string, cb func(data TMEventData)) {
  113. evsw.AddListenerForEvent(id, event, func(data events.EventData) {
  114. cb(data.(TMEventData))
  115. })
  116. }
  117. //--- block, tx, and vote events
  118. func FireEventNewBlock(fireable events.Fireable, block EventDataNewBlock) {
  119. fireEvent(fireable, EventStringNewBlock(), block)
  120. }
  121. func FireEventNewBlockHeader(fireable events.Fireable, header EventDataNewBlockHeader) {
  122. fireEvent(fireable, EventStringNewBlockHeader(), header)
  123. }
  124. func FireEventVote(fireable events.Fireable, vote EventDataVote) {
  125. fireEvent(fireable, EventStringVote(), vote)
  126. }
  127. func FireEventTx(fireable events.Fireable, tx EventDataTx) {
  128. fireEvent(fireable, EventStringTx(tx.Tx), tx)
  129. }
  130. //--- EventDataRoundState events
  131. func FireEventNewRoundStep(fireable events.Fireable, rs EventDataRoundState) {
  132. fireEvent(fireable, EventStringNewRoundStep(), rs)
  133. }
  134. func FireEventTimeoutPropose(fireable events.Fireable, rs EventDataRoundState) {
  135. fireEvent(fireable, EventStringTimeoutPropose(), rs)
  136. }
  137. func FireEventTimeoutWait(fireable events.Fireable, rs EventDataRoundState) {
  138. fireEvent(fireable, EventStringTimeoutWait(), rs)
  139. }
  140. func FireEventNewRound(fireable events.Fireable, rs EventDataRoundState) {
  141. fireEvent(fireable, EventStringNewRound(), rs)
  142. }
  143. func FireEventCompleteProposal(fireable events.Fireable, rs EventDataRoundState) {
  144. fireEvent(fireable, EventStringCompleteProposal(), rs)
  145. }
  146. func FireEventPolka(fireable events.Fireable, rs EventDataRoundState) {
  147. fireEvent(fireable, EventStringPolka(), rs)
  148. }
  149. func FireEventUnlock(fireable events.Fireable, rs EventDataRoundState) {
  150. fireEvent(fireable, EventStringUnlock(), rs)
  151. }
  152. func FireEventRelock(fireable events.Fireable, rs EventDataRoundState) {
  153. fireEvent(fireable, EventStringRelock(), rs)
  154. }
  155. func FireEventLock(fireable events.Fireable, rs EventDataRoundState) {
  156. fireEvent(fireable, EventStringLock(), rs)
  157. }