From 9a77e63cf881443374c50801542d5b0e60bfcbaa Mon Sep 17 00:00:00 2001 From: Shams Asari Date: Thu, 7 Sep 2017 19:13:57 +0100 Subject: [PATCH] Added missing app name on session-init messages --- .../net/corda/node/internal/AbstractNode.kt | 9 ++--- .../statemachine/FlowStateMachineImpl.kt | 13 +++++++- .../kotlin/net/corda/node/CordappSmokeTest.kt | 33 ++++++++++++++----- 3 files changed, 38 insertions(+), 17 deletions(-) diff --git a/node/src/main/kotlin/net/corda/node/internal/AbstractNode.kt b/node/src/main/kotlin/net/corda/node/internal/AbstractNode.kt index b03cd9e989..d2e7c742fc 100644 --- a/node/src/main/kotlin/net/corda/node/internal/AbstractNode.kt +++ b/node/src/main/kotlin/net/corda/node/internal/AbstractNode.kt @@ -55,6 +55,7 @@ import net.corda.node.services.schema.HibernateObserver import net.corda.node.services.schema.NodeSchemaService import net.corda.node.services.statemachine.FlowStateMachineImpl import net.corda.node.services.statemachine.StateMachineManager +import net.corda.node.services.statemachine.appName import net.corda.node.services.statemachine.flowVersionAndInitiatingClass import net.corda.node.services.transactions.* import net.corda.node.services.upgrade.ContractUpgradeServiceImpl @@ -349,13 +350,7 @@ abstract class AbstractNode(open val configuration: NodeConfiguration, require(classWithAnnotation == initiatingFlow) { "${InitiatedBy::class.java.name} must point to ${classWithAnnotation.name} and not ${initiatingFlow.name}" } - val jarFile = Paths.get(initiatedFlow.protectionDomain.codeSource.location.toURI()) - val appName = if (jarFile.isRegularFile() && jarFile.toString().endsWith(".jar")) { - jarFile.fileName.toString().removeSuffix(".jar") - } else { - "" - } - val flowFactory = InitiatedFlowFactory.CorDapp(version, appName, { ctor.newInstance(it) }) + val flowFactory = InitiatedFlowFactory.CorDapp(version, initiatedFlow.appName, { ctor.newInstance(it) }) val observable = internalRegisterFlowFactory(initiatingFlow, flowFactory, initiatedFlow, track) log.info("Registered ${initiatingFlow.name} to initiate ${initiatedFlow.name} (version $version)") return observable diff --git a/node/src/main/kotlin/net/corda/node/services/statemachine/FlowStateMachineImpl.kt b/node/src/main/kotlin/net/corda/node/services/statemachine/FlowStateMachineImpl.kt index d8fa038ed2..df6daf71e6 100644 --- a/node/src/main/kotlin/net/corda/node/services/statemachine/FlowStateMachineImpl.kt +++ b/node/src/main/kotlin/net/corda/node/services/statemachine/FlowStateMachineImpl.kt @@ -15,6 +15,7 @@ import net.corda.core.internal.FlowStateMachine import net.corda.core.internal.abbreviate import net.corda.core.internal.concurrent.OpenFuture import net.corda.core.internal.concurrent.openFuture +import net.corda.core.internal.isRegularFile import net.corda.core.internal.staticField import net.corda.core.transactions.SignedTransaction import net.corda.core.utilities.* @@ -27,6 +28,7 @@ import net.corda.node.utilities.DatabaseTransaction import net.corda.node.utilities.DatabaseTransactionManager import org.slf4j.Logger import org.slf4j.LoggerFactory +import java.nio.file.Paths import java.sql.SQLException import java.util.* import java.util.concurrent.TimeUnit @@ -347,7 +349,7 @@ class FlowStateMachineImpl(override val id: StateMachineRunId, val session = FlowSession(sessionFlow, random63BitValue(), null, FlowSessionState.Initiating(otherParty), retryable) openSessions[Pair(sessionFlow, otherParty)] = session val (version, initiatingFlowClass) = sessionFlow.javaClass.flowVersionAndInitiatingClass - val sessionInit = SessionInit(session.ourSessionId, initiatingFlowClass.name, version, "not defined", firstPayload) + val sessionInit = SessionInit(session.ourSessionId, initiatingFlowClass.name, version, sessionFlow.javaClass.appName, firstPayload) sendInternal(session, sessionInit) if (waitForConfirmation) { session.waitForConfirmation() @@ -491,3 +493,12 @@ val Class>.flowVersionAndInitiatingClass: Pair>.appName: String get() { + val jarFile = Paths.get(protectionDomain.codeSource.location.toURI()) + return if (jarFile.isRegularFile() && jarFile.toString().endsWith(".jar")) { + jarFile.fileName.toString().removeSuffix(".jar") + } else { + "" + } +} diff --git a/node/src/smoke-test/kotlin/net/corda/node/CordappSmokeTest.kt b/node/src/smoke-test/kotlin/net/corda/node/CordappSmokeTest.kt index 26a913db24..f295c81398 100644 --- a/node/src/smoke-test/kotlin/net/corda/node/CordappSmokeTest.kt +++ b/node/src/smoke-test/kotlin/net/corda/node/CordappSmokeTest.kt @@ -10,6 +10,7 @@ import net.corda.core.internal.list import net.corda.core.messaging.startFlow import net.corda.core.utilities.getOrThrow import net.corda.core.utilities.getX500Name +import net.corda.core.utilities.unwrap import net.corda.nodeapi.User import net.corda.smoketesting.NodeConfig import net.corda.smoketesting.NodeProcess @@ -40,16 +41,19 @@ class CordappSmokeTest { fun `FlowContent appName returns the filename of the CorDapp jar`() { val pluginsDir = (factory.baseDirectory(aliceConfig) / "plugins").createDirectories() // Find the jar file for the smoke tests of this module - val selfCorDapp = Paths.get("build", "libs").list { + val selfCordapp = Paths.get("build", "libs").list { it.filter { "-smoke-test" in it.toString() }.toList().single() } - selfCorDapp.copyToDirectory(pluginsDir) + selfCordapp.copyToDirectory(pluginsDir) factory.create(aliceConfig).use { alice -> alice.connect().use { connectionToAlice -> val aliceIdentity = connectionToAlice.proxy.nodeIdentity().legalIdentity - val future = connectionToAlice.proxy.startFlow(::DummyInitiatingFlow, aliceIdentity).returnValue - assertThat(future.getOrThrow().appName).isEqualTo(selfCorDapp.fileName.toString().removeSuffix(".jar")) + val future = connectionToAlice.proxy.startFlow(::GatherContextsFlow, aliceIdentity).returnValue + val (sessionInitContext, sessionConfirmContext) = future.getOrThrow() + val selfCordappName = selfCordapp.fileName.toString().removeSuffix(".jar") + assertThat(sessionInitContext.appName).isEqualTo(selfCordappName) + assertThat(sessionConfirmContext.appName).isEqualTo(selfCordappName) } } } @@ -62,15 +66,26 @@ class CordappSmokeTest { @InitiatingFlow @StartableByRPC - class DummyInitiatingFlow(val otherParty: Party) : FlowLogic() { + class GatherContextsFlow(private val otherParty: Party) : FlowLogic>() { @Suspendable - override fun call() = getFlowContext(otherParty) + override fun call(): Pair { + // This receive will kick off SendBackInitiatorFlowContext by sending a session-init with our app name. + // SendBackInitiatorFlowContext will send back our context using the information from this session-init + val sessionInitContext = receive(otherParty).unwrap { it } + // This context is taken from the session-confirm message + val sessionConfirmContext = getFlowContext(otherParty) + return Pair(sessionInitContext, sessionConfirmContext) + } } @Suppress("unused") - @InitiatedBy(DummyInitiatingFlow::class) - class DummyInitiatedFlow(val otherParty: Party) : FlowLogic() { + @InitiatedBy(GatherContextsFlow::class) + class SendBackInitiatorFlowContext(private val otherParty: Party) : FlowLogic() { @Suspendable - override fun call() = Unit + override fun call() { + // An initiated flow calling getFlowContext on its initiator will get the context from the session-init + val sessionInitContext = getFlowContext(otherParty) + send(otherParty, sessionInitContext) + } } }