CLI command for filter and filter header counts (#1063)

This commit is contained in:
Ben Carman 2020-01-23 14:45:33 -06:00 committed by Chris Stewart
parent f8d77ba261
commit 0c2d324258
4 changed files with 56 additions and 2 deletions

View file

@ -36,6 +36,8 @@ object CliCommand {
// Chain // Chain
case object GetBestBlockHash extends CliCommand case object GetBestBlockHash extends CliCommand
case object GetBlockCount extends CliCommand case object GetBlockCount extends CliCommand
case object GetFilterCount extends CliCommand
case object GetFilterHeaderCount extends CliCommand
case class Rescan( case class Rescan(
addressBatchSize: Option[Int], addressBatchSize: Option[Int],
startBlock: Option[BlockStamp], startBlock: Option[BlockStamp],
@ -63,6 +65,14 @@ object Cli extends App {
.hidden() .hidden()
.action((_, conf) => conf.copy(command = GetBlockCount)) .action((_, conf) => conf.copy(command = GetBlockCount))
.text(s"Get the block height"), .text(s"Get the block height"),
cmd("getfiltercount")
.hidden()
.action((_, conf) => conf.copy(command = GetFilterCount))
.text(s"Get the number of filters"),
cmd("getfilterheadercount")
.hidden()
.action((_, conf) => conf.copy(command = GetFilterHeaderCount))
.text(s"Get the number of filter headers"),
cmd("getbestblockhash") cmd("getbestblockhash")
.hidden() .hidden()
.action((_, conf) => conf.copy(command = GetBestBlockHash)) .action((_, conf) => conf.copy(command = GetBestBlockHash))
@ -211,6 +221,10 @@ object Cli extends App {
Seq(up.writeJs(address), up.writeJs(bitcoins))) Seq(up.writeJs(address), up.writeJs(bitcoins)))
// height // height
case GetBlockCount => RequestParam("getblockcount") case GetBlockCount => RequestParam("getblockcount")
// filter count
case GetFilterCount => RequestParam("getfiltercount")
// filter header count
case GetFilterHeaderCount => RequestParam("getfilterheadercount")
// besthash // besthash
case GetBestBlockHash => RequestParam("getbestblockhash") case GetBestBlockHash => RequestParam("getbestblockhash")
// peers // peers

View file

@ -65,6 +65,34 @@ class RoutesSpec
} }
} }
"return the filter count" in {
(mockChainApi.getFilterCount: () => Future[Int])
.expects()
.returning(Future.successful(1234567890))
val route =
chainRoutes.handleCommand(ServerCommand("getfiltercount", Arr()))
Get() ~> route ~> check {
contentType shouldEqual `application/json`
responseAs[String] shouldEqual """{"result":1234567890,"error":null}"""
}
}
"return the filter header count" in {
(mockChainApi.getFilterHeaderCount: () => Future[Int])
.expects()
.returning(Future.successful(1234567890))
val route =
chainRoutes.handleCommand(ServerCommand("getfilterheadercount", Arr()))
Get() ~> route ~> check {
contentType shouldEqual `application/json`
responseAs[String] shouldEqual """{"result":1234567890,"error":null}"""
}
}
"return the best block hash" in { "return the best block hash" in {
(mockChainApi.getBestBlockHash: () => Future[DoubleSha256DigestBE]) (mockChainApi.getBestBlockHash: () => Future[DoubleSha256DigestBE])
.expects() .expects()

View file

@ -20,6 +20,18 @@ case class ChainRoutes(chain: ChainApi)(implicit system: ActorSystem)
Server.httpSuccess(count) Server.httpSuccess(count)
} }
} }
case ServerCommand("getfiltercount", _) =>
complete {
chain.getFilterCount().map { count =>
Server.httpSuccess(count)
}
}
case ServerCommand("getfilterheadercount", _) =>
complete {
chain.getFilterHeaderCount().map { count =>
Server.httpSuccess(count)
}
}
case ServerCommand("getbestblockhash", _) => case ServerCommand("getbestblockhash", _) =>
complete { complete {
chain.getBestBlockHash.map { hash => chain.getBestBlockHash.map { hash =>

View file

@ -112,7 +112,7 @@ trait ChainApi extends ChainQueryApi {
blockHash: DoubleSha256DigestBE): Future[ChainApi] blockHash: DoubleSha256DigestBE): Future[ChainApi]
/** Gets the number of compact filter headers in the database */ /** Gets the number of compact filter headers in the database */
def getFilterHeaderCount: Future[Int] def getFilterHeaderCount(): Future[Int]
/** /**
* Looks up a compact filter header by its height. * Looks up a compact filter header by its height.
@ -132,7 +132,7 @@ trait ChainApi extends ChainQueryApi {
def getFilter(hash: DoubleSha256DigestBE): Future[Option[CompactFilterDb]] def getFilter(hash: DoubleSha256DigestBE): Future[Option[CompactFilterDb]]
/** Gets the number of compact filters in the database */ /** Gets the number of compact filters in the database */
def getFilterCount: Future[Int] def getFilterCount(): Future[Int]
/** /**
* Looks up a compact filter by its height. * Looks up a compact filter by its height.