[#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

@ -223,21 +223,17 @@ func registerNotificationHandlers(scHash util.Uint160, lis event.Listener, parse
subs map[event.Type][]event.Handler,
) {
for typ, handlers := range subs {
hi := event.NotificationHandlerInfo{}
hi.SetType(typ)
hi.SetScriptHash(scHash)
p, ok := parsers[typ]
if !ok {
panic(fmt.Sprintf("missing parser for event %s", typ))
}
hi.SetParser(p)
for _, h := range handlers {
hi.AddHandler(h)
}
lis.RegisterNotificationHandler(hi)
lis.RegisterNotificationHandler(event.NotificationHandlerInfo{
Contract: scHash,
Type: typ,
Parser: p,
Handlers: handlers,
})
}
}

View file

@ -90,17 +90,14 @@ func New(p *Params) (*Processor, error) {
// ListenerNotificationHandlers for the 'event.Listener' event producer.
func (bp *Processor) ListenerNotificationHandlers() []event.NotificationHandlerInfo {
var handlers []event.NotificationHandlerInfo
// lock handler
lock := event.NotificationHandlerInfo{}
lock.SetType(lockNotification)
lock.SetScriptHash(bp.balanceSC)
lock.SetParser(balanceEvent.ParseLock)
lock.AddHandler(bp.handleLock)
handlers = append(handlers, lock)
return handlers
return []event.NotificationHandlerInfo{
{
Contract: bp.balanceSC,
Type: lockNotification,
Parser: balanceEvent.ParseLock,
Handlers: []event.Handler{bp.handleLock},
},
}
}
// ListenerNotaryParsers for the 'event.Listener' event producer.

View file

@ -144,39 +144,32 @@ func New(p *Params) (*Processor, error) {
// ListenerNotificationHandlers for the 'event.Listener' event producer.
func (np *Processor) ListenerNotificationHandlers() []event.NotificationHandlerInfo {
var (
handlers = make([]event.NotificationHandlerInfo, 0, 6)
h event.NotificationHandlerInfo
)
h.SetScriptHash(np.frostfsContract)
// deposit handler
h.SetType(event.TypeFromString(depositNotification))
h.SetParser(frostfsEvent.ParseDeposit)
h.AddHandler(np.handleDeposit)
handlers = append(handlers, h)
// withdraw handler
h.SetType(event.TypeFromString(withdrawNotification))
h.SetParser(frostfsEvent.ParseWithdraw)
h.AddHandler(np.handleWithdraw)
handlers = append(handlers, h)
// cheque handler
h.SetType(event.TypeFromString(chequeNotification))
h.SetParser(frostfsEvent.ParseCheque)
h.AddHandler(np.handleCheque)
handlers = append(handlers, h)
// config handler
h.SetType(event.TypeFromString(configNotification))
h.SetParser(frostfsEvent.ParseConfig)
h.AddHandler(np.handleConfig)
handlers = append(handlers, h)
return handlers
return []event.NotificationHandlerInfo{
{
Contract: np.frostfsContract,
Type: event.TypeFromString(depositNotification),
Parser: frostfsEvent.ParseDeposit,
Handlers: []event.Handler{np.handleDeposit},
},
{
Contract: np.frostfsContract,
Type: event.TypeFromString(withdrawNotification),
Parser: frostfsEvent.ParseWithdraw,
Handlers: []event.Handler{np.handleWithdraw},
},
{
Contract: np.frostfsContract,
Type: event.TypeFromString(chequeNotification),
Parser: frostfsEvent.ParseCheque,
Handlers: []event.Handler{np.handleCheque},
},
{
Contract: np.frostfsContract,
Type: event.TypeFromString(configNotification),
Parser: frostfsEvent.ParseConfig,
Handlers: []event.Handler{np.handleConfig},
},
}
}
// ListenerNotaryParsers for the 'event.Listener' event producer.

View file

@ -157,12 +157,14 @@ func New(p *Params) (*Processor, error) {
// ListenerNotificationHandlers for the 'event.Listener' event producer.
func (gp *Processor) ListenerNotificationHandlers() []event.NotificationHandlerInfo {
var hi event.NotificationHandlerInfo
hi.SetScriptHash(gp.designate)
hi.SetType(event.TypeFromString(native.DesignationEventName))
hi.SetParser(rolemanagement.ParseDesignate)
hi.AddHandler(gp.HandleAlphabetSync)
return []event.NotificationHandlerInfo{hi}
return []event.NotificationHandlerInfo{
{
Contract: gp.designate,
Type: event.TypeFromString(native.DesignationEventName),
Parser: rolemanagement.ParseDesignate,
Handlers: []event.Handler{gp.HandleAlphabetSync},
},
}
}
// ListenerNotaryParsers for the 'event.Listener' event producer.

View file

@ -163,19 +163,14 @@ func New(p *Params) (*Processor, error) {
// ListenerNotificationHandlers for the 'event.Listener' event producer.
func (np *Processor) ListenerNotificationHandlers() []event.NotificationHandlerInfo {
handlers := make([]event.NotificationHandlerInfo, 0, 3)
var i event.NotificationHandlerInfo
i.SetScriptHash(np.netmapClient.ContractAddress())
// new epoch handler
i.SetType(newEpochNotification)
i.SetParser(netmapEvent.ParseNewEpoch)
i.AddHandler(np.handleNewEpoch)
handlers = append(handlers, i)
return handlers
return []event.NotificationHandlerInfo{
{
Contract: np.netmapClient.ContractAddress(),
Type: newEpochNotification,
Parser: netmapEvent.ParseNewEpoch,
Handlers: []event.Handler{np.handleNewEpoch},
},
}
}
// ListenerNotaryParsers for the 'event.Listener' event producer.

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