Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SLF4J support #52

Draft
wants to merge 6 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ Java 1.7+
```
git clone https://github.com/opentimestamps/java-opentimestamps
cd java-opentimestamps
mvn install
mvn install -P cli
```

#### SSL errors
Expand Down
27 changes: 25 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<groupId>com.eternitywall</groupId>
<artifactId>java-opentimestamps</artifactId>
<version>1.19</version>
<version>1.19.1-SNAPSHOT</version>

<name>java-opentimestamps</name>
<description>The java implementation of the OpenTimestamps protocol</description>
Expand Down Expand Up @@ -79,9 +79,15 @@
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-simple</artifactId>
<artifactId>slf4j-api</artifactId>
<version>1.7.29</version>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>1.2.3</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.glassfish.jaxb</groupId>
<artifactId>jaxb-runtime</artifactId>
Expand Down Expand Up @@ -198,6 +204,23 @@
</build>

<profiles>
<profile>
<id>cli</id>
<build>
<resources>
<resource>
<directory>${project.basedir}/src/cli/resources</directory>
</resource>
</resources>
</build>
<dependencies>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>1.2.3</version>
</dependency>
</dependencies>
</profile>
<profile>
<id>release-sign-artifacts</id>
<activation>
Expand Down
11 changes: 11 additions & 0 deletions src/cli/resources/logback.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<configuration>
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>%msg%n</pattern>
</encoder>
</appender>

<root level="info">
<appender-ref ref="STDOUT"/>
</root>
</configuration>
9 changes: 4 additions & 5 deletions src/main/java/com/eternitywall/http/Request.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.eternitywall.http;

import com.eternitywall.ots.Utils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.DataOutputStream;
import java.io.InputStream;
Expand All @@ -11,15 +12,13 @@
import java.util.Map;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.Callable;
import java.util.logging.Logger;
import java.util.zip.GZIPInputStream;
import java.util.zip.ZipException;

/**
* For making an HTTP request.
*/
public class Request implements Callable<Response> {
private static Logger log = Utils.getLogger(Request.class.getName());
private static final Logger log = LoggerFactory.getLogger(Request.class);

private URL url;
private byte[] data;
Expand Down Expand Up @@ -89,7 +88,7 @@ public Response call() throws Exception {
}
response.setStream(is);
} catch (Exception e) {
log.warning(url.toString() + " exception " + e);
log.warn("{} exception {}", url, e);
} finally {
if (queue != null) {
queue.offer(response);
Expand Down
3 changes: 0 additions & 3 deletions src/main/java/com/eternitywall/ots/Calendar.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
import java.net.URL;
import java.util.HashMap;
import java.util.Map;
import java.util.logging.Logger;

/**
* Class representing remote calendar server interface.
Expand All @@ -20,8 +19,6 @@ public class Calendar {
private String url;
private ECKey key;

private static Logger log = Utils.getLogger(Calendar.class.getName());

/**
* Create a RemoteCalendar.
*
Expand Down
7 changes: 4 additions & 3 deletions src/main/java/com/eternitywall/ots/Esplora.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,15 @@
import com.eternitywall.http.Request;
import com.eternitywall.http.Response;
import org.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.net.URL;
import java.util.concurrent.*;
import java.util.logging.Logger;

public class Esplora {

private static final Logger log = Utils.getLogger(Esplora.class.getName());
private static final Logger log = LoggerFactory.getLogger(Esplora.class);
private static final String esploraUrl = "https://blockstream.info/api";

/**
Expand All @@ -37,7 +38,7 @@ public static BlockHeader block(final String hash) throws Exception {
blockHeader.setMerkleroot(merkleroot);
blockHeader.setTime(time);
blockHeader.setBlockHash(hash);
log.info(take.getFromUrl() + " " + blockHeader);
log.debug("{} {}", take.getFromUrl(), blockHeader);
return blockHeader;
//log.warning("Cannot parse merkleroot from body: " + jsonObject + ": " + e.getMessage());
}
Expand Down
22 changes: 11 additions & 11 deletions src/main/java/com/eternitywall/ots/OpenTimestamps.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
import org.bitcoinj.core.DumpedPrivateKey;
import org.bitcoinj.core.ECKey;
import org.bitcoinj.core.NetworkParameters;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;
import java.math.BigInteger;
Expand All @@ -27,7 +29,6 @@
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.logging.Logger;

/**
* The main class for timestamp operations.
Expand All @@ -36,7 +37,7 @@
*/
public class OpenTimestamps {

private static Logger log = Utils.getLogger(OpenTimestamps.class.getName());
private static final Logger log = LoggerFactory.getLogger(OpenTimestamps.class);

/**
* Show information on a detached timestamp.
Expand Down Expand Up @@ -164,7 +165,7 @@ public static Timestamp stamp(List<DetachedTimestampFile> fileTimestamps, List<S
}

if (m < 0 || m > calendarsUrl.size() + privateCalendarsUrl.size()) {
log.severe("m cannot be greater than available calendar neither less or equal 0");
log.warn("m must be greater than 0 and cannot be greater than the number of available calender urls");
throw new IOException();
}

Expand Down Expand Up @@ -221,7 +222,7 @@ private static Timestamp create(Timestamp timestamp, List<String> calendarUrls,
DumpedPrivateKey dumpedPrivateKey = new DumpedPrivateKey(NetworkParameters.prodNet(), signature);
key = dumpedPrivateKey.getKey();
} catch (Exception err) {
log.severe("Invalid private key");
log.warn("Invalid private key");
}
}

Expand Down Expand Up @@ -265,7 +266,7 @@ private static Timestamp create(Timestamp timestamp, List<String> calendarUrls,
}

if (count < m) {
log.severe("Failed to create timestamp: requested " + String.valueOf(m) + " attestation" + ((m > 1) ? "s" : "") + " but received only " + String.valueOf(count));
log.warn("Failed to create timestamp: requested {} attestation{} but received only {}", m, (m > 1) ? "s" : "", count);
}

//shut down the executor service now
Expand Down Expand Up @@ -314,8 +315,7 @@ public static Timestamp makeMerkleTree(List<DetachedTimestampFile> fileTimestamp

public static HashMap<VerifyResult.Chains, VerifyResult> verify(DetachedTimestampFile ots, DetachedTimestampFile stamped) throws Exception {
if (!Arrays.equals(ots.fileDigest(), stamped.fileDigest())) {
log.severe("Expected digest " + Utils.bytesToHex(ots.fileDigest()).toLowerCase());
log.severe("File does not match original!");
log.warn("Expected digest {}. File does not match original!", Utils.bytesToHex(ots.fileDigest()).toLowerCase());
throw new Exception("File does not match original!");
}

Expand Down Expand Up @@ -375,7 +375,7 @@ public static HashMap<VerifyResult.Chains, VerifyResult> verify(Timestamp timest
throw e;
}

log.severe(Utils.toUpperFirstLetter(text) + " verification failed: " + e.getMessage());
log.warn("{} verification failed: {}", Utils.toUpperFirstLetter(text), e.getMessage());
throw e;
}
}
Expand All @@ -401,7 +401,7 @@ public static Long verify(BitcoinBlockHeaderAttestation attestation, byte[] msg)
BitcoinNode bitcoin = new BitcoinNode(properties);
blockInfo = bitcoin.getBlockHeader(height);
} catch (Exception e1) {
log.fine("There is no local node available");
log.info("This is no local node available");

try {
String blockHash = Esplora.blockHash(height);
Expand Down Expand Up @@ -430,9 +430,9 @@ public static Long verify(LitecoinBlockHeaderAttestation attestation, byte[] msg
BlockHeader blockInfo;

try {
String blockHash = blockHash = Esplora.blockHash(height);
String blockHash = Esplora.blockHash(height);
blockInfo = Esplora.block(blockHash);
log.info("Lite-client verification, assuming block " + blockHash + " is valid");
log.info("Lite-client verification, assuming block {} is valid", blockHash);
} catch (Exception e2) {
e2.printStackTrace();
throw e2;
Expand Down
32 changes: 14 additions & 18 deletions src/main/java/com/eternitywall/ots/OtsCli.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
import org.apache.commons.cli.CommandLine;
import org.apache.commons.cli.CommandLineParser;
import org.apache.commons.cli.Options;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.File;
import java.io.FileInputStream;
Expand All @@ -22,7 +24,6 @@
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.logging.Logger;

/**
* The CLI for OpenTimestamps.
Expand All @@ -31,7 +32,7 @@
*/
public class OtsCli {

private static Logger log = Utils.getLogger(OtsCli.class.getName());
private static final Logger log = LoggerFactory.getLogger(OtsCli.class);
private static String title = "OtsCli";
private static String version = "1.0";
private static List<String> calendarsUrl = new ArrayList<>();
Expand Down Expand Up @@ -215,7 +216,7 @@ public static void info(String argsOts, boolean verbose) {
String infoResult = OpenTimestamps.info(detached, verbose);
System.out.println(infoResult);
} catch (IOException e) {
log.severe("No valid file");
log.warn("No valid file");
}
}

Expand All @@ -227,7 +228,7 @@ private static void multistamp(List<String> argsFiles, List<String> calendarsUrl
try {
privateUrls = readSignature(signatureFile);
} catch (Exception e) {
log.severe("No valid signature file");
log.warn("No valid signature file");
return;
}
}
Expand All @@ -241,12 +242,10 @@ private static void multistamp(List<String> argsFiles, List<String> calendarsUrl
Hash hash = Hash.from(file, Hash.getOp(algorithm)._TAG());
mapFiles.put(argsFile, DetachedTimestampFile.from(hash));
} catch (IOException e) {
e.printStackTrace();
log.severe("File read error");
log.warn("File read error", e);
return;
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
log.severe("Crypto error");
log.warn("Crypto error", e);
return;
}
}
Expand All @@ -262,8 +261,7 @@ private static void multistamp(List<String> argsFiles, List<String> calendarsUrl
throw new IOException();
}
} catch (IOException e) {
e.printStackTrace();
log.severe("Stamp error");
log.warn("Stamp error", e);

return;
}
Expand All @@ -284,8 +282,7 @@ private static void multistamp(List<String> argsFiles, List<String> calendarsUrl
System.out.println("The timestamp proof '" + argsOts + "' has been created!");
}
} catch (Exception e) {
e.printStackTrace();
log.severe("File '" + argsOts + "' writing error");
log.warn("File '{}' writing error: {}", argsOts, e);
}
}
}
Expand All @@ -297,7 +294,7 @@ private static void stamp(Hash hash, List<String> calendarsUrl, Integer m, Strin
try {
privateUrls = readSignature(signatureFile);
} catch (Exception e) {
log.severe("No valid signature file");
log.warn("No valid signature file");
}
}

