forked from hyperledger/besu
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
After merge add a rule to check that the current block is more recent…
… than its parent (hyperledger#4066) * After merge add a rule to check that the current block is more recent than its parent Signed-off-by: Fabio Di Fabio <fabio.difabio@consensys.net> * Update CHANGELOG Signed-off-by: Fabio Di Fabio <fabio.difabio@consensys.net> * Unit test Signed-off-by: Fabio Di Fabio <fabio.difabio@consensys.net>
- Loading branch information
Showing
7 changed files
with
149 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
51 changes: 51 additions & 0 deletions
51
.../org/hyperledger/besu/consensus/merge/headervalidationrules/IncrementalTimestampRule.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
/* | ||
* 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.hyperledger.besu.consensus.merge.TransitionUtils.isTerminalProofOfWorkBlock; | ||
|
||
import org.hyperledger.besu.ethereum.ProtocolContext; | ||
import org.hyperledger.besu.ethereum.core.BlockHeader; | ||
|
||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
public class IncrementalTimestampRule extends MergeConsensusRule { | ||
|
||
private static final Logger LOG = LoggerFactory.getLogger(IncrementalTimestampRule.class); | ||
|
||
@Override | ||
public boolean validate( | ||
final BlockHeader header, final BlockHeader parent, final ProtocolContext protocolContext) { | ||
|
||
if (super.shouldUsePostMergeRules(header, protocolContext) | ||
&& !isTerminalProofOfWorkBlock(header, protocolContext)) { | ||
final long blockTimestamp = header.getTimestamp(); | ||
final long parentTimestamp = parent.getTimestamp(); | ||
final boolean isMoreRecent = blockTimestamp > parentTimestamp; | ||
|
||
LOG.trace( | ||
"Is block timestamp more recent that its parent? {}, [block timestamp {}, parent timestamp {}]", | ||
isMoreRecent, | ||
blockTimestamp, | ||
parentTimestamp); | ||
|
||
return isMoreRecent; | ||
} else { | ||
return true; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
89 changes: 89 additions & 0 deletions
89
...ledger/besu/consensus/merge/headervalidationrules/IncrementalTimestampValidationTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
/* | ||
* 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.ArgumentMatchers.any; | ||
import static org.mockito.Mockito.mock; | ||
import static org.mockito.Mockito.when; | ||
|
||
import org.hyperledger.besu.consensus.merge.MergeContext; | ||
import org.hyperledger.besu.ethereum.ProtocolContext; | ||
import org.hyperledger.besu.ethereum.chain.MutableBlockchain; | ||
import org.hyperledger.besu.ethereum.core.BlockHeader; | ||
import org.hyperledger.besu.ethereum.core.Difficulty; | ||
|
||
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 IncrementalTimestampValidationTest { | ||
|
||
@Mock private ProtocolContext protocolContext; | ||
@Mock private MutableBlockchain blockchain; | ||
@Mock private MergeContext mergeContext; | ||
|
||
@Before | ||
public void setUp() { | ||
when(blockchain.getTotalDifficultyByHash(any())).thenReturn(Optional.of(Difficulty.ONE)); | ||
when(protocolContext.getBlockchain()).thenReturn(blockchain); | ||
when(mergeContext.getTerminalTotalDifficulty()).thenReturn(Difficulty.ZERO); | ||
when(protocolContext.getConsensusContext(MergeContext.class)).thenReturn(mergeContext); | ||
} | ||
|
||
@Test | ||
public void validWhenTimestampMoreRecentThanParent() { | ||
final IncrementalTimestampRule rule = new IncrementalTimestampRule(); | ||
final long now = System.currentTimeMillis(); | ||
final BlockHeader parentHeader = mock(BlockHeader.class); | ||
when(parentHeader.getTimestamp()).thenReturn(now); | ||
final BlockHeader validHeader = mock(BlockHeader.class); | ||
when(validHeader.getNumber()).thenReturn(1337L); | ||
when(validHeader.getTimestamp()).thenReturn(now + 1); | ||
|
||
assertThat(rule.validate(validHeader, parentHeader, protocolContext)).isTrue(); | ||
} | ||
|
||
@Test | ||
public void invalidWhenTimestampNotMoreRecentThanParent() { | ||
final IncrementalTimestampRule rule = new IncrementalTimestampRule(); | ||
final long now = System.currentTimeMillis(); | ||
final BlockHeader parentHeader = mock(BlockHeader.class); | ||
when(parentHeader.getTimestamp()).thenReturn(now); | ||
final BlockHeader validHeader = mock(BlockHeader.class); | ||
when(validHeader.getNumber()).thenReturn(1337L); | ||
when(validHeader.getTimestamp()).thenReturn(now); | ||
|
||
assertThat(rule.validate(validHeader, parentHeader, protocolContext)).isFalse(); | ||
} | ||
|
||
@Test | ||
public void alwaysValidWhenTTDNotReached() { | ||
when(mergeContext.getTerminalTotalDifficulty()).thenReturn(Difficulty.of(2L)); | ||
|
||
final IncrementalTimestampRule rule = new IncrementalTimestampRule(); | ||
final BlockHeader parentHeader = mock(BlockHeader.class); | ||
final BlockHeader validHeader = mock(BlockHeader.class); | ||
when(validHeader.getNumber()).thenReturn(1337L); | ||
|
||
assertThat(rule.validate(validHeader, parentHeader, protocolContext)).isTrue(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters