diff --git a/src/main/scala/com/fluency03/blockchain/core/Blockchain.scala b/src/main/scala/com/fluency03/blockchain/core/Blockchain.scala index 1e1481d..dd0c9b5 100644 --- a/src/main/scala/com/fluency03/blockchain/core/Blockchain.scala +++ b/src/main/scala/com/fluency03/blockchain/core/Blockchain.scala @@ -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] = diff --git a/src/test/scala/com/fluency03/blockchain/core/BlockchainTest.scala b/src/test/scala/com/fluency03/blockchain/core/BlockchainTest.scala index bcd96f0..3eff4e5 100644 --- a/src/test/scala/com/fluency03/blockchain/core/BlockchainTest.scala +++ b/src/test/scala/com/fluency03/blockchain/core/BlockchainTest.scala @@ -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 @@ -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 }