Skip to content

Commit

Permalink
Merge pull request #4595 from ghubstan/remove-duplicated-supporttype-…
Browse files Browse the repository at this point in the history
…enum

Remove redundant DisputeAgentType enum
  • Loading branch information
ripcurlx authored Oct 7, 2020
2 parents 66f6e49 + 5f9b1e6 commit 7d14ff3
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 38 deletions.
10 changes: 6 additions & 4 deletions apitest/src/test/java/bisq/apitest/method/MethodTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,6 @@

import static bisq.common.app.DevEnv.DEV_PRIVILEGE_PRIV_KEY;
import static bisq.core.payment.payload.PaymentMethod.PERFECT_MONEY;
import static bisq.core.support.dispute.agent.DisputeAgent.DisputeAgentType.MEDIATOR;
import static bisq.core.support.dispute.agent.DisputeAgent.DisputeAgentType.REFUND_AGENT;
import static java.util.Comparator.comparing;
import static org.junit.jupiter.api.Assertions.assertEquals;

Expand All @@ -46,6 +44,10 @@

public class MethodTest extends ApiTestCase {

protected static final String ARBITRATOR = "arbitrator";
protected static final String MEDIATOR = "mediator";
protected static final String REFUND_AGENT = "refundagent";

// Convenience methods for building gRPC request objects

protected final GetBalanceRequest createBalanceRequest() {
Expand Down Expand Up @@ -147,7 +149,7 @@ protected static RegisterDisputeAgentRequest createRegisterDisputeAgentRequest(S
@SuppressWarnings("ResultOfMethodCallIgnored")
protected static void registerDisputeAgents(BisqAppConfig bisqAppConfig) {
var disputeAgentsService = grpcStubs(bisqAppConfig).disputeAgentsService;
disputeAgentsService.registerDisputeAgent(createRegisterDisputeAgentRequest(MEDIATOR.name()));
disputeAgentsService.registerDisputeAgent(createRegisterDisputeAgentRequest(REFUND_AGENT.name()));
disputeAgentsService.registerDisputeAgent(createRegisterDisputeAgentRequest(MEDIATOR));
disputeAgentsService.registerDisputeAgent(createRegisterDisputeAgentRequest(REFUND_AGENT));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,6 @@
import static bisq.apitest.config.BisqAppConfig.arbdaemon;
import static bisq.apitest.config.BisqAppConfig.seednode;
import static bisq.common.app.DevEnv.DEV_PRIVILEGE_PRIV_KEY;
import static bisq.core.support.dispute.agent.DisputeAgent.DisputeAgentType.ARBITRATOR;
import static bisq.core.support.dispute.agent.DisputeAgent.DisputeAgentType.MEDIATOR;
import static bisq.core.support.dispute.agent.DisputeAgent.DisputeAgentType.REFUND_AGENT;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.fail;
Expand All @@ -60,7 +57,7 @@ public static void setUp() {
@Order(1)
public void testRegisterArbitratorShouldThrowException() {
var req =
createRegisterDisputeAgentRequest(ARBITRATOR.name());
createRegisterDisputeAgentRequest(ARBITRATOR);
Throwable exception = assertThrows(StatusRuntimeException.class, () ->
grpcStubs(arbdaemon).disputeAgentsService.registerDisputeAgent(req));
assertEquals("INVALID_ARGUMENT: arbitrators must be registered in a Bisq UI",
Expand All @@ -82,7 +79,7 @@ public void testInvalidDisputeAgentTypeArgShouldThrowException() {
@Order(3)
public void testInvalidRegistrationKeyArgShouldThrowException() {
var req = RegisterDisputeAgentRequest.newBuilder()
.setDisputeAgentType(REFUND_AGENT.name().toLowerCase())
.setDisputeAgentType(REFUND_AGENT)
.setRegistrationKey("invalid" + DEV_PRIVILEGE_PRIV_KEY).build();
Throwable exception = assertThrows(StatusRuntimeException.class, () ->
grpcStubs(arbdaemon).disputeAgentsService.registerDisputeAgent(req));
Expand All @@ -94,15 +91,15 @@ public void testInvalidRegistrationKeyArgShouldThrowException() {
@Order(4)
public void testRegisterMediator() {
var req =
createRegisterDisputeAgentRequest(MEDIATOR.name());
createRegisterDisputeAgentRequest(MEDIATOR);
grpcStubs(arbdaemon).disputeAgentsService.registerDisputeAgent(req);
}

@Test
@Order(5)
public void testRegisterRefundAgent() {
var req =
createRegisterDisputeAgentRequest(REFUND_AGENT.name());
createRegisterDisputeAgentRequest(REFUND_AGENT);
grpcStubs(arbdaemon).disputeAgentsService.registerDisputeAgent(req);
}

Expand Down
46 changes: 31 additions & 15 deletions core/src/main/java/bisq/core/api/CoreDisputeAgentsService.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

package bisq.core.api;

import bisq.core.support.SupportType;
import bisq.core.support.dispute.mediation.mediator.Mediator;
import bisq.core.support.dispute.mediation.mediator.MediatorManager;
import bisq.core.support.dispute.refund.refundagent.RefundAgent;
Expand All @@ -40,10 +41,13 @@
import lombok.extern.slf4j.Slf4j;

import static bisq.common.app.DevEnv.DEV_PRIVILEGE_PRIV_KEY;
import static bisq.core.support.dispute.agent.DisputeAgent.DisputeAgentType;
import static bisq.core.support.SupportType.ARBITRATION;
import static bisq.core.support.SupportType.MEDIATION;
import static bisq.core.support.SupportType.REFUND;
import static bisq.core.support.SupportType.TRADE;
import static java.lang.String.format;
import static java.net.InetAddress.getLoopbackAddress;
import static java.util.Arrays.asList;
import static java.util.Arrays.stream;

@Slf4j
class CoreDisputeAgentsService {
Expand Down Expand Up @@ -71,7 +75,7 @@ public CoreDisputeAgentsService(Config config,
this.languageCodes = asList("de", "en", "es", "fr");
}

void registerDisputeAgent(String disputeAgentTypeString, String registrationKey) {
void registerDisputeAgent(String disputeAgentType, String registrationKey) {
if (!p2PService.isBootstrapped())
throw new IllegalStateException("p2p service is not bootstrapped yet");

Expand All @@ -83,26 +87,28 @@ void registerDisputeAgent(String disputeAgentTypeString, String registrationKey)
if (!registrationKey.equals(DEV_PRIVILEGE_PRIV_KEY))
throw new IllegalArgumentException("invalid registration key");

Optional<DisputeAgentType> disputeAgentType = getDisputeAgentTypeForString(disputeAgentTypeString);
if (disputeAgentType.isPresent()) {
Optional<SupportType> supportType = getSupportType(disputeAgentType);
if (supportType.isPresent()) {
ECKey ecKey;
String signature;
switch ((disputeAgentType.get())) {
case ARBITRATOR:
switch (supportType.get()) {
case ARBITRATION:
throw new IllegalArgumentException("arbitrators must be registered in a Bisq UI");
case MEDIATOR:
case MEDIATION:
ecKey = mediatorManager.getRegistrationKey(registrationKey);
signature = mediatorManager.signStorageSignaturePubKey(Objects.requireNonNull(ecKey));
registerMediator(nodeAddress, languageCodes, ecKey, signature);
return;
case REFUND_AGENT:
case REFUND:
ecKey = refundAgentManager.getRegistrationKey(registrationKey);
signature = refundAgentManager.signStorageSignaturePubKey(Objects.requireNonNull(ecKey));
registerRefundAgent(nodeAddress, languageCodes, ecKey, signature);
return;
case TRADE:
throw new IllegalArgumentException("trade agent registration not supported");
}
} else {
throw new IllegalArgumentException("unknown dispute agent type " + disputeAgentTypeString);
throw new IllegalArgumentException(format("unknown dispute agent type '%s'", disputeAgentType));
}
}

Expand Down Expand Up @@ -148,10 +154,20 @@ private void registerRefundAgent(NodeAddress nodeAddress,
new IllegalStateException("could not register refund agent"));
}

private Optional<DisputeAgentType> getDisputeAgentTypeForString(String disputeAgentTypeString) {
return stream(DisputeAgentType.values())
.filter(da -> da.name().equalsIgnoreCase(disputeAgentTypeString)
|| da.alternateName().equalsIgnoreCase(disputeAgentTypeString))
.findFirst();
private Optional<SupportType> getSupportType(String disputeAgentType) {
switch (disputeAgentType.toLowerCase()) {
case "arbitrator":
return Optional.of(ARBITRATION);
case "mediator":
return Optional.of(MEDIATION);
case "refundagent":
case "refund_agent":
return Optional.of(REFUND);
case "tradeagent":
case "trade_agent":
return Optional.of(TRADE);
default:
return Optional.empty();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -43,18 +43,6 @@
public abstract class DisputeAgent implements ProtectedStoragePayload, ExpirablePayload {
public static final long TTL = TimeUnit.DAYS.toMillis(10);

public enum DisputeAgentType {
ARBITRATOR,
MEDIATOR,
REFUND_AGENT;

public String alternateName() {
return this.equals(REFUND_AGENT)
? REFUND_AGENT.name().replace("_", "")
: this.name();
}
}

protected final NodeAddress nodeAddress;
protected final PubKeyRing pubKeyRing;
protected final List<String> languageCodes;
Expand Down

0 comments on commit 7d14ff3

Please sign in to comment.