Browse Source

server: Load the genesis block from the config file

prometheus-integration
Adinael Perez Ruelas 6 years ago
committed by Alexis Hernandez
parent
commit
ded2c47841
  1. 24
      server/app/com/xsn/explorer/services/XSNService.scala
  2. 13
      server/test/com/xsn/explorer/helpers/BlockLoader.scala
  3. 1
      server/test/com/xsn/explorer/helpers/DummyXSNService.scala
  4. 4
      server/test/com/xsn/explorer/services/LedgerSynchronizerServiceSpec.scala
  5. 10
      server/test/com/xsn/explorer/services/XSNServiceRPCImplSpec.scala

24
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)
.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
}

13
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)
}
}

1
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] = ???

4
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))
}

10
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"""

Loading…
Cancel
Save