From 5639545d691f56f11d38f12d46efbe5fc2cee34c Mon Sep 17 00:00:00 2001 From: Ivan Mashonskii Date: Wed, 4 Sep 2024 14:39:47 +0300 Subject: [PATCH] Create alt chain after invalid prevRandao (#4) --- src/main/scala/units/ELUpdater.scala | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/src/main/scala/units/ELUpdater.scala b/src/main/scala/units/ELUpdater.scala index 8c962488..4662a329 100644 --- a/src/main/scala/units/ELUpdater.scala +++ b/src/main/scala/units/ELUpdater.scala @@ -276,7 +276,7 @@ class ELUpdater( } } - private def checkRandao(networkBlock: NetworkL2Block, epochNumber: Int): Either[String, Unit] = + private def validateRandao(networkBlock: NetworkL2Block, epochNumber: Int): Either[String, Unit] = blockchain.vrf(epochNumber) match { case None => s"VRF of $epochNumber epoch is empty".asLeft case Some(vrf) => @@ -284,7 +284,7 @@ class ELUpdater( Either.cond( expectedPrevRandao == networkBlock.prevRandao, (), - s"expected prevRandao $expectedPrevRandao, got ${networkBlock.prevRandao}, VRF=$vrf of $epochNumber. Wrong epoch?" + s"expected prevRandao $expectedPrevRandao, got ${networkBlock.prevRandao}, VRF=$vrf of $epochNumber" ) } @@ -980,7 +980,7 @@ class ELUpdater( s"block miner ${networkBlock.minerRewardL2Address} doesn't equal to ${epochInfo.rewardAddress}" ) _ <- checkSignature(networkBlock, isConfirmed) - _ <- checkRandao(networkBlock, contractBlock.fold(epochInfo.number)(_.epoch)) + _ <- if (contractBlock.isEmpty) validateRandao(networkBlock, epochInfo.number) else Either.unit result <- preValidateWithdrawals(networkBlock, epochInfo, contractBlock) } yield result).leftMap { err => s"Block ${networkBlock.hash} validation error: $err, ignoring block" @@ -990,12 +990,14 @@ class ELUpdater( private def validateBlock( networkBlock: NetworkL2Block, parentEcBlock: EcBlock, + contractBlock: Option[ContractBlock], expectReward: Boolean, chainContractOptions: ChainContractOptions ): Job[Unit] = { (for { _ <- validateTimestamp(networkBlock, parentEcBlock) _ <- validateWithdrawals(networkBlock, parentEcBlock, expectReward, chainContractOptions) + _ <- contractBlock.fold(Either.unit[String])(cb => validateRandao(networkBlock, cb.epoch)) } yield ()).leftMap(err => ClientError(s"Network block ${networkBlock.hash} validation error: $err")) } @@ -1084,7 +1086,7 @@ class ELUpdater( logger.debug(err) case Right(preValidationResult) => val applyResult = for { - _ <- validateBlock(networkBlock, expectedParent, preValidationResult.expectReward, prevState.options) + _ <- validateBlock(networkBlock, expectedParent, contractBlock, preValidationResult.expectReward, prevState.options) _ = logger.debug(s"Block ${networkBlock.hash} was successfully validated, trying to apply and broadcast") _ <- engineApiClient.applyNewPayload(networkBlock.payload) } yield ()