Skip to content

Commit

Permalink
add some tests for Blockchain
Browse files Browse the repository at this point in the history
  • Loading branch information
fluency03 committed Apr 27, 2018
1 parent 11b81de commit 1dc98cf
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 0 deletions.
3 changes: 3 additions & 0 deletions src/main/scala/com/fluency03/blockchain/core/Blockchain.scala
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import com.fluency03.blockchain.core.Blockchain._
import org.json4s.native.JsonMethods.{compact, render}
import org.json4s.{Extraction, JValue}

import scala.annotation.tailrec

/**
* Blockchain with difficulty and the chain of Blocks.
* @param difficulty Difficulty of a Blockchain
Expand Down Expand Up @@ -45,6 +47,7 @@ object Blockchain {

def apply(difficulty: Int): Blockchain = new Blockchain(difficulty, Seq(Block.genesis(difficulty)))

@tailrec
def isValidChain(chain: Seq[Block]): Boolean = chain match {
case Nil => true
case g +: Nil => g.previousHash == ZERO64 && g.index == 0 && g.hasValidHash
Expand Down
3 changes: 3 additions & 0 deletions src/main/scala/com/fluency03/blockchain/core/Merkle.scala
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
package com.fluency03.blockchain
package core

import scala.annotation.tailrec

object Merkle {
def computeRoot(trans: Seq[Transaction]): String = computeRootOfHashes(trans.map(_.id))

@tailrec
def computeRootOfHashes(hashes: Seq[String]): String = hashes.length match {
case 0 => ZERO64
case 1 => hashes.head
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.fluency03.blockchain
package core

import com.fluency03.blockchain.core.Blockchain.isValidChain
import com.fluency03.blockchain.core.Transaction.createCoinbaseTx
import org.json4s.JsonDSL._
import org.json4s.JValue
Expand Down Expand Up @@ -64,10 +65,16 @@ class BlockchainTest extends FlatSpec with Matchers {
blockchainAdded.lastBlock().get shouldEqual expected
blockchainAdded.isValid shouldEqual true
blockchainAdded.length shouldEqual 2

val blockchainAdded2 = blockchainToAdd.addBlock("This is next Block!", Seq(t1, t2))
blockchainAdded2.lastBlock().get shouldEqual expected
blockchainAdded2.isValid shouldEqual true
blockchainAdded2.length shouldEqual 2
}

"Blockchain" should "have be validatable." in {
// TODO (Chang): isValidChain
isValidChain(Nil) shouldEqual false
}

}

0 comments on commit 1dc98cf

Please sign in to comment.