[#1546] morph/event: Export NotificationHandlerInfo fields

Hiding them achieves nothing, as the struct has no methods and is not
used concurrently.

Signed-off-by: Evgenii Stratonikov <e.stratonikov@yadro.com>
This commit is contained in:
Evgenii Stratonikov 2024-12-06 16:12:47 +03:00 committed by Evgenii Stratonikov
parent d0ce835fbf
commit b1614a284d
8 changed files with 75 additions and 110 deletions

View file

@ -4,6 +4,7 @@ import (
"context"
"github.com/nspcc-dev/neo-go/pkg/core/block"
"github.com/nspcc-dev/neo-go/pkg/util"
)
// Handler is an Event processing function.
@ -16,25 +17,10 @@ type BlockHandler func(context.Context, *block.Block)
// the parameters of the handler of particular
// contract event.
type NotificationHandlerInfo struct {
scriptHashWithType
parser NotificationParser
handlers []Handler
}
// SetParser is an event handler setter.
func (s *NotificationHandlerInfo) SetParser(p NotificationParser) {
s.parser = p
}
// AddHandler adds an event handler.
func (s *NotificationHandlerInfo) AddHandler(v Handler) {
s.handlers = append(s.handlers, v)
}
// Handler returns an event handler.
func (s NotificationHandlerInfo) Handlers() []Handler {
return s.handlers
Contract util.Uint160
Type Type
Parser NotificationParser
Handlers []Handler
}
// NotaryHandlerInfo is a structure that groups

View file

@ -443,18 +443,22 @@ func (l *listener) parseAndHandleNotary(ctx context.Context, nr *result.NotaryRe
// Ignores handlers of event without parser.
func (l *listener) RegisterNotificationHandler(hi NotificationHandlerInfo) {
log := l.log.With(
zap.String("contract", hi.ScriptHash().StringLE()),
zap.Stringer("event_type", hi.GetType()),
zap.String("contract", hi.Contract.StringLE()),
zap.Stringer("event_type", hi.Type),
)
// check if parser was set
l.mtx.Lock()
defer l.mtx.Unlock()
l.notificationParsers[hi.scriptHashWithType] = hi.parser
l.notificationHandlers[hi.scriptHashWithType] = append(
l.notificationHandlers[hi.scriptHashWithType],
hi.handlers...,
var k scriptHashWithType
k.hash = hi.Contract
k.typ = hi.Type
l.notificationParsers[k] = hi.Parser
l.notificationHandlers[k] = append(
l.notificationHandlers[k],
hi.Handlers...,
)
log.Debug(context.Background(), logs.EventRegisteredNewEventHandler)

View file

@ -39,23 +39,15 @@ func TestEventHandling(t *testing.T) {
blockHandled <- true
})
key := scriptHashWithType{
scriptHashValue: scriptHashValue{
hash: util.Uint160{100},
},
typeValue: typeValue{
typ: TypeFromString("notification type"),
},
}
notificationHandled := make(chan bool)
handledNotifications := make([]Event, 0)
l.RegisterNotificationHandler(NotificationHandlerInfo{
scriptHashWithType: key,
parser: func(cne *state.ContainedNotificationEvent) (Event, error) {
Contract: util.Uint160{100},
Type: TypeFromString("notification type"),
Parser: func(cne *state.ContainedNotificationEvent) (Event, error) {
return testNotificationEvent{source: cne}, nil
},
handlers: []Handler{
Handlers: []Handler{
func(_ context.Context, e Event) {
handledNotifications = append(handledNotifications, e)
notificationHandled <- true