Aleksey Savchuk
f0c43c8d80
All checks were successful
Vulncheck / Vulncheck (pull_request) Successful in 3m1s
Pre-commit hooks / Pre-commit (pull_request) Successful in 3m29s
Tests and linters / gopls check (pull_request) Successful in 3m50s
Tests and linters / Lint (pull_request) Successful in 4m35s
DCO action / DCO (pull_request) Successful in 5m12s
Tests and linters / Run gofumpt (pull_request) Successful in 5m33s
Build / Build Components (pull_request) Successful in 5m45s
Tests and linters / Tests with -race (pull_request) Successful in 6m37s
Tests and linters / Tests (pull_request) Successful in 7m17s
Tests and linters / Staticcheck (pull_request) Successful in 7m36s
Tests and linters / Run gofumpt (push) Successful in 1m22s
Tests and linters / Staticcheck (push) Successful in 3m19s
Tests and linters / Lint (push) Successful in 4m35s
Vulncheck / Vulncheck (push) Successful in 5m20s
Build / Build Components (push) Successful in 6m16s
Pre-commit hooks / Pre-commit (push) Successful in 6m37s
Tests and linters / Tests (push) Successful in 6m48s
Tests and linters / Tests with -race (push) Successful in 7m15s
Tests and linters / gopls check (push) Successful in 7m27s
Use `zap.Error` instead of `zap.String` for logging errors: change all expressions like `zap.String("error", err.Error())` or `zap.String("err", err.Error())` to `zap.Error(err)`. Leave similar expressions with other messages unchanged, for example, `zap.String("last_error", lastErr.Error())` or `zap.String("reason", ctx.Err().Error())`. This change was made by applying the following patch: ```diff @@ var err expression @@ -zap.String("error", err.Error()) +zap.Error(err) @@ var err expression @@ -zap.String("err", err.Error()) +zap.Error(err) ``` Signed-off-by: Aleksey Savchuk <a.savchuk@yadro.com>
140 lines
3.9 KiB
Go
140 lines
3.9 KiB
Go
package frostfs
|
|
|
|
import (
|
|
"context"
|
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-node/internal/logs"
|
|
"git.frostfs.info/TrueCloudLab/frostfs-node/pkg/morph/client/balance"
|
|
frostfsEvent "git.frostfs.info/TrueCloudLab/frostfs-node/pkg/morph/event/frostfs"
|
|
"github.com/nspcc-dev/neo-go/pkg/util"
|
|
"go.uber.org/zap"
|
|
)
|
|
|
|
const (
|
|
// lockAccountLifeTime defines the amount of epochs when lock account is valid.
|
|
lockAccountLifetime uint64 = 20
|
|
)
|
|
|
|
// Process deposit event by invoking a balance contract and sending native
|
|
// gas in the sidechain.
|
|
func (np *Processor) processDeposit(ctx context.Context, deposit frostfsEvent.Deposit) bool {
|
|
if !np.alphabetState.IsAlphabet(ctx) {
|
|
np.log.Info(ctx, logs.FrostFSNonAlphabetModeIgnoreDeposit)
|
|
return true
|
|
}
|
|
|
|
prm := balance.MintPrm{}
|
|
|
|
prm.SetTo(deposit.To())
|
|
prm.SetAmount(np.converter.ToBalancePrecision(deposit.Amount()))
|
|
prm.SetID(deposit.ID())
|
|
|
|
// send transferX to a balance contract
|
|
err := np.balanceClient.Mint(ctx, prm)
|
|
if err != nil {
|
|
np.log.Error(ctx, logs.FrostFSCantTransferAssetsToBalanceContract, zap.Error(err))
|
|
}
|
|
|
|
curEpoch := np.epochState.EpochCounter()
|
|
receiver := deposit.To()
|
|
|
|
// check if the receiver has 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+np.mintEmitThreshold >= curEpoch {
|
|
np.log.Warn(ctx, logs.FrostFSDoubleMintEmissionDeclined,
|
|
zap.Stringer("receiver", receiver),
|
|
zap.Uint64("last_emission", val),
|
|
zap.Uint64("current_epoch", curEpoch))
|
|
|
|
return false
|
|
}
|
|
|
|
// get gas balance of the node
|
|
// before gas transfer check if the balance is greater than the threshold
|
|
balance, err := np.morphClient.GasBalance()
|
|
if err != nil {
|
|
np.log.Error(ctx, logs.FrostFSCantGetGasBalanceOfTheNode, zap.Error(err))
|
|
return false
|
|
}
|
|
|
|
if balance < np.gasBalanceThreshold {
|
|
np.log.Warn(ctx, logs.FrostFSGasBalanceThresholdHasBeenReached,
|
|
zap.Int64("balance", balance),
|
|
zap.Int64("threshold", np.gasBalanceThreshold))
|
|
|
|
return false
|
|
}
|
|
|
|
err = np.morphClient.TransferGas(receiver, np.mintEmitValue)
|
|
if err != nil {
|
|
np.log.Error(ctx, logs.FrostFSCantTransferNativeGasToReceiver,
|
|
zap.Error(err))
|
|
|
|
return false
|
|
}
|
|
|
|
np.mintEmitCache.Add(receiver.String(), curEpoch)
|
|
|
|
return true
|
|
}
|
|
|
|
// Process withdraw event by locking assets in the balance account.
|
|
func (np *Processor) processWithdraw(ctx context.Context, withdraw frostfsEvent.Withdraw) bool {
|
|
if !np.alphabetState.IsAlphabet(ctx) {
|
|
np.log.Info(ctx, logs.FrostFSNonAlphabetModeIgnoreWithdraw)
|
|
return true
|
|
}
|
|
|
|
// create lock account
|
|
lock, err := util.Uint160DecodeBytesBE(withdraw.ID()[:util.Uint160Size])
|
|
if err != nil {
|
|
np.log.Error(ctx, logs.FrostFSCantCreateLockAccount, zap.Error(err))
|
|
return false
|
|
}
|
|
|
|
curEpoch := np.epochState.EpochCounter()
|
|
|
|
prm := balance.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(ctx, prm)
|
|
if err != nil {
|
|
np.log.Error(ctx, logs.FrostFSCantLockAssetsForWithdraw, zap.Error(err))
|
|
return false
|
|
}
|
|
|
|
return true
|
|
}
|
|
|
|
// Process cheque event by transferring assets from the lock account back to
|
|
// the reserve account.
|
|
func (np *Processor) processCheque(ctx context.Context, cheque frostfsEvent.Cheque) bool {
|
|
if !np.alphabetState.IsAlphabet(ctx) {
|
|
np.log.Info(ctx, logs.FrostFSNonAlphabetModeIgnoreCheque)
|
|
return true
|
|
}
|
|
|
|
prm := balance.BurnPrm{}
|
|
|
|
prm.SetTo(cheque.LockAccount())
|
|
prm.SetAmount(np.converter.ToBalancePrecision(cheque.Amount()))
|
|
prm.SetID(cheque.ID())
|
|
|
|
err := np.balanceClient.Burn(ctx, prm)
|
|
if err != nil {
|
|
np.log.Error(ctx, logs.FrostFSCantTransferAssetsToFedContract, zap.Error(err))
|
|
return false
|
|
}
|
|
|
|
return true
|
|
}
|