Browse Source

server: allow to get block extractionMethod from the BlockService

master
Alexis Hernandez 6 years ago
parent
commit
a334864e38
  1. 25
      server/app/com/xsn/explorer/services/BlockService.scala
  2. 14
      server/app/com/xsn/explorer/services/logic/BlockLogic.scala

25
server/app/com/xsn/explorer/services/BlockService.scala

@ -101,6 +101,31 @@ class BlockService @Inject() (
result.toFuture result.toFuture
} }
def extractionMethod(block: rpc.Block): FutureApplicationResult[BlockExtractionMethod] = {
if (block.tposContract.isDefined) {
Future.successful(Good(BlockExtractionMethod.TrustlessProofOfStake))
} else if (block.transactions.isEmpty) {
Future.successful(Good(BlockExtractionMethod.ProofOfWork))
} else {
isPoS(block)
.toFutureOr
.map {
case true => BlockExtractionMethod.ProofOfStake
case false => BlockExtractionMethod.ProofOfWork
}
.toFuture
}
}
private def isPoS(block: rpc.Block): FutureApplicationResult[Boolean] = {
val result = for {
coinbaseTxid <- blockLogic.getCoinbase(block).toFutureOr
coinbase <- xsnService.getTransaction(coinbaseTxid).toFutureOr
} yield blockLogic.isPoS(block, coinbase)
result.toFuture
}
private def getBlockRewards(block: Block): FutureApplicationResult[BlockRewards] = { private def getBlockRewards(block: Block): FutureApplicationResult[BlockRewards] = {
if (block.transactions.isEmpty) { if (block.transactions.isEmpty) {
Future.successful(Bad(BlockRewardsNotFoundError).accumulating) Future.successful(Bad(BlockRewardsNotFoundError).accumulating)

14
server/app/com/xsn/explorer/services/logic/BlockLogic.scala

@ -1,7 +1,7 @@
package com.xsn.explorer.services.logic package com.xsn.explorer.services.logic
import com.alexitc.playsonify.core.ApplicationResult import com.alexitc.playsonify.core.ApplicationResult
import com.xsn.explorer.errors.{BlockNotFoundError, BlockhashFormatError} import com.xsn.explorer.errors.{BlockNotFoundError, BlockhashFormatError, TransactionNotFoundError}
import com.xsn.explorer.models._ import com.xsn.explorer.models._
import com.xsn.explorer.models.rpc.{Block, Transaction} import com.xsn.explorer.models.rpc.{Block, Transaction}
import com.xsn.explorer.models.values.{Address, Blockhash, TransactionId} import com.xsn.explorer.models.values.{Address, Blockhash, TransactionId}
@ -20,6 +20,12 @@ class BlockLogic {
Or.from(maybe, One(BlockNotFoundError)) Or.from(maybe, One(BlockNotFoundError))
} }
def getCoinbase(block: Block): ApplicationResult[TransactionId] = {
val maybe = block.transactions.headOption
Or.from(maybe, One(TransactionNotFoundError))
}
/** /**
* Get the coinstake transaction id for the given block. * Get the coinstake transaction id for the given block.
* *
@ -144,4 +150,10 @@ class BlockLogic {
Good(TPoSBlockRewards(ownerReward, merchantReward, masternodeRewardMaybe)) Good(TPoSBlockRewards(ownerReward, merchantReward, masternodeRewardMaybe))
} }
def isPoS(block: rpc.Block, coinbase: rpc.Transaction): Boolean = {
block.nonce == 0 &&
coinbase.vin.isEmpty &&
coinbase.vout.flatMap(_.address).isEmpty
}
} }

Loading…
Cancel
Save