Skip to content

Commit

Permalink
add tests for AddBlockFromPool & GetBlockFromPool
Browse files Browse the repository at this point in the history
  • Loading branch information
fluency03 committed May 4, 2018
1 parent 7ae47de commit 5859ded
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,10 @@ class BlockPoolActor extends ActorSupport {
).values.toSeq

private[this] def onCreateBlock(block: Block): Unit = {
if (blocksPool.contains(block.hash)) sender() ! FailureMsg(s"Block ${block.hash} already exists.")
if (blocksPool.contains(block.hash)) sender() ! FailureMsg(s"Block ${block.hash} already exists in the Pool.")
else {
blocksPool += (block.hash -> block)
sender() ! SuccessMsg(s"Block ${block.hash} created.")
sender() ! SuccessMsg(s"Block ${block.hash} created in the Pool.")
}
}

Expand All @@ -66,7 +66,7 @@ class BlockPoolActor extends ActorSupport {
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.")
sender() ! SuccessMsg(s"Block $hash deleted from the Pool.")
} else sender() ! FailureMsg(s"Block $hash does not exist in the Pool.")

}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.fluency03.blockchain.api.actors

import akka.actor.{ActorSelection, Props}
import akka.actor.{ActorRef, ActorSelection, Props}
import akka.pattern.ask
import com.fluency03.blockchain.api.actors.BlockchainActor._
import com.fluency03.blockchain.api._
Expand Down Expand Up @@ -102,15 +102,17 @@ class BlockchainActor extends ActorSupport {
private def onAddBlockFromPool(hash: String): Unit = blockchainOpt match {
case Some(blockchain) =>
val maybeBlock: Future[Option[Block]] = (blockPoolActor ? BlockPoolActor.GetBlock(hash)).mapTo[Option[Block]]
val theSender: ActorRef = sender()
maybeBlock onComplete {
case Success(blockOpt) => blockOpt match {
case Some(block) =>
log.error(theSender.toString())
blockchainOpt = Some(blockchain.addBlock(block))
hashIndexMapping += (block.hash -> blockchain.length)
sender() ! SuccessMsg(s"New Block ${block.hash} added on the chain.")
case None => sender() ! FailureMsg(s"Does not find Block $hash in the poll.")
theSender ! SuccessMsg(s"New Block ${block.hash} added on the chain.")
case None => theSender ! FailureMsg(s"Did not find Block $hash in the poll.")
}
case Failure(t) => sender() ! FailureMsg(s"Cannot get Block $hash from the poll.")
case Failure(e) => theSender ! FailureMsg(s"Cannot get Block $hash from the poll: ${e.getMessage}")
}
case None =>
log.error("Blockchain does not exist! Clear the hash-to-index mapping!")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,19 +53,19 @@ class TxPoolActor extends ActorSupport {
).values.toSeq

private def onCreateTransaction(tx: Transaction): Unit =
if (transPool.contains(tx.id)) sender() ! FailureMsg(s"Transaction ${tx.id} already exists.")
if (transPool.contains(tx.id)) sender() ! FailureMsg(s"Transaction ${tx.id} already exists in the Pool.")
else {
transPool += (tx.id -> tx)
sender() ! SuccessMsg(s"Transaction ${tx.id} created.")
sender() ! SuccessMsg(s"Transaction ${tx.id} created in the Pool.")
}

private def onGetTransaction(id: String): Unit = sender() ! transPool.get(id)

private def onDeleteTransaction(id: String): Unit =
if (transPool contains id) {
transPool -= id
sender() ! SuccessMsg(s"Transaction $id deleted.")
} else sender() ! FailureMsg(s"Transaction $id does not exist.")
sender() ! SuccessMsg(s"Transaction $id deleted from the Pool.")
} else sender() ! FailureMsg(s"Transaction $id does not exist in the Pool.")

private def onUpdateTransaction(tx: Transaction): Unit = {
val actualId = tx.id
Expand All @@ -74,8 +74,8 @@ class TxPoolActor extends ActorSupport {
val notExistBefore = !transPool.contains(actualId)
transPool += (actualId -> tx)
sender() ! {
if (notExistBefore) SuccessMsg(s"Transaction $actualId does not exist. New transaction created.")
else SuccessMsg(s"Transaction $actualId updated.")
if (notExistBefore) SuccessMsg(s"Transaction $actualId does not exist. New transaction created in the Pool.")
else SuccessMsg(s"Transaction $actualId updated in the Pool.")
}
} else sender() ! FailureMsg(s"Transaction does not have valid ID. Should be: $expectedId; actually is: $actualId")
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,10 @@ class BlockPoolActorTest extends TestKit(ActorSystem("BlocksActorTest")) with Im
expectMsg(Seq.empty[Block])

blockPoolActor ! CreateBlock(Block.genesisBlock)
expectMsg(SuccessMsg(s"Block ${Block.genesisBlock.hash} created."))
expectMsg(SuccessMsg(s"Block ${Block.genesisBlock.hash} created in the Pool."))

blockPoolActor ! CreateBlock(Block.genesisBlock)
expectMsg(FailureMsg(s"Block ${Block.genesisBlock.hash} already exists."))
expectMsg(FailureMsg(s"Block ${Block.genesisBlock.hash} already exists in the Pool."))

blockPoolActor ! GetBlocks
expectMsg(Seq(Block.genesisBlock))
Expand All @@ -45,10 +45,10 @@ class BlockPoolActorTest extends TestKit(ActorSystem("BlocksActorTest")) with Im
expectMsg(Some(Block.genesisBlock))

blockPoolActor ! DeleteBlock(Block.genesisBlock.hash)
expectMsg(SuccessMsg(s"Block ${Block.genesisBlock.hash} deleted."))
expectMsg(SuccessMsg(s"Block ${Block.genesisBlock.hash} deleted from the Pool."))

blockPoolActor ! DeleteBlock(Block.genesisBlock.hash)
expectMsg(FailureMsg(s"Block ${Block.genesisBlock.hash} does not exist."))
expectMsg(FailureMsg(s"Block ${Block.genesisBlock.hash} does not exist in the Pool."))

blockPoolActor ! GetBlock(Block.genesisBlock.hash)
expectMsg(None)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
package com.fluency03.blockchain.api.actors

import akka.actor.{ActorRef, ActorSystem, Props}
import akka.actor.{ActorRef, ActorSelection, ActorSystem, Props}
import akka.testkit.{DefaultTimeout, ImplicitSender, TestKit}
import com.fluency03.blockchain.api.actors.BlockPoolActor.CreateBlock
import com.fluency03.blockchain.api.actors.BlockchainActor._
import com.fluency03.blockchain.api.{FailureMsg, SuccessMsg}
import com.fluency03.blockchain.api.{BLOCKCHAIN_ACTOR_NAME, BLOCK_POOL_ACTOR_NAME, FailureMsg, SuccessMsg}
import com.fluency03.blockchain.core.{Block, Blockchain, Transaction}
import org.scalatest.{BeforeAndAfterAll, Matchers, WordSpecLike}

Expand All @@ -16,7 +17,8 @@ class BlockchainActorTest extends TestKit(ActorSystem("BlockchainActorTest")) wi
shutdown()
}

val blockchainActor: ActorRef = system.actorOf(Props[BlockchainActor])
val blockchainActor: ActorRef = system.actorOf(Props[BlockchainActor], BLOCKCHAIN_ACTOR_NAME)
val blockPoolActor: ActorRef = system.actorOf(Props[BlockPoolActor], BLOCK_POOL_ACTOR_NAME)

"A BlockchainActor" should {
"Respond with a Blockchain." in {
Expand Down Expand Up @@ -67,6 +69,28 @@ class BlockchainActorTest extends TestKit(ActorSystem("BlockchainActorTest")) wi
blockchainActor ! CheckBlockchainValidity
expectMsg(SuccessMsg("true"))

blockPoolActor ! CreateBlock(genesis)
expectMsg(SuccessMsg(s"Block ${genesis.hash} created in the Pool."))

within(15 seconds) {
blockchainActor ! GetBlockFromPool(genesis.hash)
expectMsg(Some(genesis))

blockchainActor ! GetBlockFromPool("someid")
expectMsg(None)
}

within(30 seconds) {
blockchainActor ! AddBlockFromPool(genesis.hash)
expectMsg(SuccessMsg(s"New Block ${genesis.hash} added on the chain."))

blockchainActor ! AddBlockFromPool("someid")
expectMsg(FailureMsg(s"Did not find Block someid in the poll."))
}

blockchainActor ! RemoveBlock
expectMsg(SuccessMsg(s"Last Block ${genesis.hash} removed from the chain."))

within(15 seconds) {
blockchainActor ! MineNextBlock("next", Seq.empty[String])
val actualBlock = expectMsgType[Some[Block]].get
Expand All @@ -87,6 +111,9 @@ class BlockchainActorTest extends TestKit(ActorSystem("BlockchainActorTest")) wi
blockchainActor ! AddBlock(genesis)
expectMsg(FailureMsg("Blockchain does not exist."))

blockchainActor ! AddBlockFromPool(genesis.hash)
expectMsg(FailureMsg("Blockchain does not exist."))

blockchainActor ! RemoveBlock
expectMsg(FailureMsg("Blockchain does not exist."))

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,10 @@ class TxPoolActorTest extends TestKit(ActorSystem("TransactionsActorTest")) with

val genesisTx: Transaction = createCoinbaseTx(0, genesisMiner, genesisTimestamp)
txPoolActor ! CreateTransaction(genesisTx)
expectMsg(SuccessMsg(s"Transaction ${genesisTx.id} created."))
expectMsg(SuccessMsg(s"Transaction ${genesisTx.id} created in the Pool."))

txPoolActor ! CreateTransaction(genesisTx)
expectMsg(FailureMsg(s"Transaction ${genesisTx.id} already exists."))
expectMsg(FailureMsg(s"Transaction ${genesisTx.id} already exists in the Pool."))

txPoolActor ! GetTransactions
expectMsg(Seq(genesisTx))
Expand All @@ -48,22 +48,22 @@ class TxPoolActorTest extends TestKit(ActorSystem("TransactionsActorTest")) with
expectMsg(Some(genesisTx))

txPoolActor ! UpdateTransaction(genesisTx)
expectMsg(SuccessMsg(s"Transaction ${genesisTx.id} updated."))
expectMsg(SuccessMsg(s"Transaction ${genesisTx.id} updated in the Pool."))

val tx1: Transaction = createCoinbaseTx(1, genesisMiner, genesisTimestamp + 10)
txPoolActor ! UpdateTransaction(tx1)
expectMsg(SuccessMsg(s"Transaction ${tx1.id} does not exist. New transaction created."))
expectMsg(SuccessMsg(s"Transaction ${tx1.id} does not exist. New transaction created in the Pool."))

val tx0: Transaction = Transaction(Seq.empty[TxIn], Seq.empty[TxOut], genesisTimestamp, "0000")
val idOfTx0 = hashOfTransaction(tx0)
txPoolActor ! UpdateTransaction(tx0)
expectMsg(FailureMsg(s"Transaction does not have valid ID. Should be: $idOfTx0; actually is: ${tx0.id}"))

txPoolActor ! DeleteTransaction(genesisTx.id)
expectMsg(SuccessMsg(s"Transaction ${genesisTx.id} deleted."))
expectMsg(SuccessMsg(s"Transaction ${genesisTx.id} deleted from the Pool."))

txPoolActor ! DeleteTransaction(genesisTx.id)
expectMsg(FailureMsg(s"Transaction ${genesisTx.id} does not exist."))
expectMsg(FailureMsg(s"Transaction ${genesisTx.id} does not exist in the Pool."))

txPoolActor ! GetTransaction(genesisTx.id)
expectMsg(None)
Expand Down

0 comments on commit 5859ded

Please sign in to comment.