corda

Can you create flow sessions for multiple participants in Corda


I've been trying to learn Corda and I have a specific use case for a contract which involved multiple participants. Forgive me if my understanding is incorrect and please correct me if I'm wrong! :)

From what I understand, the contract is where we implement the verification functionality for a a transaction that calls upon said contract. The state can hold the 'state' of a current transaction along with any data regarding the transaction. The flow is the the business logic associated with a state/contract.

I have a specific use case that I want to address within a contract, and this involves multiple parties that share the same contract/transaction info. I want a state to be able to hold multiple participants.

Purely from what I understand, I have coded the following for a state:

@BelongsToContract(MasterContract.class)
public class MasterState implements ContractState {

    private final List<Party> employers = emptyList();
    private final List<Party> contractors = emptyList();
    private final String projectName;

    public MasterState(String projectName, List<Party> employers, List<Party> contractors) {
        this.projectName = projectName;
        this.employers.addAll(employers);
        this.contractors.addAll(contractors);
    }

    public String getProjectName() {
        return projectName;
    }

    public List<Party> getEmployers() {
        return employers;
    }

    public List<Party> getContractors() {
        return contractors;
    }

    @Override
    public List<AbstractParty> getParticipants() {
        List<AbstractParty> allParts = new ArrayList<>();
        allParts.addAll(employers);
        allParts.addAll(contractors);
        return allParts;
    }
}

I want to be able to create (via a Command) a new instance of MasterContract by providing multiple 'employers' or 'contractors'. I am trying to define: MasterCreationFlow.call() as follows:

@Suspendable
    @Override
    public Void call() throws FlowException {
        Party notary = getServiceHub().getNetworkMapCache().getNotaryIdentities().get(0);

        // Create the transaction components
        JCTMasterState outputState = new JCTMasterState(projectName, Arrays.asList(getOurIdentity()), contractors);

        //List of required signers:
        List<PublicKey> requiredSigners = Arrays.asList(getOurIdentity().getOwningKey());
        requiredSigners.addAll(getOwningKeys(contractors));

        Command createCommand = new Command<>(new JCTMasterContract.Create(), requiredSigners);
...

However, I'm stuck with the idea of InitiateFlow(). As it seems like you can only do this between 2 parties. I understand that Corda is point-to-point. But I want to understand what exactly FlowSession does? So far, I have read that FlowSessions are just a channel between 2 parties that is then consumed by some SubFlow. Is there any way to extend FlowSession to create a shared session between multiple counter-parties? Or would I have to initiate multiple flows?

Thanks in advance!


Solution

  • You are right that FlowSession is between 2 parties, which is the party calling initiateFlow(someParty) method and someParty.

    So, in order for you to create multiple sessions between your flow initiator and your contractors, you can do this:

    Set<FlowSession> sessions = contractors.stream().map(it -> 
                                initiateFlow(it)).collect(Collectors.toSet());
    

    Then for instance you can pass the sessions to collect signatures from the contractors:

    final SignedTransaction fullySignedTx = subFlow(new CollectSignaturesFlow(partSignedTx, 
                                        sessions, CollectSignaturesFlow.Companion.tracker()));