Skip to content

Commit

Permalink
Remove redundant DisputeAgentType enum
Browse files Browse the repository at this point in the history
Use existing SupportType enum instead, and map the valid api
'registerdisputeagent' method's dispute-agent-type parameter
names to SupportType.
  • Loading branch information
ghubstan committed Oct 3, 2020
1 parent f051a03 commit 9942529
Show file tree
Hide file tree
Showing 4 changed files with 40 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
45 changes: 30 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,12 @@
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.net.InetAddress.getLoopbackAddress;
import static java.util.Arrays.asList;
import static java.util.Arrays.stream;

@Slf4j
class CoreDisputeAgentsService {
Expand Down Expand Up @@ -71,7 +74,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 +86,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("unknown dispute agent type " + disputeAgentType);
}
}

Expand Down Expand Up @@ -148,10 +153,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 9942529

Please sign in to comment.