Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Test for block with invalid extension application. #484

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
139 changes: 88 additions & 51 deletions src/test/scala/org/ergoplatform/nodeView/ErgoNodeViewHolderSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,55 @@ class ErgoNodeViewHolderSpec extends ErgoPropertyTest with NodeViewTestOps with
}
}

private val t7 = TestCase("apply invalid full block") { fixture =>
private val t7 = TestCase("switching for a better chain") { fixture =>
import fixture._
val (us, bh) = createUtxoState(Some(nodeViewHolderRef))
val genesis = validFullBlock(parentOpt = None, us, bh)
val wusAfterGenesis = WrappedUtxoState(us, bh, stateConstants).applyModifier(genesis).get

applyBlock(genesis) shouldBe 'success
getRootHash shouldBe Algos.encode(wusAfterGenesis.rootHash)

val chain1block1 = validFullBlock(Some(genesis.header), wusAfterGenesis)
val expectedBestFullBlockOpt = if (verifyTransactions) Some(chain1block1) else None
applyBlock(chain1block1) shouldBe 'success
getBestFullBlockOpt shouldBe expectedBestFullBlockOpt
getBestHeaderOpt shouldBe Some(chain1block1.header)

val chain2block1 = validFullBlock(Some(genesis.header), wusAfterGenesis)
applyBlock(chain2block1) shouldBe 'success
getBestFullBlockOpt shouldBe expectedBestFullBlockOpt
getBestHeaderOpt shouldBe Some(chain1block1.header)

val wusChain2Block1 = wusAfterGenesis.applyModifier(chain2block1).get
val chain2block2 = validFullBlock(Some(chain2block1.header), wusChain2Block1)
chain2block1.header.stateRoot shouldEqual wusChain2Block1.rootHash

applyBlock(chain2block2) shouldBe 'success
if (verifyTransactions) {
getBestFullBlockEncodedId shouldBe Some(chain2block2.header.encodedId)
}

getBestHeaderOpt shouldBe Some(chain2block2.header)
getRootHash shouldBe Algos.encode(chain2block2.header.stateRoot)
}

private val t8 = TestCase("UTXO state should generate adProofs and put them in history") { fixture =>
import fixture._
if (stateType == StateType.Utxo) {
val (us, bh) = createUtxoState(Some(nodeViewHolderRef))
val genesis = validFullBlock(parentOpt = None, us, bh)

nodeViewHolderRef ! LocallyGeneratedModifier(genesis.header)
nodeViewHolderRef ! LocallyGeneratedModifier(genesis.blockTransactions)
nodeViewHolderRef ! LocallyGeneratedModifier(genesis.extension)

getBestFullBlockOpt shouldBe Some(genesis)
getModifierById(genesis.adProofs.get.id) shouldBe genesis.adProofs
}
}

private val t9 = TestCase("apply invalid full block") { fixture =>
import fixture._
val (us, bh) = createUtxoState(Some(nodeViewHolderRef))
val genesis = validFullBlock(parentOpt = None, us, bh)
Expand All @@ -113,15 +161,13 @@ class ErgoNodeViewHolderSpec extends ErgoPropertyTest with NodeViewTestOps with

applyBlock(block) shouldBe 'success
getBestHeaderOpt shouldBe Some(block.header)
if (verifyTransactions) {
getRootHash shouldBe Algos.encode(wusAfterBlock.rootHash)
}
getRootHash shouldBe Algos.encode(wusAfterBlock.rootHash)
getBestHeaderOpt shouldBe Some(block.header)

val brokenBlock = generateInvalidFullBlock(block.header, wusAfterBlock)
val brokenBlock = genInvalidFullBlock(block.header, wusAfterBlock)
applyBlock(brokenBlock) shouldBe 'success

val brokenBlock2 = generateInvalidFullBlock(block.header, wusAfterBlock)
val brokenBlock2 = genInvalidFullBlock(block.header, wusAfterBlock)
brokenBlock2.header should not be brokenBlock.header
applyBlock(brokenBlock2) shouldBe 'success

Expand All @@ -131,11 +177,11 @@ class ErgoNodeViewHolderSpec extends ErgoPropertyTest with NodeViewTestOps with
}
}

