From b9892edd6eaa72e967e824534689c1badd73c2b5 Mon Sep 17 00:00:00 2001 From: Pavel Karpy Date: Thu, 18 Mar 2021 20:42:35 +0300 Subject: [PATCH] [#416] innerring: Add gas threshold On the `Deposit` events add gas balance check. Make transfer only if the balance is greater than the `GasTransferThreshold` that is defined with environmental variable. Signed-off-by: Pavel Karpy --- cmd/neofs-ir/defaults.go | 1 + pkg/innerring/innerring.go | 25 +++--- .../processors/neofs/process_assets.go | 16 ++++ pkg/innerring/processors/neofs/processor.go | 79 ++++++++++--------- 4 files changed, 71 insertions(+), 50 deletions(-) diff --git a/cmd/neofs-ir/defaults.go b/cmd/neofs-ir/defaults.go index 10fc0b91f..5e860a664 100644 --- a/cmd/neofs-ir/defaults.go +++ b/cmd/neofs-ir/defaults.go @@ -94,6 +94,7 @@ func defaultConfiguration(cfg *viper.Viper) { cfg.SetDefault("emit.mint.cache_size", 1000) cfg.SetDefault("emit.mint.threshold", 1) cfg.SetDefault("emit.mint.value", 20000000) // 0.2 Fixed8 + cfg.SetDefault("emit.gas.balance_threshold", 0) cfg.SetDefault("audit.task.exec_pool_size", 10) cfg.SetDefault("audit.task.queue_capacity", 100) diff --git a/pkg/innerring/innerring.go b/pkg/innerring/innerring.go index 2137d6ada..2c68da924 100644 --- a/pkg/innerring/innerring.go +++ b/pkg/innerring/innerring.go @@ -487,18 +487,19 @@ func New(ctx context.Context, log *zap.Logger, cfg *viper.Viper) (*Server, error // create mainnnet neofs processor neofsProcessor, err := neofs.New(&neofs.Params{ - Log: log, - PoolSize: cfg.GetInt("workers.neofs"), - NeoFSContract: server.contracts.neofs, - BalanceContract: server.contracts.balance, - NetmapContract: server.contracts.netmap, - MorphClient: server.morphClient, - EpochState: server, - ActiveState: server, - Converter: &server.precision, - MintEmitCacheSize: cfg.GetInt("emit.mint.cache_size"), - MintEmitThreshold: cfg.GetUint64("emit.mint.threshold"), - MintEmitValue: fixedn.Fixed8(cfg.GetInt64("emit.mint.value")), + Log: log, + PoolSize: cfg.GetInt("workers.neofs"), + NeoFSContract: server.contracts.neofs, + BalanceContract: server.contracts.balance, + NetmapContract: server.contracts.netmap, + MorphClient: server.morphClient, + EpochState: server, + ActiveState: server, + Converter: &server.precision, + MintEmitCacheSize: cfg.GetInt("emit.mint.cache_size"), + MintEmitThreshold: cfg.GetUint64("emit.mint.threshold"), + MintEmitValue: fixedn.Fixed8(cfg.GetInt64("emit.mint.value")), + GasBalanceThreshold: cfg.GetInt64("emit.gas.balance_threshold"), }) if err != nil { return nil, err diff --git a/pkg/innerring/processors/neofs/process_assets.go b/pkg/innerring/processors/neofs/process_assets.go index 866640121..ca02e76e6 100644 --- a/pkg/innerring/processors/neofs/process_assets.go +++ b/pkg/innerring/processors/neofs/process_assets.go @@ -53,6 +53,22 @@ func (np *Processor) processDeposit(deposit *neofsEvent.Deposit) { return } + // get gas balance of the node + // before gas transfer check if the balance is greater than threshold + balance, err := np.morphClient.GasBalance() + if err != nil { + np.log.Error("can't get gas balance of the node", zap.Error(err)) + return + } + + if balance < np.gasBalanceThreshold { + np.log.Warn("gas balance threshold has been reached", + zap.Int64("balance", balance), + zap.Int64("threshold", np.gasBalanceThreshold)) + + return + } + err = np.morphClient.TransferGas(receiver, np.mintEmitValue) if err != nil { np.log.Error("can't transfer native gas to receiver", diff --git a/pkg/innerring/processors/neofs/processor.go b/pkg/innerring/processors/neofs/processor.go index 15ea9547c..49b1c7feb 100644 --- a/pkg/innerring/processors/neofs/processor.go +++ b/pkg/innerring/processors/neofs/processor.go @@ -32,35 +32,37 @@ type ( // Processor of events produced by neofs contract in main net. Processor struct { - log *zap.Logger - pool *ants.Pool - neofsContract util.Uint160 - balanceContract util.Uint160 - netmapContract util.Uint160 - morphClient *client.Client - epochState EpochState - activeState ActiveState - converter PrecisionConverter - mintEmitLock *sync.Mutex - mintEmitCache *lru.Cache - mintEmitThreshold uint64 - mintEmitValue fixedn.Fixed8 + log *zap.Logger + pool *ants.Pool + neofsContract util.Uint160 + balanceContract util.Uint160 + netmapContract util.Uint160 + morphClient *client.Client + epochState EpochState + activeState ActiveState + converter PrecisionConverter + mintEmitLock *sync.Mutex + mintEmitCache *lru.Cache + mintEmitThreshold uint64 + mintEmitValue fixedn.Fixed8 + gasBalanceThreshold int64 } // Params of the processor constructor. Params struct { - Log *zap.Logger - PoolSize int - NeoFSContract util.Uint160 - BalanceContract util.Uint160 - NetmapContract util.Uint160 - MorphClient *client.Client - EpochState EpochState - ActiveState ActiveState - Converter PrecisionConverter - MintEmitCacheSize int - MintEmitThreshold uint64 // in epochs - MintEmitValue fixedn.Fixed8 + Log *zap.Logger + PoolSize int + NeoFSContract util.Uint160 + BalanceContract util.Uint160 + NetmapContract util.Uint160 + MorphClient *client.Client + EpochState EpochState + ActiveState ActiveState + Converter PrecisionConverter + MintEmitCacheSize int + MintEmitThreshold uint64 // in epochs + MintEmitValue fixedn.Fixed8 + GasBalanceThreshold int64 } ) @@ -100,19 +102,20 @@ func New(p *Params) (*Processor, error) { } return &Processor{ - log: p.Log, - pool: pool, - neofsContract: p.NeoFSContract, - balanceContract: p.BalanceContract, - netmapContract: p.NetmapContract, - morphClient: p.MorphClient, - epochState: p.EpochState, - activeState: p.ActiveState, - converter: p.Converter, - mintEmitLock: new(sync.Mutex), - mintEmitCache: lruCache, - mintEmitThreshold: p.MintEmitThreshold, - mintEmitValue: p.MintEmitValue, + log: p.Log, + pool: pool, + neofsContract: p.NeoFSContract, + balanceContract: p.BalanceContract, + netmapContract: p.NetmapContract, + morphClient: p.MorphClient, + epochState: p.EpochState, + activeState: p.ActiveState, + converter: p.Converter, + mintEmitLock: new(sync.Mutex), + mintEmitCache: lruCache, + mintEmitThreshold: p.MintEmitThreshold, + mintEmitValue: p.MintEmitValue, + gasBalanceThreshold: p.GasBalanceThreshold, }, nil }