tidied remaining flows
This commit is contained in:
parent
683ef69418
commit
4d07326b78
@ -6,18 +6,18 @@ import java.util.List;
|
|||||||
import java.util.concurrent.Callable;
|
import java.util.concurrent.Callable;
|
||||||
import java.util.function.Predicate;
|
import java.util.function.Predicate;
|
||||||
import java.util.stream.Collectors;
|
import java.util.stream.Collectors;
|
||||||
|
//
|
||||||
public final class CorDappHelpers {
|
//public final class CorDappHelpers {
|
||||||
public static <T> T findAndExpectExactlyOne(Collection<T> collection, Predicate<? super T> filterFn, String exceptionMsg)
|
// public static <T> T findAndExpectExactlyOne(Collection<T> collection, Predicate<? super T> filterFn, String exceptionMsg)
|
||||||
{
|
// {
|
||||||
Collection<T> results = collection.stream().filter(filterFn).collect(Collectors.toList());
|
// Collection<T> results = collection.stream().filter(filterFn).collect(Collectors.toList());
|
||||||
if(results.size() != 1){
|
// if(results.size() != 1){
|
||||||
throw new RuntimeException(exceptionMsg);
|
// throw new RuntimeException(exceptionMsg);
|
||||||
}
|
// }
|
||||||
return results.iterator().next();
|
// return results.iterator().next();
|
||||||
}
|
// }
|
||||||
|
//
|
||||||
public static <T> T findAndExpectExactlyOne(Collection<T> collection, String exceptionMsg) {
|
// public static <T> T findAndExpectExactlyOne(Collection<T> collection, String exceptionMsg) {
|
||||||
return findAndExpectExactlyOne(collection, e -> true, exceptionMsg);
|
// return findAndExpectExactlyOne(collection, e -> true, exceptionMsg);
|
||||||
}
|
// }
|
||||||
}
|
//}
|
||||||
|
@ -6,6 +6,7 @@ import net.corda.v5.application.flows.RPCRequestData;
|
|||||||
import net.corda.v5.application.flows.RPCStartableFlow;
|
import net.corda.v5.application.flows.RPCStartableFlow;
|
||||||
import net.corda.v5.application.marshalling.JsonMarshallingService;
|
import net.corda.v5.application.marshalling.JsonMarshallingService;
|
||||||
import net.corda.v5.base.annotations.Suspendable;
|
import net.corda.v5.base.annotations.Suspendable;
|
||||||
|
import net.corda.v5.base.exceptions.CordaRuntimeException;
|
||||||
import net.corda.v5.crypto.SecureHash;
|
import net.corda.v5.crypto.SecureHash;
|
||||||
import net.corda.v5.ledger.utxo.StateAndRef;
|
import net.corda.v5.ledger.utxo.StateAndRef;
|
||||||
import net.corda.v5.ledger.utxo.UtxoLedgerService;
|
import net.corda.v5.ledger.utxo.UtxoLedgerService;
|
||||||
@ -16,8 +17,9 @@ import org.slf4j.LoggerFactory;
|
|||||||
|
|
||||||
import java.util.*;
|
import java.util.*;
|
||||||
|
|
||||||
import static com.r3.developers.csdetemplate.utilities.CorDappHelpers.findAndExpectExactlyOne;
|
//import static com.r3.developers.csdetemplate.utilities.CorDappHelpers.findAndExpectExactlyOne;
|
||||||
import static java.util.Objects.*;
|
import static java.util.Objects.*;
|
||||||
|
import static java.util.stream.Collectors.toList;
|
||||||
|
|
||||||
public class GetChatFlow implements RPCStartableFlow {
|
public class GetChatFlow implements RPCStartableFlow {
|
||||||
|
|
||||||
@ -29,27 +31,36 @@ public class GetChatFlow implements RPCStartableFlow {
|
|||||||
@CordaInject
|
@CordaInject
|
||||||
public UtxoLedgerService ledgerService;
|
public UtxoLedgerService ledgerService;
|
||||||
|
|
||||||
@NotNull
|
// @NotNull
|
||||||
@Override
|
@Override
|
||||||
@Suspendable
|
@Suspendable
|
||||||
public String call(RPCRequestData requestBody) throws IllegalArgumentException {
|
public String call(RPCRequestData requestBody) {
|
||||||
|
|
||||||
GetChatFlowArgs flowArgs = requestBody.getRequestBodyAs(jsonMarshallingService, GetChatFlowArgs.class);
|
GetChatFlowArgs flowArgs = requestBody.getRequestBodyAs(jsonMarshallingService, GetChatFlowArgs.class);
|
||||||
List<StateAndRef<ChatState>> stateAndRefs = ledgerService.findUnconsumedStatesByType(ChatState.class);
|
|
||||||
|
|
||||||
log.info("GetChatFlow Number of stateAndRefs = " + stateAndRefs.size());
|
List<StateAndRef<ChatState>> chatStateAndRefs = ledgerService.findUnconsumedStatesByType(ChatState.class);
|
||||||
log.info("GetChatFlow stateAndRefs = " + stateAndRefs);
|
List<StateAndRef<ChatState>> chatStateAndRefsWithId = chatStateAndRefs.stream()
|
||||||
|
.filter(sar -> sar.getState().getContractState().getId().equals(flowArgs.getId())).collect(toList());
|
||||||
|
if (chatStateAndRefsWithId.size() != 1) throw new CordaRuntimeException("Multiple or zero Chat states with id " + flowArgs.getId() + " found");
|
||||||
|
StateAndRef<ChatState> chatStateAndRef = chatStateAndRefsWithId.get(0);
|
||||||
|
|
||||||
StateAndRef<ChatState> state = findAndExpectExactlyOne(stateAndRefs,
|
|
||||||
stateAndRef -> stateAndRef.getState().getContractState().getId().equals(flowArgs.getId()),
|
|
||||||
"did not find an unique ChatState"
|
|
||||||
);
|
|
||||||
|
|
||||||
return jsonMarshallingService.format(resolveMessagesFromBackchain(state, flowArgs.getNumberOfRecords() ));
|
// log.info("GetChatFlow Number of stateAndRefs = " + stateAndRefs.size());
|
||||||
|
// log.info("GetChatFlow stateAndRefs = " + stateAndRefs);
|
||||||
|
|
||||||
|
// StateAndRef<ChatState> state = findAndExpectExactlyOne(stateAndRefs,
|
||||||
|
// stateAndRef -> stateAndRef.getState().getContractState().getId().equals(flowArgs.getId()),
|
||||||
|
// "did not find an unique ChatState"
|
||||||
|
// );
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
return jsonMarshallingService.format(resolveMessagesFromBackchain(chatStateAndRef, flowArgs.getNumberOfRecords() ));
|
||||||
}
|
}
|
||||||
|
|
||||||
@NotNull
|
@NotNull
|
||||||
@Suspendable
|
@Suspendable
|
||||||
private List<GetChatResponse> resolveMessagesFromBackchain(StateAndRef<?> stateAndRef, int numberOfRecords) throws IllegalArgumentException {
|
private List<GetChatResponse> resolveMessagesFromBackchain(StateAndRef<?> stateAndRef, int numberOfRecords) {
|
||||||
|
|
||||||
List<GetChatResponse> messages = new LinkedList<>();
|
List<GetChatResponse> messages = new LinkedList<>();
|
||||||
|
|
||||||
@ -58,17 +69,24 @@ public class GetChatFlow implements RPCStartableFlow {
|
|||||||
boolean moreBackchain = true;
|
boolean moreBackchain = true;
|
||||||
|
|
||||||
while (moreBackchain) {
|
while (moreBackchain) {
|
||||||
|
|
||||||
SecureHash transactionId = currentStateAndRef.getRef().getTransactionHash();
|
SecureHash transactionId = currentStateAndRef.getRef().getTransactionHash();
|
||||||
|
|
||||||
UtxoLedgerTransaction transaction = requireNonNull(
|
UtxoLedgerTransaction transaction = requireNonNull(
|
||||||
ledgerService.findLedgerTransaction(transactionId),
|
ledgerService.findLedgerTransaction(transactionId),
|
||||||
"Transaction $transactionId not found"
|
"Transaction " + transactionId + " not found."
|
||||||
);
|
);
|
||||||
|
|
||||||
ChatState output = findAndExpectExactlyOne(
|
|
||||||
transaction.getOutputStates(ChatState.class),
|
// ChatState output = findAndExpectExactlyOne(
|
||||||
"Expecting one and only one ChatState output for transaction " + transactionId
|
// transaction.getOutputStates(ChatState.class),
|
||||||
);
|
// "Expecting one and only one ChatState output for transaction " + transactionId
|
||||||
|
// );
|
||||||
|
|
||||||
|
List<ChatState> chatStates = transaction.getOutputStates(ChatState.class);
|
||||||
|
if (chatStates.size() != 1) throw new CordaRuntimeException(
|
||||||
|
"Expecting one and only one ChatState output for transaction " + transactionId + ".");
|
||||||
|
ChatState output = chatStates.get(0);
|
||||||
|
|
||||||
messages.add(new GetChatResponse(output.getMessageFrom().toString(), output.getMessage()));
|
messages.add(new GetChatResponse(output.getMessageFrom().toString(), output.getMessage()));
|
||||||
recordsToFetch--;
|
recordsToFetch--;
|
||||||
@ -78,7 +96,7 @@ public class GetChatFlow implements RPCStartableFlow {
|
|||||||
if (inputStateAndRefs.isEmpty() || recordsToFetch == 0) {
|
if (inputStateAndRefs.isEmpty() || recordsToFetch == 0) {
|
||||||
moreBackchain = false;
|
moreBackchain = false;
|
||||||
} else if (inputStateAndRefs.size() > 1) {
|
} else if (inputStateAndRefs.size() > 1) {
|
||||||
throw new IllegalArgumentException("More than one input state found for transaction " + transactionId + ".");
|
throw new CordaRuntimeException("More than one input state found for transaction " + transactionId + ".");
|
||||||
} else {
|
} else {
|
||||||
currentStateAndRef = inputStateAndRefs.get(0);
|
currentStateAndRef = inputStateAndRefs.get(0);
|
||||||
}
|
}
|
||||||
|
@ -72,15 +72,15 @@ public class UpdateChatFlow implements RPCStartableFlow {
|
|||||||
// "Multiple or zero Chat states with id " + flowArgs.getId() + " found"
|
// "Multiple or zero Chat states with id " + flowArgs.getId() + " found"
|
||||||
// );
|
// );
|
||||||
|
|
||||||
List<StateAndRef<ChatState>> chatStates = ledgerService.findUnconsumedStatesByType(ChatState.class);
|
List<StateAndRef<ChatState>> chatStateAndRefs = ledgerService.findUnconsumedStatesByType(ChatState.class);
|
||||||
List<StateAndRef<ChatState>> chatStatesWithId = chatStates.stream()
|
List<StateAndRef<ChatState>> chatStateAndRefsWithId = chatStateAndRefs.stream()
|
||||||
.filter(sar -> sar.getState().getContractState().getId().equals(flowArgs.getId())).collect(toList());
|
.filter(sar -> sar.getState().getContractState().getId().equals(flowArgs.getId())).collect(toList());
|
||||||
if (chatStatesWithId.size() != 1) throw new CordaRuntimeException("Multiple or zero Chat states with id " + flowArgs.getId() + " found");
|
if (chatStateAndRefsWithId.size() != 1) throw new CordaRuntimeException("Multiple or zero Chat states with id " + flowArgs.getId() + " found");
|
||||||
StateAndRef<ChatState> stateAndRef = chatStatesWithId.get(0);
|
StateAndRef<ChatState> chatStateAndRef = chatStateAndRefsWithId.get(0);
|
||||||
|
|
||||||
|
|
||||||
MemberInfo myInfo = memberLookup.myInfo();
|
MemberInfo myInfo = memberLookup.myInfo();
|
||||||
ChatState state = stateAndRef.getState().getContractState();
|
ChatState state = chatStateAndRef.getState().getContractState();
|
||||||
|
|
||||||
List<MemberInfo> members = state.getParticipants().stream().map(
|
List<MemberInfo> members = state.getParticipants().stream().map(
|
||||||
it -> requireNonNull(memberLookup.lookup(it), "Member not found from public Key "+ it + ".")
|
it -> requireNonNull(memberLookup.lookup(it), "Member not found from public Key "+ it + ".")
|
||||||
@ -95,10 +95,10 @@ public class UpdateChatFlow implements RPCStartableFlow {
|
|||||||
ChatState newChatState = state.updateMessage(myInfo.getName(), flowArgs.getMessage());
|
ChatState newChatState = state.updateMessage(myInfo.getName(), flowArgs.getMessage());
|
||||||
|
|
||||||
UtxoTransactionBuilder txBuilder = ledgerService.getTransactionBuilder()
|
UtxoTransactionBuilder txBuilder = ledgerService.getTransactionBuilder()
|
||||||
.setNotary(stateAndRef.getState().getNotary())
|
.setNotary(chatStateAndRef.getState().getNotary())
|
||||||
.setTimeWindowBetween(Instant.now(), Instant.now().plusMillis(Duration.ofDays(1).toMillis()))
|
.setTimeWindowBetween(Instant.now(), Instant.now().plusMillis(Duration.ofDays(1).toMillis()))
|
||||||
.addOutputState(newChatState)
|
.addOutputState(newChatState)
|
||||||
.addInputState(stateAndRef.getRef())
|
.addInputState(chatStateAndRef.getRef())
|
||||||
.addCommand(new ChatContract.Update())
|
.addCommand(new ChatContract.Update())
|
||||||
.addSignatories(newChatState.getParticipants());
|
.addSignatories(newChatState.getParticipants());
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user