Skip to content

Commit

Permalink
FAB-4088 Determine which proposals are consistent
Browse files Browse the repository at this point in the history
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>
  • Loading branch information
cr22rc committed May 22, 2017
1 parent 10323e4 commit 457ac61
Show file tree
Hide file tree
Showing 24 changed files with 220 additions and 149 deletions.
8 changes: 4 additions & 4 deletions src/main/java/org/hyperledger/fabric/sdk/BlockEvent.java
Original file line number Diff line number Diff line change
Expand Up @@ -65,12 +65,12 @@ public Event getEvent() {

TransactionEvent getTransactionEvent(int index) throws InvalidProtocolBufferException {

return new TransactionEvent((TansactionEnvelopeInfo) getEnvelopeInfo(index), index);
return new TransactionEvent((TransactionEnvelopeInfo) getEnvelopeInfo(index), index);
}

public class TransactionEvent extends BlockInfo.TansactionEnvelopeInfo {
TransactionEvent(TansactionEnvelopeInfo tansactionEnvelopeInfo, int index) {
super(tansactionEnvelopeInfo.getTransactionDeserializer(), index);
public class TransactionEvent extends TransactionEnvelopeInfo {
TransactionEvent(TransactionEnvelopeInfo transactionEnvelopeInfo, int index) {
super(transactionEnvelopeInfo.getTransactionDeserializer(), index);
}

EventHub getEventHub() {
Expand Down
18 changes: 7 additions & 11 deletions src/main/java/org/hyperledger/fabric/sdk/BlockInfo.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ public class BlockInfo {
this.block = new BlockDeserializer(block);
}


public String getChannelId() throws InvalidProtocolBufferException {

return getEnvelopeInfo(0).getChannelId();
Expand Down Expand Up @@ -67,7 +66,7 @@ public byte[] getDataHash() {
}

/**
* @return the {@link Block} tansaction metadata value
* @return the {@link Block} transaction metadata value
*/
public byte[] getTransActionsMetaData() {

Expand Down Expand Up @@ -99,7 +98,7 @@ public int getEnvelopCount() {
// * @throws InvalidProtocolBufferException
// */

// public TansactionEnvelopeInfo getEnvelopeInfo(int index) throws InvalidProtocolBufferException {
// public TransactionEnvelopeInfo getEnvelopeInfo(int index) throws InvalidProtocolBufferException {
//
// try {
// // block.getData(0).getEnvelope().getSignature();
Expand All @@ -108,7 +107,7 @@ public int getEnvelopCount() {
//
// final PayloadDeserializer payload = block.getData(index).getPayload();
//
// return new TansactionEnvelopeInfo(null, payload.getHeader());
// return new TransactionEnvelopeInfo(null, payload.getHeader());
// } catch (InvalidProtocolBufferRuntimeException e) {
// throw (InvalidProtocolBufferException) e.getCause();
// }
Expand Down Expand Up @@ -161,10 +160,9 @@ public boolean isValid() {
* @return the validation code of this Transaction (enumeration TxValidationCode in Transaction.proto)
*/
public byte getValidationCode() {
return envelopeDeserializer.validationCode();
return envelopeDeserializer.validationCode();
}


public EnvelopeType getType() {

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

switch (ed.getType()) {
case 3:
ret = new TansactionEnvelopeInfo((EndorserTransactionEnvDeserializer) ed, blockIndex);
ret = new TransactionEnvelopeInfo((EndorserTransactionEnvDeserializer) ed, blockIndex);
break;
default: //just assume base properties.
ret = new EnvelopeInfo(ed, blockIndex);
Expand All @@ -210,17 +208,15 @@ public Iterable<EnvelopeInfo> getEnvelopeInfos() {
return new TransactionInfoIterable();
}

public class TansactionEnvelopeInfo extends EnvelopeInfo {
public class TransactionEnvelopeInfo extends EnvelopeInfo {

EndorserTransactionEnvDeserializer getTransactionDeserializer() {
return transactionDeserializer;
}

protected final EndorserTransactionEnvDeserializer transactionDeserializer;



public TansactionEnvelopeInfo(EndorserTransactionEnvDeserializer transactionDeserializer, int blockIndex) {
public TransactionEnvelopeInfo(EndorserTransactionEnvDeserializer transactionDeserializer, int blockIndex) {
super(transactionDeserializer, blockIndex);

this.transactionDeserializer = transactionDeserializer;
Expand Down
16 changes: 8 additions & 8 deletions src/main/java/org/hyperledger/fabric/sdk/Channel.java
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@
import org.hyperledger.fabric.sdk.exception.TransactionEventException;
import org.hyperledger.fabric.sdk.exception.TransactionException;
import org.hyperledger.fabric.sdk.helper.Config;
import org.hyperledger.fabric.sdk.helper.SDKUtil;
import org.hyperledger.fabric.sdk.helper.Utils;
import org.hyperledger.fabric.sdk.security.CryptoSuite;
import org.hyperledger.fabric.sdk.transaction.InstallProposalBuilder;
import org.hyperledger.fabric.sdk.transaction.InstantiateProposalBuilder;
Expand All @@ -107,8 +107,8 @@
import static org.hyperledger.fabric.protos.common.Configtx.ConfigValue;
import static org.hyperledger.fabric.protos.common.Policies.SignaturePolicy;
import static org.hyperledger.fabric.protos.common.Policies.SignaturePolicyEnvelope;
import static org.hyperledger.fabric.sdk.helper.SDKUtil.checkGrpcUrl;
import static org.hyperledger.fabric.sdk.helper.SDKUtil.toHexString;
import static org.hyperledger.fabric.sdk.helper.Utils.checkGrpcUrl;
import static org.hyperledger.fabric.sdk.helper.Utils.toHexString;
import static org.hyperledger.fabric.sdk.transaction.ProtoUtils.createChannelHeader;
import static org.hyperledger.fabric.sdk.transaction.ProtoUtils.getCurrentFabricTimestamp;
import static org.hyperledger.fabric.sdk.transaction.ProtoUtils.getSignatureHeaderAsByteString;
Expand Down Expand Up @@ -298,7 +298,7 @@ public boolean isInitialized() {
name = SYSTEM_CHANNEL_NAME;///It's special !
initialized = true;
} else {
if (SDKUtil.isNullOrEmpty(name)) {
if (Utils.isNullOrEmpty(name)) {
throw new InvalidArgumentException("Channel name is invalid can not be null or empty.");
}
}
Expand Down Expand Up @@ -355,7 +355,7 @@ public Channel addPeer(Peer peer) throws InvalidArgumentException {
if (null == peer) {
throw new InvalidArgumentException("Peer is invalid can not be null.");
}
if (SDKUtil.isNullOrEmpty(peer.getName())) {
if (Utils.isNullOrEmpty(peer.getName())) {
throw new InvalidArgumentException("Peer added to channel has no name.");
}

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

}
if (SDKUtil.isNullOrEmpty(name)) {
if (Utils.isNullOrEmpty(name)) {

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

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

SignatureHeader deliverSignatureHeader = SignatureHeader.newBuilder()
.setCreator(identity.toByteString())
.setNonce(ByteString.copyFrom(SDKUtil.generateNonce()))
.setNonce(ByteString.copyFrom(Utils.generateNonce()))
.build();

Header deliverHeader = Header.newBuilder()
Expand Down Expand Up @@ -2550,7 +2550,7 @@ public String getHandle() {

BL(BlockListener listener) {

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

this.listener = listener;
Expand Down
6 changes: 3 additions & 3 deletions src/main/java/org/hyperledger/fabric/sdk/Endpoint.java
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,10 @@
import org.bouncycastle.asn1.x500.style.BCStyle;
import org.bouncycastle.asn1.x500.style.IETFUtils;
import org.bouncycastle.cert.jcajce.JcaX509CertificateHolder;
import org.hyperledger.fabric.sdk.helper.SDKUtil;
import org.hyperledger.fabric.sdk.helper.Utils;
import org.hyperledger.fabric.sdk.security.CryptoPrimitives;

import static org.hyperledger.fabric.sdk.helper.SDKUtil.parseGrpcUrl;
import static org.hyperledger.fabric.sdk.helper.Utils.parseGrpcUrl;

class Endpoint {
private static final Log logger = LogFactory.getLog(Endpoint.class);
Expand Down Expand Up @@ -133,7 +133,7 @@ class Endpoint {
.usePlaintext(true);
addNettyBuilderProps(channelBuilder, properties);
} else if (protocol.equalsIgnoreCase("grpcs")) {
if (SDKUtil.isNullOrEmpty(pem)) {
if (Utils.isNullOrEmpty(pem)) {
// use root certificate
this.channelBuilder = ManagedChannelBuilder.forAddress(addr, port);
addNettyBuilderProps(channelBuilder, properties);
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/org/hyperledger/fabric/sdk/EventHub.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
import org.hyperledger.fabric.sdk.transaction.TransactionContext;

import static java.lang.String.format;
import static org.hyperledger.fabric.sdk.helper.SDKUtil.checkGrpcUrl;
import static org.hyperledger.fabric.sdk.helper.Utils.checkGrpcUrl;

/**
* Class to manage fabric events.
Expand Down
10 changes: 5 additions & 5 deletions src/main/java/org/hyperledger/fabric/sdk/HFClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
import org.hyperledger.fabric.sdk.exception.InvalidArgumentException;
import org.hyperledger.fabric.sdk.exception.ProposalException;
import org.hyperledger.fabric.sdk.exception.TransactionException;
import org.hyperledger.fabric.sdk.helper.SDKUtil;
import org.hyperledger.fabric.sdk.helper.Utils;
import org.hyperledger.fabric.sdk.security.CryptoSuite;

import static java.lang.String.format;
Expand Down Expand Up @@ -244,7 +244,7 @@ public void setUserContext(User userContext) throws InvalidArgumentException {
throw new InvalidArgumentException("setUserContext is null");
}
final String userName = userContext.getName();
if (SDKUtil.isNullOrEmpty(userName)) {
if (Utils.isNullOrEmpty(userName)) {
throw new InvalidArgumentException("setUserContext user's name is missing");
}

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

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

if (SDKUtil.isNullOrEmpty(userContext.getName())) {
if (Utils.isNullOrEmpty(userContext.getName())) {
throw new InvalidArgumentException("setUserContext user's name is missing");
}

if (SDKUtil.isNullOrEmpty(enrollment.getCert())) {
if (Utils.isNullOrEmpty(enrollment.getCert())) {
throw new InvalidArgumentException(format("setUserContext for user %s Enrollment missing user certificate.", userName));
}
if (null == enrollment.getKey()) {
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/org/hyperledger/fabric/sdk/Orderer.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
import org.hyperledger.fabric.sdk.exception.TransactionException;

import static java.lang.String.format;
import static org.hyperledger.fabric.sdk.helper.SDKUtil.checkGrpcUrl;
import static org.hyperledger.fabric.sdk.helper.Utils.checkGrpcUrl;

/**
* The Orderer class represents a orderer to which SDK sends deploy, invoke, or query requests.
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/org/hyperledger/fabric/sdk/Peer.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
import org.hyperledger.fabric.sdk.exception.PeerException;

import static java.lang.String.format;
import static org.hyperledger.fabric.sdk.helper.SDKUtil.checkGrpcUrl;
import static org.hyperledger.fabric.sdk.helper.Utils.checkGrpcUrl;

/**
* The Peer class represents a peer to which SDK sends deploy, or query proposals requests.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public class ProposalResponse extends ChaincodeResponse {

}

private ProposalResponsePayloadDeserializer getProposalResponsePayloadDeserializer() throws InvalidArgumentException {
public ProposalResponsePayloadDeserializer getProposalResponsePayloadDeserializer() throws InvalidArgumentException {
ProposalResponsePayloadDeserializer ret = null;

if (proposalResponsePayload != null) {
Expand Down
104 changes: 104 additions & 0 deletions src/main/java/org/hyperledger/fabric/sdk/SDKUtils.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
/*
*
* Copyright 2016,2017 DTCC, Fujitsu Australia Software Technology, IBM - All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* http://www.apache.org/licenses/LICENSE-2.0
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/

package org.hyperledger.fabric.sdk;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Set;

import com.google.protobuf.ByteString;
import org.bouncycastle.asn1.ASN1Integer;
import org.bouncycastle.asn1.DEROctetString;
import org.bouncycastle.asn1.DERSequenceGenerator;
import org.hyperledger.fabric.sdk.exception.InvalidArgumentException;
import org.hyperledger.fabric.sdk.security.CryptoSuite;

public class SDKUtils {

public static CryptoSuite suite = null;

/**
* used asn1 and get hash
*
* @param blockNumber
* @param previousHash
* @param dataHash
* @return byte[]
* @throws IOException
* @throws InvalidArgumentException
*/
public static byte[] calculateBlockHash(long blockNumber, byte[] previousHash, byte[] dataHash) throws IOException, InvalidArgumentException {

if (previousHash == null) {
throw new InvalidArgumentException("previousHash parameter is null.");
}
if (dataHash == null) {
throw new InvalidArgumentException("dataHash parameter is null.");
}

if (null == suite) {
suite = CryptoSuite.Factory.getCryptoSuite();
}

ByteArrayOutputStream s = new ByteArrayOutputStream();
DERSequenceGenerator seq = new DERSequenceGenerator(s);
seq.addObject(new ASN1Integer(blockNumber));
seq.addObject(new DEROctetString(previousHash));
seq.addObject(new DEROctetString(dataHash));
seq.close();
return suite.hash(s.toByteArray());

}

/**
* Check that the proposals all have consistent read write sets
*
* @param proposalResponses
* @return A Collection of sets where each set has consistent proposals.
* @throws InvalidArgumentException
*/

public static Collection<Set<ProposalResponse>> getProposalConsistencySets(Collection<ProposalResponse> proposalResponses) throws InvalidArgumentException {

if (proposalResponses == null) {
throw new InvalidArgumentException("proposalResponses collection is null");
}

if (proposalResponses.isEmpty()) {
throw new InvalidArgumentException("proposalResponses collection is empty");
}

HashMap<ByteString, Set<ProposalResponse>> ret = new HashMap<>();

for (ProposalResponse proposalResponse : proposalResponses) {

ByteString rwsetByteString = proposalResponse.getProposalResponsePayloadDeserializer()
.getExtension().getChaincodeAction().getResults();

Set<ProposalResponse> set = ret.computeIfAbsent(rwsetByteString, k -> new HashSet<>());

set.add(proposalResponse);

}

return ret.values();

}
}
Loading

0 comments on commit 457ac61

Please sign in to comment.