Clean up cash tests

Clean up cash tests ahead of anonymisation work. This simplifies some boiler plate setup/teardown
and ensures idenities and flows are correctly registered.
This commit is contained in:
Ross Nicoll
2017-06-06 17:09:32 +01:00
committed by GitHub
parent 6f43811f60
commit 35b0ceac0b
6 changed files with 54 additions and 60 deletions

View File

@ -1,9 +1,12 @@
package net.corda.core.flows package net.corda.flows
import co.paralleluniverse.fibers.Suspendable import co.paralleluniverse.fibers.Suspendable
import net.corda.core.flows.FlowLogic
import net.corda.core.flows.InitiatedBy
import net.corda.core.flows.InitiatingFlow
import net.corda.core.flows.StartableByRPC
import net.corda.core.identity.AnonymousParty import net.corda.core.identity.AnonymousParty
import net.corda.core.identity.Party import net.corda.core.identity.Party
import net.corda.core.identity.PartyAndCertificate
import net.corda.core.serialization.CordaSerializable import net.corda.core.serialization.CordaSerializable
import net.corda.core.utilities.ProgressTracker import net.corda.core.utilities.ProgressTracker
import net.corda.core.utilities.unwrap import net.corda.core.utilities.unwrap

View File

@ -175,12 +175,13 @@ class ContractUpgradeFlowTest {
// Create some cash. // Create some cash.
val result = a.services.startFlow(CashIssueFlow(Amount(1000, USD), OpaqueBytes.of(1), a.info.legalIdentity, notary)).resultFuture val result = a.services.startFlow(CashIssueFlow(Amount(1000, USD), OpaqueBytes.of(1), a.info.legalIdentity, notary)).resultFuture
mockNet.runNetwork() mockNet.runNetwork()
val stateAndRef = result.getOrThrow().tx.outRef<Cash.State>(0)
val baseState = a.database.transaction { a.vault.unconsumedStates<ContractState>().single() } val baseState = a.database.transaction { a.vault.unconsumedStates<ContractState>().single() }
assertTrue(baseState.state.data is Cash.State, "Contract state is old version.") assertTrue(baseState.state.data is Cash.State, "Contract state is old version.")
val stateAndRef = result.getOrThrow().tx.outRef<Cash.State>(0)
// Starts contract upgrade flow. // Starts contract upgrade flow.
a.services.startFlow(ContractUpgradeFlow(stateAndRef, CashV2::class.java)) val upgradeResult = a.services.startFlow(ContractUpgradeFlow(stateAndRef, CashV2::class.java)).resultFuture
mockNet.runNetwork() mockNet.runNetwork()
upgradeResult.getOrThrow()
// Get contract state from the vault. // Get contract state from the vault.
val firstState = a.database.transaction { a.vault.unconsumedStates<ContractState>().single() } val firstState = a.database.transaction { a.vault.unconsumedStates<ContractState>().single() }
assertTrue(firstState.state.data is CashV2.State, "Contract state is upgraded to the new version.") assertTrue(firstState.state.data is CashV2.State, "Contract state is upgraded to the new version.")

View File

@ -1,4 +1,4 @@
package net.corda.core.flows package net.corda.flows
import net.corda.core.getOrThrow import net.corda.core.getOrThrow
import net.corda.core.identity.AbstractParty import net.corda.core.identity.AbstractParty
@ -37,7 +37,6 @@ class TxKeyFlowTests {
bobNode.services.identityService.registerIdentity(notaryNode.info.legalIdentityAndCert) bobNode.services.identityService.registerIdentity(notaryNode.info.legalIdentityAndCert)
// Run the flows // Run the flows
bobNode.registerInitiatedFlow(TxKeyFlow.Provider::class.java)
val requesterFlow = aliceNode.services.startFlow(TxKeyFlow.Requester(bob)) val requesterFlow = aliceNode.services.startFlow(TxKeyFlow.Requester(bob))
// Get the results // Get the results

View File

@ -3,8 +3,8 @@ package net.corda.flows
import net.corda.contracts.asset.Cash import net.corda.contracts.asset.Cash
import net.corda.core.contracts.DOLLARS import net.corda.core.contracts.DOLLARS
import net.corda.core.contracts.`issued by` import net.corda.core.contracts.`issued by`
import net.corda.core.identity.Party
import net.corda.core.getOrThrow import net.corda.core.getOrThrow
import net.corda.core.identity.Party
import net.corda.core.serialization.OpaqueBytes import net.corda.core.serialization.OpaqueBytes
import net.corda.testing.node.InMemoryMessagingNetwork.ServicePeerAllocationStrategy.RoundRobin import net.corda.testing.node.InMemoryMessagingNetwork.ServicePeerAllocationStrategy.RoundRobin
import net.corda.testing.node.MockNetwork import net.corda.testing.node.MockNetwork
@ -48,15 +48,17 @@ class CashPaymentFlowTests {
@Test @Test
fun `pay some cash`() { fun `pay some cash`() {
val payTo = notaryNode.info.legalIdentity val payTo = notaryNode.info.legalIdentity
val expected = 500.DOLLARS val expectedPayment = 500.DOLLARS
val future = bankOfCordaNode.services.startFlow(CashPaymentFlow(expected, val expectedChange = 1500.DOLLARS
val future = bankOfCordaNode.services.startFlow(CashPaymentFlow(expectedPayment,
payTo)).resultFuture payTo)).resultFuture
mockNet.runNetwork() mockNet.runNetwork()
val paymentTx = future.getOrThrow() val paymentTx = future.getOrThrow()
val states = paymentTx.tx.outputs.map { it.data }.filterIsInstance<Cash.State>() val states = paymentTx.tx.outputs.map { it.data }.filterIsInstance<Cash.State>()
val ourState = states.single { it.owner.owningKey != payTo.owningKey } val ourState = states.single { it.owner.owningKey != payTo.owningKey }
val paymentState = states.single { it.owner.owningKey == payTo.owningKey } val paymentState = states.single { it.owner.owningKey == payTo.owningKey }
assertEquals(expected.`issued by`(bankOfCorda.ref(ref)), paymentState.amount) assertEquals(expectedChange.`issued by`(bankOfCorda.ref(ref)), ourState.amount)
assertEquals(expectedPayment.`issued by`(bankOfCorda.ref(ref)), paymentState.amount)
} }
@Test @Test

View File

@ -17,9 +17,10 @@ import net.corda.core.utilities.DUMMY_NOTARY
import net.corda.flows.IssuerFlow.IssuanceRequester import net.corda.flows.IssuerFlow.IssuanceRequester
import net.corda.testing.BOC import net.corda.testing.BOC
import net.corda.testing.MEGA_CORP import net.corda.testing.MEGA_CORP
import net.corda.testing.ledger
import net.corda.testing.node.MockNetwork import net.corda.testing.node.MockNetwork
import net.corda.testing.node.MockNetwork.MockNode import net.corda.testing.node.MockNetwork.MockNode
import org.junit.After
import org.junit.Before
import org.junit.Test import org.junit.Test
import rx.Observable import rx.Observable
import java.util.* import java.util.*
@ -32,67 +33,52 @@ class IssuerFlowTest {
lateinit var bankOfCordaNode: MockNode lateinit var bankOfCordaNode: MockNode
lateinit var bankClientNode: MockNode lateinit var bankClientNode: MockNode
@Before
fun start() {
mockNet = MockNetwork(threadPerNode = true)
notaryNode = mockNet.createNotaryNode(null, DUMMY_NOTARY.name)
bankOfCordaNode = mockNet.createPartyNode(notaryNode.info.address, BOC.name)
bankClientNode = mockNet.createPartyNode(notaryNode.info.address, MEGA_CORP.name)
}
@After
fun cleanUp() {
mockNet.stopNodes()
}
@Test @Test
fun `test issuer flow`() { fun `test issuer flow`() {
mockNet = MockNetwork(false, true) // using default IssueTo Party Reference
ledger { val (issuer, issuerResult) = runIssuerAndIssueRequester(bankOfCordaNode, bankClientNode, 1000000.DOLLARS,
notaryNode = mockNet.createNotaryNode(null, DUMMY_NOTARY.name) bankClientNode.info.legalIdentity, OpaqueBytes.of(123))
bankOfCordaNode = mockNet.createPartyNode(notaryNode.info.address, BOC.name) assertEquals(issuerResult.get(), issuer.get().resultFuture.get())
bankClientNode = mockNet.createPartyNode(notaryNode.info.address, MEGA_CORP.name)
// using default IssueTo Party Reference // try to issue an amount of a restricted currency
val (issuer, issuerResult) = runIssuerAndIssueRequester(bankOfCordaNode, bankClientNode, 1000000.DOLLARS, assertFailsWith<FlowException> {
bankClientNode.info.legalIdentity, OpaqueBytes.of(123)) runIssuerAndIssueRequester(bankOfCordaNode, bankClientNode, Amount(100000L, currency("BRL")),
assertEquals(issuerResult.get(), issuer.get().resultFuture.get()) bankClientNode.info.legalIdentity, OpaqueBytes.of(123)).issueRequestResult.getOrThrow()
// try to issue an amount of a restricted currency
assertFailsWith<FlowException> {
runIssuerAndIssueRequester(bankOfCordaNode, bankClientNode, Amount(100000L, currency("BRL")),
bankClientNode.info.legalIdentity, OpaqueBytes.of(123)).issueRequestResult.getOrThrow()
}
bankOfCordaNode.stop()
bankClientNode.stop()
} }
} }
@Test @Test
fun `test issue flow to self`() { fun `test issue flow to self`() {
mockNet = MockNetwork(false, true) // using default IssueTo Party Reference
ledger { val (issuer, issuerResult) = runIssuerAndIssueRequester(bankOfCordaNode, bankOfCordaNode, 1000000.DOLLARS,
notaryNode = mockNet.createNotaryNode(null, DUMMY_NOTARY.name) bankOfCordaNode.info.legalIdentity, OpaqueBytes.of(123))
bankOfCordaNode = mockNet.createPartyNode(notaryNode.info.address, BOC.name) assertEquals(issuerResult.get(), issuer.get().resultFuture.get())
// using default IssueTo Party Reference
val (issuer, issuerResult) = runIssuerAndIssueRequester(bankOfCordaNode, bankOfCordaNode, 1000000.DOLLARS,
bankOfCordaNode.info.legalIdentity, OpaqueBytes.of(123))
assertEquals(issuerResult.get(), issuer.get().resultFuture.get())
bankOfCordaNode.stop()
}
} }
@Test @Test
fun `test concurrent issuer flow`() { fun `test concurrent issuer flow`() {
mockNet = MockNetwork(false, true) // this test exercises the Cashflow issue and move subflows to ensure consistent spending of issued states
ledger { val amount = 10000.DOLLARS
notaryNode = mockNet.createNotaryNode(null, DUMMY_NOTARY.name) val amounts = calculateRandomlySizedAmounts(10000.DOLLARS, 10, 10, Random())
bankOfCordaNode = mockNet.createPartyNode(notaryNode.info.address, BOC.name) val handles = amounts.map { pennies ->
bankClientNode = mockNet.createPartyNode(notaryNode.info.address, MEGA_CORP.name) runIssuerAndIssueRequester(bankOfCordaNode, bankClientNode, Amount(pennies, amount.token),
bankClientNode.info.legalIdentity, OpaqueBytes.of(123))
// this test exercises the Cashflow issue and move subflows to ensure consistent spending of issued states }
val amount = 10000.DOLLARS handles.forEach {
val amounts = calculateRandomlySizedAmounts(10000.DOLLARS, 10, 10, Random()) require(it.issueRequestResult.get() is SignedTransaction)
val handles = amounts.map { pennies ->
runIssuerAndIssueRequester(bankOfCordaNode, bankClientNode, Amount(pennies, amount.token),
bankClientNode.info.legalIdentity, OpaqueBytes.of(123))
}
handles.forEach {
require(it.issueRequestResult.get() is SignedTransaction)
}
bankOfCordaNode.stop()
bankClientNode.stop()
} }
} }

View File

@ -6,6 +6,7 @@ import com.google.common.util.concurrent.Futures
import com.google.common.util.concurrent.ListenableFuture import com.google.common.util.concurrent.ListenableFuture
import net.corda.core.* import net.corda.core.*
import net.corda.core.crypto.entropyToKeyPair import net.corda.core.crypto.entropyToKeyPair
import net.corda.flows.TxKeyFlow
import net.corda.core.identity.PartyAndCertificate import net.corda.core.identity.PartyAndCertificate
import net.corda.core.messaging.RPCOps import net.corda.core.messaging.RPCOps
import net.corda.core.messaging.SingleMessageRecipient import net.corda.core.messaging.SingleMessageRecipient
@ -287,6 +288,8 @@ class MockNetwork(private val networkSendManuallyPumped: Boolean = false,
val node = nodeFactory.create(config, this, networkMapAddress, advertisedServices.toSet(), id, overrideServices, entropyRoot) val node = nodeFactory.create(config, this, networkMapAddress, advertisedServices.toSet(), id, overrideServices, entropyRoot)
if (start) { if (start) {
node.setup().start() node.setup().start()
// Register flows that are normally found via plugins
node.registerInitiatedFlow(TxKeyFlow.Provider::class.java)
if (threadPerNode && networkMapAddress != null) if (threadPerNode && networkMapAddress != null)
node.networkMapRegistrationFuture.getOrThrow() // Block and wait for the node to register in the net map. node.networkMapRegistrationFuture.getOrThrow() // Block and wait for the node to register in the net map.
} }