[#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 <carpawell@nspcc.ru>
This commit is contained in:
parent
99d9544a64
commit
b9892edd6e
4 changed files with 71 additions and 50 deletions
|
@ -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)
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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",
|
||||
|
|
|
@ -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
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in a new issue