2022-12-23 17:35:35 +00:00
|
|
|
package frostfs
|
2020-07-24 13:54:03 +00:00
|
|
|
|
|
|
|
import (
|
2023-04-12 14:35:10 +00:00
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-node/internal/logs"
|
2023-03-07 13:38:26 +00:00
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-node/pkg/morph/client/balance"
|
|
|
|
frostfsEvent "git.frostfs.info/TrueCloudLab/frostfs-node/pkg/morph/event/frostfs"
|
2020-07-24 13:54:03 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/util"
|
|
|
|
"go.uber.org/zap"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2022-04-21 11:28:05 +00:00
|
|
|
// lockAccountLifeTime defines the amount of epochs when lock account is valid.
|
2020-07-24 13:54:03 +00:00
|
|
|
lockAccountLifetime uint64 = 20
|
|
|
|
)
|
|
|
|
|
2022-04-21 11:28:05 +00:00
|
|
|
// Process deposit event by invoking a balance contract and sending native
|
|
|
|
// gas in the sidechain.
|
2022-12-23 17:35:35 +00:00
|
|
|
func (np *Processor) processDeposit(deposit *frostfsEvent.Deposit) {
|
2021-03-23 15:20:44 +00:00
|
|
|
if !np.alphabetState.IsAlphabet() {
|
2023-04-12 14:35:10 +00:00
|
|
|
np.log.Info(logs.FrostFSNonAlphabetModeIgnoreDeposit)
|
2020-07-24 13:54:03 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-01-31 09:24:25 +00:00
|
|
|
prm := balance.MintPrm{}
|
2021-11-14 22:07:39 +00:00
|
|
|
|
|
|
|
prm.SetTo(deposit.To())
|
|
|
|
prm.SetAmount(np.converter.ToBalancePrecision(deposit.Amount()))
|
|
|
|
prm.SetID(deposit.ID())
|
|
|
|
|
2022-04-21 11:28:05 +00:00
|
|
|
// send transferX to a 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 {
|
2023-04-12 14:35:10 +00:00
|
|
|
np.log.Error(logs.FrostFSCantTransferAssetsToBalanceContract, zap.Error(err))
|
2020-07-24 13:54:03 +00:00
|
|
|
}
|
|
|
|
|
2020-11-03 11:16:32 +00:00
|
|
|
curEpoch := np.epochState.EpochCounter()
|
|
|
|
receiver := deposit.To()
|
|
|
|
|
2022-04-21 11:28:05 +00:00
|
|
|
// check if the receiver has already received some mint GAS emissions
|
2020-11-03 11:16:32 +00:00
|
|
|
// 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())
|
2022-12-30 09:51:49 +00:00
|
|
|
if ok && val+np.mintEmitThreshold >= curEpoch {
|
2023-04-12 14:35:10 +00:00
|
|
|
np.log.Warn(logs.FrostFSDoubleMintEmissionDeclined,
|
2020-11-03 11:16:32 +00:00
|
|
|
zap.String("receiver", receiver.String()),
|
2022-12-30 09:51:49 +00:00
|
|
|
zap.Uint64("last_emission", val),
|
2020-11-03 11:16:32 +00:00
|
|
|
zap.Uint64("current_epoch", curEpoch))
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-03-18 17:42:35 +00:00
|
|
|
// get gas balance of the node
|
2022-04-21 11:28:05 +00:00
|
|
|
// before gas transfer check if the balance is greater than the threshold
|
2021-03-18 17:42:35 +00:00
|
|
|
balance, err := np.morphClient.GasBalance()
|
|
|
|
if err != nil {
|
2023-04-12 14:35:10 +00:00
|
|
|
np.log.Error(logs.FrostFSCantGetGasBalanceOfTheNode, zap.Error(err))
|
2021-03-18 17:42:35 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if balance < np.gasBalanceThreshold {
|
2023-04-12 14:35:10 +00:00
|
|
|
np.log.Warn(logs.FrostFSGasBalanceThresholdHasBeenReached,
|
2021-03-18 17:42:35 +00:00
|
|
|
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 {
|
2023-04-12 14:35:10 +00:00
|
|
|
np.log.Error(logs.FrostFSCantTransferNativeGasToReceiver,
|
2020-11-03 11:16:32 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2022-04-21 11:28:05 +00:00
|
|
|
// Process withdraw event by locking assets in the balance account.
|
2022-12-23 17:35:35 +00:00
|
|
|
func (np *Processor) processWithdraw(withdraw *frostfsEvent.Withdraw) {
|
2021-03-23 15:20:44 +00:00
|
|
|
if !np.alphabetState.IsAlphabet() {
|
2023-04-12 14:35:10 +00:00
|
|
|
np.log.Info(logs.FrostFSNonAlphabetModeIgnoreWithdraw)
|
2020-07-24 13:54:03 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// create lock account
|
|
|
|
lock, err := util.Uint160DecodeBytesBE(withdraw.ID()[:util.Uint160Size])
|
|
|
|
if err != nil {
|
2023-04-12 14:35:10 +00:00
|
|
|
np.log.Error(logs.FrostFSCantCreateLockAccount, zap.Error(err))
|
2020-07-24 13:54:03 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
curEpoch := np.epochState.EpochCounter()
|
|
|
|
|
2022-01-31 09:24:25 +00:00
|
|
|
prm := balance.LockPrm{}
|
2021-11-14 22:07:39 +00:00
|
|
|
|
|
|
|
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 {
|
2023-04-12 14:35:10 +00:00
|
|
|
np.log.Error(logs.FrostFSCantLockAssetsForWithdraw, zap.Error(err))
|
2020-07-24 13:54:03 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-21 11:28:05 +00:00
|
|
|
// Process cheque event by transferring assets from the lock account back to
|
|
|
|
// the reserve account.
|
2022-12-23 17:35:35 +00:00
|
|
|
func (np *Processor) processCheque(cheque *frostfsEvent.Cheque) {
|
2021-03-23 15:20:44 +00:00
|
|
|
if !np.alphabetState.IsAlphabet() {
|
2023-04-12 14:35:10 +00:00
|
|
|
np.log.Info(logs.FrostFSNonAlphabetModeIgnoreCheque)
|
2020-07-24 13:54:03 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-01-31 09:24:25 +00:00
|
|
|
prm := balance.BurnPrm{}
|
2021-11-14 22:07:39 +00:00
|
|
|
|
|
|
|
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 {
|
2023-04-12 14:35:10 +00:00
|
|
|
np.log.Error(logs.FrostFSCantTransferAssetsToFedContract, zap.Error(err))
|
2020-07-24 13:54:03 +00:00
|
|
|
}
|
|
|
|
}
|