mirror of
https://github.com/kaspanet/kaspad.git
synced 2025-03-30 15:08:33 +00:00

* [NOD-1313] Refactor AddressManager (#918) * [NOD-1313] Refactor AddressManager. * [NOD-1313]Remove old tests.Minor improvements,fixes. * [NOD-1313] After merge fixes. Fix import cycle. * [NOD-1313] Integration tests fixes. * [NOD-1313] Allocate new slice for the returned key. * [NOD-1313] AddressManager improvements and fixes. * Move local and banned addresses to separate lists. * Move AddressManager config to the separate file. * Add LocalAddressManager. * Remove redundant KnownAddress structure. * Restore local addresses functionality. * Call initListeners from the LocalAddressManager. * AddressManager minor improvements and fixes. * [NOD-1313] Minor fixes. * [NOD-1313] Implement HandleGetPeerAddresses. Refactoring. * [NOD-1313] After-merge fixes. * [NOD-1313] Minor improvements. * AddressManager: added BannedAddresses() method. * AddressManager: HandleGetPeerAddresses() add banned addresses separately. * AddressManager: remove addressEntry redundant struct. * ConnectionManager: checkOutgoingConnections() minor improvements and fixes. * Minor refactoring. * Minor fixes. * [NOD-1313] GetPeerAddresses RPC message update * GetPeerAddresses RPC: add BannedAddresses in the separate field. * Update protobuf. * [NOD-1534] Update messages.pb.go Co-authored-by: Kirill <gammerxpower@gmail.com>
162 lines
4.5 KiB
Go
162 lines
4.5 KiB
Go
package app
|
|
|
|
import (
|
|
"fmt"
|
|
"sync/atomic"
|
|
|
|
infrastructuredatabase "github.com/kaspanet/kaspad/infrastructure/db/database"
|
|
|
|
"github.com/kaspanet/kaspad/domain"
|
|
|
|
"github.com/kaspanet/kaspad/infrastructure/network/addressmanager"
|
|
|
|
"github.com/kaspanet/kaspad/infrastructure/network/netadapter/id"
|
|
|
|
"github.com/kaspanet/kaspad/app/appmessage"
|
|
"github.com/kaspanet/kaspad/app/protocol"
|
|
"github.com/kaspanet/kaspad/app/rpc"
|
|
"github.com/kaspanet/kaspad/infrastructure/config"
|
|
"github.com/kaspanet/kaspad/infrastructure/network/connmanager"
|
|
"github.com/kaspanet/kaspad/infrastructure/network/dnsseed"
|
|
"github.com/kaspanet/kaspad/infrastructure/network/netadapter"
|
|
"github.com/kaspanet/kaspad/util/panics"
|
|
)
|
|
|
|
// ComponentManager is a wrapper for all the kaspad services
|
|
type ComponentManager struct {
|
|
cfg *config.Config
|
|
addressManager *addressmanager.AddressManager
|
|
protocolManager *protocol.Manager
|
|
rpcManager *rpc.Manager
|
|
connectionManager *connmanager.ConnectionManager
|
|
netAdapter *netadapter.NetAdapter
|
|
|
|
started, shutdown int32
|
|
}
|
|
|
|
// Start launches all the kaspad services.
|
|
func (a *ComponentManager) Start() {
|
|
// Already started?
|
|
if atomic.AddInt32(&a.started, 1) != 1 {
|
|
return
|
|
}
|
|
|
|
log.Trace("Starting kaspad")
|
|
|
|
err := a.netAdapter.Start()
|
|
if err != nil {
|
|
panics.Exit(log, fmt.Sprintf("Error starting the net adapter: %+v", err))
|
|
}
|
|
|
|
a.maybeSeedFromDNS()
|
|
|
|
a.connectionManager.Start()
|
|
}
|
|
|
|
// Stop gracefully shuts down all the kaspad services.
|
|
func (a *ComponentManager) Stop() {
|
|
// Make sure this only happens once.
|
|
if atomic.AddInt32(&a.shutdown, 1) != 1 {
|
|
log.Infof("Kaspad is already in the process of shutting down")
|
|
return
|
|
}
|
|
|
|
log.Warnf("Kaspad shutting down")
|
|
|
|
a.connectionManager.Stop()
|
|
|
|
err := a.netAdapter.Stop()
|
|
if err != nil {
|
|
log.Errorf("Error stopping the net adapter: %+v", err)
|
|
}
|
|
|
|
return
|
|
}
|
|
|
|
// NewComponentManager returns a new ComponentManager instance.
|
|
// Use Start() to begin all services within this ComponentManager
|
|
func NewComponentManager(cfg *config.Config, db infrastructuredatabase.Database, interrupt chan<- struct{}) (
|
|
*ComponentManager, error) {
|
|
|
|
domain, err := domain.New(cfg.ActiveNetParams, db)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
netAdapter, err := netadapter.NewNetAdapter(cfg)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
addressManager, err := addressmanager.New(addressmanager.NewConfig(cfg))
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
connectionManager, err := connmanager.New(cfg, netAdapter, addressManager)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
protocolManager, err := protocol.NewManager(cfg, domain, netAdapter, addressManager, connectionManager)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
rpcManager := setupRPC(cfg, domain, netAdapter, protocolManager, connectionManager, addressManager, interrupt)
|
|
|
|
return &ComponentManager{
|
|
cfg: cfg,
|
|
protocolManager: protocolManager,
|
|
rpcManager: rpcManager,
|
|
connectionManager: connectionManager,
|
|
netAdapter: netAdapter,
|
|
addressManager: addressManager,
|
|
}, nil
|
|
|
|
}
|
|
|
|
func setupRPC(
|
|
cfg *config.Config,
|
|
domain domain.Domain,
|
|
netAdapter *netadapter.NetAdapter,
|
|
protocolManager *protocol.Manager,
|
|
connectionManager *connmanager.ConnectionManager,
|
|
addressManager *addressmanager.AddressManager,
|
|
shutDownChan chan<- struct{},
|
|
) *rpc.Manager {
|
|
|
|
rpcManager := rpc.NewManager(
|
|
cfg, domain, netAdapter, protocolManager, connectionManager, addressManager, shutDownChan)
|
|
protocolManager.SetOnBlockAddedToDAGHandler(rpcManager.NotifyBlockAddedToDAG)
|
|
|
|
return rpcManager
|
|
}
|
|
|
|
func (a *ComponentManager) maybeSeedFromDNS() {
|
|
if !a.cfg.DisableDNSSeed {
|
|
dnsseed.SeedFromDNS(a.cfg.NetParams(), a.cfg.DNSSeed, appmessage.SFNodeNetwork, false, nil,
|
|
a.cfg.Lookup, func(addresses []*appmessage.NetAddress) {
|
|
// Kaspad uses a lookup of the dns seeder here. Since seeder returns
|
|
// IPs of nodes and not its own IP, we can not know real IP of
|
|
// source. So we'll take first returned address as source.
|
|
a.addressManager.AddAddresses(addresses...)
|
|
})
|
|
}
|
|
|
|
if a.cfg.GRPCSeed != "" {
|
|
dnsseed.SeedFromGRPC(a.cfg.NetParams(), a.cfg.GRPCSeed, appmessage.SFNodeNetwork, false, nil,
|
|
func(addresses []*appmessage.NetAddress) {
|
|
a.addressManager.AddAddresses(addresses...)
|
|
})
|
|
}
|
|
}
|
|
|
|
// P2PNodeID returns the network ID associated with this ComponentManager
|
|
func (a *ComponentManager) P2PNodeID() *id.ID {
|
|
return a.netAdapter.ID()
|
|
}
|
|
|
|
// AddressManager returns the AddressManager associated with this ComponentManager
|
|
func (a *ComponentManager) AddressManager() *addressmanager.AddressManager {
|
|
return a.addressManager
|
|
}
|