mirror of
https://github.com/corda/corda.git
synced 2025-06-22 17:09:00 +00:00
Add tests for TransactionGraphSearch
This commit is contained in:
@ -5,6 +5,7 @@ import com.r3corda.contracts.cash.Cash
|
|||||||
import com.r3corda.contracts.cash.CASH_PROGRAM_ID
|
import com.r3corda.contracts.cash.CASH_PROGRAM_ID
|
||||||
import com.r3corda.core.contracts.Amount
|
import com.r3corda.core.contracts.Amount
|
||||||
import com.r3corda.core.contracts.Contract
|
import com.r3corda.core.contracts.Contract
|
||||||
|
import com.r3corda.core.contracts.DummyContract
|
||||||
import com.r3corda.core.crypto.NullPublicKey
|
import com.r3corda.core.crypto.NullPublicKey
|
||||||
import com.r3corda.core.crypto.Party
|
import com.r3corda.core.crypto.Party
|
||||||
import com.r3corda.core.testing.DUMMY_NOTARY
|
import com.r3corda.core.testing.DUMMY_NOTARY
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
package com.r3corda.contracts.cash
|
package com.r3corda.contracts.cash
|
||||||
|
|
||||||
import com.r3corda.contracts.DummyContract
|
import com.r3corda.core.contracts.DummyContract
|
||||||
import com.r3corda.contracts.testing.`issued by`
|
import com.r3corda.contracts.testing.`issued by`
|
||||||
import com.r3corda.contracts.testing.`owned by`
|
import com.r3corda.contracts.testing.`owned by`
|
||||||
import com.r3corda.core.contracts.*
|
import com.r3corda.core.contracts.*
|
||||||
|
@ -1,7 +1,5 @@
|
|||||||
package com.r3corda.contracts
|
package com.r3corda.core.contracts
|
||||||
|
|
||||||
import com.r3corda.core.*
|
|
||||||
import com.r3corda.core.contracts.*
|
|
||||||
import com.r3corda.core.crypto.Party
|
import com.r3corda.core.crypto.Party
|
||||||
import com.r3corda.core.crypto.SecureHash
|
import com.r3corda.core.crypto.SecureHash
|
||||||
|
|
@ -13,7 +13,8 @@ import java.util.concurrent.Callable
|
|||||||
*
|
*
|
||||||
* In future, this should support restricting the search by time, and other types of useful query.
|
* In future, this should support restricting the search by time, and other types of useful query.
|
||||||
*
|
*
|
||||||
* TODO: Write unit tests for this.
|
* @param transactions map of transaction id to [SignedTransaction]
|
||||||
|
* @param startPoints transactions to use as starting points for the search
|
||||||
*/
|
*/
|
||||||
class TransactionGraphSearch(val transactions: TransactionStorage,
|
class TransactionGraphSearch(val transactions: TransactionStorage,
|
||||||
val startPoints: List<WireTransaction>) : Callable<List<WireTransaction>> {
|
val startPoints: List<WireTransaction>) : Callable<List<WireTransaction>> {
|
||||||
|
@ -77,7 +77,7 @@ class MockAttachmentStorage : AttachmentStorage {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
class MockTransactionStorage : TransactionStorage {
|
open class MockTransactionStorage : TransactionStorage {
|
||||||
private val txns = HashMap<SecureHash, SignedTransaction>()
|
private val txns = HashMap<SecureHash, SignedTransaction>()
|
||||||
override fun addTransaction(transaction: SignedTransaction) {
|
override fun addTransaction(transaction: SignedTransaction) {
|
||||||
txns[transaction.id] = transaction
|
txns[transaction.id] = transaction
|
||||||
|
@ -0,0 +1,73 @@
|
|||||||
|
package com.r3corda.core.contracts
|
||||||
|
|
||||||
|
import com.r3corda.core.node.services.testing.MockTransactionStorage
|
||||||
|
import com.r3corda.core.testing.DUMMY_NOTARY
|
||||||
|
import com.r3corda.core.testing.MEGA_CORP_KEY
|
||||||
|
import org.junit.Test
|
||||||
|
import java.security.KeyPair
|
||||||
|
import java.security.SecureRandom
|
||||||
|
import kotlin.test.assertEquals
|
||||||
|
|
||||||
|
class TransactionGraphSearchTests {
|
||||||
|
class GraphTransactionStorage(val originTx: SignedTransaction, val inputTx: SignedTransaction): MockTransactionStorage() {
|
||||||
|
init {
|
||||||
|
addTransaction(originTx)
|
||||||
|
addTransaction(inputTx)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fun random31BitValue(): Int = Math.abs(SecureRandom.getInstanceStrong().nextInt())
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Build a pair of transactions. The first issues a dummy output state, and has a command applied, the second then
|
||||||
|
* references that state.
|
||||||
|
*
|
||||||
|
* @param command the command to add to the origin transaction.
|
||||||
|
* @param signer signer for the two transactions and their commands.
|
||||||
|
*/
|
||||||
|
fun buildTransactions(command: CommandData, signer: KeyPair): GraphTransactionStorage {
|
||||||
|
val originTx = TransactionBuilder().apply {
|
||||||
|
addOutputState(DummyContract.State(random31BitValue(), DUMMY_NOTARY))
|
||||||
|
addCommand(command, signer.public)
|
||||||
|
signWith(signer)
|
||||||
|
}.toSignedTransaction(false)
|
||||||
|
val inputTx = TransactionBuilder().apply {
|
||||||
|
addInputState(originTx.tx.outRef<DummyContract.State>(0).ref)
|
||||||
|
signWith(signer)
|
||||||
|
}.toSignedTransaction(false)
|
||||||
|
return GraphTransactionStorage(originTx, inputTx)
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun `return empty from empty`() {
|
||||||
|
val storage = buildTransactions(DummyContract.Commands.Create(), MEGA_CORP_KEY)
|
||||||
|
val search = TransactionGraphSearch(storage, emptyList())
|
||||||
|
search.query = TransactionGraphSearch.Query()
|
||||||
|
val expected = emptyList<WireTransaction>()
|
||||||
|
val actual = search.call()
|
||||||
|
|
||||||
|
assertEquals(expected, actual)
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun `return empty from no match`() {
|
||||||
|
val storage = buildTransactions(DummyContract.Commands.Create(), MEGA_CORP_KEY)
|
||||||
|
val search = TransactionGraphSearch(storage, listOf(storage.inputTx.tx))
|
||||||
|
search.query = TransactionGraphSearch.Query()
|
||||||
|
val expected = emptyList<WireTransaction>()
|
||||||
|
val actual = search.call()
|
||||||
|
|
||||||
|
assertEquals(expected, actual)
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun `return origin on match`() {
|
||||||
|
val storage = buildTransactions(DummyContract.Commands.Create(), MEGA_CORP_KEY)
|
||||||
|
val search = TransactionGraphSearch(storage, listOf(storage.inputTx.tx))
|
||||||
|
search.query = TransactionGraphSearch.Query(DummyContract.Commands.Create::class.java)
|
||||||
|
val expected = listOf(storage.originTx.tx)
|
||||||
|
val actual = search.call()
|
||||||
|
|
||||||
|
assertEquals(expected, actual)
|
||||||
|
}
|
||||||
|
}
|
@ -1,6 +1,6 @@
|
|||||||
package com.r3corda.node.internal.testing
|
package com.r3corda.node.internal.testing
|
||||||
|
|
||||||
import com.r3corda.contracts.DummyContract
|
import com.r3corda.core.contracts.DummyContract
|
||||||
import com.r3corda.core.contracts.StateRef
|
import com.r3corda.core.contracts.StateRef
|
||||||
import com.r3corda.core.crypto.Party
|
import com.r3corda.core.crypto.Party
|
||||||
import com.r3corda.core.seconds
|
import com.r3corda.core.seconds
|
||||||
|
Reference in New Issue
Block a user