From 71dce97b76c12ea9664f0bddac3318e4b378e23d Mon Sep 17 00:00:00 2001 From: Alex Vanin Date: Wed, 24 Feb 2021 18:46:01 +0300 Subject: [PATCH] [#404] innerring: Make notary deposit periodically Signed-off-by: Alex Vanin --- cmd/neofs-ir/defaults.go | 3 +++ pkg/innerring/blocktimer.go | 21 +++++++++++++++++++++ pkg/innerring/innerring.go | 32 ++++++++++++++++++++++++++++++++ 3 files changed, 56 insertions(+) diff --git a/cmd/neofs-ir/defaults.go b/cmd/neofs-ir/defaults.go index 6c4ac405..10fc0b91 100644 --- a/cmd/neofs-ir/defaults.go +++ b/cmd/neofs-ir/defaults.go @@ -71,6 +71,7 @@ func defaultConfiguration(cfg *viper.Viper) { cfg.SetDefault("timers.epoch", "0") cfg.SetDefault("timers.emit", "0") + cfg.SetDefault("timers.notary", "1000") cfg.SetDefault("timers.stop_estimation.mul", 1) cfg.SetDefault("timers.stop_estimation.div", 1) cfg.SetDefault("timers.collect_basic_income.mul", 1) @@ -78,6 +79,8 @@ func defaultConfiguration(cfg *viper.Viper) { cfg.SetDefault("timers.distribute_basic_income.mul", 1) cfg.SetDefault("timers.distribute_basic_income.div", 1) + cfg.SetDefault("notary.deposit_amount", 1_0000_0000) // 1.0 Fixed8 + cfg.SetDefault("workers.netmap", "10") cfg.SetDefault("workers.balance", "10") cfg.SetDefault("workers.neofs", "10") diff --git a/pkg/innerring/blocktimer.go b/pkg/innerring/blocktimer.go index 7828f0f3..f423f5e5 100644 --- a/pkg/innerring/blocktimer.go +++ b/pkg/innerring/blocktimer.go @@ -42,6 +42,14 @@ type ( emitDuration uint32 // in blocks } + + notaryDepositArgs struct { + l *zap.Logger + + depositor func() error + + notaryDuration uint32 // in blocks + } ) func (s *Server) addBlockTimer(t *timers.BlockTimer) { @@ -130,3 +138,16 @@ func newEmissionTimer(args *emitTimerArgs) *timers.BlockTimer { }, ) } + +func newNotaryDepositTimer(args *notaryDepositArgs) *timers.BlockTimer { + return timers.NewBlockTimer( + timers.StaticBlockMeter(args.notaryDuration), + 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 37a95332..706bbd1d 100644 --- a/pkg/innerring/innerring.go +++ b/pkg/innerring/innerring.go @@ -56,6 +56,9 @@ type ( precision precision.Fixed8Converter auditClient *auditWrapper.ClientWrapper + notaryDepositAmount fixedn.Fixed8 + notaryDuration uint32 + // internal variables key *ecdsa.PrivateKey pubKey []byte @@ -105,6 +108,10 @@ type ( const ( morphPrefix = "morph" mainnetPrefix = "mainnet" + + // extra blocks to overlap two deposits, we do that to make sure that + // there won't be any blocks without deposited assets in notary contract. + notaryExtraBlocks = 100 ) // Start runs all event providers. @@ -120,6 +127,12 @@ func (s *Server) Start(ctx context.Context, intError chan<- error) error { return err } + // make an initial deposit to notary contract to enable it + err = s.depositNotary() + if err != nil { + return err + } + // vote for sidechain validator if it is prepared in config err = s.voteForSidechainValidator(s.predefinedValidators) if err != nil { @@ -533,6 +546,18 @@ func New(ctx context.Context, log *zap.Logger, cfg *viper.Viper) (*Server, error server.addBlockTimer(emissionTimer) + // initialize notary deposit timer + server.notaryDepositAmount = fixedn.Fixed8(cfg.GetInt64("notary.deposit_amount")) + server.notaryDuration = cfg.GetUint32("timers.notary") + + notaryTimer := newNotaryDepositTimer(¬aryDepositArgs{ + l: log, + depositor: server.depositNotary, + notaryDuration: server.notaryDuration, + }) + + server.addBlockTimer(notaryTimer) + return server, nil } @@ -707,3 +732,10 @@ func (s *Server) onlyActiveEventHandler(f event.Handler) event.Handler { } } } + +func (s *Server) depositNotary() error { + return s.morphClient.DepositNotary( + s.notaryDepositAmount, + s.notaryDuration+notaryExtraBlocks, + ) +}