|
1 | | -/** |
2 | | - * Copyright 2020 Matthew Layton |
3 | | - * |
4 | | - * Licensed under the Apache License, Version 2.0 (the "License"); |
5 | | - * you may not use this file except in compliance with the License. |
6 | | - * You may obtain a copy of the License at |
7 | | - * |
8 | | - * http://www.apache.org/licenses/LICENSE-2.0 |
9 | | - * |
10 | | - * Unless required by applicable law or agreed to in writing, software |
11 | | - * distributed under the License is distributed on an "AS IS" BASIS, |
12 | | - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
13 | | - * See the License for the specific language governing permissions and |
14 | | - * limitations under the License. |
15 | | - */ |
16 | | - |
17 | 1 | package io.onixlabs.corda.core.workflow |
18 | 2 |
|
19 | 3 | import co.paralleluniverse.fibers.Suspendable |
20 | 4 | import net.corda.core.contracts.ContractState |
21 | | -import net.corda.core.contracts.StateAndRef |
22 | | -import net.corda.core.contracts.StateRef |
23 | | -import net.corda.core.cordapp.CordappContext |
24 | | -import net.corda.core.crypto.SecureHash |
25 | 5 | import net.corda.core.flows.FlowException |
26 | 6 | import net.corda.core.flows.FlowLogic |
27 | 7 | import net.corda.core.flows.FlowSession |
28 | 8 | import net.corda.core.identity.AbstractParty |
29 | | -import net.corda.core.identity.CordaX500Name |
30 | | -import net.corda.core.identity.Party |
31 | | -import net.corda.core.internal.randomOrNull |
32 | | -import net.corda.core.node.ServiceHub |
33 | | -import net.corda.core.transactions.SignedTransaction |
34 | 9 | import net.corda.core.transactions.TransactionBuilder |
35 | | -import net.corda.core.utilities.ProgressTracker.Step |
36 | | - |
37 | | -/** |
38 | | - * Gets the first available notary. |
39 | | - */ |
40 | | -val FlowLogic<*>.firstNotary: Party |
41 | | - get() = serviceHub.networkMapCache.notaryIdentities.firstOrNull() |
42 | | - ?: throw NoSuchElementException("No available notaries.") |
43 | | - |
44 | | -/** |
45 | | - * Gets a randomly available notary. |
46 | | - */ |
47 | | -val FlowLogic<*>.randomNotary: Party |
48 | | - get() = serviceHub.networkMapCache.notaryIdentities.randomOrNull() |
49 | | - ?: throw NoSuchElementException("No available notaries.") |
50 | | - |
51 | | -/** |
52 | | - * Gets the preferred notary from the CorDapp config, or alternatively a default notary in the event that |
53 | | - * a preferred notary has not been specified in the CorDapp config. |
54 | | - * |
55 | | - * @param serviceHub The service hub which will be used to obtain a notary from the config file. |
56 | | - * @param defaultSelector The selector function to obtain a notary if none have been specified in the CorDapp config. |
57 | | - * @return Returns the preferred or default notary. |
58 | | - * @throws IllegalArgumentException If the preferred notary cannot be found in the network map cache. |
59 | | - */ |
60 | | -@Suspendable |
61 | | -fun FlowLogic<*>.getPreferredNotary( |
62 | | - serviceHub: ServiceHub = this.serviceHub, |
63 | | - defaultSelector: (ServiceHub) -> Party = { firstNotary } |
64 | | -): Party { |
65 | | - val cordappContext: CordappContext = serviceHub.getAppContext() |
66 | | - logger.info("Using the specified cordapp for notary selection: ${cordappContext.cordapp.name}") |
67 | | - return if (serviceHub.getAppContext().config.exists("notary")) { |
68 | | - val name = CordaX500Name.parse(serviceHub.getAppContext().config.getString("notary")) |
69 | | - serviceHub.networkMapCache.getNotary(name) ?: throw IllegalArgumentException( |
70 | | - "Notary with the specified name cannot be found in the network map cache: $name." |
71 | | - ) |
72 | | - } else defaultSelector(serviceHub) |
73 | | -} |
74 | | - |
75 | | -/** |
76 | | - * Sets the current progress tracker step. |
77 | | - * |
78 | | - * @param step The progress tracker step. |
79 | | - * @param log Determines whether to log the step. |
80 | | - * @param additionalLogInfo Additional information to log when setting the current step. |
81 | | - */ |
82 | | -@Suspendable |
83 | | -fun FlowLogic<*>.currentStep(step: Step, log: Boolean = true, additionalLogInfo: String? = null) { |
84 | | - progressTracker?.currentStep = step |
85 | | - |
86 | | - if (log) { |
87 | | - if (additionalLogInfo.isNullOrBlank()) { |
88 | | - logger.info(step.label) |
89 | | - } else { |
90 | | - logger.info("${step.label} ($additionalLogInfo)") |
91 | | - } |
92 | | - } |
93 | | -} |
94 | 10 |
|
95 | 11 | /** |
96 | 12 | * Initiates flow sessions for the specified parties, except for identities that belong to the local node, |
@@ -198,43 +114,3 @@ fun FlowLogic<*>.checkSufficientSessions(sessions: Iterable<FlowSession>, transa |
198 | 114 | val ledgerTransaction = transaction.toLedgerTransaction(serviceHub) |
199 | 115 | checkSufficientSessions(sessions, ledgerTransaction.inputStates + ledgerTransaction.outputStates) |
200 | 116 | } |
201 | | - |
202 | | -/** |
203 | | - * Finds a recorded transaction in the vault for the specified transaction hash. |
204 | | - * |
205 | | - * @param transactionHash The transaction hash of the transaction to find. |
206 | | - * @return Returns a [SignedTransaction] for the specified transaction hash. |
207 | | - * @throws FlowException If a signed Transaction cannot be found for the specified transaction hash. |
208 | | - * |
209 | | - */ |
210 | | -@Suspendable |
211 | | -fun FlowLogic<*>.findTransaction(transactionHash: SecureHash): SignedTransaction { |
212 | | - return serviceHub.validatedTransactions.getTransaction(transactionHash) |
213 | | - ?: throw FlowException("Did not find a transaction with the specified hash: $transactionHash") |
214 | | -} |
215 | | - |
216 | | -/** |
217 | | - * Finds a recorded transaction in the vault for the specified transaction hash. |
218 | | - * |
219 | | - * @param stateRef The state reference of the transaction to find. |
220 | | - * @return Returns a [SignedTransaction] for the specified transaction hash. |
221 | | - * @throws FlowException If a signed Transaction cannot be found for the specified transaction hash. |
222 | | - * |
223 | | - */ |
224 | | -@Suspendable |
225 | | -fun FlowLogic<*>.findTransaction(stateRef: StateRef): SignedTransaction { |
226 | | - return findTransaction(stateRef.txhash) |
227 | | -} |
228 | | - |
229 | | -/** |
230 | | - * Finds a recorded transaction in the vault for the specified transaction hash. |
231 | | - * |
232 | | - * @param stateAndRef The state and reference of the transaction to find. |
233 | | - * @return Returns a [SignedTransaction] for the specified transaction hash. |
234 | | - * @throws FlowException If a signed Transaction cannot be found for the specified transaction hash. |
235 | | - * |
236 | | - */ |
237 | | -@Suspendable |
238 | | -fun FlowLogic<*>.findTransaction(stateAndRef: StateAndRef<*>): SignedTransaction { |
239 | | - return findTransaction(stateAndRef.ref) |
240 | | -} |
0 commit comments