From 9b71195f4c6965c5e16601b23f66c02f50fd7cf5 Mon Sep 17 00:00:00 2001 From: fluency03 Date: Fri, 4 May 2018 00:29:07 +0200 Subject: [PATCH] rename Wallet to SingleWallet --- .../fluency03/blockchain/api/actors/BlockchainActor.scala | 8 +++----- .../com/fluency03/blockchain/api/actors/BlocksActor.scala | 3 +-- .../com/fluency03/blockchain/api/actors/PeerActor.scala | 6 +++--- .../blockchain/api/actors/TransactionsActor.scala | 3 +-- .../blockchain/core/{Wallet.scala => SingleWallet.scala} | 8 ++++---- .../core/{WalletTest.scala => SingleWalletTest.scala} | 8 ++++---- 6 files changed, 16 insertions(+), 20 deletions(-) rename src/main/scala/com/fluency03/blockchain/core/{Wallet.scala => SingleWallet.scala} (77%) rename src/test/scala/com/fluency03/blockchain/core/{WalletTest.scala => SingleWalletTest.scala} (86%) diff --git a/src/main/scala/com/fluency03/blockchain/api/actors/BlockchainActor.scala b/src/main/scala/com/fluency03/blockchain/api/actors/BlockchainActor.scala index cd3cf6f..be15392 100644 --- a/src/main/scala/com/fluency03/blockchain/api/actors/BlockchainActor.scala +++ b/src/main/scala/com/fluency03/blockchain/api/actors/BlockchainActor.scala @@ -83,11 +83,9 @@ class BlockchainActor extends ActorSupport { private def onGetBlockFromChain(hash: String): Unit = sender() ! getBlockFromChain(hash) - private def onGetTxOfBlock(id: String, hash: String): Unit = sender() ! { - getBlockFromChain(hash) match { - case Some(block) => block.transactions.find(_.id == id) - case None => None - } + private def onGetTxOfBlock(id: String, hash: String): Unit = getBlockFromChain(hash) match { + case Some(block) => sender() ! block.transactions.find(_.id == id) + case None => sender() ! None } private def onAddBlock(block: Block): Unit = blockchainOpt match { diff --git a/src/main/scala/com/fluency03/blockchain/api/actors/BlocksActor.scala b/src/main/scala/com/fluency03/blockchain/api/actors/BlocksActor.scala index 354b392..445acb4 100644 --- a/src/main/scala/com/fluency03/blockchain/api/actors/BlocksActor.scala +++ b/src/main/scala/com/fluency03/blockchain/api/actors/BlocksActor.scala @@ -63,11 +63,10 @@ class BlocksActor extends ActorSupport { private[this] def onGetBlock(hash: String): Unit = sender() ! blocksPool.get(hash) - private[this] def onDeleteBlock(hash: String): Unit = { + private[this] def onDeleteBlock(hash: String): Unit = if (blocksPool.contains(hash)) { blocksPool -= hash sender() ! SuccessMsg(s"Block $hash deleted.") } else sender() ! FailureMsg(s"Block $hash does not exist.") - } } diff --git a/src/main/scala/com/fluency03/blockchain/api/actors/PeerActor.scala b/src/main/scala/com/fluency03/blockchain/api/actors/PeerActor.scala index 92b1d2d..4a42225 100644 --- a/src/main/scala/com/fluency03/blockchain/api/actors/PeerActor.scala +++ b/src/main/scala/com/fluency03/blockchain/api/actors/PeerActor.scala @@ -5,7 +5,7 @@ import java.security.KeyPair import akka.actor.Props import com.fluency03.blockchain.api.actors.PeerActor._ -import com.fluency03.blockchain.core.{Peer, Wallet} +import com.fluency03.blockchain.core.{Peer, SingleWallet} import scala.collection.mutable @@ -24,7 +24,7 @@ class PeerActor extends ActorSupport { override def postStop(): Unit = log.info("{} stopped!", this.getClass.getSimpleName) // TODO (Chang): need persistence - val wallets = mutable.Map.empty[String, Wallet] + val wallets = mutable.Map.empty[String, SingleWallet] val others = mutable.Map.empty[String, Peer] /** @@ -41,7 +41,7 @@ class PeerActor extends ActorSupport { } private def addWallet(): String = { - val newWallet = Wallet() + val newWallet = SingleWallet() wallets += (newWallet.address -> newWallet) newWallet.address } diff --git a/src/main/scala/com/fluency03/blockchain/api/actors/TransactionsActor.scala b/src/main/scala/com/fluency03/blockchain/api/actors/TransactionsActor.scala index 17d34f7..e85e139 100644 --- a/src/main/scala/com/fluency03/blockchain/api/actors/TransactionsActor.scala +++ b/src/main/scala/com/fluency03/blockchain/api/actors/TransactionsActor.scala @@ -52,13 +52,12 @@ class TransactionsActor extends ActorSupport { k => ids.contains(k) ).values.toSeq - private def onCreateTransaction(tx: Transaction): Unit = { + private def onCreateTransaction(tx: Transaction): Unit = if (transPool.contains(tx.id)) sender() ! FailureMsg(s"Transaction ${tx.id} already exists.") else { transPool += (tx.id -> tx) sender() ! SuccessMsg(s"Transaction ${tx.id} created.") } - } private def onGetTransaction(id: String): Unit = sender() ! transPool.get(id) diff --git a/src/main/scala/com/fluency03/blockchain/core/Wallet.scala b/src/main/scala/com/fluency03/blockchain/core/SingleWallet.scala similarity index 77% rename from src/main/scala/com/fluency03/blockchain/core/Wallet.scala rename to src/main/scala/com/fluency03/blockchain/core/SingleWallet.scala index 65173a8..f881baa 100644 --- a/src/main/scala/com/fluency03/blockchain/core/Wallet.scala +++ b/src/main/scala/com/fluency03/blockchain/core/SingleWallet.scala @@ -3,12 +3,12 @@ package core import java.security.KeyPair -import com.fluency03.blockchain.core.Wallet.balanceOfWallet +import com.fluency03.blockchain.core.SingleWallet.balanceOfWallet import com.fluency03.blockchain.core.Transaction.signTxIn import scala.collection.mutable -case class Wallet() { +case class SingleWallet() { private[this] val keyPair: KeyPair = Crypto.generateKeyPair() @@ -21,9 +21,9 @@ case class Wallet() { } -object Wallet { +object SingleWallet { - def balanceOfWallet(wallet: Wallet, uTxOs: mutable.Map[Outpoint, TxOut]): Long = + def balanceOfWallet(wallet: SingleWallet, uTxOs: mutable.Map[Outpoint, TxOut]): Long = balanceOfAddress(wallet.address, uTxOs) def balanceOfAddress(address: String, uTxOs: mutable.Map[Outpoint, TxOut]): Long = diff --git a/src/test/scala/com/fluency03/blockchain/core/WalletTest.scala b/src/test/scala/com/fluency03/blockchain/core/SingleWalletTest.scala similarity index 86% rename from src/test/scala/com/fluency03/blockchain/core/WalletTest.scala rename to src/test/scala/com/fluency03/blockchain/core/SingleWalletTest.scala index ba25c80..b776bb7 100644 --- a/src/test/scala/com/fluency03/blockchain/core/WalletTest.scala +++ b/src/test/scala/com/fluency03/blockchain/core/SingleWalletTest.scala @@ -1,16 +1,16 @@ package com.fluency03.blockchain package core -import com.fluency03.blockchain.core.Wallet._ +import com.fluency03.blockchain.core.SingleWallet._ import org.scalatest.{FlatSpec, Matchers} import scala.collection.mutable -class WalletTest extends FlatSpec with Matchers { +class SingleWalletTest extends FlatSpec with Matchers { "balanceOfWallet" should "obtain the balance of a Wallet based on UTXOs." in { - val wallet = Wallet() + val wallet = SingleWallet() val uTxOs: mutable.Map[Outpoint, TxOut] = mutable.Map.empty[Outpoint, TxOut] balanceOfWallet(wallet, uTxOs) shouldEqual 0 @@ -24,7 +24,7 @@ class WalletTest extends FlatSpec with Matchers { } "Wallet" should "be able to sign a TxIn." in { - val wallet = Wallet() + val wallet = SingleWallet() val id = "".toSha256 val txIn = TxIn(Outpoint("def0", 0), "abc") val uTxOs: mutable.Map[Outpoint, TxOut] = mutable.Map.empty[Outpoint, TxOut]