Skip to content

Commit

Permalink
handle slashing sse events
Browse files Browse the repository at this point in the history
  • Loading branch information
mehdi-aouadi committed Jan 24, 2024
1 parent fcb9e5c commit 9806cd6
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,7 @@ public static BeaconNodeApi create(
validatorTimingChannel,
metricsSystem,
validatorConfig.generateEarlyAttestations(),
validatorConfig.isShutdownWhenValidatorSlashedEnabled(),
spec);

eventChannels.subscribe(BeaconNodeReadinessChannel.class, beaconChainEventAdapter);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import com.launchdarkly.eventsource.background.BackgroundEventSource;
import com.launchdarkly.eventsource.background.ConnectionErrorHandler.Action;
import java.time.Duration;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.concurrent.CountDownLatch;
Expand Down Expand Up @@ -62,6 +63,8 @@ public class EventSourceBeaconChainEventAdapter
private final BeaconChainEventAdapter timeBasedEventAdapter;
private final EventSourceHandler eventSourceHandler;

private final boolean shutdownWhenValidatorSlashedEnabled;

public EventSourceBeaconChainEventAdapter(
final BeaconNodeReadinessManager beaconNodeReadinessManager,
final RemoteValidatorApiChannel primaryBeaconNodeApi,
Expand All @@ -72,6 +75,7 @@ public EventSourceBeaconChainEventAdapter(
final ValidatorTimingChannel validatorTimingChannel,
final MetricsSystem metricsSystem,
final boolean generateEarlyAttestations,
final boolean shutdownWhenValidatorSlashedEnabled,
final Spec spec) {
this.beaconNodeReadinessManager = beaconNodeReadinessManager;
this.primaryBeaconNodeApi = primaryBeaconNodeApi;
Expand All @@ -82,6 +86,7 @@ public EventSourceBeaconChainEventAdapter(
this.eventSourceHandler =
new EventSourceHandler(
validatorTimingChannel, metricsSystem, generateEarlyAttestations, spec);
this.shutdownWhenValidatorSlashedEnabled = shutdownWhenValidatorSlashedEnabled;
}

@Override
Expand Down Expand Up @@ -128,7 +133,16 @@ public void onPrimaryNodeBackReady() {
}

private BackgroundEventSource createEventSource(final RemoteValidatorApiChannel beaconNodeApi) {
final HttpUrl eventSourceUrl = createHeadEventSourceUrl(beaconNodeApi.getEndpoint());

final List<EventType> eventTypes = new ArrayList<>();
eventTypes.add(EventType.head);
if (shutdownWhenValidatorSlashedEnabled) {
eventTypes.add(EventType.attester_slashing);
eventTypes.add(EventType.proposer_slashing);
}
final HttpUrl eventSourceUrl =
createEventStreamSourceUrl(beaconNodeApi.getEndpoint(), eventTypes);

final EventSource.Builder eventSourceBuilder =
new EventSource.Builder(ConnectStrategy.http(eventSourceUrl).httpClient(okHttpClient))
.retryDelayStrategy(
Expand All @@ -143,10 +157,13 @@ private BackgroundEventSource createEventSource(final RemoteValidatorApiChannel
.build();
}

private HttpUrl createHeadEventSourceUrl(final HttpUrl endpoint) {
private HttpUrl createEventStreamSourceUrl(
final HttpUrl endpoint, final List<EventType> eventTypes) {
final HttpUrl eventSourceUrl =
endpoint.resolve(
ValidatorApiMethod.EVENTS.getPath(emptyMap()) + "?topics=" + EventType.head);
ValidatorApiMethod.EVENTS.getPath(emptyMap())
+ "?topics="
+ String.join(",", eventTypes.stream().map(EventType::name).toList()));
return Preconditions.checkNotNull(eventSourceUrl);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,7 @@ public static BeaconNodeApi create(
validatorTimingChannel,
serviceConfig.getMetricsSystem(),
validatorConfig.generateEarlyAttestations(),
validatorConfig.isShutdownWhenValidatorSlashedEnabled(),
spec);

eventChannels.subscribe(BeaconNodeReadinessChannel.class, beaconChainEventAdapter);
Expand Down

0 comments on commit 9806cd6

Please sign in to comment.