diff --git a/app/src/main/java/eu/_5gzorro/manager/api/controller/SpectokenController.java b/app/src/main/java/eu/_5gzorro/manager/api/controller/SpectokenController.java index 9f9b1ee..713ed73 100644 --- a/app/src/main/java/eu/_5gzorro/manager/api/controller/SpectokenController.java +++ b/app/src/main/java/eu/_5gzorro/manager/api/controller/SpectokenController.java @@ -147,8 +147,8 @@ public ResponseEntity issueDerivativeSpectoken(@Valid @RequestBody @NotNull I description = "Redeem primitive Spectoken" ) }) - @PutMapping("primitive/redeem/{licenseDid}") - public ResponseEntity invalidatePrimitiveSpectoken(@Valid @PathVariable("licenseDid") @NotNull String licenseDid) { + @PutMapping("primitive/redeem") + public ResponseEntity invalidatePrimitiveSpectoken(@Valid @RequestParam("licenseDid") @NotNull String licenseDid) { List offerDids; try { offerDids = primitiveSpectokenDriver.invalidatePrimitiveSpectoken(licenseDid); diff --git a/corda-driver/workflows/src/main/java/eu/_5gzorro/manager/dlt/corda/flows/spectoken/CreateDerivativeSpecTokenTypeFromOfferFlow.java b/corda-driver/workflows/src/main/java/eu/_5gzorro/manager/dlt/corda/flows/spectoken/CreateDerivativeSpecTokenTypeFromOfferFlow.java index 5dc4085..22d1c79 100644 --- a/corda-driver/workflows/src/main/java/eu/_5gzorro/manager/dlt/corda/flows/spectoken/CreateDerivativeSpecTokenTypeFromOfferFlow.java +++ b/corda-driver/workflows/src/main/java/eu/_5gzorro/manager/dlt/corda/flows/spectoken/CreateDerivativeSpecTokenTypeFromOfferFlow.java @@ -98,10 +98,8 @@ public SignedTransaction call() throws FlowException { List> derivativeStateAndRefs = getServiceHub().getVaultService().queryBy(DerivativeSpecTokenType.class).getStates(); for (StateAndRef existingDerivativeSpecTokenTypeStateAndRef : derivativeStateAndRefs) { DerivativeSpecTokenType existingDerivativeSpecTokenType = existingDerivativeSpecTokenTypeStateAndRef.getState().getData(); - if (existingDerivativeSpecTokenType.isValid()) { - if (newDerivativeOverlapsExisting(derivativeSpecTokenType, existingDerivativeSpecTokenType)) { - throw new FlowException("Derivative Spectoken data overlaps existing Derivative Spectoken"); - } + if (existingDerivativeSpecTokenType.isValid() && newDerivativeOverlapsExisting(derivativeSpecTokenType, existingDerivativeSpecTokenType)) { + throw new FlowException("Derivative Spectoken data overlaps existing Derivative Spectoken"); } } @@ -151,14 +149,14 @@ private boolean doesDerivativeMatchPrimitive(PrimitiveSpecTokenType primitiveSpe } private boolean newDerivativeOverlapsExisting(DerivativeSpecTokenType newDerivativeSpecTokenType, DerivativeSpecTokenType existingDerivativeSpecTokenType) { - if (thereIsFrequencyOverlap(newDerivativeSpecTokenType, existingDerivativeSpecTokenType)) { - return thereIsTimeOverlap(newDerivativeSpecTokenType, existingDerivativeSpecTokenType); + if (isThereFrequencyOverlap(newDerivativeSpecTokenType, existingDerivativeSpecTokenType)) { + return isThereTimeOverlap(newDerivativeSpecTokenType, existingDerivativeSpecTokenType); } else { return false; } } - private boolean thereIsFrequencyOverlap(DerivativeSpecTokenType newDerivativeSpecTokenType, DerivativeSpecTokenType existingDerivativeSpecTokenType) { + private boolean isThereFrequencyOverlap(DerivativeSpecTokenType newDerivativeSpecTokenType, DerivativeSpecTokenType existingDerivativeSpecTokenType) { boolean dlOverlap; if (newDerivativeSpecTokenType.getEndDl() < existingDerivativeSpecTokenType.getStartDl()) { dlOverlap = false; @@ -181,7 +179,7 @@ private boolean thereIsFrequencyOverlap(DerivativeSpecTokenType newDerivativeSpe } - private boolean thereIsTimeOverlap(DerivativeSpecTokenType newDerivativeSpecTokenType, DerivativeSpecTokenType existingDerivativeSpecTokenType) { + private boolean isThereTimeOverlap(DerivativeSpecTokenType newDerivativeSpecTokenType, DerivativeSpecTokenType existingDerivativeSpecTokenType) { boolean timeOverlap; if (newDerivativeSpecTokenType.getEndDate().before(existingDerivativeSpecTokenType.getStartDate())) { timeOverlap = false; diff --git a/corda-driver/workflows/src/main/java/eu/_5gzorro/manager/dlt/corda/flows/spectoken/CreatePrimitiveSpecTokenTypeFlow.java b/corda-driver/workflows/src/main/java/eu/_5gzorro/manager/dlt/corda/flows/spectoken/CreatePrimitiveSpecTokenTypeFlow.java index d7ee308..f38e9bc 100644 --- a/corda-driver/workflows/src/main/java/eu/_5gzorro/manager/dlt/corda/flows/spectoken/CreatePrimitiveSpecTokenTypeFlow.java +++ b/corda-driver/workflows/src/main/java/eu/_5gzorro/manager/dlt/corda/flows/spectoken/CreatePrimitiveSpecTokenTypeFlow.java @@ -4,6 +4,7 @@ import com.r3.corda.lib.tokens.workflows.flows.rpc.CreateEvolvableTokens; import eu._5gzorro.manager.dlt.corda.flows.utils.ExtendedFlowLogic; import eu._5gzorro.manager.dlt.corda.states.PrimitiveSpecTokenType; +import net.corda.core.contracts.StateAndRef; import net.corda.core.contracts.TransactionState; import net.corda.core.flows.FlowException; import net.corda.core.flows.InitiatingFlow; @@ -34,8 +35,59 @@ public SignedTransaction call() throws FlowException { .flatMap(c -> c.getLegalIdentities().stream()) .filter(p -> !p.equals(getOurIdentity())) // Filter out own identity .collect(Collectors.toList()); + + List> primitiveStateAndRefs = getServiceHub().getVaultService().queryBy(PrimitiveSpecTokenType.class).getStates(); + for (StateAndRef existingPrimitiveSpecTokenTypeStateAndRef : primitiveStateAndRefs) { + PrimitiveSpecTokenType existingPrimitiveSpectokenType = existingPrimitiveSpecTokenTypeStateAndRef.getState().getData(); + if (existingPrimitiveSpectokenType.isValid() && newPrimitiveOverlapsExisting(primitiveSpecTokenType, existingPrimitiveSpectokenType)) { + throw new FlowException("Primitive Spectoken data overlaps existing Primitive Spectoken"); + } + } TransactionState transactionState = new TransactionState<>(primitiveSpecTokenType, firstNotary()); return subFlow(new CreateEvolvableTokens(transactionState, allOtherParties)); } + private boolean newPrimitiveOverlapsExisting(PrimitiveSpecTokenType newPrimitiveSpecTokenType, PrimitiveSpecTokenType existingPrimitiveSpecTokenType) { + if (isThereFrequencyOverlap(newPrimitiveSpecTokenType, existingPrimitiveSpecTokenType)) { + return isThereTimeOverlap(newPrimitiveSpecTokenType, existingPrimitiveSpecTokenType); + } else { + return false; + } + } + + private boolean isThereFrequencyOverlap(PrimitiveSpecTokenType newPrimitiveSpecTokenType, PrimitiveSpecTokenType existingPrimitiveSpecTokenType) { + boolean dlOverlap; + if (newPrimitiveSpecTokenType.getEndDl() < existingPrimitiveSpecTokenType.getStartDl()) { + dlOverlap = false; + } else if (newPrimitiveSpecTokenType.getEndDl().equals(existingPrimitiveSpecTokenType.getStartDl())) { + dlOverlap = true; + } else { + dlOverlap = newPrimitiveSpecTokenType.getStartDl() <= existingPrimitiveSpecTokenType.getEndDl(); + } + + boolean ulOverlap; + if (newPrimitiveSpecTokenType.getEndUl() < existingPrimitiveSpecTokenType.getStartUl()) { + ulOverlap = false; + } else if (newPrimitiveSpecTokenType.getEndUl().equals(existingPrimitiveSpecTokenType.getStartUl())) { + ulOverlap = true; + } else { + ulOverlap = newPrimitiveSpecTokenType.getStartUl() <= existingPrimitiveSpecTokenType.getEndUl(); + } + + return dlOverlap || ulOverlap; + + } + + private boolean isThereTimeOverlap(PrimitiveSpecTokenType newPrimitiveSpecTokenType, PrimitiveSpecTokenType existingPrimitiveSpecTokenType) { + boolean timeOverlap; + if (newPrimitiveSpecTokenType.getEndDate().before(existingPrimitiveSpecTokenType.getStartDate())) { + timeOverlap = false; + } else if (newPrimitiveSpecTokenType.getEndDate().equals(existingPrimitiveSpecTokenType.getStartDate())) { + timeOverlap = true; + } else { + timeOverlap = newPrimitiveSpecTokenType.getStartDate().before(existingPrimitiveSpecTokenType.getEndDate()) || newPrimitiveSpecTokenType.getStartDate().equals(existingPrimitiveSpecTokenType.getEndDate()); + } + return timeOverlap; + } + } diff --git a/corda-driver/workflows/src/main/java/eu/_5gzorro/manager/dlt/corda/flows/spectoken/InvalidateDerivativeSpectokensFlow.java b/corda-driver/workflows/src/main/java/eu/_5gzorro/manager/dlt/corda/flows/spectoken/InvalidateDerivativeSpectokensFlow.java index 68ca214..fc07c2a 100644 --- a/corda-driver/workflows/src/main/java/eu/_5gzorro/manager/dlt/corda/flows/spectoken/InvalidateDerivativeSpectokensFlow.java +++ b/corda-driver/workflows/src/main/java/eu/_5gzorro/manager/dlt/corda/flows/spectoken/InvalidateDerivativeSpectokensFlow.java @@ -28,9 +28,6 @@ public InvalidateDerivativeSpectokensFlow(String primitiveId) { @Override public List call() throws FlowException { List> states = getServiceHub().getVaultService().queryBy(DerivativeSpecTokenType.class).getStates(); - if (states.isEmpty()) { - throw new FlowException("Any Derivative Spectoken found."); - } DerivativeSpecTokenType derivativeSpecTokenType; List allOtherParties = getServiceHub() .getNetworkMapCache()