Add --chain=X argument, and use it to determine chain when DB is empty.

This commit is contained in:
Jonathan Moody 2022-09-02 13:45:49 -05:00
parent 321bcf6420
commit f5b8f2ce0d
2 changed files with 31 additions and 4 deletions

View file

@ -7,6 +7,7 @@ import (
"github.com/akamensky/argparse"
pb "github.com/lbryio/herald.go/protobuf/go"
"github.com/lbryio/lbcd/chaincfg"
)
const (
@ -21,6 +22,7 @@ type Args struct {
Host string
Port string
DBPath string
Chain *string
EsHost string
EsPort string
PrometheusPort string
@ -112,6 +114,8 @@ func ParseArgs(searchRequest *pb.SearchRequest) *Args {
host := parser.String("", "rpchost", &argparse.Options{Required: false, Help: "RPC host", Default: DefaultHost})
port := parser.String("", "rpcport", &argparse.Options{Required: false, Help: "RPC port", Default: DefaultPort})
dbPath := parser.String("", "db-path", &argparse.Options{Required: false, Help: "RocksDB path", Default: DefaultDBPath})
chain := parser.Selector("", "chain", []string{chaincfg.MainNetParams.Name, chaincfg.TestNet3Params.Name, chaincfg.RegressionNetParams.Name, "testnet"},
&argparse.Options{Required: false, Help: "Which chain to use, default is 'mainnet'. Values 'regtest' and 'testnet' are for testing", Default: chaincfg.MainNetParams.Name})
esHost := parser.String("", "eshost", &argparse.Options{Required: false, Help: "elasticsearch host", Default: DefaultEsHost})
esPort := parser.String("", "esport", &argparse.Options{Required: false, Help: "elasticsearch port", Default: DefaultEsPort})
prometheusPort := parser.String("", "prometheus-port", &argparse.Options{Required: false, Help: "prometheus port", Default: DefaultPrometheusPort})
@ -159,6 +163,7 @@ func ParseArgs(searchRequest *pb.SearchRequest) *Args {
Host: *host,
Port: *port,
DBPath: *dbPath,
Chain: chain,
EsHost: *esHost,
EsPort: *esPort,
PrometheusPort: *prometheusPort,

View file

@ -274,18 +274,40 @@ func MakeHubServer(ctx context.Context, args *Args) *Server {
}
}
chain := chaincfg.MainNetParams
dbChain := (*chaincfg.Params)(nil)
if myDB != nil && myDB.LastState != nil && myDB.LastState.Genesis != nil {
// The chain params can be inferred from DBStateValue.
switch *myDB.LastState.Genesis {
case *chaincfg.MainNetParams.GenesisHash:
chain = chaincfg.MainNetParams
dbChain = &chaincfg.MainNetParams
case *chaincfg.TestNet3Params.GenesisHash:
chain = chaincfg.TestNet3Params
dbChain = &chaincfg.TestNet3Params
case *chaincfg.RegressionNetParams.GenesisHash:
chain = chaincfg.RegressionNetParams
dbChain = &chaincfg.RegressionNetParams
}
}
cliChain := (*chaincfg.Params)(nil)
if args.Chain != nil {
switch *args.Chain {
case chaincfg.MainNetParams.Name:
cliChain = &chaincfg.MainNetParams
case chaincfg.TestNet3Params.Name, "testnet":
cliChain = &chaincfg.TestNet3Params
case chaincfg.RegressionNetParams.Name:
cliChain = &chaincfg.RegressionNetParams
}
}
chain := chaincfg.MainNetParams
if dbChain != nil && cliChain != nil {
if dbChain != cliChain {
logrus.Warnf("network: %v (from db) conflicts with %v (from cli)", dbChain.Name, cliChain.Name)
}
chain = *dbChain
} else if dbChain != nil {
chain = *dbChain
} else if cliChain != nil {
chain = *cliChain
}
logrus.Infof("network: %v", chain.Name)
s := &Server{