Skip to content

Commit

Permalink
Release first version 0.0.1-rc1
Browse files Browse the repository at this point in the history
  • Loading branch information
vincenzopalazzo authored Apr 25, 2021
2 parents 2ce59cf + c7ca0cb commit e91c279
Show file tree
Hide file tree
Showing 30 changed files with 242 additions and 78 deletions.
6 changes: 3 additions & 3 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ name: Publish package to the Maven Central Repository
on:
release:
types: [created]
push:
branches:
- main
#push:
# branches:
# - main
jobs:
build:
runs-on: ubuntu-latest
Expand Down
31 changes: 31 additions & 0 deletions .github/workflows/testing.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
name: Integration testing
on: push
jobs:
test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- name: Integration testing
run: |
docker-compose up --exit-code-from litebtc --quiet-pull
- name: Upload log
uses: actions/upload-artifact@v2
if: failure()
with:
path: |
**/lite-bitcoin-rpc.log
lib/build/reports/tests/test
name: ${{ github.run_number }}
- name: Upload test coverage
uses: codecov/codecov-action@v1
with:
token: ${{ secrets.CODECOV_TOKEN }} # not required for public repos
#files: lib/build/reports/jacoco/*.xml # optional
flags: unittests # optional
name: codecov-umbrella # optional
fail_ci_if_error: true # optional (default = false)
- name: Publish Unit Test Results
uses: EnricoMi/publish-unit-test-result-action@v1
if: always()
with:
files: lib/build/test-results/**/*.xml
Empty file modified Dockerfile
100644 → 100755
Empty file.
Empty file modified LICENSE
100644 → 100755
Empty file.
1 change: 1 addition & 0 deletions README.md
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# Lite Bitcoin RPC Wrapper

