Skip to content
This repository was archived by the owner on Apr 22, 2025. It is now read-only.

Commit 7011ca4

Browse files
committed
FAB-5387 Listener for custom chaincode events.
Change-Id: I3082678b840d4149dce6a6b772f2cb1ba23091a9 Signed-off-by: rickr <cr22rc@gmail.com>
1 parent 6ac15e6 commit 7011ca4

File tree

10 files changed

+501
-116
lines changed

10 files changed

+501
-116
lines changed

src/main/java/org/hyperledger/fabric/sdk/BlockEvent.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,12 @@ public EventHub getEventHub() {
4242
private final EventHub eventHub;
4343
private final Event event;
4444

45+
/**
46+
* Raw proto buff event.
47+
*
48+
* @return Return raw protobuf event.
49+
*/
50+
4551
public Event getEvent() {
4652
return event;
4753
}

src/main/java/org/hyperledger/fabric/sdk/BlockInfo.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -321,6 +321,13 @@ public int getProposalResponseStatus() {
321321

322322
}
323323

324+
/**
325+
* Get read write set for this transaction. Will return null on for Eventhub events.
326+
* For eventhub events find the block by block number to get read write set if needed.
327+
*
328+
* @return Read write set.
329+
*/
330+
324331
public TxReadWriteSetInfo getTxReadWriteSet() {
325332

326333
TxReadWriteSet txReadWriteSet = transactionAction.getPayload().getAction().getProposalResponsePayload()
@@ -333,6 +340,19 @@ public TxReadWriteSetInfo getTxReadWriteSet() {
333340

334341
}
335342

343+
/**
344+
* Get chaincode events for this transaction.
345+
*
346+
* @return A chaincode event if the chaincode set an event otherwise null.
347+
*/
348+
349+
public ChaincodeEvent getEvent() {
350+
351+
return transactionAction.getPayload().getAction().getProposalResponsePayload()
352+
.getExtension().getEvent();
353+
354+
}
355+
336356
}
337357

338358
public TransactionActionInfo getTransactionActionInfo(int index) {

src/main/java/org/hyperledger/fabric/sdk/ChaincodeActionDeserializer.java

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,11 +52,19 @@ ChaincodeAction getChaincodeAction() {
5252

5353
}
5454

55-
//TODO events ?
55+
return ret;
5656

57-
// ret.getResponse();
57+
}
5858

59-
return ret;
59+
ChaincodeEvent getEvent() {
60+
61+
ChaincodeAction ca = getChaincodeAction();
62+
ByteString eventsBytes = ca.getEvents();
63+
if (eventsBytes == null || eventsBytes.isEmpty()) {
64+
return null;
65+
}
66+
67+
return new ChaincodeEvent(eventsBytes);
6068

6169
}
6270

Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
/*
2+
*
3+
* Copyright 2016,2017 DTCC, Fujitsu Australia Software Technology, IBM - All Rights Reserved.
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
* Unless required by applicable law or agreed to in writing, software
10+
* distributed under the License is distributed on an "AS IS" BASIS,
11+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
* See the License for the specific language governing permissions and
13+
* limitations under the License.
14+
*
15+
*/
16+
17+
package org.hyperledger.fabric.sdk;
18+
19+
import java.lang.ref.WeakReference;
20+
21+
import com.google.protobuf.ByteString;
22+
import com.google.protobuf.InvalidProtocolBufferException;
23+
import org.hyperledger.fabric.protos.peer.ChaincodeEventOuterClass;
24+
import org.hyperledger.fabric.sdk.exception.InvalidProtocolBufferRuntimeException;
25+
26+
/**
27+
* Encapsulates a Chaincode event.
28+
*/
29+
public class ChaincodeEvent {
30+
private final ByteString byteString;
31+
private WeakReference<ChaincodeEventOuterClass.ChaincodeEvent> chaincodeEvent;
32+
33+
ChaincodeEvent(ByteString byteString) {
34+
this.byteString = byteString;
35+
}
36+
37+
ChaincodeEventOuterClass.ChaincodeEvent getChaincodeEvent() {
38+
ChaincodeEventOuterClass.ChaincodeEvent ret = null;
39+
40+
if (chaincodeEvent != null) {
41+
ret = chaincodeEvent.get();
42+
43+
}
44+
if (ret == null) {
45+
46+
try {
47+
ret = ChaincodeEventOuterClass.ChaincodeEvent.parseFrom(byteString);
48+
49+
} catch (InvalidProtocolBufferException e) {
50+
throw new InvalidProtocolBufferRuntimeException(e);
51+
}
52+
53+
chaincodeEvent = new WeakReference<>(ret);
54+
55+
}
56+
57+
return ret;
58+
59+
}
60+
61+
/**
62+
* Get Chaincode event's name;
63+
*
64+
* @return Return name;
65+
*/
66+
public String getEventName() {
67+
68+
return getChaincodeEvent().getEventName();
69+
70+
}
71+
72+
/**
73+
* Get Chaincode identifier.
74+
*
75+
* @return The identifier
76+
*/
77+
public String getChaincodeId() {
78+
79+
return getChaincodeEvent().getChaincodeId();
80+
81+
}
82+
83+
/**
84+
* Get transaction id associated with this event.
85+
*
86+
* @return The transactions id.
87+
*/
88+
public String getTxId() {
89+
90+
return getChaincodeEvent().getTxId();
91+
92+
}
93+
94+
/**
95+
* Binary data associated with this event.
96+
*
97+
* @return binary data set by the chaincode for this event. This may return null.
98+
*/
99+
public byte[] getPayload() {
100+
101+
ByteString ret = getChaincodeEvent().getPayload();
102+
if (null == ret) {
103+
return null;
104+
}
105+
106+
return ret.toByteArray();
107+
108+
}
109+
110+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/*
2+
*
3+
* Copyright 2016,2017 DTCC, Fujitsu Australia Software Technology, IBM - All Rights Reserved.
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
* Unless required by applicable law or agreed to in writing, software
10+
* distributed under the License is distributed on an "AS IS" BASIS,
11+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
* See the License for the specific language governing permissions and
13+
* limitations under the License.
14+
*
15+
*/
16+
17+
package org.hyperledger.fabric.sdk;
18+
19+
/**
20+
* ChaincodeEventListener implemented by classes needing to receive chaincode events.
21+
*/
22+
public interface ChaincodeEventListener {
23+
/**
24+
* Receiving a chaincode event. ChaincodeEventListener should not be long lived as they can take up thread resources.
25+
*
26+
* @param handle The handle of the chaincode event listener that produced this event.
27+
* @param blockEvent The block event information that contained the chaincode event. See {@link BlockEvent}
28+
* @param chaincodeEvent The chaincode event. see {@link ChaincodeEvent}
29+
*/
30+
void received(String handle, BlockEvent blockEvent, ChaincodeEvent chaincodeEvent);
31+
}

0 commit comments

Comments
 (0)