From ba77bb44e406a936744521d919772bd77d7ae21a Mon Sep 17 00:00:00 2001 From: Pavel Karpy Date: Wed, 8 Sep 2021 11:28:41 +0300 Subject: [PATCH] [#807] ir: Merge `ContractProcessor` and `NotaryContractProcessor` interfaces Signed-off-by: Pavel Karpy --- pkg/innerring/bindings.go | 30 ++++++----------- pkg/innerring/processors/alphabet/handlers.go | 10 +++--- .../processors/alphabet/process_emit.go | 32 +++++++++---------- .../processors/alphabet/processor.go | 16 ++++++++-- pkg/innerring/processors/balance/processor.go | 10 ++++++ .../processors/governance/processor.go | 10 ++++++ pkg/innerring/processors/neofs/processor.go | 10 ++++++ pkg/innerring/processors/netmap/processor.go | 10 ++++++ .../processors/reputation/processor.go | 10 ++++++ 9 files changed, 94 insertions(+), 44 deletions(-) diff --git a/pkg/innerring/bindings.go b/pkg/innerring/bindings.go index 1c930cdf3..d6a7d7a81 100644 --- a/pkg/innerring/bindings.go +++ b/pkg/innerring/bindings.go @@ -10,41 +10,31 @@ type ( ContractProcessor interface { ListenerNotificationParsers() []event.NotificationParserInfo ListenerNotificationHandlers() []event.NotificationHandlerInfo - TimersHandlers() []event.NotificationHandlerInfo - } - - // NotaryContractProcessor interface defines function for binding notary event - // producers such as event.Listener with contract processor. - // - // This interface is optional for contract processor. If contract processor - // supports notary event handling, it should implement both ContractProcessor - // and NotaryContractProcessor interfaces. - NotaryContractProcessor interface { ListenerNotaryParsers() []event.NotaryParserInfo ListenerNotaryHandlers() []event.NotaryHandlerInfo + TimersHandlers() []event.NotificationHandlerInfo } ) func connectListenerWithProcessor(l event.Listener, p ContractProcessor) { - // register parsers + // register notification parsers for _, parser := range p.ListenerNotificationParsers() { l.SetNotificationParser(parser) } - // register handlers + // register notification handlers for _, handler := range p.ListenerNotificationHandlers() { l.RegisterNotificationHandler(handler) } - // add notary handlers if processor supports it - if notaryProcessor, ok := p.(NotaryContractProcessor); ok { - for _, notaryParser := range notaryProcessor.ListenerNotaryParsers() { - l.SetNotaryParser(notaryParser) - } + // register notary parsers + for _, notaryParser := range p.ListenerNotaryParsers() { + l.SetNotaryParser(notaryParser) + } - for _, notaryHandler := range notaryProcessor.ListenerNotaryHandlers() { - l.RegisterNotaryHandler(notaryHandler) - } + // register notary handlers + for _, notaryHandler := range p.ListenerNotaryHandlers() { + l.RegisterNotaryHandler(notaryHandler) } } diff --git a/pkg/innerring/processors/alphabet/handlers.go b/pkg/innerring/processors/alphabet/handlers.go index 7c2e34729..b7d2e34e6 100644 --- a/pkg/innerring/processors/alphabet/handlers.go +++ b/pkg/innerring/processors/alphabet/handlers.go @@ -6,16 +6,16 @@ import ( "go.uber.org/zap" ) -func (np *Processor) HandleGasEmission(ev event.Event) { +func (ap *Processor) HandleGasEmission(ev event.Event) { _ = ev.(timers.NewAlphabetEmitTick) - np.log.Info("tick", zap.String("type", "alphabet gas emit")) + ap.log.Info("tick", zap.String("type", "alphabet gas emit")) // send event to the worker pool - err := np.pool.Submit(func() { np.processEmit() }) + err := ap.pool.Submit(func() { ap.processEmit() }) if err != nil { // there system can be moved into controlled degradation stage - np.log.Warn("alphabet processor worker pool drained", - zap.Int("capacity", np.pool.Cap())) + ap.log.Warn("alphabet processor worker pool drained", + zap.Int("capacity", ap.pool.Cap())) } } diff --git a/pkg/innerring/processors/alphabet/process_emit.go b/pkg/innerring/processors/alphabet/process_emit.go index fa880e6af..705643ba5 100644 --- a/pkg/innerring/processors/alphabet/process_emit.go +++ b/pkg/innerring/processors/alphabet/process_emit.go @@ -10,39 +10,39 @@ import ( const emitMethod = "emit" -func (np *Processor) processEmit() { - index := np.irList.AlphabetIndex() +func (ap *Processor) processEmit() { + index := ap.irList.AlphabetIndex() if index < 0 { - np.log.Info("non alphabet mode, ignore gas emission event") + ap.log.Info("non alphabet mode, ignore gas emission event") return } - contract, ok := np.alphabetContracts.GetByIndex(index) + contract, ok := ap.alphabetContracts.GetByIndex(index) if !ok { - np.log.Debug("node is out of alphabet range, ignore gas emission event", + ap.log.Debug("node is out of alphabet range, ignore gas emission event", zap.Int("index", index)) return } // there is no signature collecting, so we don't need extra fee - err := np.morphClient.Invoke(contract, 0, emitMethod) + err := ap.morphClient.Invoke(contract, 0, emitMethod) if err != nil { - np.log.Warn("can't invoke alphabet emit method") + ap.log.Warn("can't invoke alphabet emit method") return } - if np.storageEmission == 0 { - np.log.Info("storage node emission is off") + if ap.storageEmission == 0 { + ap.log.Info("storage node emission is off") return } - networkMap, err := np.netmapClient.Snapshot() + networkMap, err := ap.netmapClient.Snapshot() if err != nil { - np.log.Warn("can't get netmap snapshot to emit gas to storage nodes", + ap.log.Warn("can't get netmap snapshot to emit gas to storage nodes", zap.String("error", err.Error())) return @@ -50,27 +50,27 @@ func (np *Processor) processEmit() { ln := len(networkMap.Nodes) if ln == 0 { - np.log.Debug("empty network map, do not emit gas") + ap.log.Debug("empty network map, do not emit gas") return } - gasPerNode := fixedn.Fixed8(np.storageEmission / uint64(ln)) + gasPerNode := fixedn.Fixed8(ap.storageEmission / uint64(ln)) for i := range networkMap.Nodes { keyBytes := networkMap.Nodes[i].PublicKey() key, err := keys.NewPublicKeyFromBytes(keyBytes, elliptic.P256()) if err != nil { - np.log.Warn("can't convert node public key to address", + ap.log.Warn("can't convert node public key to address", zap.String("error", err.Error())) continue } - err = np.morphClient.TransferGas(key.GetScriptHash(), gasPerNode) + err = ap.morphClient.TransferGas(key.GetScriptHash(), gasPerNode) if err != nil { - np.log.Warn("can't transfer gas", + ap.log.Warn("can't transfer gas", zap.String("receiver", key.Address()), zap.Int64("amount", int64(gasPerNode)), zap.String("error", err.Error()), diff --git a/pkg/innerring/processors/alphabet/processor.go b/pkg/innerring/processors/alphabet/processor.go index e4f0212dd..629db57a9 100644 --- a/pkg/innerring/processors/alphabet/processor.go +++ b/pkg/innerring/processors/alphabet/processor.go @@ -83,16 +83,26 @@ func New(p *Params) (*Processor, error) { } // ListenerNotificationParsers for the 'event.Listener' event producer. -func (np *Processor) ListenerNotificationParsers() []event.NotificationParserInfo { +func (ap *Processor) ListenerNotificationParsers() []event.NotificationParserInfo { return nil } // ListenerNotificationHandlers for the 'event.Listener' event producer. -func (np *Processor) ListenerNotificationHandlers() []event.NotificationHandlerInfo { +func (ap *Processor) ListenerNotificationHandlers() []event.NotificationHandlerInfo { + return nil +} + +// ListenerNotaryParsers for the 'event.Listener' event producer. +func (ap *Processor) ListenerNotaryParsers() []event.NotaryParserInfo { + return nil +} + +// ListenerNotaryHandlers for the 'event.Listener' event producer. +func (ap *Processor) ListenerNotaryHandlers() []event.NotaryHandlerInfo { return nil } // TimersHandlers for the 'Timers' event producer. -func (np *Processor) TimersHandlers() []event.NotificationHandlerInfo { +func (ap *Processor) TimersHandlers() []event.NotificationHandlerInfo { return nil } diff --git a/pkg/innerring/processors/balance/processor.go b/pkg/innerring/processors/balance/processor.go index 8574a18f9..0137ecbef 100644 --- a/pkg/innerring/processors/balance/processor.go +++ b/pkg/innerring/processors/balance/processor.go @@ -104,6 +104,16 @@ func (bp *Processor) ListenerNotificationHandlers() []event.NotificationHandlerI return handlers } +// ListenerNotaryParsers for the 'event.Listener' event producer. +func (bp *Processor) ListenerNotaryParsers() []event.NotaryParserInfo { + return nil +} + +// ListenerNotaryHandlers for the 'event.Listener' event producer. +func (bp *Processor) ListenerNotaryHandlers() []event.NotaryHandlerInfo { + return nil +} + // TimersHandlers for the 'Timers' event producer. func (bp *Processor) TimersHandlers() []event.NotificationHandlerInfo { return nil diff --git a/pkg/innerring/processors/governance/processor.go b/pkg/innerring/processors/governance/processor.go index bb66e4b8a..e9c76817a 100644 --- a/pkg/innerring/processors/governance/processor.go +++ b/pkg/innerring/processors/governance/processor.go @@ -146,6 +146,16 @@ func (gp *Processor) ListenerNotificationHandlers() []event.NotificationHandlerI return []event.NotificationHandlerInfo{hi} } +// ListenerNotaryParsers for the 'event.Listener' event producer. +func (gp *Processor) ListenerNotaryParsers() []event.NotaryParserInfo { + return nil +} + +// ListenerNotaryHandlers for the 'event.Listener' event producer. +func (gp *Processor) ListenerNotaryHandlers() []event.NotaryHandlerInfo { + return nil +} + // TimersHandlers for the 'Timers' event producer. func (gp *Processor) TimersHandlers() []event.NotificationHandlerInfo { return nil diff --git a/pkg/innerring/processors/neofs/processor.go b/pkg/innerring/processors/neofs/processor.go index ac451a7e1..d382d9265 100644 --- a/pkg/innerring/processors/neofs/processor.go +++ b/pkg/innerring/processors/neofs/processor.go @@ -215,6 +215,16 @@ func (np *Processor) ListenerNotificationHandlers() []event.NotificationHandlerI return handlers } +// ListenerNotaryParsers for the 'event.Listener' event producer. +func (np *Processor) ListenerNotaryParsers() []event.NotaryParserInfo { + return nil +} + +// ListenerNotaryHandlers for the 'event.Listener' event producer. +func (np *Processor) ListenerNotaryHandlers() []event.NotaryHandlerInfo { + return nil +} + // TimersHandlers for the 'Timers' event producer. func (np *Processor) TimersHandlers() []event.NotificationHandlerInfo { return nil diff --git a/pkg/innerring/processors/netmap/processor.go b/pkg/innerring/processors/netmap/processor.go index 11a646035..ba6e767b0 100644 --- a/pkg/innerring/processors/netmap/processor.go +++ b/pkg/innerring/processors/netmap/processor.go @@ -202,6 +202,16 @@ func (np *Processor) ListenerNotificationHandlers() []event.NotificationHandlerI return handlers } +// ListenerNotaryParsers for the 'event.Listener' event producer. +func (np *Processor) ListenerNotaryParsers() []event.NotaryParserInfo { + return nil +} + +// ListenerNotaryHandlers for the 'event.Listener' event producer. +func (np *Processor) ListenerNotaryHandlers() []event.NotaryHandlerInfo { + return nil +} + // TimersHandlers for the 'Timers' event producer. func (np *Processor) TimersHandlers() []event.NotificationHandlerInfo { return nil diff --git a/pkg/innerring/processors/reputation/processor.go b/pkg/innerring/processors/reputation/processor.go index 3ebdc905c..25ac2162e 100644 --- a/pkg/innerring/processors/reputation/processor.go +++ b/pkg/innerring/processors/reputation/processor.go @@ -116,6 +116,16 @@ func (rp *Processor) ListenerNotificationHandlers() []event.NotificationHandlerI return handlers } +// ListenerNotaryParsers for the 'event.Listener' event producer. +func (rp *Processor) ListenerNotaryParsers() []event.NotaryParserInfo { + return nil +} + +// ListenerNotaryHandlers for the 'event.Listener' event producer. +func (rp *Processor) ListenerNotaryHandlers() []event.NotaryHandlerInfo { + return nil +} + // TimersHandlers for the 'Timers' event producer. func (rp *Processor) TimersHandlers() []event.NotificationHandlerInfo { return nil