Skip to content

Commit

Permalink
change type
Browse files Browse the repository at this point in the history
  • Loading branch information
fluency03 committed May 12, 2018
1 parent 71cbe9f commit 59f06b5
Show file tree
Hide file tree
Showing 20 changed files with 137 additions and 127 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
package com.github.fluency03.blockchain.api.actors
package com.github.fluency03.blockchain
package api.actors

import akka.actor.{ActorRef, ActorSelection, Props}
import akka.pattern.ask
Expand All @@ -11,12 +12,12 @@ import scala.util.{Failure, Success}

object BlockPoolActor {
final case object GetBlocks
final case class GetBlocks(hashes: Set[String])
final case class GetBlocks(hashes: Set[HexString])
final case class AddBlock(block: Block)
final case class GetBlock(hash: String)
final case class DeleteBlock(hash: String)
final case class GetTxOfBlock(id: String, hash: String)
final case class MineAndAddNextBlock(data: String, ids: Seq[String])
final case class GetBlock(hash: HexString)
final case class DeleteBlock(hash: HexString)
final case class GetTxOfBlock(id: HexString, hash: HexString)
final case class MineAndAddNextBlock(data: String, ids: Seq[HexString])

def props: Props = Props[BlockPoolActor]
}
Expand All @@ -32,7 +33,7 @@ class BlockPoolActor extends ActorSupport {
val txPoolActor: ActorSelection = context.actorSelection(PARENT_UP + TX_POOL_ACTOR_NAME)

// TODO (Chang): need persistence
var blocksPool: mutable.Map[String, Block] = mutable.Map.empty[String, Block]
var blocksPool: mutable.Map[HexString, Block] = mutable.Map.empty[HexString, Block]

def receive: Receive = {
case GetBlocks => onGetBlocks()
Expand All @@ -50,7 +51,7 @@ class BlockPoolActor extends ActorSupport {
*/
private[this] def onGetBlocks(): Unit = sender() ! blocksPool.values.toSeq

private[this] def onGetBlocks(hashes: Set[String]): Unit =
private[this] def onGetBlocks(hashes: Set[HexString]): Unit =
sender() ! blocksPool.filterKeys(hashes.contains).values.toSeq

private[this] def onAddBlock(block: Block): Unit = {
Expand All @@ -62,20 +63,20 @@ class BlockPoolActor extends ActorSupport {
}
}

private[this] def onGetBlock(hash: String): Unit = sender() ! blocksPool.get(hash)
private[this] def onGetBlock(hash: HexString): Unit = sender() ! blocksPool.get(hash)

private[this] def onDeleteBlock(hash: String): Unit =
private[this] def onDeleteBlock(hash: HexString): Unit =
if (blocksPool.contains(hash)) {
blocksPool -= hash
sender() ! SuccessMsg(s"Block $hash deleted from the Pool.")
} else sender() ! FailureMsg(s"Block $hash does not exist in the Pool.")

private def onGetTxOfBlock(id: String, hash: String): Unit = blocksPool.get(hash) match {
private def onGetTxOfBlock(id: HexString, hash: HexString): Unit = blocksPool.get(hash) match {
case Some(block) => sender() ! block.transactions.find(_.id == id)
case None => sender() ! None
}

private def onMineAndAddNextBlock(data: String, ids: Seq[String]): Unit = {
private def onMineAndAddNextBlock(data: String, ids: Seq[HexString]): Unit = {
val theSender: ActorRef = sender()
(blockchainActor ? BlockchainActor.MineNextBlock(data, ids))
.mapTo[Option[Block]]
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
package com.github.fluency03.blockchain.api.actors
package com.github.fluency03.blockchain
package api.actors

import akka.actor.{ActorRef, ActorSelection, Props}
import akka.pattern.ask
Expand All @@ -18,15 +19,15 @@ object BlockchainActor {
final case object DeleteBlockchain extends BlockchainMsg
final case object CheckBlockchainValidity extends BlockchainMsg

final case class GetBlockByHash(hash: String) extends BlockMsg
final case class GetBlocksByHashesAndIndices(hashes: Set[String], indices: Set[Int]) extends BlockMsg
final case class GetBlockByHash(hash: HexString) extends BlockMsg
final case class GetBlocksByHashesAndIndices(hashes: Set[HexString], indices: Set[Int]) extends BlockMsg
final case object GetLastBlock extends BlockMsg
final case class GetTxOfBlock(id: String, hash: String) extends BlockMsg
final case class GetTxOfBlock(id: HexString, hash: HexString) extends BlockMsg
final case class AppendBlock(block: Block) extends BlockMsg
final case class AppendBlockFromPool(hash: String) extends BlockMsg
final case class AppendBlockFromPool(hash: HexString) extends BlockMsg
final case object RemoveLastBlock extends BlockMsg
final case class MineNextBlock(data: String, ids: Seq[String]) extends BlockMsg
final case class GetBlockFromPool(hash: String) extends BlockMsg
final case class MineNextBlock(data: String, ids: Seq[HexString]) extends BlockMsg
final case class GetBlockFromPool(hash: HexString) extends BlockMsg

def props: Props = Props[BlockchainActor]
}
Expand All @@ -43,7 +44,7 @@ class BlockchainActor extends ActorSupport {

// TODO (Chang): need persistence
var blockchainOpt: Option[Blockchain] = None
val hashIndexMapping: mutable.Map[String, Int] = mutable.Map.empty[String, Int]
val hashIndexMapping: mutable.Map[HexString, Int] = mutable.Map.empty[HexString, Int]

def receive: Receive = {
case msg: BlockchainMsg => inCaseOfBlockchainMsg(msg)
Expand Down Expand Up @@ -105,9 +106,9 @@ class BlockchainActor extends ActorSupport {
/**
* Handlers for each of the BlockMsg.
*/
private def onGetBlockByHash(hash: String): Unit = sender() ! getBlockByHash(hash)
private def onGetBlockByHash(hash: HexString): Unit = sender() ! getBlockByHash(hash)

private def onGetBlocksByHashesAndIndices(hashes: Set[String], indices: Set[Int]): Unit =
private def onGetBlocksByHashesAndIndices(hashes: Set[HexString], indices: Set[Int]): Unit =
sender() ! getBlocksByHashesAndIndices(hashes, indices)

private def onGetLastBlock(): Unit = blockchainOpt match {
Expand All @@ -117,7 +118,7 @@ class BlockchainActor extends ActorSupport {
sender() ! None
}

private def onGetTxOfBlock(id: String, hash: String): Unit = getBlockByHash(hash) match {
private def onGetTxOfBlock(id: HexString, hash: HexString): Unit = getBlockByHash(hash) match {
case Some(block) => sender() ! block.transactions.find(_.id == id)
case None => sender() ! None
}
Expand All @@ -131,7 +132,7 @@ class BlockchainActor extends ActorSupport {
sender() ! FailureMsg("Blockchain does not exist.")
}

private def onAppendBlockFromPool(hash: String): Unit = blockchainOpt match {
private def onAppendBlockFromPool(hash: HexString): Unit = blockchainOpt match {
case Some(blockchain) =>
val theSender: ActorRef = sender()
(blockPoolActor ? BlockPoolActor.GetBlock(hash))
Expand Down Expand Up @@ -162,7 +163,7 @@ class BlockchainActor extends ActorSupport {
sender() ! FailureMsg("Blockchain does not exist.")
}

private def onMineNextBlock(data: String, ids: Seq[String]): Unit = blockchainOpt match {
private def onMineNextBlock(data: String, ids: Seq[HexString]): Unit = blockchainOpt match {
case Some(blockchain) =>
if (ids.isEmpty) sender() ! Some(blockchain.mineNextBlock(data, Seq.empty[Transaction]))
else {
Expand All @@ -179,13 +180,13 @@ class BlockchainActor extends ActorSupport {
sender() ! None
}

private def onGetBlockFromPool(hash: String): Unit =
private def onGetBlockFromPool(hash: HexString): Unit =
blockPoolActor forward BlockPoolActor.GetBlock(hash)

/**
* Private helper methods.
*/
private def getBlockByHash(hash: String): Option[Block] = hashIndexMapping.get(hash) match {
private def getBlockByHash(hash: HexString): Option[Block] = hashIndexMapping.get(hash) match {
case Some(index) => blockchainOpt match {
case Some(blockchain) => Some(blockchain.chain(index))
case None =>
Expand All @@ -195,7 +196,7 @@ class BlockchainActor extends ActorSupport {
case None => None
}

private def getBlocksByHashesAndIndices(hashes: Set[String], indices: Set[Int]): Set[Block] = {
private def getBlocksByHashesAndIndices(hashes: Set[HexString], indices: Set[Int]): Set[Block] = {
???
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
package com.github.fluency03.blockchain.api.actors
package com.github.fluency03.blockchain
package api.actors

import akka.actor.{ActorSelection, Props}
import akka.pattern.{ask, pipe}
Expand Down Expand Up @@ -56,15 +57,15 @@ class NetworkActor extends ActorSupport {

private def onGetPeers(): Unit = {
val peers = context.children.map { p =>
(p ? GetPublicKeys).mapTo[Set[String]].map(p.path.name -> _)
(p ? GetPublicKeys).mapTo[Set[HexString]].map(p.path.name -> _)
}
Future.sequence(peers).map(_.toMap).pipeTo(sender())
}

private def onGetPeers(names: Set[String]): Unit = {
val peers = context.children
.filter { p => names.contains(p.path.name) }
.map { p => (p ? GetPublicKeys).mapTo[Set[String]].map(p.path.name -> _) }
.map { p => (p ? GetPublicKeys).mapTo[Set[HexString]].map(p.path.name -> _) }
Future.sequence(peers).map(_.toMap).pipeTo(sender())
}

Expand All @@ -77,7 +78,7 @@ class NetworkActor extends ActorSupport {

private def onGetPeer(name: String): Unit = context.child(name) match {
case Some(_) => (context.child(name).get ? GetPublicKeys)
.mapTo[Set[String]]
.mapTo[Set[HexString]]
.map { keys => Some(Peer(name, keys)) }
.pipeTo(sender())
case None => sender() ! None
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ class PeerActor extends ActorSupport {
override def postStop(): Unit = log.info("{} stopped!", this.getClass.getSimpleName)

// TODO (Chang): need persistence
val wallet: mutable.Map[String, KeyContainer] = mutable.Map.empty[String, KeyContainer]
val wallet: mutable.Map[HexString, KeyContainer] = mutable.Map.empty[HexString, KeyContainer]
val others: mutable.Map[String, Peer] = mutable.Map.empty[String, Peer]

/**
Expand All @@ -38,7 +38,7 @@ class PeerActor extends ActorSupport {
case _ => unhandled _
}

private def addWallet(): String = {
private def addWallet(): HexString = {
val kc = KeyContainer()
wallet += (kc.address -> kc)
kc.address
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
package com.github.fluency03.blockchain.api.actors
package com.github.fluency03.blockchain
package api.actors

import akka.actor.{ActorSelection, Props}
import com.github.fluency03.blockchain.api.actors.TxPoolActor._
Expand All @@ -10,10 +11,10 @@ import scala.collection.mutable

object TxPoolActor {
final case object GetTransactions
final case class GetTransactions(ids: Seq[String])
final case class GetTransactions(ids: Seq[HexString])
final case class AddTransaction(tx: Transaction)
final case class GetTransaction(id: String)
final case class DeleteTransaction(id: String)
final case class GetTransaction(id: HexString)
final case class DeleteTransaction(id: HexString)
final case class UpdateTransaction(tx: Transaction)
def props: Props = Props[TxPoolActor]
}
Expand All @@ -23,7 +24,7 @@ class TxPoolActor extends ActorSupport {
override def postStop(): Unit = log.info("{} stopped!", this.getClass.getSimpleName)

// TODO (Chang): need persistence
val transPool: mutable.Map[String, Transaction] = mutable.Map.empty[String, Transaction]
val transPool: mutable.Map[HexString, Transaction] = mutable.Map.empty[HexString, Transaction]
val uTxOs: mutable.Map[Outpoint, TxOut] = mutable.Map.empty[Outpoint, TxOut]

val blockchainActor: ActorSelection = context.actorSelection(PARENT_UP + BLOCKCHAIN_ACTOR_NAME)
Expand Down Expand Up @@ -51,7 +52,7 @@ class TxPoolActor extends ActorSupport {
*/
private def onGetTransactions(): Unit = sender() ! transPool.values.toSeq

private def onGetTransactions(ids: Seq[String]): Unit =
private def onGetTransactions(ids: Seq[HexString]): Unit =
sender() ! ids.map(transPool.get).filter(_.isDefined).map(_.get)

private def onAddTransaction(tx: Transaction): Unit =
Expand All @@ -62,9 +63,9 @@ class TxPoolActor extends ActorSupport {
sender() ! SuccessMsg(s"Transaction ${tx.id} created in the Pool.")
}

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

private def onDeleteTransaction(id: String): Unit =
private def onDeleteTransaction(id: HexString): Unit =
if (transPool.contains(id)) {
transPool -= id
sender() ! SuccessMsg(s"Transaction $id deleted from the Pool.")
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
package com.github.fluency03.blockchain.api.routes
package com.github.fluency03.blockchain
package api.routes

import akka.actor.ActorRef
import akka.event.Logging
Expand All @@ -22,7 +23,7 @@ trait BlockPoolRoutes extends RoutesSupport {
lazy val blockPoolRoutes: Route =
pathPrefix(BLOCK_POOL) {
path(BLOCKS) {
parameters('hashes.as(CsvSeq[String]).?) { hashesOpt: Option[Seq[String]] =>
parameters('hashes.as(CsvSeq[HexString]).?) { hashesOpt: Option[Seq[HexString]] =>
val blocks: Future[Blocks] = hashesOpt match {
case Some(hashes) => (blockPoolActor ? GetBlocks(hashes.toSet)).mapTo[Blocks]
case None => (blockPoolActor ? GetBlocks).mapTo[Blocks]
Expand All @@ -32,9 +33,9 @@ trait BlockPoolRoutes extends RoutesSupport {
} ~
path(NEXT_BLOCK) {
post {
parameters('ids.as(CsvSeq[String]).?) { idsOpt: Option[Seq[String]] =>
parameters('ids.as(CsvSeq[HexString]).?) { idsOpt: Option[Seq[HexString]] =>
entity(as[Input]) { in =>
val action = MineAndAddNextBlock(in.content, idsOpt.getOrElse(Seq.empty[String]))
val action = MineAndAddNextBlock(in.content, idsOpt.getOrElse(Seq.empty[HexString]))
val maybeNextBlock: Future[Option[Block]] =
(blockPoolActor ? action).mapTo[Option[Block]]
rejectEmptyResponse {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
package com.github.fluency03.blockchain.api.routes
package com.github.fluency03.blockchain
package api.routes

import akka.actor.ActorRef
import akka.event.Logging
Expand Down Expand Up @@ -55,10 +56,10 @@ trait BlockchainRoutes extends RoutesSupport {
} ~
path(NEXT_BLOCK) {
post {
parameters('id.as(CsvSeq[String]).?) { idsOpt: Option[Seq[String]] =>
parameters('id.as(CsvSeq[HexString]).?) { idsOpt: Option[Seq[HexString]] =>
entity(as[Input]) { in =>
val maybeNextBlock: Future[Option[Block]] =
(blockchainActor ? MineNextBlock(in.content, idsOpt.getOrElse(Seq.empty[String])))
(blockchainActor ? MineNextBlock(in.content, idsOpt.getOrElse(Seq.empty[HexString])))
.mapTo[Option[Block]]
rejectEmptyResponse {
complete(maybeNextBlock)
Expand Down Expand Up @@ -86,9 +87,9 @@ trait BlockchainRoutes extends RoutesSupport {
} ~
path(BLOCKS) {
get {
parameters('hashes.as(CsvSeq[String]).?, 'indices.as(CsvSeq[Int]).?) {
(hashesOpt: Option[Seq[String]], indicesOpt: Option[Seq[Int]]) =>
val (hashes, indices) = (hashesOpt.getOrElse(Seq.empty[String]).toSet,
parameters('hashes.as(CsvSeq[HexString]).?, 'indices.as(CsvSeq[Int]).?) {
(hashesOpt: Option[Seq[HexString]], indicesOpt: Option[Seq[Int]]) =>
val (hashes, indices) = (hashesOpt.getOrElse(Seq.empty[HexString]).toSet,
indicesOpt.getOrElse(Seq.empty[Int]).toSet)
val blocks: Future[Set[Block]] =
(blockchainActor ? GetBlocksByHashesAndIndices(hashes, indices)).mapTo[Set[Block]]
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
package com.github.fluency03.blockchain.api.routes
package com.github.fluency03.blockchain
package api.routes

import akka.actor.ActorRef
import akka.event.Logging
Expand Down Expand Up @@ -35,10 +36,10 @@ trait NetworkRoutes extends RoutesSupport {
path(PEERS) {
get {
parameters( 'names.as(CsvSeq[String]).? ) { namesOpt =>
val peers: Future[Map[String, Set[String]]] = namesOpt match {
val peers: Future[Map[String, Set[HexString]]] = namesOpt match {
case Some(names) =>
(networkActor ? GetPeers(names.toSet)).mapTo[Map[String, Set[String]]]
case None => (networkActor ? GetPeers).mapTo[Map[String, Set[String]]]
(networkActor ? GetPeers(names.toSet)).mapTo[Map[String, Set[HexString]]]
case None => (networkActor ? GetPeers).mapTo[Map[String, Set[HexString]]]
}
complete(peers)
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
package com.github.fluency03.blockchain.api.routes
package com.github.fluency03.blockchain
package api.routes

import akka.actor.ActorSystem
import akka.http.scaladsl.model.StatusCodes
import akka.http.scaladsl.server.{RequestContext, StandardRoute}
import akka.http.scaladsl.server.StandardRoute
import akka.http.scaladsl.server.directives.RouteDirectives.complete
import akka.util.Timeout
import com.github.fluency03.blockchain.api.{FailureMsg, JsonSupport, Message, SuccessMsg}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
package com.github.fluency03.blockchain.api.routes
package com.github.fluency03.blockchain
package api.routes

import akka.actor.ActorRef
import akka.event.Logging
Expand Down Expand Up @@ -30,7 +31,7 @@ trait TxPoolRoutes extends RoutesSupport {
lazy val txPoolRoutes: Route =
pathPrefix(TX_POOL) {
path(TRANSACTIONS) {
parameters('ids.as(CsvSeq[String]).?) { idsOpt =>
parameters('ids.as(CsvSeq[HexString]).?) { idsOpt =>
val transactions: Future[Transactions] = idsOpt match {
case Some(ids) => (txPoolActor ? GetTransactions(ids)).mapTo[Transactions]
case None => (txPoolActor ? GetTransactions).mapTo[Transactions]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@ case class KeyContainer() {

private[this] val keyPair: KeyPair = Secp256k1.generateKeyPair()

// TODO (Chang): change it to actual address of a PublicKey
lazy val address: String = keyPair.getPublic.toHex
// TODO (Chang): change it to actual address (which is a Base58) of a PublicKey
lazy val address: HexString = keyPair.getPublic.toHex

lazy val publicKeyHex: String = keyPair.getPublic.toHex
lazy val publicKeyHex: HexString = keyPair.getPublic.toHex

def balance(uTxOs: mutable.Map[Outpoint, TxOut]): Long = balanceOfKey(this, uTxOs)

Expand Down
Loading

0 comments on commit 59f06b5

Please sign in to comment.