[#1546] morph/event: Export NotificationHandlerInfo fields
All checks were successful
Tests and linters / Run gofumpt (push) Successful in 1m26s
Tests and linters / Staticcheck (push) Successful in 3m21s
Vulncheck / Vulncheck (push) Successful in 3m36s
Build / Build Components (push) Successful in 3m58s
Tests and linters / Lint (push) Successful in 4m30s
Pre-commit hooks / Pre-commit (push) Successful in 4m35s
Tests and linters / Tests (push) Successful in 5m15s
Tests and linters / Tests with -race (push) Successful in 5m33s
Tests and linters / gopls check (push) Successful in 5m45s

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, subs map[event.Type][]event.Handler,
) { ) {
for typ, handlers := range subs { for typ, handlers := range subs {
hi := event.NotificationHandlerInfo{}
hi.SetType(typ)
hi.SetScriptHash(scHash)
p, ok := parsers[typ] p, ok := parsers[typ]
if !ok { if !ok {
panic(fmt.Sprintf("missing parser for event %s", typ)) panic(fmt.Sprintf("missing parser for event %s", typ))
} }
hi.SetParser(p) lis.RegisterNotificationHandler(event.NotificationHandlerInfo{
Contract: scHash,
for _, h := range handlers { Type: typ,
hi.AddHandler(h) Parser: p,
} Handlers: handlers,
lis.RegisterNotificationHandler(hi) })
} }
} }

View file

