From 66cbe8c56d72edeca33bd39bf5063cab54ab9a4a Mon Sep 17 00:00:00 2001 From: Andras Slemmer Date: Wed, 21 Feb 2018 15:56:29 +0000 Subject: [PATCH] CORDA-1043 --- .../main/kotlin/net/corda/core/flows/FlowLogic.kt | 4 ++-- .../kotlin/net/corda/core/flows/FlowTestsUtils.kt | 3 +-- docs/source/api-flows.rst | 7 +++++++ .../src/main/kotlin/net/corda/docs/FlowCookbook.kt | 14 ++++++++++++++ 4 files changed, 24 insertions(+), 4 deletions(-) diff --git a/core/src/main/kotlin/net/corda/core/flows/FlowLogic.kt b/core/src/main/kotlin/net/corda/core/flows/FlowLogic.kt index 6b92c533ab..c0a369a730 100644 --- a/core/src/main/kotlin/net/corda/core/flows/FlowLogic.kt +++ b/core/src/main/kotlin/net/corda/core/flows/FlowLogic.kt @@ -232,7 +232,7 @@ abstract class FlowLogic { * @returns a [Map] containing the objects received, wrapped in an [UntrustworthyData], by the [FlowSession]s who sent them. */ @Suspendable - open fun receiveAll(sessions: Map>): Map> { + open fun receiveAllMap(sessions: Map>): Map> { return stateMachine.receiveAll(sessions, this) } @@ -250,7 +250,7 @@ abstract class FlowLogic { @Suspendable open fun receiveAll(receiveType: Class, sessions: List): List> { enforceNoDuplicates(sessions) - return castMapValuesToKnownType(receiveAll(associateSessionsToReceiveType(receiveType, sessions))) + return castMapValuesToKnownType(receiveAllMap(associateSessionsToReceiveType(receiveType, sessions))) } /** diff --git a/core/src/test/kotlin/net/corda/core/flows/FlowTestsUtils.kt b/core/src/test/kotlin/net/corda/core/flows/FlowTestsUtils.kt index 9ea4d254f1..1c5934aadb 100644 --- a/core/src/test/kotlin/net/corda/core/flows/FlowTestsUtils.kt +++ b/core/src/test/kotlin/net/corda/core/flows/FlowTestsUtils.kt @@ -5,7 +5,6 @@ import net.corda.core.utilities.UntrustworthyData import net.corda.core.utilities.unwrap import net.corda.node.internal.InitiatedFlowFactory import net.corda.node.internal.StartedNode -import net.corda.testing.node.StartedMockNode import net.corda.testing.node.internal.InternalMockNetwork import kotlin.reflect.KClass @@ -81,7 +80,7 @@ infix fun KClass.from(session: FlowSession): Pair.receiveAll(session: Pair>, vararg sessions: Pair>): Map> { val allSessions = arrayOf(session, *sessions) allSessions.enforceNoDuplicates() - return receiveAll(mapOf(*allSessions)) + return receiveAllMap(mapOf(*allSessions)) } /** diff --git a/docs/source/api-flows.rst b/docs/source/api-flows.rst index f42661aa85..780c2572da 100644 --- a/docs/source/api-flows.rst +++ b/docs/source/api-flows.rst @@ -264,6 +264,13 @@ In order to create a communication session between your initiator flow and the r * ``sendAndReceive(receiveType: Class, payload: Any): R`` * Sends the ``payload`` object and receives an object of type ``receiveType`` back +In addition ``FlowLogic`` provides functions that batch receives: +* ``receiveAllMap(sessions: Map>): Map>`` + * Receives from all ``FlowSession``s specified in the passed in map. The received types may differ. +* ``receiveAll(receiveType: Class, sessions: List): List>`` + * Receives from all ``FlowSession``s specified in the passed in list. The received types must be the same. + +The batched functions are implemented more efficiently by the flow framework. InitiateFlow ~~~~~~~~~~~~ diff --git a/docs/source/example-code/src/main/kotlin/net/corda/docs/FlowCookbook.kt b/docs/source/example-code/src/main/kotlin/net/corda/docs/FlowCookbook.kt index e0179281da..0528caeaf3 100644 --- a/docs/source/example-code/src/main/kotlin/net/corda/docs/FlowCookbook.kt +++ b/docs/source/example-code/src/main/kotlin/net/corda/docs/FlowCookbook.kt @@ -27,6 +27,7 @@ import net.corda.finance.contracts.asset.Cash import net.corda.testing.contracts.DummyContract import net.corda.testing.contracts.DummyState import java.security.PublicKey +import java.security.Signature import java.time.Instant // ``InitiatorFlow`` is our first flow, and will communicate with @@ -205,6 +206,19 @@ class InitiatorFlow(val arg1: Boolean, val arg2: Int, private val counterparty: val packet3: UntrustworthyData = regulatorSession.receive() // DOCEND 06 + // We may also batch receives in order to increase performance. This + // ensures that only a single checkpoint is created for all received + // messages. + // Type-safe variant: + val signatures: List> = + receiveAll(Signature::class.java, listOf(counterpartySession, regulatorSession)) + // Dynamic variant: + val messages: Map> = + receiveAllMap(mapOf( + counterpartySession to Boolean::class.java, + regulatorSession to String::class.java + )) + /**----------------------------------- * EXTRACTING STATES FROM THE VAULT * -----------------------------------**/