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