Extend NetworkMapCache API (#1766)

* Add functions to:
** Return PartyAndCertificate rather than just Party
** Return all NodeInfo entries for a name (rather than just by key)
* General documentation improvements
This commit is contained in:
Ross Nicoll
2017-10-06 15:23:20 +01:00
committed by GitHub
parent 3afe855042
commit 6562579e8e
4 changed files with 74 additions and 21 deletions

View File

@ -4,6 +4,7 @@ import net.corda.core.concurrent.CordaFuture
import net.corda.core.identity.AbstractParty
import net.corda.core.identity.CordaX500Name
import net.corda.core.identity.Party
import net.corda.core.identity.PartyAndCertificate
import net.corda.core.internal.VisibleForTesting
import net.corda.core.internal.bufferUntilSubscribed
import net.corda.core.internal.concurrent.map
@ -114,7 +115,8 @@ open class PersistentNetworkMapCache(private val serviceHub: ServiceHubInternal)
return null
}
override fun getNodeByLegalName(name: CordaX500Name): NodeInfo? = serviceHub.database.transaction { queryByLegalName(name).firstOrNull() }
override fun getNodeByLegalName(name: CordaX500Name): NodeInfo? = getNodesByLegalName(name).firstOrNull()
override fun getNodesByLegalName(name: CordaX500Name): List<NodeInfo> = serviceHub.database.transaction { queryByLegalName(name) }
override fun getNodesByLegalIdentityKey(identityKey: PublicKey): List<NodeInfo> =
serviceHub.database.transaction { queryByIdentityKey(identityKey) }
override fun getNodeByLegalIdentity(party: AbstractParty): NodeInfo? {
@ -126,6 +128,8 @@ open class PersistentNetworkMapCache(private val serviceHub: ServiceHubInternal)
override fun getNodeByAddress(address: NetworkHostAndPort): NodeInfo? = serviceHub.database.transaction { queryByAddress(address) }
override fun getPeerCertificateByLegalName(name: CordaX500Name): PartyAndCertificate? = serviceHub.database.transaction { queryIdentityByLegalName(name) }
override fun track(): DataFeed<List<NodeInfo>, MapChange> {
synchronized(_changed) {
return DataFeed(partyNodes, _changed.bufferUntilSubscribed().wrapWithDatabaseTransaction())
@ -329,6 +333,19 @@ open class PersistentNetworkMapCache(private val serviceHub: ServiceHubInternal)
}
}
private fun queryIdentityByLegalName(name: CordaX500Name): PartyAndCertificate? {
createSession {
val query = it.createQuery(
// We do the JOIN here to restrict results to those present in the network map
"SELECT DISTINCT l FROM ${NodeInfoSchemaV1.PersistentNodeInfo::class.java.name} n JOIN n.legalIdentitiesAndCerts l WHERE l.name = :name",
NodeInfoSchemaV1.DBPartyAndCertificate::class.java)
query.setParameter("name", name.toString())
val candidates = query.resultList.map { it.toLegalIdentityAndCert() }
// The map is restricted to holding a single identity for any X.500 name, so firstOrNull() is correct here.
return candidates.firstOrNull()
}
}
private fun queryByLegalName(name: CordaX500Name): List<NodeInfo> {
createSession {
val query = it.createQuery(

View File

@ -65,6 +65,18 @@ class NetworkMapCacheTest {
// TODO: Should have a test case with anonymous lookup
}
@Test
fun `getPeerByLegalName`() {
val notaryNode = mockNet.createNotaryNode()
val aliceNode = mockNet.createPartyNode(ALICE.name)
val notaryCache: NetworkMapCache = notaryNode.services.networkMapCache
val expected = aliceNode.info.legalIdentities.single()
mockNet.runNetwork()
val actual = notaryNode.database.transaction { notaryCache.getPeerByLegalName(ALICE.name) }
assertEquals(expected, actual)
}
@Test
fun `remove node from cache`() {
val notaryNode = mockNet.createNotaryNode()