From 8f5f4a48cb68ef53cca97e6cd664640b5098facc Mon Sep 17 00:00:00 2001 From: Alexis Hernandez Date: Mon, 18 Mar 2019 23:39:38 -0700 Subject: [PATCH] server: Store the filter on the BlockHeader --- .../data/anorm/parsers/BlockParsers.scala | 2 +- .../models/persisted/BlockHeader.scala | 63 ++++++++++++++++--- 2 files changed, 57 insertions(+), 8 deletions(-) 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 bc40337..3accba4 100644 --- a/server/app/com/xsn/explorer/data/anorm/parsers/BlockParsers.scala +++ b/server/app/com/xsn/explorer/data/anorm/parsers/BlockParsers.scala @@ -93,6 +93,6 @@ object BlockParsers { val parseHeader = (parseBlockhash ~ parsePreviousBlockhash.? ~ parseMerkleRoot ~ parseHeight ~ parseTime).map { case blockhash ~ previousBlockhash ~ merkleRoot ~ height ~ time => - BlockHeader(blockhash, previousBlockhash, merkleRoot, height, time) + BlockHeader.Simple(blockhash, previousBlockhash, merkleRoot, height, time) } } diff --git a/server/app/com/xsn/explorer/models/persisted/BlockHeader.scala b/server/app/com/xsn/explorer/models/persisted/BlockHeader.scala index 9ac8a05..574f4c4 100644 --- a/server/app/com/xsn/explorer/models/persisted/BlockHeader.scala +++ b/server/app/com/xsn/explorer/models/persisted/BlockHeader.scala @@ -1,16 +1,65 @@ package com.xsn.explorer.models.persisted +import com.xsn.explorer.gcs.GolombCodedSet import com.xsn.explorer.models.values._ +import io.scalaland.chimney.dsl._ import play.api.libs.json.{Json, Writes} -case class BlockHeader( - hash: Blockhash, - previousBlockhash: Option[Blockhash], - merkleRoot: Blockhash, - height: Height, - time: Long) +sealed trait BlockHeader { + + def hash: Blockhash + def previousBlockhash: Option[Blockhash] + def merkleRoot: Blockhash + def height: Height + def time: Long + + def withFilter(filter: GolombCodedSet): BlockHeader.HasFilter = { + this + .into[BlockHeader.HasFilter] + .withFieldConst(_.filter, filter) + .transform + } +} object BlockHeader { - implicit val writes: Writes[BlockHeader] = Json.writes[BlockHeader] + case class Simple( + hash: Blockhash, + previousBlockhash: Option[Blockhash], + merkleRoot: Blockhash, + height: Height, + time: Long) extends BlockHeader + + case class HasFilter( + hash: Blockhash, + previousBlockhash: Option[Blockhash], + merkleRoot: Blockhash, + height: Height, + time: Long, + filter: GolombCodedSet) extends BlockHeader + + private implicit val filterWrites: Writes[GolombCodedSet] = (obj: GolombCodedSet) => { + Json.obj( + "n" -> obj.n, + "m" -> obj.m, + "p" -> obj.p, + "hex" -> obj.hex.string + ) + } + + implicit val writes: Writes[BlockHeader] = (obj: BlockHeader) => { + val filterMaybe = obj match { + case x: HasFilter => Some(x.filter) + case _ => Option.empty + } + + Json.obj( + "hash" -> obj.hash, + "previousBlockhash" -> obj.previousBlockhash, + "merkleRoot" -> obj.merkleRoot, + "height" -> obj.height, + "time" -> obj.time, + "filter" -> filterMaybe + ) + } } \ No newline at end of file