Skip to content

Commit

Permalink
add tests for Blockchain
Browse files Browse the repository at this point in the history
  • Loading branch information
fluency03 committed Apr 14, 2018
1 parent e60207c commit 32d889e
Show file tree
Hide file tree
Showing 6 changed files with 100 additions and 7 deletions.
18 changes: 18 additions & 0 deletions src/main/resources/genesis-block.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"header": {
"index": 0,
"previousHash": "0000000000000000000000000000000000000000000000000000000000000000",
"data": "Welcome to Blockchain in Scala!",
"merkleHash": "7814a9c43e9015462e5ffec1a3a9a69be024c1aacfa3ec4c879b5cd544761e7e",
"timestamp": 1523472721,
"nonce": 13860,
"hash": "00003607219f7a455e216f19ac3a34e3b158cf7282f7fdc624c93d593c2fc61f"
},
"transactions": [
{
"sender": "0000000000000000000000000000000000000000000000000000000000000000",
"receiver": "0000000000000000000000000000000000000000000000000000000000000000",
"amount": 50.0
}
]
}
4 changes: 2 additions & 2 deletions src/main/scala/com/fluency03/blockchain/core/Block.scala
Original file line number Diff line number Diff line change
Expand Up @@ -60,14 +60,14 @@ object Block {

lazy val genesisBlock: Block = genesis()

def genesis(): Block =
def genesis(difficulty: Int = 4): Block =
mineNextBlock(
0,
ZERO64,
"Welcome to Blockchain in Scala!",
List(Transaction(ZERO64, ZERO64, 50)),
Instant.parse("2018-04-11T18:52:01Z").getEpochSecond,
4)
difficulty)

