-
Notifications
You must be signed in to change notification settings - Fork 313
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
create EphemerySlotValidationService and test (#8759)
Signed-off-by: gconnect <agatevureglory@gmail.com> Co-authored-by: Paul Harris <paul.harris@consensys.net>
- Loading branch information
Showing
5 changed files
with
141 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
20 changes: 20 additions & 0 deletions
20
...hain/src/main/java/tech/pegasys/teku/services/beaconchain/EphemeryLifecycleException.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,20 @@ | ||
/* | ||
* Copyright Consensys Software Inc., 2024 | ||
* | ||
* 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.services.beaconchain; | ||
|
||
public class EphemeryLifecycleException extends RuntimeException { | ||
public EphemeryLifecycleException(final String format) { | ||
super(format); | ||
} | ||
} |
44 changes: 44 additions & 0 deletions
44
...n/src/main/java/tech/pegasys/teku/services/beaconchain/EphemerySlotValidationService.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,44 @@ | ||
/* | ||
* Copyright Consensys Software Inc., 2024 | ||
* | ||
* 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.services.beaconchain; | ||
|
||
import static tech.pegasys.teku.networks.EphemeryNetwork.MAX_EPHEMERY_SLOT; | ||
|
||
import tech.pegasys.teku.ethereum.events.SlotEventsChannel; | ||
import tech.pegasys.teku.infrastructure.async.SafeFuture; | ||
import tech.pegasys.teku.infrastructure.unsigned.UInt64; | ||
import tech.pegasys.teku.service.serviceutils.Service; | ||
|
||
public class EphemerySlotValidationService extends Service implements SlotEventsChannel { | ||
|
||
@Override | ||
public void onSlot(final UInt64 slot) { | ||
if (slot.isGreaterThan(MAX_EPHEMERY_SLOT)) { | ||
throw new EphemeryLifecycleException( | ||
String.format( | ||
"Slot %s exceeds maximum allowed slot %s for ephemery network", | ||
slot, MAX_EPHEMERY_SLOT)); | ||
} | ||
} | ||
|
||
@Override | ||
protected SafeFuture<?> doStart() { | ||
return SafeFuture.COMPLETE; | ||
} | ||
|
||
@Override | ||
protected SafeFuture<?> doStop() { | ||
return SafeFuture.COMPLETE; | ||
} | ||
} |
63 changes: 63 additions & 0 deletions
63
...c/test/java/tech/pegasys/teku/services/beaconchain/EphemerySlotValidationServiceTest.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,63 @@ | ||
/* | ||
* Copyright Consensys Software Inc., 2024 | ||
* | ||
* 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.services.beaconchain; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
import static org.assertj.core.api.AssertionsForClassTypes.assertThatThrownBy; | ||
import static org.junit.jupiter.api.Assertions.assertTrue; | ||
import static tech.pegasys.teku.networks.EphemeryNetwork.MAX_EPHEMERY_SLOT; | ||
|
||
import java.util.Optional; | ||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Test; | ||
import tech.pegasys.teku.infrastructure.async.SafeFuture; | ||
import tech.pegasys.teku.infrastructure.unsigned.UInt64; | ||
import tech.pegasys.teku.spec.networks.Eth2Network; | ||
|
||
class EphemerySlotValidationServiceTest { | ||
private EphemerySlotValidationService ephemerySlotValidationService; | ||
|
||
@BeforeEach | ||
void setUp() { | ||
ephemerySlotValidationService = new EphemerySlotValidationService(); | ||
} | ||
|
||
@Test | ||
public void onSlot_shouldNotThrow_whenSlotIsValid() { | ||
ephemerySlotValidationService.onSlot(UInt64.valueOf(MAX_EPHEMERY_SLOT)); | ||
} | ||
|
||
@Test | ||
public void onSlot_shouldThrowException_whenSlotExceedsMaxEphemerySlot_forEphemeryNetwork() { | ||
final Eth2Network network = Eth2Network.EPHEMERY; | ||
final Optional<Eth2Network> ephemeryNetwork = Optional.of(network); | ||
final UInt64 invalidSlot = UInt64.valueOf(MAX_EPHEMERY_SLOT + 1); | ||
|
||
assertThat(ephemeryNetwork).contains(Eth2Network.EPHEMERY); | ||
assertThatThrownBy(() -> ephemerySlotValidationService.onSlot(invalidSlot)) | ||
.isInstanceOf(EphemeryLifecycleException.class) | ||
.hasMessageContaining( | ||
String.format( | ||
"Slot %s exceeds maximum allowed slot %s for ephemery network", | ||
invalidSlot, MAX_EPHEMERY_SLOT)); | ||
} | ||
|
||
@Test | ||
void shouldCompleteWhenServiceStartsAndStops() { | ||
final SafeFuture<?> startFuture = ephemerySlotValidationService.doStart(); | ||
assertTrue(startFuture.isDone()); | ||
final SafeFuture<?> stopFuture = ephemerySlotValidationService.doStop(); | ||
assertTrue(stopFuture.isDone()); | ||
} | ||
} |
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