From aab2ad58d9b050738b65a72041d1678010b565a7 Mon Sep 17 00:00:00 2001 From: Christian Sailer Date: Fri, 24 Nov 2017 14:54:57 +0000 Subject: [PATCH] Hook up anonymised payment and add test for that + some code reformatting --- .../flows/CashIssueAndPaymentNoSelection.kt | 21 ++++++++++++---- .../CashIssueAndPaymentNoSelectionFlowTest.kt | 24 ++++++++++++++----- 2 files changed, 34 insertions(+), 11 deletions(-) diff --git a/perftestcordapp/src/main/kotlin/com/r3/corda/enterprise/perftestcordapp/flows/CashIssueAndPaymentNoSelection.kt b/perftestcordapp/src/main/kotlin/com/r3/corda/enterprise/perftestcordapp/flows/CashIssueAndPaymentNoSelection.kt index 560c296f99..5df653d49e 100644 --- a/perftestcordapp/src/main/kotlin/com/r3/corda/enterprise/perftestcordapp/flows/CashIssueAndPaymentNoSelection.kt +++ b/perftestcordapp/src/main/kotlin/com/r3/corda/enterprise/perftestcordapp/flows/CashIssueAndPaymentNoSelection.kt @@ -4,9 +4,11 @@ import co.paralleluniverse.fibers.Suspendable import com.r3.corda.enterprise.perftestcordapp.contracts.asset.Cash import com.r3.corda.enterprise.perftestcordapp.contracts.asset.OnLedgerAsset import com.r3.corda.enterprise.perftestcordapp.contracts.asset.PartyAndAmount +import net.corda.confidential.SwapIdentitiesFlow import net.corda.core.contracts.* import net.corda.core.flows.StartableByRPC import net.corda.core.identity.AbstractParty +import net.corda.core.identity.AnonymousParty import net.corda.core.identity.Party import net.corda.core.transactions.TransactionBuilder import net.corda.core.utilities.OpaqueBytes @@ -34,6 +36,7 @@ class CashIssueAndPaymentNoSelection(val amount: Amount, val notary: Party, progressTracker: ProgressTracker) : AbstractCashFlow(progressTracker) { constructor(request: CashIssueAndPaymentFlow.IssueAndPaymentRequest) : this(request.amount, request.issueRef, request.recipient, request.anonymous, request.notary, tracker()) + constructor(amount: Amount, issueRef: OpaqueBytes, payTo: Party, anonymous: Boolean, notary: Party) : this(amount, issueRef, payTo, anonymous, notary, tracker()) @Suspendable override fun call(): Result { @@ -42,9 +45,20 @@ class CashIssueAndPaymentNoSelection(val amount: Amount, val issueResult = subFlow(CashIssueFlow(amount, issueRef, notary)) val cashStateAndRef = issueResult.stx.tx.outRef(0) + + progressTracker.currentStep = GENERATING_ID + val txIdentities = if (anonymous) { + subFlow(SwapIdentitiesFlow(recipient)) + } else { + emptyMap() + } + val anonymousRecipient = txIdentities[recipient] ?: recipient + val changeIdentity = serviceHub.keyManagementService.freshKeyAndCert(ourIdentityAndCert, false) + + progressTracker.currentStep = GENERATING_TX val builder = TransactionBuilder(notary) - val (spendTx, keysForSigning) = OnLedgerAsset.generateSpend(builder, listOf(PartyAndAmount(recipient, amount)), listOf(cashStateAndRef), + val (spendTx, keysForSigning) = OnLedgerAsset.generateSpend(builder, listOf(PartyAndAmount(anonymousRecipient, amount)), listOf(cashStateAndRef), changeIdentity.party.anonymise(), { state, quantity, owner -> deriveState(state, quantity, owner) }, { Cash().generateMoveCommand() }) @@ -56,7 +70,4 @@ class CashIssueAndPaymentNoSelection(val amount: Amount, val notarised = finaliseTx(tx, setOf(recipient), "Unable to notarise spend") return Result(notarised, recipient) } - - constructor(amount: Amount, issueRef: OpaqueBytes, payTo: Party, anonymous: Boolean, notary: Party) : this(amount, issueRef, payTo, anonymous, notary, tracker()) - -} \ No newline at end of file +} diff --git a/perftestcordapp/src/test/kotlin/com/r3/corda/enterprise/perftestcordapp/flows/CashIssueAndPaymentNoSelectionFlowTest.kt b/perftestcordapp/src/test/kotlin/com/r3/corda/enterprise/perftestcordapp/flows/CashIssueAndPaymentNoSelectionFlowTest.kt index bfc42dd480..891cc05164 100644 --- a/perftestcordapp/src/test/kotlin/com/r3/corda/enterprise/perftestcordapp/flows/CashIssueAndPaymentNoSelectionFlowTest.kt +++ b/perftestcordapp/src/test/kotlin/com/r3/corda/enterprise/perftestcordapp/flows/CashIssueAndPaymentNoSelectionFlowTest.kt @@ -17,11 +17,19 @@ import net.corda.testing.node.MockNetwork.MockNode import org.junit.After import org.junit.Before import org.junit.Test +import org.junit.runner.RunWith +import org.junit.runners.Parameterized import kotlin.test.assertEquals -class CashIssueAndPayNoSelectionTests { +@RunWith(Parameterized::class) +class CashIssueAndPayNoSelectionTests(private val anonymous: Boolean) { + companion object { + @Parameterized.Parameters(name = "Anonymous = {0}") + @JvmStatic + fun data() = listOf(false, true) + } + private lateinit var mockNet: MockNetwork - private val initialBalance = 2000.DOLLARS private val ref = OpaqueBytes.of(0x01) private lateinit var bankOfCordaNode: StartedNode private lateinit var bankOfCorda: Party @@ -30,7 +38,8 @@ class CashIssueAndPayNoSelectionTests { @Before fun start() { - mockNet = MockNetwork(servicePeerAllocationStrategy = RoundRobin(), cordappPackages = listOf("com.r3.corda.enterprise.perftestcordapp.contracts.asset")) + mockNet = MockNetwork(servicePeerAllocationStrategy = RoundRobin(), + cordappPackages = listOf("com.r3.corda.enterprise.perftestcordapp.contracts.asset")) bankOfCordaNode = mockNet.createPartyNode(BOC.name) aliceNode = mockNet.createPartyNode(ALICE.name) bankOfCorda = bankOfCordaNode.info.chooseIdentity() @@ -51,10 +60,13 @@ class CashIssueAndPayNoSelectionTests { bankOfCordaNode.database.transaction { // Register for vault updates val criteria = QueryCriteria.VaultQueryCriteria(status = Vault.StateStatus.ALL) - val (_, vaultUpdatesBoc) = bankOfCordaNode.services.vaultService.trackBy(criteria) - val (_, vaultUpdatesBankClient) = aliceNode.services.vaultService.trackBy(criteria) + val (_, vaultUpdatesBoc) + = bankOfCordaNode.services.vaultService.trackBy(criteria) + val (_, vaultUpdatesBankClient) + = aliceNode.services.vaultService.trackBy(criteria) - val future = bankOfCordaNode.services.startFlow(CashIssueAndPaymentNoSelection(expectedPayment, OpaqueBytes.of(1), payTo, false, notary)).resultFuture + val future = bankOfCordaNode.services.startFlow(CashIssueAndPaymentNoSelection( + expectedPayment, OpaqueBytes.of(1), payTo, anonymous, notary)).resultFuture mockNet.runNetwork() future.getOrThrow()