Skip to content

Commit

Permalink
add test for allTransAreValid
Browse files Browse the repository at this point in the history
  • Loading branch information
fluency03 committed Apr 27, 2018
1 parent b2fc78b commit eceee02
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 5 deletions.
4 changes: 0 additions & 4 deletions src/main/scala/com/fluency03/blockchain/core/Block.scala
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,6 @@ case class Block(header: BlockHeader, transactions: Seq[Transaction], hash: Stri
def hasValidHeaderHash: Boolean = hash == header.hash

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

def noDuplicateTxIn(): Boolean = noDuplicateTxInOf(transactions)

Expand Down
2 changes: 2 additions & 0 deletions src/test/scala/com/fluency03/blockchain/CryptoTest.scala
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ class CryptoTest extends FlatSpec with Matchers {
generatePublicKey(pair.getPrivate) shouldEqual pair.getPublic
recoverPublicKey(publicKeyToHex(pair.getPublic)) shouldEqual pair.getPublic
recoverPrivateKey(privateKeyToHex(pair.getPrivate)) shouldEqual pair.getPrivate
recoverPublicKey(pair.getPublic.toHex) shouldEqual pair.getPublic
recoverPrivateKey(pair.getPrivate.toHex) shouldEqual pair.getPrivate
}


Expand Down
35 changes: 34 additions & 1 deletion src/test/scala/com/fluency03/blockchain/core/BlockTest.scala
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
package com.fluency03.blockchain
package core

import java.security.KeyPair

import com.fluency03.blockchain.core.BlockHeader.hashOfBlockHeader
import com.fluency03.blockchain.core.Transaction.createCoinbaseTx
import com.fluency03.blockchain.core.Transaction.{COINBASE_AMOUNT, createCoinbaseTx, signTxIn, updateUTxOs}
import org.json4s.JValue
import org.json4s.JsonAST.{JArray, JInt, JObject, JString}
import org.json4s.JsonDSL._
import org.json4s.native.JsonMethods.parse
import org.scalatest.{FlatSpec, Matchers}

import scala.collection.mutable
import scala.io.Source

class BlockTest extends FlatSpec with Matchers {
Expand Down Expand Up @@ -145,5 +148,35 @@ class BlockTest extends FlatSpec with Matchers {
parse(newBlock.toString) shouldEqual json
}

"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

val pair1 = Crypto.generateKeyPair()
val address1 = pair1.getPublic.toHex

val ts = getCurrentTimestamp
val tx: Transaction = Transaction(
Seq(TxIn(Outpoint(genesis.transactions.head.id, 0), "")),
Seq(TxOut(address1, COINBASE_AMOUNT)),
ts)

val nextBlock = Block.mineNextBlock(genesis, "This is next Block!", ts, genesis.difficulty, Seq(tx))
nextBlock.allTransAreValid(uTxOs) shouldEqual false

val uTxOs2 = updateUTxOs(genesis.transactions, uTxOs.toMap)
uTxOs ++= uTxOs2
nextBlock.allTransAreValid(uTxOs) shouldEqual false

val genesisPrivate: String = Source.fromResource("private-key").getLines.mkString
val keyPair = new KeyPair(Crypto.recoverPublicKey(genesisMiner), Crypto.recoverPrivateKey(genesisPrivate))
val signedTxIns = tx.txIns.map(txIn => signTxIn(tx.id.hex2Bytes, txIn, keyPair, uTxOs)).filter(_.isDefined).map(_.get)
signedTxIns.length shouldEqual tx.txIns.length
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)))
validNewBlock.allTransAreValid(uTxOs) shouldEqual true
}

}

0 comments on commit eceee02

Please sign in to comment.