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 27, 2018
2 parents 438cb63 + 0280376 commit 2115294
Show file tree
Hide file tree
Showing 5 changed files with 77 additions and 14 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

Simple Blockchain Implementation in Scala.

*This project is still under development.*
*This project is still under development. APIs are not fully completed according to the cores.*

Inspired by:
- [Daniel van Flymen](http://www.dvf.nyc/)'s blog [Learn Blockchains by Building One](https://hackernoon.com/learn-blockchains-by-building-one-117428612f46)
Expand Down
26 changes: 14 additions & 12 deletions src/main/scala/com/fluency03/blockchain/api/actors/PeerActor.scala
Original file line number Diff line number Diff line change
Expand Up @@ -5,36 +5,38 @@ import java.security.KeyPair

import akka.actor.Props
import com.fluency03.blockchain.api.actors.PeerActor._
import com.fluency03.blockchain.core.Peer
import com.fluency03.blockchain.core.{Peer, Wallet}

import scala.collection.mutable

object PeerActor {
final case object GetPublicKeys
final case object CreateKeyPair
final case object CreateWallet
def props: Props = Props[PeerActor]
}

class PeerActor extends ActorSupport {
override def preStart(): Unit = log.info("{} started!", this.getClass.getSimpleName)
override def preStart(): Unit = {
log.info("{} started!", this.getClass.getSimpleName)
addWallet()
log.info("Created initial wallet: {}", wallets.head._1)
}
override def postStop(): Unit = log.info("{} stopped!", this.getClass.getSimpleName)

// TODO (Chang): need persistence
val wallets = mutable.Map.empty[String, KeyPair]
addNewKeyPair()

val wallets = mutable.Map.empty[String, Wallet]
val others = mutable.Map.empty[String, Peer]

def receive: Receive = {
case GetPublicKeys => sender() ! wallets.values.map(_.getPublic.toHex).toSet
case CreateKeyPair => sender() ! addNewKeyPair().getPublic.toHex
case GetPublicKeys => sender() ! wallets.values.map(_.address).toSet
case CreateWallet => sender() ! addWallet()
case _ => unhandled _
}

private def addNewKeyPair(): KeyPair = {
val pair: KeyPair = Crypto.generateKeyPair()
wallets += (pair.getPublic.toHex -> pair)
pair
private def addWallet(): String = {
val newWallet = Wallet()
wallets += (newWallet.address -> newWallet)
newWallet.address
}

}
28 changes: 28 additions & 0 deletions src/main/scala/com/fluency03/blockchain/core/Wallet.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package com.fluency03.blockchain
package core

import java.security.KeyPair

import com.fluency03.blockchain.core.Wallet.balanceOfWallet

import scala.collection.mutable

case class Wallet() {

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

lazy val address: String = keyPair.getPublic.toHex

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

}

object Wallet {

def balanceOfWallet(wallet: Wallet, uTxOs: mutable.Map[Outpoint, TxOut]): Long =
balanceOfAddress(wallet.address, uTxOs)

def balanceOfAddress(address: String, uTxOs: mutable.Map[Outpoint, TxOut]): Long =
uTxOs.values.filter(_.address == address).map(_.amount).sum

}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package com.fluency03.blockchain.api.actors

import akka.actor.{ActorRef, ActorSystem, Props}
import akka.testkit.{ImplicitSender, TestKit}
import com.fluency03.blockchain.api.actors.PeerActor.GetPublicKeys
import com.fluency03.blockchain.api.actors.PeerActor.{CreateWallet, GetPublicKeys}
import org.scalatest.{BeforeAndAfterAll, Matchers, WordSpecLike}

class PeerActorTest extends TestKit(ActorSystem("PeerActorTest")) with ImplicitSender
Expand All @@ -22,6 +22,13 @@ class PeerActorTest extends TestKit(ActorSystem("PeerActorTest")) with ImplicitS
val publicKeys = expectMsgType[Set[String]]
publicKeys.size shouldEqual 1

peerActor ! CreateWallet
expectMsgType[String]

peerActor ! GetPublicKeys
val publicKeys2 = expectMsgType[Set[String]]
publicKeys2.size shouldEqual 2

peerActor ! "other"
expectNoMessage
}
Expand Down
26 changes: 26 additions & 0 deletions src/test/scala/com/fluency03/blockchain/core/WalletTest.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package com.fluency03.blockchain.core

import com.fluency03.blockchain.core.Wallet._

import org.scalatest.{FlatSpec, Matchers}

import scala.collection.mutable

class WalletTest extends FlatSpec with Matchers {

"balanceOfWallet" should "obtain the balance of a Wallet based on UTXOs." in {
val wallet = Wallet()
val uTxOs: mutable.Map[Outpoint, TxOut] = mutable.Map.empty[Outpoint, TxOut]

balanceOfWallet(wallet, uTxOs) shouldEqual 0
wallet.balance(uTxOs) shouldEqual 0

uTxOs += (Outpoint("def0", 0) -> TxOut(wallet.address, 40))
uTxOs += (Outpoint("def0", 1) -> TxOut("abc4", 40))

balanceOfWallet(wallet, uTxOs) shouldEqual 40
wallet.balance(uTxOs) shouldEqual 40
}


}

0 comments on commit 2115294

Please sign in to comment.