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

Commit ff4b881

Browse files
committed
FAB-8861 Missing Blockinfo information
Change-Id: I700836c24260f88f444330209fa02ce9222064ab Signed-off-by: rickr <cr22rc@gmail.com>
1 parent 03c428e commit ff4b881

File tree

5 files changed

+155
-14
lines changed

5 files changed

+155
-14
lines changed

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,18 @@ public class TransactionEvent extends TransactionEnvelopeInfo {
109109
super(filteredTransaction);
110110
}
111111

112+
/**
113+
* The BlockEvent for this TransactionEvent.
114+
*
115+
* @return BlockEvent for this transaction.
116+
*/
117+
118+
public BlockEvent getBlockEvent() {
119+
120+
return BlockEvent.this;
121+
122+
}
123+
112124
/**
113125
* The event hub that received this event.
114126
*

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

Lines changed: 104 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import org.hyperledger.fabric.protos.common.Common;
2424
import org.hyperledger.fabric.protos.common.Common.Block;
2525
import org.hyperledger.fabric.protos.ledger.rwset.Rwset.TxReadWriteSet;
26+
import org.hyperledger.fabric.protos.msp.Identities;
2627
import org.hyperledger.fabric.protos.peer.Chaincode.ChaincodeInput;
2728
import org.hyperledger.fabric.protos.peer.FabricTransaction;
2829
import org.hyperledger.fabric.protos.peer.PeerEvents;
@@ -46,16 +47,6 @@ public class BlockInfo {
4647
this.block = new BlockDeserializer(block);
4748
}
4849

49-
// BlockInfo(PeerEvents.Event event) {
50-
// if (event.getEventCase() == PeerEvents.Event.EventCase.FILTERED_BLOCK) {
51-
// block = null;
52-
// filteredBlock = event.getFilteredBlock();
53-
// } else {
54-
// this.block = new BlockDeserializer(event.getBlock());
55-
// filteredBlock = null;
56-
// }
57-
// }
58-
5950
BlockInfo(PeerEvents.DeliverResponse resp) {
6051

6152
final PeerEvents.DeliverResponse.TypeCase type = resp.getTypeCase();
@@ -149,11 +140,20 @@ public int getEnvelopeCount() {
149140
return isFiltered() ? filteredBlock.getFilteredTransactionsCount() : block.getData().getDataCount();
150141
}
151142

143+
/**
144+
* Wrappers Envelope
145+
*/
146+
152147
public class EnvelopeInfo {
153148
private final EnvelopeDeserializer envelopeDeserializer;
154149
private final HeaderDeserializer headerDeserializer;
155150
protected final FilteredTransaction filteredTx;
156151

152+
/**
153+
* This block is filtered
154+
*
155+
* @return true if it's filtered.
156+
*/
157157
boolean isFiltered() {
158158
return filteredTx != null;
159159

@@ -167,18 +167,79 @@ boolean isFiltered() {
167167
filteredTx = null;
168168
}
169169

170-
public EnvelopeInfo(FilteredTransaction filteredTx) {
170+
EnvelopeInfo(FilteredTransaction filteredTx) {
171171
this.filteredTx = filteredTx;
172172
envelopeDeserializer = null;
173173
headerDeserializer = null;
174174

175175
}
176176

177+
/**
178+
* Get channel id
179+
*
180+
* @return The channel id also referred to as channel name.
181+
*/
177182
public String getChannelId() {
178183

179184
return BlockInfo.this.isFiltered() ? filteredBlock.getChannelId() : headerDeserializer.getChannelHeader().getChannelId();
180185
}
181186

187+
public class IdentitiesInfo {
188+
final String mspid;
189+
final String id;
190+
191+
/**
192+
* The identification of the identity usually the certificate.
193+
*
194+
* @return The certificate of the user in PEM format.
195+
*/
196+
public String getId() {
197+
return id;
198+
}
199+
200+
/**
201+
* The MSPId of the user.
202+
*
203+
* @return The MSPid of the user.
204+
*/
205+
public String getMspid() {
206+
return mspid;
207+
}
208+
209+
IdentitiesInfo(Identities.SerializedIdentity identity) {
210+
mspid = identity.getMspid();
211+
id = identity.getIdBytes().toStringUtf8();
212+
213+
}
214+
215+
}
216+
217+
/**
218+
* This is the creator or submitter of the transaction.
219+
* Returns null for a filtered block.
220+
*
221+
* @return {@link IdentitiesInfo}
222+
*/
223+
public IdentitiesInfo getCreator() {
224+
return isFiltered() ? null : new IdentitiesInfo(headerDeserializer.getCreator());
225+
226+
}
227+
228+
/**
229+
* The nonce of the transaction.
230+
*
231+
* @return return null for filtered block.
232+
*/
233+
public byte[] getNonce() {
234+
return isFiltered() ? null : headerDeserializer.getNonce();
235+
236+
}
237+
238+
/**
239+
* The transaction ID
240+
*
241+
* @return the transaction id.
242+
*/
182243
public String getTransactionID() {
183244

184245
return BlockInfo.this.isFiltered() ? filteredTx.getTxid() : headerDeserializer.getChannelHeader().getTxId();
@@ -317,6 +378,16 @@ public class TransactionEnvelopeInfo extends EnvelopeInfo {
317378
this.transactionDeserializer = null;
318379
}
319380

381+
/**
382+
* Signature for the transaction.
383+
*
384+
* @return byte array that as the signature.
385+
*/
386+
public byte[] getSignature() {
387+
388+
return transactionDeserializer.getSignature();
389+
}
390+
320391
TransactionEnvelopeInfo(EndorserTransactionEnvDeserializer transactionDeserializer) {
321392
super(transactionDeserializer);
322393

@@ -600,10 +671,32 @@ public byte[] getSignature() {
600671
return endorsement.getSignature().toByteArray();
601672
}
602673

674+
/**
675+
* @return
676+
* @deprecated use getId and getMspid
677+
*/
603678
public byte[] getEndorser() {
604679
return endorsement.getEndorser().toByteArray();
605680
}
606681

682+
public String getId() {
683+
684+
try {
685+
return Identities.SerializedIdentity.parseFrom(endorsement.getEndorser()).getIdBytes().toStringUtf8();
686+
} catch (InvalidProtocolBufferException e) {
687+
throw new InvalidProtocolBufferRuntimeException(e);
688+
}
689+
690+
}
691+
692+
public String getMspid() {
693+
try {
694+
return Identities.SerializedIdentity.parseFrom(endorsement.getEndorser()).getMspid();
695+
} catch (InvalidProtocolBufferException e) {
696+
throw new InvalidProtocolBufferRuntimeException(e);
697+
}
698+
}
699+
607700
}
608701

609702
public enum EnvelopeType {

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

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,12 +57,16 @@ Envelope getEnvelope() {
5757

5858
}
5959

60-
//Todo ret.getSignature();
61-
6260
return ret;
6361

6462
}
6563

64+
byte[] getSignature() {
65+
66+
return getEnvelope().getSignature().toByteArray();
67+
68+
}
69+
6670
PayloadDeserializer getPayload() {
6771

6872
PayloadDeserializer ret = null;

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

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,11 @@
1818

1919
import java.lang.ref.WeakReference;
2020

21+
import com.google.protobuf.InvalidProtocolBufferException;
22+
import org.hyperledger.fabric.protos.common.Common;
2123
import org.hyperledger.fabric.protos.common.Common.Header;
24+
import org.hyperledger.fabric.protos.msp.Identities;
25+
import org.hyperledger.fabric.sdk.exception.InvalidProtocolBufferRuntimeException;
2226

2327
class HeaderDeserializer {
2428

@@ -53,4 +57,25 @@ ChannelHeaderDeserializer getChannelHeader() {
5357

5458
}
5559

60+
Identities.SerializedIdentity getCreator() {
61+
62+
try {
63+
Common.SignatureHeader signatureHeader1 = Common.SignatureHeader.parseFrom(header.getSignatureHeader());
64+
return Identities.SerializedIdentity.parseFrom(signatureHeader1.getCreator());
65+
} catch (InvalidProtocolBufferException e) {
66+
throw new InvalidProtocolBufferRuntimeException(e);
67+
}
68+
69+
}
70+
71+
byte[] getNonce() {
72+
73+
try {
74+
Common.SignatureHeader signatureHeader1 = Common.SignatureHeader.parseFrom(header.getSignatureHeader());
75+
return signatureHeader1.getNonce().toByteArray();
76+
} catch (InvalidProtocolBufferException e) {
77+
throw new InvalidProtocolBufferRuntimeException(e);
78+
}
79+
80+
}
5681
}

src/test/java/org/hyperledger/fabric/sdkintegration/End2endIT.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -516,9 +516,14 @@ policy OR(Org1MSP.member, Org2MSP.member) meaning 1 signature from someone in ei
516516
waitOnFabric(0);
517517

518518
assertTrue(transactionEvent.isValid()); // must be valid to be here.
519+
assertNotNull(transactionEvent.getSignature()); //musth have a signature.
520+
BlockEvent blockEvent = transactionEvent.getBlockEvent(); // This is the blockevent that has this transaction.
521+
assertNotNull(blockEvent.getBlock()); // Make sure the RAW Fabric block is returned.
522+
519523
out("Finished instantiate transaction with transaction id %s", transactionEvent.getTransactionID());
520524

521525
try {
526+
assertEquals(blockEvent.getChannelId(), channel.getName());
522527
successful.clear();
523528
failed.clear();
524529

@@ -900,6 +905,8 @@ void blockWalker(HFClient client, Channel channel) throws InvalidArgumentExcepti
900905
out(" Transaction number %d has epoch: %d", i, envelopeInfo.getEpoch());
901906
out(" Transaction number %d has transaction timestamp: %tB %<te, %<tY %<tT %<Tp", i, envelopeInfo.getTimestamp());
902907
out(" Transaction number %d has type id: %s", i, "" + envelopeInfo.getType());
908+
out(" Transaction number %d has nonce : %s", i, "" + Hex.encodeHexString(envelopeInfo.getNonce()));
909+
out(" Transaction number %d has submitter mspid: %s, certificate: %s", i, envelopeInfo.getCreator().getMspid(), envelopeInfo.getCreator().getId());
903910

904911
if (envelopeInfo.getType() == TRANSACTION_ENVELOPE) {
905912
BlockInfo.TransactionEnvelopeInfo transactionEnvelopeInfo = (BlockInfo.TransactionEnvelopeInfo) envelopeInfo;
@@ -924,7 +931,7 @@ void blockWalker(HFClient client, Channel channel) throws InvalidArgumentExcepti
924931
for (int n = 0; n < transactionActionInfo.getEndorsementsCount(); ++n) {
925932
BlockInfo.EndorserInfo endorserInfo = transactionActionInfo.getEndorsementInfo(n);
926933
out("Endorser %d signature: %s", n, Hex.encodeHexString(endorserInfo.getSignature()));
927-
out("Endorser %d endorser: %s", n, new String(endorserInfo.getEndorser(), "UTF-8"));
934+
out("Endorser %d endorser: mspid %s \n certificate %s", n, endorserInfo.getMspid(), endorserInfo.getId());
928935
}
929936
out(" Transaction action %d has %d chaincode input arguments", j, transactionActionInfo.getChaincodeInputArgsCount());
930937
for (int z = 0; z < transactionActionInfo.getChaincodeInputArgsCount(); ++z) {

0 commit comments

Comments
 (0)