|
| 1 | +/* |
| 2 | + * Copyright Consensys Software Inc., 2025 |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with |
| 5 | + * the License. You may obtain a copy of the License at |
| 6 | + * |
| 7 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | + * |
| 9 | + * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on |
| 10 | + * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the |
| 11 | + * specific language governing permissions and limitations under the License. |
| 12 | + */ |
| 13 | + |
| 14 | +package tech.pegasys.teku.beacon.sync.forward.multipeer; |
| 15 | + |
| 16 | +import static org.mockito.Mockito.mock; |
| 17 | +import static org.mockito.Mockito.verify; |
| 18 | +import static org.mockito.Mockito.verifyNoInteractions; |
| 19 | +import static org.mockito.Mockito.when; |
| 20 | + |
| 21 | +import java.util.Optional; |
| 22 | +import org.junit.jupiter.api.Test; |
| 23 | +import tech.pegasys.teku.infrastructure.unsigned.UInt64; |
| 24 | +import tech.pegasys.teku.spec.Spec; |
| 25 | +import tech.pegasys.teku.spec.TestSpecFactory; |
| 26 | +import tech.pegasys.teku.spec.datastructures.blocks.SignedBeaconBlock; |
| 27 | +import tech.pegasys.teku.spec.datastructures.blocks.SignedBlockAndState; |
| 28 | +import tech.pegasys.teku.spec.util.DataStructureUtil; |
| 29 | +import tech.pegasys.teku.statetransition.forkchoice.ForkChoiceTrigger; |
| 30 | +import tech.pegasys.teku.storage.client.ChainHead; |
| 31 | +import tech.pegasys.teku.storage.client.RecentChainData; |
| 32 | + |
| 33 | +public class SyncReorgManagerTest { |
| 34 | + private final Spec spec = TestSpecFactory.createMinimalPhase0(); |
| 35 | + private final DataStructureUtil dataStructureUtil = new DataStructureUtil(spec); |
| 36 | + |
| 37 | + private final RecentChainData recentChainData = mock(RecentChainData.class); |
| 38 | + private final ForkChoiceTrigger forkChoiceTrigger = mock(ForkChoiceTrigger.class); |
| 39 | + |
| 40 | + private final SyncReorgManager syncReorgManager = |
| 41 | + new SyncReorgManager(recentChainData, forkChoiceTrigger); |
| 42 | + |
| 43 | + @Test |
| 44 | + public void onBlocksImported_shouldDoNothingIfNoCurrentHead() { |
| 45 | + when(recentChainData.getChainHead()).thenReturn(Optional.empty()); |
| 46 | + syncReorgManager.onBlocksImported(dataStructureUtil.randomSignedBeaconBlock()); |
| 47 | + |
| 48 | + verifyNoInteractions(forkChoiceTrigger); |
| 49 | + } |
| 50 | + |
| 51 | + @Test |
| 52 | + public void onBlocksImported_shouldDoNothingIfLastImportedBlockIsCurrentHead() { |
| 53 | + final SignedBlockAndState headBlock = dataStructureUtil.randomSignedBlockAndState(UInt64.ONE); |
| 54 | + when(recentChainData.getChainHead()).thenReturn(Optional.of(ChainHead.create(headBlock))); |
| 55 | + syncReorgManager.onBlocksImported(headBlock.getBlock()); |
| 56 | + |
| 57 | + verifyNoInteractions(forkChoiceTrigger); |
| 58 | + } |
| 59 | + |
| 60 | + @Test |
| 61 | + public void onBlocksImported_shouldDoNothingIfLastImportedBlockIsWithinReorgThreshold() { |
| 62 | + final SignedBlockAndState headBlock = dataStructureUtil.randomSignedBlockAndState(UInt64.ONE); |
| 63 | + final SignedBeaconBlock lastImportedBlock = |
| 64 | + dataStructureUtil.randomSignedBeaconBlock(UInt64.valueOf(10)); |
| 65 | + when(recentChainData.getChainHead()).thenReturn(Optional.of(ChainHead.create(headBlock))); |
| 66 | + syncReorgManager.onBlocksImported(lastImportedBlock); |
| 67 | + |
| 68 | + verifyNoInteractions(forkChoiceTrigger); |
| 69 | + } |
| 70 | + |
| 71 | + @Test |
| 72 | + public void onBlocksImported_shouldTriggerReorgWhenLastImportedBlockIsOutsideReorgThreshold() { |
| 73 | + final SignedBlockAndState headBlock = dataStructureUtil.randomSignedBlockAndState(UInt64.ONE); |
| 74 | + final SignedBeaconBlock lastImportedBlock = |
| 75 | + dataStructureUtil.randomSignedBeaconBlock(UInt64.valueOf(11)); |
| 76 | + when(recentChainData.getChainHead()).thenReturn(Optional.of(ChainHead.create(headBlock))); |
| 77 | + syncReorgManager.onBlocksImported(lastImportedBlock); |
| 78 | + |
| 79 | + verify(forkChoiceTrigger).reorgWhileSyncing(headBlock.getRoot(), lastImportedBlock.getRoot()); |
| 80 | + } |
| 81 | +} |
0 commit comments