mirror of
https://github.com/kaspanet/kaspad.git
synced 2025-06-19 12:36:40 +00:00

* [NOD-289] Implemented database isCurrent checking and connection. * [NOD-289] Added GetChainFromBlock to RPCClient. * [NOD-289] Limited the amount of blocks in GetChainFromBlockResponse. * [NOD-289] Fixed various issues that were keeping GetChainFromBlocks from working properly. * [NOD-289] Created blockloop.go. * [NOD-289] Updated go.mod after merge. * [NOD-289] Implemented collection of current selected parent chain. * [NOD-289] Fixed test. Reverted not deleting utxoDiffData from the DB. * [NOD-289] Implemented GetBlocks. * [NOD-289] Added comment to BlockHashesFrom. * [NOD-289] Added GetBlocks to rpcclient. * [NOD-289] Added verboseBlocks to GetBlocks. * [NOD-289] Implemented block insertion. * [NOD-289] Added AUTO_INCREMENT to tables that were missing it. * [NOD-289] Made gasLimit in subnetwork nullable. * [NOD-289] Renamed transactions_outputs to transaction_outputs. * [NOD-289] Fixed weird coinbase behavior in vin. * [NOD-289] Made collectCurrentBlocks start from the most recent startHash. * [NOD-289] Added IsChainBlock to GetBlockVerboseResult. * [NOD-289] Implemented adding a block from onBlockAdded. * [NOD-289] Added removedParentChainHashes to getChainFromBlock. * [NOD-289] Implemented updating the selected parent chain from onChainChanged. * [NOD-289] Implemented some initial logic for updating the UTXO. * [NOD-289] Fixed merge errors. * [NOD-326] Fixed some more merge errors. * [NOD-289] Added error handling for missing required records. * [NOD-289] Implemented handling removedChainHashes. * [NOD-289] Implemented handling addedChainBlocks. * [NOD-289] Fixed incorrect coinbase check. * [NOD-289] Implemented inserting the transaction output address. * [NOD-289] Added updating block.IsChainBlock. * [NOD-289] Split insertBlock into many small functions. * [NOD-289] Split updateSelectedParentChain into smaller functions. * [NOD-289] Fixed pointer errors. * [NOD-289] Fixed a bad exists check. * [NOD-289] Fixed a couple of small bugs. * [NOD-289] Fixed a TxID/Hash mixup. * [NOD-289] Added block/tx mass to getBlockVerboseResponse. * [NOD-289] Renamed blockLoop.go to sync.go. Added comments. * [NOD-289] Deleted apiserver README. * [NOD-289] Fixed golint errors. * [NOD-289] Renamed findMostRecentBlockHash to findHashOfBluestBlock. * [NOD-289] Fixed style in syncBlocks and fixed a comment. * [NOD-289] Copied NewErrorFromDBErrors over from NOD-324. * [NOD-289] Created a couple of utils to make error handling with gorm slightly less painful. * [NOD-289] Added error handling for database calls. * [NOD-289] Fixed some more style/comments. * [NOD-289] Fixed comments. * [NOD-289] Renamed TransactionInput.TransactionOutput to TransactionInput.PreviousTransactionOutput. * [NOD-289] Added a commends about pagination in getBlocks and getChainFromBlock. * [NOD-289] Removed the coinbase field from Vin. * [NOD-289] Deferred handling chainChangedMsgs until we have the appropriate data. * [NOD-289] Optimized queries in updateRemovedChainHashes and updateAddedChainBlocks. * [NOD-289] Optimized queries in insertBlockParents. * [NOD-289] Optimized queries in insertTransactionInput. * [NOD-289] Split Where calls to separate lines. * [NOD-289] Fixed merge errors. * [NOD-289] Exited early from insertBlockParents if we're the genesis block. * [NOD-289] Improved nextChainChangedChan mechanism. * [NOD-289] Fixed the above sync mechanism a bit. * [NOD-289] Renamed IsDBRecordNotFoundError to HasDBRecordNotFoundError and IsDBError to HasDBError. * [NOD-289] Replaced old error handling for db errors with the lovely new stuff. * [NOD-289] Exited early if we already inserted a block. This saves us checking if a record already exists for some record types. * [NOD-289] Decoupled syncBlocks from syncSelectedParentChain. * [NOD-289] Made a comment more explicit. * [NOD-289] Extracted net resolution to a separate function. * [NOD-289] Extracted syncing to a separate function. * [NOD-289] Fixed a comment. * [NOD-289] Fixed merge erros. * [NOD-289] Fixed a couple of bugs. * [NOD-289] Fixed another bug. * [NOD-289] Extracted ChainChangedMsg conversion to a separate function. * [NOD-289] Optimized queries in canHandleChainChangedMsg. * [NOD-289] Moved the sync function closer to its call site. * [NOD-289] Renamed HasDBRecordNotFoundError to IsDBRecordNotFoundError. * [NOD-289] Used count instead of first. * [NOD-289] Renamed address to hexAddress.
127 lines
3.8 KiB
Go
127 lines
3.8 KiB
Go
package config
|
|
|
|
import (
|
|
"errors"
|
|
"github.com/daglabs/btcd/apiserver/logger"
|
|
"github.com/daglabs/btcd/dagconfig"
|
|
"github.com/daglabs/btcd/util"
|
|
"github.com/jessevdk/go-flags"
|
|
"path/filepath"
|
|
)
|
|
|
|
const (
|
|
defaultLogFilename = "apiserver.log"
|
|
defaultErrLogFilename = "apiserver_err.log"
|
|
)
|
|
|
|
var (
|
|
// activeNetParams are the currently active net params
|
|
activeNetParams dagconfig.Params
|
|
)
|
|
|
|
var (
|
|
// Default configuration options
|
|
defaultLogDir = util.AppDataDir("apiserver", false)
|
|
defaultDBAddress = "localhost:3306"
|
|
defaultHTTPListen = "0.0.0.0:8080"
|
|
)
|
|
|
|
// Config defines the configuration options for the API server.
|
|
type Config struct {
|
|
LogDir string `long:"logdir" description:"Directory to log output."`
|
|
RPCUser string `short:"u" long:"rpcuser" description:"RPC username"`
|
|
RPCPassword string `short:"P" long:"rpcpass" default-mask:"-" description:"RPC password"`
|
|
RPCServer string `short:"s" long:"rpcserver" description:"RPC server to connect to"`
|
|
RPCCert string `short:"c" long:"rpccert" description:"RPC server certificate chain for validation"`
|
|
DisableTLS bool `long:"notls" description:"Disable TLS"`
|
|
DBAddress string `long:"dbaddress" description:"Database address"`
|
|
DBUser string `long:"dbuser" description:"Database user" required:"true"`
|
|
DBPassword string `long:"dbpass" description:"Database password" required:"true"`
|
|
DBName string `long:"dbname" description:"Database name" required:"true"`
|
|
HTTPListen string `long:"listen" description:"HTTP address to listen on (default: 0.0.0.0:8080)"`
|
|
Migrate bool `long:"migrate" description:"Migrate the database to the latest version. The server will not start when using this flag."`
|
|
TestNet bool `long:"testnet" description:"Connect to testnet"`
|
|
SimNet bool `long:"simnet" description:"Connect to the simulation test network"`
|
|
DevNet bool `long:"devnet" description:"Connect to the development test network"`
|
|
}
|
|
|
|
// Parse parses the CLI arguments and returns a config struct.
|
|
func Parse() (*Config, error) {
|
|
cfg := &Config{
|
|
LogDir: defaultLogDir,
|
|
DBAddress: defaultDBAddress,
|
|
HTTPListen: defaultHTTPListen,
|
|
}
|
|
parser := flags.NewParser(cfg, flags.PrintErrors|flags.HelpFlag)
|
|
_, err := parser.Parse()
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
if !cfg.Migrate {
|
|
if cfg.RPCUser == "" {
|
|
return nil, errors.New("--rpcuser is required if --migrate flag is not used")
|
|
}
|
|
if cfg.RPCPassword == "" {
|
|
return nil, errors.New("--rpcpass is required if --migrate flag is not used")
|
|
}
|
|
if cfg.RPCServer == "" {
|
|
return nil, errors.New("--rpcserver is required if --migrate flag is not used")
|
|
}
|
|
}
|
|
|
|
if cfg.RPCCert == "" && !cfg.DisableTLS {
|
|
return nil, errors.New("--notls has to be disabled if --cert is used")
|
|
}
|
|
|
|
if cfg.RPCCert != "" && cfg.DisableTLS {
|
|
return nil, errors.New("--cert should be omitted if --notls is used")
|
|
}
|
|
|
|
err = resolveNetwork(cfg)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
logFile := filepath.Join(cfg.LogDir, defaultLogFilename)
|
|
errLogFile := filepath.Join(cfg.LogDir, defaultErrLogFilename)
|
|
logger.InitLog(logFile, errLogFile)
|
|
|
|
return cfg, nil
|
|
}
|
|
|
|
func resolveNetwork(cfg *Config) error {
|
|
// Multiple networks can't be selected simultaneously.
|
|
numNets := 0
|
|
if cfg.TestNet {
|
|
numNets++
|
|
}
|
|
if cfg.SimNet {
|
|
numNets++
|
|
}
|
|
if cfg.DevNet {
|
|
numNets++
|
|
}
|
|
if numNets > 1 {
|
|
return errors.New("multiple net params (testnet, simnet, devnet, etc.) can't be used " +
|
|
"together -- choose one of them")
|
|
}
|
|
|
|
activeNetParams = dagconfig.MainNetParams
|
|
switch {
|
|
case cfg.TestNet:
|
|
activeNetParams = dagconfig.TestNet3Params
|
|
case cfg.SimNet:
|
|
activeNetParams = dagconfig.SimNetParams
|
|
case cfg.DevNet:
|
|
activeNetParams = dagconfig.DevNetParams
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
// ActiveNetParams returns the currently active net params
|
|
func ActiveNetParams() *dagconfig.Params {
|
|
return &activeNetParams
|
|
}
|