From ded2c478410e3b96c2fe8bd234fa4b066bd2ca20 Mon Sep 17 00:00:00 2001 From: Adinael Perez Ruelas Date: Sun, 18 Nov 2018 20:44:19 -0700 Subject: [PATCH] server: Load the genesis block from the config file --- .../xsn/explorer/services/XSNService.scala | 28 +++++++++---------- .../xsn/explorer/helpers/BlockLoader.scala | 13 +++++++-- .../explorer/helpers/DummyXSNService.scala | 1 + .../LedgerSynchronizerServiceSpec.scala | 4 +-- .../services/XSNServiceRPCImplSpec.scala | 10 +++++-- 5 files changed, 34 insertions(+), 22 deletions(-) diff --git a/server/app/com/xsn/explorer/services/XSNService.scala b/server/app/com/xsn/explorer/services/XSNService.scala index 068e95a..f832a0f 100644 --- a/server/app/com/xsn/explorer/services/XSNService.scala +++ b/server/app/com/xsn/explorer/services/XSNService.scala @@ -1,14 +1,13 @@ package com.xsn.explorer.services -import javax.inject.Inject - import com.alexitc.playsonify.core.FutureOr.Implicits.{FutureOps, OrOps} import com.alexitc.playsonify.core.{ApplicationResult, FutureApplicationResult} import com.alexitc.playsonify.models.ApplicationError -import com.xsn.explorer.config.RPCConfig +import com.xsn.explorer.config.{ExplorerConfig, RPCConfig} import com.xsn.explorer.errors._ import com.xsn.explorer.executors.ExternalServiceExecutionContext import com.xsn.explorer.models._ +import javax.inject.Inject import org.scalactic.{Bad, Good} import org.slf4j.LoggerFactory import play.api.libs.json.{JsNull, JsValue, Reads} @@ -45,29 +44,25 @@ trait XSNService { def getUnspentOutputs(address: Address): FutureApplicationResult[JsValue] 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) + .filter(_.hash == genesisBlockhash) + .map(_.copy(transactions = List.empty)) + .getOrElse(block) } + + def genesisBlockhash: Blockhash + } class XSNServiceRPCImpl @Inject() ( ws: WSClient, - rpcConfig: RPCConfig)( + rpcConfig: RPCConfig, + explorerConfig: ExplorerConfig)( implicit ec: ExternalServiceExecutionContext) extends XSNService { - import XSNService._ - private val logger = LoggerFactory.getLogger(this.getClass) private val server = ws.url(rpcConfig.host.string) @@ -450,4 +445,7 @@ class XSNServiceRPCImpl @Inject() ( .map { e => Bad(e).accumulating } } } + + override val genesisBlockhash: Blockhash = explorerConfig.genesisBlock + } diff --git a/server/test/com/xsn/explorer/helpers/BlockLoader.scala b/server/test/com/xsn/explorer/helpers/BlockLoader.scala index 9186e7f..9ff63c3 100644 --- a/server/test/com/xsn/explorer/helpers/BlockLoader.scala +++ b/server/test/com/xsn/explorer/helpers/BlockLoader.scala @@ -3,7 +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 com.xsn.explorer.models.{Blockhash, rpc} import play.api.libs.json.{JsValue, Json} object BlockLoader { @@ -12,7 +12,7 @@ object BlockLoader { def get(blockhash: String): Block = { val block = json(blockhash).as[Block] - XSNService.cleanGenesisBlock(block) + cleanGenesisBlock(block) } def json(blockhash: String): JsValue = { @@ -33,4 +33,13 @@ object BlockLoader { .map(_.getName) .map(get) } + + def cleanGenesisBlock(block: rpc.Block): rpc.Block = { + val genesisBlockhash: Blockhash = Blockhash.from("00000c822abdbb23e28f79a49d29b41429737c6c7e15df40d1b1f1b35907ae34").get + + Option(block) + .filter(_.hash == genesisBlockhash) + .map(_.copy(transactions = List.empty)) + .getOrElse(block) + } } diff --git a/server/test/com/xsn/explorer/helpers/DummyXSNService.scala b/server/test/com/xsn/explorer/helpers/DummyXSNService.scala index f1b25ed..06ab5ae 100644 --- a/server/test/com/xsn/explorer/helpers/DummyXSNService.scala +++ b/server/test/com/xsn/explorer/helpers/DummyXSNService.scala @@ -8,6 +8,7 @@ import play.api.libs.json.JsValue class DummyXSNService extends XSNService { + override def genesisBlockhash: Blockhash = Blockhash.from("00000c822abdbb23e28f79a49d29b41429737c6c7e15df40d1b1f1b35907ae34").get override def getTransaction(txid: TransactionId): FutureApplicationResult[rpc.Transaction] = ??? override def getRawTransaction(txid: TransactionId): FutureApplicationResult[JsValue] = ??? override def getAddressBalance(address: Address): FutureApplicationResult[rpc.AddressBalance] = ??? diff --git a/server/test/com/xsn/explorer/services/LedgerSynchronizerServiceSpec.scala b/server/test/com/xsn/explorer/services/LedgerSynchronizerServiceSpec.scala index 9a3ad5b..feaa755 100644 --- a/server/test/com/xsn/explorer/services/LedgerSynchronizerServiceSpec.scala +++ b/server/test/com/xsn/explorer/services/LedgerSynchronizerServiceSpec.scala @@ -206,14 +206,14 @@ class LedgerSynchronizerServiceSpec extends PostgresDataHandlerSpec with BeforeA override def getBlock(blockhash: Blockhash): FutureApplicationResult[Block] = { blocks .find(_.hash == blockhash) - .map { block => Future.successful(Good(XSNService.cleanGenesisBlock(block))) } + .map { block => Future.successful(Good(cleanGenesisBlock(block))) } .getOrElse { Future.successful(Bad(BlockNotFoundError).accumulating) } } override def getLatestBlock(): FutureApplicationResult[Block] = { - val block = XSNService.cleanGenesisBlock(blocks.maxBy(_.height.int)) + val block = cleanGenesisBlock(blocks.maxBy(_.height.int)) Future.successful(Good(block)) } diff --git a/server/test/com/xsn/explorer/services/XSNServiceRPCImplSpec.scala b/server/test/com/xsn/explorer/services/XSNServiceRPCImplSpec.scala index a32e057..82b1019 100644 --- a/server/test/com/xsn/explorer/services/XSNServiceRPCImplSpec.scala +++ b/server/test/com/xsn/explorer/services/XSNServiceRPCImplSpec.scala @@ -1,6 +1,6 @@ package com.xsn.explorer.services -import com.xsn.explorer.config.RPCConfig +import com.xsn.explorer.config.{ExplorerConfig, RPCConfig} import com.xsn.explorer.errors._ import com.xsn.explorer.helpers.{BlockLoader, DataHelper, Executors, TransactionLoader} import com.xsn.explorer.models._ @@ -22,19 +22,23 @@ class XSNServiceRPCImplSpec extends WordSpec with MustMatchers with ScalaFutures val ws = mock[WSClient] val ec = Executors.externalServiceEC - val config = new RPCConfig { + val rpcConfig = new RPCConfig { override def password: RPCConfig.Password = RPCConfig.Password("pass") override def host: RPCConfig.Host = RPCConfig.Host("localhost") override def username: RPCConfig.Username = RPCConfig.Username("user") } + val explorerConfig = new ExplorerConfig { + override def genesisBlock: Blockhash = Blockhash.from("00000c822abdbb23e28f79a49d29b41429737c6c7e15df40d1b1f1b35907ae34").get + } + val request = mock[WSRequest] val response = mock[WSResponse] when(ws.url(anyString)).thenReturn(request) when(request.withAuth(anyString(), anyString(), any())).thenReturn(request) when(request.withHttpHeaders(any())).thenReturn(request) - val service = new XSNServiceRPCImpl(ws, config)(ec) + val service = new XSNServiceRPCImpl(ws, rpcConfig, explorerConfig)(ec) def createRPCSuccessfulResponse(result: JsValue): String = { s"""