diff --git a/core/src/main/kotlin/net/corda/core/node/services/Services.kt b/core/src/main/kotlin/net/corda/core/node/services/Services.kt index 2e4bc8031b..536e032910 100644 --- a/core/src/main/kotlin/net/corda/core/node/services/Services.kt +++ b/core/src/main/kotlin/net/corda/core/node/services/Services.kt @@ -247,6 +247,13 @@ interface VaultService { fun softLockedStates(lockId: UUID? = null): List> // DOCEND SoftLockAPI + + /** + * TODO: this function should be private to the vault, but currently Cash Exit functionality + * is implemented in a separate module (finance) and requires access to it. + */ + @Suspendable + fun unconsumedStatesForSpending(amount: Amount, onlyFromIssuerParties: Set? = null, notary: Party? = null, lockId: UUID): List> } inline fun VaultService.unconsumedStates(includeSoftLockedStates: Boolean = true): Iterable> = diff --git a/finance/src/main/kotlin/net/corda/flows/CashExitFlow.kt b/finance/src/main/kotlin/net/corda/flows/CashExitFlow.kt index a31930a6e0..c80903f95a 100644 --- a/finance/src/main/kotlin/net/corda/flows/CashExitFlow.kt +++ b/finance/src/main/kotlin/net/corda/flows/CashExitFlow.kt @@ -7,8 +7,6 @@ import net.corda.core.contracts.InsufficientBalanceException import net.corda.core.contracts.TransactionType import net.corda.core.contracts.issuedBy import net.corda.core.crypto.Party -import net.corda.core.node.services.Vault -import net.corda.core.node.services.unconsumedStates import net.corda.core.serialization.OpaqueBytes import net.corda.core.transactions.SignedTransaction import net.corda.core.transactions.TransactionBuilder @@ -35,11 +33,12 @@ class CashExitFlow(val amount: Amount, val issueRef: OpaqueBytes, prog progressTracker.currentStep = GENERATING_TX val builder: TransactionBuilder = TransactionType.General.Builder(null) val issuer = serviceHub.myInfo.legalIdentity.ref(issueRef) + val exitStates = serviceHub.vaultService.unconsumedStatesForSpending(amount, setOf(issuer.party), builder.notary, builder.lockId) try { Cash().generateExit( builder, amount.issuedBy(issuer), - serviceHub.vaultService.unconsumedStates().filter { it.state.data.owner == issuer.party.owningKey }) + exitStates) } catch (e: InsufficientBalanceException) { throw CashException("Exiting more cash than exists", e) } diff --git a/node/src/main/kotlin/net/corda/node/services/vault/NodeVaultService.kt b/node/src/main/kotlin/net/corda/node/services/vault/NodeVaultService.kt index 86e4b27fdd..58b4a2dc5c 100644 --- a/node/src/main/kotlin/net/corda/node/services/vault/NodeVaultService.kt +++ b/node/src/main/kotlin/net/corda/node/services/vault/NodeVaultService.kt @@ -326,7 +326,7 @@ class NodeVaultService(private val services: ServiceHub, dataSourceProperties: P val spendLock: ReentrantLock = ReentrantLock() @Suspendable - internal fun unconsumedStatesForSpending(amount: Amount, onlyFromIssuerParties: Set? = null, notary: Party? = null, lockId: UUID): List> { + override fun unconsumedStatesForSpending(amount: Amount, onlyFromIssuerParties: Set?, notary: Party?, lockId: UUID): List> { val issuerKeysStr = onlyFromIssuerParties?.fold("") { left, right -> left + "('${right.owningKey.toBase58String()}')," }?.dropLast(1) var stateAndRefs = mutableListOf>()