github.com/mysteriumnetwork/node@v0.0.0-20240516044423-365054f76801/identity/registry/free_registrar.go (about) 1 /* 2 * Copyright (C) 2023 The "MysteriumNetwork/node" Authors. 3 * 4 * This program is free software: you can redistribute it and/or modify 5 * it under the terms of the GNU General Public License as published by 6 * the Free Software Foundation, either version 3 of the License, or 7 * (at your option) any later version. 8 * 9 * This program is distributed in the hope that it will be useful, 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 * GNU General Public License for more details. 13 * 14 * You should have received a copy of the GNU General Public License 15 * along with this program. If not, see <http://www.gnu.org/licenses/>. 16 */ 17 18 package registry 19 20 import ( 21 "fmt" 22 "math/big" 23 "sync" 24 25 "github.com/mysteriumnetwork/node/config" 26 "github.com/mysteriumnetwork/node/core/node/event" 27 "github.com/mysteriumnetwork/node/eventbus" 28 identity_selector "github.com/mysteriumnetwork/node/identity/selector" 29 "github.com/rs/zerolog/log" 30 ) 31 32 // FreeRegistrar is responsible for registering default identity for free 33 type FreeRegistrar struct { 34 lock sync.Mutex 35 selector identity_selector.Handler 36 transactor transactor 37 contractRegistry IdentityRegistry 38 freeRegistrationEnabled bool 39 } 40 41 // NewFreeRegistrar creates new free registrar 42 func NewFreeRegistrar(selector identity_selector.Handler, transactor transactor, contractRegistry IdentityRegistry, freeRegistrationEnabled bool) *FreeRegistrar { 43 return &FreeRegistrar{ 44 selector: selector, 45 transactor: transactor, 46 contractRegistry: contractRegistry, 47 freeRegistrationEnabled: freeRegistrationEnabled, 48 } 49 } 50 51 // Subscribe subscribes to Node events 52 func (f *FreeRegistrar) Subscribe(eb eventbus.Subscriber) error { 53 if !f.freeRegistrationEnabled { 54 return nil 55 } 56 err := eb.SubscribeAsync(event.AppTopicNode, f.handleNodeEvent) 57 return err 58 } 59 60 func (f *FreeRegistrar) handleNodeEvent(ev event.Payload) { 61 if ev.Status == event.StatusStarted { 62 err := f.handleStart() 63 if err != nil { 64 log.Error().Err(err).Msg("failed to handle free registrar start") 65 } 66 return 67 } 68 } 69 70 func (f *FreeRegistrar) handleStart() error { 71 log.Debug().Msg("Try register provider for free") 72 73 chainID := config.GetInt64(config.FlagChainID) 74 id, err := f.selector.UseOrCreate("", "", chainID) 75 if err != nil { 76 log.Error().Err(err).Msg("could not create default identity identity") 77 return nil 78 } 79 status, err := f.contractRegistry.GetRegistrationStatus(chainID, id) 80 if err != nil { 81 log.Error().Err(err).Msg("could not check registration status") 82 return err 83 } 84 if status == Registered { 85 log.Info().Msg("Default identity is already registered") 86 return nil 87 } 88 89 eligible, err := f.transactor.GetFreeProviderRegistrationEligibility() 90 if err != nil { 91 return fmt.Errorf("failed to check free registration eligibility: %w", err) 92 } 93 94 if !eligible { 95 log.Warn().Msg("Free registration is not eligible") 96 return nil 97 } 98 err = f.transactor.RegisterProviderIdentity(id.Address, big.NewInt(0), big.NewInt(0), "", chainID, nil) 99 if err != nil { 100 return fmt.Errorf("could not register identity: %w", err) 101 } 102 log.Info().Msgf("Identity created and registered for free: %s", id.Address) 103 return nil 104 }