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 1dc98cf commit 048ab54
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,15 @@ object Transaction {
def createCoinbaseTx(blockIndex: Int, miner: String, timestamp: Long): Transaction =
Transaction(Seq(createCoinbase(blockIndex)), Seq(TxOut(miner, COINBASE_AMOUNT)), timestamp)

def validateCoinbaseTx(cbTx: Transaction, blockIndex: Int): Boolean =
cbTx.txIns.length == 1 &&
cbTx.txOuts.length == 1 &&
cbTx.txIns.head.previousOut.id == "" &&
cbTx.txIns.head.previousOut.index == blockIndex &&
cbTx.txIns.head.signature == "" &&
cbTx.txOuts.head.amount == COINBASE_AMOUNT &&
hashOfTransaction(cbTx) == cbTx.id

// hash of transaction
def hashOfTransaction(tx: Transaction): String = sha256Of(
tx.txIns.map(tx => tx.previousOut.id + tx.previousOut.index).mkString,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,14 +67,20 @@ class BlockchainTest extends FlatSpec with Matchers {
blockchainAdded.length shouldEqual 2

val blockchainAdded2 = blockchainToAdd.addBlock("This is next Block!", Seq(t1, t2))
blockchainAdded2.lastBlock().get shouldEqual expected
val newLastBlock = blockchainAdded2.lastBlock().get
newLastBlock.transactions shouldEqual expected.transactions
newLastBlock.index shouldEqual expected.index
newLastBlock.previousHash shouldEqual expected.previousHash
newLastBlock.data shouldEqual expected.data
newLastBlock.merkleHash shouldEqual expected.merkleHash
newLastBlock.difficulty shouldEqual expected.difficulty
blockchainAdded2.isValid shouldEqual true
blockchainAdded2.length shouldEqual 2
}

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

}

0 comments on commit 048ab54

Please sign in to comment.