neo-go/pkg/neorpc/events.go
Ekaterina Pavlova 5d514538cf rpc: add new header_of_added_block event subscription
New event is to notify the user about header's content by the moment
when block is stored (which happens after block's processing). This is
needed for proper Waiter work.

Closes #2751.

Signed-off-by: Ekaterina Pavlova <ekt@morphbits.io>
2023-12-22 15:14:01 +03:00

94 lines
2.4 KiB
Go

package neorpc
import (
"encoding/json"
"errors"
)
// EventID represents an event type happening on the chain.
type EventID byte
const (
// InvalidEventID is an invalid event id that is the default value of
// EventID. It's only used as an initial value similar to nil.
InvalidEventID EventID = iota
// BlockEventID is a `block_added` event.
BlockEventID
// TransactionEventID corresponds to the `transaction_added` event.
TransactionEventID
// NotificationEventID represents `notification_from_execution` events.
NotificationEventID
// ExecutionEventID is used for `transaction_executed` events.
ExecutionEventID
// NotaryRequestEventID is used for the `notary_request_event` event.
NotaryRequestEventID
// HeaderOfAddedBlockEventID is used for the `header_of_added_block` event.
HeaderOfAddedBlockEventID
// MissedEventID notifies user of missed events.
MissedEventID EventID = 255
)
// String is a good old Stringer implementation.
func (e EventID) String() string {
switch e {
case BlockEventID:
return "block_added"
case TransactionEventID:
return "transaction_added"
case NotificationEventID:
return "notification_from_execution"
case ExecutionEventID:
return "transaction_executed"
case NotaryRequestEventID:
return "notary_request_event"
case HeaderOfAddedBlockEventID:
return "header_of_added_block"
case MissedEventID:
return "event_missed"
default:
return "unknown"
}
}
// GetEventIDFromString converts an input string into an EventID if it's possible.
func GetEventIDFromString(s string) (EventID, error) {
switch s {
case "block_added":
return BlockEventID, nil
case "transaction_added":
return TransactionEventID, nil
case "notification_from_execution":
return NotificationEventID, nil
case "transaction_executed":
return ExecutionEventID, nil
case "notary_request_event":
return NotaryRequestEventID, nil
case "header_of_added_block":
return HeaderOfAddedBlockEventID, nil
case "event_missed":
return MissedEventID, nil
default:
return 255, errors.New("invalid stream name")
}
}
// MarshalJSON implements the json.Marshaler interface.
func (e EventID) MarshalJSON() ([]byte, error) {
return json.Marshal(e.String())
}
// UnmarshalJSON implements the json.Unmarshaler interface.
func (e *EventID) UnmarshalJSON(b []byte) error {
var s string
err := json.Unmarshal(b, &s)
if err != nil {
return err
}
id, err := GetEventIDFromString(s)
if err != nil {
return err
}
*e = id
return nil
}