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 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.Party
import net.corda.core.identity.PartyAndCertificate
import net.corda.core.serialization.CordaSerializable
import net.corda.core.utilities.ProgressTracker
import net.corda.core.utilities.unwrap

View File

@ -175,12 +175,13 @@ class ContractUpgradeFlowTest {
// Create some cash.
val result = a.services.startFlow(CashIssueFlow(Amount(1000, USD), OpaqueBytes.of(1), a.info.legalIdentity, notary)).resultFuture
mockNet.runNetwork()
val stateAndRef = result.getOrThrow().tx.outRef<Cash.State>(0)
val baseState = a.database.transaction { a.vault.unconsumedStates<ContractState>().single() }
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.
a.services.startFlow(ContractUpgradeFlow(stateAndRef, CashV2::class.java))
val upgradeResult = a.services.startFlow(ContractUpgradeFlow(stateAndRef, CashV2::class.java)).resultFuture
mockNet.runNetwork()
upgradeResult.getOrThrow()
// Get contract state from the vault.
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.")

View File

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

View File

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

View File

@ -17,9 +17,10 @@ import net.corda.core.utilities.DUMMY_NOTARY
import net.corda.flows.IssuerFlow.IssuanceRequester
import net.corda.testing.BOC
import net.corda.testing.MEGA_CORP
import net.corda.testing.ledger
import net.corda.testing.node.MockNetwork
import net.corda.testing.node.MockNetwork.MockNode
import org.junit.After
import org.junit.Before
import org.junit.Test
import rx.Observable
import java.util.*
@ -32,67 +33,52 @@ class IssuerFlowTest {
lateinit var bankOfCordaNode: 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
fun `test issuer flow`() {
mockNet = MockNetwork(false, true)
ledger {
notaryNode = mockNet.createNotaryNode(null, DUMMY_NOTARY.name)
bankOfCordaNode = mockNet.createPartyNode(notaryNode.info.address, BOC.name)
bankClientNode = mockNet.createPartyNode(notaryNode.info.address, MEGA_CORP.name)
// using default IssueTo Party Reference
val (issuer, issuerResult) = runIssuerAndIssueRequester(bankOfCordaNode, bankClientNode, 1000000.DOLLARS,
bankClientNode.info.legalIdentity, OpaqueBytes.of(123))
assertEquals(issuerResult.get(), issuer.get().resultFuture.get())
// using default IssueTo Party Reference
val (issuer, issuerResult) = runIssuerAndIssueRequester(bankOfCordaNode, bankClientNode, 1000000.DOLLARS,
bankClientNode.info.legalIdentity, OpaqueBytes.of(123))
assertEquals(issuerResult.get(), issuer.get().resultFuture.get())
// 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()
// 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()
}
}
@Test
fun `test issue flow to self`() {
mockNet = MockNetwork(false, true)
ledger {
notaryNode = mockNet.createNotaryNode(null, DUMMY_NOTARY.name)
bankOfCordaNode = mockNet.createPartyNode(notaryNode.info.address, BOC.name)
// 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()
}
// 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())
}
@Test
fun `test concurrent issuer flow`() {
mockNet = MockNetwork(false, true)
ledger {
notaryNode = mockNet.createNotaryNode(null, DUMMY_NOTARY.name)
bankOfCordaNode = mockNet.createPartyNode(notaryNode.info.address, BOC.name)
bankClientNode = mockNet.createPartyNode(notaryNode.info.address, MEGA_CORP.name)
// this test exercises the Cashflow issue and move subflows to ensure consistent spending of issued states
val amount = 10000.DOLLARS
val amounts = calculateRandomlySizedAmounts(10000.DOLLARS, 10, 10, Random())
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()
// this test exercises the Cashflow issue and move subflows to ensure consistent spending of issued states
val amount = 10000.DOLLARS
val amounts = calculateRandomlySizedAmounts(10000.DOLLARS, 10, 10, Random())
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)
}
}

View File

@ -6,6 +6,7 @@ import com.google.common.util.concurrent.Futures
import com.google.common.util.concurrent.ListenableFuture
import net.corda.core.*
import net.corda.core.crypto.entropyToKeyPair
import net.corda.flows.TxKeyFlow
import net.corda.core.identity.PartyAndCertificate
import net.corda.core.messaging.RPCOps
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)
if (start) {
node.setup().start()
// Register flows that are normally found via plugins
node.registerInitiatedFlow(TxKeyFlow.Provider::class.java)
if (threadPerNode && networkMapAddress != null)
node.networkMapRegistrationFuture.getOrThrow() // Block and wait for the node to register in the net map.
}