Skip to content

Commit

Permalink
add test for updateUTxOs
Browse files Browse the repository at this point in the history
  • Loading branch information
fluency03 committed Apr 24, 2018
1 parent 93f5c1b commit 3ee0537
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,9 @@ object Blockchain {
): Map[Outpoint, TxOut] = {
val newUnspentTxOuts = getNewUTxOs(transactions)
val consumedTxOuts = getConsumedUTxOs(transactions)
unspentTxOuts.filterKeys(consumedTxOuts contains) ++ newUnspentTxOuts
unspentTxOuts.filterNot {
case (i, _) => consumedTxOuts.contains(i)
} ++ newUnspentTxOuts
}

def getNewUTxOs(transactions: Seq[Transaction]): Map[Outpoint, TxOut] =
Expand Down
21 changes: 17 additions & 4 deletions src/test/scala/com/fluency03/blockchain/core/BlockchainTest.scala
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.fluency03.blockchain
package core

import com.fluency03.blockchain.core.Blockchain.updateUTxOs
import com.fluency03.blockchain.core.Transaction.createCoinbaseTx
import org.json4s.JValue
import org.json4s.native.JsonMethods.parse
Expand Down Expand Up @@ -59,9 +59,22 @@ class BlockchainTest extends FlatSpec with Matchers {
}

"updateUTxOs" should "update the UTXOs from a latest Seq of transactions." in {



val tx: Transaction = Transaction(
Seq(TxIn(Outpoint("def0", 0), "abc1"),
TxIn(Outpoint("def0", 1), "abc1")),
Seq(TxOut("abc4", 40)),
genesisTimestamp
)

val unspentTxOuts1: mutable.Map[Outpoint, TxOut] = mutable.Map.empty[Outpoint, TxOut]
unspentTxOuts1 += (Outpoint("def0", 0) -> TxOut("abc4", 20))
unspentTxOuts1 += (Outpoint("def0", 1) -> TxOut("abc4", 20))

val unspentTxOuts2: mutable.Map[Outpoint, TxOut] = mutable.Map.empty[Outpoint, TxOut]
updateUTxOs(Seq(tx), unspentTxOuts1.toMap) should not equal unspentTxOuts2

unspentTxOuts2 += (Outpoint(tx.id, 0) -> TxOut("abc4", 40))
updateUTxOs(Seq(tx), unspentTxOuts1.toMap) shouldEqual unspentTxOuts2
}


Expand Down

0 comments on commit 3ee0537

Please sign in to comment.