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

* [NOD-1162] Separate kaspad to it's own package, so that I can use it out of integration test * [NOD-1162] Begin integration tests * [NOD-1162] [FIX] Assign cfg to RPCServer * [NOD-1162] Basic integration test ready * [NOD-1162] Wait for connection for real * [NOD-1162] [FIX] Connection manager should run the moment it adds a request * [NOD-1162] Make connect something that can be invoked in middle of test * [NOD-1162] Complete first integration test * [NOD-1162] Undo refactor error * [NOD-1162] Rename Kaspad to App * [NOD-1162] Convert checking connection to polling * [NOD-1162] [FIX] Set peerID on handshake * [NOD-1162] [FIX] Broadcast should send to outgoing route, not incoming * [NOD-1162] [FIX] Add CmdInvRelayBlock to MakeEmptyMessage * [NOD-1162] [FIX] Initialize Hash before decoding MsgInvRelayBlock * [NOD-1162] [FIX] Invert condition * [NOD-1162] [FIX] Fixes to encoding of MsgGetRelayBlocks * [NOD-1162] [FIX] Add MsgGetRelayBlocks to MakeEmptyMessage * [NOD-1162] [FIX] Connection manager should run the moment it adds a request * [NOD-1162] [FIX] Set peerID on handshake * [NOD-1162] [FIX] Broadcast should send to outgoing route, not incoming * [NOD-1162] [FIX] Add CmdInvRelayBlock to MakeEmptyMessage * [NOD-1162] [FIX] Initialize Hash before decoding MsgInvRelayBlock * [NOD-1162] [FIX] Invert condition * [NOD-1162] [FIX] Fixes to encoding of MsgGetRelayBlocks * [NOD-1162] [FIX] Add MsgGetRelayBlocks to MakeEmptyMessage * [NOD-1162] Add comment * [NOD-1162] Added support for 3 nodes and clients in integration tests * [NOD-1162] Add third node to integration test * [NOD-1192] Use lock-less functions in TxPool.HandleNewBlock * [NOD-1192] Broadcast transactions only if there's more then 0 * [NOD-1162] Removed double waitTillNextIteration * [NOD-1192] Rename: broadcastTransactions -> broadcastTransactionsAfterBlockAdded * [NOD-1162] Call NotifyBlocks on client3 as well * [NOD-1162] ErrTimeout and ErrRouteClosed should be ProtocolErrors * [NOD-1162] Added comment and removed redundant type PeerAddedCallback * [NOD-1162] Revert overly eager rename * [NOD-1162] Move DisalbeTLS to common config + minimize call for ioutil.TempDir() * [NOD-1162] Add some clarifications in code * [NOD-1193] Skip closed connections in NetAdapter.Broadcast * [NOD-1193] Make sure to protect connectionsToRouters from concurrent access * [NOD-1162] Add _test to all files in integration package * [NOD-1162] Introduced appHarness to better encapsulate a single node * [NOD-1162] Removed onChainChanged handler * [NOD-1162] Remove redundant closure * [NOD-1162] Correctly mark integration_test config as Simnet * [NOD-1162] Rename app.ID -> app.P2PNodeID
218 lines
5.4 KiB
Go
218 lines
5.4 KiB
Go
// Copyright (c) 2013-2016 The btcsuite developers
|
|
// Use of this source code is governed by an ISC
|
|
// license that can be found in the LICENSE file.
|
|
|
|
package main
|
|
|
|
import (
|
|
"fmt"
|
|
_ "net/http/pprof"
|
|
"os"
|
|
"path/filepath"
|
|
"runtime"
|
|
"runtime/pprof"
|
|
"time"
|
|
|
|
"github.com/kaspanet/kaspad/app"
|
|
|
|
"github.com/kaspanet/kaspad/dbaccess"
|
|
|
|
"github.com/kaspanet/kaspad/blockdag/indexers"
|
|
"github.com/kaspanet/kaspad/config"
|
|
"github.com/kaspanet/kaspad/limits"
|
|
"github.com/kaspanet/kaspad/signal"
|
|
"github.com/kaspanet/kaspad/util/panics"
|
|
"github.com/kaspanet/kaspad/util/profiling"
|
|
"github.com/kaspanet/kaspad/version"
|
|
)
|
|
|
|
const (
|
|
// blockDbNamePrefix is the prefix for the block database name. The
|
|
// database type is appended to this value to form the full block
|
|
// database name.
|
|
blockDbNamePrefix = "blocks"
|
|
)
|
|
|
|
// winServiceMain is only invoked on Windows. It detects when kaspad is running
|
|
// as a service and reacts accordingly.
|
|
var winServiceMain func() (bool, error)
|
|
|
|
// kaspadMain is the real main function for kaspad. It is necessary to work
|
|
// around the fact that deferred functions do not run when os.Exit() is called.
|
|
// The optional startedChan writes once all services has started.
|
|
func kaspadMain(startedChan chan<- struct{}) error {
|
|
interrupt := signal.InterruptListener()
|
|
|
|
// Load configuration and parse command line. This function also
|
|
// initializes logging and configures it accordingly.
|
|
cfg, _, err := config.LoadConfig()
|
|
if err != nil {
|
|
return err
|
|
}
|
|
defer panics.HandlePanic(log, "MAIN", nil)
|
|
|
|
// Get a channel that will be closed when a shutdown signal has been
|
|
// triggered either from an OS signal such as SIGINT (Ctrl+C) or from
|
|
// another subsystem such as the RPC server.
|
|
defer log.Info("Shutdown complete")
|
|
|
|
// Show version at startup.
|
|
log.Infof("Version %s", version.Version())
|
|
|
|
// Enable http profiling server if requested.
|
|
if cfg.Profile != "" {
|
|
profiling.Start(cfg.Profile, log)
|
|
}
|
|
|
|
// Write cpu profile if requested.
|
|
if cfg.CPUProfile != "" {
|
|
f, err := os.Create(cfg.CPUProfile)
|
|
if err != nil {
|
|
log.Errorf("Unable to create cpu profile: %s", err)
|
|
return err
|
|
}
|
|
pprof.StartCPUProfile(f)
|
|
defer f.Close()
|
|
defer pprof.StopCPUProfile()
|
|
}
|
|
|
|
// Perform upgrades to kaspad as new versions require it.
|
|
if err := doUpgrades(); err != nil {
|
|
log.Errorf("%s", err)
|
|
return err
|
|
}
|
|
|
|
// Return now if an interrupt signal was triggered.
|
|
if signal.InterruptRequested(interrupt) {
|
|
return nil
|
|
}
|
|
|
|
if cfg.ResetDatabase {
|
|
err := removeDatabase(cfg)
|
|
if err != nil {
|
|
log.Errorf("%s", err)
|
|
return err
|
|
}
|
|
}
|
|
|
|
// Open the database
|
|
databaseContext, err := openDB(cfg)
|
|
if err != nil {
|
|
log.Errorf("%s", err)
|
|
return err
|
|
}
|
|
defer func() {
|
|
log.Infof("Gracefully shutting down the database...")
|
|
err := databaseContext.Close()
|
|
if err != nil {
|
|
log.Errorf("Failed to close the database: %s", err)
|
|
}
|
|
}()
|
|
|
|
// Return now if an interrupt signal was triggered.
|
|
if signal.InterruptRequested(interrupt) {
|
|
return nil
|
|
}
|
|
|
|
// Drop indexes and exit if requested.
|
|
if cfg.DropAcceptanceIndex {
|
|
if err := indexers.DropAcceptanceIndex(databaseContext); err != nil {
|
|
log.Errorf("%s", err)
|
|
return err
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
// Create app and start it.
|
|
app, err := app.New(cfg, databaseContext, interrupt)
|
|
if err != nil {
|
|
log.Errorf("Unable to start kaspad: %+v", err)
|
|
return err
|
|
}
|
|
defer func() {
|
|
log.Infof("Gracefully shutting down kaspad...")
|
|
err := app.Stop()
|
|
if err != nil {
|
|
log.Errorf("Error stopping kaspad: %+v", err)
|
|
}
|
|
|
|
shutdownDone := make(chan struct{})
|
|
go func() {
|
|
app.WaitForShutdown()
|
|
shutdownDone <- struct{}{}
|
|
}()
|
|
|
|
const shutdownTimeout = 2 * time.Minute
|
|
|
|
select {
|
|
case <-shutdownDone:
|
|
case <-time.After(shutdownTimeout):
|
|
log.Criticalf("Graceful shutdown timed out %s. Terminating...", shutdownTimeout)
|
|
}
|
|
log.Infof("Kaspad shutdown complete")
|
|
}()
|
|
app.Start()
|
|
if startedChan != nil {
|
|
startedChan <- struct{}{}
|
|
}
|
|
|
|
// Wait until the interrupt signal is received from an OS signal or
|
|
// shutdown is requested through one of the subsystems such as the RPC
|
|
// server.
|
|
<-interrupt
|
|
return nil
|
|
}
|
|
|
|
func removeDatabase(cfg *config.Config) error {
|
|
dbPath := blockDbPath(cfg)
|
|
return os.RemoveAll(dbPath)
|
|
}
|
|
|
|
// removeRegressionDB removes the existing regression test database if running
|
|
// in regression test mode and it already exists.
|
|
|
|
// dbPath returns the path to the block database given a database type.
|
|
func blockDbPath(cfg *config.Config) string {
|
|
// The database name is based on the database type.
|
|
dbName := blockDbNamePrefix + "_" + cfg.DbType
|
|
dbPath := filepath.Join(cfg.DataDir, dbName)
|
|
return dbPath
|
|
}
|
|
|
|
func openDB(cfg *config.Config) (*dbaccess.DatabaseContext, error) {
|
|
dbPath := filepath.Join(cfg.DataDir, "db")
|
|
log.Infof("Loading database from '%s'", dbPath)
|
|
return dbaccess.New(dbPath)
|
|
}
|
|
|
|
func main() {
|
|
// Use all processor cores.
|
|
runtime.GOMAXPROCS(runtime.NumCPU())
|
|
|
|
// Up some limits.
|
|
if err := limits.SetLimits(); err != nil {
|
|
fmt.Fprintf(os.Stderr, "failed to set limits: %s\n", err)
|
|
os.Exit(1)
|
|
}
|
|
|
|
// Call serviceMain on Windows to handle running as a service. When
|
|
// the return isService flag is true, exit now since we ran as a
|
|
// service. Otherwise, just fall through to normal operation.
|
|
if runtime.GOOS == "windows" {
|
|
isService, err := winServiceMain()
|
|
if err != nil {
|
|
fmt.Println(err)
|
|
os.Exit(1)
|
|
}
|
|
if isService {
|
|
os.Exit(0)
|
|
}
|
|
}
|
|
|
|
// Work around defer not working after os.Exit()
|
|
if err := kaspadMain(nil); err != nil {
|
|
os.Exit(1)
|
|
}
|
|
}
|