diff --git a/server/app/com/xsn/explorer/data/anorm/parsers/BalanceParsers.scala b/server/app/com/xsn/explorer/data/anorm/parsers/BalanceParsers.scala index c8f142a..3e55f39 100644 --- a/server/app/com/xsn/explorer/data/anorm/parsers/BalanceParsers.scala +++ b/server/app/com/xsn/explorer/data/anorm/parsers/BalanceParsers.scala @@ -11,7 +11,7 @@ object BalanceParsers { val parseReceived = get[BigDecimal]("received") 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) } } diff --git a/server/app/com/xsn/explorer/data/anorm/parsers/BlockFilterParsers.scala b/server/app/com/xsn/explorer/data/anorm/parsers/BlockFilterParsers.scala index bc31566..4ad27ec 100644 --- a/server/app/com/xsn/explorer/data/anorm/parsers/BlockFilterParsers.scala +++ b/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._ import com.xsn.explorer.gcs.GolombCodedSet -import com.xsn.explorer.models.values.HexString object BlockFilterParsers { + import CommonParsers._ + val parseP = int("p") val parseM = int("m") val parseN = int("n") - val parseHex = str("hex") - .map(HexString.from) - .map { _.getOrElse(throw new RuntimeException("corrupted hex")) } + val parseHex = parseHexString("hex") val parseFilter = (parseN ~ parseM ~ parseP ~ parseHex).map { case n ~ m ~ p ~ hex => diff --git a/server/app/com/xsn/explorer/data/anorm/parsers/BlockParsers.scala b/server/app/com/xsn/explorer/data/anorm/parsers/BlockParsers.scala index 3accba4..0f8adbe 100644 --- a/server/app/com/xsn/explorer/data/anorm/parsers/BlockParsers.scala +++ b/server/app/com/xsn/explorer/data/anorm/parsers/BlockParsers.scala @@ -10,27 +10,15 @@ object BlockParsers { import CommonParsers._ - val parseNextBlockhash = str("next_blockhash") - .map(Blockhash.from) - .map { _.getOrElse(throw new RuntimeException("corrupted next_blockhash")) } - - 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 parseNextBlockhash = parseBlockhash("next_blockhash") + val parsePreviousBlockhash = parseBlockhash("previous_blockhash") + val parseTposContract = parseTransactionId("tpos_contract") + val parseMerkleRoot = parseBlockhash("merkle_root") val parseExtractionMethod = str("extraction_method") .map(BlockExtractionMethod.withNameInsensitiveOption) .map { _.getOrElse(throw new RuntimeException("corrupted extraction_method")) } - val parseSize = int("size").map(Size.apply) val parseHeight = int("height").map(Height.apply) val parseVersion = int("version") val parseMedianTime = long("median_time") @@ -40,7 +28,7 @@ object BlockParsers { val parseDifficulty = get[BigDecimal]("difficulty") val parseBlock = ( - parseBlockhash ~ + parseBlockhash() ~ parseNextBlockhash.? ~ parsePreviousBlockhash.? ~ 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 => BlockHeader.Simple(blockhash, previousBlockhash, merkleRoot, height, time) } diff --git a/server/app/com/xsn/explorer/data/anorm/parsers/CommonParsers.scala b/server/app/com/xsn/explorer/data/anorm/parsers/CommonParsers.scala index 60e5fd3..07f9eb7 100644 --- a/server/app/com/xsn/explorer/data/anorm/parsers/CommonParsers.scala +++ b/server/app/com/xsn/explorer/data/anorm/parsers/CommonParsers.scala @@ -5,14 +5,23 @@ import com.xsn.explorer.models.values._ object CommonParsers { - val parseBlockhash = str("blockhash") + def parseBlockhash(field: String = "blockhash") = str(field) .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 { _.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 parseSize = int("size").map(Size.apply) + val parseIndex = int("index") } diff --git a/server/app/com/xsn/explorer/data/anorm/parsers/TransactionParsers.scala b/server/app/com/xsn/explorer/data/anorm/parsers/TransactionParsers.scala index dc033f7..0bca7e0 100644 --- a/server/app/com/xsn/explorer/data/anorm/parsers/TransactionParsers.scala +++ b/server/app/com/xsn/explorer/data/anorm/parsers/TransactionParsers.scala @@ -1,49 +1,32 @@ package com.xsn.explorer.data.anorm.parsers -import anorm.SqlParser.{get, str} +import anorm.SqlParser._ import anorm.~ import com.xsn.explorer.models._ import com.xsn.explorer.models.persisted.{AddressTransactionDetails, Transaction} -import com.xsn.explorer.models.values._ object TransactionParsers { import CommonParsers._ - val parseTransactionId = str("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 parseFromTxid = parseTransactionId("from_txid") val parseFromOutputIndex = get[Int]("from_output_index") val parseReceived = get[BigDecimal]("received") val parseSpent = get[BigDecimal]("spent") val parseSent = get[BigDecimal]("sent") - - val parseIndex = get[Int]("index") val parseValue = get[BigDecimal]("value") - val parseHexString = get[String]("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 parseHexScript = parseHexString("hex_script") - val parseTposMerchantAddress = str("tpos_merchant_address") - .map(Address.from) - .map { _.getOrElse(throw new RuntimeException("corrupted tpos_merchant_address")) } + val parseTposOwnerAddress = parseAddress("tpos_owner_address") + val parseTposMerchantAddress = parseAddress("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) } val parseTransactionWithValues = ( - parseTransactionId ~ - parseBlockhash ~ + parseTransactionId() ~ + parseBlockhash() ~ parseTime ~ parseSize ~ parseSent ~ @@ -53,17 +36,17 @@ object TransactionParsers { 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 => Transaction.Input(fromTxid, fromOutputIndex, index, value, address) } val parseTransactionOutput = ( - parseTransactionId ~ + parseTransactionId() ~ parseIndex ~ parseValue ~ - parseAddress ~ - parseHexString ~ + parseAddress() ~ + parseHexScript ~ parseTposOwnerAddress.? ~ parseTposMerchantAddress.?).map { @@ -71,7 +54,7 @@ object TransactionParsers { 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( address, txid,