Eliminate unnecessary transactions

This commit is contained in:
Dominic Fox 2018-07-19 11:45:42 +01:00
parent fc5cc89fdb
commit 2992866269
10 changed files with 33 additions and 50 deletions

View File

@ -5,8 +5,8 @@ import com.natpryce.hamkrest.*
import com.natpryce.hamkrest.assertion.assert import com.natpryce.hamkrest.assertion.assert
import net.corda.core.contracts.Attachment import net.corda.core.contracts.Attachment
import net.corda.core.crypto.SecureHash 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.willReturn
import net.corda.core.flows.matchers.flow.willThrow
import net.corda.core.flows.mixins.WithMockNet import net.corda.core.flows.mixins.WithMockNet
import net.corda.core.identity.CordaX500Name import net.corda.core.identity.CordaX500Name
import net.corda.core.identity.Party 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.core.singleIdentity
import net.corda.testing.node.internal.InternalMockNetwork import net.corda.testing.node.internal.InternalMockNetwork
import net.corda.testing.node.internal.InternalMockNodeParameters import net.corda.testing.node.internal.InternalMockNodeParameters
import net.corda.testing.node.internal.startFlow
import org.junit.AfterClass import org.junit.AfterClass
import org.junit.Test import org.junit.Test
import java.io.ByteArrayOutputStream import java.io.ByteArrayOutputStream
@ -149,19 +148,19 @@ class AttachmentTests : WithMockNet {
//endregion //endregion
//region Operations //region Operations
private fun StartedNode<*>.importAttachment(attachment: ByteArray) = database.transaction { private fun StartedNode<*>.importAttachment(attachment: ByteArray) =
attachments.importAttachment(attachment.inputStream(), "test", null) attachments.importAttachment(attachment.inputStream(), "test", null)
}.andRunNetwork() .andRunNetwork()
private fun StartedNode<*>.updateAttachment(attachment: NodeAttachmentService.DBAttachment) = private fun StartedNode<*>.updateAttachment(attachment: NodeAttachmentService.DBAttachment) = database.transaction {
database.transaction { session.update(attachment) }.andRunNetwork() session.update(attachment).andRunNetwork()
private fun StartedNode<*>.startAttachmentFlow(hash: SecureHash, otherSide: Party) = services.startFlow(
InitiatingFetchAttachmentsFlow(otherSide, setOf(hash))).andRunNetwork()
private fun StartedNode<*>.getAttachmentWithId(id: SecureHash) = database.transaction {
attachments.openAttachment(id)!!
} }
private fun StartedNode<*>.startAttachmentFlow(hash: SecureHash, otherSide: Party) = startFlowAndRunNetwork(
InitiatingFetchAttachmentsFlow(otherSide, setOf(hash)))
private fun StartedNode<*>.getAttachmentWithId(id: SecureHash) =
attachments.openAttachment(id)!!
//endregion //endregion
//region Matchers //region Matchers

View File

@ -5,8 +5,8 @@ import com.natpryce.hamkrest.assertion.assert
import net.corda.core.contracts.Command import net.corda.core.contracts.Command
import net.corda.core.contracts.StateAndContract import net.corda.core.contracts.StateAndContract
import net.corda.core.contracts.requireThat 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.willReturn
import net.corda.core.flows.matchers.flow.willThrow
import net.corda.core.flows.mixins.WithContracts import net.corda.core.flows.mixins.WithContracts
import net.corda.core.identity.CordaX500Name import net.corda.core.identity.CordaX500Name
import net.corda.core.identity.Party import net.corda.core.identity.Party
@ -93,12 +93,11 @@ class CollectSignaturesFlowTests : WithContracts {
//region Operators //region Operators
private fun StartedNode<*>.startTestFlow(vararg party: Party) = private fun StartedNode<*>.startTestFlow(vararg party: Party) =
startFlow( startFlowAndRunNetwork(
TestFlow.Initiator(DummyContract.MultiOwnerState( TestFlow.Initiator(DummyContract.MultiOwnerState(
MAGIC_NUMBER, MAGIC_NUMBER,
listOf(*party)), listOf(*party)),
mockNet.defaultNotaryIdentity)) mockNet.defaultNotaryIdentity))
.andRunNetwork()
//region Test Flow //region Test Flow
// With this flow, the initiator starts the "CollectTransactionFlow". It is then the responders responsibility to // With this flow, the initiator starts the "CollectTransactionFlow". It is then the responders responsibility to

View File

@ -1,22 +1,20 @@
package net.corda.core.flows package net.corda.core.flows
import co.paralleluniverse.fibers.Suspendable import com.natpryce.hamkrest.and
import com.natpryce.hamkrest.* import com.natpryce.hamkrest.anything
import com.natpryce.hamkrest.assertion.assert 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.CordaRuntimeException
import net.corda.core.contracts.* import net.corda.core.contracts.ContractState
import net.corda.core.flows.matchers.rpc.willThrow import net.corda.core.contracts.StateAndRef
import net.corda.core.flows.matchers.rpc.willReturn 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.WithContracts
import net.corda.core.flows.mixins.WithFinality 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.messaging.CordaRPCOps
import net.corda.core.transactions.ContractUpgradeLedgerTransaction import net.corda.core.transactions.ContractUpgradeLedgerTransaction
import net.corda.core.transactions.LedgerTransaction
import net.corda.core.transactions.SignedTransaction import net.corda.core.transactions.SignedTransaction
import net.corda.finance.contracts.asset.Cash
import net.corda.node.internal.StartedNode import net.corda.node.internal.StartedNode
import net.corda.node.services.Permissions.Companion.startFlow import net.corda.node.services.Permissions.Companion.startFlow
import net.corda.testing.contracts.DummyContract import net.corda.testing.contracts.DummyContract
@ -127,10 +125,9 @@ class ContractUpgradeFlowRPCTest : WithContracts, WithFinality {
{ getContractUpgradeTransaction(it) }, { getContractUpgradeTransaction(it) },
isUpgrade<FROM, TO>()) isUpgrade<FROM, TO>())
private fun StartedNode<*>.getContractUpgradeTransaction(state: StateAndRef<ContractState>) = database.transaction { private fun StartedNode<*>.getContractUpgradeTransaction(state: StateAndRef<ContractState>) =
services.validatedTransactions.getTransaction(state.ref.txhash)!! services.validatedTransactions.getTransaction(state.ref.txhash)!!
.resolveContractUpgradeTransaction(services) .resolveContractUpgradeTransaction(services)
}
private inline fun <reified FROM : Any, reified TO : Any> isUpgrade() = private inline fun <reified FROM : Any, reified TO : Any> isUpgrade() =
isUpgradeFrom<FROM>() and isUpgradeTo<TO>() isUpgradeFrom<FROM>() and isUpgradeTo<TO>()

View File

@ -1,17 +1,14 @@
package net.corda.core.flows package net.corda.core.flows
import co.paralleluniverse.fibers.Suspendable
import com.natpryce.hamkrest.* import com.natpryce.hamkrest.*
import com.natpryce.hamkrest.assertion.assert import com.natpryce.hamkrest.assertion.assert
import net.corda.core.contracts.* 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.willReturn
import net.corda.core.flows.matchers.flow.willThrow
import net.corda.core.flows.mixins.WithContracts import net.corda.core.flows.mixins.WithContracts
import net.corda.core.flows.mixins.WithFinality import net.corda.core.flows.mixins.WithFinality
import net.corda.core.identity.AbstractParty import net.corda.core.identity.AbstractParty
import net.corda.core.identity.Party
import net.corda.core.internal.Emoji import net.corda.core.internal.Emoji
import net.corda.core.messaging.CordaRPCOps
import net.corda.core.transactions.ContractUpgradeLedgerTransaction import net.corda.core.transactions.ContractUpgradeLedgerTransaction
import net.corda.core.transactions.LedgerTransaction import net.corda.core.transactions.LedgerTransaction
import net.corda.core.transactions.SignedTransaction 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.contracts.asset.Cash
import net.corda.finance.flows.CashIssueFlow import net.corda.finance.flows.CashIssueFlow
import net.corda.node.internal.StartedNode 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.DummyContract
import net.corda.testing.contracts.DummyContractV2 import net.corda.testing.contracts.DummyContractV2
import net.corda.testing.core.ALICE_NAME import net.corda.testing.core.ALICE_NAME
import net.corda.testing.core.BOB_NAME import net.corda.testing.core.BOB_NAME
import net.corda.testing.core.singleIdentity import net.corda.testing.core.singleIdentity
import net.corda.testing.node.User import net.corda.testing.node.internal.InternalMockNetwork
import net.corda.testing.node.internal.* import net.corda.testing.node.internal.startFlow
import net.corda.testing.node.internal.InternalMockNetwork.MockNode
import org.junit.AfterClass import org.junit.AfterClass
import org.junit.Test import org.junit.Test
import java.util.* import java.util.*
@ -186,10 +181,9 @@ class ContractUpgradeFlowTest : WithContracts, WithFinality {
{ getContractUpgradeTransaction(it) }, { getContractUpgradeTransaction(it) },
isUpgrade<FROM, TO>()) isUpgrade<FROM, TO>())
private fun StartedNode<*>.getContractUpgradeTransaction(state: StateAndRef<ContractState>) = database.transaction { private fun StartedNode<*>.getContractUpgradeTransaction(state: StateAndRef<ContractState>) =
services.validatedTransactions.getTransaction(state.ref.txhash)!! services.validatedTransactions.getTransaction(state.ref.txhash)!!
.resolveContractUpgradeTransaction(services) .resolveContractUpgradeTransaction(services)
}
private inline fun <reified FROM : Any, reified TO : Any> isUpgrade() = private inline fun <reified FROM : Any, reified TO : Any> isUpgrade() =
isUpgradeFrom<FROM>() and isUpgradeTo<TO>() isUpgradeFrom<FROM>() and isUpgradeTo<TO>()

