Skip to content

Commit

Permalink
FAB-6601 Use DiagnosticFileDumper clean up trace
Browse files Browse the repository at this point in the history
Change-Id: I0fd33f67cb141562cd20900e59c5458f5818626e
Signed-off-by: rickr <cr22rc@gmail.com>
  • Loading branch information
cr22rc committed Oct 27, 2017
1 parent 83223f9 commit 97ca7c6
Show file tree
Hide file tree
Showing 5 changed files with 75 additions and 20 deletions.
22 changes: 18 additions & 4 deletions src/main/java/org/hyperledger/fabric/sdk/ProposalResponse.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,17 @@
import org.hyperledger.fabric.sdk.exception.InvalidArgumentException;
import org.hyperledger.fabric.sdk.exception.ProposalException;
import org.hyperledger.fabric.sdk.helper.Config;
import org.hyperledger.fabric.sdk.helper.DiagnosticFileDumper;
import org.hyperledger.fabric.sdk.security.CryptoSuite;

public class ProposalResponse extends ChaincodeResponse {

private static final Log logger = LogFactory.getLog(ProposalResponse.class);
private static final Config config = Config.getConfig();
private static final boolean IS_TRACE_LEVEL = logger.isTraceEnabled();

private static final DiagnosticFileDumper diagnosticFileDumper = IS_TRACE_LEVEL
? config.getDiagnosticFileDumper() : null;

private boolean isVerified = false;

Expand Down Expand Up @@ -105,10 +110,19 @@ public boolean verify(CryptoSuite crypto) {

if (config.extraLogLevel(10)) {

logger.trace("payload TransactionBuilderbytes in hex: " + DatatypeConverter.printHexBinary(proposalResponse.getPayload().toByteArray()));
logger.trace("endorser bytes in hex: "
+ DatatypeConverter.printHexBinary(endorsement.getEndorser().toByteArray()));
logger.trace("plainText bytes in hex: " + DatatypeConverter.printHexBinary(plainText.toByteArray()));
if (null != diagnosticFileDumper) {
StringBuilder sb = new StringBuilder(10000);
sb.append("payload TransactionBuilderbytes in hex: " + DatatypeConverter.printHexBinary(proposalResponse.getPayload().toByteArray()));
sb.append("\n");
sb.append("endorser bytes in hex: "
+ DatatypeConverter.printHexBinary(endorsement.getEndorser().toByteArray()));
sb.append("\n");
sb.append("plainText bytes in hex: " + DatatypeConverter.printHexBinary(plainText.toByteArray()));

logger.trace("payload TransactionBuilderbytes: " +
diagnosticFileDumper.createDiagnosticFile(sb.toString()));
}

}

this.isVerified = crypto.verify(endorser.getIdBytes().toByteArray(), config.getSignatureAlgorithm(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ public class Config {
* Logging settings
**/
public static final String MAX_LOG_STRING_LENGTH = "org.hyperledger.fabric.sdk.log.stringlengthmax";
public static final String EXTRALOGLEVEL = "org.hyperledger.fabric.sdk.log.extraloglevel";
public static final String EXTRALOGLEVEL = "org.hyperledger.fabric.sdk.log.extraloglevel"; // ORG_HYPERLEDGER_FABRIC_SDK_LOG_EXTRALOGLEVEL
public static final String LOGGERLEVEL = "org.hyperledger.fabric.sdk.loglevel"; // ORG_HYPERLEDGER_FABRIC_SDK_LOGLEVEL=TRACE,DEBUG
public static final String DIAGNOTISTIC_FILE_DIRECTORY = "org.hyperledger.fabric.sdk.diagnosticFileDir"; //ORG_HYPERLEDGER_FABRIC_SDK_DIAGNOSTICFILEDIR

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import java.nio.ByteBuffer;
import java.nio.channels.AsynchronousFileChannel;
import java.nio.channels.CompletionHandler;
import java.nio.charset.StandardCharsets;
import java.nio.file.Paths;
import java.nio.file.StandardOpenOption;
import java.text.SimpleDateFormat;
Expand All @@ -33,7 +34,6 @@

/**
* Dumps files for diagnostic purposes
*
*/

public class DiagnosticFileDumper implements Runnable {
Expand Down Expand Up @@ -87,6 +87,12 @@ public String createDiagnosticFile(byte[] bytes) {

}

public String createDiagnosticFile(String msg) {

return createDiagnosticFile(msg.getBytes(StandardCharsets.UTF_8), null, null);

}

public String createDiagnosticFile(byte[] bytes, String prefix, String ext) {
String fileName = "";
if (cantWrite()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,12 +85,18 @@
import org.hyperledger.fabric.sdk.exception.CryptoException;
import org.hyperledger.fabric.sdk.exception.InvalidArgumentException;
import org.hyperledger.fabric.sdk.helper.Config;
import org.hyperledger.fabric.sdk.helper.DiagnosticFileDumper;

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

public class CryptoPrimitives implements CryptoSuite {
private final Config config = Config.getConfig();
private static final Log logger = LogFactory.getLog(CryptoPrimitives.class);
private static final Config config = Config.getConfig();
private static final boolean IS_TRACE_LEVEL = logger.isTraceEnabled();

private static final DiagnosticFileDumper diagnosticFileDumper = IS_TRACE_LEVEL
? config.getDiagnosticFileDumper() : null;

private String curveName;
private CertificateFactory cf;
Expand All @@ -112,8 +118,6 @@ public class CryptoPrimitives implements CryptoSuite {
// private String SYMMETRIC_ALGORITHM = "AES/CFB/NoPadding";
// private int MAC_KEY_BYTE_COUNT = 32;

private static final Log logger = LogFactory.getLog(CryptoPrimitives.class);

public CryptoPrimitives() throws ClassNotFoundException, IllegalAccessException, InstantiationException {
String securityProviderClassName = config.getSecurityProviderClassName();

Expand Down Expand Up @@ -263,11 +267,16 @@ public boolean verify(byte[] pemCertificate, String signatureAlgorithm, byte[] s
}

if (config.extraLogLevel(10)) {

logger.trace("plaintext in hex: " + DatatypeConverter.printHexBinary(plainText));
logger.trace("signature in hex: " + DatatypeConverter.printHexBinary(signature));
logger.trace("PEM cert in hex: " + DatatypeConverter.printHexBinary(pemCertificate));

if (null != diagnosticFileDumper) {
StringBuilder sb = new StringBuilder(10000);
sb.append("plaintext in hex: " + DatatypeConverter.printHexBinary(plainText));
sb.append("\n");
sb.append("signature in hex: " + DatatypeConverter.printHexBinary(signature));
sb.append("\n");
sb.append("PEM cert in hex: " + DatatypeConverter.printHexBinary(pemCertificate));
logger.trace("verify : " +
diagnosticFileDumper.createDiagnosticFile(sb.toString()));
}
}

try {
Expand Down Expand Up @@ -421,7 +430,10 @@ void addCACertificateToTrustStore(Certificate caCert, String alias) throws Inval

try {
if (config.extraLogLevel(10)) {
logger.trace("Adding cert to trust store. alias: " + alias + "cert: " + caCert.toString());
if (null != diagnosticFileDumper) {
logger.trace("Adding cert to trust store. alias: " + diagnosticFileDumper.createDiagnosticFile(alias + "cert: " + caCert.toString()));
}

}
getTrustStore().setCertificateEntry(alias, caCert);
} catch (KeyStoreException e) {
Expand Down Expand Up @@ -455,12 +467,20 @@ public void loadCACertificatesAsBytes(Collection<byte[]> certificatesBytes) thro
if (certificatesBytes == null || certificatesBytes.size() == 0) {
throw new CryptoException("List of CA certificates is empty. Nothing to load.");
}
StringBuilder sb = new StringBuilder(1000);
ArrayList<Certificate> certList = new ArrayList<>();
for (byte[] certBytes : certificatesBytes) {
logger.trace("certificate to load:\n" + new String(certBytes));
if (null != diagnosticFileDumper) {
sb.append("certificate to load:\n" + new String(certBytes));

}
certList.add(bytesToCertificate(certBytes));
}
loadCACertificates(certList);
if (diagnosticFileDumper != null && sb.length() > 1) {
logger.trace("loaded certificates: " + diagnosticFileDumper.createDiagnosticFile(sb.toString()));

}
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,16 @@
import org.hyperledger.fabric.protos.peer.FabricProposalResponse;
import org.hyperledger.fabric.protos.peer.FabricTransaction;
import org.hyperledger.fabric.sdk.helper.Config;
import org.hyperledger.fabric.sdk.helper.DiagnosticFileDumper;

public class TransactionBuilder {

private static final Log logger = LogFactory.getLog(TransactionBuilder.class);
Config config = Config.getConfig();
private static final Config config = Config.getConfig();
private static final boolean IS_TRACE_LEVEL = logger.isTraceEnabled();

private static final DiagnosticFileDumper diagnosticFileDumper = IS_TRACE_LEVEL
? config.getDiagnosticFileDumper() : null;
private FabricProposal.Proposal chaincodeProposal;
private Collection<FabricProposalResponse.Endorsement> endorsements;
private ByteString proposalResponsePayload;
Expand Down Expand Up @@ -83,15 +88,25 @@ private Common.Payload createTransactionCommonPayload(FabricProposal.Proposal ch
Common.Header header = Common.Header.parseFrom(chaincodeProposal.getHeader());

if (config.extraLogLevel(10)) {
logger.trace("transaction header bytes:" + Arrays.toString(header.toByteArray()));
logger.trace("transaction header sig bytes:" + Arrays.toString(header.getSignatureHeader().toByteArray()));

if (null != diagnosticFileDumper) {
StringBuilder sb = new StringBuilder(10000);
sb.append("transaction header bytes:" + Arrays.toString(header.toByteArray()));
sb.append("\n");
sb.append("transaction header sig bytes:" + Arrays.toString(header.getSignatureHeader().toByteArray()));
logger.trace("transaction header: " +
diagnosticFileDumper.createDiagnosticFile(sb.toString()));
}
}

transactionActionBuilder.setHeader(header.getSignatureHeader());

FabricTransaction.ChaincodeActionPayload chaincodeActionPayload = chaincodeActionPayloadBuilder.build();
if (config.extraLogLevel(10)) {
logger.trace("transactionActionBuilder.setPayload" + Arrays.toString(chaincodeActionPayload.toByteString().toByteArray()));
if (null != diagnosticFileDumper) {
logger.trace("transactionActionBuilder.setPayload: " +
diagnosticFileDumper.createDiagnosticFile(Arrays.toString(chaincodeActionPayload.toByteString().toByteArray())));
}
}
transactionActionBuilder.setPayload(chaincodeActionPayload.toByteString());

Expand Down

0 comments on commit 97ca7c6

Please sign in to comment.