From 504ec42720ff8971127b526b64d50369a43b782a Mon Sep 17 00:00:00 2001 From: Jose Coll Date: Thu, 27 Oct 2016 16:35:26 +0100 Subject: [PATCH] Additional method on VaultService to retrieve notes for a transaction --- .../com/r3corda/core/node/services/Services.kt | 2 ++ .../core/testing/InMemoryVaultService.kt | 4 ++++ .../node/services/vault/NodeVaultService.kt | 12 ++++++++++-- .../node/services/NodeVaultServiceTest.kt | 17 ++++++++++++++++- 4 files changed, 32 insertions(+), 3 deletions(-) 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 2c59054095..0426b88340 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 @@ -154,6 +154,8 @@ interface VaultService { * Add a note to an existing [LedgerTransaction] given by its unique [SecureHash] id */ fun addNoteToTransaction(txnId: SecureHash, noteText: String) + + fun getTransactionNotes(txnId: SecureHash): Iterable } inline fun VaultService.linearHeadsOfType() = linearHeadsOfType_(T::class.java) 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 19c5d81f37..3813e99582 100644 --- a/core/src/main/kotlin/com/r3corda/core/testing/InMemoryVaultService.kt +++ b/core/src/main/kotlin/com/r3corda/core/testing/InMemoryVaultService.kt @@ -99,6 +99,10 @@ open class InMemoryVaultService(protected val services: ServiceHub) : SingletonS throw UnsupportedOperationException("not implemented") //To change body of created functions use File | Settings | File Templates. } + override fun getTransactionNotes(txnId: SecureHash): Iterable { + throw UnsupportedOperationException("not implemented") //To change body of created functions use File | Settings | File Templates. + } + private fun isRelevant(state: ContractState, ourKeys: Set): Boolean { return if (state is OwnableState) { state.owner in ourKeys 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 42b22d20f5..2810344ec5 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 @@ -13,6 +13,7 @@ import com.r3corda.core.transactions.WireTransaction import com.r3corda.core.utilities.loggerFor import com.r3corda.core.utilities.trace import com.r3corda.node.utilities.* +import kotlinx.support.jdk8.collections.putIfAbsent import org.jetbrains.exposed.sql.ResultRow import org.jetbrains.exposed.sql.statements.InsertStatement import rx.Observable @@ -134,9 +135,16 @@ class NodeVaultService(private val services: ServiceHub) : SingletonSerializeAsT override fun addNoteToTransaction(txnId: SecureHash, noteText: String) { mutex.locked { - transactionNotes.getOrPut(key = txnId, defaultValue = { + val notes = transactionNotes.getOrPut(key = txnId, defaultValue = { setOf(noteText) - }).plus(noteText) + }) + transactionNotes.put(txnId, notes.plus(noteText)) + } + } + + override fun getTransactionNotes(txnId: SecureHash): Iterable { + mutex.locked { + return transactionNotes.get(txnId)!!.asIterable() } } diff --git a/node/src/test/kotlin/com/r3corda/node/services/NodeVaultServiceTest.kt b/node/src/test/kotlin/com/r3corda/node/services/NodeVaultServiceTest.kt index bf01373dcb..10405cba9c 100644 --- a/node/src/test/kotlin/com/r3corda/node/services/NodeVaultServiceTest.kt +++ b/node/src/test/kotlin/com/r3corda/node/services/NodeVaultServiceTest.kt @@ -3,6 +3,7 @@ package com.r3corda.node.services import com.r3corda.contracts.asset.Cash import com.r3corda.contracts.testing.fillWithSomeTestCash import com.r3corda.core.contracts.DOLLARS +import com.r3corda.core.contracts.POUNDS import com.r3corda.core.contracts.TransactionType import com.r3corda.core.contracts.`issued by` import com.r3corda.core.node.services.TxWritableStorageService @@ -109,9 +110,23 @@ class NodeVaultServiceTest { services.recordTransactions(listOf(usefulTX)) - services.vaultService.addNoteToTransaction(usefulTX.id, "Sample Note 1") + services.vaultService.addNoteToTransaction(usefulTX.id, "USD Sample Note 1") + services.vaultService.addNoteToTransaction(usefulTX.id, "USD Sample Note 2") + services.vaultService.addNoteToTransaction(usefulTX.id, "USD Sample Note 3") assertEquals(1, services.vaultService.currentVault.transactionNotes.toList().size) + assertEquals(3, services.vaultService.getTransactionNotes(usefulTX.id).count()) + // Issue more Money (GBP) + val anotherTX = TransactionType.General.Builder(null).apply { + Cash().generateIssue(this, 200.POUNDS `issued by` MEGA_CORP.ref(1), freshKey.public, DUMMY_NOTARY) + signWith(MEGA_CORP_KEY) + }.toSignedTransaction() + + services.recordTransactions(listOf(anotherTX)) + + services.vaultService.addNoteToTransaction(anotherTX.id, "GPB Sample Note 1") + assertEquals(2, services.vaultService.currentVault.transactionNotes.toList().size) + assertEquals(1, services.vaultService.getTransactionNotes(anotherTX.id).count()) } } } \ No newline at end of file