Skip to content

Commit

Permalink
Merge branch 'develop'
Browse files Browse the repository at this point in the history
  • Loading branch information
fluency03 committed Apr 26, 2018
2 parents e1fdbe6 + b2b1a5f commit aa36d2b
Show file tree
Hide file tree
Showing 23 changed files with 227 additions and 123 deletions.
63 changes: 0 additions & 63 deletions src/main/scala/com/fluency03/blockchain/Util.scala

This file was deleted.

4 changes: 2 additions & 2 deletions src/main/scala/com/fluency03/blockchain/api/Message.scala
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@ package com.fluency03.blockchain.api
final case class Input(content: String)

sealed trait Message
final case class SuccessMsg(content: String) extends Message
final case class FailureMsg(content: String) extends Message
final case class SuccessMsg(message: String) extends Message
final case class FailureMsg(error: String) extends Message
2 changes: 2 additions & 0 deletions src/main/scala/com/fluency03/blockchain/api/package.scala
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.fluency03.blockchain

import akka.http.scaladsl.model.{StatusCode, StatusCodes}
import com.fluency03.blockchain.core.{Block, Peer, Transaction}

package object api {
Expand All @@ -16,4 +17,5 @@ package object api {

val PARENT_UP = "../"


}
Original file line number Diff line number Diff line change
Expand Up @@ -21,20 +21,20 @@ trait BlockRoutes extends RoutesSupport {
def blocksActor: ActorRef

lazy val blockRoutes: Route =
path("blocks") {
path(BLOCKS) {
get {
val blocks: Future[Blocks] = (blocksActor ? GetBlocks).mapTo[Blocks]
complete(blocks)
}
} ~
pathPrefix("block") {
pathPrefix(BLOCK) {
pathEnd {
post {
entity(as[Block]) { block =>
val blockCreated: Future[Message] = (blocksActor ? CreateBlock(block)).mapTo[Message]
onSuccess(blockCreated) {
case SuccessMsg(content) => complete((StatusCodes.Created, content))
case FailureMsg(content) => complete((StatusCodes.Conflict, content))
case s: SuccessMsg => complete((StatusCodes.Created, s))
case f: FailureMsg => complete((StatusCodes.Conflict, f))
}
}
}
Expand All @@ -47,8 +47,8 @@ trait BlockRoutes extends RoutesSupport {
delete {
val blockDeleted: Future[Message] = (blocksActor ? DeleteBlock(hash)).mapTo[Message]
onSuccess(blockDeleted) {
case SuccessMsg(content) => complete((StatusCodes.OK, content))
case FailureMsg(content) => complete((StatusCodes.NotFound, content))
case s: SuccessMsg => complete((StatusCodes.OK, s))
case f: FailureMsg => complete((StatusCodes.NotFound, f))
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ trait BlockchainRoutes extends RoutesSupport {
def blockchainActor: ActorRef

lazy val blockchainRoutes: Route =
pathPrefix("blockchain") {
pathPrefix(BLOCKCHAIN) {
pathEnd {
get {
val blockchain: Future[Option[Blockchain]] = (blockchainActor ? GetBlockchain).mapTo[Option[Blockchain]]
Expand All @@ -31,16 +31,16 @@ trait BlockchainRoutes extends RoutesSupport {
entity(as[JValue]) { _ =>
val blockchainCreated: Future[Message] = (blockchainActor ? CreateBlockchain).mapTo[Message]
onSuccess(blockchainCreated) {
case SuccessMsg(content) => complete((StatusCodes.Created, content))
case FailureMsg(content) => complete((StatusCodes.Conflict, content))
case s: SuccessMsg => complete((StatusCodes.Created, s))
case f: FailureMsg => complete((StatusCodes.Conflict, f))
}
}
} ~
delete {
val blockchainDeleted: Future[Message] = (blockchainActor ? DeleteBlockchain).mapTo[Message]
onSuccess(blockchainDeleted) {
case SuccessMsg(content) => complete((StatusCodes.OK, content))
case FailureMsg(content) => complete((StatusCodes.NotFound, content))
case s: SuccessMsg => complete((StatusCodes.OK, s))
case f: FailureMsg => complete((StatusCodes.NotFound, f))
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,49 +4,45 @@ package api.routes
import java.time.Instant

import akka.event.Logging
import akka.http.scaladsl.model.StatusCodes
import akka.http.scaladsl.server.Directives._
import akka.http.scaladsl.server.Route
import akka.http.scaladsl.server.directives.MethodDirectives.post
import akka.http.scaladsl.server.directives.RouteDirectives.complete
import com.fluency03.blockchain.Util._
import com.fluency03.blockchain.api.Input
import com.fluency03.blockchain.api.{Input, SuccessMsg}

trait GenericRoutes extends RoutesSupport {
lazy val log = Logging(system, classOf[GenericRoutes])



lazy val genericRoutes: Route =
pathSingleSlash {
get {
complete("Welcome to Blockchain in Scala!")
complete(SuccessMsg(SLOGAN))
}
} ~
pathPrefix("generic") {
path("toSha256") {
pathPrefix(GENERIC) {
path(TO_SHA256) {
post {
entity(as[Input]) { in => complete((StatusCodes.Created, in.content.toSha256)) }
entity(as[Input]) { in => complete( failsafeResp { in.content.toSha256 } ) }
}
} ~
path("toBase64") {
path(TO_BASE64) {
post {
entity(as[Input]) { in => complete((StatusCodes.Created, in.content.toBase64)) }
entity(as[Input]) { in => complete( failsafeResp { in.content.toBase64 } ) }
}
} ~
path("fromBase64") {
path(FROM_BASE64) {
post {
entity(as[Input]) { in => complete((StatusCodes.Created, fromBase64(in.content))) }
entity(as[Input]) { in => complete( failsafeResp { fromBase64(in.content) } ) }
}
} ~
path("epoch-time") {
path(TO_EPOCH_TIME) {
post {
entity(as[Input]) { in => complete((StatusCodes.Created, epochTimeOf(in.content))) }
entity(as[Input]) { in => complete( failsafeResp { epochTimeOf(in.content).toString } ) }
}
} ~
path("time-of-epoch") {
path(TIME_FROM_EPOCH) {
post {
entity(as[Input]) { in => complete((StatusCodes.Created, Instant.ofEpochSecond(in.content.toLong))) }
entity(as[Input]) { in => complete( failsafeResp { Instant.ofEpochSecond(in.content.toLong).toString } ) }
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,20 +21,20 @@ trait NetworkRoutes extends RoutesSupport {
def networkActor: ActorRef

lazy val networkRoutes: Route =
path("peers") {
path(PEERS) {
get {
val peers: Future[Set[String]] = (networkActor ? GetPeers).mapTo[Set[String]]
complete(peers)
}
} ~
pathPrefix("peer") {
pathPrefix(PEER) {
pathEnd {
post {
entity(as[PeerSimple]) { peer =>
val peerCreated: Future[Message] = (networkActor ? CreatePeer(peer.name)).mapTo[Message]
onSuccess(peerCreated) {
case SuccessMsg(content) => complete((StatusCodes.Created, content))
case FailureMsg(content) => complete((StatusCodes.Conflict, content))
case s: SuccessMsg => complete((StatusCodes.Created, s))
case f: FailureMsg => complete((StatusCodes.Conflict, f))
}
}
}
Expand All @@ -47,8 +47,8 @@ trait NetworkRoutes extends RoutesSupport {
delete {
val peerDeleted: Future[Message] = (networkActor ? DeletePeer(name)).mapTo[Message]
onSuccess(peerDeleted) {
case SuccessMsg(content) => complete((StatusCodes.OK, content))
case FailureMsg(content) => complete((StatusCodes.NotFound, content))
case s: SuccessMsg => complete((StatusCodes.OK, s))
case f: FailureMsg => complete((StatusCodes.NotFound, f))
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,20 +21,20 @@ trait TransactionRoutes extends RoutesSupport {
def transActor: ActorRef

lazy val transRoutes: Route =
path("transactions") {
path(TRANSACTIONS) {
get {
val transactions: Future[Transactions] = (transActor ? GetTransactions).mapTo[Transactions]
complete(transactions)
}
} ~
pathPrefix("transaction") {
pathPrefix(TRANSACTION) {
pathEnd {
post {
entity(as[Transaction]) { tx =>
val txCreated: Future[Message] = (transActor ? CreateTransaction(tx)).mapTo[Message]
onSuccess(txCreated) {
case SuccessMsg(content) => complete((StatusCodes.Created, content))
case FailureMsg(content) => complete((StatusCodes.Conflict, content))
case s: SuccessMsg => complete((StatusCodes.Created, s))
case f: FailureMsg => complete((StatusCodes.Conflict, f))
}
}
}
Expand All @@ -47,8 +47,8 @@ trait TransactionRoutes extends RoutesSupport {
delete {
val txDeleted: Future[Message] = (transActor ? DeleteTransaction(id)).mapTo[Message]
onSuccess(txDeleted) {
case SuccessMsg(content) => complete((StatusCodes.OK, content))
case FailureMsg(content) => complete((StatusCodes.NotFound, content))
case s: SuccessMsg => complete((StatusCodes.OK, s))
case f: FailureMsg => complete((StatusCodes.NotFound, f))
}
}
}
Expand Down
59 changes: 59 additions & 0 deletions src/main/scala/com/fluency03/blockchain/api/routes/package.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package com.fluency03.blockchain.api

import akka.http.scaladsl.model.{StatusCode, StatusCodes}

package object routes {

// generics
val SLASH = "/"
val GENERIC = "generic"
val TO_SHA256 = "to-sha256"
val TO_BASE64 = "to-base64"
val FROM_BASE64 = "from-base64"
val TO_EPOCH_TIME = "to-epoch-time"
val TIME_FROM_EPOCH = "time-from-epoch"

// blockchain
val BLOCKCHAIN = "blockchain"

// block
val BLOCKS = "blocks"
val BLOCK = "block"

// network
val PEERS = "peers"
val PEER = "peer"

// transaction
val TRANSACTIONS = "transactions"
val TRANSACTION = "transaction"

/**
* Concatenate a seq of String segments into full API path
*/
def pathOf(seg: String*): String = SLASH + seg.mkString(SLASH)

/**
* Return either SuccessMsg (if fun successfully returned a String) or FailureMsg (if fun failed).
*/
def failsafeMsg(fun: => String): Message =
try { SuccessMsg(fun) }
catch {
case e: Exception => FailureMsg(e.getMessage)
}

/**
* Return either SuccessMsg (if fun successfully returned a String) or FailureMsg (if fun failed).
*/
def failsafeResp(fun: => String): (StatusCode, Message) =
try { (StatusCodes.OK, SuccessMsg(fun)) }
catch {
case e: Exception => (StatusCodes.InternalServerError, FailureMsg(e.getMessage))
}






}
1 change: 0 additions & 1 deletion src/main/scala/com/fluency03/blockchain/core/Block.scala
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package com.fluency03.blockchain
package core

import com.fluency03.blockchain.Util.isWithValidDifficulty
import com.fluency03.blockchain.core.BlockHeader.hashOfHeaderFields
import com.fluency03.blockchain.core.Transaction.createCoinbaseTx
import org.json4s.JsonAST.JObject
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package com.fluency03.blockchain
package core

import com.fluency03.blockchain.core.BlockHeader.hashOfBlockHeader
import com.fluency03.blockchain.Util.sha256Of
import org.json4s.native.JsonMethods.{compact, render}
import org.json4s.{Extraction, JValue}

Expand Down
3 changes: 0 additions & 3 deletions src/main/scala/com/fluency03/blockchain/core/Blockchain.scala
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
package com.fluency03.blockchain
package core

import com.fluency03.blockchain.Util.getCurrentTimestamp
import com.fluency03.blockchain.core.Blockchain._
import com.fluency03.blockchain.core.Block.canBeChained

import scala.collection.mutable

/**
* Blockchain with difficulty and the chain of Blocks.
* @param difficulty Difficulty of a Blockchain
Expand Down
2 changes: 0 additions & 2 deletions src/main/scala/com/fluency03/blockchain/core/Merkle.scala
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package com.fluency03.blockchain
package core

import com.fluency03.blockchain.Util.sha256Of

object Merkle {
def computeRoot(trans: Seq[Transaction]): String =
computeRootOfHashes(trans.map(_.id))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ package core
import java.security.KeyPair

import com.fluency03.blockchain.Crypto
import com.fluency03.blockchain.Util.sha256Of
import com.fluency03.blockchain.core.Transaction.hashOfTransaction
import org.bouncycastle.util.encoders.Hex
import org.json4s.JsonAST.JObject
Expand Down
Loading

0 comments on commit aa36d2b

Please sign in to comment.