diff --git a/.gitignore b/.gitignore index c99c3ac..c81757e 100644 --- a/.gitignore +++ b/.gitignore @@ -77,7 +77,6 @@ bin/ - *.cpi *.cpb *.cpk diff --git a/contracts/src/main/java/com/r3/developers/csdetemplate/utxoexample/contracts/ChatContract.java b/contracts/src/main/java/com/r3/developers/csdetemplate/utxoexample/contracts/ChatContract.java index 7e114ad..e42f0ca 100644 --- a/contracts/src/main/java/com/r3/developers/csdetemplate/utxoexample/contracts/ChatContract.java +++ b/contracts/src/main/java/com/r3/developers/csdetemplate/utxoexample/contracts/ChatContract.java @@ -1,8 +1,11 @@ package com.r3.developers.csdetemplate.utxoexample.contracts; +import com.r3.developers.csdetemplate.utxoexample.states.ChatState; +import net.corda.v5.base.exceptions.CordaRuntimeException; import net.corda.v5.ledger.utxo.Command; import net.corda.v5.ledger.utxo.Contract; import net.corda.v5.ledger.utxo.ContractState; +import net.corda.v5.ledger.utxo.StateAndRef; import net.corda.v5.ledger.utxo.transaction.UtxoLedgerTransaction; import org.jetbrains.annotations.NotNull; @@ -11,36 +14,54 @@ import java.util.Set; import static java.util.Objects.*; +// todo: START here + public class ChatContract implements Contract { public static class Create implements Command { } public static class Update implements Command { } - @Override - public boolean isRelevant(@NotNull ContractState state, @NotNull Set myKeys) { - return Contract.super.isRelevant(state, myKeys); - } +// @Override +// public boolean isRelevant(@NotNull ContractState state, @NotNull Set myKeys) { +// return Contract.super.isRelevant(state, myKeys); +// } @Override - public void verify(@NotNull UtxoLedgerTransaction transaction) throws IllegalArgumentException { - Command command = requireNonNull( transaction.getCommands().get(0), "Require a single command"); + public void verify(UtxoLedgerTransaction transaction) { + + +// Command command = requireNonNull( transaction.getCommands().get(0), "Require a single command"); // this doesn't ensure there is one command + requireThat( transaction.getCommands().size() == 1, "Require a single command."); + Command command = transaction.getCommands().get(0); + + ChatState input = transaction.getOutputStates(ChatState.class).get(0); + ChatState output = transaction.getInputStates(ChatState.class).get(0); + + requireThat(output.getParticipants().size() == 2, "The output state should have two and only two participants."); if(command.getClass() == Create.class) { - requireThat(transaction.getInputContractStates().isEmpty(), "When command is Create there should be no input state"); - requireThat(transaction.getOutputContractStates().size() == 1, "When command is Create there should be one and only one output state"); + requireThat(transaction.getInputContractStates().isEmpty(), "When command is Create there should be no input states."); + requireThat(transaction.getOutputContractStates().size() == 1, "When command is Create there should be one and only one output state."); } else if(command.getClass() == Update.class) { - requireThat(transaction.getInputContractStates().size() == 1, "When command is Update there should be one and only one input state"); - requireThat(transaction.getOutputContractStates().size() == 1, "When command is Update there should be one and only one output state"); + requireThat(transaction.getInputContractStates().size() == 1, "When command is Update there should be one and only one input state."); + requireThat(transaction.getOutputContractStates().size() == 1, "When command is Update there should be one and only one output state."); + requireThat(input.getId() == output.getId(), "When command is Update id must not change."); + requireThat(input.getChatName() == output.getChatName(), "When command is Update chatName must not change."); + requireThat( + input.getParticipants().containsAll(output.getParticipants()) && + output.getParticipants().containsAll(input.getParticipants()), + "When command is Update participants must not change."); + } else { - throw new IllegalArgumentException("Unsupported command"); + throw new CordaRuntimeException("Unsupported command"); } } - private void requireThat(boolean asserted, String errorMessage) throws IllegalArgumentException { + private void requireThat(boolean asserted, String errorMessage) { if(!asserted) { - throw new IllegalArgumentException("Failed requirement: " + errorMessage); + throw new CordaRuntimeException("Failed requirement: " + errorMessage); } } } diff --git a/contracts/src/main/java/com/r3/developers/csdetemplate/utxoexample/states/ChatState.java b/contracts/src/main/java/com/r3/developers/csdetemplate/utxoexample/states/ChatState.java index 64af258..20f80ef 100644 --- a/contracts/src/main/java/com/r3/developers/csdetemplate/utxoexample/states/ChatState.java +++ b/contracts/src/main/java/com/r3/developers/csdetemplate/utxoexample/states/ChatState.java @@ -11,6 +11,8 @@ import org.jetbrains.annotations.NotNull; import java.security.PublicKey; import java.util.*; +// todo: Clear out commented code + //@CordaSerializable @BelongsToContract(ChatContract.class) public class ChatState implements ContractState { @@ -38,17 +40,17 @@ public class ChatState implements ContractState { this.participants = participants; } - // Convenience constructor for initial ChatState objects that need a new UUID generated. - public ChatState(String chatName, - MemberX500Name messageFrom, - String message, - List participants) { - this(UUID.randomUUID(), - chatName, - messageFrom, - message, - participants); - } +// // Convenience constructor for initial ChatState objects that need a new UUID generated. +// public ChatState(String chatName, +// MemberX500Name messageFrom, +// String message, +// List participants) { +// this(UUID.randomUUID(), +// chatName, +// messageFrom, +// message, +// participants); +// } public UUID getId() { return id; diff --git a/workflows/src/main/java/com/r3/developers/csdetemplate/utxoexample/workflows/CreateNewChatFlow.java b/workflows/src/main/java/com/r3/developers/csdetemplate/utxoexample/workflows/CreateNewChatFlow.java index dd51fdd..c433fab 100644 --- a/workflows/src/main/java/com/r3/developers/csdetemplate/utxoexample/workflows/CreateNewChatFlow.java +++ b/workflows/src/main/java/com/r3/developers/csdetemplate/utxoexample/workflows/CreateNewChatFlow.java @@ -24,6 +24,7 @@ import java.time.Duration; import java.time.Instant; import java.util.Arrays; import java.util.Objects; +import java.util.UUID; import static java.util.Objects.*; @@ -65,7 +66,7 @@ public class CreateNewChatFlow implements RPCStartableFlow { "can't find other member" ); - ChatState chatState = new ChatState( + ChatState chatState = new ChatState(UUID.randomUUID(), flowArgs.getChatName(), myInfo.getName(), flowArgs.getMessage(),