mirror of
https://github.com/bitcoin-s/bitcoin-s.git
synced 2025-02-24 23:08:31 +01:00
Adding test cases to LockTimeInterpreter
This commit is contained in:
parent
716758cd64
commit
3279e5c45f
3 changed files with 83 additions and 5 deletions
|
@ -27,6 +27,16 @@ trait TransactionFactory extends Factory[Transaction] {
|
|||
TransactionImpl(oldTx.version,updatedInputs.inputs,oldTx.outputs,oldTx.lockTime)
|
||||
}
|
||||
|
||||
/**
|
||||
* Factory function that modifies a transactions locktime
|
||||
* @param oldTx
|
||||
* @param lockTime
|
||||
* @return
|
||||
*/
|
||||
def factory(oldTx : Transaction, lockTime : Long) : Transaction = {
|
||||
TransactionImpl(oldTx.version,oldTx.inputs,oldTx.outputs,lockTime)
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Removes the inputs of the transactions
|
||||
|
|
|
@ -35,10 +35,16 @@ trait LockTimeInterpreter extends BitcoinSLogger {
|
|||
}
|
||||
else {
|
||||
val isValid = program.stack.head match {
|
||||
case s : ScriptNumber if (s < ScriptNumberImpl(0)) => false
|
||||
case s : ScriptNumber if (s > ScriptNumberImpl(500000000) && program.transaction.lockTime < 500000000) => false
|
||||
case s : ScriptNumber if (s < ScriptNumberImpl(500000000) && program.transaction.lockTime > 500000000) => false
|
||||
case _ => false
|
||||
case s : ScriptNumber if (s < ScriptNumberImpl(0)) =>
|
||||
logger.warn("OP_CHECKLOCKTIMEVERIFY marks tx as invalid if the stack top is negative")
|
||||
false
|
||||
case s : ScriptNumber if (s >= ScriptNumberImpl(500000000) && program.transaction.lockTime < 500000000) =>
|
||||
logger.warn("OP_CHECKLOCKTIMEVERIFY marks the tx as invalid if stack top >= 500000000 & tx locktime < 500000000")
|
||||
false
|
||||
case s : ScriptNumber if (s < ScriptNumberImpl(500000000) && program.transaction.lockTime >= 500000000) =>
|
||||
logger.warn("OP_CHECKLOCKTIMEVERIFY marks the tx as invalid if stack top < 500000000 & tx locktime >= 500000000")
|
||||
false
|
||||
case _ => true
|
||||
}
|
||||
ScriptProgramFactory.factory(program,program.stack, program.script.tail, isValid)
|
||||
}
|
||||
|
|
|
@ -1,7 +1,8 @@
|
|||
package org.scalacoin.script.locktime
|
||||
|
||||
import org.scalacoin.protocol.transaction.{UpdateTransactionInputs, TransactionInputFactory, TransactionFactory}
|
||||
import org.scalacoin.script.ScriptProgramFactory
|
||||
import org.scalacoin.script.constant.OP_0
|
||||
import org.scalacoin.script.constant.{ScriptNumberImpl, OP_0}
|
||||
import org.scalacoin.util.TestUtil
|
||||
import org.scalatest.{MustMatchers, FlatSpec}
|
||||
|
||||
|
@ -25,4 +26,65 @@ class LockTimeInterpreterTest extends FlatSpec with MustMatchers with LockTimeIn
|
|||
val newProgram = opCheckLockTimeVerify(program)
|
||||
newProgram.isValid must be (false)
|
||||
}
|
||||
|
||||
it must "mark the transaction as invalid if the stack top is negative" in {
|
||||
val stack = Seq(ScriptNumberImpl(-1))
|
||||
val script = Seq(OP_CHECKLOCKTIMEVERIFY)
|
||||
val txInputAdjustedSequenceNumber = TransactionInputFactory.factory(TestUtil.transaction.inputs(0),0)
|
||||
val txAdjustedSequenceNumber = TransactionFactory.factory(TestUtil.transaction,UpdateTransactionInputs(Seq(txInputAdjustedSequenceNumber)))
|
||||
val adjustedLockTimeTx = TransactionFactory.factory(txAdjustedSequenceNumber,0)
|
||||
val baseProgram = ScriptProgramFactory.factory(adjustedLockTimeTx,TestUtil.testProgram.scriptPubKey,TestUtil.testProgram.inputIndex,TestUtil.testProgram.flags)
|
||||
val program = ScriptProgramFactory.factory(baseProgram,stack,script)
|
||||
val newProgram = opCheckLockTimeVerify(program)
|
||||
newProgram.isValid must be (false)
|
||||
}
|
||||
|
||||
it must "mark the transaction as invalid if the locktime on the tx is < 500000000 && stack top is >= 500000000" in {
|
||||
val stack = Seq(ScriptNumberImpl(500000000))
|
||||
val script = Seq(OP_CHECKLOCKTIMEVERIFY)
|
||||
val txInputAdjustedSequenceNumber = TransactionInputFactory.factory(TestUtil.transaction.inputs(0),0)
|
||||
val txAdjustedSequenceNumber = TransactionFactory.factory(TestUtil.transaction,UpdateTransactionInputs(Seq(txInputAdjustedSequenceNumber)))
|
||||
val adjustedLockTimeTx = TransactionFactory.factory(txAdjustedSequenceNumber,0)
|
||||
val baseProgram = ScriptProgramFactory.factory(adjustedLockTimeTx,TestUtil.testProgram.scriptPubKey,TestUtil.testProgram.inputIndex,TestUtil.testProgram.flags)
|
||||
val program = ScriptProgramFactory.factory(baseProgram,stack,script)
|
||||
val newProgram = opCheckLockTimeVerify(program)
|
||||
newProgram.isValid must be (false)
|
||||
}
|
||||
|
||||
it must "mark the transaction as invalid if the locktime on the tx is >= 500000000 && stack top is < 500000000" in {
|
||||
val stack = Seq(ScriptNumberImpl(499999999))
|
||||
val script = Seq(OP_CHECKLOCKTIMEVERIFY)
|
||||
val txInputAdjustedSequenceNumber = TransactionInputFactory.factory(TestUtil.transaction.inputs(0),0)
|
||||
val txAdjustedSequenceNumber = TransactionFactory.factory(TestUtil.transaction,UpdateTransactionInputs(Seq(txInputAdjustedSequenceNumber)))
|
||||
val adjustedLockTimeTx = TransactionFactory.factory(txAdjustedSequenceNumber,500000000)
|
||||
val baseProgram = ScriptProgramFactory.factory(adjustedLockTimeTx,TestUtil.testProgram.scriptPubKey,TestUtil.testProgram.inputIndex,TestUtil.testProgram.flags)
|
||||
val program = ScriptProgramFactory.factory(baseProgram,stack,script)
|
||||
val newProgram = opCheckLockTimeVerify(program)
|
||||
newProgram.isValid must be (false)
|
||||
}
|
||||
|
||||
it must "mark the transaction as valid if the locktime on the tx is < 500000000 && stack top is < 500000000" in {
|
||||
val stack = Seq(ScriptNumberImpl(499999999))
|
||||
val script = Seq(OP_CHECKLOCKTIMEVERIFY)
|
||||
val txInputAdjustedSequenceNumber = TransactionInputFactory.factory(TestUtil.transaction.inputs(0),0)
|
||||
val txAdjustedSequenceNumber = TransactionFactory.factory(TestUtil.transaction,UpdateTransactionInputs(Seq(txInputAdjustedSequenceNumber)))
|
||||
val adjustedLockTimeTx = TransactionFactory.factory(txAdjustedSequenceNumber,0)
|
||||
val baseProgram = ScriptProgramFactory.factory(adjustedLockTimeTx,TestUtil.testProgram.scriptPubKey,TestUtil.testProgram.inputIndex,TestUtil.testProgram.flags)
|
||||
val program = ScriptProgramFactory.factory(baseProgram,stack,script)
|
||||
val newProgram = opCheckLockTimeVerify(program)
|
||||
newProgram.isValid must be (true)
|
||||
}
|
||||
|
||||
it must "mark the transaction as valid if the locktime on the tx is >= 500000000 && stack top is >= 500000000" in {
|
||||
val stack = Seq(ScriptNumberImpl(500000000))
|
||||
val script = Seq(OP_CHECKLOCKTIMEVERIFY)
|
||||
val txInputAdjustedSequenceNumber = TransactionInputFactory.factory(TestUtil.transaction.inputs(0),0)
|
||||
val txAdjustedSequenceNumber = TransactionFactory.factory(TestUtil.transaction,UpdateTransactionInputs(Seq(txInputAdjustedSequenceNumber)))
|
||||
val adjustedLockTimeTx = TransactionFactory.factory(txAdjustedSequenceNumber,500000000)
|
||||
val baseProgram = ScriptProgramFactory.factory(adjustedLockTimeTx,TestUtil.testProgram.scriptPubKey,TestUtil.testProgram.inputIndex,TestUtil.testProgram.flags)
|
||||
val program = ScriptProgramFactory.factory(baseProgram,stack,script)
|
||||
val newProgram = opCheckLockTimeVerify(program)
|
||||
newProgram.isValid must be (true)
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Reference in a new issue