Skip to content

Commit

Permalink
Merge pull request corda#1053 from corda/kit-getServiceOf
Browse files Browse the repository at this point in the history
Extending NetwokMapCache with a convenience method for by type servic…
  • Loading branch information
mkit authored Jul 27, 2017
2 parents 9cc3428 + 818f8a5 commit 197e627
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,20 @@ interface NetworkMapCache {
return nodes.randomOrNull()?.notaryIdentity
}

/**
* Returns a service identity advertised by one of the nodes on the network
* @param type Specifies the type of the service
*/
fun getAnyServiceOfType(type: ServiceType): Party? {
for (node in partyNodes) {
val serviceIdentities = node.serviceIdentities(type)
if (serviceIdentities.isNotEmpty()) {
return serviceIdentities.randomOrNull()
}
}
return null;
}

/** Checks whether a given party is an advertised notary identity */
fun isNotary(party: Party): Boolean = notaryNodes.any { it.notaryIdentity == party }

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package net.corda.node.services

import co.paralleluniverse.fibers.Suspendable
import net.corda.core.flows.FlowLogic
import net.corda.core.flows.StartableByRPC
import net.corda.core.messaging.startFlow
import net.corda.core.node.services.ServiceInfo
import net.corda.core.node.services.ServiceType
import net.corda.nodeapi.User
import net.corda.testing.driver.driver
import org.bouncycastle.asn1.x500.X500Name
import org.junit.Test
import kotlin.test.assertTrue

class AdvertisedServiceTests {
private val serviceName = X500Name("CN=Custom Service,O=R3,OU=corda,L=London,C=GB")
private val serviceType = ServiceType.corda.getSubType("custom")
private val user = "bankA"
private val pass = "passA"


@StartableByRPC
class ServiceTypeCheckingFlow : FlowLogic<Boolean>() {

@Suspendable
override fun call(): Boolean {
return serviceHub.networkMapCache.getAnyServiceOfType(ServiceType.corda.getSubType("custom")) != null;
}
}

@Test
fun `service is accessible through getAnyServiceOfType`() {
driver(startNodesInProcess = true) {
val bankA = startNode(rpcUsers = listOf(User(user, pass, setOf(startFlowPermission<ServiceTypeCheckingFlow>())))).get()
val bankB = startNode(advertisedServices = setOf(ServiceInfo(serviceType, serviceName))).get()
bankA.rpcClientToNode().use(user, pass) { connection ->
val result = connection.proxy.startFlow(::ServiceTypeCheckingFlow).returnValue.get()
assertTrue(result)
}
}
}
}

0 comments on commit 197e627

Please sign in to comment.