github.com/kaleido-io/firefly@v0.0.0-20210622132723-8b4b6aacb971/pkg/fftypes/event.go (about)

     1  // Copyright © 2021 Kaleido, Inc.
     2  //
     3  // SPDX-License-Identifier: Apache-2.0
     4  //
     5  // Licensed under the Apache License, Version 2.0 (the "License");
     6  // you may not use this file except in compliance with the License.
     7  // You may obtain a copy of the License at
     8  //
     9  //     http://www.apache.org/licenses/LICENSE-2.0
    10  //
    11  // Unless required by applicable law or agreed to in writing, software
    12  // distributed under the License is distributed on an "AS IS" BASIS,
    13  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    14  // See the License for the specific language governing permissions and
    15  // limitations under the License.
    16  
    17  package fftypes
    18  
    19  // EventType indicates what the event means, as well as what the Reference in the event refers to
    20  type EventType = LowerCasedType
    21  
    22  const (
    23  	// EventTypeMessageConfirmed is the most important event type in the system. This means a message and all of its data
    24  	// is available for processing by an application. Most applications only need to listen to this event type
    25  	EventTypeMessageConfirmed EventType = "message_confirmed"
    26  	// EventTypeMessageInvalid occurs if a message is received and confirmed from a sequencing perspective, but is invalid
    27  	EventTypeMessageInvalid EventType = "message_invalid"
    28  	// EventTypeNamespaceConfirmed occurs when a new namespace is ready for use (on the namespace itself)
    29  	EventTypeNamespaceConfirmed EventType = "namespace_confirmed"
    30  	// EventTypeDatatypeConfirmed occurs when a new datatype is ready for use (on the namespace of the datatype)
    31  	EventTypeDatatypeConfirmed EventType = "datatype_confirmed"
    32  	// EventTypeGroupConfirmed occurs when a new group is ready to use (on the namespace of the group, on all group participants)
    33  	EventTypeGroupConfirmed EventType = "group_confirmed"
    34  )
    35  
    36  // Event is an activity in the system, delivered reliably to applications, that indicates something has happened in the network
    37  type Event struct {
    38  	ID        *UUID     `json:"id"`
    39  	Sequence  int64     `json:"sequence"`
    40  	Type      EventType `json:"type"`
    41  	Namespace string    `json:"namespace"`
    42  	Reference *UUID     `json:"reference"`
    43  	Group     *Bytes32  `json:"group,omitempty"`
    44  	Created   *FFTime   `json:"created"`
    45  }
    46  
    47  // EventDelivery adds the referred object to an event, as well as details of the subscription that caused the event to
    48  // be dispatched to an applciation.
    49  type EventDelivery struct {
    50  	Event
    51  	Subscription SubscriptionRef `json:"subscription"`
    52  	Message      *Message        `json:"message,omitempty"`
    53  	Data         *DataRef        `json:"data,omitempty"`
    54  }
    55  
    56  // EventDeliveryResponse is the payload an application sends back, to confirm it has accepted (or rejected) the event and as such
    57  // does not need to receive it again.
    58  type EventDeliveryResponse struct {
    59  	ID           *UUID           `json:"id"`
    60  	Rejected     bool            `json:"rejected,omitempty"`
    61  	Info         string          `json:"info,omitempty"`
    62  	Subscription SubscriptionRef `json:"subscription"`
    63  }
    64  
    65  func NewEvent(t EventType, ns string, ref *UUID, group *Bytes32) *Event {
    66  	return &Event{
    67  		ID:        NewUUID(),
    68  		Type:      t,
    69  		Namespace: ns,
    70  		Reference: ref,
    71  		Group:     group,
    72  		Created:   Now(),
    73  	}
    74  }
    75  
    76  func (e *Event) LocalSequence() int64 {
    77  	return e.Sequence
    78  }