![GitHub Workflow Status](https://img.shields.io/github/workflow/status/clightning4j/lite-bitcoin-rpc/Java%20CI?style=flat-square)
[![codecov](https://codecov.io/gh/clightning4j/lite-bitcoin-rpc/branch/main/graph/badge.svg?token=KFIW2FXMBJ)](https://codecov.io/gh/clightning4j/lite-bitcoin-rpc)
![Maven Central](https://img.shields.io/maven-central/v/io.github.clightning4j/lite-bitcoin-rpc?style=flat-square)
![Sonatype Nexus (Snapshots)](https://img.shields.io/nexus/s/io.github.clightning4j/lite-bitcoin-rpc?server=https%3A%2F%2Foss.sonatype.org&style=flat-square)

Expand Down
Empty file modified docker-compose.yml
100644 → 100755
Empty file.
11 changes: 2 additions & 9 deletions gradle.properties
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,11 +1,4 @@
GROUP_ID=io.github.clightning4j
ARTIFACT_ID=lite-bitcoin-rpc
VERSION=0.0.1-rc1-SNAPSHOT
MODULE_NAME=io.clightning4j.litebitcoinrpc

#Information abaut the maven repository
signing.keyId=
signing.password=
signing.secretKeyRingFile=
ossrhUsername=
ossrhPassword=
VERSION=0.0.1-rc1
MODULE_NAME=io.clightning4j.litebitcoinrpc
Empty file modified gradle/wrapper/gradle-wrapper.jar
100644 → 100755
Empty file.
Empty file modified gradle/wrapper/gradle-wrapper.properties
100644 → 100755
Empty file.
Empty file modified gradlew.bat
100644 → 100755
Empty file.
29 changes: 28 additions & 1 deletion lib/build.gradle.kts
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ plugins {
id("com.github.sherter.google-java-format") version "0.9"
`maven-publish`
signing
jacoco
`java-library`
}
group = project.property("GROUP_ID")!!
Expand All @@ -21,10 +22,35 @@ dependencies {
implementation("com.squareup.okhttp3:logging-interceptor")
implementation("com.google.code.gson:gson:2.8.6")

testImplementation("junit:junit:4.13")
testImplementation("junit:junit:4.13.1")
}

tasks.jacocoTestReport {
reports {
xml.isEnabled = true
xml.destination = File("$buildDir/reports/jacoco/report.xml")
csv.isEnabled = false
html.isEnabled = false
}
executionData(File("$buildDir/jacoco/test.exec"))
}

tasks.test {
reports {
junitXml.isEnabled = true
html.isEnabled = true
}
finalizedBy(tasks.jacocoTestReport)
}

tasks {
test {
reports {
junitXml.isEnabled = true
html.isEnabled = true
}
}

register("fatJar", Jar::class.java) {
archiveBaseName.set(rootProject.name)
archiveClassifier.set("all")
Expand All @@ -41,6 +67,7 @@ tasks {
}

tasks{

create<Jar>("sourcesJar") {
archiveBaseName.set(rootProject.name)
archiveClassifier.set("sources")
Expand Down
2 changes: 0 additions & 2 deletions lib/src/main/java/io/github/clightning4j/litebtc/LiteBitcoinRPC.java
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,6 @@ public <T> T makeBitcoinRequest(Parameters parameters, Class<T> clazz)
T result = HttpFactory.getInstance().makeRequest(parameters);
String resultStr = converter.serialization(result);
return (T) converter.deserialization(resultStr, clazz);
} catch (BitcoinCoreException bitcoinCoreException) {
throw bitcoinCoreException;
} catch (UtilsExceptions utilsExceptions) {
throw new LiteBitcoinRPCException(utilsExceptions.getCause());
}
Expand Down
9 changes: 8 additions & 1 deletion lib/src/main/java/io/github/clightning4j/litebtc/exceptions/BitcoinCoreException.java
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,7 +1,14 @@
package io.github.clightning4j.litebtc.exceptions;

public class BitcoinCoreException extends Exception {
public BitcoinCoreException(String message) {
private int code;

public BitcoinCoreException(int code, String message) {
super(message);
this.code = code;
}

public int getCode() {
return code;
}
}
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
17 changes: 15 additions & 2 deletions lib/src/main/java/io/github/clightning4j/litebtc/model/generic/ResponseWrapper.java
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public class ResponseWrapper<T> {

private int id;
private T result;
private String error;
private ResponseError error;

public int getId() {
return id;
Expand All @@ -33,7 +33,20 @@ public T getResult() {
return result;
}

public String getError() {
public ResponseError getError() {
return error;
}

public class ResponseError {
private int code;
private String message;

public int getCode() {
return code;
}

public String getMessage() {
return message;
}
}
}
Empty file.
Empty file.
66 changes: 8 additions & 58 deletions lib/src/main/java/io/github/clightning4j/litebtc/utils/okhttp/HttpFactory.java
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,7 @@
import io.github.clightning4j.litebtc.model.generic.ResponseWrapper;
import io.github.clightning4j.litebtc.utils.gson.JsonConverter;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.lang.reflect.Type;
import java.net.Authenticator;
import java.net.HttpURLConnection;
import java.net.PasswordAuthentication;
import java.net.URL;
import java.util.concurrent.TimeUnit;
import okhttp3.*;
import org.slf4j.Logger;
Expand Down Expand Up @@ -74,24 +68,28 @@ public <T> T makeRequest(Parameters parameters) throws UtilsExceptions, BitcoinC
try {
Response response = this.client.newCall(request).execute();
ResponseBody body = response.body();
Type type = new TypeToken<ResponseWrapper<T>>() {}.getType();
if (!response.isSuccessful()) {
String message = "";
LOGGER.error("Request error with code: " + response.code());
if (body != null) {
message = body.string();
LOGGER.debug("Response Body\n" + message);
ResponseWrapper<T> wrapper =
(ResponseWrapper<T>) converter.deserialization(message, type);
if (wrapper.getError() != null) {
LOGGER.debug("Bitcoin core error with message: " + wrapper.getError().getMessage());
throw new BitcoinCoreException(
wrapper.getError().getCode(), wrapper.getError().getMessage());
}
}
throw new UtilsExceptions("Request error: " + response.code() + "Body: " + message);
}
if (body != null) {
String responseStr = body.string();
LOGGER.debug("Response from bitcoind\n" + responseStr);
Type type = new TypeToken<ResponseWrapper<T>>() {}.getType();
ResponseWrapper<T> wrapper =
(ResponseWrapper<T>) converter.deserialization(responseStr, type);
if (wrapper.getError() != null) {
throw new BitcoinCoreException(wrapper.getError());
}
String result = converter.serialization(wrapper.getResult());
LOGGER.error("Result conversion is: \n" + result);
return wrapper.getResult();
Expand All @@ -103,52 +101,4 @@ public <T> T makeRequest(Parameters parameters) throws UtilsExceptions, BitcoinC
throw new UtilsExceptions(e.getCause());
}
}

public <T> T makeRequestTest(Parameters parameters) throws UtilsExceptions {
if (parameters == null) {
LOGGER.error("Parameters object null");
throw new UtilsExceptions("Parameters object null");
}
Authenticator.setDefault(
new Authenticator() {
@Override
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(
configuration.getUser(), configuration.getPass().toCharArray());
}
});

try {
HttpURLConnection httpcon =
(HttpURLConnection) new URL(configuration.getUrl()).openConnection();
httpcon.setDoOutput(true);
httpcon.setRequestProperty("Content-Type", "application/json");
httpcon.setRequestProperty("Accept", "application/json");
httpcon.setRequestMethod("POST");
httpcon.connect();

OutputStream stream = httpcon.getOutputStream();
String jsonBody = converter.serialization(parameters);
LOGGER.debug("JSON body:\n" + jsonBody);
stream.write(jsonBody.getBytes());
int code = httpcon.getResponseCode();
boolean isError = code >= 400 && code <= 500;
String text = "";
if (isError) {
LOGGER.error("Eroror with code: " + code);
throw new UtilsExceptions("error");
} else {
InputStream inputStream = httpcon.getInputStream();
if (inputStream != null) {
text = new String(inputStream.readAllBytes());
}
}
LOGGER.error("Response from bitcoind\n" + text);
Type type = new TypeToken<T>() {}.getType();
return (T) converter.deserialization(text, type.getClass());
} catch (IOException e) {
LOGGER.error("Exception in makeRequest" + e.getLocalizedMessage());
throw new UtilsExceptions(e.getCause());
}
}
}
31 changes: 31 additions & 0 deletions lib/src/main/resources/logback.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<configuration scan="true">
<appender name="FILE" class="ch.qos.logback.core.FileAppender">
<file>${user.home}/clightning4j/lite-bitcoin-rpc.log</file>
<append>true</append>
<!-- set immediateFlush to false for much higher logging throughput -->
<immediateFlush>true</immediateFlush>
<!-- encoders are assigned the type
ch.qos.logback.classic.encoder.PatternLayoutEncoder by default -->
<encoder>
<pattern>%-4relative [%thread] %-5level %logger{35} - %msg%n</pattern>
</encoder>
</appender>

<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<!-- encoders are assigned the type
ch.qos.logback.classic.encoder.PatternLayoutEncoder by default -->
<encoder>
<pattern>%-4relative [%thread] %-5level %logger{35} - %msg %n</pattern>
</encoder>
</appender>

<root level="INFO">
<appender-ref ref="FILE" />
<appender-ref ref="STDOUT" />
</root>

<logger name="io.github.clightning4j.litebtc" level="INFO">
<appender-ref ref="FILE" />
<appender-ref ref="STDOUT" />
</logger>
</configuration>
49 changes: 48 additions & 1 deletion lib/src/test/java/io/github/clightning4j/litebtc/LiteBitcoinRPCTest.java
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,12 @@
*/
package io.github.clightning4j.litebtc;

import io.github.clightning4j.litebtc.exceptions.BitcoinCoreException;
import io.github.clightning4j.litebtc.exceptions.LiteBitcoinRPCException;
import io.github.clightning4j.litebtc.mock.BitcoinEstimateFee;
import io.github.clightning4j.litebtc.mock.BitcoinUTXO;
import io.github.clightning4j.litebtc.model.bitcoin.BlockchainInfo;
import io.github.clightning4j.litebtc.model.generic.Parameters;
import junit.framework.TestCase;
import org.junit.Test;

Expand All @@ -28,7 +33,7 @@ public class LiteBitcoinRPCTest {
private LiteBitcoinRPC bitcoinRPC;

public LiteBitcoinRPCTest() {
this.bitcoinRPC = new LiteBitcoinRPC("sandbox", "sandbox", "http://127.0.0.1:18332/");
this.bitcoinRPC = new LiteBitcoinRPC("sandbox", "sandbox", "http://127.0.0.1:18333/");
}

@Test
Expand All @@ -42,4 +47,46 @@ public void getBlockchainInfo() {
TestCase.fail(e.getLocalizedMessage());
}
}

@Test
public void estimateFeeRateWithError() {
Parameters parameters = new Parameters("estimatesmartfee");
parameters.addParameter("conf_target", 6);
try {
BitcoinEstimateFee feee = bitcoinRPC.makeBitcoinRequest(parameters, BitcoinEstimateFee.class);
TestCase.assertFalse(feee.getErrors().isEmpty());
} catch (LiteBitcoinRPCException | BitcoinCoreException e) {
TestCase.fail(e.getMessage());
}
}

@Test
public void getUTXOWithError() {
Parameters parameters = new Parameters("gettxout");
parameters.addParameter("txid", "txid");
parameters.addParameter("n", 0);
try {
bitcoinRPC.makeBitcoinRequest(parameters, BitcoinUTXO.class);
TestCase.fail("Expected BitcoinCoreException but we not receive it");
} catch (LiteBitcoinRPCException e) {
TestCase.fail("Wrong exception received (LiteBitcoinRPCException)");
} catch (BitcoinCoreException bitcoinCoreException) {
TestCase.assertNotNull(bitcoinCoreException);
}
}

@Test
public void getUTXOWithErrorWithMessage() throws LiteBitcoinRPCException {
Parameters parameters = new Parameters("gettxout");
parameters.addParameter("txid", "txid");
parameters.addParameter("n", 0);
try {
bitcoinRPC.makeBitcoinRequest(parameters, BitcoinUTXO.class);
TestCase.fail("Expected BitcoinCoreException but we not receive it");
} catch (BitcoinCoreException bitcoinCoreException) {
TestCase.assertEquals(-8, bitcoinCoreException.getCode());
TestCase.assertEquals(
"txid must be of length 64 (not 4, for 'txid')", bitcoinCoreException.getMessage());
}
}
}
Loading

0 comments on commit e91c279

Please sign in to comment.