Alexis Hernandez
6 years ago
2 changed files with 57 additions and 8 deletions
@ -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 |
|||
) |
|||
} |
|||
} |
Loading…
Reference in new issue