diff --git a/core/src/main/kotlin/net/corda/core/node/services/IdentityService.kt b/core/src/main/kotlin/net/corda/core/node/services/IdentityService.kt index f5b765fd8d..e3a17ba222 100644 --- a/core/src/main/kotlin/net/corda/core/node/services/IdentityService.kt +++ b/core/src/main/kotlin/net/corda/core/node/services/IdentityService.kt @@ -10,6 +10,11 @@ import net.corda.core.crypto.Party */ interface IdentityService { fun registerIdentity(party: Party) + /** + * Get all identities known to the service. This is expensive, and [partyFromKey] or [partyFromName] should be + * used in preference where possible. + */ + fun getAllIdentities(): Iterable // There is no method for removing identities, as once we are made aware of a Party we want to keep track of them // indefinitely. It may be that in the long term we need to drop or archive very old Party information for space, diff --git a/node/src/main/kotlin/net/corda/node/services/identity/InMemoryIdentityService.kt b/node/src/main/kotlin/net/corda/node/services/identity/InMemoryIdentityService.kt index fe247ace48..0c4ee18a6c 100644 --- a/node/src/main/kotlin/net/corda/node/services/identity/InMemoryIdentityService.kt +++ b/node/src/main/kotlin/net/corda/node/services/identity/InMemoryIdentityService.kt @@ -4,6 +4,7 @@ import net.corda.core.crypto.CompositeKey import net.corda.core.crypto.Party import net.corda.core.node.services.IdentityService import net.corda.core.serialization.SingletonSerializeAsToken +import java.util.* import java.util.concurrent.ConcurrentHashMap import javax.annotation.concurrent.ThreadSafe @@ -20,6 +21,9 @@ class InMemoryIdentityService() : SingletonSerializeAsToken(), IdentityService { nameToParties[party.name] = party } + // We give the caller a copy of the data set to avoid any locking problems + override fun getAllIdentities(): Iterable = ArrayList(keyToParties.values) + override fun partyFromKey(key: CompositeKey): Party? = keyToParties[key] override fun partyFromName(name: String): Party? = nameToParties[name] } diff --git a/node/src/test/kotlin/net/corda/node/services/InMemoryIdentityServiceTests.kt b/node/src/test/kotlin/net/corda/node/services/InMemoryIdentityServiceTests.kt new file mode 100644 index 0000000000..f74e59cc33 --- /dev/null +++ b/node/src/test/kotlin/net/corda/node/services/InMemoryIdentityServiceTests.kt @@ -0,0 +1,50 @@ +package net.corda.node.services + +import net.corda.node.services.identity.InMemoryIdentityService +import net.corda.testing.ALICE +import net.corda.testing.ALICE_PUBKEY +import net.corda.testing.BOB +import net.corda.testing.BOB_PUBKEY +import org.junit.Test +import kotlin.test.assertEquals +import kotlin.test.assertNull + +/** + * Tests for the in memory identity service. + */ +class InMemoryIdentityServiceTests { + + @Test + fun `get all identities`() { + val service = InMemoryIdentityService() + assertNull(service.getAllIdentities().firstOrNull()) + service.registerIdentity(ALICE) + var expected = setOf(ALICE) + var actual = service.getAllIdentities().toHashSet() + assertEquals(expected, actual) + + // Add a second party and check we get both back + service.registerIdentity(BOB) + expected = setOf(ALICE, BOB) + actual = service.getAllIdentities().toHashSet() + assertEquals(expected, actual) + } + + @Test + fun `get identity by key`() { + val service = InMemoryIdentityService() + assertNull(service.partyFromKey(ALICE_PUBKEY)) + service.registerIdentity(ALICE) + assertEquals(ALICE, service.partyFromKey(ALICE_PUBKEY)) + assertNull(service.partyFromKey(BOB_PUBKEY)) + } + + @Test + fun `get identity by name`() { + val service = InMemoryIdentityService() + assertNull(service.partyFromName(ALICE.name)) + service.registerIdentity(ALICE) + assertEquals(ALICE, service.partyFromName(ALICE.name)) + assertNull(service.partyFromName(BOB.name)) + } +} \ No newline at end of file diff --git a/test-utils/src/main/kotlin/net/corda/testing/node/MockServices.kt b/test-utils/src/main/kotlin/net/corda/testing/node/MockServices.kt index 5633dd4dab..a99247b6ad 100644 --- a/test-utils/src/main/kotlin/net/corda/testing/node/MockServices.kt +++ b/test-utils/src/main/kotlin/net/corda/testing/node/MockServices.kt @@ -72,6 +72,7 @@ class MockIdentityService(val identities: List) : IdentityService, Single get() = synchronized(identities) { identities.associateBy { it.name } } override fun registerIdentity(party: Party) { throw UnsupportedOperationException() } + override fun getAllIdentities(): Iterable = ArrayList(keyToParties.values) override fun partyFromKey(key: CompositeKey): Party? = keyToParties[key] override fun partyFromName(name: String): Party? = nameToParties[name] }