diff --git a/src/it/scala/io/iohk/ethereum/ledger/BlockImporterItSpec.scala b/src/it/scala/io/iohk/ethereum/ledger/BlockImporterItSpec.scala index ad914d9c8f..88850b8b9e 100644 --- a/src/it/scala/io/iohk/ethereum/ledger/BlockImporterItSpec.scala +++ b/src/it/scala/io/iohk/ethereum/ledger/BlockImporterItSpec.scala @@ -129,7 +129,7 @@ class BlockImporterItSpec extends MockFactory with TestSetupWithVmAndValidators blockchain.getBestBlock().get shouldEqual oldBlock4 } - it should "return a correct new best block after reorganising longer chain to a shorter one" in { + it should "return a correct new best block after reorganising longer chain to a shorter one if its weight is bigger" in { //returning discarded initial chain blockchain.save(oldBlock2, Nil, oldWeight2, saveAsBestBlock = true) @@ -145,8 +145,8 @@ class BlockImporterItSpec extends MockFactory with TestSetupWithVmAndValidators it should "switch to a branch with a checkpoint" in { - val chackpoint = ObjectGenerators.fakeCheckpointGen(3, 3).sample.get - val oldBlock5WithCheckpoint: Block = checkpointBlockGenerator.generate(oldBlock4, chackpoint) + val checkpoint = ObjectGenerators.fakeCheckpointGen(3, 3).sample.get + val oldBlock5WithCheckpoint: Block = checkpointBlockGenerator.generate(oldBlock4, checkpoint) blockchain.save(oldBlock5WithCheckpoint, Nil, oldWeight4, saveAsBestBlock = true) val newBranch = List(newBlock2, newBlock3) @@ -158,10 +158,10 @@ class BlockImporterItSpec extends MockFactory with TestSetupWithVmAndValidators blockchain.getLatestCheckpointBlockNumber() shouldEqual oldBlock5WithCheckpoint.header.number } - it should "return a correct checkpointed block after reorganising longer chain to a shorter one and back" in { + it should "switch to a branch with a newer checkpoint" in { - val chackpoint = ObjectGenerators.fakeCheckpointGen(3, 3).sample.get - val newBlock4WithCheckpoint: Block = checkpointBlockGenerator.generate(newBlock3, chackpoint) + val checkpoint = ObjectGenerators.fakeCheckpointGen(3, 3).sample.get + val newBlock4WithCheckpoint: Block = checkpointBlockGenerator.generate(newBlock3, checkpoint) blockchain.save(newBlock4WithCheckpoint, Nil, newWeight3, saveAsBestBlock = true) val newBranch = List(newBlock4WithCheckpoint) @@ -173,7 +173,7 @@ class BlockImporterItSpec extends MockFactory with TestSetupWithVmAndValidators blockchain.getLatestCheckpointBlockNumber() shouldEqual newBlock4WithCheckpoint.header.number } - it should "return a correct checkpointed block after receiving a new chackpoint from morpho" in { + it should "return a correct checkpointed block after receiving a request for generating a new checkpoint" in { val parent = blockchain.getBestBlock().get val newBlock5: Block = getBlock(genesisBlock.number + 5, difficulty = 104, parent = parent.header.hash) diff --git a/src/main/scala/io/iohk/ethereum/ledger/BlockExecution.scala b/src/main/scala/io/iohk/ethereum/ledger/BlockExecution.scala index df05e860c0..f2587305b6 100644 --- a/src/main/scala/io/iohk/ethereum/ledger/BlockExecution.scala +++ b/src/main/scala/io/iohk/ethereum/ledger/BlockExecution.scala @@ -6,9 +6,7 @@ import io.iohk.ethereum.ledger.Ledger.BlockResult import io.iohk.ethereum.utils.{BlockchainConfig, DaoForkConfig, Logger} import io.iohk.ethereum.vm.EvmConfig import scala.annotation.tailrec -import scala.util.Try -import akka.util.ByteString -import io.iohk.ethereum.mpt.MerklePatriciaTrie.MissingNodeException +import cats.implicits._ class BlockExecution( blockchain: BlockchainImpl, @@ -62,9 +60,8 @@ class BlockExecution( .getBlockHeaderByHash(block.header.parentHash) .toRight(MissingParentError) // Should not never occur because validated earlier execResult <- executeBlockTransactions(block, parent) - worldToPersist <- Try { - blockPreparator.payBlockReward(block, execResult.worldState) - }.toEither.left.map(BlockExecutionError.MPTError(_)) + worldToPersist <- Either.catchOnly[Throwable](blockPreparator.payBlockReward(block, execResult.worldState)) + .leftMap(BlockExecutionError.MPTError.apply) // State root hash needs to be up-to-date for validateBlockAfterExecution worldPersisted = InMemoryWorldStateProxy.persistState(worldToPersist) } yield execResult.copy(worldState = worldPersisted) @@ -174,21 +171,21 @@ sealed trait BlockExecutionError { sealed trait BlockExecutionSuccess -case object BlockExecutionSuccess extends BlockExecutionSuccess +final case object BlockExecutionSuccess extends BlockExecutionSuccess object BlockExecutionError { - case class ValidationBeforeExecError(reason: Any) extends BlockExecutionError + final case class ValidationBeforeExecError(reason: Any) extends BlockExecutionError - case class StateBeforeFailure(worldState: InMemoryWorldStateProxy, acumGas: BigInt, acumReceipts: Seq[Receipt]) + final case class StateBeforeFailure(worldState: InMemoryWorldStateProxy, acumGas: BigInt, acumReceipts: Seq[Receipt]) - case class TxsExecutionError(stx: SignedTransaction, stateBeforeError: StateBeforeFailure, reason: String) + final case class TxsExecutionError(stx: SignedTransaction, stateBeforeError: StateBeforeFailure, reason: String) extends BlockExecutionError - case class ValidationAfterExecError(reason: String) extends BlockExecutionError + final case class ValidationAfterExecError(reason: String) extends BlockExecutionError - case object MissingParentError extends BlockExecutionError { + final case object MissingParentError extends BlockExecutionError { override val reason: Any = "Cannot find parent" } - case class MPTError(reason: Throwable) extends BlockExecutionError + final case class MPTError(reason: Throwable) extends BlockExecutionError }