Browse Source

server: Simplify the anorm parsers

master
Alexis Hernandez 6 years ago
parent
commit
750ab7c4ae
  1. 2
      server/app/com/xsn/explorer/data/anorm/parsers/BalanceParsers.scala
  2. 7
      server/app/com/xsn/explorer/data/anorm/parsers/BlockFilterParsers.scala
  3. 24
      server/app/com/xsn/explorer/data/anorm/parsers/BlockParsers.scala
  4. 17
      server/app/com/xsn/explorer/data/anorm/parsers/CommonParsers.scala
  5. 43
      server/app/com/xsn/explorer/data/anorm/parsers/TransactionParsers.scala

2
server/app/com/xsn/explorer/data/anorm/parsers/BalanceParsers.scala

@ -11,7 +11,7 @@ object BalanceParsers {
val parseReceived = get[BigDecimal]("received") val parseReceived = get[BigDecimal]("received")
val parseSpent = get[BigDecimal]("spent") val parseSpent = get[BigDecimal]("spent")
val parseBalance = (parseAddress ~ parseReceived ~ parseSpent).map { case address ~ received ~ spent => val parseBalance = (parseAddress() ~ parseReceived ~ parseSpent).map { case address ~ received ~ spent =>
Balance(address, received, spent) Balance(address, received, spent)
} }
} }

7
server/app/com/xsn/explorer/data/anorm/parsers/BlockFilterParsers.scala

