From 4837f4b0861ae5f3ec1fe9679f97347c6d830daa Mon Sep 17 00:00:00 2001 From: Nithin Pankaj Date: Thu, 18 Jan 2024 17:07:04 +0530 Subject: [PATCH] Remove breaking and untested dependencies and introduce backward compatiability for Chaincode Event publishing (#125) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * Remove breaking and untested dependencies and package options Signed-off-by: “Nithin * Publish Chaincode Events with backward compatiability and without breaking current downstream contract Signed-off-by: “Nithin --------- Signed-off-by: “Nithin Co-authored-by: “Nithin --- pom.xml | 94 +------------------ .../listener/ChaincodeEventListener.java | 37 +++++++- .../java/rest/client/sdk/StandardCCEvent.java | 8 +- .../client/service/EventPublishService.java | 4 +- .../service/impl/EventPublishServiceImpl.java | 21 +---- 5 files changed, 43 insertions(+), 121 deletions(-) diff --git a/pom.xml b/pom.xml index b53d147a..b59110f4 100644 --- a/pom.xml +++ b/pom.xml @@ -38,46 +38,6 @@ - - org.hyperledger.fabric-chaincode-java - fabric-chaincode-shim - ${fabric-chaincode-java.version} - compile - - - com.github.everit-org.json-schema - org.everit.json.schema - - - org.hyperledger.fabric-chaincode-java - fabric-chaincode-protos - - - javax.json - javax.json-api - - - - - org.hyperledger.fabric-chaincode-java - fabric-chaincode-protos - ${fabric-chaincode-java.version} - - - javax.annotation - javax.annotation-api - - - javax.json - javax.json-api - - - - - io.github.classgraph - classgraph - 4.8.139 - org.springframework.boot spring-boot-starter @@ -185,20 +145,12 @@ org.hyperledger.fabric-sdk-java fabric-sdk-java - 2.2.25 + 2.2.12 org.apache.logging.log4j log4j-core - - javax.annotation - javax.annotation-api - - - javax.xml.bind - jaxb-api - @@ -217,12 +169,6 @@ org.springframework.boot spring-boot-starter-test test - - - org.skyscreamer - jsonassert - - org.mockito @@ -256,11 +202,6 @@ micrometer-registry-prometheus runtime - - net.bytebuddy - byte-buddy - 1.14.11 - @@ -327,17 +268,6 @@ - - org.springframework.boot - spring-boot-maven-plugin - - - - repackage - - - - @@ -365,17 +295,6 @@ - - org.springframework.boot - spring-boot-maven-plugin - - - - repackage - - - - @@ -403,17 +322,6 @@ - - org.springframework.boot - spring-boot-maven-plugin - - - - repackage - - - - diff --git a/src/main/java/hlf/java/rest/client/listener/ChaincodeEventListener.java b/src/main/java/hlf/java/rest/client/listener/ChaincodeEventListener.java index f452111e..b275daaa 100644 --- a/src/main/java/hlf/java/rest/client/listener/ChaincodeEventListener.java +++ b/src/main/java/hlf/java/rest/client/listener/ChaincodeEventListener.java @@ -1,6 +1,9 @@ package hlf.java.rest.client.listener; +import com.fasterxml.jackson.core.JsonProcessingException; +import hlf.java.rest.client.config.FabricProperties; import hlf.java.rest.client.model.EventType; +import hlf.java.rest.client.sdk.StandardCCEvent; import hlf.java.rest.client.service.EventPublishService; import hlf.java.rest.client.util.FabricClientConstants; import hlf.java.rest.client.util.FabricEventParseUtil; @@ -24,6 +27,8 @@ public class ChaincodeEventListener { @Autowired(required = false) private EventPublishService eventPublishService; + @Autowired private FabricProperties fabricProperties; + private static String eventTxnId = FabricClientConstants.FABRIC_TRANSACTION_ID; public void chaincodeEventListener(ContractEvent contractEvent) { @@ -75,13 +80,41 @@ private void publishChaincodeEvent( return; } + String messageKey = String.valueOf(payload.hashCode()); + String payloadToPublish = payload; + + if (fabricProperties.getEvents().isStandardCCEventEnabled()) { + // Fetch the key information for chaincode events, only if the feature is enabled. + // Parse the payload and use the key. + try { + StandardCCEvent standardCCEvent = + FabricEventParseUtil.parseString(payload, StandardCCEvent.class); + messageKey = + StringUtils.isNotBlank(standardCCEvent.getKey()) + ? standardCCEvent.getKey() + : messageKey; + // Prefer the Raw Event Payload. + payloadToPublish = + StringUtils.isNotBlank(standardCCEvent.getEvent()) + ? standardCCEvent.getEvent() + : payloadToPublish; + } catch (JsonProcessingException e) { + // Likely thrown if the Event generated from Chaincode might not be wrapped in a model + // that matches 'StandardCCEvent' + // Instead of failing the op, fallback to the defaults and proceed with the publish. + log.error( + "Failed to deserialize Event payload to StandardCCEvent structure. Incoming Event Payload and Default Key will be utilised for publishing."); + } + } + eventPublishService.publishChaincodeEvents( FabricEventParseUtil.createEventStructure( - payload, "", txId, blockNumber, EventType.CHAINCODE_EVENT), + payloadToPublish, "", txId, blockNumber, EventType.CHAINCODE_EVENT), chaincodeId, txId, eventName, - channelName); + channelName, + messageKey); eventTxnId = txId; } else { log.debug("Duplicate Transaction; ID: {}", txId); diff --git a/src/main/java/hlf/java/rest/client/sdk/StandardCCEvent.java b/src/main/java/hlf/java/rest/client/sdk/StandardCCEvent.java index c2467539..e990a9de 100644 --- a/src/main/java/hlf/java/rest/client/sdk/StandardCCEvent.java +++ b/src/main/java/hlf/java/rest/client/sdk/StandardCCEvent.java @@ -1,24 +1,18 @@ package hlf.java.rest.client.sdk; -import java.io.Serializable; - import com.fasterxml.jackson.annotation.JsonProperty; +import java.io.Serializable; import lombok.Data; -import org.hyperledger.fabric.contract.annotation.DataType; -import org.hyperledger.fabric.contract.annotation.Property; /** * StandardCCEvent can be used by smart contract developers to send a commonly wrapped event that * the hlf-connector decodes. The decoded event can be used to publish to Kafka. */ @Data -@DataType public class StandardCCEvent implements Serializable { - @Property() @JsonProperty("key") private String key; - @Property @JsonProperty("event") private String event; } diff --git a/src/main/java/hlf/java/rest/client/service/EventPublishService.java b/src/main/java/hlf/java/rest/client/service/EventPublishService.java index ce94f863..554b8073 100644 --- a/src/main/java/hlf/java/rest/client/service/EventPublishService.java +++ b/src/main/java/hlf/java/rest/client/service/EventPublishService.java @@ -24,6 +24,7 @@ boolean sendMessage( * @param fabricTxId String Fabric transaction ID * @param eventName String chaincode event-name * @param channelName String Name of the channel where the event was generated. + * @param messageKey associated key for the payload. * @return status boolean status of msg sent */ boolean publishChaincodeEvents( @@ -31,7 +32,8 @@ boolean publishChaincodeEvents( String chaincodeName, String fabricTxId, String eventName, - String channelName); + String channelName, + String messageKey); /** * @param payload String message payload diff --git a/src/main/java/hlf/java/rest/client/service/impl/EventPublishServiceImpl.java b/src/main/java/hlf/java/rest/client/service/impl/EventPublishServiceImpl.java index 8e9d9424..a7f526f8 100644 --- a/src/main/java/hlf/java/rest/client/service/impl/EventPublishServiceImpl.java +++ b/src/main/java/hlf/java/rest/client/service/impl/EventPublishServiceImpl.java @@ -2,10 +2,8 @@ import hlf.java.rest.client.config.FabricProperties; import hlf.java.rest.client.config.KafkaProperties; -import hlf.java.rest.client.sdk.StandardCCEvent; import hlf.java.rest.client.service.EventPublishService; import hlf.java.rest.client.util.FabricClientConstants; -import hlf.java.rest.client.util.FabricEventParseUtil; import lombok.extern.slf4j.Slf4j; import org.apache.kafka.clients.producer.ProducerRecord; import org.apache.kafka.common.header.internals.RecordHeader; @@ -87,26 +85,13 @@ public boolean publishChaincodeEvents( String chaincodeName, String fabricTxId, String eventName, - String channelName) { + String channelName, + String messageKey) { boolean status = true; try { - String key = String.valueOf(payload.hashCode()); - String payloadToPublish = payload; - if (fabricProperties.getEvents().isStandardCCEventEnabled()) { - // Fetch the key information for chaincode events, - // but only if the feature is enabled. - - // Parse the payload and use the key. - StandardCCEvent standardCCEvent = - FabricEventParseUtil.parseString(payload, StandardCCEvent.class); - key = standardCCEvent.getKey(); - // Prefer the Raw Event Payload. - payloadToPublish = standardCCEvent.getEvent(); - } ProducerRecord producerRecord = - new ProducerRecord<>( - kafkaProperties.getEventListener().getTopic(), key, payloadToPublish); + new ProducerRecord<>(kafkaProperties.getEventListener().getTopic(), messageKey, payload); producerRecord .headers()