Created
July 27, 2020 03:37
-
-
Save peterli-r3/2a824fa9d960f76168167d4dcf7ac5e0 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.badge.flows; | |
import co.paralleluniverse.fibers.Suspendable; | |
import com.badge.contracts.BadgeContract; | |
import com.badge.states.BadgeState; | |
import net.corda.core.contracts.StateAndRef; | |
import net.corda.core.contracts.UniqueIdentifier; | |
import net.corda.core.flows.*; | |
import net.corda.core.identity.Party; | |
import net.corda.core.node.services.Vault; | |
import net.corda.core.node.services.vault.QueryCriteria; | |
import net.corda.core.transactions.SignedTransaction; | |
import net.corda.core.transactions.TransactionBuilder; | |
import net.corda.core.utilities.ProgressTracker; | |
import java.util.Arrays; | |
import java.util.Date; | |
import java.util.List; | |
import java.util.UUID; | |
import java.util.stream.Collectors; | |
// ****************** | |
// * Responder flow * | |
// ****************** | |
public class ShowFlow { | |
@InitiatingFlow | |
@StartableByRPC | |
public static class ShowBadge extends FlowLogic<String> { | |
// We will not use these ProgressTracker for this Hello-World sample | |
private final ProgressTracker progressTracker = new ProgressTracker(); | |
@Override | |
public ProgressTracker getProgressTracker() { | |
return progressTracker; | |
} | |
//private variables | |
private UUID badgeId; | |
//public constructor | |
public ShowBadge(UUID badgeId) { | |
this.badgeId = badgeId; | |
} | |
@Suspendable | |
@Override | |
public String call() throws FlowException { | |
// Step 1. Get a reference to the notary service on our network and our key pair. | |
// Note: ongoing work to support multiple notary identities is still in progress. | |
final Party notary = getServiceHub().getNetworkMapCache().getNotaryIdentities().get(0); | |
// Step 2. query the badge from the vault. | |
QueryCriteria.LinearStateQueryCriteria inputCriteria = new QueryCriteria.LinearStateQueryCriteria().withUuid(Arrays.asList(badgeId)); | |
StateAndRef inputStateAndRef = getServiceHub().getVaultService().queryBy(BadgeState.class, inputCriteria).getStates().get(0); | |
BadgeState input = (BadgeState) inputStateAndRef.getState().getData(); | |
// Step 3. Compose the State that carries the Hello World message | |
int balanceNew = input.getShowOffBalance()-1; | |
final BadgeState output = new BadgeState(input.getName(),new UniqueIdentifier(null,badgeId), | |
input.getIssueDate(),input.getIssueAgency(), | |
input.getReceiver(),input.getDescription(),balanceNew); | |
// Step 4.Create a new TransactionBuilder object. | |
final TransactionBuilder builder = new TransactionBuilder(notary); | |
// Step 5. Add the iou as an output state, as well as a command to the transaction builder. | |
builder.addInputState(inputStateAndRef); | |
builder.addOutputState(output); | |
builder.addCommand(new BadgeContract.Commands.show(), | |
Arrays.asList(getOurIdentity().getOwningKey())); | |
// Step 6. Verify and sign it with our KeyPair. | |
builder.verify(getServiceHub()); | |
final SignedTransaction ptx = getServiceHub().signInitialTransaction(builder); | |
FlowSession session = initiateFlow(input.getIssueAgency()); | |
// Step 7. Assuming no exceptions, we can now finalise the transaction | |
SignedTransaction stx = subFlow(new FinalityFlow(ptx,session)); | |
return "The "+input.getName()+ " badge has "+ balanceNew + " times left to show!"; | |
} | |
} | |
@InitiatedBy(ShowBadge.class) | |
public static class ShowBadgeResponder extends FlowLogic<Void> { | |
//private variable | |
private FlowSession counterpartySession; | |
//Constructor | |
public ShowBadgeResponder(FlowSession counterpartySession) { | |
this.counterpartySession = counterpartySession; | |
} | |
@Suspendable | |
@Override | |
public Void call() throws FlowException { | |
SignedTransaction signedTransaction = subFlow(new SignTransactionFlow(counterpartySession) { | |
@Suspendable | |
@Override | |
protected void checkTransaction(SignedTransaction stx) throws FlowException { | |
} | |
}); | |
//Stored the transaction into data base. | |
subFlow(new ReceiveFinalityFlow(counterpartySession, signedTransaction.getId())); | |
return null; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment