Skip to content

Commit

Permalink
fix json of Block
Browse files Browse the repository at this point in the history
  • Loading branch information
fluency03 committed Apr 13, 2018
1 parent 5bd348d commit e60207c
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 20 deletions.
1 change: 0 additions & 1 deletion build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ val json4sVersion = "3.5.3"
val scalaTestVersion = "3.0.5"
val scalaMockVersion = "4.1.0"


val httpDependencies = Seq(
"com.typesafe.akka" %% "akka-http" % akkaHttpVersion,
"com.typesafe.akka" %% "akka-stream" % akkaVersion,
Expand Down
8 changes: 2 additions & 6 deletions src/main/scala/com/fluency03/blockchain/core/Block.scala
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@ import java.time.Instant

import com.fluency03.blockchain.Util.isWithValidDifficulty
import com.fluency03.blockchain.core.BlockHeader.hashOfHeaderFields
import org.json4s.JValue
import org.json4s.JsonDSL._
import org.json4s.{Extraction, JValue}
import org.json4s.native.JsonMethods.{compact, render}

/**
Expand Down Expand Up @@ -40,10 +39,7 @@ case class Block(header: BlockHeader, transactions: List[Transaction] = List())

def hasValidMerkleHash: Boolean = merkleHash == MerkleNode.computeRoot(transactions)

def toJson: JValue =
("header" -> header.toJson) ~
("hash" -> hash) ~
("transactions" -> transactions.map(_.toJson))
def toJson: JValue = Extraction.decompose(this)

override def toString: String = compact(render(toJson))

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package com.fluency03.blockchain.core

import com.fluency03.blockchain.Util.{hashOf, isWithValidDifficulty}
import com.fluency03.blockchain.core.BlockHeader.hashOfBlockHeader
import org.json4s.native.JsonMethods.{compact, render}
import org.json4s.{Extraction, JValue}

Expand All @@ -14,6 +13,7 @@ import org.json4s.{Extraction, JValue}
* @param merkleHash Merkle root hash of current Block
* @param timestamp Timestamp of current Block
* @param nonce Nonce of current Block
* @param hash Hash of current Block
*/
case class BlockHeader(
index: Int,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ class BlockHeaderTest extends FlatSpec with Matchers {
("data" -> "Welcome to Blockchain in Scala!") ~
("merkleHash" -> genesisTx.hash) ~
("timestamp" -> 1523472721) ~
("nonce" -> 13860)
("nonce" -> 13860) ~
("hash" -> "00003607219f7a455e216f19ac3a34e3b158cf7282f7fdc624c93d593c2fc61f")
genesisHeader.toJson shouldEqual json
parse(genesisHeader.toString) shouldEqual json
}
Expand All @@ -45,7 +46,8 @@ class BlockHeaderTest extends FlatSpec with Matchers {
("data" -> "Welcome to Blockchain in Scala!") ~
("merkleHash" -> genesisTx.hash) ~
("timestamp" -> 1523472721) ~
("nonce" -> (genesisHeader.nonce + 1))
("nonce" -> (genesisHeader.nonce + 1)) ~
("hash" -> newHash)
genesisHeaderNextTrial.toJson shouldEqual json
parse(genesisHeaderNextTrial.toString) shouldEqual json
}
Expand Down
20 changes: 10 additions & 10 deletions src/test/scala/com/fluency03/blockchain/core/BlockTest.scala
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ class BlockTest extends FlatSpec with Matchers {
"data":"Welcome to Blockchain in Scala!",
"merkleHash":"7814a9c43e9015462e5ffec1a3a9a69be024c1aacfa3ec4c879b5cd544761e7e",
"timestamp":1523472721,
"nonce":13860
"nonce":13860,
"hash":"00003607219f7a455e216f19ac3a34e3b158cf7282f7fdc624c93d593c2fc61f",
},
"hash":"00003607219f7a455e216f19ac3a34e3b158cf7282f7fdc624c93d593c2fc61f",
"transactions":[
{
"sender":"0000000000000000000000000000000000000000000000000000000000000000",
Expand Down Expand Up @@ -48,8 +48,8 @@ class BlockTest extends FlatSpec with Matchers {
("data" -> "Welcome to Blockchain in Scala!") ~
("merkleHash" -> genesisTx.hash) ~
("timestamp" -> 1523472721) ~
("nonce" -> 13860)) ~
("hash" -> "00003607219f7a455e216f19ac3a34e3b158cf7282f7fdc624c93d593c2fc61f") ~
("nonce" -> 13860) ~
("hash" -> "00003607219f7a455e216f19ac3a34e3b158cf7282f7fdc624c93d593c2fc61f")) ~
("transactions" -> List(genesisTx).map(_.toJson))
genesis.toJson shouldEqual json
parse(genesis.toString) shouldEqual json
Expand All @@ -75,8 +75,8 @@ class BlockTest extends FlatSpec with Matchers {
("data" -> "Welcome to Blockchain in Scala!") ~
("merkleHash" -> genesisTx.hash) ~
("timestamp" -> 1523472721) ~
("nonce" -> (genesis.nonce + 1))) ~
("hash" -> newHash) ~
("nonce" -> (genesis.nonce + 1)) ~
("hash" -> newHash)) ~
("transactions" -> List(genesisTx).map(_.toJson))
genesisNextTrial.toJson shouldEqual json
parse(genesisNextTrial.toString) shouldEqual json
Expand Down Expand Up @@ -105,8 +105,8 @@ class BlockTest extends FlatSpec with Matchers {
("data" -> "Welcome to Blockchain in Scala!") ~
("merkleHash" -> newMerkleHash) ~
("timestamp" -> 1523472721) ~
("nonce" -> genesis.nonce)) ~
("hash" -> newHash) ~
("nonce" -> genesis.nonce) ~
("hash" -> newHash)) ~
("transactions" -> (t1 :: genesis.transactions).map(_.toJson))
newBlock.toJson shouldEqual json
parse(newBlock.toString) shouldEqual json
Expand Down Expand Up @@ -138,8 +138,8 @@ class BlockTest extends FlatSpec with Matchers {
("data" -> "Welcome to Blockchain in Scala!") ~
("merkleHash" -> newMerkleHash) ~
("timestamp" -> 1523472721) ~
("nonce" -> genesis.nonce)) ~
("hash" -> newHash) ~
("nonce" -> genesis.nonce) ~
("hash" -> newHash)) ~
("transactions" -> (t1 :: t2 :: t3 :: t4 :: genesis.transactions).map(_.toJson))
newBlock.toJson shouldEqual json
parse(newBlock.toString) shouldEqual json
Expand Down

0 comments on commit e60207c

Please sign in to comment.