2020-10-12 10:17:40 +00:00
|
|
|
package alphabet
|
|
|
|
|
|
|
|
import (
|
2020-11-03 08:03:15 +00:00
|
|
|
"crypto/elliptic"
|
|
|
|
|
|
|
|
"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"
|
2020-10-12 10:17:40 +00:00
|
|
|
"go.uber.org/zap"
|
|
|
|
)
|
|
|
|
|
2021-05-21 07:45:15 +00:00
|
|
|
const emitMethod = "emit"
|
|
|
|
|
2021-09-08 08:28:41 +00:00
|
|
|
func (ap *Processor) processEmit() {
|
|
|
|
index := ap.irList.AlphabetIndex()
|
2020-10-12 10:17:40 +00:00
|
|
|
if index < 0 {
|
2021-09-08 08:28:41 +00:00
|
|
|
ap.log.Info("non alphabet mode, ignore gas emission event")
|
2020-11-03 08:03:15 +00:00
|
|
|
|
2020-10-12 10:17:40 +00:00
|
|
|
return
|
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 {
|
2021-09-08 08:28:41 +00:00
|
|
|
ap.log.Debug("node is out of alphabet range, ignore gas emission event",
|
2021-02-21 06:28:53 +00:00
|
|
|
zap.Int("index", index))
|
2020-11-03 08:03:15 +00:00
|
|
|
|
2020-10-12 10:17:40 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-05-21 07:45:15 +00:00
|
|
|
// there is no signature collecting, so we don't need extra fee
|
2021-09-08 08:28:41 +00:00
|
|
|
err := ap.morphClient.Invoke(contract, 0, emitMethod)
|
2020-10-12 10:17:40 +00:00
|
|
|
if err != nil {
|
2022-06-07 11:06:10 +00:00
|
|
|
ap.log.Warn("can't invoke alphabet emit method", zap.String("error", err.Error()))
|
2020-11-03 08:03:15 +00:00
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-09-08 08:28:41 +00:00
|
|
|
if ap.storageEmission == 0 {
|
|
|
|
ap.log.Info("storage node emission is off")
|
2020-11-03 08:03:15 +00:00
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-09-28 11:34:28 +00:00
|
|
|
networkMap, err := ap.netmapClient.NetMap()
|
2020-11-03 08:03:15 +00:00
|
|
|
if err != nil {
|
2021-09-08 08:28:41 +00:00
|
|
|
ap.log.Warn("can't get netmap snapshot to emit gas to storage nodes",
|
2020-11-03 08:03:15 +00:00
|
|
|
zap.String("error", err.Error()))
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-06-08 23:18:26 +00:00
|
|
|
nmNodes := networkMap.Nodes()
|
|
|
|
|
|
|
|
ln := len(nmNodes)
|
2020-11-03 08:03:15 +00:00
|
|
|
if ln == 0 {
|
2021-09-08 08:28:41 +00:00
|
|
|
ap.log.Debug("empty network map, do not emit gas")
|
2020-11-03 08:03:15 +00:00
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-03-09 13:19:39 +00:00
|
|
|
gasPerNode := fixedn.Fixed8(ap.storageEmission / uint64(ln+len(ap.parsedWallets)))
|
2020-11-03 08:03:15 +00:00
|
|
|
|
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 {
|
2022-06-07 11:06:10 +00:00
|
|
|
ap.log.Warn("can't parse node public key",
|
2020-11-03 08:03:15 +00:00
|
|
|
zap.String("error", err.Error()))
|
|
|
|
|
|
|
|
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 {
|
2021-09-08 08:28:41 +00:00
|
|
|
ap.log.Warn("can't transfer gas",
|
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)),
|
|
|
|
zap.String("error", err.Error()),
|
|
|
|
)
|
2020-11-03 08:03:15 +00:00
|
|
|
}
|
2020-10-12 10:17:40 +00:00
|
|
|
}
|
2023-03-09 13:19:39 +00:00
|
|
|
|
|
|
|
err = ap.morphClient.BatchTransferGas(ap.parsedWallets, gasPerNode)
|
|
|
|
if err != nil {
|
|
|
|
receiversLog := make([]string, len(ap.parsedWallets))
|
|
|
|
for i, addr := range ap.parsedWallets {
|
|
|
|
receiversLog[i] = addr.StringLE()
|
|
|
|
}
|
|
|
|
ap.log.Warn("can't transfer gas to wallet",
|
|
|
|
zap.Strings("receivers", receiversLog),
|
|
|
|
zap.Int64("amount", int64(gasPerNode)),
|
|
|
|
zap.String("error", err.Error()),
|
|
|
|
)
|
|
|
|
}
|
2020-10-12 10:17:40 +00:00
|
|
|
}
|