Browse Source

server: Add support for storing the genesis block (#39)

prometheus-integration
Alexis Hernandez 7 years ago
parent
commit
d338ff9fad
  1. 29
      server/app/com/xsn/explorer/services/XSNService.scala
  2. 4
      server/test/com/xsn/explorer/helpers/BlockLoader.scala
  3. 20
      server/test/com/xsn/explorer/services/XSNServiceRPCImplSpec.scala

29
server/app/com/xsn/explorer/services/XSNService.scala

@ -47,12 +47,27 @@ trait XSNService {
def sendRawTransaction(hex: HexString): FutureApplicationResult[Unit]
}
object XSNService {
val GenesisBlockhash: Blockhash = Blockhash.from("00000c822abdbb23e28f79a49d29b41429737c6c7e15df40d1b1f1b35907ae34").get
def cleanGenesisBlock(block: rpc.Block): rpc.Block = {
// the genesis transaction is not available, see https://github.com/X9Developers/XSN/issues/32
Option(block)
.filter(_.hash == GenesisBlockhash)
.map(_.copy(transactions = List.empty))
.getOrElse(block)
}
}
class XSNServiceRPCImpl @Inject() (
ws: WSClient,
rpcConfig: RPCConfig)(
implicit ec: ExternalServiceExecutionContext)
extends XSNService {
import XSNService._
private val logger = LoggerFactory.getLogger(this.getClass)
private val server = ws.url(rpcConfig.host.string)
@ -155,7 +170,12 @@ class XSNServiceRPCImpl @Inject() (
.map { response =>
val maybe = getResult[rpc.Block](response, errorCodeMapper)
maybe.getOrElse {
maybe
.map {
case Good(block) => Good(cleanGenesisBlock(block))
case x => x
}
.getOrElse {
logger.warn(s"Unexpected response from XSN Server, txid = ${blockhash.string}, status = ${response.status}, response = ${response.body}")
Bad(XSNUnexpectedResponseError).accumulating
@ -219,7 +239,12 @@ class XSNServiceRPCImpl @Inject() (
.getOrElse(Bad(XSNUnexpectedResponseError).accumulating)
.toFutureOr
block <- getBlock(blockhash).toFutureOr
block <- getBlock(blockhash).
map {
case Good(block) => Good(cleanGenesisBlock(block))
case x => x
}
.toFutureOr
} yield block
result.toFuture

4
server/test/com/xsn/explorer/helpers/BlockLoader.scala

@ -3,6 +3,7 @@ package com.xsn.explorer.helpers
import java.io.File
import com.xsn.explorer.models.rpc.Block
import com.xsn.explorer.services.XSNService
import play.api.libs.json.{JsValue, Json}
object BlockLoader {
@ -10,7 +11,8 @@ object BlockLoader {
private val BasePath = "blocks"
def get(blockhash: String): Block = {
json(blockhash).as[Block]
val block = json(blockhash).as[Block]
XSNService.cleanGenesisBlock(block)
}
def json(blockhash: String): JsValue = {

20
server/test/com/xsn/explorer/services/XSNServiceRPCImplSpec.scala

@ -293,6 +293,26 @@ class XSNServiceRPCImplSpec extends WordSpec with MustMatchers with ScalaFutures
}
"getBlock" should {
"return the genesis block" in {
val block = BlockLoader.json("00000c822abdbb23e28f79a49d29b41429737c6c7e15df40d1b1f1b35907ae34")
val responseBody = createRPCSuccessfulResponse(block)
val blockhash = Blockhash.from("00000c822abdbb23e28f79a49d29b41429737c6c7e15df40d1b1f1b35907ae34").get
val json = Json.parse(responseBody)
when(response.status).thenReturn(200)
when(response.json).thenReturn(json)
when(request.post[String](anyString())(any())).thenReturn(Future.successful(response))
whenReady(service.getBlock(blockhash)) { result =>
result.isGood mustEqual true
val block = result.get
block.hash mustEqual blockhash
block.transactions mustEqual List.empty
}
}
"return a block" in {
val block = BlockLoader.json("b72dd1655408e9307ef5874be20422ee71029333283e2360975bc6073bdb2b81")
val responseBody = createRPCSuccessfulResponse(block)

Loading…
Cancel
Save