remove MergeUnfinalizedRule since consensus layer should be allowed to execute a reorg around an already finalized block. (#4240)

Signed-off-by: garyschulte <garyschulte@gmail.com>
pull/4224/head
garyschulte 2 years ago committed by GitHub
parent 37e0e0478e
commit f43b9ade7e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 2
      consensus/merge/src/main/java/org/hyperledger/besu/consensus/merge/MergeValidationRulesetFactory.java
  2. 50
      consensus/merge/src/main/java/org/hyperledger/besu/consensus/merge/headervalidationrules/MergeUnfinalizedValidationRule.java
  3. 67
      consensus/merge/src/test/java/org/hyperledger/besu/consensus/merge/headervalidationrules/MergeUnfinalizedValidationRuleTest.java

@ -19,7 +19,6 @@ import static org.hyperledger.besu.ethereum.mainnet.MainnetBlockHeaderValidator.
import org.hyperledger.besu.consensus.merge.headervalidationrules.ConstantOmmersHashRule;
import org.hyperledger.besu.consensus.merge.headervalidationrules.IncrementalTimestampRule;
import org.hyperledger.besu.consensus.merge.headervalidationrules.MergeUnfinalizedValidationRule;
import org.hyperledger.besu.consensus.merge.headervalidationrules.NoDifficultyRule;
import org.hyperledger.besu.consensus.merge.headervalidationrules.NoNonceRule;
import org.hyperledger.besu.ethereum.core.BlockHeader;
@ -70,7 +69,6 @@ public class MergeValidationRulesetFactory {
.addRule(new TimestampBoundedByFutureParameter(TIMESTAMP_TOLERANCE_S))
.addRule(new ExtraDataMaxLengthValidationRule(BlockHeader.MAX_EXTRA_DATA_BYTES))
.addRule((new BaseFeeMarketBlockHeaderGasPriceValidationRule(baseFeeMarket)))
.addRule(new MergeUnfinalizedValidationRule())
.addRule(new ConstantOmmersHashRule())
.addRule(new NoNonceRule())
.addRule(new NoDifficultyRule())

@ -1,50 +0,0 @@
/*
* Copyright Hyperledger Besu Contributors.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
* specific language governing permissions and limitations under the License.
*
* SPDX-License-Identifier: Apache-2.0
*/
package org.hyperledger.besu.consensus.merge.headervalidationrules;
import org.hyperledger.besu.consensus.merge.MergeContext;
import org.hyperledger.besu.ethereum.ProtocolContext;
import org.hyperledger.besu.ethereum.core.BlockHeader;
import org.hyperledger.besu.ethereum.mainnet.AttachedBlockHeaderValidationRule;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class MergeUnfinalizedValidationRule implements AttachedBlockHeaderValidationRule {
private static final Logger LOG = LoggerFactory.getLogger(MergeUnfinalizedValidationRule.class);
@Override
public boolean validate(
final BlockHeader header, final BlockHeader parent, final ProtocolContext protocolContext) {
MergeContext mergeContext = protocolContext.getConsensusContext(MergeContext.class);
// if we have a finalized blockheader, fail this rule if
// the block number is lower than finalized
// or block number is the same but hash is different
if (mergeContext
.getFinalized()
.filter(finalized -> header.getNumber() <= finalized.getNumber())
.filter(finalized -> !header.getHash().equals(finalized.getHash()))
.isPresent()) {
LOG.warn(
"BlockHeader {} failed validation due to block {} already finalized",
header.toLogString(),
mergeContext.getFinalized().map(BlockHeader::toLogString).orElse("{}"));
return false;
}
return true;
}
}

@ -1,67 +0,0 @@
/*
* Copyright Hyperledger Besu Contributors.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
* specific language governing permissions and limitations under the License.
*
* SPDX-License-Identifier: Apache-2.0
*/
package org.hyperledger.besu.consensus.merge.headervalidationrules;
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
import org.hyperledger.besu.consensus.merge.MergeContext;
import org.hyperledger.besu.datatypes.Hash;
import org.hyperledger.besu.ethereum.ProtocolContext;
import org.hyperledger.besu.ethereum.core.BlockHeader;
import java.util.Optional;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.junit.MockitoJUnitRunner;
@RunWith(MockitoJUnitRunner.class)
public class MergeUnfinalizedValidationRuleTest {
@Mock private ProtocolContext protocolContext;
@Mock private MergeContext mergeContext;
@Mock private BlockHeader finalizedHeader;
final MergeUnfinalizedValidationRule rule = new MergeUnfinalizedValidationRule();
@Before
public void setUp() {
when(protocolContext.getConsensusContext(MergeContext.class)).thenReturn(mergeContext);
when(finalizedHeader.getNumber()).thenReturn(50L);
when(finalizedHeader.getHash()).thenReturn(Hash.ZERO);
when(mergeContext.getFinalized()).thenReturn(Optional.of(finalizedHeader));
}
@Test
public void shouldFailBlocksPriorToFinalized() {
final BlockHeader invalidHeader = mock(BlockHeader.class);
when(invalidHeader.getHash()).thenReturn(Hash.fromHexStringLenient("0x1337"));
when(invalidHeader.getNumber()).thenReturn(1L);
assertThat(rule.validate(invalidHeader, mock(BlockHeader.class), protocolContext)).isFalse();
}
@Test
public void shouldPassBlocksWithSameHashAsFinalized() {
final BlockHeader duplicateOfFinalized = mock(BlockHeader.class);
when(duplicateOfFinalized.getHash()).thenReturn(Hash.ZERO);
when(duplicateOfFinalized.getNumber()).thenReturn(50L);
assertThat(rule.validate(duplicateOfFinalized, mock(BlockHeader.class), protocolContext))
.isTrue();
}
}
Loading…
Cancel
Save