/
BitcoinSRunner.scala
116 lines (94 loc) · 3.71 KB
/
BitcoinSRunner.scala
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
package org.bitcoins.server.routes
import akka.actor.ActorSystem
import com.typesafe.config.{Config, ConfigFactory}
import grizzled.slf4j.Logging
import org.bitcoins.core.util.{EnvUtil, StartStopAsync}
import org.bitcoins.db.AppConfig
import org.bitcoins.db.AppConfig.safePathToString
import org.bitcoins.server.util.DatadirUtil
import java.nio.file.{Path, Paths}
import scala.concurrent.{ExecutionContext, Future}
import scala.util.Properties
trait BitcoinSRunner extends StartStopAsync[Unit] with Logging {
protected def args: Array[String]
def actorSystemName: String
implicit lazy val system: ActorSystem = {
val system = ActorSystem(actorSystemName, baseConfig)
system.log.info("Akka started")
system
}
implicit lazy val ec: ExecutionContext = system.dispatcher
lazy val argsWithIndex: Vector[(String, Int)] = args.toVector.zipWithIndex
/** The ip address we are binding the server to */
lazy val rpcBindOpt: Option[String] = {
val rpcbindOpt = argsWithIndex.find(_._1.toLowerCase == "--rpcbind")
rpcbindOpt.map { case (_, idx) =>
args(idx + 1)
}
}
lazy val rpcPortOpt: Option[Int] = {
val portOpt = argsWithIndex.find(_._1.toLowerCase == "--rpcport")
portOpt.map { case (_, idx) =>
args(idx + 1).toInt
}
}
lazy val forceChainWorkRecalc: Boolean =
args.exists(_.toLowerCase == "--force-recalc-chainwork")
private lazy val dataDirIndexOpt: Option[(String, Int)] = {
argsWithIndex.find(_._1.toLowerCase == "--datadir")
}
/** Sets the default data dir, overridden by the --datadir option */
private lazy val datadirPath: Path = dataDirIndexOpt match {
case None => AppConfig.DEFAULT_BITCOIN_S_DATADIR
case Some((_, dataDirIndex)) =>
val str = args(dataDirIndex + 1)
val usableStr = str.replace("~", Properties.userHome)
Paths.get(usableStr)
}
private lazy val configIndexOpt: Option[Int] = {
argsWithIndex.find(_._1.toLowerCase == "--conf").map(_._2)
}
lazy val datadirConfig: Config =
ConfigFactory.parseString(
s"bitcoin-s.datadir = ${safePathToString(datadirPath)}")
lazy val baseConfig: Config = configIndexOpt match {
case None =>
AppConfig
.getBaseConfig(datadirPath)
.withFallback(datadirConfig)
.resolve()
case Some(configIndex) =>
val str = args(configIndex + 1)
val usableStr = str.replace("~", Properties.userHome)
val path = Paths.get(usableStr)
ConfigFactory
.parseFile(path.toFile)
.withFallback(datadirConfig)
.resolve()
}
/** Base directory for all bitcoin-s data. This is the resulting datadir from
* the --datadir option and all configuration files.
*/
lazy val datadir: Path =
Paths.get(baseConfig.getString("bitcoin-s.datadir"))
// start everything!
final def run(customFinalDirOpt: Option[String] = None): Unit = {
/** Directory specific for current network or custom dir */
val usedDir: Path =
DatadirUtil.getFinalDatadir(datadir, baseConfig, customFinalDirOpt)
if (Properties.isWin) {
System.setProperty("HOME", datadir.getParent.toAbsolutePath.toString)
}
//We need to set the system property before any logger instances
//are in instantiated. If we don't do this, we will not log to
//the correct location
//see: https://github.com/bitcoin-s/bitcoin-s/issues/2496
System.setProperty("bitcoins.log.location", usedDir.toAbsolutePath.toString)
logger.info(s"version=${EnvUtil.getVersion}")
logger.info(s"using directory ${usedDir.toAbsolutePath.toString}")
val runner: Future[Unit] = start()
runner.failed.foreach { err =>
logger.error(s"Failed to startup server!", err)
}(scala.concurrent.ExecutionContext.Implicits.global)
}
}