private def generateInvalidFullBlock(parentHeader: Header, parentState: WrappedUtxoState) = {
private def genInvalidFullBlock(parentHeader: Header, parentState: WrappedUtxoState) = {
val extensionIn = extensionGen.sample.get
val brokenBlockIn = validFullBlock(Some(parentHeader), parentState)
val headTx = brokenBlockIn.blockTransactions.txs.head
val wrongBoxId: ADKey = ADKey @@ Algos.hash("wrong input")
val wrongBoxId: ADKey = ADKey !@@ Algos.hash("wrong input")
val newInput = headTx.inputs.head.copy(boxId = wrongBoxId)
val brokenTransactionsIn = brokenBlockIn.blockTransactions
.copy(txs = headTx.copy(inputs = newInput +: headTx.inputs.tail) +: brokenBlockIn.blockTransactions.txs.tail)
Expand All @@ -147,55 +193,46 @@ class ErgoNodeViewHolderSpec extends ErgoPropertyTest with NodeViewTestOps with
ErgoFullBlock(brokenHeader, brokenTransactions, extension, Some(brokenProofs))
}

private val t8 = TestCase("switching for a better chain") { fixture =>
private val t10 = TestCase("apply full block with invalid extension") { fixture =>
import fixture._
val (us, bh) = createUtxoState(Some(nodeViewHolderRef))
val genesis = validFullBlock(parentOpt = None, us, bh)
val wusAfterGenesis = WrappedUtxoState(us, bh, stateConstants).applyModifier(genesis).get
if (verifyTransactions) {
applyBlock(genesis) shouldBe 'success

applyBlock(genesis) shouldBe 'success
getRootHash shouldBe Algos.encode(wusAfterGenesis.rootHash)
val block = validFullBlock(Some(genesis.header), wusAfterGenesis)
val wusAfterBlock = wusAfterGenesis.applyModifier(block).get

val chain1block1 = validFullBlock(Some(genesis.header), wusAfterGenesis)
val expectedBestFullBlockOpt = if (verifyTransactions) Some(chain1block1) else None
applyBlock(chain1block1) shouldBe 'success
getBestFullBlockOpt shouldBe expectedBestFullBlockOpt
getBestHeaderOpt shouldBe Some(chain1block1.header)
applyBlock(block) shouldBe 'success
getBestHeaderOpt shouldBe Some(block.header)
getRootHash shouldBe Algos.encode(wusAfterBlock.rootHash)
getBestHeaderOpt shouldBe Some(block.header)

val chain2block1 = validFullBlock(Some(genesis.header), wusAfterGenesis)
applyBlock(chain2block1) shouldBe 'success
getBestFullBlockOpt shouldBe expectedBestFullBlockOpt
getBestHeaderOpt shouldBe Some(chain1block1.header)
val brokenBlock = genBlockWithInvalidExtension(block.header, wusAfterBlock)
applyBlock(brokenBlock) shouldBe 'success

val wusChain2Block1 = wusAfterGenesis.applyModifier(chain2block1).get
val chain2block2 = validFullBlock(Some(chain2block1.header), wusChain2Block1)
chain2block1.header.stateRoot shouldEqual wusChain2Block1.rootHash
val brokenBlock2 = genBlockWithInvalidExtension(block.header, wusAfterBlock)
brokenBlock2.header should not be brokenBlock.header
applyBlock(brokenBlock2) shouldBe 'success

applyBlock(chain2block2) shouldBe 'success
if (verifyTransactions) {
getBestFullBlockEncodedId shouldBe Some(chain2block2.header.encodedId)
getBestFullBlockOpt shouldBe Some(block)
getRootHash shouldBe Algos.encode(wusAfterBlock.rootHash)
getBestHeaderOpt shouldBe Some(block.header)
}

getBestHeaderOpt shouldBe Some(chain2block2.header)
getRootHash shouldBe Algos.encode(chain2block2.header.stateRoot)
}

private val t9 = TestCase("UTXO state should generate adProofs and put them in history") { fixture =>
import fixture._
if (stateType == StateType.Utxo) {
val (us, bh) = createUtxoState(Some(nodeViewHolderRef))
val genesis = validFullBlock(parentOpt = None, us, bh)

nodeViewHolderRef ! LocallyGeneratedModifier(genesis.header)
nodeViewHolderRef ! LocallyGeneratedModifier(genesis.blockTransactions)
nodeViewHolderRef ! LocallyGeneratedModifier(genesis.extension)

getBestFullBlockOpt shouldBe Some(genesis)
getModifierById(genesis.adProofs.get.id) shouldBe genesis.adProofs
}
private def genBlockWithInvalidExtension(parentHeader: Header, parentState: WrappedUtxoState) = {
val extensionIn = extensionGen.sample.get
val validBlockIn = validFullBlock(Some(parentHeader), parentState)
val brokenExtension = extensionIn.copy(
mandatoryFields = extensionIn.mandatoryFields :+ extensionIn.mandatoryFields.head,
headerId = validBlockIn.id
)
validBlockIn.copy(extension = brokenExtension)
}

private val t10 = TestCase("NodeViewHolder start from inconsistent state") { fixture =>
private val t11 = TestCase("NodeViewHolder start from inconsistent state") { fixture =>
import fixture._
val (us, bh) = createUtxoState(Some(nodeViewHolderRef))
val genesis = validFullBlock(parentOpt = None, us, bh)
Expand All @@ -215,7 +252,7 @@ class ErgoNodeViewHolderSpec extends ErgoPropertyTest with NodeViewTestOps with
getRootHash shouldBe Algos.encode(block1.header.stateRoot)
}

private val t11 = TestCase("apply payload in incorrect order") { fixture =>
private val t12 = TestCase("apply payload in incorrect order") { fixture =>
import fixture._
val (us, bh) = createUtxoState(Some(nodeViewHolderRef))
val genesis = validFullBlock(parentOpt = None, us, bh)
Expand All @@ -240,7 +277,7 @@ class ErgoNodeViewHolderSpec extends ErgoPropertyTest with NodeViewTestOps with
getBestFullBlockEncodedId shouldBe Some(chain2block2.header.encodedId)
}

private val t12 = TestCase("Do not apply txs with wrong header id") { fixture =>
private val t13 = TestCase("Do not apply txs with wrong header id") { fixture =>
import fixture._

val (us, bh) = createUtxoState(Some(nodeViewHolderRef))
Expand Down Expand Up @@ -291,7 +328,7 @@ class ErgoNodeViewHolderSpec extends ErgoPropertyTest with NodeViewTestOps with
expectMsgType[SyntacticallySuccessfulModifier[BlockTransactions]]
}

private val t13 = TestCase("Do not apply wrong adProofs") { fixture =>
private val t14 = TestCase("Do not apply wrong adProofs") { fixture =>
import fixture._

val (us, bh) = createUtxoState(Some(nodeViewHolderRef))
Expand Down Expand Up @@ -321,7 +358,7 @@ class ErgoNodeViewHolderSpec extends ErgoPropertyTest with NodeViewTestOps with
expectMsgType[SyntacticallySuccessfulModifier[ADProofs]]
}

private val t14 = TestCase("do not apply genesis block header if " +
private val t15 = TestCase("do not apply genesis block header if " +
"it's not equal to genesisId from config") { fixture =>
import fixture._
updateConfig(genesisIdConfig(modifierIdGen.sample))
Expand All @@ -341,7 +378,7 @@ class ErgoNodeViewHolderSpec extends ErgoPropertyTest with NodeViewTestOps with
getHistoryHeight shouldBe -1
}

private val t15 = TestCase("apply genesis block header if it's equal to genesisId from config") { fixture =>
private val t16 = TestCase("apply genesis block header if it's equal to genesisId from config") { fixture =>
import fixture._
val (us, bh) = createUtxoState(Some(nodeViewHolderRef))
val block = validFullBlock(None, us, bh)
Expand All @@ -359,7 +396,7 @@ class ErgoNodeViewHolderSpec extends ErgoPropertyTest with NodeViewTestOps with
getHeightOf(block.header.id) shouldBe Some(0)
}

val cases: List[TestCase] = List(t1, t2, t3, t4, t5, t6, t7, t8, t9)
val cases: List[TestCase] = List(t1, t2, t3, t4, t5, t6, t7, t8)
NodeViewTestConfig.allConfigs.foreach { c =>
cases.foreach { t =>
property(s"${t.name} - $c") {
Expand All @@ -368,7 +405,7 @@ class ErgoNodeViewHolderSpec extends ErgoPropertyTest with NodeViewTestOps with
}
}

val verifyingTxCases = List(t10, t11, t12, t13)
val verifyingTxCases = List(t9, t10, t11, t12, t13, t14)

NodeViewTestConfig.verifyTxConfigs.foreach { c =>
verifyingTxCases.foreach { t =>
Expand All @@ -378,7 +415,7 @@ class ErgoNodeViewHolderSpec extends ErgoPropertyTest with NodeViewTestOps with
}
}

val genesisIdTestCases = List(t14, t15)
val genesisIdTestCases = List(t15, t16)
def genesisIdConfig(expectedGenesisIdOpt: Option[ModifierId])(protoSettings: ErgoSettings): ErgoSettings = {
protoSettings.copy(chainSettings = protoSettings.chainSettings.copy(genesisId = expectedGenesisIdOpt))
}
Expand Down