Expand All @@ -315,7 +312,7 @@ private static void stamp(Hash hash, List<String> calendarsUrl, Integer m, Strin
Files.write(path, stampResult.serialize());
System.out.println("The timestamp proof '" + argsOts + "' has been created!");
} catch (Exception e) {
log.severe("Invalid shasum");
log.warn("Invalid shasum");
}
}

Expand Down Expand Up @@ -359,7 +356,7 @@ public static void verify(String argsOts, Hash hash, String argsFile) {
return;
}
} catch (Exception e) {
log.severe("No valid file");
log.warn("No valid file");
}
}

Expand Down Expand Up @@ -391,10 +388,9 @@ public static void upgrade(String argsOts, boolean shrink) {
Files.write(pathOts, detachedOts.serialize());
}
} catch (IOException e) {
log.severe("No valid file");
log.warn("No valid file");
} catch (Exception e) {
e.printStackTrace();
log.severe("Shrink error");
log.warn("Shrink error", e);
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
package com.eternitywall.ots;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Arrays;
import java.util.logging.Logger;

public class StreamDeserializationContext {

private static Logger log = Utils.getLogger(StreamDeserializationContext.class.getName());
private static final Logger log = LoggerFactory.getLogger(StreamDeserializationContext.class);

byte[] buffer;
int counter = 0;
Expand Down Expand Up @@ -81,10 +83,10 @@ public byte[] readVarbytes(int maxLen, int minLen) {
int l = this.readVaruint();

if ((l & 0xff) > maxLen) {
log.severe("varbytes max length exceeded;");
log.warn("varbytes max length exceeded;");
return null;
} else if ((l & 0xff) < minLen) {
log.severe("varbytes min length not met;");
log.warn("varbytes min length not met;");
return null;
}

Expand Down
Loading