github.com/hyperledger/burrow@v0.34.5-0.20220512172541-77f09336001d/execution/exec/event.go (about)

     1  package exec
     2  
     3  import (
     4  	"fmt"
     5  	"reflect"
     6  
     7  	"github.com/hyperledger/burrow/event"
     8  	"github.com/hyperledger/burrow/event/query"
     9  )
    10  
    11  var eventMessageType = reflect.TypeOf(&Event{}).String()
    12  
    13  type EventType uint32
    14  
    15  // Execution event types
    16  const (
    17  	TypeUnknown EventType = iota
    18  	TypeCall
    19  	TypeLog
    20  	TypeAccountInput
    21  	TypeAccountOutput
    22  	TypeTxExecution
    23  	TypeBlockExecution
    24  	TypeGovernAccount
    25  	TypeBeginBlock
    26  	TypeBeginTx
    27  	TypeEnvelope
    28  	TypeEndTx
    29  	TypeEndBlock
    30  	TypePrint
    31  )
    32  
    33  var nameFromType = map[EventType]string{
    34  	TypeUnknown:        "UnknownEvent",
    35  	TypeCall:           "CallEvent",
    36  	TypeLog:            "LogEvent",
    37  	TypeAccountInput:   "AccountInputEvent",
    38  	TypeAccountOutput:  "AccountOutputEvent",
    39  	TypeTxExecution:    "TxExecutionEvent",
    40  	TypeBlockExecution: "BlockExecutionEvent",
    41  	TypeGovernAccount:  "GovernAccountEvent",
    42  	TypeBeginBlock:     "BeginBlockEvent",
    43  	TypeEndBlock:       "EndBlockEvent",
    44  }
    45  
    46  var typeFromName = make(map[string]EventType)
    47  
    48  func init() {
    49  	for t, n := range nameFromType {
    50  		typeFromName[n] = t
    51  	}
    52  }
    53  
    54  func EventTypeFromString(name string) EventType {
    55  	return typeFromName[name]
    56  }
    57  
    58  func (ev *Event) EventType() EventType {
    59  	return ev.Header.EventType
    60  }
    61  
    62  func (typ EventType) String() string {
    63  	name, ok := nameFromType[typ]
    64  	if ok {
    65  		return name
    66  	}
    67  	return "UnknownEventType"
    68  }
    69  
    70  func (typ EventType) MarshalText() ([]byte, error) {
    71  	return []byte(typ.String()), nil
    72  }
    73  
    74  func (typ *EventType) UnmarshalText(data []byte) error {
    75  	*typ = EventTypeFromString(string(data))
    76  	return nil
    77  }
    78  
    79  // Event
    80  
    81  func (ev *Event) String() string {
    82  	return fmt.Sprintf("ExecutionEvent{%v: %s}", ev.Header.String(), ev.Body())
    83  }
    84  
    85  func (ev *Event) Body() string {
    86  	if ev.Input != nil {
    87  		return ev.Input.String()
    88  	}
    89  	if ev.Output != nil {
    90  		return ev.Output.String()
    91  	}
    92  	if ev.Log != nil {
    93  		return ev.Log.String()
    94  	}
    95  	if ev.Call != nil {
    96  		return ev.Call.String()
    97  	}
    98  	return "<empty>"
    99  }
   100  
   101  func (ev *Event) Get(key string) (value interface{}, ok bool) {
   102  	switch key {
   103  	case event.MessageTypeKey:
   104  		return eventMessageType, true
   105  	}
   106  	if ev == nil {
   107  		return nil, false
   108  	}
   109  	v, ok := ev.Log.Get(key)
   110  	if ok {
   111  		return v, true
   112  	}
   113  	v, ok = query.GetReflect(reflect.ValueOf(ev.Header), key)
   114  	if ok {
   115  		return v, true
   116  	}
   117  	return query.GetReflect(reflect.ValueOf(ev), key)
   118  }