Skip to content

Commit

Permalink
add noDuplicateTxIn tests and rewrite allTransactionsValid considerin…
Browse files Browse the repository at this point in the history
…g coinbase tx
  • Loading branch information
fluency03 committed Apr 27, 2018
1 parent 1014558 commit 658718f
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 8 deletions.
9 changes: 7 additions & 2 deletions src/main/scala/com/fluency03/blockchain/core/Block.scala
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package com.fluency03.blockchain
package core

import com.fluency03.blockchain.core.BlockHeader.hashOfHeaderFields
import com.fluency03.blockchain.core.Transaction.createCoinbaseTx
import com.fluency03.blockchain.core.Transaction.{createCoinbaseTx, validateCoinbaseTx, noDuplicateTxInOf}
import org.json4s.native.JsonMethods.{compact, render}
import org.json4s.{Extraction, JValue}

Expand Down Expand Up @@ -39,7 +39,12 @@ case class Block(header: BlockHeader, transactions: Seq[Transaction], hash: Stri

def hasValidHeaderHash: Boolean = hash == header.hash

def allTransactionsValid(uTxOs: mutable.Map[Outpoint, TxOut]): Boolean = transactions.forall(tx => tx.isValid(uTxOs))
def allTransactionsValid(uTxOs: mutable.Map[Outpoint, TxOut]): Boolean = transactions match {
case Nil => false
case init :+ last => validateCoinbaseTx(last, index) && init.forall(tx => tx.isValid(uTxOs))
}

def noDuplicateTxIn(): Boolean = noDuplicateTxInOf(transactions)

def toJson: JValue = Extraction.decompose(this)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ object Transaction {
.map(txIn => Outpoint(txIn.previousOut.id, txIn.previousOut.index) -> TxOut("", 0))
.toMap

def noDuplicateTxIn(transactions: Seq[Transaction]): Boolean = {
def noDuplicateTxInOf(transactions: Seq[Transaction]): Boolean = {
val allRefs = transactions.map(_.txIns.map(_.previousOut)).foldLeft(Seq.empty[Outpoint])(_ ++ _)
allRefs.distinct.length == allRefs.length
}
Expand Down
3 changes: 3 additions & 0 deletions src/test/scala/com/fluency03/blockchain/core/BlockTest.scala
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ class BlockTest extends FlatSpec with Matchers {
genesis.nonce shouldEqual expectedHeader.nonce
genesis.hash shouldEqual expectedHeader.hash
genesis.hasValidHash shouldEqual true
genesis.noDuplicateTxIn shouldEqual true
genesis.toJson shouldEqual expectedBlockJson
parse(genesis.toString) shouldEqual expectedBlockJson

Expand Down Expand Up @@ -69,6 +70,7 @@ class BlockTest extends FlatSpec with Matchers {
genesisNextTrial.hash shouldEqual newExpectedHeader.hash
genesisNextTrial.hasValidMerkleHash shouldEqual true
genesisNextTrial.hasValidHash shouldEqual false
genesisNextTrial.noDuplicateTxIn shouldEqual true
val headerJson = expectedHeader.toJson.transformField {
case ("nonce", JInt(x)) => ("nonce", JInt(x+1))
}
Expand Down Expand Up @@ -96,6 +98,7 @@ class BlockTest extends FlatSpec with Matchers {
newBlock.hash shouldEqual newHash
newBlock.hasValidMerkleHash shouldEqual true
newBlock.hasValidHash shouldEqual false
newBlock.noDuplicateTxIn shouldEqual true
val headerJson = expectedHeader.toJson.transformField {
case ("merkleHash", JString(_)) => ("merkleHash", newMerkleHash)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -208,35 +208,35 @@ class TransactionTest extends FlatSpec with Matchers {
}

"noDuplicateTxIn" should "detect whether Seq of Transactions contians duplicate TxIns." in {
noDuplicateTxIn(Seq.empty[Transaction]) shouldEqual true
noDuplicateTxInOf(Seq.empty[Transaction]) shouldEqual true
val tx1: Transaction = Transaction(
Seq(TxIn(Outpoint("a", 0), ""),
TxIn(Outpoint("b", 1), "")),
Seq(TxOut("o", 40)),
genesisTimestamp
)
noDuplicateTxIn(Seq(tx1)) shouldEqual true
noDuplicateTxInOf(Seq(tx1)) shouldEqual true
val tx2: Transaction = Transaction(
Seq(TxIn(Outpoint("c", 0), ""),
TxIn(Outpoint("d", 1), "")),
Seq(TxOut("o", 40)),
genesisTimestamp
)
noDuplicateTxIn(Seq(tx1, tx2)) shouldEqual true
noDuplicateTxInOf(Seq(tx1, tx2)) shouldEqual true
val tx3: Transaction = Transaction(
Seq(TxIn(Outpoint("e", 0), ""),
TxIn(Outpoint("a", 1), "")),
Seq(TxOut("o", 40)),
genesisTimestamp
)
noDuplicateTxIn(Seq(tx1, tx2, tx3)) shouldEqual true
noDuplicateTxInOf(Seq(tx1, tx2, tx3)) shouldEqual true
val tx4: Transaction = Transaction(
Seq(TxIn(Outpoint("a", 0), ""),
TxIn(Outpoint("f", 1), "")),
Seq(TxOut("o", 40)),
genesisTimestamp
)
noDuplicateTxIn(Seq(tx1, tx2, tx3, tx4)) shouldEqual false
noDuplicateTxInOf(Seq(tx1, tx2, tx3, tx4)) shouldEqual false
}


Expand Down

0 comments on commit 658718f

Please sign in to comment.