2020-10-12 10:17:40 +00:00
|
|
|
package alphabet
|
|
|
|
|
|
|
|
import (
|
2024-10-21 07:22:54 +00:00
|
|
|
"context"
|
2020-11-03 08:03:15 +00:00
|
|
|
"crypto/elliptic"
|
|
|
|
|
2023-04-12 14:35:10 +00:00
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-node/internal/logs"
|
2023-03-29 14:22:34 +00:00
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-node/pkg/morph/client/netmap"
|
2020-11-03 08:03:15 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/crypto/keys"
|
2020-12-11 08:33:27 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/encoding/fixedn"
|
2023-05-12 07:41:04 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/util"
|
2020-10-12 10:17:40 +00:00
|
|
|
"go.uber.org/zap"
|
|
|
|
)
|
|
|
|
|
2021-05-21 07:45:15 +00:00
|
|
|
const emitMethod = "emit"
|
|
|
|
|
2024-10-21 13:27:28 +00:00
|
|
|
func (ap *Processor) processEmit(ctx context.Context) bool {
|
|
|
|
index := ap.irList.AlphabetIndex(ctx)
|
2020-10-12 10:17:40 +00:00
|
|
|
if index < 0 {
|
2024-10-21 13:27:28 +00:00
|
|
|
ap.log.Info(ctx, logs.AlphabetNonAlphabetModeIgnoreGasEmissionEvent)
|
2020-11-03 08:03:15 +00:00
|
|
|
|
2023-05-26 10:24:41 +00:00
|
|
|
return true
|
2021-02-21 06:53:18 +00:00
|
|
|
}
|
|
|
|
|
2021-09-08 08:28:41 +00:00
|
|
|
contract, ok := ap.alphabetContracts.GetByIndex(index)
|
2021-02-21 06:53:18 +00:00
|
|
|
if !ok {
|
2024-10-21 13:27:28 +00:00
|
|
|
ap.log.Debug(ctx, logs.AlphabetNodeIsOutOfAlphabetRangeIgnoreGasEmissionEvent,
|
2021-02-21 06:28:53 +00:00
|
|
|
zap.Int("index", index))
|
2020-11-03 08:03:15 +00:00
|
|
|
|
2023-05-26 10:24:41 +00:00
|
|
|
return false
|
2020-10-12 10:17:40 +00:00
|
|
|
}
|
|
|
|
|
2021-05-21 07:45:15 +00:00
|
|
|
// there is no signature collecting, so we don't need extra fee
|
2024-10-21 13:27:28 +00:00
|
|
|
_, err := ap.morphClient.Invoke(ctx, contract, 0, emitMethod)
|
2020-10-12 10:17:40 +00:00
|
|
|
if err != nil {
|
2024-12-13 08:44:56 +00:00
|
|
|
ap.log.Warn(ctx, logs.AlphabetCantInvokeAlphabetEmitMethod, zap.Error(err))
|
2020-11-03 08:03:15 +00:00
|
|
|
|
2023-05-26 10:24:41 +00:00
|
|
|
return false
|
2020-11-03 08:03:15 +00:00
|
|
|
}
|
|
|
|
|
2021-09-08 08:28:41 +00:00
|
|
|
if ap.storageEmission == 0 {
|
2024-10-21 13:27:28 +00:00
|
|
|
ap.log.Info(ctx, logs.AlphabetStorageNodeEmissionIsOff)
|
2020-11-03 08:03:15 +00:00
|
|
|
|
2023-05-26 10:24:41 +00:00
|
|
|
return true
|
2020-11-03 08:03:15 +00:00
|
|
|
}
|
|
|
|
|
2022-09-28 11:34:28 +00:00
|
|
|
networkMap, err := ap.netmapClient.NetMap()
|
2020-11-03 08:03:15 +00:00
|
|
|
if err != nil {
|
2024-10-21 13:27:28 +00:00
|
|
|
ap.log.Warn(ctx, logs.AlphabetCantGetNetmapSnapshotToEmitGasToStorageNodes,
|
2024-12-13 08:44:56 +00:00
|
|
|
zap.Error(err))
|
2020-11-03 08:03:15 +00:00
|
|
|
|
2023-05-26 10:24:41 +00:00
|
|
|
return false
|
2020-11-03 08:03:15 +00:00
|
|
|
}
|
|
|
|
|
2022-06-08 23:18:26 +00:00
|
|
|
nmNodes := networkMap.Nodes()
|
2023-03-20 16:58:11 +00:00
|
|
|
nmLen := len(nmNodes)
|
2023-05-12 07:41:04 +00:00
|
|
|
ap.pwLock.RLock()
|
|
|
|
pw := ap.parsedWallets
|
|
|
|
ap.pwLock.RUnlock()
|
|
|
|
extraLen := len(pw)
|
2022-06-08 23:18:26 +00:00
|
|
|
|
2024-10-21 13:27:28 +00:00
|
|
|
ap.log.Debug(ctx, logs.AlphabetGasEmission,
|
2023-03-20 16:58:11 +00:00
|
|
|
zap.Int("network_map", nmLen),
|
|
|
|
zap.Int("extra_wallets", extraLen))
|
2020-11-03 08:03:15 +00:00
|
|
|
|
2023-03-20 16:58:11 +00:00
|
|
|
if nmLen+extraLen == 0 {
|
2023-05-26 10:24:41 +00:00
|
|
|
return true
|
2020-11-03 08:03:15 +00:00
|
|
|
}
|
|
|
|
|
2023-03-20 16:58:11 +00:00
|
|
|
gasPerNode := fixedn.Fixed8(ap.storageEmission / uint64(nmLen+extraLen))
|
2020-11-03 08:03:15 +00:00
|
|
|
|
2024-10-21 13:27:28 +00:00
|
|
|
ap.transferGasToNetmapNodes(ctx, nmNodes, gasPerNode)
|
2023-03-29 14:22:34 +00:00
|
|
|
|
2024-10-21 13:27:28 +00:00
|
|
|
ap.transferGasToExtraNodes(ctx, pw, gasPerNode)
|
2023-05-26 10:24:41 +00:00
|
|
|
|
|
|
|
return true
|
2023-03-29 14:22:34 +00:00
|
|
|
}
|
|
|
|
|
2024-10-21 13:27:28 +00:00
|
|
|
func (ap *Processor) transferGasToNetmapNodes(ctx context.Context, nmNodes []netmap.NodeInfo, gasPerNode fixedn.Fixed8) {
|
2022-06-08 23:18:26 +00:00
|
|
|
for i := range nmNodes {
|
|
|
|
keyBytes := nmNodes[i].PublicKey()
|
2020-11-03 08:03:15 +00:00
|
|
|
|
|
|
|
key, err := keys.NewPublicKeyFromBytes(keyBytes, elliptic.P256())
|
|
|
|
if err != nil {
|
2024-10-21 13:27:28 +00:00
|
|
|
ap.log.Warn(ctx, logs.AlphabetCantParseNodePublicKey,
|
2024-12-13 08:44:56 +00:00
|
|
|
zap.Error(err))
|
2020-11-03 08:03:15 +00:00
|
|
|
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2021-09-08 08:28:41 +00:00
|
|
|
err = ap.morphClient.TransferGas(key.GetScriptHash(), gasPerNode)
|
2020-11-03 08:03:15 +00:00
|
|
|
if err != nil {
|
2024-10-21 13:27:28 +00:00
|
|
|
ap.log.Warn(ctx, logs.AlphabetCantTransferGas,
|
2020-11-03 08:03:15 +00:00
|
|
|
zap.String("receiver", key.Address()),
|
2021-01-25 12:56:53 +00:00
|
|
|
zap.Int64("amount", int64(gasPerNode)),
|
2024-12-13 08:44:56 +00:00
|
|
|
zap.Error(err),
|
2021-01-25 12:56:53 +00:00
|
|
|
)
|
2020-11-03 08:03:15 +00:00
|
|
|
}
|
2020-10-12 10:17:40 +00:00
|
|
|
}
|
2023-03-29 14:22:34 +00:00
|
|
|
}
|
2023-03-09 13:19:39 +00:00
|
|
|
|
2024-10-21 13:27:28 +00:00
|
|
|
func (ap *Processor) transferGasToExtraNodes(ctx context.Context, pw []util.Uint160, gasPerNode fixedn.Fixed8) {
|
2023-05-12 07:41:04 +00:00
|
|
|
if len(pw) > 0 {
|
|
|
|
err := ap.morphClient.BatchTransferGas(pw, gasPerNode)
|
2023-03-20 16:58:11 +00:00
|
|
|
if err != nil {
|
2023-05-12 07:41:04 +00:00
|
|
|
receiversLog := make([]string, len(pw))
|
|
|
|
for i, addr := range pw {
|
2023-03-20 16:58:11 +00:00
|
|
|
receiversLog[i] = addr.StringLE()
|
|
|
|
}
|
2024-10-21 13:27:28 +00:00
|
|
|
ap.log.Warn(ctx, logs.AlphabetCantTransferGasToWallet,
|
2023-03-20 16:58:11 +00:00
|
|
|
zap.Strings("receivers", receiversLog),
|
|
|
|
zap.Int64("amount", int64(gasPerNode)),
|
2024-12-13 08:44:56 +00:00
|
|
|
zap.Error(err),
|
2023-03-20 16:58:11 +00:00
|
|
|
)
|
2023-03-09 13:19:39 +00:00
|
|
|
}
|
|
|
|
}
|
2020-10-12 10:17:40 +00:00
|
|
|
}
|