[#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 <alexey@nspcc.ru>
This commit is contained in:
Alex Vanin 2021-11-01 18:55:25 +03:00 committed by Alex Vanin
parent 45f244eb77
commit a437ffc3ed
6 changed files with 35 additions and 38 deletions

View file

@ -50,12 +50,6 @@ type (
depositor func() (util.Uint256, error) depositor func() (util.Uint256, error)
awaiter func(context.Context, util.Uint256) error awaiter func(context.Context, util.Uint256) error
notaryDepositArgs struct {
l *zap.Logger
depositor depositor
}
) )
func (s *Server) addBlockTimer(t *timer.BlockTimer) { 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()))
}
}
}

View file

@ -647,6 +647,9 @@ func New(ctx context.Context, log *zap.Logger, cfg *viper.Viper) (*Server, error
HandleAudit: server.onlyActiveEventHandler( HandleAudit: server.onlyActiveEventHandler(
auditProcessor.StartAuditHandler(), auditProcessor.StartAuditHandler(),
), ),
NotaryDepositHandler: server.onlyAlphabetEventHandler(
server.notaryHandler,
),
AuditSettlementsHandler: server.onlyAlphabetEventHandler( AuditSettlementsHandler: server.onlyAlphabetEventHandler(
settlementProcessor.HandleAuditEvent, settlementProcessor.HandleAuditEvent,
), ),
@ -777,7 +780,7 @@ func New(ctx context.Context, log *zap.Logger, cfg *viper.Viper) (*Server, error
// initialize epoch timers // initialize epoch timers
server.epochTimer = newEpochTimer(&epochTimerArgs{ server.epochTimer = newEpochTimer(&epochTimerArgs{
l: server.log, l: server.log,
newEpochHandlers: server.newEpochHandlers(log), newEpochHandlers: server.newEpochTickHandlers(),
cnrWrapper: cnrClient, cnrWrapper: cnrClient,
epoch: server, epoch: server,
stopEstimationDMul: cfg.GetUint32("timers.stop_estimation.mul"), 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{ newEpochHandlers := []newEpochHandler{
func() { func() {
s.netmapProcessor.HandleNewEpochTick(timerEvent.NewEpochTick{}) s.netmapProcessor.HandleNewEpochTick(timerEvent.NewEpochTick{})
}, },
} }
if !s.mainNotaryConfig.disabled {
newEpochHandlers = append(newEpochHandlers,
newNotaryDepositHandler(&notaryDepositArgs{
l: log,
depositor: s.depositMainNotary,
}),
)
}
if !s.sideNotaryConfig.disabled {
newEpochHandlers = append(newEpochHandlers,
newNotaryDepositHandler(&notaryDepositArgs{
l: log,
depositor: s.depositSideNotary,
}),
)
}
return newEpochHandlers return newEpochHandlers
} }

View file

@ -6,7 +6,9 @@ import (
"github.com/nspcc-dev/neo-go/pkg/util" "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/client"
"github.com/nspcc-dev/neofs-node/pkg/morph/event"
"github.com/spf13/viper" "github.com/spf13/viper"
"go.uber.org/zap"
) )
type ( 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 { func (s *Server) awaitMainNotaryDeposit(ctx context.Context, tx util.Uint256) error {
return awaitNotaryDepositInClient(ctx, s.mainnetClient, tx) return awaitNotaryDepositInClient(ctx, s.mainnetClient, tx)
} }

View file

@ -32,7 +32,7 @@ func (np *Processor) handleNewEpoch(ev event.Event) {
// send event to the worker pool // send event to the worker pool
err := np.pool.Submit(func() { err := np.pool.Submit(func() {
np.processNewEpoch(epochEvent.EpochNumber()) np.processNewEpoch(epochEvent)
}) })
if err != nil { if err != nil {
// there system can be moved into controlled degradation stage // there system can be moved into controlled degradation stage

View file

@ -4,12 +4,15 @@ import (
"github.com/nspcc-dev/neofs-node/pkg/innerring/processors/audit" "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/governance"
"github.com/nspcc-dev/neofs-node/pkg/innerring/processors/settlement" "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" "go.uber.org/zap"
) )
// Process new epoch notification by setting global epoch value and resetting // Process new epoch notification by setting global epoch value and resetting
// local epoch timer. // local epoch timer.
func (np *Processor) processNewEpoch(epoch uint64) { func (np *Processor) processNewEpoch(event netmapEvent.NewEpoch) {
epoch := event.EpochNumber()
epochDuration, err := np.netmapClient.EpochDuration() epochDuration, err := np.netmapClient.EpochDuration()
if err != nil { if err != nil {
np.log.Warn("can't get epoch duration", np.log.Warn("can't get epoch duration",
@ -48,6 +51,7 @@ func (np *Processor) processNewEpoch(epoch uint64) {
np.handleNewAudit(audit.NewAuditStartEvent(epoch)) np.handleNewAudit(audit.NewAuditStartEvent(epoch))
np.handleAuditSettlements(settlement.NewAuditEvent(epoch)) np.handleAuditSettlements(settlement.NewAuditEvent(epoch))
np.handleAlphabetSync(governance.NewSyncEvent()) np.handleAlphabetSync(governance.NewSyncEvent())
np.handleNotaryDeposit(event)
} }
// Process new epoch tick by invoking new epoch method in network map contract. // Process new epoch tick by invoking new epoch method in network map contract.

View file

@ -65,6 +65,7 @@ type (
handleNewAudit event.Handler handleNewAudit event.Handler
handleAuditSettlements event.Handler handleAuditSettlements event.Handler
handleAlphabetSync event.Handler handleAlphabetSync event.Handler
handleNotaryDeposit event.Handler
nodeValidator NodeValidator nodeValidator NodeValidator
@ -86,6 +87,7 @@ type (
HandleAudit event.Handler HandleAudit event.Handler
AuditSettlementsHandler event.Handler AuditSettlementsHandler event.Handler
AlphabetSyncHandler event.Handler AlphabetSyncHandler event.Handler
NotaryDepositHandler event.Handler
NodeValidator NodeValidator NodeValidator NodeValidator
@ -116,6 +118,8 @@ func New(p *Params) (*Processor, error) {
return nil, errors.New("ir/netmap: audit settlement handler is not set") return nil, errors.New("ir/netmap: audit settlement handler is not set")
case p.AlphabetSyncHandler == nil: case p.AlphabetSyncHandler == nil:
return nil, errors.New("ir/netmap: alphabet sync handler is not set") 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: case p.ContainerWrapper == nil:
return nil, errors.New("ir/netmap: container contract wrapper is not set") return nil, errors.New("ir/netmap: container contract wrapper is not set")
case p.NodeValidator == nil: case p.NodeValidator == nil:
@ -144,6 +148,8 @@ func New(p *Params) (*Processor, error) {
handleAlphabetSync: p.AlphabetSyncHandler, handleAlphabetSync: p.AlphabetSyncHandler,
handleNotaryDeposit: p.NotaryDepositHandler,
nodeValidator: p.NodeValidator, nodeValidator: p.NodeValidator,
notaryDisabled: p.NotaryDisabled, notaryDisabled: p.NotaryDisabled,