Browse Source

server: Store the filter on the BlockHeader

master
Alexis Hernandez 6 years ago
parent
commit
8f5f4a48cb
  1. 2
      server/app/com/xsn/explorer/data/anorm/parsers/BlockParsers.scala
  2. 63
      server/app/com/xsn/explorer/models/persisted/BlockHeader.scala

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

@ -93,6 +93,6 @@ 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(blockhash, previousBlockhash, merkleRoot, height, time) BlockHeader.Simple(blockhash, previousBlockhash, merkleRoot, height, time)
} }
} }

63
server/app/com/xsn/explorer/models/persisted/BlockHeader.scala

@ -1,16 +1,65 @@
package com.xsn.explorer.models.persisted package com.xsn.explorer.models.persisted
import com.xsn.explorer.gcs.GolombCodedSet
import com.xsn.explorer.models.values._ import com.xsn.explorer.models.values._
import io.scalaland.chimney.dsl._
import play.api.libs.json.{Json, Writes} import play.api.libs.json.{Json, Writes}
case class BlockHeader( sealed trait BlockHeader {
hash: Blockhash,
previousBlockhash: Option[Blockhash], def hash: Blockhash
merkleRoot: Blockhash, def previousBlockhash: Option[Blockhash]
height: Height, def merkleRoot: Blockhash
time: Long) def height: Height
def time: Long
def withFilter(filter: GolombCodedSet): BlockHeader.HasFilter = {
this
.into[BlockHeader.HasFilter]
.withFieldConst(_.filter, filter)
.transform
}
}
object BlockHeader { 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
)
}
} }
Loading…
Cancel
Save