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"
|
|
|
|
|
2020-10-12 10:17:40 +00:00
|
|
|
func (np *Processor) processEmit() {
|
2021-03-23 15:20:44 +00:00
|
|
|
index := np.irList.AlphabetIndex()
|
2020-10-12 10:17:40 +00:00
|
|
|
if index < 0 {
|
2021-03-23 15:20:44 +00:00
|
|
|
np.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
|
|
|
}
|
|
|
|
|
|
|
|
contract, ok := np.alphabetContracts.GetByIndex(index)
|
|
|
|
if !ok {
|
2020-10-12 10:17:40 +00:00
|
|
|
np.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
|
|
|
|
err := np.morphClient.Invoke(contract, 0, emitMethod)
|
2020-10-12 10:17:40 +00:00
|
|
|
if err != nil {
|
|
|
|
np.log.Warn("can't invoke alphabet emit method")
|
2020-11-03 08:03:15 +00:00
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if np.storageEmission == 0 {
|
|
|
|
np.log.Info("storage node emission is off")
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-05-31 11:50:11 +00:00
|
|
|
networkMap, err := np.netmapClient.Snapshot()
|
2020-11-03 08:03:15 +00:00
|
|
|
if err != nil {
|
|
|
|
np.log.Warn("can't get netmap snapshot to emit gas to storage nodes",
|
|
|
|
zap.String("error", err.Error()))
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
ln := len(networkMap.Nodes)
|
|
|
|
if ln == 0 {
|
|
|
|
np.log.Debug("empty network map, do not emit gas")
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-12-11 08:33:27 +00:00
|
|
|
gasPerNode := fixedn.Fixed8(np.storageEmission / uint64(ln))
|
2020-11-03 08:03:15 +00:00
|
|
|
|
|
|
|
for i := range networkMap.Nodes {
|
|
|
|
keyBytes := networkMap.Nodes[i].PublicKey()
|
|
|
|
|
|
|
|
key, err := keys.NewPublicKeyFromBytes(keyBytes, elliptic.P256())
|
|
|
|
if err != nil {
|
|
|
|
np.log.Warn("can't convert node public key to address",
|
|
|
|
zap.String("error", err.Error()))
|
|
|
|
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
err = np.morphClient.TransferGas(key.GetScriptHash(), gasPerNode)
|
|
|
|
if err != nil {
|
|
|
|
np.log.Warn("can't transfer gas",
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|