diff --git a/contracts/src/test/kotlin/com/r3corda/contracts/asset/CashTests.kt b/contracts/src/test/kotlin/com/r3corda/contracts/asset/CashTests.kt index 9c7d35f9b9..7a9ca1c085 100644 --- a/contracts/src/test/kotlin/com/r3corda/contracts/asset/CashTests.kt +++ b/contracts/src/test/kotlin/com/r3corda/contracts/asset/CashTests.kt @@ -50,7 +50,7 @@ class CashTests { val vault: VaultService get() = services.vaultService lateinit var dataSource: Closeable lateinit var database: Database - lateinit var VAULT: Vault + lateinit var vaultService: Vault @Before fun setUp() { @@ -60,7 +60,6 @@ class CashTests { database = dataSourceAndDatabase.second databaseTransaction(database) { services = object : MockServices() { - override val keyManagementService: MockKeyManagementService = MockKeyManagementService(MINI_CORP_KEY, MEGA_CORP_KEY, OUR_KEY) override val vaultService: VaultService = NodeVaultService(this) @@ -82,7 +81,7 @@ class CashTests { services.fillWithSomeTestCash(howMuch = 80.SWISS_FRANCS, atLeastThisManyStates = 1, atMostThisManyStates = 1, issuedBy = MINI_CORP.ref(1), issuerKey = MINI_CORP_KEY, ownedBy = OUR_PUBKEY_1) - VAULT = services.vaultService.currentVault + vaultService = services.vaultService.currentVault } } @@ -557,7 +556,7 @@ class CashTests { val wtx = makeSpend(100.DOLLARS, THEIR_PUBKEY_1) - val vaultState = VAULT.states.elementAt(0) as StateAndRef + val vaultState = vaultService.states.elementAt(0) as StateAndRef assertEquals(vaultState.ref, wtx.inputs[0]) assertEquals(vaultState.state.data.copy(owner = THEIR_PUBKEY_1), wtx.outputs[0].data) assertEquals(OUR_PUBKEY_1, wtx.commands.single { it.value is Cash.Commands.Move }.signers[0]) @@ -572,7 +571,7 @@ class CashTests { val tx = TransactionType.General.Builder(DUMMY_NOTARY) vault.generateSpend(tx, 80.DOLLARS, ALICE_PUBKEY, setOf(MINI_CORP)) - assertEquals(VAULT.states.elementAt(2).ref, tx.inputStates()[0]) + assertEquals(vaultService.states.elementAt(2).ref, tx.inputStates()[0]) } } @@ -583,7 +582,7 @@ class CashTests { val wtx = makeSpend(10.DOLLARS, THEIR_PUBKEY_1) - val vaultState = VAULT.states.elementAt(0) as StateAndRef + val vaultState = vaultService.states.elementAt(0) as StateAndRef assertEquals(vaultState.ref, wtx.inputs[0]) assertEquals(vaultState.state.data.copy(owner = THEIR_PUBKEY_1, amount = 10.DOLLARS `issued by` defaultIssuer), wtx.outputs[0].data) assertEquals(vaultState.state.data.copy(amount = 90.DOLLARS `issued by` defaultIssuer), wtx.outputs[1].data) @@ -597,8 +596,8 @@ class CashTests { databaseTransaction(database) { val wtx = makeSpend(500.DOLLARS, THEIR_PUBKEY_1) - val vaultState0 = VAULT.states.elementAt(0) as StateAndRef - val vaultState1 = VAULT.states.elementAt(1) + val vaultState0 = vaultService.states.elementAt(0) as StateAndRef + val vaultState1 = vaultService.states.elementAt(1) assertEquals(vaultState0.ref, wtx.inputs[0]) assertEquals(vaultState1.ref, wtx.inputs[1]) assertEquals(vaultState0.state.data.copy(owner = THEIR_PUBKEY_1, amount = 500.DOLLARS `issued by` defaultIssuer), wtx.outputs[0].data) @@ -613,9 +612,9 @@ class CashTests { val wtx = makeSpend(580.DOLLARS, THEIR_PUBKEY_1) assertEquals(3, wtx.inputs.size) - val vaultState0 = VAULT.states.elementAt(0) as StateAndRef - val vaultState1 = VAULT.states.elementAt(1) - val vaultState2 = VAULT.states.elementAt(2) as StateAndRef + val vaultState0 = vaultService.states.elementAt(0) as StateAndRef + val vaultState1 = vaultService.states.elementAt(1) + val vaultState2 = vaultService.states.elementAt(2) as StateAndRef assertEquals(vaultState0.ref, wtx.inputs[0]) assertEquals(vaultState1.ref, wtx.inputs[1]) assertEquals(vaultState2.ref, wtx.inputs[2]) diff --git a/core/src/main/kotlin/com/r3corda/core/node/services/Services.kt b/core/src/main/kotlin/com/r3corda/core/node/services/Services.kt index b4bd737aba..322517c859 100644 --- a/core/src/main/kotlin/com/r3corda/core/node/services/Services.kt +++ b/core/src/main/kotlin/com/r3corda/core/node/services/Services.kt @@ -106,7 +106,15 @@ interface VaultService { * Returns a map of how much cash we have in each currency, ignoring details like issuer. Note: currencies for * which we have no cash evaluate to null (not present in map), not 0. */ + @Suppress("UNCHECKED_CAST") val cashBalances: Map> + get() = currentVault.states. + // Select the states we own which are cash, ignore the rest, take the amounts. + mapNotNull { (it.state.data as? FungibleAsset)?.amount }. + // Turn into a Map> like { GBP -> (£100, £500, etc), USD -> ($2000, $50) } + groupBy { it.token.product }. + // Collapse to Map by summing all the amounts of the same currency together. + mapValues { it.value.map { Amount(it.quantity, it.token.product) }.sumOrThrow() } /** * Atomically get the current vault and a stream of updates. Note that the Observable buffers updates until the diff --git a/core/src/main/kotlin/com/r3corda/core/testing/InMemoryVaultService.kt b/core/src/main/kotlin/com/r3corda/core/testing/InMemoryVaultService.kt index f7f970f1ea..2f8d84441c 100644 --- a/core/src/main/kotlin/com/r3corda/core/testing/InMemoryVaultService.kt +++ b/core/src/main/kotlin/com/r3corda/core/testing/InMemoryVaultService.kt @@ -42,16 +42,6 @@ open class InMemoryVaultService(protected val services: ServiceHub) : SingletonS override val updates: Observable get() = mutex.content._updatesPublisher - @Suppress("UNCHECKED_CAST") - override val cashBalances: Map> - get() = currentVault.states. - // Select the states we own which are cash, ignore the rest, take the amounts. - mapNotNull { (it.state.data as? FungibleAsset)?.amount }. - // Turn into a Map> like { GBP -> (£100, £500, etc), USD -> ($2000, $50) } - groupBy { it.token.product }. - // Collapse to Map by summing all the amounts of the same currency together. - mapValues { it.value.map { Amount(it.quantity, it.token.product) }.sumOrThrow() } - override fun track(): Pair> { return mutex.locked { Pair(vault, updates.bufferUntilSubscribed()) @@ -107,7 +97,8 @@ open class InMemoryVaultService(protected val services: ServiceHub) : SingletonS } override fun generateSpend(tx: TransactionBuilder, amount: Amount, to: PublicKey, onlyFromParties: Set?): Pair> { - throw UnsupportedOperationException("not implemented") //To change body of created functions use File | Settings | File Templates. + // TODO: decommission entirely this Vault implementation + throw UnsupportedOperationException("Should be using NodeVaultService implementation!") } diff --git a/node/src/main/kotlin/com/r3corda/node/services/vault/NodeVaultService.kt b/node/src/main/kotlin/com/r3corda/node/services/vault/NodeVaultService.kt index 1f0011d749..2e50747e49 100644 --- a/node/src/main/kotlin/com/r3corda/node/services/vault/NodeVaultService.kt +++ b/node/src/main/kotlin/com/r3corda/node/services/vault/NodeVaultService.kt @@ -84,16 +84,6 @@ class NodeVaultService(private val services: ServiceHub) : SingletonSerializeAsT override val updates: Observable get() = mutex.locked { _updatesPublisher } - @Suppress("UNCHECKED_CAST") - override val cashBalances: Map> - get() = currentVault.states. - // Select the states we own which are cash, ignore the rest, take the amounts. - mapNotNull { (it.state.data as? FungibleAsset)?.amount }. - // Turn into a Map> like { GBP -> (£100, £500, etc), USD -> ($2000, $50) } - groupBy { it.token.product }. - // Collapse to Map by summing all the amounts of the same currency together. - mapValues { it.value.map { Amount(it.quantity, it.token.product) }.sumOrThrow() } - override fun track(): Pair> { return mutex.locked { Pair(Vault(allUnconsumedStates()), _updatesPublisher.bufferUntilSubscribed())