Skip to content

Commit

Permalink
add removeBlock on Blockchain
Browse files Browse the repository at this point in the history
  • Loading branch information
fluency03 committed May 1, 2018
1 parent fc4c0e1 commit f4cdf68
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -90,4 +90,6 @@ class BlockchainActor extends ActorSupport {
}




}
Original file line number Diff line number Diff line change
Expand Up @@ -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)) {

Expand All @@ -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 = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit f4cdf68

Please sign in to comment.