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

Fixed appending invalid block #117

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ trait BlockStorage extends ScorexLogging {
case Failure(e) =>
log.error("Failed to apply block to state", e)
db.rollback()
throw e
case Success(m) =>
db.commit()
}
Expand Down
4 changes: 3 additions & 1 deletion scorex-basics/src/test/scala/scorex/ScorexTestSuite.scala
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,14 @@ import scorex.account.AccountSpecification
import scorex.crypto.SigningFunctionsSpecification
import scorex.crypto.ads.merkle.{AuthDataBlockSpecification, MerkleSpecification, MerkleTreeStorageSpecification}
import scorex.network.HandshakeSpecification
import scorex.transaction.BlockStorageSpecification

class ScorexTestSuite extends Suites(
new AccountSpecification,
new AuthDataBlockSpecification,
new SigningFunctionsSpecification,
new MerkleSpecification,
new MerkleTreeStorageSpecification,
new HandshakeSpecification
new HandshakeSpecification,
new BlockStorageSpecification
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package scorex.transaction

import scala.util.{Failure, Success, Try}
import org.h2.mvstore.MVStore
import org.scalamock.scalatest.PathMockFactory
import org.scalatest.prop.{GeneratorDrivenPropertyChecks, PropertyChecks}
import org.scalatest.{Matchers, PropSpec}
import scorex.account.Account
import scorex.block.Block
import scorex.network.BlockSeq

class BlockStorageSpecification extends PropSpec with PropertyChecks with GeneratorDrivenPropertyChecks with Matchers with PathMockFactory {
private def mockHistory: History = {
val history = mock[History]
(history.appendBlock(_: Block)) expects * onCall { b: Block => Success(Seq(b)) } anyNumberOfTimes()
history
}

private class MockLagonakiState(v: Try[State]) extends LagonakiState {
override private[transaction] def processBlock(block: Block): Try[State] = v
override def validate(txs: Seq[Transaction], height: Option[Int]): Seq[Transaction] = ???
override def included(signature: Array[Byte], heightOpt: Option[Int]): Option[Int] = ???
override private[transaction] def rollbackTo(height: Int): State = ???
override def balance(account: Account, height: Option[Int]): Long = ???
override def balanceWithConfirmations(account: Account, confirmations: Int, heightOpt: Option[Int]): Long = ???
override def accountTransactions(account: Account): Array[_ <: Transaction] = ???
}

property("BlockStorage appendBlock should returns failed try when state.processBlock fails") {
val f = Failure(new IllegalStateException)

new BlockStorage {
override val history: History = mockHistory
override protected[this] val db: MVStore = new MVStore.Builder().open()
override val state: LagonakiState = new MockLagonakiState(f)
override val blockSeq: BlockSeq = null
}.appendBlock(/*i'm a block*/null) shouldBe f
}
}