From a99a91073051ff675204149b58aafd6e37fbd6d7 Mon Sep 17 00:00:00 2001 From: Shams Asari Date: Thu, 15 Mar 2018 20:55:40 +0000 Subject: [PATCH] Removed duplicate SignedNetworkParameters and SignedNetworkMap type aliases (#562) --- .../net/corda/bridge/internal/BridgeInstance.kt | 4 ++-- .../common/persistence/NetworkMapStorage.kt | 7 ++++--- .../persistence/PersistentNetworkMapStorage.kt | 4 ++-- .../common/persistence/entity/NetworkMapEntity.kt | 2 +- .../persistence/entity/NetworkParametersEntity.kt | 5 ++--- .../common/signer/NetworkMapSigner.kt | 4 ++-- .../r3/corda/networkmanage/common/utils/Utils.kt | 7 ------- .../doorman/webservice/NetworkMapWebService.kt | 14 +++++++------- .../common/signer/NetworkMapSignerTest.kt | 8 ++++---- .../doorman/webservice/NetworkMapWebServiceTest.kt | 4 ++-- 10 files changed, 26 insertions(+), 33 deletions(-) diff --git a/bridge/src/main/kotlin/net/corda/bridge/internal/BridgeInstance.kt b/bridge/src/main/kotlin/net/corda/bridge/internal/BridgeInstance.kt index 9523e04244..28c9df9eb7 100644 --- a/bridge/src/main/kotlin/net/corda/bridge/internal/BridgeInstance.kt +++ b/bridge/src/main/kotlin/net/corda/bridge/internal/BridgeInstance.kt @@ -8,7 +8,6 @@ import net.corda.bridge.services.supervisors.FloatSupervisorServiceImpl import net.corda.bridge.services.util.ServiceStateCombiner import net.corda.bridge.services.util.ServiceStateHelper import net.corda.core.concurrent.CordaFuture -import net.corda.core.internal.SignedDataWithCert import net.corda.core.internal.concurrent.openFuture import net.corda.core.internal.div import net.corda.core.internal.exists @@ -22,6 +21,7 @@ import net.corda.nodeapi.internal.ShutdownHook import net.corda.nodeapi.internal.addShutdownHook import net.corda.nodeapi.internal.crypto.X509Utilities import net.corda.nodeapi.internal.network.NETWORK_PARAMS_FILE_NAME +import net.corda.nodeapi.internal.network.SignedNetworkParameters import net.corda.nodeapi.internal.network.verifiedNetworkMapCert import net.corda.nodeapi.internal.serialization.AMQP_P2P_CONTEXT import net.corda.nodeapi.internal.serialization.AMQP_STORAGE_CONTEXT @@ -105,7 +105,7 @@ class BridgeInstance(val conf: BridgeConfiguration, val trustRoot = conf.loadTrustStore().getCertificate(X509Utilities.CORDA_ROOT_CA) val networkParamsFile = conf.baseDirectory / NETWORK_PARAMS_FILE_NAME require(networkParamsFile.exists()) { "No network-parameters file found." } - networkParameters = networkParamsFile.readObject>().verifiedNetworkMapCert(trustRoot) + networkParameters = networkParamsFile.readObject().verifiedNetworkMapCert(trustRoot) log.info("Loaded network parameters: $networkParameters") check(networkParameters.minimumPlatformVersion <= versionInfo.platformVersion) { "Node's platform version is lower than network's required minimumPlatformVersion" diff --git a/network-management/src/main/kotlin/com/r3/corda/networkmanage/common/persistence/NetworkMapStorage.kt b/network-management/src/main/kotlin/com/r3/corda/networkmanage/common/persistence/NetworkMapStorage.kt index 56546b0d99..b07d66748b 100644 --- a/network-management/src/main/kotlin/com/r3/corda/networkmanage/common/persistence/NetworkMapStorage.kt +++ b/network-management/src/main/kotlin/com/r3/corda/networkmanage/common/persistence/NetworkMapStorage.kt @@ -10,11 +10,12 @@ package com.r3.corda.networkmanage.common.persistence -import com.r3.corda.networkmanage.common.utils.SignedNetworkMap -import com.r3.corda.networkmanage.common.utils.SignedNetworkParameters +import com.r3.corda.networkmanage.common.persistence.entity.NodeInfoEntity import net.corda.core.crypto.SecureHash import net.corda.core.internal.DigitalSignatureWithCert import net.corda.core.node.NetworkParameters +import net.corda.nodeapi.internal.network.SignedNetworkMap +import net.corda.nodeapi.internal.network.SignedNetworkParameters /** * Data access object interface for NetworkMap persistence layer @@ -27,7 +28,7 @@ interface NetworkMapStorage { fun getCurrentNetworkMap(): SignedNetworkMap? /** - * Retrieves node info hashes where [isCurrent] is true and the certificate status is [CertificateStatus.VALID] + * Retrieves node info hashes where [NodeInfoEntity.isCurrent] is true and the certificate status is [CertificateStatus.VALID] * * @return list of current and valid node info hashes. */ diff --git a/network-management/src/main/kotlin/com/r3/corda/networkmanage/common/persistence/PersistentNetworkMapStorage.kt b/network-management/src/main/kotlin/com/r3/corda/networkmanage/common/persistence/PersistentNetworkMapStorage.kt index 4b9d8190d0..809da2d0d1 100644 --- a/network-management/src/main/kotlin/com/r3/corda/networkmanage/common/persistence/PersistentNetworkMapStorage.kt +++ b/network-management/src/main/kotlin/com/r3/corda/networkmanage/common/persistence/PersistentNetworkMapStorage.kt @@ -11,13 +11,13 @@ package com.r3.corda.networkmanage.common.persistence import com.r3.corda.networkmanage.common.persistence.entity.* -import com.r3.corda.networkmanage.common.utils.SignedNetworkMap -import com.r3.corda.networkmanage.common.utils.SignedNetworkParameters import net.corda.core.crypto.SecureHash import net.corda.core.crypto.sha256 import net.corda.core.internal.DigitalSignatureWithCert import net.corda.core.node.NetworkParameters import net.corda.core.serialization.serialize +import net.corda.nodeapi.internal.network.SignedNetworkMap +import net.corda.nodeapi.internal.network.SignedNetworkParameters import net.corda.nodeapi.internal.persistence.CordaPersistence /** diff --git a/network-management/src/main/kotlin/com/r3/corda/networkmanage/common/persistence/entity/NetworkMapEntity.kt b/network-management/src/main/kotlin/com/r3/corda/networkmanage/common/persistence/entity/NetworkMapEntity.kt index 2c07d8bc08..22634f5dba 100644 --- a/network-management/src/main/kotlin/com/r3/corda/networkmanage/common/persistence/entity/NetworkMapEntity.kt +++ b/network-management/src/main/kotlin/com/r3/corda/networkmanage/common/persistence/entity/NetworkMapEntity.kt @@ -10,12 +10,12 @@ package com.r3.corda.networkmanage.common.persistence.entity -import com.r3.corda.networkmanage.common.utils.SignedNetworkMap import net.corda.core.internal.DigitalSignatureWithCert import net.corda.core.serialization.SerializedBytes import net.corda.core.serialization.deserialize import net.corda.nodeapi.internal.crypto.X509CertificateFactory import net.corda.nodeapi.internal.network.NetworkMap +import net.corda.nodeapi.internal.network.SignedNetworkMap import javax.persistence.* @Entity diff --git a/network-management/src/main/kotlin/com/r3/corda/networkmanage/common/persistence/entity/NetworkParametersEntity.kt b/network-management/src/main/kotlin/com/r3/corda/networkmanage/common/persistence/entity/NetworkParametersEntity.kt index 59367f6504..826fd30051 100644 --- a/network-management/src/main/kotlin/com/r3/corda/networkmanage/common/persistence/entity/NetworkParametersEntity.kt +++ b/network-management/src/main/kotlin/com/r3/corda/networkmanage/common/persistence/entity/NetworkParametersEntity.kt @@ -10,13 +10,12 @@ package com.r3.corda.networkmanage.common.persistence.entity -import com.r3.corda.networkmanage.common.utils.SignedNetworkParameters import net.corda.core.internal.DigitalSignatureWithCert -import net.corda.core.internal.SignedDataWithCert import net.corda.core.node.NetworkParameters import net.corda.core.serialization.SerializedBytes import net.corda.core.serialization.deserialize import net.corda.nodeapi.internal.crypto.X509CertificateFactory +import net.corda.nodeapi.internal.network.SignedNetworkParameters import org.hibernate.annotations.CreationTimestamp import java.time.Instant import javax.persistence.* @@ -51,7 +50,7 @@ class NetworkParametersEntity( fun toSignedNetworkParameters(): SignedNetworkParameters { if (certificate == null || signature == null) throw IllegalStateException("Network parameters entity is not signed: $parametersHash") - return SignedDataWithCert( + return SignedNetworkParameters( SerializedBytes(parametersBytes), DigitalSignatureWithCert(X509CertificateFactory().generateCertificate(certificate.inputStream()), signature) ) diff --git a/network-management/src/main/kotlin/com/r3/corda/networkmanage/common/signer/NetworkMapSigner.kt b/network-management/src/main/kotlin/com/r3/corda/networkmanage/common/signer/NetworkMapSigner.kt index 54cf7f1bb4..bc8a3f25ae 100644 --- a/network-management/src/main/kotlin/com/r3/corda/networkmanage/common/signer/NetworkMapSigner.kt +++ b/network-management/src/main/kotlin/com/r3/corda/networkmanage/common/signer/NetworkMapSigner.kt @@ -11,11 +11,11 @@ package com.r3.corda.networkmanage.common.signer import com.r3.corda.networkmanage.common.persistence.NetworkMapStorage -import net.corda.core.internal.SignedDataWithCert import net.corda.core.node.NetworkParameters import net.corda.core.serialization.serialize import net.corda.core.utilities.contextLogger import net.corda.nodeapi.internal.network.NetworkMap +import net.corda.nodeapi.internal.network.SignedNetworkMap class NetworkMapSigner(private val networkMapStorage: NetworkMapStorage, private val signer: Signer) { private companion object { @@ -51,7 +51,7 @@ class NetworkMapSigner(private val networkMapStorage: NetworkMapStorage, private if (serialisedNetworkMap != currentSignedNetworkMap?.raw) { logger.info("Signing a new network map: $newNetworkMap") logger.debug("Creating a new signed network map: ${serialisedNetworkMap.hash}") - val newSignedNetworkMap = SignedDataWithCert(serialisedNetworkMap, signer.signBytes(serialisedNetworkMap.bytes)) + val newSignedNetworkMap = SignedNetworkMap(serialisedNetworkMap, signer.signBytes(serialisedNetworkMap.bytes)) networkMapStorage.saveNetworkMap(newSignedNetworkMap) logger.debug("Signed network map saved") } else { diff --git a/network-management/src/main/kotlin/com/r3/corda/networkmanage/common/utils/Utils.kt b/network-management/src/main/kotlin/com/r3/corda/networkmanage/common/utils/Utils.kt index 772ca955c5..8fb7e72c9b 100644 --- a/network-management/src/main/kotlin/com/r3/corda/networkmanage/common/utils/Utils.kt +++ b/network-management/src/main/kotlin/com/r3/corda/networkmanage/common/utils/Utils.kt @@ -17,14 +17,11 @@ import joptsimple.OptionParser import net.corda.core.CordaOID import net.corda.core.crypto.sha256 import net.corda.core.internal.CertRole -import net.corda.core.internal.SignedDataWithCert -import net.corda.core.node.NetworkParameters import net.corda.core.serialization.internal.SerializationEnvironmentImpl import net.corda.core.serialization.internal.nodeSerializationEnv import net.corda.nodeapi.internal.config.parseAs import net.corda.nodeapi.internal.crypto.X509CertificateFactory import net.corda.nodeapi.internal.crypto.X509KeyStore -import net.corda.nodeapi.internal.network.NetworkMap import net.corda.nodeapi.internal.serialization.AMQP_P2P_CONTEXT import net.corda.nodeapi.internal.serialization.SerializationFactoryImpl import net.corda.nodeapi.internal.serialization.amqp.AMQPClientSerializationScheme @@ -45,10 +42,6 @@ const val CORDA_NETWORK_MAP = "cordanetworkmap" val logger: Logger = LoggerFactory.getLogger("com.r3.corda.networkmanage.common.utils") -// TODO These should be defined in node-api -typealias SignedNetworkParameters = SignedDataWithCert -typealias SignedNetworkMap = SignedDataWithCert - data class CertPathAndKey(val certPath: List, val key: PrivateKey) { fun toKeyPair(): KeyPair = KeyPair(certPath[0].publicKey, key) } diff --git a/network-management/src/main/kotlin/com/r3/corda/networkmanage/doorman/webservice/NetworkMapWebService.kt b/network-management/src/main/kotlin/com/r3/corda/networkmanage/doorman/webservice/NetworkMapWebService.kt index 18afb91cd5..4f31b85374 100644 --- a/network-management/src/main/kotlin/com/r3/corda/networkmanage/doorman/webservice/NetworkMapWebService.kt +++ b/network-management/src/main/kotlin/com/r3/corda/networkmanage/doorman/webservice/NetworkMapWebService.kt @@ -14,7 +14,6 @@ import com.github.benmanes.caffeine.cache.Caffeine import com.github.benmanes.caffeine.cache.LoadingCache import com.r3.corda.networkmanage.common.persistence.NetworkMapStorage import com.r3.corda.networkmanage.common.persistence.NodeInfoStorage -import com.r3.corda.networkmanage.common.utils.SignedNetworkMap import com.r3.corda.networkmanage.doorman.NetworkMapConfig import com.r3.corda.networkmanage.doorman.webservice.NetworkMapWebService.Companion.NETWORK_MAP_PATH import net.corda.core.crypto.SecureHash @@ -27,6 +26,7 @@ import net.corda.core.utilities.debug import net.corda.core.utilities.trace import net.corda.nodeapi.internal.NodeInfoAndSigned import net.corda.nodeapi.internal.SignedNodeInfo +import net.corda.nodeapi.internal.network.SignedNetworkMap import java.io.InputStream import java.security.InvalidKeyException import java.security.SignatureException @@ -52,18 +52,18 @@ class NetworkMapWebService(private val nodeInfoStorage: NodeInfoStorage, private val networkMapCache: LoadingCache = Caffeine.newBuilder() .expireAfterWrite(config.cacheTimeout, TimeUnit.MILLISECONDS) - .build({ _ -> + .build { _ -> networkMapStorage.getCurrentNetworkMap()?.let { val networkMap = it.verified() - CachedData(it, networkMap.nodeInfoHashes.toSet(), networkMapStorage.getSignedNetworkParameters(networkMap.networkParameterHash)?.verified()) } - }) + val networkParameters = networkMapStorage.getSignedNetworkParameters(networkMap.networkParameterHash)?.verified() + CachedData(it, networkMap.nodeInfoHashes.toSet(), networkParameters) + } + } private val nodeInfoCache: LoadingCache = Caffeine.newBuilder() // TODO: Define cache retention policy. .softValues() - .build({ key -> - key?.let { nodeInfoStorage.getNodeInfo(it) } - }) + .build(nodeInfoStorage::getNodeInfo) private val currentSignedNetworkMap: SignedNetworkMap? get() = networkMapCache.get(true)?.signedNetworkMap private val currentNodeInfoHashes: Set get() = networkMapCache.get(true)?.nodeInfoHashes ?: emptySet() diff --git a/network-management/src/test/kotlin/com/r3/corda/networkmanage/common/signer/NetworkMapSignerTest.kt b/network-management/src/test/kotlin/com/r3/corda/networkmanage/common/signer/NetworkMapSignerTest.kt index 83de879cea..ae37435475 100644 --- a/network-management/src/test/kotlin/com/r3/corda/networkmanage/common/signer/NetworkMapSignerTest.kt +++ b/network-management/src/test/kotlin/com/r3/corda/networkmanage/common/signer/NetworkMapSignerTest.kt @@ -13,17 +13,17 @@ package com.r3.corda.networkmanage.common.signer import com.nhaarman.mockito_kotlin.* import com.r3.corda.networkmanage.TestBase import com.r3.corda.networkmanage.common.persistence.NetworkMapStorage -import com.r3.corda.networkmanage.common.utils.SignedNetworkMap import net.corda.core.crypto.Crypto import net.corda.core.crypto.SecureHash import net.corda.core.crypto.sha256 import net.corda.core.internal.DigitalSignatureWithCert -import net.corda.core.internal.SignedDataWithCert import net.corda.core.internal.signWithCert import net.corda.core.serialization.serialize import net.corda.nodeapi.internal.createDevNetworkMapCa import net.corda.nodeapi.internal.crypto.CertificateAndKeyPair import net.corda.nodeapi.internal.network.NetworkMap +import net.corda.nodeapi.internal.network.SignedNetworkMap +import net.corda.nodeapi.internal.network.SignedNetworkParameters import net.corda.nodeapi.internal.network.verifiedNetworkMapCert import net.corda.testing.common.internal.testNetworkParameters import net.corda.testing.internal.createDevIntermediateCaCertPath @@ -68,7 +68,7 @@ class NetworkMapSignerTest : TestBase() { } whenever(signer.signObject(latestNetworkParameters)).then { val serialised = latestNetworkParameters.serialize() - SignedDataWithCert(serialised, signer.signBytes(serialised.bytes)) + SignedNetworkParameters(serialised, signer.signBytes(serialised.bytes)) } // when @@ -120,7 +120,7 @@ class NetworkMapSignerTest : TestBase() { } whenever(signer.signObject(networkParameters)).then { val serialised = networkParameters.serialize() - SignedDataWithCert(serialised, signer.signBytes(serialised.bytes)) + SignedNetworkParameters(serialised, signer.signBytes(serialised.bytes)) } // when networkMapSigner.signNetworkMap() diff --git a/network-management/src/test/kotlin/com/r3/corda/networkmanage/doorman/webservice/NetworkMapWebServiceTest.kt b/network-management/src/test/kotlin/com/r3/corda/networkmanage/doorman/webservice/NetworkMapWebServiceTest.kt index c0266db9a6..86ec8a0d3a 100644 --- a/network-management/src/test/kotlin/com/r3/corda/networkmanage/doorman/webservice/NetworkMapWebServiceTest.kt +++ b/network-management/src/test/kotlin/com/r3/corda/networkmanage/doorman/webservice/NetworkMapWebServiceTest.kt @@ -16,8 +16,6 @@ import com.nhaarman.mockito_kotlin.times import com.nhaarman.mockito_kotlin.verify import com.r3.corda.networkmanage.common.persistence.NetworkMapStorage import com.r3.corda.networkmanage.common.persistence.NodeInfoStorage -import com.r3.corda.networkmanage.common.utils.SignedNetworkMap -import com.r3.corda.networkmanage.common.utils.SignedNetworkParameters import com.r3.corda.networkmanage.doorman.NetworkManagementWebServer import com.r3.corda.networkmanage.doorman.NetworkMapConfig import net.corda.core.crypto.SecureHash.Companion.randomSHA256 @@ -33,6 +31,8 @@ import net.corda.nodeapi.internal.SignedNodeInfo import net.corda.nodeapi.internal.createDevNetworkMapCa import net.corda.nodeapi.internal.crypto.CertificateAndKeyPair import net.corda.nodeapi.internal.network.NetworkMap +import net.corda.nodeapi.internal.network.SignedNetworkMap +import net.corda.nodeapi.internal.network.SignedNetworkParameters import net.corda.nodeapi.internal.network.verifiedNetworkMapCert import net.corda.testing.common.internal.testNetworkParameters import net.corda.testing.core.SerializationEnvironmentRule