Skip to content

Commit

Permalink
chore: Various code cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
peacekeeper committed Oct 22, 2023
1 parent a6edca5 commit 9fa51cc
Show file tree
Hide file tree
Showing 8 changed files with 67 additions and 76 deletions.
4 changes: 2 additions & 2 deletions src/main/java/info/weboftrust/btctxlookup/DidBtcrData.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package info.weboftrust.btctxlookup;

import java.net.URI;

import com.fasterxml.jackson.annotation.JsonIgnore;

import java.net.URI;

public class DidBtcrData {

private final ChainAndTxid spentInChainAndTxid;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
package info.weboftrust.btctxlookup.bitcoinconnection;

import java.io.IOException;

import javax.annotation.Nullable;

import info.weboftrust.btctxlookup.Chain;
import info.weboftrust.btctxlookup.ChainAndLocationData;
import info.weboftrust.btctxlookup.ChainAndTxid;

import javax.annotation.Nullable;
import java.io.IOException;

public abstract class AbstractBitcoinConnection implements BitcoinConnection {

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -1,41 +1,38 @@
package info.weboftrust.btctxlookup.bitcoinconnection;

import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URI;
import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.util.*;
import java.util.regex.Matcher;

import javax.annotation.Nullable;

import org.apache.commons.codec.DecoderException;
import org.apache.commons.codec.binary.Hex;
import org.apache.commons.lang3.StringUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.google.common.base.Preconditions;

import info.weboftrust.btctxlookup.BitcoinClientID;
import info.weboftrust.btctxlookup.Chain;
import info.weboftrust.btctxlookup.ChainAndTxid;
import info.weboftrust.btctxlookup.DidBtcrData;
import info.weboftrust.btctxlookup.dto.AddressRelatedTx;
import org.apache.commons.codec.DecoderException;
import org.apache.commons.codec.binary.Hex;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import wf.bitcoin.javabitcoindrpcclient.BitcoinJSONRPCClient;
import wf.bitcoin.javabitcoindrpcclient.BitcoindRpcClient;
import wf.bitcoin.javabitcoindrpcclient.BitcoindRpcClient.RawTransaction.In;
import wf.bitcoin.javabitcoindrpcclient.BitcoindRpcClient.RawTransaction.Out;

import javax.annotation.Nullable;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URI;
import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.util.*;
import java.util.regex.Matcher;

public class BTCDRPCBitcoinConnection extends BitcoindRPCBitcoinConnection {

private final static ObjectMapper mapper;
private static final Logger log = LogManager.getLogger(BTCDRPCBitcoinConnection.class);
private static final Logger log = LoggerFactory.getLogger(BTCDRPCBitcoinConnection.class);

private static final int DEFAULT_COUNT = 100;
private static final int DEFAULT_SKIP = 0;
Expand Down Expand Up @@ -274,7 +271,7 @@ public Map<String, Long> findUnspents(String address, int skip, int count, boole
log.info("Request received for finding UTXOs. \nAddress: {}, skip {}, count {}, reverse {}", address, skip,
count, reverse);
List<AddressRelatedTx> addRelTxs = searchRawTransactions(address, skip, count, 1, reverse, null);
log.debug("{} TX found with for the address {}", addRelTxs::size, () -> address);
log.debug("{} TX found with for the address {}", addRelTxs.size(), address);
List<String> txins = new ArrayList<>();
Map<String, Long> txouts = new LinkedHashMap<>();

Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
package info.weboftrust.btctxlookup.bitcoinconnection;

import java.io.IOException;

import info.weboftrust.btctxlookup.Chain;
import info.weboftrust.btctxlookup.ChainAndLocationData;
import info.weboftrust.btctxlookup.ChainAndTxid;
import info.weboftrust.btctxlookup.DidBtcrData;

import java.io.IOException;

public interface BitcoinConnection {

ChainAndTxid lookupChainAndTxid(ChainAndLocationData chainAndLocationData) throws IOException;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,33 +1,16 @@
package info.weboftrust.btctxlookup.bitcoinconnection;

import java.io.IOException;
import java.math.BigDecimal;
import java.net.MalformedURLException;
import java.net.URI;
import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.annotation.Nullable;

import org.apache.commons.codec.DecoderException;
import org.apache.commons.codec.binary.Hex;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.google.common.base.Preconditions;

import info.weboftrust.btctxlookup.*;
import info.weboftrust.btctxlookup.dto.UTXOSet;
import org.apache.commons.codec.DecoderException;
import org.apache.commons.codec.binary.Hex;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import wf.bitcoin.javabitcoindrpcclient.BitcoinJSONRPCClient;
import wf.bitcoin.javabitcoindrpcclient.BitcoindRpcClient;
import wf.bitcoin.javabitcoindrpcclient.BitcoindRpcClient.Block;
Expand All @@ -36,12 +19,26 @@
import wf.bitcoin.javabitcoindrpcclient.BitcoindRpcClient.RawTransaction.Out;
import wf.bitcoin.javabitcoindrpcclient.GenericRpcException;

import javax.annotation.Nullable;
import java.io.IOException;
import java.math.BigDecimal;
import java.net.MalformedURLException;
import java.net.URI;
import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class BitcoindRPCBitcoinConnection extends AbstractBitcoinConnection implements BitcoinConnection {

protected static final Pattern patternAsmInputScriptPubKey = Pattern.compile("^[^\\s]+ ([0-9a-fA-F]+)$");
protected static final Pattern patternAsmContinuationUri = Pattern.compile("^OP_RETURN ([0-9a-fA-F]+)$");
private final static ObjectMapper mapper;
private static final Logger log = LogManager.getLogger(BitcoindRPCBitcoinConnection.class);
private static final Logger log = LoggerFactory.getLogger(BitcoindRPCBitcoinConnection.class);

private static final BitcoinClientID CLIENT_ID = BitcoinClientID.BITCOIND;

Expand Down Expand Up @@ -140,8 +137,8 @@ protected BitcoinJSONRPCClient getBitcoinRpcClient(Chain chain) {
@Nullable
@Override
public ChainAndLocationData lookupChainAndLocationData(ChainAndTxid chainAndTxid) {
log.info("Getting chain and location data for txid: {} on chain: {}", chainAndTxid::getTxid,
chainAndTxid::getChain);
log.info("Getting chain and location data for txid: {} on chain: {}", chainAndTxid.getTxid(),
chainAndTxid.getChain());

BitcoindRpcClient client = getBitcoinRpcClient(chainAndTxid.getChain());

Expand All @@ -168,8 +165,9 @@ public ChainAndLocationData lookupChainAndLocationData(ChainAndTxid chainAndTxid
transactionPosition, chainAndTxid.getTxoIndex());

log.debug("Resolved chain and location data is: \nBlock Height: {}, TX Position: {}, txoIndex: {}",
() -> result.getLocationData().getBlockHeight(),
() -> result.getLocationData().getTransactionPosition(), () -> result.getLocationData().getTxoIndex());
result.getLocationData().getBlockHeight(),
result.getLocationData().getTransactionPosition(),
result.getLocationData().getTxoIndex());

return result;
}
Expand Down Expand Up @@ -299,7 +297,7 @@ public int getBlockCount() throws BitcoinConnectionException {

public BigDecimal estimateFees(int targetConfirmInBlocks) {
Preconditions.checkState(!legacy);
return bitcoindRpcClient.estimateFee(targetConfirmInBlocks);
return bitcoindRpcClient.estimateSmartFee(targetConfirmInBlocks).feeRate();
}

public Map<String, Long> findUnspents(String address) throws BitcoinConnectionException {
Expand All @@ -311,7 +309,7 @@ public Map<String, Long> findUnspents(String address) throws BitcoinConnectionEx
final UTXOSet utxoSet = mapper.convertValue(response, UTXOSet.class);

if (utxoSet.getUnspents().isEmpty()) {
log.info("No UTXO found for the address: {}", address::toString);
log.info("No UTXO found for the address: {}", address);
return null;
}

Expand Down Expand Up @@ -344,18 +342,18 @@ public BitcoindRpcClient.RawTransaction getRawTransaction(String txid) throws Bi

public boolean isTxConfirmed(String txID, int requiredDepth) {
Preconditions.checkState(!legacy);
log.debug("Checking confirmations for tx id {} ", () -> txID);
log.debug("Checking confirmations for tx id {} ", txID);
BitcoindRpcClient.RawTransaction raw;

try {
raw = bitcoindRpcClient.getRawTransaction(txID);
} catch (GenericRpcException e) {
log.error(e);
log.error(e.getMessage(), e);
return false;
}

Preconditions.checkNotNull(raw, "Cannot get the TX from bitcoin client");
log.trace("RAW TX for txID {} is:\n{}", () -> txID, () -> raw);
log.trace("RAW TX for txID {} is:\n{}", txID, raw);
int confirms = 0;
if (raw.confirmations() != null) {
confirms = raw.confirmations();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
package info.weboftrust.btctxlookup.bitcoinconnection;

import java.io.IOException;

import org.bitcoinj.core.BlockChain;

import info.weboftrust.btctxlookup.ChainAndLocationData;
import info.weboftrust.btctxlookup.ChainAndTxid;
import info.weboftrust.btctxlookup.DidBtcrData;
import org.bitcoinj.core.BlockChain;

import java.io.IOException;

public class BitcoinjSPVBitcoinConnection extends AbstractBitcoinConnection implements BitcoinConnection {

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,15 @@
package info.weboftrust.btctxlookup.bitcoinconnection;

import com.google.common.base.Preconditions;
import com.google.gson.*;
import info.weboftrust.btctxlookup.*;
import org.apache.commons.codec.DecoderException;
import org.apache.commons.codec.binary.Hex;
import org.apache.commons.io.IOUtils;
import org.bitcoinj.script.Script;
import org.bitcoinj.script.ScriptChunk;
import org.bitcoinj.script.ScriptException;

import java.io.IOException;
import java.io.InputStream;
import java.net.URI;
Expand All @@ -9,18 +19,6 @@
import java.text.SimpleDateFormat;
import java.util.TimeZone;

import org.apache.commons.codec.DecoderException;
import org.apache.commons.codec.binary.Hex;
import org.apache.commons.io.IOUtils;
import org.bitcoinj.script.Script;
import org.bitcoinj.script.ScriptChunk;
import org.bitcoinj.script.ScriptException;

import com.google.common.base.Preconditions;
import com.google.gson.*;

import info.weboftrust.btctxlookup.*;

/**
* TODO
*/
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
package info.weboftrust.btctxlookup.dto;

import com.fasterxml.jackson.annotation.JsonProperty;

import java.math.BigDecimal;
import java.util.Collections;
import java.util.List;

import com.fasterxml.jackson.annotation.JsonProperty;

public class AddressRelatedTx {

private String hex;
Expand Down

0 comments on commit 9fa51cc

Please sign in to comment.