[#505] ir/container: Refactor ListenerParsers and ListenerHandlers

Pre-allocate slices for a known number of elements. Use single `ParserInfo`
/ `HandlerInfo` variable in order to set Container contracts's address once
and change only values that differ between events.

Signed-off-by: Leonard Lyubich <leonard@nspcc.ru>
This commit is contained in:
Leonard Lyubich 2021-05-19 15:32:19 +03:00 committed by Alex Vanin
parent 372cba1fca
commit 4949f4b064

View file

@ -81,41 +81,46 @@ func New(p *Params) (*Processor, error) {
// ListenerParsers for the 'event.Listener' event producer. // ListenerParsers for the 'event.Listener' event producer.
func (cp *Processor) ListenerParsers() []event.ParserInfo { func (cp *Processor) ListenerParsers() []event.ParserInfo {
var parsers []event.ParserInfo var (
parsers = make([]event.ParserInfo, 0, 2)
// container put event p event.ParserInfo
put := event.ParserInfo{} )
put.SetType(putNotification)
put.SetScriptHash(cp.containerContract)
put.SetParser(containerEvent.ParsePut)
parsers = append(parsers, put)
// container del event p.SetScriptHash(cp.containerContract)
del := event.ParserInfo{}
del.SetType(deleteNotification) // container put
del.SetScriptHash(cp.containerContract) p.SetType(event.TypeFromString(putNotification))
del.SetParser(containerEvent.ParseDelete) p.SetParser(containerEvent.ParsePut)
parsers = append(parsers, del) parsers = append(parsers, p)
// container delete
p.SetType(event.TypeFromString(deleteNotification))
p.SetParser(containerEvent.ParseDelete)
parsers = append(parsers, p)
return parsers return parsers
} }
// ListenerHandlers for the 'event.Listener' event producer. // ListenerHandlers for the 'event.Listener' event producer.
func (cp *Processor) ListenerHandlers() []event.HandlerInfo { func (cp *Processor) ListenerHandlers() []event.HandlerInfo {
var handlers []event.HandlerInfo var (
handlers = make([]event.HandlerInfo, 0, 2)
// container put handler h event.HandlerInfo
put := event.HandlerInfo{} )
put.SetType(putNotification)
put.SetScriptHash(cp.containerContract)
put.SetHandler(cp.handlePut)
handlers = append(handlers, put)
del := event.HandlerInfo{} h.SetScriptHash(cp.containerContract)
del.SetType(deleteNotification)
del.SetScriptHash(cp.containerContract) // container put
del.SetHandler(cp.handleDelete) h.SetType(event.TypeFromString(putNotification))
handlers = append(handlers, del) h.SetHandler(cp.handlePut)
handlers = append(handlers, h)
// container delete
h.SetType(event.TypeFromString(deleteNotification))
h.SetHandler(cp.handleDelete)
handlers = append(handlers, h)
return handlers return handlers
} }