From f2562e8c47aa47857af8fbff6fa303c08460f6f2 Mon Sep 17 00:00:00 2001 From: Alex Vanin Date: Thu, 29 Apr 2021 16:40:34 +0300 Subject: [PATCH] [#486] innerring: Use fee provider and notary disabled flag in processors Processors that use `invoke` package to make chain invocation should provide fee config and client with enabled or disabled notary support. If notary support is disabled, then functions from `invoke` package will perform ordinary method invocation with extra fee. Processors that use `morph/client` wrappers should check `notaryDisabled` flag to call corresponding wrapper function. Netmap processor omits some actions during validator syncronization if notary is disabled. Signed-off-by: Alex Vanin --- pkg/innerring/processors/audit/processor.go | 8 +++- .../processors/balance/process_assets.go | 2 +- pkg/innerring/processors/balance/processor.go | 6 +++ .../processors/container/process_container.go | 4 +- .../processors/container/processor.go | 6 +++ .../processors/governance/process_update.go | 21 ++++++---- .../processors/governance/processor.go | 40 +++++++++++++------ .../processors/neofs/process_assets.go | 6 +-- .../processors/neofs/process_config.go | 2 +- pkg/innerring/processors/neofs/processor.go | 6 +++ .../processors/netmap/process_cleanup.go | 9 +++-- .../processors/netmap/process_epoch.go | 9 ++++- .../processors/netmap/process_peers.go | 5 ++- pkg/innerring/processors/netmap/processor.go | 12 ++++++ .../processors/reputation/process_put.go | 9 ++++- .../processors/reputation/processor.go | 4 ++ 16 files changed, 111 insertions(+), 38 deletions(-) diff --git a/pkg/innerring/processors/audit/processor.go b/pkg/innerring/processors/audit/processor.go index 8b2970102..37240e257 100644 --- a/pkg/innerring/processors/audit/processor.go +++ b/pkg/innerring/processors/audit/processor.go @@ -7,6 +7,7 @@ import ( "github.com/nspcc-dev/neo-go/pkg/util" SDKClient "github.com/nspcc-dev/neofs-api-go/pkg/client" + "github.com/nspcc-dev/neofs-node/pkg/innerring/config" "github.com/nspcc-dev/neofs-node/pkg/innerring/invoke" "github.com/nspcc-dev/neofs-node/pkg/morph/client" wrapContainer "github.com/nspcc-dev/neofs-node/pkg/morph/client/container/wrapper" @@ -66,6 +67,7 @@ type ( AuditContract util.Uint160 MorphClient *client.Client IRList Indexer + FeeProvider *config.FeeConfig ClientCache NeoFSClientCache RPCSearchTimeout time.Duration TaskManager TaskManager @@ -94,6 +96,8 @@ func New(p *Params) (*Processor, error) { return nil, errors.New("ir/audit: neo:morph client is not set") case p.IRList == nil: return nil, errors.New("ir/audit: global state is not set") + case p.FeeProvider == nil: + return nil, errors.New("ir/audit: fee provider is not set") case p.ClientCache == nil: return nil, errors.New("ir/audit: neofs RPC client cache is not set") case p.TaskManager == nil: @@ -110,13 +114,13 @@ func New(p *Params) (*Processor, error) { } // creating enhanced client for getting network map - netmapClient, err := invoke.NewNetmapClient(p.MorphClient, p.NetmapContract) + netmapClient, err := invoke.NewNetmapClient(p.MorphClient, p.NetmapContract, p.FeeProvider) if err != nil { return nil, err } // creating enhanced client for getting containers - containerClient, err := invoke.NewContainerClient(p.MorphClient, p.ContainerContract) + containerClient, err := invoke.NewContainerClient(p.MorphClient, p.ContainerContract, p.FeeProvider) if err != nil { return nil, err } diff --git a/pkg/innerring/processors/balance/process_assets.go b/pkg/innerring/processors/balance/process_assets.go index d31bc9601..d40649c45 100644 --- a/pkg/innerring/processors/balance/process_assets.go +++ b/pkg/innerring/processors/balance/process_assets.go @@ -14,7 +14,7 @@ func (bp *Processor) processLock(lock *balanceEvent.Lock) { return } - err := invoke.CashOutCheque(bp.mainnetClient, bp.neofsContract, + err := invoke.CashOutCheque(bp.mainnetClient, bp.neofsContract, bp.feeProvider, &invoke.ChequeParams{ ID: lock.ID(), Amount: bp.converter.ToFixed8(lock.Amount()), diff --git a/pkg/innerring/processors/balance/processor.go b/pkg/innerring/processors/balance/processor.go index 070e6c8bb..a98df4ff6 100644 --- a/pkg/innerring/processors/balance/processor.go +++ b/pkg/innerring/processors/balance/processor.go @@ -2,6 +2,7 @@ package balance import ( "github.com/nspcc-dev/neo-go/pkg/util" + "github.com/nspcc-dev/neofs-node/pkg/innerring/config" "github.com/nspcc-dev/neofs-node/pkg/morph/client" "github.com/nspcc-dev/neofs-node/pkg/morph/event" balanceEvent "github.com/nspcc-dev/neofs-node/pkg/morph/event/balance" @@ -30,6 +31,7 @@ type ( mainnetClient *client.Client alphabetState AlphabetState converter PrecisionConverter + feeProvider *config.FeeConfig } // Params of the processor constructor. @@ -41,6 +43,7 @@ type ( MainnetClient *client.Client AlphabetState AlphabetState Converter PrecisionConverter + FeeProvider *config.FeeConfig } ) @@ -59,6 +62,8 @@ func New(p *Params) (*Processor, error) { return nil, errors.New("ir/balance: global state is not set") case p.Converter == nil: return nil, errors.New("ir/balance: balance precision converter is not set") + case p.FeeProvider == nil: + return nil, errors.New("ir/balance: fee provider is not set") } p.Log.Debug("balance worker pool", zap.Int("size", p.PoolSize)) @@ -76,6 +81,7 @@ func New(p *Params) (*Processor, error) { mainnetClient: p.MainnetClient, alphabetState: p.AlphabetState, converter: p.Converter, + feeProvider: p.FeeProvider, }, nil } diff --git a/pkg/innerring/processors/container/process_container.go b/pkg/innerring/processors/container/process_container.go index 1e985c83a..b781986f3 100644 --- a/pkg/innerring/processors/container/process_container.go +++ b/pkg/innerring/processors/container/process_container.go @@ -37,7 +37,7 @@ func (cp *Processor) processContainerPut(put *containerEvent.Put) { return } - err := invoke.RegisterContainer(cp.morphClient, cp.containerContract, + err := invoke.RegisterContainer(cp.morphClient, cp.containerContract, cp.feeProvider, &invoke.ContainerParams{ Key: put.PublicKey(), Container: cnrData, @@ -56,7 +56,7 @@ func (cp *Processor) processContainerDelete(delete *containerEvent.Delete) { return } - err := invoke.RemoveContainer(cp.morphClient, cp.containerContract, + err := invoke.RemoveContainer(cp.morphClient, cp.containerContract, cp.feeProvider, &invoke.RemoveContainerParams{ ContainerID: delete.ContainerID(), Signature: delete.Signature(), diff --git a/pkg/innerring/processors/container/processor.go b/pkg/innerring/processors/container/processor.go index c21208a9e..84c242a34 100644 --- a/pkg/innerring/processors/container/processor.go +++ b/pkg/innerring/processors/container/processor.go @@ -2,6 +2,7 @@ package container import ( "github.com/nspcc-dev/neo-go/pkg/util" + "github.com/nspcc-dev/neofs-node/pkg/innerring/config" "github.com/nspcc-dev/neofs-node/pkg/morph/client" "github.com/nspcc-dev/neofs-node/pkg/morph/event" containerEvent "github.com/nspcc-dev/neofs-node/pkg/morph/event/container" @@ -23,6 +24,7 @@ type ( containerContract util.Uint160 morphClient *client.Client alphabetState AlphabetState + feeProvider *config.FeeConfig } // Params of the processor constructor. @@ -32,6 +34,7 @@ type ( ContainerContract util.Uint160 MorphClient *client.Client AlphabetState AlphabetState + FeeProvider *config.FeeConfig } ) @@ -49,6 +52,8 @@ func New(p *Params) (*Processor, error) { return nil, errors.New("ir/container: neo:morph client is not set") case p.AlphabetState == nil: return nil, errors.New("ir/container: global state is not set") + case p.FeeProvider == nil: + return nil, errors.New("ir/container: fee provider is not set") } p.Log.Debug("container worker pool", zap.Int("size", p.PoolSize)) @@ -64,6 +69,7 @@ func New(p *Params) (*Processor, error) { containerContract: p.ContainerContract, morphClient: p.MorphClient, alphabetState: p.AlphabetState, + feeProvider: p.FeeProvider, }, nil } diff --git a/pkg/innerring/processors/governance/process_update.go b/pkg/innerring/processors/governance/process_update.go index e86522303..07e7aee89 100644 --- a/pkg/innerring/processors/governance/process_update.go +++ b/pkg/innerring/processors/governance/process_update.go @@ -65,7 +65,12 @@ func (gp *Processor) processAlphabetSync() { } else { sort.Sort(newInnerRing) - err = gp.morphClient.UpdateNeoFSAlphabetList(newInnerRing) + if gp.notaryDisabled { + err = invoke.SetInnerRing(gp.morphClient, gp.netmapContract, gp.feeProvider, newInnerRing) + } else { + err = gp.morphClient.UpdateNeoFSAlphabetList(newInnerRing) + } + if err != nil { gp.log.Error("can't update inner ring list with new alphabet keys", zap.String("error", err.Error())) @@ -73,11 +78,13 @@ func (gp *Processor) processAlphabetSync() { } } - // 3. Update notary role in side chain. - err = gp.morphClient.UpdateNotaryList(newAlphabet) - if err != nil { - gp.log.Error("can't update list of notary nodes in side chain", - zap.String("error", err.Error())) + if !gp.notaryDisabled { + // 3. Update notary role in side chain. + err = gp.morphClient.UpdateNotaryList(newAlphabet) + if err != nil { + gp.log.Error("can't update list of notary nodes in side chain", + zap.String("error", err.Error())) + } } // 4. Update NeoFS contract in main net. @@ -88,7 +95,7 @@ func (gp *Processor) processAlphabetSync() { id := append([]byte(alphabetUpdateIDPrefix), buf...) - err = invoke.AlphabetUpdate(gp.mainnetClient, gp.neofsContract, id, newAlphabet) + err = invoke.AlphabetUpdate(gp.mainnetClient, gp.neofsContract, gp.feeProvider, id, newAlphabet) if err != nil { gp.log.Error("can't update list of alphabet nodes in neofs contract", zap.String("error", err.Error())) diff --git a/pkg/innerring/processors/governance/processor.go b/pkg/innerring/processors/governance/processor.go index 47fe0bdb0..7c811a055 100644 --- a/pkg/innerring/processors/governance/processor.go +++ b/pkg/innerring/processors/governance/processor.go @@ -3,6 +3,7 @@ package governance import ( "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/config" "github.com/nspcc-dev/neofs-node/pkg/morph/client" "github.com/nspcc-dev/neofs-node/pkg/morph/event" "github.com/panjf2000/ants/v2" @@ -33,9 +34,10 @@ type ( // Processor of events related to governance in the network. Processor struct { - log *zap.Logger - pool *ants.Pool - neofsContract util.Uint160 + log *zap.Logger + pool *ants.Pool + neofsContract util.Uint160 + netmapContract util.Uint160 alphabetState AlphabetState epochState EpochState @@ -43,12 +45,16 @@ type ( mainnetClient *client.Client morphClient *client.Client + + notaryDisabled bool + feeProvider *config.FeeConfig } // Params of the processor constructor. Params struct { - Log *zap.Logger - NeoFSContract util.Uint160 + Log *zap.Logger + NeoFSContract util.Uint160 + NetmapContract util.Uint160 AlphabetState AlphabetState EpochState EpochState @@ -56,6 +62,9 @@ type ( MorphClient *client.Client MainnetClient *client.Client + + NotaryDisabled bool + FeeProvider *config.FeeConfig } ) @@ -74,6 +83,8 @@ func New(p *Params) (*Processor, error) { return nil, errors.New("ir/governance: global state is not set") case p.Voter == nil: return nil, errors.New("ir/governance: global state is not set") + case p.FeeProvider == nil: + return nil, errors.New("ir/governance: fee provider is not set") } pool, err := ants.NewPool(ProcessorPoolSize, ants.WithNonblocking(true)) @@ -82,14 +93,17 @@ func New(p *Params) (*Processor, error) { } return &Processor{ - log: p.Log, - pool: pool, - neofsContract: p.NeoFSContract, - alphabetState: p.AlphabetState, - epochState: p.EpochState, - voter: p.Voter, - mainnetClient: p.MainnetClient, - morphClient: p.MorphClient, + log: p.Log, + pool: pool, + neofsContract: p.NeoFSContract, + netmapContract: p.NetmapContract, + alphabetState: p.AlphabetState, + epochState: p.EpochState, + voter: p.Voter, + mainnetClient: p.MainnetClient, + morphClient: p.MorphClient, + notaryDisabled: p.NotaryDisabled, + feeProvider: p.FeeProvider, }, nil } diff --git a/pkg/innerring/processors/neofs/process_assets.go b/pkg/innerring/processors/neofs/process_assets.go index 22a293b17..a4e257153 100644 --- a/pkg/innerring/processors/neofs/process_assets.go +++ b/pkg/innerring/processors/neofs/process_assets.go @@ -21,7 +21,7 @@ func (np *Processor) processDeposit(deposit *neofsEvent.Deposit) { } // send transferX to balance contract - err := invoke.Mint(np.morphClient, np.balanceContract, + err := invoke.Mint(np.morphClient, np.balanceContract, np.feeProvider, &invoke.MintBurnParams{ ScriptHash: deposit.To().BytesBE(), Amount: np.converter.ToBalancePrecision(deposit.Amount()), @@ -94,7 +94,7 @@ func (np *Processor) processWithdraw(withdraw *neofsEvent.Withdraw) { curEpoch := np.epochState.EpochCounter() - err = invoke.LockAsset(np.morphClient, np.balanceContract, + err = invoke.LockAsset(np.morphClient, np.balanceContract, np.feeProvider, &invoke.LockParams{ ID: withdraw.ID(), User: withdraw.User(), @@ -115,7 +115,7 @@ func (np *Processor) processCheque(cheque *neofsEvent.Cheque) { return } - err := invoke.Burn(np.morphClient, np.balanceContract, + err := invoke.Burn(np.morphClient, np.balanceContract, np.feeProvider, &invoke.MintBurnParams{ ScriptHash: cheque.LockAccount().BytesBE(), Amount: np.converter.ToBalancePrecision(cheque.Amount()), diff --git a/pkg/innerring/processors/neofs/process_config.go b/pkg/innerring/processors/neofs/process_config.go index 815db93a7..5d04c9d88 100644 --- a/pkg/innerring/processors/neofs/process_config.go +++ b/pkg/innerring/processors/neofs/process_config.go @@ -14,7 +14,7 @@ func (np *Processor) processConfig(config *neofsEvent.Config) { return } - err := invoke.SetConfig(np.morphClient, np.netmapContract, + err := invoke.SetConfig(np.morphClient, np.netmapContract, np.feeProvider, &invoke.SetConfigArgs{ ID: config.ID(), Key: config.Key(), diff --git a/pkg/innerring/processors/neofs/processor.go b/pkg/innerring/processors/neofs/processor.go index d15497d79..c0d7500a2 100644 --- a/pkg/innerring/processors/neofs/processor.go +++ b/pkg/innerring/processors/neofs/processor.go @@ -6,6 +6,7 @@ import ( lru "github.com/hashicorp/golang-lru" "github.com/nspcc-dev/neo-go/pkg/encoding/fixedn" "github.com/nspcc-dev/neo-go/pkg/util" + "github.com/nspcc-dev/neofs-node/pkg/innerring/config" "github.com/nspcc-dev/neofs-node/pkg/morph/client" "github.com/nspcc-dev/neofs-node/pkg/morph/event" neofsEvent "github.com/nspcc-dev/neofs-node/pkg/morph/event/neofs" @@ -41,6 +42,7 @@ type ( epochState EpochState alphabetState AlphabetState converter PrecisionConverter + feeProvider *config.FeeConfig mintEmitLock *sync.Mutex mintEmitCache *lru.Cache mintEmitThreshold uint64 @@ -59,6 +61,7 @@ type ( EpochState EpochState AlphabetState AlphabetState Converter PrecisionConverter + FeeProvider *config.FeeConfig MintEmitCacheSize int MintEmitThreshold uint64 // in epochs MintEmitValue fixedn.Fixed8 @@ -86,6 +89,8 @@ func New(p *Params) (*Processor, error) { return nil, errors.New("ir/neofs: global state is not set") case p.Converter == nil: return nil, errors.New("ir/neofs: balance precision converter is not set") + case p.FeeProvider == nil: + return nil, errors.New("ir/neofs: fee provider is not set") } p.Log.Debug("neofs worker pool", zap.Int("size", p.PoolSize)) @@ -110,6 +115,7 @@ func New(p *Params) (*Processor, error) { epochState: p.EpochState, alphabetState: p.AlphabetState, converter: p.Converter, + feeProvider: p.FeeProvider, mintEmitLock: new(sync.Mutex), mintEmitCache: lruCache, mintEmitThreshold: p.MintEmitThreshold, diff --git a/pkg/innerring/processors/netmap/process_cleanup.go b/pkg/innerring/processors/netmap/process_cleanup.go index 06b30ab48..1920c1bae 100644 --- a/pkg/innerring/processors/netmap/process_cleanup.go +++ b/pkg/innerring/processors/netmap/process_cleanup.go @@ -25,10 +25,11 @@ func (np *Processor) processNetmapCleanupTick(epoch uint64) { np.log.Info("vote to remove node from netmap", zap.String("key", s)) - err = invoke.UpdatePeerState(np.morphClient, np.netmapContract, &invoke.UpdatePeerArgs{ - Key: key, - Status: netmap.NodeStateOffline, - }) + err = invoke.UpdatePeerState(np.morphClient, np.netmapContract, np.feeProvider, + &invoke.UpdatePeerArgs{ + Key: key, + Status: netmap.NodeStateOffline, + }) if err != nil { np.log.Error("can't invoke netmap.UpdateState", zap.Error(err)) } diff --git a/pkg/innerring/processors/netmap/process_epoch.go b/pkg/innerring/processors/netmap/process_epoch.go index 38781dc50..af29593f4 100644 --- a/pkg/innerring/processors/netmap/process_epoch.go +++ b/pkg/innerring/processors/netmap/process_epoch.go @@ -27,7 +27,12 @@ func (np *Processor) processNewEpoch(epoch uint64) { } if epoch > 0 { // estimates are invalid in genesis epoch - err = np.containerWrp.StartEstimationNotary(epoch - 1) + if np.notaryDisabled { + err = np.containerWrp.StartEstimation(epoch - 1) + } else { + err = np.containerWrp.StartEstimationNotary(epoch - 1) + } + if err != nil { np.log.Warn("can't start container size estimation", zap.Uint64("epoch", epoch), @@ -52,7 +57,7 @@ func (np *Processor) processNewEpochTick() { nextEpoch := np.epochState.EpochCounter() + 1 np.log.Debug("next epoch", zap.Uint64("value", nextEpoch)) - err := invoke.SetNewEpoch(np.morphClient, np.netmapContract, nextEpoch) + err := invoke.SetNewEpoch(np.morphClient, np.netmapContract, np.feeProvider, nextEpoch) if err != nil { np.log.Error("can't invoke netmap.NewEpoch", zap.Error(err)) } diff --git a/pkg/innerring/processors/netmap/process_peers.go b/pkg/innerring/processors/netmap/process_peers.go index 44ee62348..e0664c0c6 100644 --- a/pkg/innerring/processors/netmap/process_peers.go +++ b/pkg/innerring/processors/netmap/process_peers.go @@ -66,7 +66,8 @@ func (np *Processor) processAddPeer(node []byte) { np.log.Info("approving network map candidate", zap.String("key", keyString)) - if err := invoke.ApprovePeer(np.morphClient, np.netmapContract, node); err != nil { + err := invoke.ApprovePeer(np.morphClient, np.netmapContract, np.feeProvider, node) + if err != nil { np.log.Error("can't invoke netmap.AddPeer", zap.Error(err)) } } @@ -92,7 +93,7 @@ func (np *Processor) processUpdatePeer(ev netmapEvent.UpdatePeer) { // again before new epoch will tick np.netmapSnapshot.flag(hex.EncodeToString(ev.PublicKey().Bytes())) - err := invoke.UpdatePeerState(np.morphClient, np.netmapContract, + err := invoke.UpdatePeerState(np.morphClient, np.netmapContract, np.feeProvider, &invoke.UpdatePeerArgs{ Key: ev.PublicKey(), Status: ev.Status(), diff --git a/pkg/innerring/processors/netmap/processor.go b/pkg/innerring/processors/netmap/processor.go index 2f480665d..88a37f86b 100644 --- a/pkg/innerring/processors/netmap/processor.go +++ b/pkg/innerring/processors/netmap/processor.go @@ -3,6 +3,7 @@ package netmap import ( "github.com/nspcc-dev/neo-go/pkg/util" "github.com/nspcc-dev/neofs-api-go/pkg/netmap" + "github.com/nspcc-dev/neofs-node/pkg/innerring/config" "github.com/nspcc-dev/neofs-node/pkg/morph/client" container "github.com/nspcc-dev/neofs-node/pkg/morph/client/container/wrapper" "github.com/nspcc-dev/neofs-node/pkg/morph/event" @@ -64,6 +65,9 @@ type ( handleAlphabetSync event.Handler nodeValidator NodeValidator + + notaryDisabled bool + feeProvider *config.FeeConfig } // Params of the processor constructor. @@ -84,6 +88,9 @@ type ( AlphabetSyncHandler event.Handler NodeValidator NodeValidator + + NotaryDisabled bool + FeeProvider *config.FeeConfig } ) @@ -116,6 +123,8 @@ func New(p *Params) (*Processor, error) { return nil, errors.New("ir/netmap: container contract wrapper is not set") case p.NodeValidator == nil: return nil, errors.New("ir/netmap: node validator is not set") + case p.FeeProvider == nil: + return nil, errors.New("ir/netmap: fee provider is not set") } p.Log.Debug("netmap worker pool", zap.Int("size", p.PoolSize)) @@ -142,6 +151,9 @@ func New(p *Params) (*Processor, error) { handleAlphabetSync: p.AlphabetSyncHandler, nodeValidator: p.NodeValidator, + + notaryDisabled: p.NotaryDisabled, + feeProvider: p.FeeProvider, }, nil } diff --git a/pkg/innerring/processors/reputation/process_put.go b/pkg/innerring/processors/reputation/process_put.go index c1f97cfd9..c346fa7cd 100644 --- a/pkg/innerring/processors/reputation/process_put.go +++ b/pkg/innerring/processors/reputation/process_put.go @@ -41,7 +41,14 @@ func (rp *Processor) processPut(epoch uint64, id reputation.PeerID, value reputa args.SetPeerID(id) args.SetValue(value) - err := rp.reputationWrp.PutViaNotary(args) + var err error + + if rp.notaryDisabled { + err = rp.reputationWrp.Put(args) + } else { + err = rp.reputationWrp.PutViaNotary(args) + } + if err != nil { rp.log.Warn("can't send approval tx for reputation value", zap.String("peer_id", hex.EncodeToString(id.ToV2().GetValue())), diff --git a/pkg/innerring/processors/reputation/processor.go b/pkg/innerring/processors/reputation/processor.go index 561b221c7..e5dca8f5a 100644 --- a/pkg/innerring/processors/reputation/processor.go +++ b/pkg/innerring/processors/reputation/processor.go @@ -26,6 +26,8 @@ type ( log *zap.Logger pool *ants.Pool + notaryDisabled bool + reputationContract util.Uint160 epochState EpochState @@ -38,6 +40,7 @@ type ( Params struct { Log *zap.Logger PoolSize int + NotaryDisabled bool ReputationContract util.Uint160 EpochState EpochState AlphabetState AlphabetState @@ -72,6 +75,7 @@ func New(p *Params) (*Processor, error) { return &Processor{ log: p.Log, pool: pool, + notaryDisabled: p.NotaryDisabled, reputationContract: p.ReputationContract, epochState: p.EpochState, alphabetState: p.AlphabetState,