-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
17 changed files
with
160 additions
and
101 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
31 changes: 31 additions & 0 deletions
31
src/main/scala/com/fluency03/blockchain/api/actors/TransactionRegistryActor.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package com.fluency03.blockchain.api.actors | ||
|
||
import akka.actor.{Actor, ActorLogging, ActorRef, Props} | ||
import com.fluency03.blockchain.core.{Block, Transaction} | ||
|
||
object TransactionRegistryActor { | ||
final case class ActionPerformed(description: String) | ||
final case object GetTransactions | ||
final case class CreateTransaction(tx: Transaction) | ||
final case class GetTransaction(hash: String) | ||
final case class DeleteTransaction(hash: String) | ||
|
||
def props: Props = Props[TransactionRegistryActor] | ||
} | ||
|
||
class TransactionRegistryActor extends Actor with ActorLogging { | ||
import TransactionRegistryActor._ | ||
|
||
var transactions = Set.empty[Transaction] | ||
|
||
def receive: Receive = { | ||
case GetTransactions => sender() ! transactions.toList | ||
case CreateTransaction(tx) => | ||
transactions += tx | ||
sender() ! ActionPerformed(s"Transaction ${tx.hash} created.") | ||
case GetTransaction(hash) => sender() ! transactions.find(_.hash == hash) | ||
case DeleteTransaction(hash) => | ||
transactions.find(_.hash == hash) foreach { tx => transactions -= tx } | ||
sender() ! ActionPerformed(s"Transaction $hash deleted.") | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
53 changes: 53 additions & 0 deletions
53
src/main/scala/com/fluency03/blockchain/api/routes/TransactionRoutes.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
package com.fluency03.blockchain.api.routes | ||
|
||
import akka.actor.ActorRef | ||
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.{delete, get, post} | ||
import akka.http.scaladsl.server.directives.PathDirectives.path | ||
import akka.http.scaladsl.server.directives.RouteDirectives.complete | ||
import akka.pattern.ask | ||
import com.fluency03.blockchain.api.actors.BlockRegistryActor._ | ||
import com.fluency03.blockchain.core.Block | ||
|
||
import scala.concurrent.Future | ||
|
||
trait TransactionRoutes extends Routes { | ||
lazy val log = Logging(system, classOf[TransactionRoutes]) | ||
|
||
def txRoutesRegistryActor: ActorRef | ||
|
||
// lazy val transactionRoutes: Route = | ||
// pathPrefix("transactions") { | ||
// pathEnd { | ||
// get { | ||
// val blocks: Future[List[Block]] = (txRoutesRegistryActor ? GetBlocks).mapTo[List[Block]] | ||
// complete(blocks) | ||
// } ~ | ||
// post { | ||
// entity(as[Block]) { block => | ||
// val blockCreated: Future[ActionPerformed] = (txRoutesRegistryActor ? CreateBlock(block)).mapTo[ActionPerformed] | ||
// onSuccess(blockCreated) { performed => | ||
// log.info("Created user [{}]: {}", block.hash, performed.description) | ||
// complete((StatusCodes.Created, performed)) | ||
// } | ||
// } | ||
// } | ||
// } ~ | ||
// path(Segment) { hash => | ||
// get { | ||
// val maybeBlock: Future[Option[Block]] = (txRoutesRegistryActor ? GetBlock(hash)).mapTo[Option[Block]] | ||
// rejectEmptyResponse { complete(maybeBlock) } | ||
// } ~ | ||
// delete { | ||
// val blockDeleted: Future[ActionPerformed] = (txRoutesRegistryActor ? DeleteBlock(hash)).mapTo[ActionPerformed] | ||
// onSuccess(blockDeleted) { performed => | ||
// log.info("Deleted block [{}]: {}", hash, performed.description) | ||
// complete((StatusCodes.OK, performed)) | ||
// } | ||
// } | ||
// } | ||
// } | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,14 @@ | ||
package com.fluency03.blockchain | ||
|
||
import java.time.Instant | ||
|
||
import org.json4s.NoTypeHints | ||
import org.json4s.native.Serialization | ||
|
||
package object core { | ||
|
||
implicit val formats = Serialization.formats(NoTypeHints) | ||
lazy val ZERO64: String = "0000000000000000000000000000000000000000000000000000000000000000" | ||
lazy val genesisTimestamp: Long = Instant.parse("2018-04-11T18:52:01Z").getEpochSecond | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,18 +1 @@ | ||
{ | ||
"header": { | ||
"index": 0, | ||
"previousHash": "0000000000000000000000000000000000000000000000000000000000000000", | ||
"data": "Welcome to Blockchain in Scala!", | ||
"merkleHash": "7814a9c43e9015462e5ffec1a3a9a69be024c1aacfa3ec4c879b5cd544761e7e", | ||
"timestamp": 1523472721, | ||
"nonce": 13860, | ||
"hash": "00003607219f7a455e216f19ac3a34e3b158cf7282f7fdc624c93d593c2fc61f" | ||
}, | ||
"transactions": [ | ||
{ | ||
"sender": "0000000000000000000000000000000000000000000000000000000000000000", | ||
"receiver": "0000000000000000000000000000000000000000000000000000000000000000", | ||
"amount": 50.0 | ||
} | ||
] | ||
} | ||
{"header":{"index":0,"previousHash":"0000000000000000000000000000000000000000000000000000000000000000","data":"Welcome to Blockchain in Scala!","merkleHash":"21d1db6630265eb9e991b410d82870a8d3f62cb11d1d0917e926a49bdb3993b5","timestamp":1523472721,"nonce":33660,"hash":"0000a26af9a70022a6c6d270a0ced7478eb40bcfc4301b5e73c0ed3207a3de0e"},"transactions":[{"sender":"0000000000000000000000000000000000000000000000000000000000000000","receiver":"0000000000000000000000000000000000000000000000000000000000000000","amount":50.0,"timestamp":1523472721}]} |
Oops, something went wrong.