Skip to content

Commit

Permalink
Remove breaking and untested dependencies and introduce backward comp…
Browse files Browse the repository at this point in the history
…atiability for Chaincode Event publishing (#125)

* Remove breaking and untested dependencies and package options

Signed-off-by: “Nithin <[email protected]>

* Publish Chaincode Events with backward compatiability and without breaking current downstream contract

Signed-off-by: “Nithin <[email protected]>

---------

Signed-off-by: “Nithin <[email protected]>
Co-authored-by: “Nithin <[email protected]>
  • Loading branch information
nithin-pankaj and “Nithin authored Jan 18, 2024
1 parent 0761876 commit 4837f4b
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 121 deletions.
94 changes: 1 addition & 93 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -38,46 +38,6 @@
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>org.hyperledger.fabric-chaincode-java</groupId>
<artifactId>fabric-chaincode-shim</artifactId>
<version>${fabric-chaincode-java.version}</version>
<scope>compile</scope>
<exclusions>
<exclusion>
<groupId>com.github.everit-org.json-schema</groupId>
<artifactId>org.everit.json.schema</artifactId>
</exclusion>
<exclusion>
<groupId>org.hyperledger.fabric-chaincode-java</groupId>
<artifactId>fabric-chaincode-protos</artifactId>
</exclusion>
<exclusion>
<groupId>javax.json</groupId>
<artifactId>javax.json-api</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.hyperledger.fabric-chaincode-java</groupId>
<artifactId>fabric-chaincode-protos</artifactId>
<version>${fabric-chaincode-java.version}</version>
<exclusions>
<exclusion>
<groupId>javax.annotation</groupId>
<artifactId>javax.annotation-api</artifactId>
</exclusion>
<exclusion>
<groupId>javax.json</groupId>
<artifactId>javax.json-api</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>io.github.classgraph</groupId>
<artifactId>classgraph</artifactId>
<version>4.8.139</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
Expand Down Expand Up @@ -185,20 +145,12 @@
<dependency>
<groupId>org.hyperledger.fabric-sdk-java</groupId>
<artifactId>fabric-sdk-java</artifactId>
<version>2.2.25</version>
<version>2.2.12</version>
<exclusions>
<exclusion>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
</exclusion>
<exclusion>
<groupId>javax.annotation</groupId>
<artifactId>javax.annotation-api</artifactId>
</exclusion>
<exclusion>
<groupId>javax.xml.bind</groupId>
<artifactId>jaxb-api</artifactId>
</exclusion>
</exclusions>
</dependency>
<!-- Logging -->
Expand All @@ -217,12 +169,6 @@
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.skyscreamer</groupId>
<artifactId>jsonassert</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
Expand Down Expand Up @@ -256,11 +202,6 @@
<artifactId>micrometer-registry-prometheus</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>net.bytebuddy</groupId>
<artifactId>byte-buddy</artifactId>
<version>1.14.11</version>
</dependency>
</dependencies>

<distributionManagement>
Expand Down Expand Up @@ -327,17 +268,6 @@
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</profile>
Expand Down Expand Up @@ -365,17 +295,6 @@
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</profile>
Expand Down Expand Up @@ -403,17 +322,6 @@
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</profile>
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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) {
Expand Down Expand Up @@ -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);
Expand Down
8 changes: 1 addition & 7 deletions src/main/java/hlf/java/rest/client/sdk/StandardCCEvent.java
Original file line number Diff line number Diff line change
@@ -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;
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,16 @@ 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(
final String payload,
String chaincodeName,
String fabricTxId,
String eventName,
String channelName);
String channelName,
String messageKey);

/**
* @param payload String message payload
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<String, String> producerRecord =
new ProducerRecord<>(
kafkaProperties.getEventListener().getTopic(), key, payloadToPublish);
new ProducerRecord<>(kafkaProperties.getEventListener().getTopic(), messageKey, payload);

producerRecord
.headers()
Expand Down

0 comments on commit 4837f4b

Please sign in to comment.