From a437ffc3ed4f8dd3ce712db4f06793e4254dcb50 Mon Sep 17 00:00:00 2001 From: Alex Vanin Date: Mon, 1 Nov 2021 18:55:25 +0300 Subject: [PATCH] [#910] innerring: Make notary deposit on notification instead of timer Timer is not suitable for notary deposits because it can never fire in case of desynchronization or external epoch changes. Notary deposits must be handled on new epoch event. Signed-off-by: Alex Vanin --- pkg/innerring/blocktimer.go | 16 ------------ pkg/innerring/innerring.go | 25 ++++--------------- pkg/innerring/notary.go | 18 +++++++++++++ pkg/innerring/processors/netmap/handlers.go | 2 +- .../processors/netmap/process_epoch.go | 6 ++++- pkg/innerring/processors/netmap/processor.go | 6 +++++ 6 files changed, 35 insertions(+), 38 deletions(-) diff --git a/pkg/innerring/blocktimer.go b/pkg/innerring/blocktimer.go index 3592d74a9..dfeba744f 100644 --- a/pkg/innerring/blocktimer.go +++ b/pkg/innerring/blocktimer.go @@ -50,12 +50,6 @@ type ( depositor func() (util.Uint256, error) awaiter func(context.Context, util.Uint256) error - - notaryDepositArgs struct { - l *zap.Logger - - depositor depositor - } ) func (s *Server) addBlockTimer(t *timer.BlockTimer) { @@ -148,13 +142,3 @@ func newEmissionTimer(args *emitTimerArgs) *timer.BlockTimer { }, ) } - -func newNotaryDepositHandler(args *notaryDepositArgs) newEpochHandler { - return func() { - _, err := args.depositor() - if err != nil { - args.l.Warn("can't deposit notary contract", - zap.String("error", err.Error())) - } - } -} diff --git a/pkg/innerring/innerring.go b/pkg/innerring/innerring.go index 4bb7bd903..1b7c1c02a 100644 --- a/pkg/innerring/innerring.go +++ b/pkg/innerring/innerring.go @@ -647,6 +647,9 @@ func New(ctx context.Context, log *zap.Logger, cfg *viper.Viper) (*Server, error HandleAudit: server.onlyActiveEventHandler( auditProcessor.StartAuditHandler(), ), + NotaryDepositHandler: server.onlyAlphabetEventHandler( + server.notaryHandler, + ), AuditSettlementsHandler: server.onlyAlphabetEventHandler( settlementProcessor.HandleAuditEvent, ), @@ -777,7 +780,7 @@ func New(ctx context.Context, log *zap.Logger, cfg *viper.Viper) (*Server, error // initialize epoch timers server.epochTimer = newEpochTimer(&epochTimerArgs{ l: server.log, - newEpochHandlers: server.newEpochHandlers(log), + newEpochHandlers: server.newEpochTickHandlers(), cnrWrapper: cnrClient, epoch: server, stopEstimationDMul: cfg.GetUint32("timers.stop_estimation.mul"), @@ -1015,30 +1018,12 @@ func (s *Server) onlyAlphabetEventHandler(f event.Handler) event.Handler { } } -func (s *Server) newEpochHandlers(log *zap.Logger) []newEpochHandler { +func (s *Server) newEpochTickHandlers() []newEpochHandler { newEpochHandlers := []newEpochHandler{ func() { s.netmapProcessor.HandleNewEpochTick(timerEvent.NewEpochTick{}) }, } - if !s.mainNotaryConfig.disabled { - newEpochHandlers = append(newEpochHandlers, - newNotaryDepositHandler(¬aryDepositArgs{ - l: log, - depositor: s.depositMainNotary, - }), - ) - } - - if !s.sideNotaryConfig.disabled { - newEpochHandlers = append(newEpochHandlers, - newNotaryDepositHandler(¬aryDepositArgs{ - l: log, - depositor: s.depositSideNotary, - }), - ) - } - return newEpochHandlers } diff --git a/pkg/innerring/notary.go b/pkg/innerring/notary.go index 0d8965712..11d001409 100644 --- a/pkg/innerring/notary.go +++ b/pkg/innerring/notary.go @@ -6,7 +6,9 @@ import ( "github.com/nspcc-dev/neo-go/pkg/util" "github.com/nspcc-dev/neofs-node/pkg/morph/client" + "github.com/nspcc-dev/neofs-node/pkg/morph/event" "github.com/spf13/viper" + "go.uber.org/zap" ) type ( @@ -50,6 +52,22 @@ func (s *Server) depositSideNotary() (tx util.Uint256, err error) { ) } +func (s *Server) notaryHandler(_ event.Event) { + if !s.mainNotaryConfig.disabled { + _, err := s.depositMainNotary() + if err != nil { + s.log.Error("can't make notary deposit in main chain", zap.Error(err)) + } + } + + if !s.sideNotaryConfig.disabled { + _, err := s.depositSideNotary() + if err != nil { + s.log.Error("can't make notary deposit in side chain", zap.Error(err)) + } + } +} + func (s *Server) awaitMainNotaryDeposit(ctx context.Context, tx util.Uint256) error { return awaitNotaryDepositInClient(ctx, s.mainnetClient, tx) } diff --git a/pkg/innerring/processors/netmap/handlers.go b/pkg/innerring/processors/netmap/handlers.go index ca83ea2bd..acd34f764 100644 --- a/pkg/innerring/processors/netmap/handlers.go +++ b/pkg/innerring/processors/netmap/handlers.go @@ -32,7 +32,7 @@ func (np *Processor) handleNewEpoch(ev event.Event) { // send event to the worker pool err := np.pool.Submit(func() { - np.processNewEpoch(epochEvent.EpochNumber()) + np.processNewEpoch(epochEvent) }) if err != nil { // there system can be moved into controlled degradation stage diff --git a/pkg/innerring/processors/netmap/process_epoch.go b/pkg/innerring/processors/netmap/process_epoch.go index 9c7f1324c..1ee4c463a 100644 --- a/pkg/innerring/processors/netmap/process_epoch.go +++ b/pkg/innerring/processors/netmap/process_epoch.go @@ -4,12 +4,15 @@ import ( "github.com/nspcc-dev/neofs-node/pkg/innerring/processors/audit" "github.com/nspcc-dev/neofs-node/pkg/innerring/processors/governance" "github.com/nspcc-dev/neofs-node/pkg/innerring/processors/settlement" + netmapEvent "github.com/nspcc-dev/neofs-node/pkg/morph/event/netmap" "go.uber.org/zap" ) // Process new epoch notification by setting global epoch value and resetting // local epoch timer. -func (np *Processor) processNewEpoch(epoch uint64) { +func (np *Processor) processNewEpoch(event netmapEvent.NewEpoch) { + epoch := event.EpochNumber() + epochDuration, err := np.netmapClient.EpochDuration() if err != nil { np.log.Warn("can't get epoch duration", @@ -48,6 +51,7 @@ func (np *Processor) processNewEpoch(epoch uint64) { np.handleNewAudit(audit.NewAuditStartEvent(epoch)) np.handleAuditSettlements(settlement.NewAuditEvent(epoch)) np.handleAlphabetSync(governance.NewSyncEvent()) + np.handleNotaryDeposit(event) } // Process new epoch tick by invoking new epoch method in network map contract. diff --git a/pkg/innerring/processors/netmap/processor.go b/pkg/innerring/processors/netmap/processor.go index c4e7bd848..aa582c327 100644 --- a/pkg/innerring/processors/netmap/processor.go +++ b/pkg/innerring/processors/netmap/processor.go @@ -65,6 +65,7 @@ type ( handleNewAudit event.Handler handleAuditSettlements event.Handler handleAlphabetSync event.Handler + handleNotaryDeposit event.Handler nodeValidator NodeValidator @@ -86,6 +87,7 @@ type ( HandleAudit event.Handler AuditSettlementsHandler event.Handler AlphabetSyncHandler event.Handler + NotaryDepositHandler event.Handler NodeValidator NodeValidator @@ -116,6 +118,8 @@ func New(p *Params) (*Processor, error) { return nil, errors.New("ir/netmap: audit settlement handler is not set") case p.AlphabetSyncHandler == nil: return nil, errors.New("ir/netmap: alphabet sync handler is not set") + case p.NotaryDepositHandler == nil: + return nil, errors.New("ir/netmap: notary deposit handler is not set") case p.ContainerWrapper == nil: return nil, errors.New("ir/netmap: container contract wrapper is not set") case p.NodeValidator == nil: @@ -144,6 +148,8 @@ func New(p *Params) (*Processor, error) { handleAlphabetSync: p.AlphabetSyncHandler, + handleNotaryDeposit: p.NotaryDepositHandler, + nodeValidator: p.NodeValidator, notaryDisabled: p.NotaryDisabled,