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.

78 lines
2.2 KiB

  1. package eventlog
  2. import (
  3. "strings"
  4. abci "github.com/tendermint/tendermint/abci/types"
  5. "github.com/tendermint/tendermint/internal/eventlog/cursor"
  6. "github.com/tendermint/tendermint/types"
  7. )
  8. // Cached constants for the pieces of reserved event names.
  9. var (
  10. tmTypeTag string
  11. tmTypeKey string
  12. )
  13. func init() {
  14. parts := strings.SplitN(types.EventTypeKey, ".", 2)
  15. if len(parts) != 2 {
  16. panic("invalid event type key: " + types.EventTypeKey)
  17. }
  18. tmTypeTag = parts[0]
  19. tmTypeKey = parts[1]
  20. }
  21. // ABCIEventer is an optional extension interface that may be implemented by
  22. // event data types, to expose ABCI metadata to the event log. If an event item
  23. // does not implement this interface, it is presumed to have no ABCI metadata.
  24. type ABCIEventer interface {
  25. // Return any ABCI events metadata the receiver contains.
  26. // The reported slice must not contain a type (tm.event) record, since some
  27. // events share the same structure among different event types.
  28. ABCIEvents() []abci.Event
  29. }
  30. // An Item is a single event item.
  31. type Item struct {
  32. Cursor cursor.Cursor
  33. Type string
  34. Data types.EventData
  35. Events []abci.Event
  36. }
  37. // newItem constructs a new item with the specified cursor, type, and data.
  38. func newItem(cursor cursor.Cursor, etype string, data types.EventData) *Item {
  39. return &Item{Cursor: cursor, Type: etype, Data: data, Events: makeEvents(etype, data)}
  40. }
  41. // makeEvents returns a slice of ABCI events comprising the type tag along with
  42. // any internal events exported by the data value.
  43. func makeEvents(etype string, data types.EventData) []abci.Event {
  44. base := []abci.Event{{
  45. Type: tmTypeTag,
  46. Attributes: []abci.EventAttribute{{
  47. Key: tmTypeKey, Value: etype,
  48. }},
  49. }}
  50. if evt, ok := data.(ABCIEventer); ok {
  51. return append(base, evt.ABCIEvents()...)
  52. }
  53. return base
  54. }
  55. // FindType reports whether events contains a tm.event event, and if so returns
  56. // its value, which is the type of the underlying event item.
  57. func FindType(events []abci.Event) (string, bool) {
  58. for _, evt := range events {
  59. if evt.Type != tmTypeTag {
  60. continue
  61. }
  62. for _, attr := range evt.Attributes {
  63. if attr.Key == tmTypeKey {
  64. return attr.Value, true
  65. }
  66. }
  67. }
  68. return "", false
  69. }