Skip to content

Commit

Permalink
Add rpcport configuration options in both bitcoin-s.conf and as a com…
Browse files Browse the repository at this point in the history
…mand line parameter (#1387)

Implement ability to specify rpcport with bitcoin-s-cli
  • Loading branch information
Christewart committed May 5, 2020
1 parent be945fe commit 29e439d
Show file tree
Hide file tree
Showing 9 changed files with 79 additions and 22 deletions.
22 changes: 14 additions & 8 deletions app/cli/src/main/scala/org/bitcoins/cli/ConsoleCli.scala
Expand Up @@ -30,6 +30,9 @@ object ConsoleCli {
opt[Unit]("debug")
.action((_, conf) => conf.copy(debug = true))
.text("Print debugging information"),
opt[Int]("rpcport")
.action((port,conf) => conf.copy(rpcPort = port))
.text(s"The port to send our rpc request to on the server"),
help('h', "help").text("Display this help message and exit"),
note(sys.props("line.separator") + "Commands:"),
note(sys.props("line.separator") + "===Blockchain ==="),
Expand Down Expand Up @@ -310,7 +313,7 @@ object ConsoleCli {
}))
),
checkConfig {
case Config(NoCommand, _, _) =>
case Config(NoCommand, _, _, _) =>
failure("You need to provide a command!")
case _ => success
}
Expand All @@ -323,15 +326,15 @@ object ConsoleCli {
case Some(conf) => conf
}

exec(config.command, config.debug)
exec(config.command, config)
}

