You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
32 lines
1.2 KiB
32 lines
1.2 KiB
package com.xsn.explorer.helpers
|
|
|
|
import com.alexitc.playsonify.core.FutureApplicationResult
|
|
import com.xsn.explorer.errors.{BlockNotFoundError, TransactionNotFoundError}
|
|
import com.xsn.explorer.models.{Blockhash, TransactionId}
|
|
import com.xsn.explorer.models.rpc.{Block, Transaction}
|
|
import org.scalactic.{Good, One, Or}
|
|
|
|
import scala.concurrent.Future
|
|
|
|
class FileBasedXSNService extends DummyXSNService {
|
|
|
|
private lazy val blockMap = BlockLoader.all().map { block => block.hash -> block }.toMap
|
|
private lazy val transactionMap = TransactionLoader.all().map { tx => tx.id -> tx }.toMap
|
|
|
|
override def getBlock(blockhash: Blockhash): FutureApplicationResult[Block] = {
|
|
val maybe = blockMap.get(blockhash)
|
|
val result = Or.from(maybe, One(BlockNotFoundError))
|
|
Future.successful(result)
|
|
}
|
|
|
|
override def getLatestBlock(): FutureApplicationResult[Block] = {
|
|
val block = blockMap.values.maxBy(_.height.int)
|
|
Future.successful(Good(block))
|
|
}
|
|
|
|
override def getTransaction(txid: TransactionId): FutureApplicationResult[Transaction] = {
|
|
val maybe = transactionMap.get(txid)
|
|
val result = Or.from(maybe, One(TransactionNotFoundError))
|
|
Future.successful(result)
|
|
}
|
|
}
|
|
|