frostfs-node/pkg/innerring/processors/alphabet/process_emit.go
Dmitrii Stepanov 692749dba1
Some checks failed
DCO action / DCO (pull_request) Failing after 1m30s
Tests and linters / Run gofumpt (pull_request) Successful in 1m26s
Vulncheck / Vulncheck (pull_request) Successful in 2m11s
Pre-commit hooks / Pre-commit (pull_request) Successful in 2m25s
Build / Build Components (pull_request) Successful in 2m29s
Tests and linters / Lint (pull_request) Failing after 2m26s
Tests and linters / gopls check (pull_request) Successful in 2m43s
Tests and linters / Staticcheck (pull_request) Successful in 2m56s
Tests and linters / Tests (pull_request) Successful in 4m22s
Tests and linters / Tests with -race (pull_request) Successful in 5m58s
[#1437] node: Use ctx for logging
Signed-off-by: Dmitrii Stepanov <d.stepanov@yadro.com>
2024-10-21 10:24:41 +03:00

117 lines
3 KiB
Go

package alphabet
import (
"context"
"crypto/elliptic"
"git.frostfs.info/TrueCloudLab/frostfs-node/internal/logs"
"git.frostfs.info/TrueCloudLab/frostfs-node/pkg/morph/client/netmap"
"github.com/nspcc-dev/neo-go/pkg/crypto/keys"
"github.com/nspcc-dev/neo-go/pkg/encoding/fixedn"
"github.com/nspcc-dev/neo-go/pkg/util"
"go.uber.org/zap"
)
const emitMethod = "emit"
func (ap *Processor) processEmit() bool {
index := ap.irList.AlphabetIndex()
if index < 0 {
ap.log.Info(context.Background(), logs.AlphabetNonAlphabetModeIgnoreGasEmissionEvent)
return true
}
contract, ok := ap.alphabetContracts.GetByIndex(index)
if !ok {
ap.log.Debug(context.Background(), logs.AlphabetNodeIsOutOfAlphabetRangeIgnoreGasEmissionEvent,
zap.Int("index", index))
return false
}
// there is no signature collecting, so we don't need extra fee
_, err := ap.morphClient.Invoke(contract, 0, emitMethod)
if err != nil {
ap.log.Warn(context.Background(), logs.AlphabetCantInvokeAlphabetEmitMethod, zap.String("error", err.Error()))
return false
}
if ap.storageEmission == 0 {
ap.log.Info(context.Background(), logs.AlphabetStorageNodeEmissionIsOff)
return true
}
networkMap, err := ap.netmapClient.NetMap()
if err != nil {
ap.log.Warn(context.Background(), logs.AlphabetCantGetNetmapSnapshotToEmitGasToStorageNodes,
zap.String("error", err.Error()))
return false
}
nmNodes := networkMap.Nodes()
nmLen := len(nmNodes)
ap.pwLock.RLock()
pw := ap.parsedWallets
ap.pwLock.RUnlock()
extraLen := len(pw)
ap.log.Debug(context.Background(), logs.AlphabetGasEmission,
zap.Int("network_map", nmLen),
zap.Int("extra_wallets", extraLen))
if nmLen+extraLen == 0 {
return true
}
gasPerNode := fixedn.Fixed8(ap.storageEmission / uint64(nmLen+extraLen))
ap.transferGasToNetmapNodes(nmNodes, gasPerNode)
ap.transferGasToExtraNodes(pw, gasPerNode)
return true
}
func (ap *Processor) transferGasToNetmapNodes(nmNodes []netmap.NodeInfo, gasPerNode fixedn.Fixed8) {
for i := range nmNodes {
keyBytes := nmNodes[i].PublicKey()
key, err := keys.NewPublicKeyFromBytes(keyBytes, elliptic.P256())
if err != nil {
ap.log.Warn(context.Background(), logs.AlphabetCantParseNodePublicKey,
zap.String("error", err.Error()))
continue
}
err = ap.morphClient.TransferGas(key.GetScriptHash(), gasPerNode)
if err != nil {
ap.log.Warn(context.Background(), logs.AlphabetCantTransferGas,
zap.String("receiver", key.Address()),
zap.Int64("amount", int64(gasPerNode)),
zap.String("error", err.Error()),
)
}
}
}
func (ap *Processor) transferGasToExtraNodes(pw []util.Uint160, gasPerNode fixedn.Fixed8) {
if len(pw) > 0 {
err := ap.morphClient.BatchTransferGas(pw, gasPerNode)
if err != nil {
receiversLog := make([]string, len(pw))
for i, addr := range pw {
receiversLog[i] = addr.StringLE()
}
ap.log.Warn(context.Background(), logs.AlphabetCantTransferGasToWallet,
zap.Strings("receivers", receiversLog),
zap.Int64("amount", int64(gasPerNode)),
zap.String("error", err.Error()),
)
}
}
}