Skip to content

Commit

Permalink
Merge pull request #41 from 5GZORRO/feature/spectoken
Browse files Browse the repository at this point in the history
Feature/spectoken
  • Loading branch information
Dabami authored Mar 29, 2023
2 parents ea301aa + 1c9d911 commit 06e69e4
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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<String> offerDids;
try {
offerDids = primitiveSpectokenDriver.invalidatePrimitiveSpectoken(licenseDid);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,10 +98,8 @@ public SignedTransaction call() throws FlowException {
List<StateAndRef<DerivativeSpecTokenType>> derivativeStateAndRefs = getServiceHub().getVaultService().queryBy(DerivativeSpecTokenType.class).getStates();
for (StateAndRef<DerivativeSpecTokenType> 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");
}
}

Expand Down Expand Up @@ -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;
Expand All @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<StateAndRef<PrimitiveSpecTokenType>> primitiveStateAndRefs = getServiceHub().getVaultService().queryBy(PrimitiveSpecTokenType.class).getStates();
for (StateAndRef<PrimitiveSpecTokenType> existingPrimitiveSpecTokenTypeStateAndRef : primitiveStateAndRefs) {
PrimitiveSpecTokenType existingPrimitiveSpectokenType = existingPrimitiveSpecTokenTypeStateAndRef.getState().getData();
if (existingPrimitiveSpectokenType.isValid() && newPrimitiveOverlapsExisting(primitiveSpecTokenType, existingPrimitiveSpectokenType)) {
throw new FlowException("Primitive Spectoken data overlaps existing Primitive Spectoken");
}
}
TransactionState<PrimitiveSpecTokenType> 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;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,6 @@ public InvalidateDerivativeSpectokensFlow(String primitiveId) {
@Override
public List<String> call() throws FlowException {
List<StateAndRef<DerivativeSpecTokenType>> states = getServiceHub().getVaultService().queryBy(DerivativeSpecTokenType.class).getStates();
if (states.isEmpty()) {
throw new FlowException("Any Derivative Spectoken found.");
}
DerivativeSpecTokenType derivativeSpecTokenType;
List<Party> allOtherParties = getServiceHub()
.getNetworkMapCache()
Expand Down

0 comments on commit 06e69e4

Please sign in to comment.