mirror of
https://github.com/kaspanet/kaspad.git
synced 2025-05-19 13:26:47 +00:00

* [NOD-1579] Rename AcceptedTxIDs to AcceptedTransactionIDs. * [NOD-1579] Add InsertBlockResult to ValidateAndInsertBlock results. * [NOD-1593] Rename InsertBlockResult to BlockInsertionResult. * [NOD-1593] Add SelectedParentChainChanges to AddBlockToVirtual's result. * [NOD-1593] Implement findSelectedParentChainChanges. * [NOD-1593] Implement TestFindSelectedParentChainChanges. * [NOD-1593] Fix a string. * [NOD-1593] Finish implementing TestFindSelectedParentChainChanges. * [NOD-1593] Fix merge errors. * [NOD-1597] Begin implementing UTXOIndex. * [NOD-1597] Connect UTXOIndex to RPC. * [NOD-1597] Connect Consensus to UTXOIndex. * [NOD-1597] Add AcceptanceData to BlockInfo. * [NOD-1597] Implement UTXOIndex.Update(). * [NOD-1597] Implement add(), remove(), and discard() in utxoIndexStore. * [NOD-1597] Add error cases to add() and remove(). * [NOD-1597] Add special cases to add() and remove(). * [NOD-1597] Implement commit. * [NOD-1597] Add a mutex around UTXOIndex.Update(). * [NOD-1597] Return changes to the UTXO from Update(). * [NOD-1597] Add NotifyUTXOsChangedRequestMessage and related structs. * [NOD-1597] Implement HandleNotifyUTXOsChanged. * [NOD-1597] Begin implementing TestUTXOIndex. * [NOD-1597] Implement RegisterForUTXOsChangedNotifications. * [NOD-1597] Fix bad transaction.ID usage. * [NOD-1597] Implement convertUTXOChangesToUTXOsChangedNotification. * [NOD-1597] Make UTXOsChangedNotificationMessage.Removed UTXOsByAddressesEntry instead of just RPCOutpoint so that the client can discern which address was the UTXO removed for. * [NOD-1597] Collect outpoints in TestUTXOIndex. * [NOD-1597] Rename RPC stuff. * [NOD-1597] Add messages for GetUTXOsByAddresses. * [NOD-1597] Implement HandleGetUTXOsByAddresses. * [NOD-1597] Implement GetUTXOsByAddresses. * [NOD-1597] Implement UTXOs(). * [NOD-1597] Implement getUTXOOutpointEntryPairs(). * [NOD-1597] Expand TestUTXOIndex. * [NOD-1597] Convert SubmitTransaction to use RPCTransaction instead of MsgTx. * [NOD-1597] Finish implementing TestUTXOIndex. * [NOD-1597] Add messages for GetVirtualSelectedParentBlueScore. * [NOD-1597] Implement HandleGetVirtualSelectedParentBlueScore and GetVirtualSelectedParentBlueScore. * [NOD-1597] Implement TestVirtualSelectedParentBlueScore. * [NOD-1597] Implement NotifyVirtualSelectedParentBlueScoreChanged. * [NOD-1597] Expand TestVirtualSelectedParentBlueScore. * [NOD-1597] Implement notifyVirtualSelectedParentBlueScoreChanged. * [NOD-1597] Make go lint happy. * [NOD-1593] Fix merge errors. * [NOD-1593] Rename findSelectedParentChainChanges to calculateSelectedParentChainChanges. * [NOD-1593] Expand TestCalculateSelectedParentChainChanges. * [NOD-1597] Add logs to utxoindex.go. * [NOD-1597] Add logs to utxoindex/store.go. * [NOD-1597] Add logs to RPCManager.NotifyXXX functions. * [NOD-1597] Ignore transactions that aren't accepted. * [NOD-1597] Use GetBlockAcceptanceData instead of GetBlockInfo. * [NOD-1597] Convert scriptPublicKey to string directly, instead of using hex. * [NOD-1597] Add a comment. * [NOD-1597] Guard against calling utxoindex methods when utxoindex is turned off. * [NOD-1597] Add lock to UTXOs. * [NOD-1597] Guard against calls to getUTXOOutpointEntryPairs when staging isn't empty.
176 lines
4.7 KiB
Go
176 lines
4.7 KiB
Go
package app
|
|
|
|
import (
|
|
"fmt"
|
|
"github.com/kaspanet/kaspad/domain/utxoindex"
|
|
"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
|
|
}
|
|
|
|
var utxoIndex *utxoindex.UTXOIndex
|
|
if cfg.UTXOIndex {
|
|
utxoIndex = utxoindex.New(domain.Consensus(), db)
|
|
log.Infof("UTXO index started")
|
|
}
|
|
|
|
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, utxoIndex, 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,
|
|
utxoIndex *utxoindex.UTXOIndex,
|
|
shutDownChan chan<- struct{},
|
|
) *rpc.Manager {
|
|
|
|
rpcManager := rpc.NewManager(
|
|
cfg,
|
|
domain,
|
|
netAdapter,
|
|
protocolManager,
|
|
connectionManager,
|
|
addressManager,
|
|
utxoIndex,
|
|
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...)
|
|
})
|
|
|
|
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
|
|
}
|