From 2e3883b69d30eddae219b0f5c75bead2ec90f758 Mon Sep 17 00:00:00 2001 From: fluency03 Date: Sat, 28 Apr 2018 11:37:41 +0100 Subject: [PATCH] add new api for mineNextBlock with new miner address for coinbase --- .../scala/com/fluency03/blockchain/core/Block.scala | 10 ++++++++++ .../com/fluency03/blockchain/core/BlockTest.scala | 4 +++- 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/src/main/scala/com/fluency03/blockchain/core/Block.scala b/src/main/scala/com/fluency03/blockchain/core/Block.scala index e562c19..1036f0e 100644 --- a/src/main/scala/com/fluency03/blockchain/core/Block.scala +++ b/src/main/scala/com/fluency03/blockchain/core/Block.scala @@ -119,6 +119,16 @@ object Block { transactions: Seq[Transaction]): Block = mineNextBlock(currentBlock.index + 1, currentBlock.hash, newBlockData, timestamp, difficulty, transactions) + def mineNextBlock( + currentBlock: Block, + newBlockData: String, + timestamp: Long, + difficulty: Int, + transactions: Seq[Transaction], + miner: String): Block = + mineNextBlock(currentBlock.index + 1, currentBlock.hash, newBlockData, timestamp, difficulty, + transactions :+ createCoinbaseTx(currentBlock.index + 1, miner, timestamp)) + /** * Check whether transactions of a Block are valid: * 1. Coinbase transaction is valid diff --git a/src/test/scala/com/fluency03/blockchain/core/BlockTest.scala b/src/test/scala/com/fluency03/blockchain/core/BlockTest.scala index 47a4f2b..5d2bbb1 100644 --- a/src/test/scala/com/fluency03/blockchain/core/BlockTest.scala +++ b/src/test/scala/com/fluency03/blockchain/core/BlockTest.scala @@ -3,6 +3,7 @@ package core import java.security.KeyPair +import com.fluency03.blockchain.core.Block.allTransValidOf import com.fluency03.blockchain.core.BlockHeader.hashOfBlockHeader import com.fluency03.blockchain.core.Transaction.{COINBASE_AMOUNT, createCoinbaseTx, signTxIn, updateUTxOs} import org.json4s.JValue @@ -151,6 +152,7 @@ class BlockTest extends FlatSpec with Matchers { "allTransAreValid" should "check whether all transactions of a Block are valid." in { val uTxOs: mutable.Map[Outpoint, TxOut] = mutable.Map.empty[Outpoint, TxOut] genesis.allTransAreValid(uTxOs) shouldEqual true + allTransValidOf(Nil, 0, uTxOs) shouldEqual false val pair1 = Crypto.generateKeyPair() val address1 = pair1.getPublic.toHex @@ -175,7 +177,7 @@ class BlockTest extends FlatSpec with Matchers { val signedTx = Transaction(signedTxIns, Seq(TxOut(address1, COINBASE_AMOUNT)), ts) val validNewBlock = Block.mineNextBlock(genesis, "This is next Block!", ts, genesis.difficulty, - Seq(signedTx, createCoinbaseTx(1, address1, ts))) + Seq(signedTx), address1) validNewBlock.allTransAreValid(uTxOs) shouldEqual true }