Added Update flow checks and bugs fixes

This commit is contained in:
Chris Barratt 2023-01-23 01:37:01 +00:00
parent e9c1139bfd
commit f3951ae0b0
3 changed files with 13 additions and 26 deletions

View File

@ -11,19 +11,6 @@ import org.jetbrains.annotations.NotNull;
import java.security.PublicKey;
import java.util.*;
/*
@BelongsToContract(ChatContract::class)
data class ChatState(
val id : UUID = UUID.randomUUID(),
val chatName: String,
val messageFrom: MemberX500Name,
val message: String,
override val participants: List<PublicKey>) : ContractState {
fun updateMessage(messageFrom: MemberX500Name, message: String) = copy(messageFrom = messageFrom, message = message)
}
*/
@CordaSerializable
@BelongsToContract(ChatContract.class)
public class ChatState implements ContractState {
@ -105,7 +92,7 @@ public class ChatState implements ContractState {
public List<PublicKey> participants;
public ChatState updateMessage(MemberX500Name name, String message) {
return new ChatState(chatName, name, message, participants);
return new ChatState(id, chatName, name, message, participants);
}
@Override

View File

@ -36,8 +36,11 @@ public class GetChatFlow implements RPCStartableFlow {
GetChatFlowArgs flowArgs = requestBody.getRequestBodyAs(jsonMarshallingService, GetChatFlowArgs.class);
List<StateAndRef<ChatState>> stateAndRefs = ledgerService.findUnconsumedStatesByType(ChatState.class);
log.info("GetChatFlow Number of stateAndRefs = " + stateAndRefs.size());
log.info("GetChatFlow stateAndRefs = " + stateAndRefs);
StateAndRef<ChatState> state = findAndExpectExactlyOne(stateAndRefs,
stateAndRef -> stateAndRef.getState().getContractState().getId() == flowArgs.getId(),
stateAndRef -> stateAndRef.getState().getContractState().getId().equals(flowArgs.getId()),
"did not find an unique ChatState"
);

View File

@ -60,10 +60,11 @@ public class UpdateChatFlow implements RPCStartableFlow {
try {
UpdateChatFlowArgs flowArgs = requestBody.getRequestBodyAs(jsonMarshallingService, UpdateChatFlowArgs.class);
// look up state (this is very inefficient)
// Look up state (this is very inefficient)
// Can get the error when you forget to update the ID.
StateAndRef<ChatState> stateAndRef = findAndExpectExactlyOne(
ledgerService.findUnconsumedStatesByType(ChatState.class),
sAndR -> sAndR.getState().getContractState().getId() == flowArgs.getId(),
sAndR -> sAndR.getState().getContractState().getId().equals(flowArgs.getId()),
"Multiple or zero Chat states with id " + flowArgs.getId() + " found"
);
@ -74,15 +75,11 @@ public class UpdateChatFlow implements RPCStartableFlow {
it -> requireNonNull(memberLookup.lookup(it), "Member not found from Key")
).collect(Collectors.toList());
// Now we want to check that there is only
/*
val otherMember = (members - myInfo).singleOrNull()
?: throw Exception("Should be only one participant other than the initiator")
*/
// NEED TO ADD CHECKS
// Now we want to check that there is only one member other than ourselves in the chat.
members.remove(myInfo);
if(members.size() != 1) {
throw new IllegalArgumentException("Should be only one participant other than the initiator");
}
MemberInfo otherMember = members.get(0);
// This needs to be a deep copy?
@ -114,7 +111,7 @@ RequestBody for triggering the flow via http-rpc:
"clientRequestId": "update-1",
"flowClassName": "com.r3.developers.csdetemplate.utxoexample.workflows.UpdateChatFlow",
"requestData": {
"id":" ** chat id **",
"id":" ** fill in id **",
"message": "How are you today?"
}
}