forked from Consensys/teku
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added more utilities for late block reorg (Consensys#7741)
* Added more utilities for late block reorg - Added validator_is_connected - added is_timely Currently Timeliness is going to be enabled, adding to a small cache. This is minimal overhead. partially addresses Consensys#6595 Signed-off-by: Paul Harris <[email protected]>
- Loading branch information
Showing
16 changed files
with
333 additions
and
0 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
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
82 changes: 82 additions & 0 deletions
82
.../src/test/java/tech/pegasys/teku/statetransition/forkchoice/ProposersDataManagerTest.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,82 @@ | ||
/* | ||
* Copyright Consensys Software Inc., 2023 | ||
* | ||
* 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. | ||
*/ | ||
|
||
package tech.pegasys.teku.statetransition.forkchoice; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
import static org.mockito.Mockito.mock; | ||
|
||
import java.util.List; | ||
import java.util.Optional; | ||
import org.hyperledger.besu.metrics.noop.NoOpMetricsSystem; | ||
import org.hyperledger.besu.plugin.services.MetricsSystem; | ||
import org.junit.jupiter.api.Test; | ||
import tech.pegasys.teku.ethereum.execution.types.Eth1Address; | ||
import tech.pegasys.teku.infrastructure.async.eventthread.EventThread; | ||
import tech.pegasys.teku.infrastructure.unsigned.UInt64; | ||
import tech.pegasys.teku.spec.Spec; | ||
import tech.pegasys.teku.spec.TestSpecFactory; | ||
import tech.pegasys.teku.spec.datastructures.operations.versions.bellatrix.BeaconPreparableProposer; | ||
import tech.pegasys.teku.spec.executionlayer.ExecutionLayerChannel; | ||
import tech.pegasys.teku.spec.util.DataStructureUtil; | ||
import tech.pegasys.teku.storage.client.RecentChainData; | ||
|
||
class ProposersDataManagerTest { | ||
|
||
private final Spec spec = TestSpecFactory.createMinimalCapella(); | ||
|
||
private final DataStructureUtil dataStructureUtil = new DataStructureUtil(spec); | ||
|
||
private final RecentChainData recentChainData = mock(RecentChainData.class); | ||
|
||
private final ExecutionLayerChannel channel = ExecutionLayerChannel.NOOP; | ||
private final MetricsSystem metricsSystem = new NoOpMetricsSystem(); | ||
|
||
private final Eth1Address defaultAddress = dataStructureUtil.randomEth1Address(); | ||
private final ProposersDataManager manager = | ||
new ProposersDataManager( | ||
mock(EventThread.class), | ||
spec, | ||
metricsSystem, | ||
channel, | ||
recentChainData, | ||
Optional.of(defaultAddress)); | ||
|
||
final List<BeaconPreparableProposer> proposers = | ||
List.of( | ||
new BeaconPreparableProposer(UInt64.ONE, dataStructureUtil.randomEth1Address()), | ||
new BeaconPreparableProposer(UInt64.ZERO, defaultAddress)); | ||
|
||
@Test | ||
void validatorIsConnected_notFound_withEmptyPreparedList() { | ||
assertThat(manager.validatorIsConnected(UInt64.ZERO, UInt64.ZERO)).isFalse(); | ||
} | ||
|
||
@Test | ||
void validatorIsConnected_found_withPreparedProposer() { | ||
manager.updatePreparedProposers(proposers, UInt64.ONE); | ||
assertThat(manager.validatorIsConnected(UInt64.ONE, UInt64.valueOf(1))).isTrue(); | ||
} | ||
|
||
@Test | ||
void validatorIsConnected_notFound_withDifferentPreparedProposer() { | ||
manager.updatePreparedProposers(proposers, UInt64.ONE); | ||
assertThat(manager.validatorIsConnected(UInt64.valueOf(2), UInt64.valueOf(2))).isFalse(); | ||
} | ||
|
||
@Test | ||
void validatorIsConnected_notFound_withExpiredPreparedProposer() { | ||
manager.updatePreparedProposers(proposers, UInt64.ONE); | ||
assertThat(manager.validatorIsConnected(UInt64.ONE, UInt64.valueOf(26))).isFalse(); | ||
} | ||
} |
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
93 changes: 93 additions & 0 deletions
93
storage/src/main/java/tech/pegasys/teku/storage/client/BlockTimelinessTracker.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,93 @@ | ||
/* | ||
* Copyright Consensys Software Inc., 2023 | ||
* | ||
* 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. | ||
*/ | ||
|
||
package tech.pegasys.teku.storage.client; | ||
|
||
import static tech.pegasys.teku.spec.constants.NetworkConstants.INTERVALS_PER_SLOT; | ||
|
||
import java.util.Map; | ||
import java.util.Optional; | ||
import org.apache.logging.log4j.LogManager; | ||
import org.apache.logging.log4j.Logger; | ||
import org.apache.tuweni.bytes.Bytes32; | ||
import tech.pegasys.teku.infrastructure.collections.LimitedMap; | ||
import tech.pegasys.teku.infrastructure.time.TimeProvider; | ||
import tech.pegasys.teku.infrastructure.unsigned.UInt64; | ||
import tech.pegasys.teku.spec.Spec; | ||
import tech.pegasys.teku.spec.datastructures.blocks.SignedBeaconBlock; | ||
|
||
public class BlockTimelinessTracker { | ||
private static final Logger LOG = LogManager.getLogger(); | ||
private final Map<Bytes32, Boolean> blockTimeliness; | ||
private final TimeProvider timeProvider; | ||
private final Spec spec; | ||
private final RecentChainData recentChainData; | ||
|
||
// implements is_timely from Consensus Spec | ||
public BlockTimelinessTracker( | ||
final Spec spec, final RecentChainData recentChainData, final TimeProvider timeProvider) { | ||
this.spec = spec; | ||
final int epochsForTimeliness = | ||
Math.max(spec.getGenesisSpecConfig().getReorgMaxEpochsSinceFinalization(), 3); | ||
this.blockTimeliness = | ||
LimitedMap.createSynchronizedNatural( | ||
spec.getGenesisSpec().getSlotsPerEpoch() * epochsForTimeliness); | ||
this.timeProvider = timeProvider; | ||
this.recentChainData = recentChainData; | ||
} | ||
|
||
public void setBlockTimelinessFromArrivalTime( | ||
final SignedBeaconBlock block, final UInt64 arrivalTimeMillis) { | ||
final UInt64 genesisTime = recentChainData.getGenesisTime(); | ||
final UInt64 computedSlot = spec.getCurrentSlot(timeProvider.getTimeInSeconds(), genesisTime); | ||
final Bytes32 root = block.getRoot(); | ||
if (computedSlot.isGreaterThan(block.getMessage().getSlot())) { | ||
LOG.debug( | ||
"Block {}:{} is before computed slot {}, timeliness set to false.", | ||
root, | ||
block.getSlot(), | ||
computedSlot); | ||
blockTimeliness.put(root, false); | ||
return; | ||
} | ||
recentChainData | ||
.getCurrentSlot() | ||
.ifPresent( | ||
slot -> { | ||
final UInt64 slotStartTimeMillis = | ||
spec.getSlotStartTimeMillis(slot, genesisTime.times(1000)); | ||
final int millisIntoSlot = | ||
arrivalTimeMillis.minusMinZero(slotStartTimeMillis).intValue(); | ||
|
||
final UInt64 timelinessLimit = | ||
spec.getMillisPerSlot(slot).dividedBy(INTERVALS_PER_SLOT); | ||
|
||
final boolean isTimely = | ||
block.getMessage().getSlot().equals(slot) | ||
&& timelinessLimit.isGreaterThan(millisIntoSlot); | ||
LOG.debug( | ||
"Block {}:{} arrived at {} ms into slot {}, timeliness limit is {} ms. result: {}", | ||
root, | ||
block.getSlot(), | ||
millisIntoSlot, | ||
computedSlot, | ||
timelinessLimit, | ||
isTimely); | ||
blockTimeliness.put(root, isTimely); | ||
}); | ||
} | ||
|
||
public Optional<Boolean> isBlockTimely(final Bytes32 root) { | ||
return Optional.ofNullable(blockTimeliness.get(root)); | ||
} | ||
} |
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
Oops, something went wrong.