Commit e297d217 authored by luxq's avatar luxq

update attacker client

parent 2ad8622f
...@@ -11,11 +11,15 @@ public class AttackService { ...@@ -11,11 +11,15 @@ public class AttackService {
private final JsonRpcHttpClient jsonRpcClient; private final JsonRpcHttpClient jsonRpcClient;
public AttackService() { public AttackService() {
this.jsonRpcClient = JsonRpcClientUtil.getJsonRpcClient(); this.jsonRpcClient = JsonRpcClientUtil.getInstance().getJsonRpcClient();
} }
public AttackService(final String url) { public AttackService(final String url) {
this.jsonRpcClient = JsonRpcClientUtil.getJsonRpcClientByUrl(url); this.jsonRpcClient = JsonRpcClientUtil.getInstance().getJsonRpcClientByUrl(url);
}
public boolean enabled() {
return this.jsonRpcClient != null;
} }
private SafeFuture<AttackerResponse> invokeRpc(final String method, final Object[] params) { private SafeFuture<AttackerResponse> invokeRpc(final String method, final Object[] params) {
......
...@@ -7,28 +7,34 @@ import java.net.URI; ...@@ -7,28 +7,34 @@ import java.net.URI;
public class JsonRpcClientUtil { public class JsonRpcClientUtil {
private JsonRpcClientUtil(){ private static final JsonRpcClientUtil INSTANCE = new JsonRpcClientUtil();
private JsonRpcClientUtil() {
// Private constructor to prevent instantiation
} }
public static JsonRpcHttpClient getJsonRpcClient(){
return JsonRpcHttpClientSingleton.singleton(""); public static JsonRpcClientUtil getInstance() {
return INSTANCE;
}
public JsonRpcHttpClient getJsonRpcClient() {
return createJsonRpcClient("");
} }
public static JsonRpcHttpClient getJsonRpcClientByUrl(final String url){
return JsonRpcHttpClientSingleton.singleton(url); public JsonRpcHttpClient getJsonRpcClientByUrl(final String url) {
return createJsonRpcClient(url);
} }
private static class JsonRpcHttpClientSingleton{
private static JsonRpcHttpClient singleton(final String url){ private JsonRpcHttpClient createJsonRpcClient(final String url) {
JsonRpcHttpClient client = null;
try { try {
String real = url; String realUrl = url.isEmpty() ? System.getenv("ATTACKER_SERVICE_URL") : url;
if (url.isEmpty()) { if (realUrl == null || realUrl.isEmpty()) {
real = System.getenv("ATTACKER_SERVICE_URL"); return null;
} else {
return new JsonRpcHttpClient(URI.create(realUrl).toURL());
} }
client = new JsonRpcHttpClient(URI.create(real).toURL());
} catch (MalformedURLException e) { } catch (MalformedURLException e) {
throw new RuntimeException(e); throw new RuntimeException(e);
} }
return client;
}
} }
} }
\ No newline at end of file
...@@ -2,6 +2,7 @@ package tech.pegasys.teku.attacker; ...@@ -2,6 +2,7 @@ package tech.pegasys.teku.attacker;
import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThat;
import org.checkerframework.checker.units.qual.A;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import tech.pegasys.teku.infrastructure.async.SafeFuture; import tech.pegasys.teku.infrastructure.async.SafeFuture;
...@@ -9,16 +10,59 @@ import java.util.concurrent.ExecutionException; ...@@ -9,16 +10,59 @@ import java.util.concurrent.ExecutionException;
public class AttackServiceTest { public class AttackServiceTest {
private AttackService attackService; @Test
public void attackIsNotEnabled() {
AttackService s = new AttackService();
assertThat(s.enabled()).isEqualTo(false);
}
@Test
public void attackIsEnabled() {
AttackService s = new AttackService("http://127.0.0.1:12000");
assertThat(s.enabled()).isEqualTo(true);
}
@Test @Test
public void blockGetNewParentRoot() throws ExecutionException, InterruptedException { public void blockGetNewParentRoot() throws ExecutionException, InterruptedException {
attackService = new AttackService("http://127.0.0.1:12000"); AttackService s = new AttackService("http://127.0.0.1:12000");
long slot = 1L; long slot = 1L;
String pub = ""; String pub = "";
String parentRoot = ""; String parentRoot = "";
SafeFuture<AttackerResponse> attackerResponse = attackService.blockGetNewParentRoot(slot, pub, parentRoot); SafeFuture<AttackerResponse> attackerResponse = s.blockGetNewParentRoot(slot, pub, parentRoot);
assertThat(attackerResponse.get().getCmd().getValue()) assertThat(attackerResponse.get().getCmd().getValue())
.isEqualTo(AttackerCommand.CMD_NULL.getValue()); .isEqualTo(AttackerCommand.CMD_NULL.getValue());
} }
public void blockBeforeBroadcast() {
AttackService s = new AttackService("http://127.0.0.1:12000");
long slot = 1L;
boolean skipBroadCast = false;
try {
AttackerResponse attackerResponse = s.blockBeforeBroadcast(slot).get(); // Blocking call to get the result
switch (attackerResponse.getCmd()) {
case CMD_EXIT:
case CMD_ABORT:
System.exit(-1); // Terminate the process
break;
case CMD_SKIP:
skipBroadCast = true; // Skip broadcast
break;
case CMD_RETURN:
// Simulate returning a response (adjust as per actual method requirements)
return; // Exit the method
case CMD_NULL:
case CMD_CONTINUE:
// Do nothing
break;
default:
throw new IllegalStateException("Unexpected command received: " + attackerResponse.getCmd());
}
} catch (Exception e) {
e.printStackTrace();
throw new RuntimeException("Failed to process attacker response", e);
}
System.out.println("Block before broadcast completed for slot: " + slot);
// then sleep 10s
}
} }
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment