mirror of
https://github.com/corda/corda.git
synced 2025-01-28 23:24:29 +00:00
Remove primary key constraint on host and port (#336)
* Remove primary key constraint on DBHostAndPort * Update migration file
This commit is contained in:
parent
9575381733
commit
133cf13674
@ -66,26 +66,24 @@ object NodeInfoSchemaV1 : MappedSchema(
|
||||
}
|
||||
}
|
||||
|
||||
@Embeddable
|
||||
data class PKHostAndPort(
|
||||
val host: String? = null,
|
||||
val port: Int? = null
|
||||
) : Serializable
|
||||
|
||||
@Entity
|
||||
@Table(name = "node_info_hosts")
|
||||
data class DBHostAndPort(
|
||||
@EmbeddedId
|
||||
private val pk: PKHostAndPort
|
||||
@Id
|
||||
@GeneratedValue
|
||||
@Column(name = "hosts_id")
|
||||
var id: Int,
|
||||
val host: String? = null,
|
||||
val port: Int? = null
|
||||
) {
|
||||
companion object {
|
||||
fun fromHostAndPort(hostAndPort: NetworkHostAndPort) = DBHostAndPort(
|
||||
PKHostAndPort(hostAndPort.host, hostAndPort.port)
|
||||
0, hostAndPort.host, hostAndPort.port
|
||||
)
|
||||
}
|
||||
|
||||
fun toHostAndPort(): NetworkHostAndPort {
|
||||
return NetworkHostAndPort(this.pk.host!!, this.pk.port!!)
|
||||
return NetworkHostAndPort(host!!, port!!)
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -1,5 +1,6 @@
|
||||
package net.corda.node.services.network
|
||||
|
||||
import net.corda.core.crypto.generateKeyPair
|
||||
import net.corda.core.identity.CordaX500Name
|
||||
import net.corda.core.identity.Party
|
||||
import net.corda.core.node.NodeInfo
|
||||
@ -15,6 +16,7 @@ import org.junit.ClassRule
|
||||
import org.junit.Test
|
||||
import kotlin.test.assertEquals
|
||||
|
||||
// TODO Clean up these tests, they were written with old network map design in place.
|
||||
class PersistentNetworkMapCacheTest : NodeBasedTest() {
|
||||
companion object {
|
||||
val ALICE = TestIdentity(ALICE_NAME, 70).party
|
||||
@ -60,6 +62,19 @@ class PersistentNetworkMapCacheTest : NodeBasedTest() {
|
||||
}
|
||||
}
|
||||
|
||||
// This test has to be done as normal node not mock, because MockNodes don't have addresses.
|
||||
@Test
|
||||
fun `insert two node infos with the same host and port`() {
|
||||
val aliceNode = startNode(ALICE_NAME)
|
||||
val charliePartyCert = getTestPartyAndCertificate(CHARLIE_NAME, generateKeyPair().public)
|
||||
val aliceCache = aliceNode.services.networkMapCache
|
||||
aliceCache.addNode(aliceNode.info.copy(legalIdentitiesAndCerts = listOf(charliePartyCert)))
|
||||
val res = aliceNode.database.transaction {
|
||||
aliceCache.allNodes.filter { aliceNode.info.addresses[0] in it.addresses }
|
||||
}
|
||||
assertEquals(2, res.size)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `restart node with DB map cache`() {
|
||||
val alice = startNodesWithPort(listOf(ALICE))[0]
|
||||
|
@ -279,13 +279,13 @@ open class PersistentNetworkMapCache(
|
||||
|
||||
private fun queryByAddress(session: Session, hostAndPort: NetworkHostAndPort): NodeInfo? {
|
||||
val query = session.createQuery(
|
||||
"SELECT n FROM ${NodeInfoSchemaV1.PersistentNodeInfo::class.java.name} n JOIN n.addresses a WHERE a.pk.host = :host AND a.pk.port = :port",
|
||||
"SELECT n FROM ${NodeInfoSchemaV1.PersistentNodeInfo::class.java.name} n JOIN n.addresses a WHERE a.host = :host AND a.port = :port",
|
||||
NodeInfoSchemaV1.PersistentNodeInfo::class.java)
|
||||
query.setParameter("host", hostAndPort.host)
|
||||
query.setParameter("port", hostAndPort.port)
|
||||
query.setMaxResults(1)
|
||||
val result = query.resultList
|
||||
return if (result.isEmpty()) null
|
||||
else result.map { it.toNodeInfo() }.singleOrNull() ?: throw IllegalStateException("More than one node with the same host and port")
|
||||
return result.map { it.toNodeInfo() }.singleOrNull()
|
||||
}
|
||||
|
||||
/** Object Relational Mapping support. */
|
||||
|
@ -67,4 +67,17 @@
|
||||
constraintName="FK__info_hosts__infos"
|
||||
referencedColumnNames="node_info_id" referencedTableName="node_infos"/>
|
||||
</changeSet>
|
||||
<changeSet author="R3.Corda" id="remove_host_port_pk">
|
||||
<delete tableName="node_infos"/>
|
||||
<delete tableName="node_link_nodeinfo_party"/>
|
||||
<delete tableName="node_info_hosts"/>
|
||||
<delete tableName="node_info_party_cert"/>
|
||||
<dropPrimaryKey tableName="node_info_hosts" constraintName="node_info_hosts_pkey"/>
|
||||
<addColumn tableName="node_info_hosts">
|
||||
<column name="hosts_id" type="INT">
|
||||
<constraints nullable="false"/>
|
||||
</column>
|
||||
</addColumn>
|
||||
<addPrimaryKey columnNames="hosts_id" constraintName="node_info_hosts_pkey_id" tableName="node_info_hosts"/>
|
||||
</changeSet>
|
||||
</databaseChangeLog>
|
||||
|
@ -1,9 +1,11 @@
|
||||
package net.corda.node.services.network
|
||||
|
||||
import net.corda.core.crypto.generateKeyPair
|
||||
import net.corda.core.node.services.NetworkMapCache
|
||||
import net.corda.node.services.api.NetworkMapCacheInternal
|
||||
import net.corda.testing.ALICE_NAME
|
||||
import net.corda.testing.BOB_NAME
|
||||
import net.corda.testing.getTestPartyAndCertificate
|
||||
import net.corda.testing.node.MockNetwork
|
||||
import net.corda.testing.node.MockNodeParameters
|
||||
import net.corda.testing.singleIdentity
|
||||
@ -106,4 +108,14 @@ class NetworkMapCacheTest {
|
||||
assertThat(bobCache.getNodeByLegalName(alice.name) == null)
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `add two nodes the same name different keys`() {
|
||||
val aliceNode = mockNet.createPartyNode(ALICE_NAME)
|
||||
val aliceCache = aliceNode.services.networkMapCache
|
||||
val alicePartyAndCert2 = getTestPartyAndCertificate(ALICE_NAME, generateKeyPair().public)
|
||||
aliceCache.addNode(aliceNode.info.copy(legalIdentitiesAndCerts = listOf(alicePartyAndCert2)))
|
||||
// This is correct behaviour as we may have distributed service nodes.
|
||||
assertEquals(2, aliceCache.getNodesByLegalName(ALICE_NAME).size)
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user