tutus-chain/pkg/services/rpcsrv/notification_comparator.go

66 lines
2.1 KiB
Go

package rpcsrv
import (
"github.com/tutus-one/tutus-chain/pkg/core/state"
"github.com/tutus-one/tutus-chain/pkg/neorpc"
"github.com/tutus-one/tutus-chain/pkg/neorpc/rpcevent"
"github.com/tutus-one/tutus-chain/pkg/util"
"github.com/tutus-one/tutus-chain/pkg/vm/vmstate"
)
// notificationEventComparator is a comparator for notification events.
type notificationEventComparator struct {
filter neorpc.SubscriptionFilter
}
// EventID returns the event ID for the notification event comparator.
func (s notificationEventComparator) EventID() neorpc.EventID {
return neorpc.NotificationEventID
}
// Filter returns the filter for the notification event comparator.
func (c notificationEventComparator) Filter() neorpc.SubscriptionFilter {
return c.filter
}
// notificationEventContainer is a container for a notification event.
type notificationEventContainer struct {
ntf *state.ContainedNotificationEvent
}
// EventID returns the event ID for the notification event container.
func (c notificationEventContainer) EventID() neorpc.EventID {
return neorpc.NotificationEventID
}
// EventPayload returns the payload for the notification event container.
func (c notificationEventContainer) EventPayload() any {
return c.ntf
}
func processAppExecResults(aers []state.AppExecResult, filter *neorpc.NotificationFilter) []state.ContainedNotificationEvent {
var notifications []state.ContainedNotificationEvent
for _, aer := range aers {
if aer.VMState == vmstate.Halt {
notifications = append(notifications, filterEvents(aer.Events, aer.Container, filter)...)
}
}
return notifications
}
func filterEvents(events []state.NotificationEvent, container util.Uint256, filter *neorpc.NotificationFilter) []state.ContainedNotificationEvent {
var notifications []state.ContainedNotificationEvent
for _, evt := range events {
ntf := state.ContainedNotificationEvent{
Container: container,
NotificationEvent: evt,
}
if filter == nil || rpcevent.Matches(&notificationEventComparator{
filter: *filter,
}, &notificationEventContainer{ntf: &ntf}) {
notifications = append(notifications, ntf)
}
}
return notifications
}