This repository has been archived by the owner on May 13, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 346
/
event.go
95 lines (80 loc) · 1.75 KB
/
event.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
package exec
import (
"fmt"
"reflect"
)
var eventMessageType = reflect.TypeOf(&Event{}).String()
type EventType uint32
// Execution event types
const (
TypeUnknown EventType = iota
TypeCall
TypeLog
TypeAccountInput
TypeAccountOutput
TypeTxExecution
TypeBlockExecution
TypeGovernAccount
TypeBeginBlock
TypeBeginTx
TypeEnvelope
TypeEndTx
TypeEndBlock
)
var nameFromType = map[EventType]string{
TypeUnknown: "UnknownEvent",
TypeCall: "CallEvent",
TypeLog: "LogEvent",
TypeAccountInput: "AccountInputEvent",
TypeAccountOutput: "AccountOutputEvent",
TypeTxExecution: "TxExecutionEvent",
TypeBlockExecution: "BlockExecutionEvent",
TypeGovernAccount: "GovernAccountEvent",
TypeBeginBlock: "BeginBlockEvent",
TypeEndBlock: "EndBlockEvent",
}
var typeFromName = make(map[string]EventType)
func init() {
for t, n := range nameFromType {
typeFromName[n] = t
}
}
func EventTypeFromString(name string) EventType {
return typeFromName[name]
}
func (ev *Event) EventType() EventType {
return ev.Header.EventType
}
func (typ EventType) String() string {
name, ok := nameFromType[typ]
if ok {
return name
}
return "UnknownEventType"
}
func (typ EventType) MarshalText() ([]byte, error) {
return []byte(typ.String()), nil
}
func (typ *EventType) UnmarshalText(data []byte) error {
*typ = EventTypeFromString(string(data))
return nil
}
// Event
func (ev *Event) String() string {
return fmt.Sprintf("ExecutionEvent{%v: %s}", ev.Header.String(), ev.Body())
}
func (ev *Event) Body() string {
if ev.Input != nil {
return ev.Input.String()
}
if ev.Output != nil {
return ev.Output.String()
}
if ev.Log != nil {
return ev.Log.String()
}
if ev.Call != nil {
return ev.Call.String()
}
return "<empty>"
}