From b020e602ba5b032d2a22d90e155b88b863ee5fe1 Mon Sep 17 00:00:00 2001 From: Shams Asari Date: Fri, 7 Dec 2018 18:57:51 +0000 Subject: [PATCH] Fingerprint for Exception class reverted to V3 (fixes CORDA-2227 & CORDA-2292) (#4386) --- .../internal/amqp/CustomSerializer.kt | 12 ++++++-- .../model/TypeModellingFingerPrinter.kt | 2 +- .../SerializationCompatibilityTests.kt | 14 +++++++++ .../net/corda/blobinspector/BlobInspector.kt | 30 +++++++------------ 4 files changed, 35 insertions(+), 23 deletions(-) create mode 100644 serialization/src/test/kotlin/net/corda/serialization/internal/SerializationCompatibilityTests.kt diff --git a/serialization/src/main/kotlin/net/corda/serialization/internal/amqp/CustomSerializer.kt b/serialization/src/main/kotlin/net/corda/serialization/internal/amqp/CustomSerializer.kt index 8e44e3ab56..0be0de6c23 100644 --- a/serialization/src/main/kotlin/net/corda/serialization/internal/amqp/CustomSerializer.kt +++ b/serialization/src/main/kotlin/net/corda/serialization/internal/amqp/CustomSerializer.kt @@ -65,17 +65,25 @@ abstract class CustomSerializer : AMQPSerializer, SerializerFor { override val schemaForDocumentation = Schema(emptyList()) override fun isSerializerFor(clazz: Class<*>): Boolean = clazz == this.clazz + override val type: Type get() = clazz + override val typeDescriptor: Symbol by lazy { - Symbol.valueOf("$DESCRIPTOR_DOMAIN:${FingerprintWriter(false).write(arrayOf(superClassSerializer.typeDescriptor.toString(), AMQPTypeIdentifiers.nameForType(clazz)).joinToString()).fingerprint}") + val fingerprint = FingerprintWriter() + .write(superClassSerializer.typeDescriptor) + .write(AMQPTypeIdentifiers.nameForType(clazz)) + .fingerprint + Symbol.valueOf("$DESCRIPTOR_DOMAIN:$fingerprint") } + private val typeNotation: TypeNotation = RestrictedType( AMQPTypeIdentifiers.nameForType(clazz), null, emptyList(), AMQPTypeIdentifiers.nameForType(superClassSerializer.type), Descriptor(typeDescriptor), - emptyList()) + emptyList() + ) override fun writeClassInfo(output: SerializationOutput) { output.writeTypeNotations(typeNotation) diff --git a/serialization/src/main/kotlin/net/corda/serialization/internal/model/TypeModellingFingerPrinter.kt b/serialization/src/main/kotlin/net/corda/serialization/internal/model/TypeModellingFingerPrinter.kt index d9247a178a..c0db392d88 100644 --- a/serialization/src/main/kotlin/net/corda/serialization/internal/model/TypeModellingFingerPrinter.kt +++ b/serialization/src/main/kotlin/net/corda/serialization/internal/model/TypeModellingFingerPrinter.kt @@ -45,7 +45,7 @@ class TypeModellingFingerPrinter( * Wrapper for the [Hasher] we use to generate fingerprints, providing methods for writing various kinds of content * into the hash. */ -internal class FingerprintWriter(debugEnabled: Boolean) { +internal class FingerprintWriter(debugEnabled: Boolean = false) { companion object { private const val ARRAY_HASH: String = "Array = true" diff --git a/serialization/src/test/kotlin/net/corda/serialization/internal/SerializationCompatibilityTests.kt b/serialization/src/test/kotlin/net/corda/serialization/internal/SerializationCompatibilityTests.kt new file mode 100644 index 0000000000..af4fbeebaa --- /dev/null +++ b/serialization/src/test/kotlin/net/corda/serialization/internal/SerializationCompatibilityTests.kt @@ -0,0 +1,14 @@ +package net.corda.serialization.internal + +import net.corda.serialization.internal.amqp.custom.ThrowableSerializer +import net.corda.serialization.internal.amqp.testutils.testDefaultFactoryNoEvolution +import org.assertj.core.api.Assertions.assertThat +import org.junit.Test + +class SerializationCompatibilityTests { + @Test + fun `fingerprint is stable`() { + val factory = testDefaultFactoryNoEvolution().apply { register(ThrowableSerializer(this)) } + assertThat(factory.get(Exception::class.java).typeDescriptor.toString()).isEqualTo("net.corda:ApZ2a/36VVskaoDZMbiZ8A==") + } +} diff --git a/tools/blobinspector/src/main/kotlin/net/corda/blobinspector/BlobInspector.kt b/tools/blobinspector/src/main/kotlin/net/corda/blobinspector/BlobInspector.kt index 7cdedde753..d60cb31070 100644 --- a/tools/blobinspector/src/main/kotlin/net/corda/blobinspector/BlobInspector.kt +++ b/tools/blobinspector/src/main/kotlin/net/corda/blobinspector/BlobInspector.kt @@ -2,7 +2,6 @@ package net.corda.blobinspector import com.fasterxml.jackson.core.JsonFactory import com.fasterxml.jackson.dataformat.yaml.YAMLFactory -import com.jcabi.manifests.Manifests import net.corda.client.jackson.JacksonSupport import net.corda.cliutils.CordaCliWrapper import net.corda.cliutils.ExitCodes @@ -16,18 +15,18 @@ import net.corda.core.serialization.internal._contextSerializationEnv import net.corda.core.utilities.base64ToByteArray import net.corda.core.utilities.hexToByteArray import net.corda.core.utilities.sequence -import net.corda.serialization.internal.* +import net.corda.serialization.internal.AMQP_P2P_CONTEXT +import net.corda.serialization.internal.AMQP_STORAGE_CONTEXT +import net.corda.serialization.internal.CordaSerializationMagic +import net.corda.serialization.internal.SerializationFactoryImpl import net.corda.serialization.internal.amqp.AbstractAMQPSerializationScheme import net.corda.serialization.internal.amqp.DeserializationInput import net.corda.serialization.internal.amqp.amqpMagic -import org.slf4j.event.Level import picocli.CommandLine.* import java.io.PrintStream import java.net.MalformedURLException import java.net.URL import java.nio.file.Paths -import java.util.* -import kotlin.system.exitProcess fun main(args: Array) { BlobInspector().start(args) @@ -57,6 +56,8 @@ class BlobInspector : CordaCliWrapper("blob-inspector", "Convert AMQP serialised val bytes = parseToBinaryRelaxed(inputFormatType, inputBytes) ?: throw IllegalArgumentException("Error: this input does not appear to be encoded in Corda's AMQP extended format, sorry.") + initialiseSerialization() + if (schema) { val envelope = DeserializationInput.getEnvelope(bytes.sequence(), SerializationDefaults.STORAGE_CONTEXT.encodingWhitelist) out.println(envelope.schema) @@ -72,14 +73,13 @@ class BlobInspector : CordaCliWrapper("blob-inspector", "Convert AMQP serialised val mapper = JacksonSupport.createNonRpcMapper(factory, fullParties) - initialiseSerialization() - try { + return try { val deserialized = bytes.deserialize(context = SerializationDefaults.STORAGE_CONTEXT) out.println(deserialized.javaClass.name) mapper.writeValue(out, deserialized) - return ExitCodes.SUCCESS - } catch(e: Exception) { - return ExitCodes.FAILURE + ExitCodes.SUCCESS + } catch (e: Exception) { + ExitCodes.FAILURE } finally { _contextSerializationEnv.set(null) } @@ -148,15 +148,5 @@ private class SourceConverter : ITypeConverter { } } -private class CordaVersionProvider : IVersionProvider { - override fun getVersion(): Array { - return arrayOf( - "Version: ${Manifests.read("Corda-Release-Version")}", - "Revision: ${Manifests.read("Corda-Revision")}" - ) - } -} - private enum class OutputFormatType { YAML, JSON } private enum class InputFormatType { BINARY, HEX, BASE64 } -