Additional method on VaultService to retrieve notes for a transaction

This commit is contained in:
Jose Coll 2016-10-27 16:35:26 +01:00
parent f2e98ffba5
commit 504ec42720
4 changed files with 32 additions and 3 deletions

View File

@ -154,6 +154,8 @@ interface VaultService {
* Add a note to an existing [LedgerTransaction] given by its unique [SecureHash] id * Add a note to an existing [LedgerTransaction] given by its unique [SecureHash] id
*/ */
fun addNoteToTransaction(txnId: SecureHash, noteText: String) fun addNoteToTransaction(txnId: SecureHash, noteText: String)
fun getTransactionNotes(txnId: SecureHash): Iterable<String>
} }
inline fun <reified T : LinearState> VaultService.linearHeadsOfType() = linearHeadsOfType_(T::class.java) inline fun <reified T : LinearState> VaultService.linearHeadsOfType() = linearHeadsOfType_(T::class.java)

View File

@ -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. throw UnsupportedOperationException("not implemented") //To change body of created functions use File | Settings | File Templates.
} }
override fun getTransactionNotes(txnId: SecureHash): Iterable<String> {
throw UnsupportedOperationException("not implemented") //To change body of created functions use File | Settings | File Templates.
}
private fun isRelevant(state: ContractState, ourKeys: Set<PublicKey>): Boolean { private fun isRelevant(state: ContractState, ourKeys: Set<PublicKey>): Boolean {
return if (state is OwnableState) { return if (state is OwnableState) {
state.owner in ourKeys state.owner in ourKeys

View File

@ -13,6 +13,7 @@ import com.r3corda.core.transactions.WireTransaction
import com.r3corda.core.utilities.loggerFor import com.r3corda.core.utilities.loggerFor
import com.r3corda.core.utilities.trace import com.r3corda.core.utilities.trace
import com.r3corda.node.utilities.* import com.r3corda.node.utilities.*
import kotlinx.support.jdk8.collections.putIfAbsent
import org.jetbrains.exposed.sql.ResultRow import org.jetbrains.exposed.sql.ResultRow
import org.jetbrains.exposed.sql.statements.InsertStatement import org.jetbrains.exposed.sql.statements.InsertStatement
import rx.Observable import rx.Observable
@ -134,9 +135,16 @@ class NodeVaultService(private val services: ServiceHub) : SingletonSerializeAsT
override fun addNoteToTransaction(txnId: SecureHash, noteText: String) { override fun addNoteToTransaction(txnId: SecureHash, noteText: String) {
mutex.locked { mutex.locked {
transactionNotes.getOrPut(key = txnId, defaultValue = { val notes = transactionNotes.getOrPut(key = txnId, defaultValue = {
setOf(noteText) setOf(noteText)
}).plus(noteText) })
transactionNotes.put(txnId, notes.plus(noteText))
}
}
override fun getTransactionNotes(txnId: SecureHash): Iterable<String> {
mutex.locked {
return transactionNotes.get(txnId)!!.asIterable()
} }
} }

View File

@ -3,6 +3,7 @@ package com.r3corda.node.services
import com.r3corda.contracts.asset.Cash import com.r3corda.contracts.asset.Cash
import com.r3corda.contracts.testing.fillWithSomeTestCash import com.r3corda.contracts.testing.fillWithSomeTestCash
import com.r3corda.core.contracts.DOLLARS import com.r3corda.core.contracts.DOLLARS
import com.r3corda.core.contracts.POUNDS
import com.r3corda.core.contracts.TransactionType import com.r3corda.core.contracts.TransactionType
import com.r3corda.core.contracts.`issued by` import com.r3corda.core.contracts.`issued by`
import com.r3corda.core.node.services.TxWritableStorageService import com.r3corda.core.node.services.TxWritableStorageService
@ -109,9 +110,23 @@ class NodeVaultServiceTest {
services.recordTransactions(listOf(usefulTX)) 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(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())
} }
} }
} }