Skip to content

Commit

Permalink
rename Wallet to SingleWallet
Browse files Browse the repository at this point in the history
  • Loading branch information
fluency03 committed May 3, 2018
1 parent a5cb94a commit 9b71195
Show file tree
Hide file tree
Showing 6 changed files with 16 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.")
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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]

/**
Expand All @@ -41,7 +41,7 @@ class PeerActor extends ActorSupport {
}

private def addWallet(): String = {
val newWallet = Wallet()
val newWallet = SingleWallet()
wallets += (newWallet.address -> newWallet)
newWallet.address
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand All @@ -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 =
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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]
Expand Down

0 comments on commit 9b71195

Please sign in to comment.