Skip to content

Commit

Permalink
server: Load the genesis block from the config file
Browse files Browse the repository at this point in the history
  • Loading branch information
adinael authored and AlexITC committed Nov 19, 2018
1 parent 117a668 commit ded2c47
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 22 deletions.
28 changes: 13 additions & 15 deletions server/app/com/xsn/explorer/services/XSNService.scala
Original file line number Diff line number Diff line change
@@ -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}
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -450,4 +445,7 @@ class XSNServiceRPCImpl @Inject() (
.map { e => Bad(e).accumulating }
}
}

override val genesisBlockhash: Blockhash = explorerConfig.genesisBlock

}
13 changes: 11 additions & 2 deletions server/test/com/xsn/explorer/helpers/BlockLoader.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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 = {
Expand All @@ -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 change: 1 addition & 0 deletions server/test/com/xsn/explorer/helpers/DummyXSNService.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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] = ???
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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))
}

Expand Down
Original file line number Diff line number Diff line change
@@ -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._
Expand All @@ -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"""
Expand Down

0 comments on commit ded2c47

Please sign in to comment.