View File

@ -2,8 +2,8 @@ package net.corda.core.flows
import com.natpryce.hamkrest.and import com.natpryce.hamkrest.and
import com.natpryce.hamkrest.assertion.assert 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.willReturn
import net.corda.core.flows.matchers.flow.willThrow
import net.corda.core.flows.mixins.WithFinality import net.corda.core.flows.mixins.WithFinality
import net.corda.core.identity.Party import net.corda.core.identity.Party
import net.corda.core.transactions.SignedTransaction import net.corda.core.transactions.SignedTransaction

View File

@ -2,10 +2,10 @@ package net.corda.core.flows.matchers.flow
import com.natpryce.hamkrest.Matcher import com.natpryce.hamkrest.Matcher
import com.natpryce.hamkrest.has 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.willFailWithException
import net.corda.core.flows.matchers.willReturn import net.corda.core.flows.matchers.willReturn
import net.corda.core.flows.matchers.willSucceedWithResult import net.corda.core.flows.matchers.willSucceedWithResult
import net.corda.core.flows.matchers.willThrow
import net.corda.core.internal.FlowStateMachine import net.corda.core.internal.FlowStateMachine
/** /**

View File

@ -2,10 +2,10 @@ package net.corda.core.flows.matchers.rpc
import com.natpryce.hamkrest.Matcher import com.natpryce.hamkrest.Matcher
import com.natpryce.hamkrest.has 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.willFailWithException
import net.corda.core.flows.matchers.willReturn import net.corda.core.flows.matchers.willReturn
import net.corda.core.flows.matchers.willSucceedWithResult import net.corda.core.flows.matchers.willSucceedWithResult
import net.corda.core.flows.matchers.willThrow
import net.corda.core.messaging.FlowHandle import net.corda.core.messaging.FlowHandle
/** /**

View File

@ -6,8 +6,6 @@ import net.corda.core.contracts.StateAndRef
import net.corda.core.contracts.UpgradedContract import net.corda.core.contracts.UpgradedContract
import net.corda.core.flows.CollectSignaturesFlow import net.corda.core.flows.CollectSignaturesFlow
import net.corda.core.flows.ContractUpgradeFlow 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.CordaRPCOps
import net.corda.core.messaging.startFlow import net.corda.core.messaging.startFlow
import net.corda.core.node.ServiceHub import net.corda.core.node.ServiceHub

View File

@ -3,7 +3,6 @@ package net.corda.core.flows.mixins
import co.paralleluniverse.fibers.Suspendable import co.paralleluniverse.fibers.Suspendable
import com.natpryce.hamkrest.Matcher import com.natpryce.hamkrest.Matcher
import com.natpryce.hamkrest.equalTo import com.natpryce.hamkrest.equalTo
import net.corda.core.flows.ContractUpgradeFlowTest
import net.corda.core.flows.FinalityFlow import net.corda.core.flows.FinalityFlow
import net.corda.core.flows.FlowLogic import net.corda.core.flows.FlowLogic
import net.corda.core.flows.StartableByRPC import net.corda.core.flows.StartableByRPC
@ -20,9 +19,8 @@ interface WithFinality : WithMockNet {
fun StartedNode<*>.finalise(stx: SignedTransaction, vararg additionalParties: Party) = fun StartedNode<*>.finalise(stx: SignedTransaction, vararg additionalParties: Party) =
startFlowAndRunNetwork(FinalityFlow(stx, additionalParties.toSet())) startFlowAndRunNetwork(FinalityFlow(stx, additionalParties.toSet()))
fun StartedNode<*>.getValidatedTransaction(stx: SignedTransaction) = database.transaction { fun StartedNode<*>.getValidatedTransaction(stx: SignedTransaction) =
services.validatedTransactions.getTransaction(stx.id)!! services.validatedTransactions.getTransaction(stx.id)!!
}
fun CordaRPCOps.finalise(stx: SignedTransaction, vararg parties: Party) = fun CordaRPCOps.finalise(stx: SignedTransaction, vararg parties: Party) =
startFlow(::FinalityInvoker, stx, parties.toSet()) startFlow(::FinalityInvoker, stx, parties.toSet())

View File

@ -47,9 +47,8 @@ interface WithMockNet {
/** /**
* Retrieve the sole instance of a state of a particular class from the node's vault * Retrieve the sole instance of a state of a particular class from the node's vault
*/ */
fun <S: ContractState> StartedNode<*>.getStateFromVault(stateClass: KClass<S>) = database.transaction { fun <S: ContractState> StartedNode<*>.getStateFromVault(stateClass: KClass<S>) =
services.vaultService.queryBy(stateClass.java).states.single() services.vaultService.queryBy(stateClass.java).states.single()
}
/** /**
* Start a flow * Start a flow
@ -62,15 +61,14 @@ interface WithMockNet {
fun <T> StartedNode<*>.startFlowAndRunNetwork(logic: FlowLogic<T>): FlowStateMachine<T> = fun <T> StartedNode<*>.startFlowAndRunNetwork(logic: FlowLogic<T>): FlowStateMachine<T> =
startFlow(logic).andRunNetwork() startFlow(logic).andRunNetwork()
fun StartedNode<*>.createConfidentialIdentity(party: Party) = database.transaction { fun StartedNode<*>.createConfidentialIdentity(party: Party) =
services.keyManagementService.freshKeyAndCert( services.keyManagementService.freshKeyAndCert(
services.myInfo.legalIdentitiesAndCerts.single { it.name == party.name }, services.myInfo.legalIdentitiesAndCerts.single { it.name == party.name },
false) false)
}
fun StartedNode<*>.verifyAndRegister(identity: PartyAndCertificate) = database.transaction { fun StartedNode<*>.verifyAndRegister(identity: PartyAndCertificate) =
services.identityService.verifyAndRegisterIdentity(identity) services.identityService.verifyAndRegisterIdentity(identity)
}
//endregion //endregion
//region Matchers //region Matchers