Skip to content

Commit eceee02

Browse files
committed
add test for allTransAreValid
1 parent b2fc78b commit eceee02

File tree

3 files changed

+36
-5
lines changed

3 files changed

+36
-5
lines changed

src/main/scala/com/fluency03/blockchain/core/Block.scala

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,10 +41,6 @@ case class Block(header: BlockHeader, transactions: Seq[Transaction], hash: Stri
4141
def hasValidHeaderHash: Boolean = hash == header.hash
4242

4343
def allTransAreValid(uTxOs: mutable.Map[Outpoint, TxOut]): Boolean = allTransValidOf(transactions, index, uTxOs)
44-
// transactions match {
45-
// case Nil => false
46-
// case init :+ last => validateCoinbaseTx(last, index) && init.forall(tx => tx.isValid(uTxOs))
47-
// }
4844

4945
def noDuplicateTxIn(): Boolean = noDuplicateTxInOf(transactions)
5046

src/test/scala/com/fluency03/blockchain/CryptoTest.scala

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ class CryptoTest extends FlatSpec with Matchers {
1818
generatePublicKey(pair.getPrivate) shouldEqual pair.getPublic
1919
recoverPublicKey(publicKeyToHex(pair.getPublic)) shouldEqual pair.getPublic
2020
recoverPrivateKey(privateKeyToHex(pair.getPrivate)) shouldEqual pair.getPrivate
21+
recoverPublicKey(pair.getPublic.toHex) shouldEqual pair.getPublic
22+
recoverPrivateKey(pair.getPrivate.toHex) shouldEqual pair.getPrivate
2123
}
2224

2325

src/test/scala/com/fluency03/blockchain/core/BlockTest.scala

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,17 @@
11
package com.fluency03.blockchain
22
package core
33

4+
import java.security.KeyPair
5+
46
import com.fluency03.blockchain.core.BlockHeader.hashOfBlockHeader
5-
import com.fluency03.blockchain.core.Transaction.createCoinbaseTx
7+
import com.fluency03.blockchain.core.Transaction.{COINBASE_AMOUNT, createCoinbaseTx, signTxIn, updateUTxOs}
68
import org.json4s.JValue
79
import org.json4s.JsonAST.{JArray, JInt, JObject, JString}
810
import org.json4s.JsonDSL._
911
import org.json4s.native.JsonMethods.parse
1012
import org.scalatest.{FlatSpec, Matchers}
1113

14+
import scala.collection.mutable
1215
import scala.io.Source
1316

1417
class BlockTest extends FlatSpec with Matchers {
@@ -145,5 +148,35 @@ class BlockTest extends FlatSpec with Matchers {
145148
parse(newBlock.toString) shouldEqual json
146149
}
147150

151+
"allTransAreValid" should "check whether all transactions of a Block are valid." in {
152+
val uTxOs: mutable.Map[Outpoint, TxOut] = mutable.Map.empty[Outpoint, TxOut]
153+
genesis.allTransAreValid(uTxOs) shouldEqual true
154+
155+
val pair1 = Crypto.generateKeyPair()
156+
val address1 = pair1.getPublic.toHex
157+
158+
val ts = getCurrentTimestamp
159+
val tx: Transaction = Transaction(
160+
Seq(TxIn(Outpoint(genesis.transactions.head.id, 0), "")),
161+
Seq(TxOut(address1, COINBASE_AMOUNT)),
162+
ts)
163+
164+
val nextBlock = Block.mineNextBlock(genesis, "This is next Block!", ts, genesis.difficulty, Seq(tx))
165+
nextBlock.allTransAreValid(uTxOs) shouldEqual false
166+
167+
val uTxOs2 = updateUTxOs(genesis.transactions, uTxOs.toMap)
168+
uTxOs ++= uTxOs2
169+
nextBlock.allTransAreValid(uTxOs) shouldEqual false
170+
171+
val genesisPrivate: String = Source.fromResource("private-key").getLines.mkString
172+
val keyPair = new KeyPair(Crypto.recoverPublicKey(genesisMiner), Crypto.recoverPrivateKey(genesisPrivate))
173+
val signedTxIns = tx.txIns.map(txIn => signTxIn(tx.id.hex2Bytes, txIn, keyPair, uTxOs)).filter(_.isDefined).map(_.get)
174+
signedTxIns.length shouldEqual tx.txIns.length
175+
val signedTx = Transaction(signedTxIns, Seq(TxOut(address1, COINBASE_AMOUNT)), ts)
176+
177+
val validNewBlock = Block.mineNextBlock(genesis, "This is next Block!", ts, genesis.difficulty,
178+
Seq(signedTx, createCoinbaseTx(1, address1, ts)))
179+
validNewBlock.allTransAreValid(uTxOs) shouldEqual true
180+
}
148181

149182
}

0 commit comments

Comments
 (0)