File tree Expand file tree Collapse file tree 3 files changed +13
-0
lines changed
main/scala/com/fluency03/blockchain/core
test/scala/com/fluency03/blockchain/core Expand file tree Collapse file tree 3 files changed +13
-0
lines changed Original file line number Diff line number Diff line change @@ -6,6 +6,8 @@ import com.fluency03.blockchain.core.Blockchain._
6
6
import org .json4s .native .JsonMethods .{compact , render }
7
7
import org .json4s .{Extraction , JValue }
8
8
9
+ import scala .annotation .tailrec
10
+
9
11
/**
10
12
* Blockchain with difficulty and the chain of Blocks.
11
13
* @param difficulty Difficulty of a Blockchain
@@ -45,6 +47,7 @@ object Blockchain {
45
47
46
48
def apply (difficulty : Int ): Blockchain = new Blockchain (difficulty, Seq (Block .genesis(difficulty)))
47
49
50
+ @ tailrec
48
51
def isValidChain (chain : Seq [Block ]): Boolean = chain match {
49
52
case Nil => true
50
53
case g +: Nil => g.previousHash == ZERO64 && g.index == 0 && g.hasValidHash
Original file line number Diff line number Diff line change 1
1
package com .fluency03 .blockchain
2
2
package core
3
3
4
+ import scala .annotation .tailrec
5
+
4
6
object Merkle {
5
7
def computeRoot (trans : Seq [Transaction ]): String = computeRootOfHashes(trans.map(_.id))
6
8
9
+ @ tailrec
7
10
def computeRootOfHashes (hashes : Seq [String ]): String = hashes.length match {
8
11
case 0 => ZERO64
9
12
case 1 => hashes.head
Original file line number Diff line number Diff line change 1
1
package com .fluency03 .blockchain
2
2
package core
3
3
4
+ import com .fluency03 .blockchain .core .Blockchain .isValidChain
4
5
import com .fluency03 .blockchain .core .Transaction .createCoinbaseTx
5
6
import org .json4s .JsonDSL ._
6
7
import org .json4s .JValue
@@ -64,10 +65,16 @@ class BlockchainTest extends FlatSpec with Matchers {
64
65
blockchainAdded.lastBlock().get shouldEqual expected
65
66
blockchainAdded.isValid shouldEqual true
66
67
blockchainAdded.length shouldEqual 2
68
+
69
+ val blockchainAdded2 = blockchainToAdd.addBlock(" This is next Block!" , Seq (t1, t2))
70
+ blockchainAdded2.lastBlock().get shouldEqual expected
71
+ blockchainAdded2.isValid shouldEqual true
72
+ blockchainAdded2.length shouldEqual 2
67
73
}
68
74
69
75
" Blockchain" should " have be validatable." in {
70
76
// TODO (Chang): isValidChain
77
+ isValidChain(Nil ) shouldEqual false
71
78
}
72
79
73
80
}
You can’t perform that action at this time.
0 commit comments