github.com/mysteriumnetwork/node@v0.0.0-20240516044423-365054f76801/cmd/di_discovery.go (about)

     1  /*
     2   * Copyright (C) 2020 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 cmd
    19  
    20  import (
    21  	"fmt"
    22  	"time"
    23  
    24  	"github.com/mysteriumnetwork/node/core/discovery"
    25  	"github.com/mysteriumnetwork/node/core/discovery/apidiscovery"
    26  	"github.com/mysteriumnetwork/node/core/discovery/brokerdiscovery"
    27  	"github.com/mysteriumnetwork/node/core/discovery/dhtdiscovery"
    28  	"github.com/mysteriumnetwork/node/core/discovery/proposal"
    29  	"github.com/mysteriumnetwork/node/core/node"
    30  	"github.com/mysteriumnetwork/node/core/service"
    31  	"github.com/pkg/errors"
    32  )
    33  
    34  func (di *Dependencies) bootstrapDiscoveryComponents(options node.OptionsDiscovery) error {
    35  	di.FilterPresetStorage = proposal.NewFilterPresetStorage(di.Storage)
    36  	proposalRepository := discovery.NewRepository()
    37  	proposalRegistry := discovery.NewRegistry()
    38  	discoveryWorker := discovery.NewWorker()
    39  
    40  	for _, discoveryType := range options.Types {
    41  		switch discoveryType {
    42  		case node.DiscoveryTypeAPI:
    43  			// Broker is the way to announce node presence currently, so enabled by default no matter the users preferences.
    44  			proposalRegistry.AddRegistry(brokerdiscovery.NewRegistry(di.BrokerConnection))
    45  			proposalRepository.Add(apidiscovery.NewRepository(di.MysteriumAPI))
    46  
    47  		case node.DiscoveryTypeBroker:
    48  			storage := brokerdiscovery.NewStorage(di.EventBus)
    49  			brokerRepository := brokerdiscovery.NewRepository(di.BrokerConnection, storage, options.PingInterval+time.Second, 1*time.Second)
    50  			if options.FetchEnabled {
    51  				discoveryWorker.AddWorker(brokerRepository)
    52  			}
    53  
    54  			proposalRegistry.AddRegistry(brokerdiscovery.NewRegistry(di.BrokerConnection))
    55  			proposalRepository.Add(brokerRepository)
    56  
    57  		case node.DiscoveryTypeDHT:
    58  			dhtNode, err := dhtdiscovery.NewNode(
    59  				fmt.Sprintf("/ip4/%s/%s/%d", options.DHT.Address, options.DHT.Protocol, options.DHT.Port),
    60  				options.DHT.BootstrapPeers,
    61  			)
    62  			if err != nil {
    63  				return errors.Wrap(err, "failed to configure DHT node")
    64  			}
    65  			discoveryWorker.AddWorker(dhtNode)
    66  
    67  			proposalRegistry.AddRegistry(dhtdiscovery.NewRegistry())
    68  			proposalRepository.Add(dhtdiscovery.NewRepository())
    69  
    70  		default:
    71  			return errors.Errorf("unknown discovery adapter: %s", discoveryType)
    72  		}
    73  	}
    74  
    75  	di.DiscoveryWorker = discoveryWorker
    76  	if err := di.DiscoveryWorker.Start(); err != nil {
    77  		return errors.Wrap(err, "failed to start discovery")
    78  	}
    79  
    80  	di.ProposalRepository = discovery.NewPricedServiceProposalRepository(proposalRepository, di.PricingHelper, di.FilterPresetStorage)
    81  	di.DiscoveryFactory = func() service.Discovery {
    82  		return discovery.NewService(di.IdentityRegistry, proposalRegistry, options.PingInterval, di.SignerFactory, di.EventBus)
    83  	}
    84  	return nil
    85  }