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

8 years ago
8 years ago
  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. abci "github.com/tendermint/abci/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. Data []byte `json:"data"`
  65. Log string `json:"log"`
  66. Code abci.CodeType `json:"code"`
  67. Error string `json:"error"` // this is redundant information for now
  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. Vote *Vote
  79. }
  80. func (_ EventDataNewBlock) AssertIsTMEventData() {}
  81. func (_ EventDataNewBlockHeader) AssertIsTMEventData() {}
  82. func (_ EventDataTx) AssertIsTMEventData() {}
  83. func (_ EventDataRoundState) AssertIsTMEventData() {}
  84. func (_ EventDataVote) AssertIsTMEventData() {}
  85. //----------------------------------------
  86. // Wrappers for type safety
  87. type Fireable interface {
  88. events.Fireable
  89. }
  90. type Eventable interface {
  91. SetEventSwitch(EventSwitch)
  92. }
  93. type EventSwitch interface {
  94. events.EventSwitch
  95. }
  96. type EventCache interface {
  97. Fireable
  98. Flush()
  99. }
  100. func NewEventSwitch() EventSwitch {
  101. return events.NewEventSwitch()
  102. }
  103. func NewEventCache(evsw EventSwitch) EventCache {
  104. return events.NewEventCache(evsw)
  105. }
  106. // All events should be based on this FireEvent to ensure they are TMEventData
  107. func fireEvent(fireable events.Fireable, event string, data TMEventData) {
  108. if fireable != nil {
  109. fireable.FireEvent(event, data)
  110. }
  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. }