Skip to content

Commit

Permalink
add length of Blockchain
Browse files Browse the repository at this point in the history
  • Loading branch information
fluency03 committed Apr 26, 2018
1 parent bc4efe5 commit 40f40fb
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 7 deletions.
8 changes: 5 additions & 3 deletions src/main/scala/com/fluency03/blockchain/core/Blockchain.scala
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
package com.fluency03.blockchain
package core

import com.fluency03.blockchain.core.Blockchain._
import com.fluency03.blockchain.core.Block.canBeChained
import org.json4s.JsonDSL._
import org.json4s.{Extraction, JValue}
import com.fluency03.blockchain.core.Blockchain._
import org.json4s.native.JsonMethods.{compact, render}
import org.json4s.{Extraction, JValue}

/**
* Blockchain with difficulty and the chain of Blocks.
Expand All @@ -14,6 +13,7 @@ import org.json4s.native.JsonMethods.{compact, render}
*/
case class Blockchain(difficulty: Int = 4, chain: Seq[Block] = Seq(Block.genesisBlock)) {


def addBlock(newBlockData: String, transactions: Seq[Transaction]): Blockchain =
Blockchain(difficulty, mineNextBlock(newBlockData, transactions) +: chain)

Expand All @@ -35,6 +35,8 @@ case class Blockchain(difficulty: Int = 4, chain: Seq[Block] = Seq(Block.genesis
case _ => isValidChain(chain)
}

def length: Int = chain.length

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
Expand Up @@ -3,11 +3,7 @@ package core

import java.security.KeyPair

import com.fluency03.blockchain.Crypto
import com.fluency03.blockchain.core.Transaction.hashOfTransaction
import org.bouncycastle.util.encoders.Hex
import org.json4s.JsonAST.JObject
import org.json4s.JsonDSL._
import org.json4s.native.JsonMethods.{compact, render}
import org.json4s.{Extraction, JValue}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ class BlockchainTest extends FlatSpec with Matchers {
blockchain.lastBlock().isEmpty shouldEqual false
blockchain.lastBlock().get shouldEqual expectedGenesisBlock
blockchain.isValid shouldEqual true
blockchain.length shouldEqual 1
val json = ("difficulty" -> blockchain.difficulty) ~ ("chain" -> JArray(List(expectedBlockJson)))
blockchain.toJson shouldEqual json
parse(blockchain.toString) shouldEqual json
Expand All @@ -46,6 +47,7 @@ class BlockchainTest extends FlatSpec with Matchers {
blockchainOf5.lastBlock().isEmpty shouldEqual false
blockchainOf5.lastBlock().get shouldEqual genesisOf5
blockchainOf5.isValid shouldEqual true
blockchainOf5.length shouldEqual 1
}

"Blockchain" should "be able to mine the next Block." in {
Expand All @@ -61,6 +63,7 @@ class BlockchainTest extends FlatSpec with Matchers {
val blockchainAdded = blockchainToAdd.addBlock(actual)
blockchainAdded.lastBlock().get shouldEqual expected
blockchainAdded.isValid shouldEqual true
blockchainToAdd.length shouldEqual 2
}

"Blockchain" should "have be validatable." in {
Expand Down

0 comments on commit 40f40fb

Please sign in to comment.