Skip to content

Commit

Permalink
rename canBeChained to validLinkBetween
Browse files Browse the repository at this point in the history
  • Loading branch information
fluency03 committed Apr 27, 2018
1 parent 4a6256f commit b2fc78b
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 9 deletions.
37 changes: 30 additions & 7 deletions src/main/scala/com/fluency03/blockchain/core/Block.scala
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.fluency03.blockchain
package core

import com.fluency03.blockchain.core.Block.allTransValidOf
import com.fluency03.blockchain.core.BlockHeader.hashOfHeaderFields
import com.fluency03.blockchain.core.Transaction.{createCoinbaseTx, validateCoinbaseTx, noDuplicateTxInOf}
import org.json4s.native.JsonMethods.{compact, render}
Expand Down Expand Up @@ -39,10 +40,11 @@ case class Block(header: BlockHeader, transactions: Seq[Transaction], hash: Stri

def hasValidHeaderHash: Boolean = hash == header.hash

def allTransactionsValid(uTxOs: mutable.Map[Outpoint, TxOut]): Boolean = transactions match {
case Nil => false
case init :+ last => validateCoinbaseTx(last, index) && init.forall(tx => tx.isValid(uTxOs))
}
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 Expand Up @@ -121,10 +123,31 @@ object Block {
transactions: Seq[Transaction]): Block =
mineNextBlock(currentBlock.index + 1, currentBlock.hash, newBlockData, timestamp, difficulty, transactions)

// TODO (Chang): Check transactions in the new block?
def canBeChained(newBlock: Block, previousBlock: Block): Boolean =
previousBlock.index + 1 == newBlock.index && previousBlock.hash == newBlock.previousHash && newBlock.hasValidHash
/**
* Check whether transactions of a Block are valid:
* 1. Coinbase transaction is valid
* 2. Rest of the transactions are valid
* 3. if the Seq is empty, then it is not valid, because it has to at least contain one coinbase transaction
*/
def allTransValidOf(
transactions: Seq[Transaction],
blockIndex: Int,
uTxOs: mutable.Map[Outpoint, TxOut])
: Boolean = transactions match {
case Nil => false
case init :+ last => validateCoinbaseTx(last, blockIndex) && init.forall(tx => tx.isValid(uTxOs))
}

/**
* Check whether a new Block can be chained to the last Block of a Blockchain (previousBlock of newBlock):
* 1. New Block's index should be the previous index plus one
* 2. New Block's previousHash should be the hash of previous Block
* 3. New Block should have valid hash (that means, valid header hash and merkle hash)
*/
def validLinkBetween(newBlock: Block, previousBlock: Block): Boolean =
previousBlock.index + 1 == newBlock.index &&
previousBlock.hash == newBlock.previousHash &&
newBlock.hasValidHash


}
Expand Down
4 changes: 2 additions & 2 deletions src/main/scala/com/fluency03/blockchain/core/Blockchain.scala
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.fluency03.blockchain
package core

import com.fluency03.blockchain.core.Block.canBeChained
import com.fluency03.blockchain.core.Block.validLinkBetween
import com.fluency03.blockchain.core.Blockchain._
import org.json4s.native.JsonMethods.{compact, render}
import org.json4s.{Extraction, JValue}
Expand Down Expand Up @@ -51,7 +51,7 @@ object Blockchain {
def isValidChain(chain: Seq[Block]): Boolean = chain match {
case Nil => true
case g +: Nil => g.previousHash == ZERO64 && g.index == 0 && g.hasValidHash
case a +: b +: tail => canBeChained(a, b) && isValidChain(b +: tail)
case a +: b +: tail => validLinkBetween(a, b) && isValidChain(b +: tail)
}

}
Expand Down

0 comments on commit b2fc78b

Please sign in to comment.