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

* [NOD-510] Change coinbase flags to kaspad. * [NOD-510] Removed superfluous spaces after periods in comments. * [NOD-510] Rename btcd -> kaspad in the root folder. * [NOD-510] Rename BtcEncode -> KaspaEncode and BtcDecode -> KaspaDecode. * [NOD-510] Rename BtcEncode -> KaspaEncode and BtcDecode -> KaspaDecode. * [NOD-510] Continue renaming btcd -> kaspad. * [NOD-510] Rename btcjson -> kaspajson. * [NOD-510] Rename file names inside kaspajson. * [NOD-510] Rename kaspajson -> jsonrpc. * [NOD-510] Finish renaming in addrmgr. * [NOD-510] Rename package btcec to ecc. * [NOD-510] Finish renaming stuff in blockdag. * [NOD-510] Rename stuff in cmd. * [NOD-510] Rename stuff in config. * [NOD-510] Rename stuff in connmgr. * [NOD-510] Rename stuff in dagconfig. * [NOD-510] Rename stuff in database. * [NOD-510] Rename stuff in docker. * [NOD-510] Rename stuff in integration. * [NOD-510] Rename jsonrpc to rpcmodel. * [NOD-510] Rename stuff in limits. * [NOD-510] Rename stuff in logger. * [NOD-510] Rename stuff in mempool. * [NOD-510] Rename stuff in mining. * [NOD-510] Rename stuff in netsync. * [NOD-510] Rename stuff in peer. * [NOD-510] Rename stuff in release. * [NOD-510] Rename stuff in rpcclient. * [NOD-510] Rename stuff in server. * [NOD-510] Rename stuff in signal. * [NOD-510] Rename stuff in txscript. * [NOD-510] Rename stuff in util. * [NOD-510] Rename stuff in wire. * [NOD-510] Fix failing tests. * [NOD-510] Fix merge errors. * [NOD-510] Fix go vet errors. * [NOD-510] Remove merged file that's no longer relevant. * [NOD-510] Add a comment above Op0. * [NOD-510] Fix some comments referencing Bitcoin Core. * [NOD-510] Fix some more comments referencing Bitcoin Core. * [NOD-510] Fix bitcoin -> kaspa. * [NOD-510] Fix more bitcoin -> kaspa. * [NOD-510] Fix comments, remove DisconnectBlock in addrindex. * [NOD-510] Rename KSPD to KASD. * [NOD-510] Fix comments and user agent.
145 lines
3.7 KiB
Go
145 lines
3.7 KiB
Go
package server
|
|
|
|
import (
|
|
"sync/atomic"
|
|
"time"
|
|
|
|
"github.com/kaspanet/kaspad/config"
|
|
"github.com/kaspanet/kaspad/dagconfig"
|
|
"github.com/kaspanet/kaspad/database"
|
|
"github.com/kaspanet/kaspad/mempool"
|
|
"github.com/kaspanet/kaspad/mining"
|
|
"github.com/kaspanet/kaspad/mining/cpuminer"
|
|
"github.com/kaspanet/kaspad/server/p2p"
|
|
"github.com/kaspanet/kaspad/server/rpc"
|
|
"github.com/kaspanet/kaspad/signal"
|
|
)
|
|
|
|
// Server is a wrapper for p2p server and rpc server
|
|
type Server struct {
|
|
rpcServer *rpc.Server
|
|
p2pServer *p2p.Server
|
|
cpuminer *cpuminer.CPUMiner
|
|
startupTime int64
|
|
|
|
started, shutdown int32
|
|
}
|
|
|
|
// Start begins accepting connections from peers.
|
|
func (s *Server) Start() {
|
|
// Already started?
|
|
if atomic.AddInt32(&s.started, 1) != 1 {
|
|
return
|
|
}
|
|
|
|
log.Trace("Starting server")
|
|
|
|
// Server startup time. Used for the uptime command for uptime calculation.
|
|
s.startupTime = time.Now().Unix()
|
|
|
|
s.p2pServer.Start()
|
|
|
|
// Start the CPU miner if generation is enabled.
|
|
cfg := config.ActiveConfig()
|
|
if cfg.Generate {
|
|
s.cpuminer.Start()
|
|
}
|
|
|
|
if !cfg.DisableRPC {
|
|
|
|
s.rpcServer.Start()
|
|
}
|
|
}
|
|
|
|
// Stop gracefully shuts down the server by stopping and disconnecting all
|
|
// peers and the main listener.
|
|
func (s *Server) Stop() error {
|
|
// Make sure this only happens once.
|
|
if atomic.AddInt32(&s.shutdown, 1) != 1 {
|
|
log.Infof("Server is already in the process of shutting down")
|
|
return nil
|
|
}
|
|
|
|
log.Warnf("Server shutting down")
|
|
|
|
// Stop the CPU miner if needed
|
|
s.cpuminer.Stop()
|
|
|
|
s.p2pServer.Stop()
|
|
|
|
// Shutdown the RPC server if it's not disabled.
|
|
if !config.ActiveConfig().DisableRPC {
|
|
s.rpcServer.Stop()
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
// NewServer returns a new kaspad server configured to listen on addr for the
|
|
// kaspa network type specified by dagParams. Use start to begin accepting
|
|
// connections from peers.
|
|
func NewServer(listenAddrs []string, db database.DB, dagParams *dagconfig.Params, interrupt <-chan struct{}) (*Server, error) {
|
|
s := &Server{}
|
|
var err error
|
|
notifyNewTransactions := func(txns []*mempool.TxDesc) {
|
|
// Notify both websocket and getblocktemplate long poll clients of all
|
|
// newly accepted transactions.
|
|
if s.rpcServer != nil {
|
|
s.rpcServer.NotifyNewTransactions(txns)
|
|
}
|
|
}
|
|
s.p2pServer, err = p2p.NewServer(listenAddrs, db, dagParams, interrupt, notifyNewTransactions)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
cfg := config.ActiveConfig()
|
|
|
|
// Create the mining policy and block template generator based on the
|
|
// configuration options.
|
|
//
|
|
// NOTE: The CPU miner relies on the mempool, so the mempool has to be
|
|
// created before calling the function to create the CPU miner.
|
|
policy := mining.Policy{
|
|
BlockMaxMass: cfg.BlockMaxMass,
|
|
}
|
|
blockTemplateGenerator := mining.NewBlkTmplGenerator(&policy,
|
|
s.p2pServer.DAGParams, s.p2pServer.TxMemPool, s.p2pServer.DAG, s.p2pServer.TimeSource, s.p2pServer.SigCache)
|
|
s.cpuminer = cpuminer.New(&cpuminer.Config{
|
|
DAGParams: dagParams,
|
|
BlockTemplateGenerator: blockTemplateGenerator,
|
|
MiningAddrs: cfg.MiningAddrs,
|
|
ProcessBlock: s.p2pServer.SyncManager.ProcessBlock,
|
|
ConnectedCount: s.p2pServer.ConnectedCount,
|
|
ShouldMineOnGenesis: s.p2pServer.ShouldMineOnGenesis,
|
|
IsCurrent: s.p2pServer.SyncManager.IsCurrent,
|
|
})
|
|
|
|
if !cfg.DisableRPC {
|
|
|
|
s.rpcServer, err = rpc.NewRPCServer(
|
|
s.startupTime,
|
|
s.p2pServer,
|
|
db,
|
|
blockTemplateGenerator,
|
|
s.cpuminer,
|
|
)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
// Signal process shutdown when the RPC server requests it.
|
|
spawn(func() {
|
|
<-s.rpcServer.RequestedProcessShutdown()
|
|
signal.ShutdownRequestChannel <- struct{}{}
|
|
})
|
|
}
|
|
|
|
return s, nil
|
|
}
|
|
|
|
// WaitForShutdown blocks until the main listener and peer handlers are stopped.
|
|
func (s *Server) WaitForShutdown() {
|
|
s.p2pServer.WaitForShutdown()
|
|
}
|