def exec(command: CliCommand, debugEnabled: Boolean = false): Try[String] = {
def exec(command: CliCommand, config: Config): Try[String] = {
import System.err.{println => printerr}

/** Prints the given message to stderr if debug is set */
def debug(message: Any): Unit = {
if (debugEnabled) {
if (config.debug) {
printerr(s"DEBUG: $message")
}
}
Expand Down Expand Up @@ -413,7 +416,7 @@ object ConsoleCli {
HttpURLConnectionBackend()
val request =
sttp
.post(uri"http://$host:$port/")
.post(uri"http://$host:${config.rpcPort}/")
.contentType("application/json")
.body({
val uuid = java.util.UUID.randomUUID.toString
Expand Down Expand Up @@ -468,8 +471,6 @@ object ConsoleCli {
}.flatten
}

// TODO make this dynamic
def port = 9999
def host = "localhost"

case class RequestParam(
Expand All @@ -485,9 +486,14 @@ object ConsoleCli {
case class Config(
command: CliCommand = CliCommand.NoCommand,
network: Option[NetworkParameters] = None,
debug: Boolean = false
debug: Boolean = false,
rpcPort: Int = 9999
)

object Config {
val empty = Config()
}

sealed abstract class CliCommand

object CliCommand {
Expand Down
9 changes: 5 additions & 4 deletions app/gui/src/main/scala/org/bitcoins/gui/WalletGUIModel.scala
@@ -1,7 +1,7 @@
package org.bitcoins.gui

import org.bitcoins.cli.CliCommand.{GetBalance, GetNewAddress, SendToAddress}
import org.bitcoins.cli.ConsoleCli
import org.bitcoins.cli.{Config, ConsoleCli}
import org.bitcoins.core.currency.Bitcoins
import org.bitcoins.core.protocol.BitcoinAddress
import org.bitcoins.gui.dialog.{GetNewAddressDialog, SendDialog}
Expand All @@ -28,7 +28,7 @@ class WalletGUIModel() {
taskRunner.run(
caption = "Get New Address",
op = {
ConsoleCli.exec(GetNewAddress) match {
ConsoleCli.exec(GetNewAddress,Config.empty) match {
case Success(commandReturn) => address.value = commandReturn
case Failure(err) => throw err
}
Expand All @@ -49,7 +49,8 @@ class WalletGUIModel() {
ConsoleCli.exec(
SendToAddress(BitcoinAddress(address).get,
Bitcoins(BigDecimal(amount)),
satoshisPerVirtualByte = None)) match {
satoshisPerVirtualByte = None),
Config.empty) match {
case Success(txid) =>
GlobalData.log.value =
s"Sent $amount to $address in tx: $txid\n\n${GlobalData.log()}"
Expand All @@ -64,7 +65,7 @@ class WalletGUIModel() {
}

private def updateBalance(): Unit = {
ConsoleCli.exec(GetBalance(isSats = false)) match {
ConsoleCli.exec(GetBalance(isSats = false), Config.empty) match {
case Success(commandReturn) =>
GlobalData.currentBalance.value = commandReturn.split(' ').head.toDouble
case Failure(err) =>
Expand Down
5 changes: 5 additions & 0 deletions app/server/src/main/resources/reference.conf
Expand Up @@ -78,6 +78,11 @@ bitcoin-s {
# before we timeout
addressQueueTimeout = 5 seconds
}

server {
# The port we bind our rpc server on
rpcport = 9999
}
}

akka {
Expand Down
Expand Up @@ -44,6 +44,18 @@ case class BitcoinSAppConfig(
// be equal
nodeConf.config
}

def serverConf: Config = {
config.getConfig("server")
}

def rpcPortOpt: Option[Int] = {
if(serverConf.hasPath("rpcport")) {
Some(serverConf.getInt("rpcport"))
} else {
None
}
}
}

/**
Expand Down
27 changes: 21 additions & 6 deletions app/server/src/main/scala/org/bitcoins/server/Main.scala
Expand Up @@ -26,11 +26,13 @@ import scala.concurrent.{Await, ExecutionContext, Future}
object Main extends App {
implicit val system = ActorSystem("bitcoin-s")
implicit val ec: ExecutionContext = system.dispatcher
val argsWithIndex = args.zipWithIndex

implicit val conf = {
val dataDirIndexOpt = args.zipWithIndex
.find(_._1.toLowerCase == "--datadir")

val dataDirIndexOpt = {
argsWithIndex.find(_._1.toLowerCase == "--datadir")
}
val datadirPath = dataDirIndexOpt match {
case None => AppConfig.DEFAULT_BITCOIN_S_DATADIR
case Some((_, dataDirIndex)) =>
Expand All @@ -39,7 +41,12 @@ object Main extends App {
}
BitcoinSAppConfig(datadirPath)
}

val rpcPortOpt: Option[Int] = {
val portOpt = argsWithIndex.find(_._1.toLowerCase == "--rpcport")
portOpt.map {
case (_,idx) => args(idx+1).toInt
}
}
private val logger = HttpLoggerImpl(conf.nodeConf).getLogger

implicit val walletConf: WalletAppConfig = conf.walletConf
Expand Down Expand Up @@ -75,9 +82,17 @@ object Main extends App {
val nodeRoutes = NodeRoutes(node)
val chainRoutes = ChainRoutes(chainApi)
val coreRoutes = CoreRoutes(Core)
val server =
Server(nodeConf, Seq(walletRoutes, nodeRoutes, chainRoutes, coreRoutes))

val server = rpcPortOpt match {
case Some(rpcport) =>
Server(nodeConf, Seq(walletRoutes, nodeRoutes, chainRoutes, coreRoutes), rpcport = rpcport)
case None =>
conf.rpcPortOpt match {
case Some(rpcport) =>
Server(nodeConf, Seq(walletRoutes, nodeRoutes, chainRoutes, coreRoutes), rpcport)
case None =>
Server(nodeConf, Seq(walletRoutes, nodeRoutes, chainRoutes, coreRoutes))
}
}
server.start()
}
} yield {
Expand Down
9 changes: 5 additions & 4 deletions app/server/src/main/scala/org/bitcoins/server/Server.scala
Expand Up @@ -7,13 +7,14 @@ import akka.stream.ActorMaterializer
import akka.http.scaladsl.model._
import akka.http.scaladsl.server._
import akka.http.scaladsl.server.Directives._

import de.heikoseeberger.akkahttpupickle.UpickleSupport._
import akka.http.scaladsl.server.directives.DebuggingDirectives
import akka.event.Logging
import org.bitcoins.db.AppConfig

case class Server(conf: AppConfig, handlers: Seq[ServerRoute])(
import scala.concurrent.Future

case class Server(conf: AppConfig, handlers: Seq[ServerRoute], rpcport: Int = 9999)(
implicit system: ActorSystem)
extends HttpLogger {
implicit private val config: AppConfig = conf
Expand Down Expand Up @@ -76,9 +77,9 @@ case class Server(conf: AppConfig, handlers: Seq[ServerRoute])(
}
}

def start() = {
def start(): Future[Http.ServerBinding] = {
val httpFut =
Http().bindAndHandle(route, "localhost", 9999)
Http().bindAndHandle(route, "localhost", rpcport)
httpFut.foreach { http =>
logger.info(s"Started Bitcoin-S HTTP server at ${http.localAddress}")
}
Expand Down
7 changes: 7 additions & 0 deletions docs/applications/server.md
Expand Up @@ -36,6 +36,13 @@ If you would like to pass in a custom datadir for your server, you can do
```bash
./app/server/target/universal/stage/bin/bitcoin-s-server --datadir /path/to/datadir/
```

You can also pass in a custom `rpcport` to bind to

```bash
./app/server/target/universal/stage/bin/bitcoin-s-server --rpcport 12345
```

For more information on configuring the server please see our [configuration](../config/configuration.md) document

For more information on how to use our built in `cli` to interact with the server please see [cli.md](cli.md)
5 changes: 5 additions & 0 deletions docs/config/configuration.md
Expand Up @@ -153,6 +153,11 @@ bitcoin-s {
addressQueueTimeout = 5 seconds
}
server {
# The port we bind our rpc server on
rpcport = 9999
}
}
Expand Down
5 changes: 5 additions & 0 deletions testkit/src/main/resources/reference.conf
Expand Up @@ -80,6 +80,11 @@ bitcoin-s {
# before we timeout
addressQueueTimeout = 5 seconds
}

server {
# The port we bind our rpc server on
rpcport = 9999
}
}


Expand Down

0 comments on commit 29e439d

Please sign in to comment.