forked from TrueCloudLab/frostfs-node
[#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.cache_size", 1000)
|
||||||
cfg.SetDefault("emit.mint.threshold", 1)
|
cfg.SetDefault("emit.mint.threshold", 1)
|
||||||
cfg.SetDefault("emit.mint.value", 20000000) // 0.2 Fixed8
|
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.exec_pool_size", 10)
|
||||||
cfg.SetDefault("audit.task.queue_capacity", 100)
|
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
|
// create mainnnet neofs processor
|
||||||
neofsProcessor, err := neofs.New(&neofs.Params{
|
neofsProcessor, err := neofs.New(&neofs.Params{
|
||||||
Log: log,
|
Log: log,
|
||||||
PoolSize: cfg.GetInt("workers.neofs"),
|
PoolSize: cfg.GetInt("workers.neofs"),
|
||||||
NeoFSContract: server.contracts.neofs,
|
NeoFSContract: server.contracts.neofs,
|
||||||
BalanceContract: server.contracts.balance,
|
BalanceContract: server.contracts.balance,
|
||||||
NetmapContract: server.contracts.netmap,
|
NetmapContract: server.contracts.netmap,
|
||||||
MorphClient: server.morphClient,
|
MorphClient: server.morphClient,
|
||||||
EpochState: server,
|
EpochState: server,
|
||||||
ActiveState: server,
|
ActiveState: server,
|
||||||
Converter: &server.precision,
|
Converter: &server.precision,
|
||||||
MintEmitCacheSize: cfg.GetInt("emit.mint.cache_size"),
|
MintEmitCacheSize: cfg.GetInt("emit.mint.cache_size"),
|
||||||
MintEmitThreshold: cfg.GetUint64("emit.mint.threshold"),
|
MintEmitThreshold: cfg.GetUint64("emit.mint.threshold"),
|
||||||
MintEmitValue: fixedn.Fixed8(cfg.GetInt64("emit.mint.value")),
|
MintEmitValue: fixedn.Fixed8(cfg.GetInt64("emit.mint.value")),
|
||||||
|
GasBalanceThreshold: cfg.GetInt64("emit.gas.balance_threshold"),
|
||||||
})
|
})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
|
|
|
@ -53,6 +53,22 @@ func (np *Processor) processDeposit(deposit *neofsEvent.Deposit) {
|
||||||
return
|
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)
|
err = np.morphClient.TransferGas(receiver, np.mintEmitValue)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
np.log.Error("can't transfer native gas to receiver",
|
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 of events produced by neofs contract in main net.
|
||||||
Processor struct {
|
Processor struct {
|
||||||
log *zap.Logger
|
log *zap.Logger
|
||||||
pool *ants.Pool
|
pool *ants.Pool
|
||||||
neofsContract util.Uint160
|
neofsContract util.Uint160
|
||||||
balanceContract util.Uint160
|
balanceContract util.Uint160
|
||||||
netmapContract util.Uint160
|
netmapContract util.Uint160
|
||||||
morphClient *client.Client
|
morphClient *client.Client
|
||||||
epochState EpochState
|
epochState EpochState
|
||||||
activeState ActiveState
|
activeState ActiveState
|
||||||
converter PrecisionConverter
|
converter PrecisionConverter
|
||||||
mintEmitLock *sync.Mutex
|
mintEmitLock *sync.Mutex
|
||||||
mintEmitCache *lru.Cache
|
mintEmitCache *lru.Cache
|
||||||
mintEmitThreshold uint64
|
mintEmitThreshold uint64
|
||||||
mintEmitValue fixedn.Fixed8
|
mintEmitValue fixedn.Fixed8
|
||||||
|
gasBalanceThreshold int64
|
||||||
}
|
}
|
||||||
|
|
||||||
// Params of the processor constructor.
|
// Params of the processor constructor.
|
||||||
Params struct {
|
Params struct {
|
||||||
Log *zap.Logger
|
Log *zap.Logger
|
||||||
PoolSize int
|
PoolSize int
|
||||||
NeoFSContract util.Uint160
|
NeoFSContract util.Uint160
|
||||||
BalanceContract util.Uint160
|
BalanceContract util.Uint160
|
||||||
NetmapContract util.Uint160
|
NetmapContract util.Uint160
|
||||||
MorphClient *client.Client
|
MorphClient *client.Client
|
||||||
EpochState EpochState
|
EpochState EpochState
|
||||||
ActiveState ActiveState
|
ActiveState ActiveState
|
||||||
Converter PrecisionConverter
|
Converter PrecisionConverter
|
||||||
MintEmitCacheSize int
|
MintEmitCacheSize int
|
||||||
MintEmitThreshold uint64 // in epochs
|
MintEmitThreshold uint64 // in epochs
|
||||||
MintEmitValue fixedn.Fixed8
|
MintEmitValue fixedn.Fixed8
|
||||||
|
GasBalanceThreshold int64
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -100,19 +102,20 @@ func New(p *Params) (*Processor, error) {
|
||||||
}
|
}
|
||||||
|
|
||||||
return &Processor{
|
return &Processor{
|
||||||
log: p.Log,
|
log: p.Log,
|
||||||
pool: pool,
|
pool: pool,
|
||||||
neofsContract: p.NeoFSContract,
|
neofsContract: p.NeoFSContract,
|
||||||
balanceContract: p.BalanceContract,
|
balanceContract: p.BalanceContract,
|
||||||
netmapContract: p.NetmapContract,
|
netmapContract: p.NetmapContract,
|
||||||
morphClient: p.MorphClient,
|
morphClient: p.MorphClient,
|
||||||
epochState: p.EpochState,
|
epochState: p.EpochState,
|
||||||
activeState: p.ActiveState,
|
activeState: p.ActiveState,
|
||||||
converter: p.Converter,
|
converter: p.Converter,
|
||||||
mintEmitLock: new(sync.Mutex),
|
mintEmitLock: new(sync.Mutex),
|
||||||
mintEmitCache: lruCache,
|
mintEmitCache: lruCache,
|
||||||
mintEmitThreshold: p.MintEmitThreshold,
|
mintEmitThreshold: p.MintEmitThreshold,
|
||||||
mintEmitValue: p.MintEmitValue,
|
mintEmitValue: p.MintEmitValue,
|
||||||
|
gasBalanceThreshold: p.GasBalanceThreshold,
|
||||||
}, nil
|
}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue