Skip to content

Commit 6e7dd19

Browse files
1.0.0-rc1
1 parent 8dea9dd commit 6e7dd19

File tree

1 file changed

+59
-0
lines changed

1 file changed

+59
-0
lines changed

onixlabs-corda-core-workflow/src/main/kotlin/io/onixlabs/corda/core/workflow/FlowLogicExtensions.kt

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ import net.corda.core.identity.Party
3131
import net.corda.core.internal.randomOrNull
3232
import net.corda.core.node.ServiceHub
3333
import net.corda.core.transactions.SignedTransaction
34+
import net.corda.core.transactions.TransactionBuilder
3435
import net.corda.core.utilities.ProgressTracker.Step
3536

3637
/**
@@ -102,6 +103,64 @@ fun FlowLogic<*>.initiateFlows(parties: Iterable<AbstractParty>, vararg states:
102103
.toSet()
103104
}
104105

106+
/**
107+
* Checks that sufficient flow sessions have been provided for the specified states.
108+
*
109+
* Assuming that the specified states will be used as input or output states in a transaction, this function will
110+
* extract a set of all state participants, excluding identities owned by the initiating node, and then check
111+
* that a flow session exists for each participant.
112+
*
113+
* @param sessions The flow sessions that have been provided to the flow.
114+
* @param states The states that will be used as input or output states in a transaction.
115+
* @throws FlowException if a required counter-party session is missing for a state participant.
116+
*/
117+
@Suspendable
118+
fun FlowLogic<*>.checkSufficientSessions(sessions: Iterable<FlowSession>, states: Iterable<ContractState>) {
119+
val stateCounterparties = states
120+
.flatMap { it.participants }
121+
.filter { it !in serviceHub.myInfo.legalIdentities }
122+
.toSet()
123+
124+
val sessionCounterparties = sessions
125+
.map { it.counterparty }
126+
.toSet()
127+
128+
stateCounterparties.forEach {
129+
if (it !in sessionCounterparties) {
130+
throw FlowException("A flow session must be provided for the specified counter-party: $it.")
131+
}
132+
}
133+
}
134+
135+
/**
136+
* Checks that sufficient flow sessions have been provided for the specified states.
137+
*
138+
* Assuming that the specified states will be used as input or output states in a transaction, this function will
139+
* extract a set of all state participants, excluding identities owned by the initiating node, and then check
140+
* that a flow session exists for each participant.
141+
*
142+
* @param sessions The flow sessions that have been provided to the flow.
143+
* @param states The states that will be used as input or output states in a transaction.
144+
* @throws FlowException if a required counter-party session is missing for a state participant.
145+
*/
146+
@Suspendable
147+
fun FlowLogic<*>.checkSufficientSessions(sessions: Iterable<FlowSession>, vararg states: ContractState) {
148+
checkSufficientSessions(sessions, states.toSet())
149+
}
150+
151+
/**
152+
* Checks that sufficient flow sessions have been provided for the specified transaction.
153+
*
154+
* @param sessions The flow sessions that have been provided to the flow.
155+
* @param transaction The transaction for which to check that sufficient flow sessions exist.
156+
* @throws FlowException if a required counter-party session is missing for a state participant.
157+
*/
158+
@Suspendable
159+
fun FlowLogic<*>.checkSufficientSessions(sessions: Iterable<FlowSession>, transaction: TransactionBuilder) {
160+
val ledgerTransaction = transaction.toLedgerTransaction(serviceHub)
161+
checkSufficientSessions(sessions, ledgerTransaction.inputStates + ledgerTransaction.outputStates)
162+
}
163+
105164
/**
106165
* Finds a recorded transaction in the vault for the specified transaction hash.
107166
*

0 commit comments

Comments
 (0)