update contract to match Kotlin

This commit is contained in:
mattbradburyr3 2023-01-29 09:26:32 +00:00
parent 2d507701d2
commit 7efe9a39f4
4 changed files with 49 additions and 26 deletions

1
.gitignore vendored
View File

@ -77,7 +77,6 @@ bin/
*.cpi *.cpi
*.cpb *.cpb
*.cpk *.cpk

View File

@ -1,8 +1,11 @@
package com.r3.developers.csdetemplate.utxoexample.contracts; 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.Command;
import net.corda.v5.ledger.utxo.Contract; import net.corda.v5.ledger.utxo.Contract;
import net.corda.v5.ledger.utxo.ContractState; import net.corda.v5.ledger.utxo.ContractState;
import net.corda.v5.ledger.utxo.StateAndRef;
import net.corda.v5.ledger.utxo.transaction.UtxoLedgerTransaction; import net.corda.v5.ledger.utxo.transaction.UtxoLedgerTransaction;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
@ -11,36 +14,54 @@ import java.util.Set;
import static java.util.Objects.*; import static java.util.Objects.*;
// todo: START here
public class ChatContract implements Contract { public class ChatContract implements Contract {
public static class Create implements Command { } public static class Create implements Command { }
public static class Update implements Command { } public static class Update implements Command { }
@Override // @Override
public boolean isRelevant(@NotNull ContractState state, @NotNull Set<? extends PublicKey> myKeys) { // public boolean isRelevant(@NotNull ContractState state, @NotNull Set<? extends PublicKey> myKeys) {
return Contract.super.isRelevant(state, myKeys); // return Contract.super.isRelevant(state, myKeys);
} // }
@Override @Override
public void verify(@NotNull UtxoLedgerTransaction transaction) throws IllegalArgumentException { public void verify(UtxoLedgerTransaction transaction) {
Command command = requireNonNull( transaction.getCommands().get(0), "Require a single command");
// 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) { if(command.getClass() == Create.class) {
requireThat(transaction.getInputContractStates().isEmpty(), "When command is Create there should be no input 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"); requireThat(transaction.getOutputContractStates().size() == 1, "When command is Create there should be one and only one output state.");
} }
else if(command.getClass() == Update.class) { 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.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.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 { 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) { if(!asserted) {
throw new IllegalArgumentException("Failed requirement: " + errorMessage); throw new CordaRuntimeException("Failed requirement: " + errorMessage);
} }
} }
} }

View File

@ -11,6 +11,8 @@ import org.jetbrains.annotations.NotNull;
import java.security.PublicKey; import java.security.PublicKey;
import java.util.*; import java.util.*;
// todo: Clear out commented code
//@CordaSerializable //@CordaSerializable
@BelongsToContract(ChatContract.class) @BelongsToContract(ChatContract.class)
public class ChatState implements ContractState { public class ChatState implements ContractState {
@ -38,17 +40,17 @@ public class ChatState implements ContractState {
this.participants = participants; this.participants = participants;
} }
// Convenience constructor for initial ChatState objects that need a new UUID generated. // // Convenience constructor for initial ChatState objects that need a new UUID generated.
public ChatState(String chatName, // public ChatState(String chatName,
MemberX500Name messageFrom, // MemberX500Name messageFrom,
String message, // String message,
List<PublicKey> participants) { // List<PublicKey> participants) {
this(UUID.randomUUID(), // this(UUID.randomUUID(),
chatName, // chatName,
messageFrom, // messageFrom,
message, // message,
participants); // participants);
} // }
public UUID getId() { public UUID getId() {
return id; return id;

View File

@ -24,6 +24,7 @@ import java.time.Duration;
import java.time.Instant; import java.time.Instant;
import java.util.Arrays; import java.util.Arrays;
import java.util.Objects; import java.util.Objects;
import java.util.UUID;
import static java.util.Objects.*; import static java.util.Objects.*;
@ -65,7 +66,7 @@ public class CreateNewChatFlow implements RPCStartableFlow {
"can't find other member" "can't find other member"
); );
ChatState chatState = new ChatState( ChatState chatState = new ChatState(UUID.randomUUID(),
flowArgs.getChatName(), flowArgs.getChatName(),
myInfo.getName(), myInfo.getName(),
flowArgs.getMessage(), flowArgs.getMessage(),