From f4cdf684312b39673a9ac4a7efc108b24a4b5c8e Mon Sep 17 00:00:00 2001 From: fluency03 Date: Tue, 1 May 2018 15:58:42 +0200 Subject: [PATCH] add removeBlock on Blockchain --- .../fluency03/blockchain/api/actors/BlockchainActor.scala | 2 ++ .../scala/com/fluency03/blockchain/core/Blockchain.scala | 5 ++++- .../scala/com/fluency03/blockchain/core/BlockchainTest.scala | 1 + 3 files changed, 7 insertions(+), 1 deletion(-) diff --git a/src/main/scala/com/fluency03/blockchain/api/actors/BlockchainActor.scala b/src/main/scala/com/fluency03/blockchain/api/actors/BlockchainActor.scala index 3cd5859..34d1d9e 100644 --- a/src/main/scala/com/fluency03/blockchain/api/actors/BlockchainActor.scala +++ b/src/main/scala/com/fluency03/blockchain/api/actors/BlockchainActor.scala @@ -90,4 +90,6 @@ class BlockchainActor extends ActorSupport { } + + } diff --git a/src/main/scala/com/fluency03/blockchain/core/Blockchain.scala b/src/main/scala/com/fluency03/blockchain/core/Blockchain.scala index c4426b0..2b4e0ee 100644 --- a/src/main/scala/com/fluency03/blockchain/core/Blockchain.scala +++ b/src/main/scala/com/fluency03/blockchain/core/Blockchain.scala @@ -11,7 +11,8 @@ import scala.annotation.tailrec /** * Blockchain with difficulty and the chain of Blocks. * @param difficulty Difficulty of a Blockchain - * @param chain Chain of Blocks + * @param chain Chain of Blocks, the newest Block is on the head of the chain; + * the first genesis Block is on the last of the chain */ case class Blockchain(difficulty: Int = 4, chain: Seq[Block] = Seq(Block.genesisBlock)) { @@ -20,6 +21,8 @@ case class Blockchain(difficulty: Int = 4, chain: Seq[Block] = Seq(Block.genesis def addBlock(newBlock: Block): Blockchain = Blockchain(difficulty, newBlock +: chain) + def removeBlock(): Blockchain = Blockchain(difficulty, chain.tail) + def lastBlock(): Option[Block] = chain.headOption def mineNextBlock(newBlockData: String, transactions: Seq[Transaction]): Block = { diff --git a/src/test/scala/com/fluency03/blockchain/core/BlockchainTest.scala b/src/test/scala/com/fluency03/blockchain/core/BlockchainTest.scala index ae43c55..81825a4 100644 --- a/src/test/scala/com/fluency03/blockchain/core/BlockchainTest.scala +++ b/src/test/scala/com/fluency03/blockchain/core/BlockchainTest.scala @@ -65,6 +65,7 @@ class BlockchainTest extends FlatSpec with Matchers { blockchainAdded.lastBlock().get shouldEqual expected blockchainAdded.isValid shouldEqual true blockchainAdded.length shouldEqual 2 + blockchainAdded.removeBlock() shouldEqual blockchainToAdd val blockchainAdded2 = blockchainToAdd.addBlock("This is next Block!", Seq(t1, t2)) val newLastBlock = blockchainAdded2.lastBlock().get