From 7f6608deb5baa24dc67b3c5ab47b73a405a829aa Mon Sep 17 00:00:00 2001 From: josecoll Date: Tue, 24 Oct 2017 11:41:59 +0100 Subject: [PATCH] Retrofit changes from Enterprise PR #61 (#1934) --- .../net/corda/core/schemas/NodeInfoSchema.kt | 11 +++---- .../identity/PersistentIdentityService.kt | 5 +-- .../keys/PersistentKeyManagementService.kt | 31 ++++++++++--------- .../network/PersistentNetworkMapService.kt | 15 +++++---- .../corda/node/utilities/CordaPersistence.kt | 8 +++++ 5 files changed, 42 insertions(+), 28 deletions(-) diff --git a/core/src/main/kotlin/net/corda/core/schemas/NodeInfoSchema.kt b/core/src/main/kotlin/net/corda/core/schemas/NodeInfoSchema.kt index a0e6c80f49..b8b8ef9460 100644 --- a/core/src/main/kotlin/net/corda/core/schemas/NodeInfoSchema.kt +++ b/core/src/main/kotlin/net/corda/core/schemas/NodeInfoSchema.kt @@ -85,13 +85,12 @@ object NodeInfoSchemaV1 : MappedSchema( @Table(name = "node_info_party_cert") data class DBPartyAndCertificate( @Id - @Column(name = "owning_key", length = 65535, nullable = false) - val owningKey: String, - - //@Id // TODO Do we assume that names are unique? Note: We can't have it as Id, because our toString on X500 is inconsistent. @Column(name = "party_name", nullable = false) val name: String, + @Column(name = "owning_key", length = 65535, nullable = false) + val owningKey: String, + @Column(name = "party_cert_binary") @Lob val partyCertBinary: ByteArray, @@ -102,10 +101,10 @@ object NodeInfoSchemaV1 : MappedSchema( private val persistentNodeInfos: Set = emptySet() ) { constructor(partyAndCert: PartyAndCertificate, isMain: Boolean = false) - : this(partyAndCert.party.owningKey.toBase58String(), partyAndCert.party.name.toString(), partyAndCert.serialize().bytes, isMain) + : this(partyAndCert.name.toString(), partyAndCert.party.owningKey.toBase58String(), partyAndCert.serialize().bytes, isMain) fun toLegalIdentityAndCert(): PartyAndCertificate { - return partyCertBinary.deserialize() + return partyCertBinary.deserialize() } } } diff --git a/node/src/main/kotlin/net/corda/node/services/identity/PersistentIdentityService.kt b/node/src/main/kotlin/net/corda/node/services/identity/PersistentIdentityService.kt index 7d3151c63b..04cb310e69 100644 --- a/node/src/main/kotlin/net/corda/node/services/identity/PersistentIdentityService.kt +++ b/node/src/main/kotlin/net/corda/node/services/identity/PersistentIdentityService.kt @@ -12,6 +12,7 @@ import net.corda.core.serialization.SingletonSerializeAsToken import net.corda.core.utilities.debug import net.corda.core.utilities.loggerFor import net.corda.node.utilities.AppendOnlyPersistentMap +import net.corda.node.utilities.MAX_HASH_HEX_SIZE import net.corda.node.utilities.NODE_DATABASE_PREFIX import org.bouncycastle.cert.X509CertificateHolder import java.io.ByteArrayInputStream @@ -72,7 +73,7 @@ class PersistentIdentityService(identities: Iterable = empt @javax.persistence.Table(name = "${NODE_DATABASE_PREFIX}identities") class PersistentIdentity( @Id - @Column(name = "pk_hash", length = 64) + @Column(name = "pk_hash", length = MAX_HASH_HEX_SIZE) var publicKeyHash: String = "", @Lob @@ -87,7 +88,7 @@ class PersistentIdentityService(identities: Iterable = empt @Column(name = "name", length = 128) var name: String = "", - @Column(name = "pk_hash", length = 64) + @Column(name = "pk_hash", length = MAX_HASH_HEX_SIZE) var publicKeyHash: String = "" ) diff --git a/node/src/main/kotlin/net/corda/node/services/keys/PersistentKeyManagementService.kt b/node/src/main/kotlin/net/corda/node/services/keys/PersistentKeyManagementService.kt index 4645c1d8e0..7316562ce1 100644 --- a/node/src/main/kotlin/net/corda/node/services/keys/PersistentKeyManagementService.kt +++ b/node/src/main/kotlin/net/corda/node/services/keys/PersistentKeyManagementService.kt @@ -8,9 +8,8 @@ import net.corda.core.serialization.SerializationDefaults import net.corda.core.serialization.SingletonSerializeAsToken import net.corda.core.serialization.deserialize import net.corda.core.serialization.serialize -import net.corda.core.utilities.parsePublicKeyBase58 -import net.corda.core.utilities.toBase58String import net.corda.node.utilities.AppendOnlyPersistentMap +import net.corda.node.utilities.MAX_HASH_HEX_SIZE import net.corda.node.utilities.NODE_DATABASE_PREFIX import org.bouncycastle.operator.ContentSigner import java.security.KeyPair @@ -36,27 +35,31 @@ class PersistentKeyManagementService(val identityService: IdentityService, class PersistentKey( @Id - @Column(length = 6000, name = "public_key") - var publicKey: String = "", + @Column(name = "public_key_hash", length = MAX_HASH_HEX_SIZE) + var publicKeyHash: String, + + @Lob + @Column(name = "public_key") + var publicKey: ByteArray = ByteArray(0), @Lob @Column(name = "private_key") var privateKey: ByteArray = ByteArray(0) - ) + ) { + constructor(publicKey: PublicKey, privateKey: PrivateKey) + : this(publicKey.toStringShort(), + publicKey.serialize(context = SerializationDefaults.STORAGE_CONTEXT).bytes, + privateKey.serialize(context = SerializationDefaults.STORAGE_CONTEXT).bytes) + } private companion object { fun createKeyMap(): AppendOnlyPersistentMap { return AppendOnlyPersistentMap( - toPersistentEntityKey = { it.toBase58String() }, - fromPersistentEntity = { - Pair(parsePublicKeyBase58(it.publicKey), - it.privateKey.deserialize(context = SerializationDefaults.STORAGE_CONTEXT)) - }, + toPersistentEntityKey = { it.toStringShort() }, + fromPersistentEntity = { Pair(it.publicKey.deserialize(context = SerializationDefaults.STORAGE_CONTEXT), + it.privateKey.deserialize(context = SerializationDefaults.STORAGE_CONTEXT)) }, toPersistentEntity = { key: PublicKey, value: PrivateKey -> - PersistentKey().apply { - publicKey = key.toBase58String() - privateKey = value.serialize(context = SerializationDefaults.STORAGE_CONTEXT).bytes - } + PersistentKey(key, value) }, persistentEntityClass = PersistentKey::class.java ) diff --git a/node/src/main/kotlin/net/corda/node/services/network/PersistentNetworkMapService.kt b/node/src/main/kotlin/net/corda/node/services/network/PersistentNetworkMapService.kt index 6d3600d398..3d1f7ebd97 100644 --- a/node/src/main/kotlin/net/corda/node/services/network/PersistentNetworkMapService.kt +++ b/node/src/main/kotlin/net/corda/node/services/network/PersistentNetworkMapService.kt @@ -1,6 +1,6 @@ package net.corda.node.services.network -import net.corda.core.utilities.toBase58String +import net.corda.core.crypto.toStringShort import net.corda.core.identity.PartyAndCertificate import net.corda.core.internal.ThreadBox import net.corda.core.messaging.SingleMessageRecipient @@ -9,7 +9,9 @@ import net.corda.core.serialization.deserialize import net.corda.core.serialization.serialize import net.corda.node.services.api.NetworkMapCacheInternal import net.corda.node.services.messaging.MessagingService -import net.corda.node.utilities.* +import net.corda.node.utilities.MAX_HASH_HEX_SIZE +import net.corda.node.utilities.NODE_DATABASE_PREFIX +import net.corda.node.utilities.PersistentMap import net.corda.nodeapi.ArtemisMessagingComponent import java.io.ByteArrayInputStream import java.security.cert.CertificateFactory @@ -31,8 +33,9 @@ class PersistentNetworkMapService(network: MessagingService, networkMapCache: Ne @Entity @Table(name = "${NODE_DATABASE_PREFIX}network_map_nodes") class NetworkNode( - @Id @Column(name = "node_party_key") - var publicKey: String = "", + @Id + @Column(name = "node_party_key_hash", length = MAX_HASH_HEX_SIZE) + var publicKeyHash: String, @Column var nodeParty: NodeParty = NodeParty(), @@ -58,14 +61,14 @@ class PersistentNetworkMapService(network: MessagingService, networkMapCache: Ne fun createNetworkNodesMap(): PersistentMap { return PersistentMap( - toPersistentEntityKey = { it.owningKey.toBase58String() }, + toPersistentEntityKey = { it.owningKey.toStringShort() }, fromPersistentEntity = { Pair(PartyAndCertificate(factory.generateCertPath(ByteArrayInputStream(it.nodeParty.certPath))), it.registrationInfo.deserialize(context = SerializationDefaults.STORAGE_CONTEXT)) }, toPersistentEntity = { key: PartyAndCertificate, value: NodeRegistrationInfo -> NetworkNode( - publicKey = key.owningKey.toBase58String(), + publicKeyHash = key.owningKey.toStringShort(), nodeParty = NodeParty( key.name.toString(), key.certificate.encoded, diff --git a/node/src/main/kotlin/net/corda/node/utilities/CordaPersistence.kt b/node/src/main/kotlin/net/corda/node/utilities/CordaPersistence.kt index 77055c6073..a8c85d105d 100644 --- a/node/src/main/kotlin/net/corda/node/utilities/CordaPersistence.kt +++ b/node/src/main/kotlin/net/corda/node/utilities/CordaPersistence.kt @@ -20,6 +20,14 @@ import java.util.concurrent.CopyOnWriteArrayList */ const val NODE_DATABASE_PREFIX = "node_" +/** + * The maximum supported field-size for hash HEX-encoded outputs (e.g. database fields). + * This value is enough to support hash functions with outputs up to 512 bits (e.g. SHA3-512), in which + * case 128 HEX characters are required. + * 130 was selected instead of 128, to allow for 2 extra characters that will be used as hash-scheme identifiers. + */ +internal const val MAX_HASH_HEX_SIZE = 130 + //HikariDataSource implements Closeable which allows CordaPersistence to be Closeable class CordaPersistence(var dataSource: HikariDataSource, private val schemaService: SchemaService, private val createIdentityService: () -> IdentityService, databaseProperties: Properties) : Closeable {