mirror of https://github.com/hyperledger/besu
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
parent
37e0e0478e
commit
f43b9ade7e
@ -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…
Reference in new issue