From 5e816dc01a1746248e3c61db088c6f0846851d9c Mon Sep 17 00:00:00 2001 From: Pavel Karpy Date: Thu, 30 Sep 2021 22:11:20 +0300 Subject: [PATCH] [#873] node: Make deposits depend on epoch and balances SN tries to keep 1:3 proportion of GAS and notary balances respectively. If that proportion has been messed(means that notary balance is lower than required) it sends half of its GAS balance to the notary service. Signed-off-by: Pavel Karpy --- cmd/neofs-node/morph.go | 23 +++++++++++++++++------ cmd/neofs-node/netmap.go | 14 ++++++++++++++ 2 files changed, 31 insertions(+), 6 deletions(-) diff --git a/cmd/neofs-node/morph.go b/cmd/neofs-node/morph.go index 2fce060ff..c4cfda682 100644 --- a/cmd/neofs-node/morph.go +++ b/cmd/neofs-node/morph.go @@ -78,11 +78,6 @@ func initMorphComponents(c *cfg) { ), ) fatalOnErr(err) - - c.cfgMorph.notaryDepositAmount = morphconfig.Notary(c.appCfg).Amount() - c.cfgMorph.notaryDepositDuration = morphconfig.Notary(c.appCfg).Duration() - - newDepositTimer(c) } c.log.Debug("notary support", @@ -122,8 +117,24 @@ func makeAndWaitNotaryDeposit(c *cfg) { } func makeNotaryDeposit(c *cfg) (util.Uint256, error) { + const ( + // gasMultiplier defines how many times more the notary + // balance must be compared to the GAS balance of the node: + // notaryBalance = GASBalance * gasMultiplier + gasMultiplier = 3 + + // gasDivisor defines what part of GAS balance (1/gasDivisor) + // should be transferred to the notary service + gasDivisor = 2 + ) + + depositAmount, err := client.CalculateNotaryDepositAmount(c.cfgMorph.client, gasMultiplier, gasDivisor) + if err != nil { + return util.Uint256{}, fmt.Errorf("could not calculate notary deposit: %w", err) + } + return c.cfgMorph.client.DepositNotary( - c.cfgMorph.notaryDepositAmount, + depositAmount, c.cfgMorph.notaryDepositDuration+notaryDepositExtraBlocks, ) } diff --git a/cmd/neofs-node/netmap.go b/cmd/neofs-node/netmap.go index 66c4a3101..206e34211 100644 --- a/cmd/neofs-node/netmap.go +++ b/cmd/neofs-node/netmap.go @@ -7,6 +7,7 @@ import ( netmapSDK "github.com/nspcc-dev/neofs-api-go/pkg/netmap" netmapV2 "github.com/nspcc-dev/neofs-api-go/v2/netmap" netmapGRPC "github.com/nspcc-dev/neofs-api-go/v2/netmap/grpc" + morphconfig "github.com/nspcc-dev/neofs-node/cmd/neofs-node/config/morph" "github.com/nspcc-dev/neofs-node/pkg/core/netmap" "github.com/nspcc-dev/neofs-node/pkg/morph/event" netmapEvent "github.com/nspcc-dev/neofs-node/pkg/morph/event/netmap" @@ -145,6 +146,19 @@ func initNetmapService(c *cfg) { c.handleLocalNodeInfo(ni) }) + + if c.cfgMorph.notaryEnabled { + c.cfgMorph.notaryDepositDuration = morphconfig.Notary(c.appCfg).Duration() + + addNewEpochAsyncNotificationHandler(c, func(ev event.Event) { + _, err := makeNotaryDeposit(c) + if err != nil { + c.log.Error("could not make notary deposit", + zap.String("error", err.Error()), + ) + } + }) + } } func bootstrapNode(c *cfg) {