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

Commit 457ac61

Browse files
committed
FAB-4088 Determine which proposals are consistent
Utility to determine which proposals are consistent Move ChainUtils and rename to SDKUtils so it with other public classes. Rename SDKUtils to just Utils for internal usage. Fix typo tansaction to transaction Change-Id: I0d39dc2a89dc4c57ffee035d0ec3a73b8c32b78d Signed-off-by: rickr <cr22rc@gmail.com>
1 parent 10323e4 commit 457ac61

24 files changed

+220
-149
lines changed

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -65,12 +65,12 @@ public Event getEvent() {
6565

6666
TransactionEvent getTransactionEvent(int index) throws InvalidProtocolBufferException {
6767

68-
return new TransactionEvent((TansactionEnvelopeInfo) getEnvelopeInfo(index), index);
68+
return new TransactionEvent((TransactionEnvelopeInfo) getEnvelopeInfo(index), index);
6969
}
7070

71-
public class TransactionEvent extends BlockInfo.TansactionEnvelopeInfo {
72-
TransactionEvent(TansactionEnvelopeInfo tansactionEnvelopeInfo, int index) {
73-
super(tansactionEnvelopeInfo.getTransactionDeserializer(), index);
71+
public class TransactionEvent extends TransactionEnvelopeInfo {
72+
TransactionEvent(TransactionEnvelopeInfo transactionEnvelopeInfo, int index) {
73+
super(transactionEnvelopeInfo.getTransactionDeserializer(), index);
7474
}
7575

7676
EventHub getEventHub() {

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

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,6 @@ public class BlockInfo {
3838
this.block = new BlockDeserializer(block);
3939
}
4040

41-
4241
public String getChannelId() throws InvalidProtocolBufferException {
4342

4443
return getEnvelopeInfo(0).getChannelId();
@@ -67,7 +66,7 @@ public byte[] getDataHash() {
6766
}
6867

6968
/**
70-
* @return the {@link Block} tansaction metadata value
69+
* @return the {@link Block} transaction metadata value
7170
*/
7271
public byte[] getTransActionsMetaData() {
7372

@@ -99,7 +98,7 @@ public int getEnvelopCount() {
9998
// * @throws InvalidProtocolBufferException
10099
// */
101100

102-
// public TansactionEnvelopeInfo getEnvelopeInfo(int index) throws InvalidProtocolBufferException {
101+
// public TransactionEnvelopeInfo getEnvelopeInfo(int index) throws InvalidProtocolBufferException {
103102
//
104103
// try {
105104
// // block.getData(0).getEnvelope().getSignature();
@@ -108,7 +107,7 @@ public int getEnvelopCount() {
108107
//
109108
// final PayloadDeserializer payload = block.getData(index).getPayload();
110109
//
111-
// return new TansactionEnvelopeInfo(null, payload.getHeader());
110+
// return new TransactionEnvelopeInfo(null, payload.getHeader());
112111
// } catch (InvalidProtocolBufferRuntimeException e) {
113112
// throw (InvalidProtocolBufferException) e.getCause();
114113
// }
@@ -161,10 +160,9 @@ public boolean isValid() {
161160
* @return the validation code of this Transaction (enumeration TxValidationCode in Transaction.proto)
162161
*/
163162
public byte getValidationCode() {
164-
return envelopeDeserializer.validationCode();
163+
return envelopeDeserializer.validationCode();
165164
}
166165

167-
168166
public EnvelopeType getType() {
169167

170168
switch (headerDeserializer.getChannelHeader().getType()) {
@@ -190,7 +188,7 @@ public EnvelopeInfo getEnvelopeInfo(int blockIndex) throws InvalidProtocolBuffer
190188

191189
switch (ed.getType()) {
192190
case 3:
193-
ret = new TansactionEnvelopeInfo((EndorserTransactionEnvDeserializer) ed, blockIndex);
191+
ret = new TransactionEnvelopeInfo((EndorserTransactionEnvDeserializer) ed, blockIndex);
194192
break;
195193
default: //just assume base properties.
196194
ret = new EnvelopeInfo(ed, blockIndex);
@@ -210,17 +208,15 @@ public Iterable<EnvelopeInfo> getEnvelopeInfos() {
210208
return new TransactionInfoIterable();
211209
}
212210

213-
public class TansactionEnvelopeInfo extends EnvelopeInfo {
211+
public class TransactionEnvelopeInfo extends EnvelopeInfo {
214212

215213
EndorserTransactionEnvDeserializer getTransactionDeserializer() {
216214
return transactionDeserializer;
217215
}
218216

219217
protected final EndorserTransactionEnvDeserializer transactionDeserializer;
220218

221-
222-
223-
public TansactionEnvelopeInfo(EndorserTransactionEnvDeserializer transactionDeserializer, int blockIndex) {
219+
public TransactionEnvelopeInfo(EndorserTransactionEnvDeserializer transactionDeserializer, int blockIndex) {
224220
super(transactionDeserializer, blockIndex);
225221

226222
this.transactionDeserializer = transactionDeserializer;

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

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@
8686
import org.hyperledger.fabric.sdk.exception.TransactionEventException;
8787
import org.hyperledger.fabric.sdk.exception.TransactionException;
8888
import org.hyperledger.fabric.sdk.helper.Config;
89-
import org.hyperledger.fabric.sdk.helper.SDKUtil;
89+
import org.hyperledger.fabric.sdk.helper.Utils;
9090
import org.hyperledger.fabric.sdk.security.CryptoSuite;
9191
import org.hyperledger.fabric.sdk.transaction.InstallProposalBuilder;
9292
import org.hyperledger.fabric.sdk.transaction.InstantiateProposalBuilder;
@@ -107,8 +107,8 @@
107107
import static org.hyperledger.fabric.protos.common.Configtx.ConfigValue;
108108
import static org.hyperledger.fabric.protos.common.Policies.SignaturePolicy;
109109
import static org.hyperledger.fabric.protos.common.Policies.SignaturePolicyEnvelope;
110-
import static org.hyperledger.fabric.sdk.helper.SDKUtil.checkGrpcUrl;
111-
import static org.hyperledger.fabric.sdk.helper.SDKUtil.toHexString;
110+
import static org.hyperledger.fabric.sdk.helper.Utils.checkGrpcUrl;
111+
import static org.hyperledger.fabric.sdk.helper.Utils.toHexString;
112112
import static org.hyperledger.fabric.sdk.transaction.ProtoUtils.createChannelHeader;
113113
import static org.hyperledger.fabric.sdk.transaction.ProtoUtils.getCurrentFabricTimestamp;
114114
import static org.hyperledger.fabric.sdk.transaction.ProtoUtils.getSignatureHeaderAsByteString;
@@ -298,7 +298,7 @@ public boolean isInitialized() {
298298
name = SYSTEM_CHANNEL_NAME;///It's special !
299299
initialized = true;
300300
} else {
301-
if (SDKUtil.isNullOrEmpty(name)) {
301+
if (Utils.isNullOrEmpty(name)) {
302302
throw new InvalidArgumentException("Channel name is invalid can not be null or empty.");
303303
}
304304
}
@@ -355,7 +355,7 @@ public Channel addPeer(Peer peer) throws InvalidArgumentException {
355355
if (null == peer) {
356356
throw new InvalidArgumentException("Peer is invalid can not be null.");
357357
}
358-
if (SDKUtil.isNullOrEmpty(peer.getName())) {
358+
if (Utils.isNullOrEmpty(peer.getName())) {
359359
throw new InvalidArgumentException("Peer added to channel has no name.");
360360
}
361361

@@ -586,7 +586,7 @@ public Channel initialize() throws InvalidArgumentException, TransactionExceptio
586586
throw new InvalidArgumentException("Channel needs at least one peer.");
587587

588588
}
589-
if (SDKUtil.isNullOrEmpty(name)) {
589+
if (Utils.isNullOrEmpty(name)) {
590590

591591
throw new InvalidArgumentException("Can not initialize Channel without a valid name.");
592592

@@ -710,7 +710,7 @@ private Block getGenesisBlock(Orderer order) throws TransactionException {
710710

711711
SignatureHeader deliverSignatureHeader = SignatureHeader.newBuilder()
712712
.setCreator(identity.toByteString())
713-
.setNonce(ByteString.copyFrom(SDKUtil.generateNonce()))
713+
.setNonce(ByteString.copyFrom(Utils.generateNonce()))
714714
.build();
715715

716716
Header deliverHeader = Header.newBuilder()
@@ -2550,7 +2550,7 @@ public String getHandle() {
25502550

25512551
BL(BlockListener listener) {
25522552

2553-
handle = SDKUtil.generateUUID();
2553+
handle = Utils.generateUUID();
25542554
logger.debug(format("Channel %s blockListener %s starting", name, handle));
25552555

25562556
this.listener = listener;

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,10 +45,10 @@
4545
import org.bouncycastle.asn1.x500.style.BCStyle;
4646
import org.bouncycastle.asn1.x500.style.IETFUtils;
4747
import org.bouncycastle.cert.jcajce.JcaX509CertificateHolder;
48-
import org.hyperledger.fabric.sdk.helper.SDKUtil;
48+
import org.hyperledger.fabric.sdk.helper.Utils;
4949
import org.hyperledger.fabric.sdk.security.CryptoPrimitives;
5050

51-
import static org.hyperledger.fabric.sdk.helper.SDKUtil.parseGrpcUrl;
51+
import static org.hyperledger.fabric.sdk.helper.Utils.parseGrpcUrl;
5252

5353
class Endpoint {
5454
private static final Log logger = LogFactory.getLog(Endpoint.class);
@@ -133,7 +133,7 @@ class Endpoint {
133133
.usePlaintext(true);
134134
addNettyBuilderProps(channelBuilder, properties);
135135
} else if (protocol.equalsIgnoreCase("grpcs")) {
136-
if (SDKUtil.isNullOrEmpty(pem)) {
136+
if (Utils.isNullOrEmpty(pem)) {
137137
// use root certificate
138138
this.channelBuilder = ManagedChannelBuilder.forAddress(addr, port);
139139
addNettyBuilderProps(channelBuilder, properties);

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@
3535
import org.hyperledger.fabric.sdk.transaction.TransactionContext;
3636

3737
import static java.lang.String.format;
38-
import static org.hyperledger.fabric.sdk.helper.SDKUtil.checkGrpcUrl;
38+
import static org.hyperledger.fabric.sdk.helper.Utils.checkGrpcUrl;
3939

4040
/**
4141
* Class to manage fabric events.

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
import org.hyperledger.fabric.sdk.exception.InvalidArgumentException;
3333
import org.hyperledger.fabric.sdk.exception.ProposalException;
3434
import org.hyperledger.fabric.sdk.exception.TransactionException;
35-
import org.hyperledger.fabric.sdk.helper.SDKUtil;
35+
import org.hyperledger.fabric.sdk.helper.Utils;
3636
import org.hyperledger.fabric.sdk.security.CryptoSuite;
3737

3838
import static java.lang.String.format;
@@ -244,7 +244,7 @@ public void setUserContext(User userContext) throws InvalidArgumentException {
244244
throw new InvalidArgumentException("setUserContext is null");
245245
}
246246
final String userName = userContext.getName();
247-
if (SDKUtil.isNullOrEmpty(userName)) {
247+
if (Utils.isNullOrEmpty(userName)) {
248248
throw new InvalidArgumentException("setUserContext user's name is missing");
249249
}
250250

@@ -253,15 +253,15 @@ public void setUserContext(User userContext) throws InvalidArgumentException {
253253
throw new InvalidArgumentException(format("setUserContext for user %s has no Enrollment set", userName));
254254
}
255255

256-
if (SDKUtil.isNullOrEmpty(userContext.getMSPID())) {
256+
if (Utils.isNullOrEmpty(userContext.getMSPID())) {
257257
throw new InvalidArgumentException(format("setUserContext for user %s has user's MSPID is missing", userName));
258258
}
259259

260-
if (SDKUtil.isNullOrEmpty(userContext.getName())) {
260+
if (Utils.isNullOrEmpty(userContext.getName())) {
261261
throw new InvalidArgumentException("setUserContext user's name is missing");
262262
}
263263

264-
if (SDKUtil.isNullOrEmpty(enrollment.getCert())) {
264+
if (Utils.isNullOrEmpty(enrollment.getCert())) {
265265
throw new InvalidArgumentException(format("setUserContext for user %s Enrollment missing user certificate.", userName));
266266
}
267267
if (null == enrollment.getKey()) {

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
import org.hyperledger.fabric.sdk.exception.TransactionException;
2727

2828
import static java.lang.String.format;
29-
import static org.hyperledger.fabric.sdk.helper.SDKUtil.checkGrpcUrl;
29+
import static org.hyperledger.fabric.sdk.helper.Utils.checkGrpcUrl;
3030

3131
/**
3232
* The Orderer class represents a orderer to which SDK sends deploy, invoke, or query requests.

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
import org.hyperledger.fabric.sdk.exception.PeerException;
2828

2929
import static java.lang.String.format;
30-
import static org.hyperledger.fabric.sdk.helper.SDKUtil.checkGrpcUrl;
30+
import static org.hyperledger.fabric.sdk.helper.Utils.checkGrpcUrl;
3131

3232
/**
3333
* The Peer class represents a peer to which SDK sends deploy, or query proposals requests.

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ public class ProposalResponse extends ChaincodeResponse {
4040

4141
}
4242

43-
private ProposalResponsePayloadDeserializer getProposalResponsePayloadDeserializer() throws InvalidArgumentException {
43+
public ProposalResponsePayloadDeserializer getProposalResponsePayloadDeserializer() throws InvalidArgumentException {
4444
ProposalResponsePayloadDeserializer ret = null;
4545

4646
if (proposalResponsePayload != null) {
Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
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.io.ByteArrayOutputStream;
20+
import java.io.IOException;
21+
import java.util.Collection;
22+
import java.util.HashMap;
23+
import java.util.HashSet;
24+
import java.util.Set;
25+
26+
import com.google.protobuf.ByteString;
27+
import org.bouncycastle.asn1.ASN1Integer;
28+
import org.bouncycastle.asn1.DEROctetString;
29+
import org.bouncycastle.asn1.DERSequenceGenerator;
30+
import org.hyperledger.fabric.sdk.exception.InvalidArgumentException;
31+
import org.hyperledger.fabric.sdk.security.CryptoSuite;
32+
33+
public class SDKUtils {
34+
35+
public static CryptoSuite suite = null;
36+
37+
/**
38+
* used asn1 and get hash
39+
*
40+
* @param blockNumber
41+
* @param previousHash
42+
* @param dataHash
43+
* @return byte[]
44+
* @throws IOException
45+
* @throws InvalidArgumentException
46+
*/
47+
public static byte[] calculateBlockHash(long blockNumber, byte[] previousHash, byte[] dataHash) throws IOException, InvalidArgumentException {
48+
49+
if (previousHash == null) {
50+
throw new InvalidArgumentException("previousHash parameter is null.");
51+
}
52+
if (dataHash == null) {
53+
throw new InvalidArgumentException("dataHash parameter is null.");
54+
}
55+
56+
if (null == suite) {
57+
suite = CryptoSuite.Factory.getCryptoSuite();
58+
}
59+
60+
ByteArrayOutputStream s = new ByteArrayOutputStream();
61+
DERSequenceGenerator seq = new DERSequenceGenerator(s);
62+
seq.addObject(new ASN1Integer(blockNumber));
63+
seq.addObject(new DEROctetString(previousHash));
64+
seq.addObject(new DEROctetString(dataHash));
65+
seq.close();
66+
return suite.hash(s.toByteArray());
67+
68+
}
69+
70+
/**
71+
* Check that the proposals all have consistent read write sets
72+
*
73+
* @param proposalResponses
74+
* @return A Collection of sets where each set has consistent proposals.
75+
* @throws InvalidArgumentException
76+
*/
77+
78+
public static Collection<Set<ProposalResponse>> getProposalConsistencySets(Collection<ProposalResponse> proposalResponses) throws InvalidArgumentException {
79+
80+
if (proposalResponses == null) {
81+
throw new InvalidArgumentException("proposalResponses collection is null");
82+
}
83+
84+
if (proposalResponses.isEmpty()) {
85+
throw new InvalidArgumentException("proposalResponses collection is empty");
86+
}
87+
88+
HashMap<ByteString, Set<ProposalResponse>> ret = new HashMap<>();
89+
90+
for (ProposalResponse proposalResponse : proposalResponses) {
91+
92+
ByteString rwsetByteString = proposalResponse.getProposalResponsePayloadDeserializer()
93+
.getExtension().getChaincodeAction().getResults();
94+
95+
Set<ProposalResponse> set = ret.computeIfAbsent(rwsetByteString, k -> new HashSet<>());
96+
97+
set.add(proposalResponse);
98+
99+
}
100+
101+
return ret.values();
102+
103+
}
104+
}

0 commit comments

Comments
 (0)