@ -3,16 +3,15 @@ package com.xsn.explorer.data.anorm.parsers
import anorm.SqlParser._ import anorm.SqlParser._
import anorm._ import anorm._
import com.xsn.explorer.gcs.GolombCodedSet import com.xsn.explorer.gcs.GolombCodedSet
import com.xsn.explorer.models.values.HexString
object BlockFilterParsers { object BlockFilterParsers {
import CommonParsers._
val parseP = int("p") val parseP = int("p")
val parseM = int("m") val parseM = int("m")
val parseN = int("n") val parseN = int("n")
val parseHex = str("hex") val parseHex = parseHexString("hex")
.map(HexString.from)
.map { _.getOrElse(throw new RuntimeException("corrupted hex")) }
val parseFilter = (parseN ~ parseM ~ parseP ~ parseHex).map { val parseFilter = (parseN ~ parseM ~ parseP ~ parseHex).map {
case n ~ m ~ p ~ hex => case n ~ m ~ p ~ hex =>

24
server/app/com/xsn/explorer/data/anorm/parsers/BlockParsers.scala

@ -10,27 +10,15 @@ object BlockParsers {
import CommonParsers._ import CommonParsers._
val parseNextBlockhash = str("next_blockhash") val parseNextBlockhash = parseBlockhash("next_blockhash")
.map(Blockhash.from) val parsePreviousBlockhash = parseBlockhash("previous_blockhash")
.map { _.getOrElse(throw new RuntimeException("corrupted next_blockhash")) } val parseTposContract = parseTransactionId("tpos_contract")
val parseMerkleRoot = parseBlockhash("merkle_root")
val parsePreviousBlockhash = str("previous_blockhash")
.map(Blockhash.from)
.map { _.getOrElse(throw new RuntimeException("corrupted previous_blockhash")) }
val parseTposContract = str("tpos_contract")
.map(TransactionId.from)
.map { _.getOrElse(throw new RuntimeException("corrupted tpos_contract")) }
val parseMerkleRoot = str("merkle_root")
.map(Blockhash.from)
.map { _.getOrElse(throw new RuntimeException("corrupted merkle_root")) }
val parseExtractionMethod = str("extraction_method") val parseExtractionMethod = str("extraction_method")
.map(BlockExtractionMethod.withNameInsensitiveOption) .map(BlockExtractionMethod.withNameInsensitiveOption)
.map { _.getOrElse(throw new RuntimeException("corrupted extraction_method")) } .map { _.getOrElse(throw new RuntimeException("corrupted extraction_method")) }
val parseSize = int("size").map(Size.apply)
val parseHeight = int("height").map(Height.apply) val parseHeight = int("height").map(Height.apply)
val parseVersion = int("version") val parseVersion = int("version")
val parseMedianTime = long("median_time") val parseMedianTime = long("median_time")
@ -40,7 +28,7 @@ object BlockParsers {
val parseDifficulty = get[BigDecimal]("difficulty") val parseDifficulty = get[BigDecimal]("difficulty")
val parseBlock = ( val parseBlock = (
parseBlockhash ~ parseBlockhash() ~
parseNextBlockhash.? ~ parseNextBlockhash.? ~
parsePreviousBlockhash.? ~ parsePreviousBlockhash.? ~
parseTposContract.? ~ parseTposContract.? ~
@ -91,7 +79,7 @@ object BlockParsers {
) )
} }
val parseHeader = (parseBlockhash ~ parsePreviousBlockhash.? ~ parseMerkleRoot ~ parseHeight ~ parseTime).map { val parseHeader = (parseBlockhash() ~ parsePreviousBlockhash.? ~ parseMerkleRoot ~ parseHeight ~ parseTime).map {
case blockhash ~ previousBlockhash ~ merkleRoot ~ height ~ time => case blockhash ~ previousBlockhash ~ merkleRoot ~ height ~ time =>
BlockHeader.Simple(blockhash, previousBlockhash, merkleRoot, height, time) BlockHeader.Simple(blockhash, previousBlockhash, merkleRoot, height, time)
} }

17
server/app/com/xsn/explorer/data/anorm/parsers/CommonParsers.scala

@ -5,14 +5,23 @@ import com.xsn.explorer.models.values._
object CommonParsers { object CommonParsers {
val parseBlockhash = str("blockhash") def parseBlockhash(field: String = "blockhash") = str(field)
.map(Blockhash.from) .map(Blockhash.from)
.map { _.getOrElse(throw new RuntimeException("corrupted blockhash")) } .map { _.getOrElse(throw new RuntimeException(s"corrupted $field")) }
val parseAddress = str("address") def parseAddress(field: String = "address") = str(field)
.map(Address.from) .map(Address.from)
.map { _.getOrElse(throw new RuntimeException("corrupted address")) } .map { _.getOrElse(throw new RuntimeException(s"corrupted $field")) }
def parseTransactionId(field: String = "txid") = str(field)
.map(TransactionId.from)
.map { _.getOrElse(throw new RuntimeException(s"corrupted $field")) }
def parseHexString(field: String) = str(field)
.map(HexString.from)
.map { _.getOrElse(throw new RuntimeException(s"corrupted $field")) }
val parseTime = long("time") val parseTime = long("time")
val parseSize = int("size").map(Size.apply) val parseSize = int("size").map(Size.apply)
val parseIndex = int("index")
} }

43
server/app/com/xsn/explorer/data/anorm/parsers/TransactionParsers.scala

@ -1,49 +1,32 @@
package com.xsn.explorer.data.anorm.parsers package com.xsn.explorer.data.anorm.parsers
import anorm.SqlParser.{get, str} import anorm.SqlParser._
import anorm.~ import anorm.~
import com.xsn.explorer.models._ import com.xsn.explorer.models._
import com.xsn.explorer.models.persisted.{AddressTransactionDetails, Transaction} import com.xsn.explorer.models.persisted.{AddressTransactionDetails, Transaction}
import com.xsn.explorer.models.values._
object TransactionParsers { object TransactionParsers {
import CommonParsers._ import CommonParsers._
val parseTransactionId = str("txid") val parseFromTxid = parseTransactionId("from_txid")
.map(TransactionId.from)
.map { _.getOrElse(throw new RuntimeException("corrupted txid")) }
val parseFromTxid = str("from_txid")
.map(TransactionId.from)
.map { _.getOrElse(throw new RuntimeException("corrupted from_txid")) }
val parseFromOutputIndex = get[Int]("from_output_index") val parseFromOutputIndex = get[Int]("from_output_index")
val parseReceived = get[BigDecimal]("received") val parseReceived = get[BigDecimal]("received")
val parseSpent = get[BigDecimal]("spent") val parseSpent = get[BigDecimal]("spent")
val parseSent = get[BigDecimal]("sent") val parseSent = get[BigDecimal]("sent")
val parseIndex = get[Int]("index")
val parseValue = get[BigDecimal]("value") val parseValue = get[BigDecimal]("value")
val parseHexString = get[String]("hex_script") val parseHexScript = parseHexString("hex_script")
.map(HexString.from)
.map { _.getOrElse(throw new RuntimeException("corrupted hex_script")) }
val parseTposOwnerAddress = str("tpos_owner_address")
.map(Address.from)
.map { _.getOrElse(throw new RuntimeException("corrupted tpos_owner_address")) }
val parseTposMerchantAddress = str("tpos_merchant_address") val parseTposOwnerAddress = parseAddress("tpos_owner_address")
.map(Address.from) val parseTposMerchantAddress = parseAddress("tpos_merchant_address")
.map { _.getOrElse(throw new RuntimeException("corrupted tpos_merchant_address")) }
val parseTransaction = (parseTransactionId ~ parseBlockhash ~ parseTime ~ parseSize).map { val parseTransaction = (parseTransactionId() ~ parseBlockhash() ~ parseTime ~ parseSize).map {
case txid ~ blockhash ~ time ~ size => Transaction(txid, blockhash, time, size) case txid ~ blockhash ~ time ~ size => Transaction(txid, blockhash, time, size)
} }
val parseTransactionWithValues = ( val parseTransactionWithValues = (
parseTransactionId ~ parseTransactionId() ~
parseBlockhash ~ parseBlockhash() ~
parseTime ~ parseTime ~
parseSize ~ parseSize ~
parseSent ~ parseSent ~
@ -53,17 +36,17 @@ object TransactionParsers {
TransactionWithValues(txid, blockhash, time, size, sent, received) TransactionWithValues(txid, blockhash, time, size, sent, received)
} }
val parseTransactionInput = (parseFromTxid ~ parseFromOutputIndex ~ parseIndex ~ parseValue ~ parseAddress) val parseTransactionInput = (parseFromTxid ~ parseFromOutputIndex ~ parseIndex ~ parseValue ~ parseAddress())
.map { case fromTxid ~ fromOutputIndex ~ index ~ value ~ address => .map { case fromTxid ~ fromOutputIndex ~ index ~ value ~ address =>
Transaction.Input(fromTxid, fromOutputIndex, index, value, address) Transaction.Input(fromTxid, fromOutputIndex, index, value, address)
} }
val parseTransactionOutput = ( val parseTransactionOutput = (
parseTransactionId ~ parseTransactionId() ~
parseIndex ~ parseIndex ~
parseValue ~ parseValue ~
parseAddress ~ parseAddress() ~
parseHexString ~ parseHexScript ~
parseTposOwnerAddress.? ~ parseTposOwnerAddress.? ~
parseTposMerchantAddress.?).map { parseTposMerchantAddress.?).map {
@ -71,7 +54,7 @@ object TransactionParsers {
Transaction.Output(txid, index, value, address, script, tposOwnerAddress, tposMerchantAddress) Transaction.Output(txid, index, value, address, script, tposOwnerAddress, tposMerchantAddress)
} }
val parseAddressTransactionDetails = (parseAddress ~ parseTransactionId ~ parseSent ~ parseReceived ~ parseTime).map { val parseAddressTransactionDetails = (parseAddress() ~ parseTransactionId() ~ parseSent ~ parseReceived ~ parseTime).map {
case address ~ txid ~ sent ~ received ~ time => AddressTransactionDetails( case address ~ txid ~ sent ~ received ~ time => AddressTransactionDetails(
address, address,
txid, txid,

Loading…
Cancel
Save