From d193f1087cef82cf9bd0ae1afa6aea18b5132c5c Mon Sep 17 00:00:00 2001 From: Alex Vanin Date: Tue, 3 Nov 2020 11:03:15 +0300 Subject: [PATCH] [#139] Emit gas to the storage nodes at emission tick Signed-off-by: Alex Vanin --- .../processors/alphabet/process_emit.go | 50 +++++++++++++++++++ .../processors/alphabet/processor.go | 6 +++ 2 files changed, 56 insertions(+) diff --git a/pkg/innerring/processors/alphabet/process_emit.go b/pkg/innerring/processors/alphabet/process_emit.go index 9a5985bdf..dc5552c28 100644 --- a/pkg/innerring/processors/alphabet/process_emit.go +++ b/pkg/innerring/processors/alphabet/process_emit.go @@ -1,6 +1,10 @@ package alphabet import ( + "crypto/elliptic" + + "github.com/nspcc-dev/neo-go/pkg/crypto/keys" + "github.com/nspcc-dev/neo-go/pkg/util" "github.com/nspcc-dev/neofs-node/pkg/innerring/invoke" "go.uber.org/zap" ) @@ -9,15 +13,61 @@ func (np *Processor) processEmit() { index := np.irList.Index() if index < 0 { np.log.Info("passive mode, ignore gas emission event") + return } else if int(index) >= len(np.alphabetContracts) { np.log.Debug("node is out of alphabet range, ignore gas emission event", zap.Int32("index", index)) + return } err := invoke.AlphabetEmit(np.morphClient, np.alphabetContracts[index]) if err != nil { np.log.Warn("can't invoke alphabet emit method") + + return + } + + if np.storageEmission == 0 { + np.log.Info("storage node emission is off") + + return + } + + networkMap, err := invoke.NetmapSnapshot(np.morphClient, np.netmapContract) + 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 + } + + gasPerNode := util.Fixed8(np.storageEmission / uint64(ln)) + + 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()), + zap.Int64("amount", int64(gasPerNode))) + } } } diff --git a/pkg/innerring/processors/alphabet/processor.go b/pkg/innerring/processors/alphabet/processor.go index 0e92b19e0..5f536454a 100644 --- a/pkg/innerring/processors/alphabet/processor.go +++ b/pkg/innerring/processors/alphabet/processor.go @@ -21,8 +21,10 @@ type ( log *zap.Logger pool *ants.Pool alphabetContracts [7]util.Uint160 + netmapContract util.Uint160 morphClient *client.Client irList Indexer + storageEmission uint64 } // Params of the processor constructor. @@ -30,8 +32,10 @@ type ( Log *zap.Logger PoolSize int AlphabetContracts [7]util.Uint160 + NetmapContract util.Uint160 MorphClient *client.Client IRList Indexer + StorageEmission uint64 } ) @@ -57,8 +61,10 @@ func New(p *Params) (*Processor, error) { log: p.Log, pool: pool, alphabetContracts: p.AlphabetContracts, + netmapContract: p.NetmapContract, morphClient: p.MorphClient, irList: p.IRList, + storageEmission: p.StorageEmission, }, nil }