Skip to content

Commit

Permalink
refactoring slashing risk actions (#7913)
Browse files Browse the repository at this point in the history
  • Loading branch information
mehdi-aouadi authored Jan 24, 2024
1 parent 37806d0 commit c2e9bb5
Show file tree
Hide file tree
Showing 17 changed files with 46 additions and 46 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,9 @@
import tech.pegasys.teku.validator.beaconnode.GenesisDataProvider;
import tech.pegasys.teku.validator.client.KeyManager;
import tech.pegasys.teku.validator.client.NoOpKeyManager;
import tech.pegasys.teku.validator.client.doppelganger.DoppelgangerDetectionAlert;
import tech.pegasys.teku.validator.client.restapi.ValidatorRestApi;
import tech.pegasys.teku.validator.client.restapi.ValidatorRestApiConfig;
import tech.pegasys.teku.validator.client.slashingriskactions.DoppelgangerDetectionAlert;

@Command(
name = "debug-tools",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
import tech.pegasys.teku.services.executionlayer.ExecutionLayerService;
import tech.pegasys.teku.services.powchain.PowchainService;
import tech.pegasys.teku.validator.client.ValidatorClientService;
import tech.pegasys.teku.validator.client.doppelganger.DoppelgangerDetectionShutDown;
import tech.pegasys.teku.validator.client.slashingriskactions.DoppelgangerDetectionShutDown;

public class BeaconNodeServiceController extends ServiceController {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
import tech.pegasys.teku.config.TekuConfiguration;
import tech.pegasys.teku.service.serviceutils.ServiceConfig;
import tech.pegasys.teku.validator.client.ValidatorClientService;
import tech.pegasys.teku.validator.client.doppelganger.DoppelgangerDetectionShutDown;
import tech.pegasys.teku.validator.client.slashingriskactions.DoppelgangerDetectionShutDown;

public class ValidatorNodeServiceController extends ServiceController {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,12 @@
import java.util.Optional;
import tech.pegasys.teku.bls.BLSPublicKey;
import tech.pegasys.teku.data.SlashingProtectionImporter;
import tech.pegasys.teku.validator.client.doppelganger.DoppelgangerDetectionAction;
import tech.pegasys.teku.validator.client.doppelganger.DoppelgangerDetector;
import tech.pegasys.teku.validator.client.restapi.apis.schema.DeleteKeysResponse;
import tech.pegasys.teku.validator.client.restapi.apis.schema.DeleteRemoteKeysResponse;
import tech.pegasys.teku.validator.client.restapi.apis.schema.ExternalValidator;
import tech.pegasys.teku.validator.client.restapi.apis.schema.PostKeyResult;
import tech.pegasys.teku.validator.client.slashingriskactions.SlashingRiskAction;

public interface KeyManager {
List<Validator> getLocalValidatorKeys();
Expand All @@ -42,10 +42,10 @@ List<PostKeyResult> importValidators(
final List<String> passwords,
final Optional<SlashingProtectionImporter> slashingProtectionImporter,
final Optional<DoppelgangerDetector> doppelgangerDetector,
final DoppelgangerDetectionAction doppelgangerDetectionAction);
final SlashingRiskAction doppelgangerDetectionAction);

List<PostKeyResult> importExternalValidators(
final List<ExternalValidator> validators,
final Optional<DoppelgangerDetector> doppelgangerDetector,
final DoppelgangerDetectionAction doppelgangerDetectionAction);
final SlashingRiskAction doppelgangerDetectionAction);
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,12 @@
import java.util.Optional;
import tech.pegasys.teku.bls.BLSPublicKey;
import tech.pegasys.teku.data.SlashingProtectionImporter;
import tech.pegasys.teku.validator.client.doppelganger.DoppelgangerDetectionAction;
import tech.pegasys.teku.validator.client.doppelganger.DoppelgangerDetector;
import tech.pegasys.teku.validator.client.restapi.apis.schema.DeleteKeysResponse;
import tech.pegasys.teku.validator.client.restapi.apis.schema.DeleteRemoteKeysResponse;
import tech.pegasys.teku.validator.client.restapi.apis.schema.ExternalValidator;
import tech.pegasys.teku.validator.client.restapi.apis.schema.PostKeyResult;
import tech.pegasys.teku.validator.client.slashingriskactions.SlashingRiskAction;

public class NoOpKeyManager implements KeyManager {

Expand Down Expand Up @@ -60,15 +60,15 @@ public List<PostKeyResult> importValidators(
final List<String> passwords,
final Optional<SlashingProtectionImporter> slashingProtectionImporter,
final Optional<DoppelgangerDetector> maybeDoppelgangerDetector,
final DoppelgangerDetectionAction doppelgangerDetectionAction) {
final SlashingRiskAction doppelgangerDetectionAction) {
return Collections.emptyList();
}

@Override
public List<PostKeyResult> importExternalValidators(
List<ExternalValidator> validators,
final Optional<DoppelgangerDetector> maybeDoppelgangerDetector,
final DoppelgangerDetectionAction doppelgangerDetectionAction) {
final SlashingRiskAction doppelgangerDetectionAction) {
return Collections.emptyList();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@
import tech.pegasys.teku.infrastructure.unsigned.UInt64;
import tech.pegasys.teku.spec.signatures.Signer;
import tech.pegasys.teku.validator.api.ValidatorTimingChannel;
import tech.pegasys.teku.validator.client.doppelganger.DoppelgangerDetectionAction;
import tech.pegasys.teku.validator.client.doppelganger.DoppelgangerDetector;
import tech.pegasys.teku.validator.client.loader.ValidatorLoader;
import tech.pegasys.teku.validator.client.restapi.apis.schema.DeleteKeyResult;
Expand All @@ -43,6 +42,7 @@
import tech.pegasys.teku.validator.client.restapi.apis.schema.ExternalValidator;
import tech.pegasys.teku.validator.client.restapi.apis.schema.ImportStatus;
import tech.pegasys.teku.validator.client.restapi.apis.schema.PostKeyResult;
import tech.pegasys.teku.validator.client.slashingriskactions.SlashingRiskAction;

public class OwnedKeyManager implements KeyManager {
private static final String EXPORT_FAILED =
Expand Down Expand Up @@ -227,7 +227,7 @@ public List<PostKeyResult> importValidators(
final List<String> passwords,
final Optional<SlashingProtectionImporter> slashingProtectionImporter,
final Optional<DoppelgangerDetector> maybeDoppelgangerDetector,
final DoppelgangerDetectionAction doppelgangerDetectionAction) {
final SlashingRiskAction doppelgangerDetectionAction) {
final List<LocalValidatorImportResult> importResults = new ArrayList<>();
boolean reloadRequired;

Expand Down Expand Up @@ -262,7 +262,7 @@ public List<PostKeyResult> importValidators(
}

private void handleValidatorsDoppelgangers(
final DoppelgangerDetectionAction doppelgangerDetectionAction,
final SlashingRiskAction doppelgangerDetectionAction,
final List<LocalValidatorImportResult> importResults,
final Map<UInt64, BLSPublicKey> doppelgangers) {
final List<BLSPublicKey> doppelgangerList = new ArrayList<>(doppelgangers.values());
Expand Down Expand Up @@ -382,7 +382,7 @@ private Set<BLSPublicKey> filterExternallyImportedPubKeys(
public List<PostKeyResult> importExternalValidators(
final List<ExternalValidator> validators,
final Optional<DoppelgangerDetector> maybeDoppelgangerDetector,
final DoppelgangerDetectionAction doppelgangerDetectionAction) {
final SlashingRiskAction doppelgangerDetectionAction) {
final List<ExternalValidatorImportResult> importResults = new ArrayList<>();
boolean reloadRequired = false;

Expand Down Expand Up @@ -448,7 +448,7 @@ public List<PostKeyResult> importExternalValidators(
}

private void handleExternalValidatorDoppelgangers(
final DoppelgangerDetectionAction doppelgangerDetectionAction,
final SlashingRiskAction doppelgangerDetectionAction,
final List<ExternalValidatorImportResult> importResults,
final Map<UInt64, BLSPublicKey> doppelgangers) {
final List<BLSPublicKey> doppelgangerList = new ArrayList<>(doppelgangers.values());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,6 @@
import tech.pegasys.teku.validator.api.ValidatorTimingChannel;
import tech.pegasys.teku.validator.beaconnode.BeaconNodeApi;
import tech.pegasys.teku.validator.beaconnode.GenesisDataProvider;
import tech.pegasys.teku.validator.client.doppelganger.DoppelgangerDetectionAction;
import tech.pegasys.teku.validator.client.doppelganger.DoppelgangerDetectionAlert;
import tech.pegasys.teku.validator.client.doppelganger.DoppelgangerDetector;
import tech.pegasys.teku.validator.client.duties.BeaconCommitteeSubscriptions;
import tech.pegasys.teku.validator.client.duties.BlockDutyFactory;
Expand All @@ -72,6 +70,8 @@
import tech.pegasys.teku.validator.client.restapi.ValidatorRestApiConfig;
import tech.pegasys.teku.validator.client.signer.BlockContainerSigner;
import tech.pegasys.teku.validator.client.signer.MilestoneBasedBlockContainerSigner;
import tech.pegasys.teku.validator.client.slashingriskactions.DoppelgangerDetectionAlert;
import tech.pegasys.teku.validator.client.slashingriskactions.SlashingRiskAction;
import tech.pegasys.teku.validator.eventadapter.InProcessBeaconNodeApi;
import tech.pegasys.teku.validator.remote.RemoteBeaconNodeApi;
import tech.pegasys.teku.validator.remote.sentry.SentryBeaconNodeApi;
Expand All @@ -94,7 +94,7 @@ public class ValidatorClientService extends Service {
private final ValidatorStatusProvider validatorStatusProvider;
private ValidatorIndexProvider validatorIndexProvider;
private Optional<DoppelgangerDetector> maybeDoppelgangerDetector = Optional.empty();
private final DoppelgangerDetectionAction doppelgangerDetectionAction;
private final SlashingRiskAction doppelgangerDetectionAction;
private Optional<RestApi> maybeValidatorRestApi = Optional.empty();
private final Optional<BeaconProposerPreparer> beaconProposerPreparer;
private final Optional<ValidatorRegistrator> validatorRegistrator;
Expand All @@ -115,7 +115,7 @@ private ValidatorClientService(
final Optional<ValidatorRegistrator> validatorRegistrator,
final Spec spec,
final MetricsSystem metricsSystem,
final DoppelgangerDetectionAction doppelgangerDetectionAction) {
final SlashingRiskAction doppelgangerDetectionAction) {
this.eventChannels = eventChannels;
this.validatorLoader = validatorLoader;
this.beaconNodeApi = beaconNodeApi;
Expand All @@ -132,7 +132,7 @@ private ValidatorClientService(
public static ValidatorClientService create(
final ServiceConfig services,
final ValidatorClientConfiguration config,
final DoppelgangerDetectionAction doppelgangerDetectionAction) {
final SlashingRiskAction doppelgangerDetectionAction) {
final EventChannels eventChannels = services.getEventChannels();
final ValidatorConfig validatorConfig = config.getValidatorConfig();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@
import tech.pegasys.teku.validator.client.ProposerConfigManager;
import tech.pegasys.teku.validator.client.ValidatorClientService;
import tech.pegasys.teku.validator.client.VoluntaryExitDataProvider;
import tech.pegasys.teku.validator.client.doppelganger.DoppelgangerDetectionAction;
import tech.pegasys.teku.validator.client.doppelganger.DoppelgangerDetector;
import tech.pegasys.teku.validator.client.restapi.apis.DeleteFeeRecipient;
import tech.pegasys.teku.validator.client.restapi.apis.DeleteGasLimit;
Expand All @@ -50,6 +49,7 @@
import tech.pegasys.teku.validator.client.restapi.apis.PostVoluntaryExit;
import tech.pegasys.teku.validator.client.restapi.apis.SetFeeRecipient;
import tech.pegasys.teku.validator.client.restapi.apis.SetGasLimit;
import tech.pegasys.teku.validator.client.slashingriskactions.SlashingRiskAction;

public class ValidatorRestApi {

Expand All @@ -68,7 +68,7 @@ public static RestApi create(
final DataDirLayout dataDirLayout,
final TimeProvider timeProvider,
final Optional<DoppelgangerDetector> maybeDoppelgangerDetector,
final DoppelgangerDetectionAction doppelgangerDetectionAction) {
final SlashingRiskAction doppelgangerDetectionAction) {
final VoluntaryExitDataProvider voluntaryExitDataProvider =
new VoluntaryExitDataProvider(
spec, keyManager, validatorApiChannel, genesisDataProvider, timeProvider);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,23 +31,23 @@
import tech.pegasys.teku.infrastructure.restapi.endpoints.RestApiEndpoint;
import tech.pegasys.teku.infrastructure.restapi.endpoints.RestApiRequest;
import tech.pegasys.teku.validator.client.KeyManager;
import tech.pegasys.teku.validator.client.doppelganger.DoppelgangerDetectionAction;
import tech.pegasys.teku.validator.client.doppelganger.DoppelgangerDetector;
import tech.pegasys.teku.validator.client.restapi.ValidatorTypes;
import tech.pegasys.teku.validator.client.restapi.apis.schema.PostKeysRequest;
import tech.pegasys.teku.validator.client.slashingriskactions.SlashingRiskAction;

public class PostKeys extends RestApiEndpoint {
private final KeyManager keyManager;
public static final String ROUTE = "/eth/v1/keystores";
private final Path slashingProtectionPath;
private final Optional<DoppelgangerDetector> maybeDoppelgangerDetector;
private final DoppelgangerDetectionAction doppelgangerDetectionAction;
private final SlashingRiskAction doppelgangerDetectionAction;

public PostKeys(
final KeyManager keyManager,
final Path slashingProtectionPath,
final Optional<DoppelgangerDetector> maybeDoppelgangerDetector,
final DoppelgangerDetectionAction doppelgangerDetectionAction) {
final SlashingRiskAction doppelgangerDetectionAction) {
super(
EndpointMetadata.post(ROUTE)
.operationId("ImportKeystores")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,11 @@
import tech.pegasys.teku.infrastructure.restapi.endpoints.RestApiEndpoint;
import tech.pegasys.teku.infrastructure.restapi.endpoints.RestApiRequest;
import tech.pegasys.teku.validator.client.KeyManager;
import tech.pegasys.teku.validator.client.doppelganger.DoppelgangerDetectionAction;
import tech.pegasys.teku.validator.client.doppelganger.DoppelgangerDetector;
import tech.pegasys.teku.validator.client.restapi.ValidatorTypes;
import tech.pegasys.teku.validator.client.restapi.apis.schema.ExternalValidator;
import tech.pegasys.teku.validator.client.restapi.apis.schema.PostRemoteKeysRequest;
import tech.pegasys.teku.validator.client.slashingriskactions.SlashingRiskAction;

public class PostRemoteKeys extends RestApiEndpoint {
public static final String ROUTE = "/eth/v1/remotekeys";
Expand All @@ -37,12 +37,12 @@ public class PostRemoteKeys extends RestApiEndpoint {
private final KeyManager keyManager;

private final Optional<DoppelgangerDetector> maybeDoppelgangerDetector;
private final DoppelgangerDetectionAction doppelgangerDetectionAction;
private final SlashingRiskAction doppelgangerDetectionAction;

public PostRemoteKeys(
final KeyManager keyManager,
final Optional<DoppelgangerDetector> maybeDoppelgangerDetector,
final DoppelgangerDetectionAction doppelgangerDetectionAction) {
final SlashingRiskAction doppelgangerDetectionAction) {
super(
EndpointMetadata.post(ROUTE)
.operationId("ImportRemoteKeys")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,14 @@
* specific language governing permissions and limitations under the License.
*/

package tech.pegasys.teku.validator.client.doppelganger;
package tech.pegasys.teku.validator.client.slashingriskactions;

import java.util.List;
import java.util.stream.Collectors;
import tech.pegasys.teku.bls.BLSPublicKey;
import tech.pegasys.teku.infrastructure.logging.StatusLogger;

public class DoppelgangerDetectionAlert implements DoppelgangerDetectionAction {
public class DoppelgangerDetectionAlert implements SlashingRiskAction {

private final StatusLogger statusLog;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,21 +11,21 @@
* specific language governing permissions and limitations under the License.
*/

package tech.pegasys.teku.validator.client.doppelganger;
package tech.pegasys.teku.validator.client.slashingriskactions;

import static tech.pegasys.teku.infrastructure.logging.StatusLogger.STATUS_LOG;

import java.util.List;
import java.util.stream.Collectors;
import tech.pegasys.teku.bls.BLSPublicKey;

public class DoppelgangerDetectionShutDown implements DoppelgangerDetectionAction {
public class DoppelgangerDetectionShutDown implements SlashingRiskAction {
@Override
public void perform(final List<BLSPublicKey> doppelgangers) {
STATUS_LOG.exitOnDoppelgangerDetected(
doppelgangers.stream()
.map(BLSPublicKey::toAbbreviatedString)
.collect(Collectors.joining(", ")));
System.exit(1);
shutdown();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,16 @@
* specific language governing permissions and limitations under the License.
*/

package tech.pegasys.teku.validator.client.doppelganger;
package tech.pegasys.teku.validator.client.slashingriskactions;

import java.util.List;
import tech.pegasys.teku.bls.BLSPublicKey;

public interface DoppelgangerDetectionAction {
public interface SlashingRiskAction {

void perform(final List<BLSPublicKey> doppelgangers);

default void shutdown() {
System.exit(2);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,6 @@
import tech.pegasys.teku.spec.signatures.Signer;
import tech.pegasys.teku.spec.util.DataStructureUtil;
import tech.pegasys.teku.validator.api.ValidatorTimingChannel;
import tech.pegasys.teku.validator.client.doppelganger.DoppelgangerDetectionAction;
import tech.pegasys.teku.validator.client.doppelganger.DoppelgangerDetector;
import tech.pegasys.teku.validator.client.loader.OwnedValidators;
import tech.pegasys.teku.validator.client.loader.ValidatorLoader;
Expand All @@ -65,6 +64,7 @@
import tech.pegasys.teku.validator.client.restapi.apis.schema.DeletionStatus;
import tech.pegasys.teku.validator.client.restapi.apis.schema.ExternalValidator;
import tech.pegasys.teku.validator.client.restapi.apis.schema.PostKeyResult;
import tech.pegasys.teku.validator.client.slashingriskactions.SlashingRiskAction;

class OwnedKeyManagerTest {

Expand All @@ -79,8 +79,7 @@ class OwnedKeyManagerTest {
private final ValidatorTimingChannel channel = mock(ValidatorTimingChannel.class);
private final OwnedKeyManager keyManager = new OwnedKeyManager(validatorLoader, channel);
private final DoppelgangerDetector doppelgangerDetector = mock(DoppelgangerDetector.class);
private final DoppelgangerDetectionAction doppelgangerDetectionAction =
mock(DoppelgangerDetectionAction.class);
private final SlashingRiskAction doppelgangerDetectionAction = mock(SlashingRiskAction.class);
private final BLSPublicKey doppelgangerPublicKey =
BLSPublicKey.fromSSZBytes(
Bytes.fromHexString(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
import tech.pegasys.teku.validator.beaconnode.GenesisDataProvider;
import tech.pegasys.teku.validator.client.OwnedKeyManager;
import tech.pegasys.teku.validator.client.ProposerConfigManager;
import tech.pegasys.teku.validator.client.doppelganger.DoppelgangerDetectionAction;
import tech.pegasys.teku.validator.client.slashingriskactions.SlashingRiskAction;

class ValidatorOpenApiTest {
private final ValidatorRestApiConfig config = mock(ValidatorRestApiConfig.class);
Expand All @@ -45,8 +45,7 @@ class ValidatorOpenApiTest {
private final OpenApiTestUtil<ValidatorOpenApiTest> util =
new OpenApiTestUtil<>(ValidatorOpenApiTest.class);
private JsonNode jsonNode;
private DoppelgangerDetectionAction doppelgangerDetectionAction =
mock(DoppelgangerDetectionAction.class);
private SlashingRiskAction doppelgangerDetectionAction = mock(SlashingRiskAction.class);

@BeforeEach
void setup() throws IOException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,13 @@
import tech.pegasys.teku.api.exceptions.BadRequestException;
import tech.pegasys.teku.infrastructure.restapi.endpoints.RestApiRequest;
import tech.pegasys.teku.validator.client.OwnedKeyManager;
import tech.pegasys.teku.validator.client.doppelganger.DoppelgangerDetectionAction;
import tech.pegasys.teku.validator.client.restapi.apis.schema.PostKeysRequest;
import tech.pegasys.teku.validator.client.slashingriskactions.SlashingRiskAction;

public class PostKeysTest {
private final OwnedKeyManager keyManager = mock(OwnedKeyManager.class);
private final RestApiRequest request = mock(RestApiRequest.class);
private final DoppelgangerDetectionAction doppelgangerDetectionAction =
mock(DoppelgangerDetectionAction.class);
private final SlashingRiskAction doppelgangerDetectionAction = mock(SlashingRiskAction.class);

@Test
void shouldRespondBadRequestIfPasswordsAndKeystoresMisMatch(@TempDir final Path tempDir)
Expand Down
Loading

0 comments on commit c2e9bb5

Please sign in to comment.