Skip to content

Commit

Permalink
ad api todos
Browse files Browse the repository at this point in the history
  • Loading branch information
fluency03 committed Apr 29, 2018
1 parent 7dfeddb commit 0e57819
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ class BlocksActor extends ActorSupport {
val transActor: ActorSelection = context.actorSelection(PARENT_UP + TRANS_ACTOR_NAME)

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

def receive: Receive = {
case GetBlocks => onGetBlocks()
Expand All @@ -46,24 +46,25 @@ class BlocksActor extends ActorSupport {
*
*/

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

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

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

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

private[this] def onDeleteBlock(hash: String): Unit = {
if (blocks.contains(hash)) {
blocks -= hash
if (blocksPool.contains(hash)) {
blocksPool -= hash
sender() ! SuccessMsg(s"Block $hash deleted.")
} else sender() ! FailureMsg(s"Block $hash does not exist.")
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,13 @@ class NetworkActor extends ActorSupport {

// TODO (Chang): need persistence

/**
* TODO (Chang):
* - Remove wallet
* - Sign transaction
*
*/

def receive: Receive = {
case GetNetwork => onGetNetwork()
case GetPeers => onGetPeers()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,13 @@ class PeerActor extends ActorSupport {
val wallets = mutable.Map.empty[String, Wallet]
val others = mutable.Map.empty[String, Peer]

/**
* TODO (Chang):
* - Remove wallet
* - Sign transactions
*
*/

def receive: Receive = {
case GetPublicKeys => sender() ! wallets.values.map(_.address).toSet
case CreateWallet => sender() ! addWallet()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,20 @@ class TransactionsActor extends ActorSupport {
override def postStop(): Unit = log.info("{} stopped!", this.getClass.getSimpleName)

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

val blockchainActor: ActorSelection = context.actorSelection(PARENT_UP + BLOCKCHAIN_ACTOR_NAME)
val blockActor: ActorSelection = context.actorSelection(PARENT_UP + BLOCKS_ACTOR_NAME)
val networkActor: ActorSelection = context.actorSelection(PARENT_UP + NETWORK_ACTOR_NAME)

/**
* TODO (Chang):
* - Update transaction
* - Sign transaction
*
*/

def receive: Receive = {
case GetTransactions => onGetTransactions()
case GetTransactions(ids) => onGetTransactions(ids)
Expand All @@ -38,25 +45,25 @@ class TransactionsActor extends ActorSupport {
case _ => unhandled _
}

private def onGetTransactions(): Unit = sender() ! currentTransactions.values.toSeq
private def onGetTransactions(): Unit = sender() ! transPool.values.toSeq

private def onGetTransactions(ids: Set[String]): Unit = sender() ! currentTransactions.filterKeys(
private def onGetTransactions(ids: Set[String]): Unit = sender() ! transPool.filterKeys(
k => ids.contains(k)
).values.toSeq

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

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

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,12 @@ trait NetworkRoutes extends RoutesSupport {

def networkActor: ActorRef

/**
* TODO (Chang):
* - Sign transaction
*
*/

lazy val networkRoutes: Route =
path(NETWORK) {
get {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,13 @@ trait TransactionRoutes extends RoutesSupport {

def transActor: ActorRef

/**
* TODO (Chang):
* - Update transaction
* - Sign transaction
*
*/

lazy val transRoutes: Route =
path(TRANSACTIONS) {
parameters( 'ids.as(CsvSeq[String]) ? ) { ids =>
Expand Down

0 comments on commit 0e57819

Please sign in to comment.