From e05f1e139411e71957caee28fb4ecef03a12bded Mon Sep 17 00:00:00 2001 From: Alex Vanin Date: Tue, 23 Mar 2021 18:20:44 +0300 Subject: [PATCH] [#446] innerring: Use alphabet state in processors Signed-off-by: Alex Vanin --- pkg/innerring/innerring.go | 21 ++++++++++++++----- .../processors/alphabet/process_emit.go | 4 ++-- .../processors/alphabet/processor.go | 2 +- .../processors/balance/process_assets.go | 4 ++-- pkg/innerring/processors/balance/processor.go | 14 ++++++------- .../processors/container/process_container.go | 8 +++---- .../processors/container/processor.go | 14 ++++++------- .../processors/neofs/process_assets.go | 12 +++++------ .../processors/neofs/process_config.go | 4 ++-- .../processors/neofs/process_update.go | 4 ++-- pkg/innerring/processors/neofs/processor.go | 14 ++++++------- .../processors/netmap/process_cleanup.go | 4 ++-- .../processors/netmap/process_epoch.go | 4 ++-- .../processors/netmap/process_peers.go | 8 +++---- pkg/innerring/processors/netmap/processor.go | 14 ++++++------- pkg/innerring/processors/settlement/calls.go | 8 +++---- .../processors/settlement/processor.go | 10 ++++----- 17 files changed, 80 insertions(+), 69 deletions(-) diff --git a/pkg/innerring/innerring.go b/pkg/innerring/innerring.go index 2e04014e..8a569ddf 100644 --- a/pkg/innerring/innerring.go +++ b/pkg/innerring/innerring.go @@ -431,14 +431,14 @@ func New(ctx context.Context, log *zap.Logger, cfg *viper.Viper) (*Server, error EpochTimer: server, MorphClient: server.morphClient, EpochState: server, - ActiveState: server, + AlphabetState: server, CleanupEnabled: cfg.GetBool("netmap_cleaner.enabled"), CleanupThreshold: cfg.GetUint64("netmap_cleaner.threshold"), ContainerWrapper: cnrClient, HandleAudit: server.onlyActiveEventHandler( auditProcessor.StartAuditHandler(), ), - AuditSettlementsHandler: server.onlyActiveEventHandler( + AuditSettlementsHandler: server.onlyAlphabetEventHandler( settlementProcessor.HandleAuditEvent, ), NodeValidator: locodeValidator, @@ -458,7 +458,7 @@ func New(ctx context.Context, log *zap.Logger, cfg *viper.Viper) (*Server, error PoolSize: cfg.GetInt("workers.container"), ContainerContract: server.contracts.container, MorphClient: server.morphClient, - ActiveState: server, + AlphabetState: server, }) if err != nil { return nil, err @@ -476,7 +476,7 @@ func New(ctx context.Context, log *zap.Logger, cfg *viper.Viper) (*Server, error NeoFSContract: server.contracts.neofs, BalanceContract: server.contracts.balance, MainnetClient: server.mainnetClient, - ActiveState: server, + AlphabetState: server, Converter: &server.precision, }) if err != nil { @@ -499,7 +499,7 @@ func New(ctx context.Context, log *zap.Logger, cfg *viper.Viper) (*Server, error NetmapContract: server.contracts.netmap, MorphClient: server.morphClient, EpochState: server, - ActiveState: server, + AlphabetState: server, Converter: &server.precision, MintEmitCacheSize: cfg.GetInt("emit.mint.cache_size"), MintEmitThreshold: cfg.GetUint64("emit.mint.threshold"), @@ -727,6 +727,7 @@ func (s *Server) initConfigFromBlockchain() error { s.log.Debug("read config from blockchain", zap.Bool("active", s.IsActive()), + zap.Bool("alphabet", s.IsAlphabet()), zap.Int64("epoch", epoch), zap.Uint32("precision", balancePrecision), ) @@ -744,6 +745,16 @@ func (s *Server) onlyActiveEventHandler(f event.Handler) event.Handler { } } +// onlyAlphabet wrapper around event handler that executes it +// only if inner ring node is alphabet node. +func (s *Server) onlyAlphabetEventHandler(f event.Handler) event.Handler { + return func(ev event.Event) { + if s.IsAlphabet() { + f(ev) + } + } +} + func (s *Server) depositNotary() error { return s.morphClient.DepositNotary( s.notaryDepositAmount, diff --git a/pkg/innerring/processors/alphabet/process_emit.go b/pkg/innerring/processors/alphabet/process_emit.go index b258e0ae..250146a3 100644 --- a/pkg/innerring/processors/alphabet/process_emit.go +++ b/pkg/innerring/processors/alphabet/process_emit.go @@ -10,9 +10,9 @@ import ( ) func (np *Processor) processEmit() { - index := np.irList.InnerRingIndex() + index := np.irList.AlphabetIndex() if index < 0 { - np.log.Info("passive mode, ignore gas emission event") + np.log.Info("non alphabet mode, ignore gas emission event") return } diff --git a/pkg/innerring/processors/alphabet/processor.go b/pkg/innerring/processors/alphabet/processor.go index f656c1fe..51a53199 100644 --- a/pkg/innerring/processors/alphabet/processor.go +++ b/pkg/innerring/processors/alphabet/processor.go @@ -12,7 +12,7 @@ import ( type ( // Indexer is a callback interface for inner ring global state. Indexer interface { - InnerRingIndex() int + AlphabetIndex() int } // Contracts is an interface of the storage diff --git a/pkg/innerring/processors/balance/process_assets.go b/pkg/innerring/processors/balance/process_assets.go index 062616e5..d31bc960 100644 --- a/pkg/innerring/processors/balance/process_assets.go +++ b/pkg/innerring/processors/balance/process_assets.go @@ -9,8 +9,8 @@ import ( // Process lock event by invoking Cheque method in main net to send assets // back to the withdraw issuer. func (bp *Processor) processLock(lock *balanceEvent.Lock) { - if !bp.activeState.IsActive() { - bp.log.Info("passive mode, ignore balance lock") + if !bp.alphabetState.IsAlphabet() { + bp.log.Info("non alphabet mode, ignore balance lock") return } diff --git a/pkg/innerring/processors/balance/processor.go b/pkg/innerring/processors/balance/processor.go index 329e31a1..070e6c8b 100644 --- a/pkg/innerring/processors/balance/processor.go +++ b/pkg/innerring/processors/balance/processor.go @@ -11,9 +11,9 @@ import ( ) type ( - // ActiveState is a callback interface for inner ring global state - ActiveState interface { - IsActive() bool + // AlphabetState is a callback interface for inner ring global state + AlphabetState interface { + IsAlphabet() bool } // PrecisionConverter converts balance amount values. @@ -28,7 +28,7 @@ type ( neofsContract util.Uint160 balanceContract util.Uint160 mainnetClient *client.Client - activeState ActiveState + alphabetState AlphabetState converter PrecisionConverter } @@ -39,7 +39,7 @@ type ( NeoFSContract util.Uint160 BalanceContract util.Uint160 MainnetClient *client.Client - ActiveState ActiveState + AlphabetState AlphabetState Converter PrecisionConverter } ) @@ -55,7 +55,7 @@ func New(p *Params) (*Processor, error) { return nil, errors.New("ir/balance: logger is not set") case p.MainnetClient == nil: return nil, errors.New("ir/balance: neo:mainnet client is not set") - case p.ActiveState == nil: + case p.AlphabetState == nil: 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") @@ -74,7 +74,7 @@ func New(p *Params) (*Processor, error) { neofsContract: p.NeoFSContract, balanceContract: p.BalanceContract, mainnetClient: p.MainnetClient, - activeState: p.ActiveState, + alphabetState: p.AlphabetState, converter: p.Converter, }, nil } diff --git a/pkg/innerring/processors/container/process_container.go b/pkg/innerring/processors/container/process_container.go index 9d994344..1e985c83 100644 --- a/pkg/innerring/processors/container/process_container.go +++ b/pkg/innerring/processors/container/process_container.go @@ -11,8 +11,8 @@ import ( // Process new container from the user by checking container sanity // and sending approve tx back to morph. func (cp *Processor) processContainerPut(put *containerEvent.Put) { - if !cp.activeState.IsActive() { - cp.log.Info("passive mode, ignore container put") + if !cp.alphabetState.IsAlphabet() { + cp.log.Info("non alphabet mode, ignore container put") return } @@ -51,8 +51,8 @@ func (cp *Processor) processContainerPut(put *containerEvent.Put) { // Process delete container operation from the user by checking container sanity // and sending approve tx back to morph. func (cp *Processor) processContainerDelete(delete *containerEvent.Delete) { - if !cp.activeState.IsActive() { - cp.log.Info("passive mode, ignore container put") + if !cp.alphabetState.IsAlphabet() { + cp.log.Info("non alphabet mode, ignore container delete") return } diff --git a/pkg/innerring/processors/container/processor.go b/pkg/innerring/processors/container/processor.go index daa34261..c21208a9 100644 --- a/pkg/innerring/processors/container/processor.go +++ b/pkg/innerring/processors/container/processor.go @@ -11,9 +11,9 @@ import ( ) type ( - // ActiveState is a callback interface for inner ring global state. - ActiveState interface { - IsActive() bool + // AlphabetState is a callback interface for inner ring global state. + AlphabetState interface { + IsAlphabet() bool } // Processor of events produced by container contract in morph chain. @@ -22,7 +22,7 @@ type ( pool *ants.Pool containerContract util.Uint160 morphClient *client.Client - activeState ActiveState + alphabetState AlphabetState } // Params of the processor constructor. @@ -31,7 +31,7 @@ type ( PoolSize int ContainerContract util.Uint160 MorphClient *client.Client - ActiveState ActiveState + AlphabetState AlphabetState } ) @@ -47,7 +47,7 @@ func New(p *Params) (*Processor, error) { return nil, errors.New("ir/container: logger is not set") case p.MorphClient == nil: return nil, errors.New("ir/container: neo:morph client is not set") - case p.ActiveState == nil: + case p.AlphabetState == nil: return nil, errors.New("ir/container: global state is not set") } @@ -63,7 +63,7 @@ func New(p *Params) (*Processor, error) { pool: pool, containerContract: p.ContainerContract, morphClient: p.MorphClient, - activeState: p.ActiveState, + alphabetState: p.AlphabetState, }, nil } diff --git a/pkg/innerring/processors/neofs/process_assets.go b/pkg/innerring/processors/neofs/process_assets.go index ca02e76e..56e14b8a 100644 --- a/pkg/innerring/processors/neofs/process_assets.go +++ b/pkg/innerring/processors/neofs/process_assets.go @@ -18,8 +18,8 @@ const ( // Process deposit event by invoking balance contract and sending native // gas in morph chain. func (np *Processor) processDeposit(deposit *neofsEvent.Deposit) { - if !np.activeState.IsActive() { - np.log.Info("passive mode, ignore deposit") + if !np.alphabetState.IsAlphabet() { + np.log.Info("non alphabet mode, ignore deposit") return } @@ -82,8 +82,8 @@ func (np *Processor) processDeposit(deposit *neofsEvent.Deposit) { // Process withdraw event by locking assets in balance account. func (np *Processor) processWithdraw(withdraw *neofsEvent.Withdraw) { - if !np.activeState.IsActive() { - np.log.Info("passive mode, ignore withdraw") + if !np.alphabetState.IsAlphabet() { + np.log.Info("non alphabet mode, ignore withdraw") return } @@ -118,8 +118,8 @@ func (np *Processor) processWithdraw(withdraw *neofsEvent.Withdraw) { // Process cheque event by transferring assets from lock account back to // reserve account. func (np *Processor) processCheque(cheque *neofsEvent.Cheque) { - if !np.activeState.IsActive() { - np.log.Info("passive mode, ignore cheque") + if !np.alphabetState.IsAlphabet() { + np.log.Info("non alphabet mode, ignore cheque") return } diff --git a/pkg/innerring/processors/neofs/process_config.go b/pkg/innerring/processors/neofs/process_config.go index 443344ac..815db93a 100644 --- a/pkg/innerring/processors/neofs/process_config.go +++ b/pkg/innerring/processors/neofs/process_config.go @@ -9,8 +9,8 @@ import ( // Process config event by setting configuration value from main chain in // side chain. func (np *Processor) processConfig(config *neofsEvent.Config) { - if !np.activeState.IsActive() { - np.log.Info("passive mode, ignore deposit") + if !np.alphabetState.IsAlphabet() { + np.log.Info("non alphabet mode, ignore config") return } diff --git a/pkg/innerring/processors/neofs/process_update.go b/pkg/innerring/processors/neofs/process_update.go index c5a56202..e4f72cab 100644 --- a/pkg/innerring/processors/neofs/process_update.go +++ b/pkg/innerring/processors/neofs/process_update.go @@ -9,8 +9,8 @@ import ( // Process update inner ring event by setting inner ring list value from // main chain in side chain. func (np *Processor) processUpdateInnerRing(list *neofsEvent.UpdateInnerRing) { - if !np.activeState.IsActive() { - np.log.Info("passive mode, ignore deposit") + if !np.alphabetState.IsAlphabet() { + np.log.Info("non alphabet mode, ignore inner ring update") return } diff --git a/pkg/innerring/processors/neofs/processor.go b/pkg/innerring/processors/neofs/processor.go index 49b1c7fe..3481d452 100644 --- a/pkg/innerring/processors/neofs/processor.go +++ b/pkg/innerring/processors/neofs/processor.go @@ -20,9 +20,9 @@ type ( EpochCounter() uint64 } - // ActiveState is a callback interface for inner ring global state - ActiveState interface { - IsActive() bool + // AlphabetState is a callback interface for inner ring global state + AlphabetState interface { + IsAlphabet() bool } // PrecisionConverter converts balance amount values. @@ -39,7 +39,7 @@ type ( netmapContract util.Uint160 morphClient *client.Client epochState EpochState - activeState ActiveState + alphabetState AlphabetState converter PrecisionConverter mintEmitLock *sync.Mutex mintEmitCache *lru.Cache @@ -57,7 +57,7 @@ type ( NetmapContract util.Uint160 MorphClient *client.Client EpochState EpochState - ActiveState ActiveState + AlphabetState AlphabetState Converter PrecisionConverter MintEmitCacheSize int MintEmitThreshold uint64 // in epochs @@ -83,7 +83,7 @@ func New(p *Params) (*Processor, error) { return nil, errors.New("ir/neofs: neo:morph client is not set") case p.EpochState == nil: return nil, errors.New("ir/neofs: global state is not set") - case p.ActiveState == nil: + case p.AlphabetState == nil: 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") @@ -109,7 +109,7 @@ func New(p *Params) (*Processor, error) { netmapContract: p.NetmapContract, morphClient: p.MorphClient, epochState: p.EpochState, - activeState: p.ActiveState, + alphabetState: p.AlphabetState, converter: p.Converter, mintEmitLock: new(sync.Mutex), mintEmitCache: lruCache, diff --git a/pkg/innerring/processors/netmap/process_cleanup.go b/pkg/innerring/processors/netmap/process_cleanup.go index d37ddd08..06b30ab4 100644 --- a/pkg/innerring/processors/netmap/process_cleanup.go +++ b/pkg/innerring/processors/netmap/process_cleanup.go @@ -8,8 +8,8 @@ import ( ) func (np *Processor) processNetmapCleanupTick(epoch uint64) { - if !np.activeState.IsActive() { - np.log.Info("passive mode, ignore new netmap cleanup tick") + if !np.alphabetState.IsAlphabet() { + np.log.Info("non alphabet mode, ignore new netmap cleanup tick") return } diff --git a/pkg/innerring/processors/netmap/process_epoch.go b/pkg/innerring/processors/netmap/process_epoch.go index 18ae7946..a1f6517f 100644 --- a/pkg/innerring/processors/netmap/process_epoch.go +++ b/pkg/innerring/processors/netmap/process_epoch.go @@ -42,8 +42,8 @@ func (np *Processor) processNewEpoch(epoch uint64) { // Process new epoch tick by invoking new epoch method in network map contract. func (np *Processor) processNewEpochTick() { - if !np.activeState.IsActive() { - np.log.Info("passive mode, ignore new epoch tick") + if !np.alphabetState.IsAlphabet() { + np.log.Info("non alphabet mode, ignore new epoch tick") return } diff --git a/pkg/innerring/processors/netmap/process_peers.go b/pkg/innerring/processors/netmap/process_peers.go index 974bc508..44ee6234 100644 --- a/pkg/innerring/processors/netmap/process_peers.go +++ b/pkg/innerring/processors/netmap/process_peers.go @@ -14,8 +14,8 @@ import ( // Process add peer notification by sanity check of new node // local epoch timer. func (np *Processor) processAddPeer(node []byte) { - if !np.activeState.IsActive() { - np.log.Info("passive mode, ignore new peer notification") + if !np.alphabetState.IsAlphabet() { + np.log.Info("non alphabet mode, ignore new peer notification") return } @@ -74,8 +74,8 @@ func (np *Processor) processAddPeer(node []byte) { // Process update peer notification by sending approval tx to the smart contract. func (np *Processor) processUpdatePeer(ev netmapEvent.UpdatePeer) { - if !np.activeState.IsActive() { - np.log.Info("passive mode, ignore new epoch tick") + if !np.alphabetState.IsAlphabet() { + np.log.Info("non alphabet mode, ignore update peer notification") return } diff --git a/pkg/innerring/processors/netmap/processor.go b/pkg/innerring/processors/netmap/processor.go index 9f3c6271..9cc62d0b 100644 --- a/pkg/innerring/processors/netmap/processor.go +++ b/pkg/innerring/processors/netmap/processor.go @@ -24,9 +24,9 @@ type ( EpochCounter() uint64 } - // ActiveState is a callback interface for inner ring global state. - ActiveState interface { - IsActive() bool + // AlphabetState is a callback interface for inner ring global state. + AlphabetState interface { + IsAlphabet() bool } // NodeValidator wraps basic method of checking the correctness @@ -52,7 +52,7 @@ type ( netmapContract util.Uint160 epochTimer EpochTimerReseter epochState EpochState - activeState ActiveState + alphabetState AlphabetState morphClient *client.Client containerWrp *container.Wrapper @@ -73,7 +73,7 @@ type ( EpochTimer EpochTimerReseter MorphClient *client.Client EpochState EpochState - ActiveState ActiveState + AlphabetState AlphabetState CleanupEnabled bool CleanupThreshold uint64 // in epochs ContainerWrapper *container.Wrapper @@ -102,7 +102,7 @@ func New(p *Params) (*Processor, error) { return nil, errors.New("ir/netmap: epoch itmer is not set") case p.EpochState == nil: return nil, errors.New("ir/netmap: global state is not set") - case p.ActiveState == nil: + case p.AlphabetState == nil: return nil, errors.New("ir/netmap: global state is not set") case p.HandleAudit == nil: return nil, errors.New("ir/netmap: audit handler is not set") @@ -127,7 +127,7 @@ func New(p *Params) (*Processor, error) { netmapContract: p.NetmapContract, epochTimer: p.EpochTimer, epochState: p.EpochState, - activeState: p.ActiveState, + alphabetState: p.AlphabetState, morphClient: p.MorphClient, containerWrp: p.ContainerWrapper, netmapSnapshot: newCleanupTable(p.CleanupEnabled, p.CleanupThreshold), diff --git a/pkg/innerring/processors/settlement/calls.go b/pkg/innerring/processors/settlement/calls.go index fa6c1190..dae6ae16 100644 --- a/pkg/innerring/processors/settlement/calls.go +++ b/pkg/innerring/processors/settlement/calls.go @@ -45,8 +45,8 @@ func (p *Processor) HandleIncomeCollectionEvent(e event.Event) { ev := e.(BasicIncomeCollectEvent) epoch := ev.Epoch() - if !p.state.IsActive() { - p.log.Info("passive mode, ignore income collection event") + if !p.state.IsAlphabet() { + p.log.Info("non alphabet mode, ignore income collection event") return } @@ -90,8 +90,8 @@ func (p *Processor) HandleIncomeDistributionEvent(e event.Event) { ev := e.(BasicIncomeDistributeEvent) epoch := ev.Epoch() - if !p.state.IsActive() { - p.log.Info("passive mode, ignore income distribution event") + if !p.state.IsAlphabet() { + p.log.Info("non alphabet mode, ignore income distribution event") return } diff --git a/pkg/innerring/processors/settlement/processor.go b/pkg/innerring/processors/settlement/processor.go index fc7856bd..efdf8710 100644 --- a/pkg/innerring/processors/settlement/processor.go +++ b/pkg/innerring/processors/settlement/processor.go @@ -13,16 +13,16 @@ import ( ) type ( - // ActiveState is a callback interface for inner ring global state - ActiveState interface { - IsActive() bool + // AlphabetState is a callback interface for inner ring global state + AlphabetState interface { + IsAlphabet() bool } // Processor is an event handler for payments in the system. Processor struct { log *logger.Logger - state ActiveState + state AlphabetState pool nodeutil.WorkerPool @@ -38,7 +38,7 @@ type ( Prm struct { AuditProcessor AuditProcessor BasicIncome BasicIncomeInitializer - State ActiveState + State AlphabetState } )