[#807] ir: Merge ContractProcessor and NotaryContractProcessor interfaces

Signed-off-by: Pavel Karpy <carpawell@nspcc.ru>
This commit is contained in:
Pavel Karpy 2021-09-08 11:28:41 +03:00 committed by Pavel Karpy
parent 539da27ccb
commit ba77bb44e4
9 changed files with 94 additions and 44 deletions

View file

@ -10,43 +10,33 @@ type (
ContractProcessor interface { ContractProcessor interface {
ListenerNotificationParsers() []event.NotificationParserInfo ListenerNotificationParsers() []event.NotificationParserInfo
ListenerNotificationHandlers() []event.NotificationHandlerInfo 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 ListenerNotaryParsers() []event.NotaryParserInfo
ListenerNotaryHandlers() []event.NotaryHandlerInfo ListenerNotaryHandlers() []event.NotaryHandlerInfo
TimersHandlers() []event.NotificationHandlerInfo
} }
) )
func connectListenerWithProcessor(l event.Listener, p ContractProcessor) { func connectListenerWithProcessor(l event.Listener, p ContractProcessor) {
// register parsers // register notification parsers
for _, parser := range p.ListenerNotificationParsers() { for _, parser := range p.ListenerNotificationParsers() {
l.SetNotificationParser(parser) l.SetNotificationParser(parser)
} }
// register handlers // register notification handlers
for _, handler := range p.ListenerNotificationHandlers() { for _, handler := range p.ListenerNotificationHandlers() {
l.RegisterNotificationHandler(handler) l.RegisterNotificationHandler(handler)
} }
// add notary handlers if processor supports it // register notary parsers
if notaryProcessor, ok := p.(NotaryContractProcessor); ok { for _, notaryParser := range p.ListenerNotaryParsers() {
for _, notaryParser := range notaryProcessor.ListenerNotaryParsers() {
l.SetNotaryParser(notaryParser) l.SetNotaryParser(notaryParser)
} }
for _, notaryHandler := range notaryProcessor.ListenerNotaryHandlers() { // register notary handlers
for _, notaryHandler := range p.ListenerNotaryHandlers() {
l.RegisterNotaryHandler(notaryHandler) l.RegisterNotaryHandler(notaryHandler)
} }
} }
}
// bindMorphProcessor connects morph chain listener handlers. // bindMorphProcessor connects morph chain listener handlers.
func bindMorphProcessor(proc ContractProcessor, s *Server) error { func bindMorphProcessor(proc ContractProcessor, s *Server) error {

View file

@ -6,16 +6,16 @@ import (
"go.uber.org/zap" "go.uber.org/zap"
) )
func (np *Processor) HandleGasEmission(ev event.Event) { func (ap *Processor) HandleGasEmission(ev event.Event) {
_ = ev.(timers.NewAlphabetEmitTick) _ = 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 // send event to the worker pool
err := np.pool.Submit(func() { np.processEmit() }) err := ap.pool.Submit(func() { ap.processEmit() })
if err != nil { if err != nil {
// there system can be moved into controlled degradation stage // there system can be moved into controlled degradation stage
np.log.Warn("alphabet processor worker pool drained", ap.log.Warn("alphabet processor worker pool drained",
zap.Int("capacity", np.pool.Cap())) zap.Int("capacity", ap.pool.Cap()))
} }
} }

View file

@ -10,39 +10,39 @@ import (
const emitMethod = "emit" const emitMethod = "emit"
func (np *Processor) processEmit() { func (ap *Processor) processEmit() {
index := np.irList.AlphabetIndex() index := ap.irList.AlphabetIndex()
if index < 0 { if index < 0 {
np.log.Info("non alphabet mode, ignore gas emission event") ap.log.Info("non alphabet mode, ignore gas emission event")
return return
} }
contract, ok := np.alphabetContracts.GetByIndex(index) contract, ok := ap.alphabetContracts.GetByIndex(index)
if !ok { 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)) zap.Int("index", index))
return return
} }
// there is no signature collecting, so we don't need extra fee // 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 { if err != nil {
np.log.Warn("can't invoke alphabet emit method") ap.log.Warn("can't invoke alphabet emit method")
return return
} }
if np.storageEmission == 0 { if ap.storageEmission == 0 {
np.log.Info("storage node emission is off") ap.log.Info("storage node emission is off")
return return
} }
networkMap, err := np.netmapClient.Snapshot() networkMap, err := ap.netmapClient.Snapshot()
if err != nil { 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())) zap.String("error", err.Error()))
return return
@ -50,27 +50,27 @@ func (np *Processor) processEmit() {
ln := len(networkMap.Nodes) ln := len(networkMap.Nodes)
if ln == 0 { if ln == 0 {
np.log.Debug("empty network map, do not emit gas") ap.log.Debug("empty network map, do not emit gas")
return return
} }
gasPerNode := fixedn.Fixed8(np.storageEmission / uint64(ln)) gasPerNode := fixedn.Fixed8(ap.storageEmission / uint64(ln))
for i := range networkMap.Nodes { for i := range networkMap.Nodes {
keyBytes := networkMap.Nodes[i].PublicKey() keyBytes := networkMap.Nodes[i].PublicKey()
key, err := keys.NewPublicKeyFromBytes(keyBytes, elliptic.P256()) key, err := keys.NewPublicKeyFromBytes(keyBytes, elliptic.P256())
if err != nil { 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())) zap.String("error", err.Error()))
continue continue
} }
err = np.morphClient.TransferGas(key.GetScriptHash(), gasPerNode) err = ap.morphClient.TransferGas(key.GetScriptHash(), gasPerNode)
if err != nil { if err != nil {
np.log.Warn("can't transfer gas", ap.log.Warn("can't transfer gas",
zap.String("receiver", key.Address()), zap.String("receiver", key.Address()),
zap.Int64("amount", int64(gasPerNode)), zap.Int64("amount", int64(gasPerNode)),
zap.String("error", err.Error()), zap.String("error", err.Error()),

View file

@ -83,16 +83,26 @@ func New(p *Params) (*Processor, error) {
} }
// ListenerNotificationParsers for the 'event.Listener' event producer. // ListenerNotificationParsers for the 'event.Listener' event producer.
func (np *Processor) ListenerNotificationParsers() []event.NotificationParserInfo { func (ap *Processor) ListenerNotificationParsers() []event.NotificationParserInfo {
return nil return nil
} }
// ListenerNotificationHandlers for the 'event.Listener' event producer. // 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 return nil
} }
// TimersHandlers for the 'Timers' event producer. // TimersHandlers for the 'Timers' event producer.
func (np *Processor) TimersHandlers() []event.NotificationHandlerInfo { func (ap *Processor) TimersHandlers() []event.NotificationHandlerInfo {
return nil return nil
} }

View file

@ -104,6 +104,16 @@ func (bp *Processor) ListenerNotificationHandlers() []event.NotificationHandlerI
return handlers 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. // TimersHandlers for the 'Timers' event producer.
func (bp *Processor) TimersHandlers() []event.NotificationHandlerInfo { func (bp *Processor) TimersHandlers() []event.NotificationHandlerInfo {
return nil return nil

View file

@ -146,6 +146,16 @@ func (gp *Processor) ListenerNotificationHandlers() []event.NotificationHandlerI
return []event.NotificationHandlerInfo{hi} 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. // TimersHandlers for the 'Timers' event producer.
func (gp *Processor) TimersHandlers() []event.NotificationHandlerInfo { func (gp *Processor) TimersHandlers() []event.NotificationHandlerInfo {
return nil return nil

View file

@ -215,6 +215,16 @@ func (np *Processor) ListenerNotificationHandlers() []event.NotificationHandlerI
return handlers 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. // TimersHandlers for the 'Timers' event producer.
func (np *Processor) TimersHandlers() []event.NotificationHandlerInfo { func (np *Processor) TimersHandlers() []event.NotificationHandlerInfo {
return nil return nil

View file

@ -202,6 +202,16 @@ func (np *Processor) ListenerNotificationHandlers() []event.NotificationHandlerI
return handlers 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. // TimersHandlers for the 'Timers' event producer.
func (np *Processor) TimersHandlers() []event.NotificationHandlerInfo { func (np *Processor) TimersHandlers() []event.NotificationHandlerInfo {
return nil return nil

View file

@ -116,6 +116,16 @@ func (rp *Processor) ListenerNotificationHandlers() []event.NotificationHandlerI
return handlers 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. // TimersHandlers for the 'Timers' event producer.
func (rp *Processor) TimersHandlers() []event.NotificationHandlerInfo { func (rp *Processor) TimersHandlers() []event.NotificationHandlerInfo {
return nil return nil