@ -90,17 +90,14 @@ func New(p *Params) (*Processor, error) {
// ListenerNotificationHandlers for the 'event.Listener' event producer. // ListenerNotificationHandlers for the 'event.Listener' event producer.
func (bp *Processor) ListenerNotificationHandlers() []event.NotificationHandlerInfo { func (bp *Processor) ListenerNotificationHandlers() []event.NotificationHandlerInfo {
var handlers []event.NotificationHandlerInfo return []event.NotificationHandlerInfo{
{
// lock handler Contract: bp.balanceSC,
lock := event.NotificationHandlerInfo{} Type: lockNotification,
lock.SetType(lockNotification) Parser: balanceEvent.ParseLock,
lock.SetScriptHash(bp.balanceSC) Handlers: []event.Handler{bp.handleLock},
lock.SetParser(balanceEvent.ParseLock) },
lock.AddHandler(bp.handleLock) }
handlers = append(handlers, lock)
return handlers
} }
// ListenerNotaryParsers for the 'event.Listener' event producer. // 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. // ListenerNotificationHandlers for the 'event.Listener' event producer.
func (np *Processor) ListenerNotificationHandlers() []event.NotificationHandlerInfo { func (np *Processor) ListenerNotificationHandlers() []event.NotificationHandlerInfo {
var ( return []event.NotificationHandlerInfo{
handlers = make([]event.NotificationHandlerInfo, 0, 6) {
Contract: np.frostfsContract,
h event.NotificationHandlerInfo Type: event.TypeFromString(depositNotification),
) Parser: frostfsEvent.ParseDeposit,
Handlers: []event.Handler{np.handleDeposit},
h.SetScriptHash(np.frostfsContract) },
{
// deposit handler Contract: np.frostfsContract,
h.SetType(event.TypeFromString(depositNotification)) Type: event.TypeFromString(withdrawNotification),
h.SetParser(frostfsEvent.ParseDeposit) Parser: frostfsEvent.ParseWithdraw,
h.AddHandler(np.handleDeposit) Handlers: []event.Handler{np.handleWithdraw},
handlers = append(handlers, h) },
{
// withdraw handler Contract: np.frostfsContract,
h.SetType(event.TypeFromString(withdrawNotification)) Type: event.TypeFromString(chequeNotification),
h.SetParser(frostfsEvent.ParseWithdraw) Parser: frostfsEvent.ParseCheque,
h.AddHandler(np.handleWithdraw) Handlers: []event.Handler{np.handleCheque},
handlers = append(handlers, h) },
{
// cheque handler Contract: np.frostfsContract,
h.SetType(event.TypeFromString(chequeNotification)) Type: event.TypeFromString(configNotification),
h.SetParser(frostfsEvent.ParseCheque) Parser: frostfsEvent.ParseConfig,
h.AddHandler(np.handleCheque) Handlers: []event.Handler{np.handleConfig},
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
} }
// ListenerNotaryParsers for the 'event.Listener' event producer. // 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. // ListenerNotificationHandlers for the 'event.Listener' event producer.
func (gp *Processor) ListenerNotificationHandlers() []event.NotificationHandlerInfo { func (gp *Processor) ListenerNotificationHandlers() []event.NotificationHandlerInfo {
var hi event.NotificationHandlerInfo return []event.NotificationHandlerInfo{
hi.SetScriptHash(gp.designate) {
hi.SetType(event.TypeFromString(native.DesignationEventName)) Contract: gp.designate,
hi.SetParser(rolemanagement.ParseDesignate) Type: event.TypeFromString(native.DesignationEventName),
hi.AddHandler(gp.HandleAlphabetSync) Parser: rolemanagement.ParseDesignate,
return []event.NotificationHandlerInfo{hi} Handlers: []event.Handler{gp.HandleAlphabetSync},
},
}
} }
// ListenerNotaryParsers for the 'event.Listener' event producer. // 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. // ListenerNotificationHandlers for the 'event.Listener' event producer.
func (np *Processor) ListenerNotificationHandlers() []event.NotificationHandlerInfo { func (np *Processor) ListenerNotificationHandlers() []event.NotificationHandlerInfo {
handlers := make([]event.NotificationHandlerInfo, 0, 3) return []event.NotificationHandlerInfo{
{
var i event.NotificationHandlerInfo Contract: np.netmapClient.ContractAddress(),
Type: newEpochNotification,
i.SetScriptHash(np.netmapClient.ContractAddress()) Parser: netmapEvent.ParseNewEpoch,
Handlers: []event.Handler{np.handleNewEpoch},
// new epoch handler },
i.SetType(newEpochNotification) }
i.SetParser(netmapEvent.ParseNewEpoch)
i.AddHandler(np.handleNewEpoch)
handlers = append(handlers, i)
return handlers
} }
// ListenerNotaryParsers for the 'event.Listener' event producer. // ListenerNotaryParsers for the 'event.Listener' event producer.

View file

@ -4,6 +4,7 @@ import (
"context" "context"
"github.com/nspcc-dev/neo-go/pkg/core/block" "github.com/nspcc-dev/neo-go/pkg/core/block"
"github.com/nspcc-dev/neo-go/pkg/util"
) )
// Handler is an Event processing function. // Handler is an Event processing function.
@ -16,25 +17,10 @@ type BlockHandler func(context.Context, *block.Block)
// the parameters of the handler of particular // the parameters of the handler of particular
// contract event. // contract event.
type NotificationHandlerInfo struct { type NotificationHandlerInfo struct {
scriptHashWithType Contract util.Uint160
Type Type
parser NotificationParser Parser NotificationParser
handlers []Handler 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
} }
// NotaryHandlerInfo is a structure that groups // 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. // Ignores handlers of event without parser.
func (l *listener) RegisterNotificationHandler(hi NotificationHandlerInfo) { func (l *listener) RegisterNotificationHandler(hi NotificationHandlerInfo) {
log := l.log.With( log := l.log.With(
zap.String("contract", hi.ScriptHash().StringLE()), zap.String("contract", hi.Contract.StringLE()),
zap.Stringer("event_type", hi.GetType()), zap.Stringer("event_type", hi.Type),
) )
// check if parser was set // check if parser was set
l.mtx.Lock() l.mtx.Lock()
defer l.mtx.Unlock() defer l.mtx.Unlock()
l.notificationParsers[hi.scriptHashWithType] = hi.parser var k scriptHashWithType
l.notificationHandlers[hi.scriptHashWithType] = append( k.hash = hi.Contract
l.notificationHandlers[hi.scriptHashWithType], k.typ = hi.Type
hi.handlers...,
l.notificationParsers[k] = hi.Parser
l.notificationHandlers[k] = append(
l.notificationHandlers[k],
hi.Handlers...,
) )
log.Debug(context.Background(), logs.EventRegisteredNewEventHandler) log.Debug(context.Background(), logs.EventRegisteredNewEventHandler)

View file

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