def mineNextBlock(
nextIndex: Int,
Expand Down
12 changes: 8 additions & 4 deletions src/main/scala/com/fluency03/blockchain/core/Blockchain.scala
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,24 @@ package com.fluency03.blockchain.core

import com.fluency03.blockchain.Util.getCurrentTimestamp

import scala.collection.mutable
import scala.collection.mutable.ListBuffer

/**
* Blockchain with difficulty and the chain of Blocks.
* @param difficulty Difficulty of a Blockchain
* @param chain Chain of Blocks
*/
case class Blockchain(difficulty: Int = 4, chain: List[Block] = List(Block.genesisBlock)) {
val currentTransactions: mutable.Set[Transaction] = new mutable.HashSet[Transaction]()
val currentTransactions: ListBuffer[Transaction] = new ListBuffer[Transaction]()

def addBlock(newBlockData: String): Blockchain = {
Blockchain(difficulty, mineNextBlock(newBlockData).addTransactions(currentTransactions.toList) :: chain)
}

def addBlock(newBlock: Block): Blockchain = {
Blockchain(difficulty, newBlock :: chain)
}

def addTransaction(t: Transaction): Blockchain = {
currentTransactions += t
this
Expand All @@ -24,7 +28,7 @@ case class Blockchain(difficulty: Int = 4, chain: List[Block] = List(Block.genes
def addTransaction(sender: String, receiver: String, amount: Double): Blockchain =
addTransaction(Transaction(sender, receiver, amount))

def addTransaction(trans: Set[Transaction]): Blockchain = {
def addTransactions(trans: List[Transaction]): Blockchain = {
currentTransactions ++= trans
this
}
Expand All @@ -48,7 +52,7 @@ case class Blockchain(difficulty: Int = 4, chain: List[Block] = List(Block.genes

object Blockchain {

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

}

18 changes: 18 additions & 0 deletions src/test/resources/genesis-block.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"header": {
"index": 0,
"previousHash": "0000000000000000000000000000000000000000000000000000000000000000",
"data": "Welcome to Blockchain in Scala!",
"merkleHash": "7814a9c43e9015462e5ffec1a3a9a69be024c1aacfa3ec4c879b5cd544761e7e",
"timestamp": 1523472721,
"nonce": 13860,
"hash": "00003607219f7a455e216f19ac3a34e3b158cf7282f7fdc624c93d593c2fc61f"
},
"transactions": [
{
"sender": "0000000000000000000000000000000000000000000000000000000000000000",
"receiver": "0000000000000000000000000000000000000000000000000000000000000000",
"amount": 50.0
}
]
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ class BlockTest extends FlatSpec with Matchers {
"merkleHash":"7814a9c43e9015462e5ffec1a3a9a69be024c1aacfa3ec4c879b5cd544761e7e",
"timestamp":1523472721,
"nonce":13860,
"hash":"00003607219f7a455e216f19ac3a34e3b158cf7282f7fdc624c93d593c2fc61f",
"hash":"00003607219f7a455e216f19ac3a34e3b158cf7282f7fdc624c93d593c2fc61f"
},
"transactions":[
{
Expand Down
53 changes: 53 additions & 0 deletions src/test/scala/com/fluency03/blockchain/core/BlockchainTest.scala
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,68 @@ package com.fluency03.blockchain.core

import org.scalatest.{FlatSpec, Matchers}

import scala.collection.mutable.ListBuffer

class BlockchainTest extends FlatSpec with Matchers {

val blockchain: Blockchain = Blockchain()
val genesis: Block = Block.genesisBlock

val blockchainOf5: Blockchain = Blockchain(5)
val genesisOf5: Block = Block.genesis(5)

val t1: Transaction = Transaction(ZERO64, ZERO64, 10)
val t2: Transaction = Transaction(ZERO64, ZERO64, 20)
val t3: Transaction = Transaction(ZERO64, ZERO64, 30)
val t4: Transaction = Transaction(ZERO64, ZERO64, 40)

"A new Blockchain" should "have all default values." in {
blockchain.difficulty shouldEqual 4
blockchain.chain shouldEqual List(genesis)
blockchain.lastBlock().isEmpty shouldEqual false
blockchain.lastBlock().get shouldEqual genesis
blockchain.currentTransactions shouldEqual new ListBuffer[Transaction]()
}

"A new Blockchain with different difficulty" should "have all default values but the difficulty." in {
blockchainOf5.difficulty shouldEqual 5
blockchainOf5.chain shouldEqual List(genesisOf5)
blockchainOf5.lastBlock().isEmpty shouldEqual false
blockchainOf5.lastBlock().get shouldEqual genesisOf5
blockchainOf5.currentTransactions shouldEqual new ListBuffer[Transaction]()
}

"Add a Transaction to a Blockchain" should "add these Transactions to its currentTransactions collection." in {
val trans = new ListBuffer[Transaction]()
blockchain.addTransaction(t1)
trans += t1
blockchain.currentTransactions shouldEqual trans

blockchain.addTransaction(ZERO64, ZERO64, 20)
blockchain.addTransaction(t3)
trans ++= t2 :: t3 :: Nil
blockchain.currentTransactions shouldEqual trans
}

"Add a List of Transaction to a Blockchain" should "add these Transactions to its currentTransactions collection." in {
val trans = new ListBuffer[Transaction]()
blockchainOf5.addTransactions(t2 :: t3 :: t4 :: Nil)
trans ++= t2 :: t3 :: t4 :: Nil
blockchainOf5.currentTransactions shouldEqual trans
}

"Blockchain" should "be able to mine the next Block." in {
val blockchainToAdd: Blockchain = Blockchain()
val genesis: Block = Block.genesisBlock

val actual = blockchainToAdd.mineNextBlock("This is next Block!")
val expected = Block.mineNextBlock(genesis, "This is next Block!", List(), actual.timestamp, blockchain.difficulty)
actual shouldEqual expected

blockchainToAdd.lastBlock().get shouldEqual genesis
val blockchainAdded = blockchainToAdd.addBlock(actual)
blockchainAdded.lastBlock().get shouldEqual expected
}


}

0 comments on commit 32d889e

Please sign in to comment.