2020-07-24 13:54:03 +00:00
|
|
|
package neofs
|
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/util"
|
2021-11-14 22:07:39 +00:00
|
|
|
balancewrp "github.com/nspcc-dev/neofs-node/pkg/morph/client/balance/wrapper"
|
2020-07-24 13:54:03 +00:00
|
|
|
neofsEvent "github.com/nspcc-dev/neofs-node/pkg/morph/event/neofs"
|
|
|
|
"go.uber.org/zap"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
// lockAccountLifeTime defines amount of epochs when lock account is valid.
|
|
|
|
lockAccountLifetime uint64 = 20
|
|
|
|
)
|
|
|
|
|
|
|
|
// Process deposit event by invoking balance contract and sending native
|
|
|
|
// gas in morph chain.
|
|
|
|
func (np *Processor) processDeposit(deposit *neofsEvent.Deposit) {
|
2021-03-23 15:20:44 +00:00
|
|
|
if !np.alphabetState.IsAlphabet() {
|
|
|
|
np.log.Info("non alphabet mode, ignore deposit")
|
2020-07-24 13:54:03 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-11-14 22:07:39 +00:00
|
|
|
prm := balancewrp.MintPrm{}
|
|
|
|
|
|
|
|
prm.SetTo(deposit.To())
|
|
|
|
prm.SetAmount(np.converter.ToBalancePrecision(deposit.Amount()))
|
|
|
|
prm.SetID(deposit.ID())
|
|
|
|
|
2020-07-24 13:54:03 +00:00
|
|
|
// send transferX to balance contract
|
2021-11-14 22:07:39 +00:00
|
|
|
err := np.balanceClient.Mint(prm)
|
2020-07-24 13:54:03 +00:00
|
|
|
if err != nil {
|
|
|
|
np.log.Error("can't transfer assets to balance contract", zap.Error(err))
|
|
|
|
}
|
|
|
|
|
2020-11-03 11:16:32 +00:00
|
|
|
curEpoch := np.epochState.EpochCounter()
|
|
|
|
receiver := deposit.To()
|
|
|
|
|
|
|
|
// check if receiver already received some mint GAS emissions
|
|
|
|
// we should lock there even though LRU cache is already thread save
|
|
|
|
// we lock there because GAS transfer AND cache update must be atomic
|
|
|
|
np.mintEmitLock.Lock()
|
|
|
|
defer np.mintEmitLock.Unlock()
|
|
|
|
|
|
|
|
val, ok := np.mintEmitCache.Get(receiver.String())
|
|
|
|
if ok && val.(uint64)+np.mintEmitThreshold >= curEpoch {
|
|
|
|
np.log.Warn("double mint emission declined",
|
|
|
|
zap.String("receiver", receiver.String()),
|
|
|
|
zap.Uint64("last_emission", val.(uint64)),
|
|
|
|
zap.Uint64("current_epoch", curEpoch))
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-03-18 17:42:35 +00:00
|
|
|
// 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
|
|
|
|
}
|
|
|
|
|
2020-11-03 11:16:32 +00:00
|
|
|
err = np.morphClient.TransferGas(receiver, np.mintEmitValue)
|
2020-07-24 13:54:03 +00:00
|
|
|
if err != nil {
|
2020-11-03 11:16:32 +00:00
|
|
|
np.log.Error("can't transfer native gas to receiver",
|
|
|
|
zap.String("error", err.Error()))
|
|
|
|
|
|
|
|
return
|
2020-07-24 13:54:03 +00:00
|
|
|
}
|
2020-11-03 11:16:32 +00:00
|
|
|
|
|
|
|
np.mintEmitCache.Add(receiver.String(), curEpoch)
|
2020-07-24 13:54:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Process withdraw event by locking assets in balance account.
|
|
|
|
func (np *Processor) processWithdraw(withdraw *neofsEvent.Withdraw) {
|
2021-03-23 15:20:44 +00:00
|
|
|
if !np.alphabetState.IsAlphabet() {
|
|
|
|
np.log.Info("non alphabet mode, ignore withdraw")
|
2020-07-24 13:54:03 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// create lock account
|
2020-09-08 09:09:08 +00:00
|
|
|
// fixme: check collision there, consider reversed script hash
|
2020-07-24 13:54:03 +00:00
|
|
|
lock, err := util.Uint160DecodeBytesBE(withdraw.ID()[:util.Uint160Size])
|
|
|
|
if err != nil {
|
|
|
|
np.log.Error("can't create lock account", zap.Error(err))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
curEpoch := np.epochState.EpochCounter()
|
|
|
|
|
2021-11-14 22:07:39 +00:00
|
|
|
prm := balancewrp.LockPrm{}
|
|
|
|
|
|
|
|
prm.SetID(withdraw.ID())
|
|
|
|
prm.SetUser(withdraw.User())
|
|
|
|
prm.SetLock(lock)
|
|
|
|
prm.SetAmount(np.converter.ToBalancePrecision(withdraw.Amount()))
|
|
|
|
prm.SetDueEpoch(int64(curEpoch + lockAccountLifetime))
|
|
|
|
|
|
|
|
err = np.balanceClient.Lock(prm)
|
2020-07-24 13:54:03 +00:00
|
|
|
if err != nil {
|
|
|
|
np.log.Error("can't lock assets for withdraw", zap.Error(err))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Process cheque event by transferring assets from lock account back to
|
|
|
|
// reserve account.
|
|
|
|
func (np *Processor) processCheque(cheque *neofsEvent.Cheque) {
|
2021-03-23 15:20:44 +00:00
|
|
|
if !np.alphabetState.IsAlphabet() {
|
|
|
|
np.log.Info("non alphabet mode, ignore cheque")
|
2020-07-24 13:54:03 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-11-14 22:07:39 +00:00
|
|
|
prm := balancewrp.BurnPrm{}
|
|
|
|
|
|
|
|
prm.SetTo(cheque.LockAccount())
|
|
|
|
prm.SetAmount(np.converter.ToBalancePrecision(cheque.Amount()))
|
|
|
|
prm.SetID(cheque.ID())
|
|
|
|
|
|
|
|
err := np.balanceClient.Burn(prm)
|
2020-07-24 13:54:03 +00:00
|
|
|
if err != nil {
|
|
|
|
np.log.Error("can't transfer assets to fed contract", zap.Error(err))
|
|
|
|
}
|
|
|
|
}
|