From 299286626993bdb925cf3e77569a9a01ef91a293 Mon Sep 17 00:00:00 2001 From: Dominic Fox Date: Thu, 19 Jul 2018 11:45:42 +0100 Subject: [PATCH] Eliminate unnecessary transactions --- .../net/corda/core/flows/AttachmentTests.kt | 19 +++++++++---------- .../core/flows/CollectSignaturesFlowTests.kt | 5 ++--- .../core/flows/ContractUpgradeFlowRPCTest.kt | 19 ++++++++----------- .../core/flows/ContractUpgradeFlowTest.kt | 14 ++++---------- .../net/corda/core/flows/FinalityFlowTests.kt | 2 +- .../core/flows/matchers/flow/FlowMatchers.kt | 2 +- .../core/flows/matchers/rpc/RpcMatchers.kt | 2 +- .../corda/core/flows/mixins/WithContracts.kt | 2 -- .../corda/core/flows/mixins/WithFinality.kt | 4 +--- .../corda/core/flows/mixins/WithMockNet.kt | 10 ++++------ 10 files changed, 31 insertions(+), 48 deletions(-) diff --git a/core/src/test/kotlin/net/corda/core/flows/AttachmentTests.kt b/core/src/test/kotlin/net/corda/core/flows/AttachmentTests.kt index 9c45a72c675..451ef6387b0 100644 --- a/core/src/test/kotlin/net/corda/core/flows/AttachmentTests.kt +++ b/core/src/test/kotlin/net/corda/core/flows/AttachmentTests.kt @@ -5,8 +5,8 @@ import com.natpryce.hamkrest.* import com.natpryce.hamkrest.assertion.assert import net.corda.core.contracts.Attachment import net.corda.core.crypto.SecureHash -import net.corda.core.flows.matchers.flow.willThrow import net.corda.core.flows.matchers.flow.willReturn +import net.corda.core.flows.matchers.flow.willThrow import net.corda.core.flows.mixins.WithMockNet import net.corda.core.identity.CordaX500Name import net.corda.core.identity.Party @@ -20,7 +20,6 @@ import net.corda.testing.core.BOB_NAME import net.corda.testing.core.singleIdentity import net.corda.testing.node.internal.InternalMockNetwork import net.corda.testing.node.internal.InternalMockNodeParameters -import net.corda.testing.node.internal.startFlow import org.junit.AfterClass import org.junit.Test import java.io.ByteArrayOutputStream @@ -149,19 +148,19 @@ class AttachmentTests : WithMockNet { //endregion //region Operations - private fun StartedNode<*>.importAttachment(attachment: ByteArray) = database.transaction { + private fun StartedNode<*>.importAttachment(attachment: ByteArray) = attachments.importAttachment(attachment.inputStream(), "test", null) - }.andRunNetwork() + .andRunNetwork() - private fun StartedNode<*>.updateAttachment(attachment: NodeAttachmentService.DBAttachment) = - database.transaction { session.update(attachment) }.andRunNetwork() + private fun StartedNode<*>.updateAttachment(attachment: NodeAttachmentService.DBAttachment) = database.transaction { + session.update(attachment).andRunNetwork() + } - private fun StartedNode<*>.startAttachmentFlow(hash: SecureHash, otherSide: Party) = services.startFlow( - InitiatingFetchAttachmentsFlow(otherSide, setOf(hash))).andRunNetwork() + private fun StartedNode<*>.startAttachmentFlow(hash: SecureHash, otherSide: Party) = startFlowAndRunNetwork( + InitiatingFetchAttachmentsFlow(otherSide, setOf(hash))) - private fun StartedNode<*>.getAttachmentWithId(id: SecureHash) = database.transaction { + private fun StartedNode<*>.getAttachmentWithId(id: SecureHash) = attachments.openAttachment(id)!! - } //endregion //region Matchers diff --git a/core/src/test/kotlin/net/corda/core/flows/CollectSignaturesFlowTests.kt b/core/src/test/kotlin/net/corda/core/flows/CollectSignaturesFlowTests.kt index 576522606a3..81a6a4061a5 100644 --- a/core/src/test/kotlin/net/corda/core/flows/CollectSignaturesFlowTests.kt +++ b/core/src/test/kotlin/net/corda/core/flows/CollectSignaturesFlowTests.kt @@ -5,8 +5,8 @@ import com.natpryce.hamkrest.assertion.assert import net.corda.core.contracts.Command import net.corda.core.contracts.StateAndContract import net.corda.core.contracts.requireThat -import net.corda.core.flows.matchers.flow.willThrow import net.corda.core.flows.matchers.flow.willReturn +import net.corda.core.flows.matchers.flow.willThrow import net.corda.core.flows.mixins.WithContracts import net.corda.core.identity.CordaX500Name import net.corda.core.identity.Party @@ -93,12 +93,11 @@ class CollectSignaturesFlowTests : WithContracts { //region Operators private fun StartedNode<*>.startTestFlow(vararg party: Party) = - startFlow( + startFlowAndRunNetwork( TestFlow.Initiator(DummyContract.MultiOwnerState( MAGIC_NUMBER, listOf(*party)), mockNet.defaultNotaryIdentity)) - .andRunNetwork() //region Test Flow // With this flow, the initiator starts the "CollectTransactionFlow". It is then the responders responsibility to diff --git a/core/src/test/kotlin/net/corda/core/flows/ContractUpgradeFlowRPCTest.kt b/core/src/test/kotlin/net/corda/core/flows/ContractUpgradeFlowRPCTest.kt index f625fe8be47..a8143751250 100644 --- a/core/src/test/kotlin/net/corda/core/flows/ContractUpgradeFlowRPCTest.kt +++ b/core/src/test/kotlin/net/corda/core/flows/ContractUpgradeFlowRPCTest.kt @@ -1,22 +1,20 @@ package net.corda.core.flows -import co.paralleluniverse.fibers.Suspendable -import com.natpryce.hamkrest.* +import com.natpryce.hamkrest.and +import com.natpryce.hamkrest.anything import com.natpryce.hamkrest.assertion.assert +import com.natpryce.hamkrest.has +import com.natpryce.hamkrest.isA import net.corda.core.CordaRuntimeException -import net.corda.core.contracts.* -import net.corda.core.flows.matchers.rpc.willThrow +import net.corda.core.contracts.ContractState +import net.corda.core.contracts.StateAndRef import net.corda.core.flows.matchers.rpc.willReturn +import net.corda.core.flows.matchers.rpc.willThrow import net.corda.core.flows.mixins.WithContracts import net.corda.core.flows.mixins.WithFinality -import net.corda.core.identity.AbstractParty -import net.corda.core.identity.Party -import net.corda.core.internal.Emoji import net.corda.core.messaging.CordaRPCOps import net.corda.core.transactions.ContractUpgradeLedgerTransaction -import net.corda.core.transactions.LedgerTransaction import net.corda.core.transactions.SignedTransaction -import net.corda.finance.contracts.asset.Cash import net.corda.node.internal.StartedNode import net.corda.node.services.Permissions.Companion.startFlow import net.corda.testing.contracts.DummyContract @@ -127,10 +125,9 @@ class ContractUpgradeFlowRPCTest : WithContracts, WithFinality { { getContractUpgradeTransaction(it) }, isUpgrade()) - private fun StartedNode<*>.getContractUpgradeTransaction(state: StateAndRef) = database.transaction { + private fun StartedNode<*>.getContractUpgradeTransaction(state: StateAndRef) = services.validatedTransactions.getTransaction(state.ref.txhash)!! .resolveContractUpgradeTransaction(services) - } private inline fun isUpgrade() = isUpgradeFrom() and isUpgradeTo() diff --git a/core/src/test/kotlin/net/corda/core/flows/ContractUpgradeFlowTest.kt b/core/src/test/kotlin/net/corda/core/flows/ContractUpgradeFlowTest.kt index b91b3265ff7..dfc4d890388 100644 --- a/core/src/test/kotlin/net/corda/core/flows/ContractUpgradeFlowTest.kt +++ b/core/src/test/kotlin/net/corda/core/flows/ContractUpgradeFlowTest.kt @@ -1,17 +1,14 @@ package net.corda.core.flows -import co.paralleluniverse.fibers.Suspendable import com.natpryce.hamkrest.* import com.natpryce.hamkrest.assertion.assert import net.corda.core.contracts.* -import net.corda.core.flows.matchers.flow.willThrow import net.corda.core.flows.matchers.flow.willReturn +import net.corda.core.flows.matchers.flow.willThrow import net.corda.core.flows.mixins.WithContracts import net.corda.core.flows.mixins.WithFinality import net.corda.core.identity.AbstractParty -import net.corda.core.identity.Party import net.corda.core.internal.Emoji -import net.corda.core.messaging.CordaRPCOps import net.corda.core.transactions.ContractUpgradeLedgerTransaction import net.corda.core.transactions.LedgerTransaction import net.corda.core.transactions.SignedTransaction @@ -22,15 +19,13 @@ import net.corda.finance.`issued by` import net.corda.finance.contracts.asset.Cash import net.corda.finance.flows.CashIssueFlow import net.corda.node.internal.StartedNode -import net.corda.node.services.Permissions.Companion.startFlow import net.corda.testing.contracts.DummyContract import net.corda.testing.contracts.DummyContractV2 import net.corda.testing.core.ALICE_NAME import net.corda.testing.core.BOB_NAME import net.corda.testing.core.singleIdentity -import net.corda.testing.node.User -import net.corda.testing.node.internal.* -import net.corda.testing.node.internal.InternalMockNetwork.MockNode +import net.corda.testing.node.internal.InternalMockNetwork +import net.corda.testing.node.internal.startFlow import org.junit.AfterClass import org.junit.Test import java.util.* @@ -186,10 +181,9 @@ class ContractUpgradeFlowTest : WithContracts, WithFinality { { getContractUpgradeTransaction(it) }, isUpgrade()) - private fun StartedNode<*>.getContractUpgradeTransaction(state: StateAndRef) = database.transaction { + private fun StartedNode<*>.getContractUpgradeTransaction(state: StateAndRef) = services.validatedTransactions.getTransaction(state.ref.txhash)!! .resolveContractUpgradeTransaction(services) - } private inline fun isUpgrade() = isUpgradeFrom() and isUpgradeTo() diff --git a/core/src/test/kotlin/net/corda/core/flows/FinalityFlowTests.kt b/core/src/test/kotlin/net/corda/core/flows/FinalityFlowTests.kt index c53473d727a..20f1c9928cc 100644 --- a/core/src/test/kotlin/net/corda/core/flows/FinalityFlowTests.kt +++ b/core/src/test/kotlin/net/corda/core/flows/FinalityFlowTests.kt @@ -2,8 +2,8 @@ package net.corda.core.flows import com.natpryce.hamkrest.and import com.natpryce.hamkrest.assertion.assert -import net.corda.core.flows.matchers.flow.willThrow import net.corda.core.flows.matchers.flow.willReturn +import net.corda.core.flows.matchers.flow.willThrow import net.corda.core.flows.mixins.WithFinality import net.corda.core.identity.Party import net.corda.core.transactions.SignedTransaction diff --git a/core/src/test/kotlin/net/corda/core/flows/matchers/flow/FlowMatchers.kt b/core/src/test/kotlin/net/corda/core/flows/matchers/flow/FlowMatchers.kt index a28814d2216..f9af1194dce 100644 --- a/core/src/test/kotlin/net/corda/core/flows/matchers/flow/FlowMatchers.kt +++ b/core/src/test/kotlin/net/corda/core/flows/matchers/flow/FlowMatchers.kt @@ -2,10 +2,10 @@ package net.corda.core.flows.matchers.flow import com.natpryce.hamkrest.Matcher import com.natpryce.hamkrest.has -import net.corda.core.flows.matchers.willThrow import net.corda.core.flows.matchers.willFailWithException import net.corda.core.flows.matchers.willReturn import net.corda.core.flows.matchers.willSucceedWithResult +import net.corda.core.flows.matchers.willThrow import net.corda.core.internal.FlowStateMachine /** diff --git a/core/src/test/kotlin/net/corda/core/flows/matchers/rpc/RpcMatchers.kt b/core/src/test/kotlin/net/corda/core/flows/matchers/rpc/RpcMatchers.kt index 0583754ec41..8f5803a4092 100644 --- a/core/src/test/kotlin/net/corda/core/flows/matchers/rpc/RpcMatchers.kt +++ b/core/src/test/kotlin/net/corda/core/flows/matchers/rpc/RpcMatchers.kt @@ -2,10 +2,10 @@ package net.corda.core.flows.matchers.rpc import com.natpryce.hamkrest.Matcher import com.natpryce.hamkrest.has -import net.corda.core.flows.matchers.willThrow import net.corda.core.flows.matchers.willFailWithException import net.corda.core.flows.matchers.willReturn import net.corda.core.flows.matchers.willSucceedWithResult +import net.corda.core.flows.matchers.willThrow import net.corda.core.messaging.FlowHandle /** diff --git a/core/src/test/kotlin/net/corda/core/flows/mixins/WithContracts.kt b/core/src/test/kotlin/net/corda/core/flows/mixins/WithContracts.kt index 846b4ded418..03da82085f8 100644 --- a/core/src/test/kotlin/net/corda/core/flows/mixins/WithContracts.kt +++ b/core/src/test/kotlin/net/corda/core/flows/mixins/WithContracts.kt @@ -6,8 +6,6 @@ import net.corda.core.contracts.StateAndRef import net.corda.core.contracts.UpgradedContract import net.corda.core.flows.CollectSignaturesFlow import net.corda.core.flows.ContractUpgradeFlow -import net.corda.core.identity.Party -import net.corda.core.identity.PartyAndCertificate import net.corda.core.messaging.CordaRPCOps import net.corda.core.messaging.startFlow import net.corda.core.node.ServiceHub diff --git a/core/src/test/kotlin/net/corda/core/flows/mixins/WithFinality.kt b/core/src/test/kotlin/net/corda/core/flows/mixins/WithFinality.kt index 417326c44d0..fe1811520d5 100644 --- a/core/src/test/kotlin/net/corda/core/flows/mixins/WithFinality.kt +++ b/core/src/test/kotlin/net/corda/core/flows/mixins/WithFinality.kt @@ -3,7 +3,6 @@ package net.corda.core.flows.mixins import co.paralleluniverse.fibers.Suspendable import com.natpryce.hamkrest.Matcher import com.natpryce.hamkrest.equalTo -import net.corda.core.flows.ContractUpgradeFlowTest import net.corda.core.flows.FinalityFlow import net.corda.core.flows.FlowLogic import net.corda.core.flows.StartableByRPC @@ -20,9 +19,8 @@ interface WithFinality : WithMockNet { fun StartedNode<*>.finalise(stx: SignedTransaction, vararg additionalParties: Party) = startFlowAndRunNetwork(FinalityFlow(stx, additionalParties.toSet())) - fun StartedNode<*>.getValidatedTransaction(stx: SignedTransaction) = database.transaction { + fun StartedNode<*>.getValidatedTransaction(stx: SignedTransaction) = services.validatedTransactions.getTransaction(stx.id)!! - } fun CordaRPCOps.finalise(stx: SignedTransaction, vararg parties: Party) = startFlow(::FinalityInvoker, stx, parties.toSet()) diff --git a/core/src/test/kotlin/net/corda/core/flows/mixins/WithMockNet.kt b/core/src/test/kotlin/net/corda/core/flows/mixins/WithMockNet.kt index 41a75a6aa22..c3dfbe67cdb 100644 --- a/core/src/test/kotlin/net/corda/core/flows/mixins/WithMockNet.kt +++ b/core/src/test/kotlin/net/corda/core/flows/mixins/WithMockNet.kt @@ -47,9 +47,8 @@ interface WithMockNet { /** * Retrieve the sole instance of a state of a particular class from the node's vault */ - fun StartedNode<*>.getStateFromVault(stateClass: KClass) = database.transaction { + fun StartedNode<*>.getStateFromVault(stateClass: KClass) = services.vaultService.queryBy(stateClass.java).states.single() - } /** * Start a flow @@ -62,15 +61,14 @@ interface WithMockNet { fun StartedNode<*>.startFlowAndRunNetwork(logic: FlowLogic): FlowStateMachine = startFlow(logic).andRunNetwork() - fun StartedNode<*>.createConfidentialIdentity(party: Party) = database.transaction { + fun StartedNode<*>.createConfidentialIdentity(party: Party) = services.keyManagementService.freshKeyAndCert( services.myInfo.legalIdentitiesAndCerts.single { it.name == party.name }, false) - } - fun StartedNode<*>.verifyAndRegister(identity: PartyAndCertificate) = database.transaction { + fun StartedNode<*>.verifyAndRegister(identity: PartyAndCertificate) = services.identityService.verifyAndRegisterIdentity(identity) - } + //endregion //region Matchers