This commit is contained in:
Manfred Karrer 2016-02-09 00:36:59 +01:00
parent 96090b71ad
commit 86284fe644
30 changed files with 135 additions and 115 deletions

View file

@ -61,7 +61,7 @@ public class FeePolicy {
}
// Some wallets (Mycelium) don't support higher fees
public static Coin getMinFundingFee() {
public static Coin getMinRequiredFeeForFundingTx() {
return Coin.valueOf(20_000);
}

View file

@ -4,10 +4,14 @@ import com.google.common.util.concurrent.FutureCallback;
import com.google.common.util.concurrent.Futures;
import com.google.common.util.concurrent.SettableFuture;
import com.google.inject.Inject;
import io.bitsquare.app.Log;
import io.bitsquare.btc.BitcoinNetwork;
import io.bitsquare.btc.FeePolicy;
import io.bitsquare.btc.blockchain.providers.BlockTrailProvider;
import io.bitsquare.btc.blockchain.providers.BlockchainApiProvider;
import io.bitsquare.btc.blockchain.providers.BlockrIOProvider;
import io.bitsquare.btc.blockchain.providers.TradeBlockProvider;
import io.bitsquare.user.Preferences;
import org.bitcoinj.core.Coin;
import org.jetbrains.annotations.NotNull;
import org.slf4j.Logger;
@ -20,34 +24,47 @@ public class BlockchainService {
private static final Logger log = LoggerFactory.getLogger(BlockchainService.class);
private final ArrayList<BlockchainApiProvider> providers;
private final boolean isMainNet;
@Inject
public BlockchainService(Preferences preferences) {
isMainNet = preferences.getBitcoinNetwork() == BitcoinNetwork.MAINNET;
providers = new ArrayList<>(Arrays.asList(new BlockrIOProvider(), new BlockTrailProvider(), new TradeBlockProvider()));
}
public BlockchainService() {
isMainNet = false;
providers = new ArrayList<>(Arrays.asList(new BlockrIOProvider(), new BlockTrailProvider(), new TradeBlockProvider()));
}
public SettableFuture<Coin> requestFeeFromBlockchain(String transactionId) {
log.debug("Request fee from providers");
Log.traceCall(transactionId);
long startTime = System.currentTimeMillis();
final SettableFuture<Coin> resultFuture = SettableFuture.create();
for (BlockchainApiProvider provider : providers) {
GetFeeRequest getFeeRequest = new GetFeeRequest();
SettableFuture<Coin> future = getFeeRequest.requestFee(transactionId, provider);
Futures.addCallback(future, new FutureCallback<Coin>() {
public void onSuccess(Coin fee) {
if (!resultFuture.isDone()) {
log.info("Request fee from providers done after {} ms.", (System.currentTimeMillis() - startTime));
resultFuture.set(fee);
}
}
public void onFailure(@NotNull Throwable throwable) {
if (!resultFuture.isDone()) {
log.warn("Could not get the fee from any provider after repeated requests.");
resultFuture.setException(throwable);
if (isMainNet) {
for (BlockchainApiProvider provider : providers) {
GetFeeRequest getFeeRequest = new GetFeeRequest();
SettableFuture<Coin> future = getFeeRequest.requestFee(transactionId, provider);
Futures.addCallback(future, new FutureCallback<Coin>() {
public void onSuccess(Coin fee) {
if (!resultFuture.isDone()) {
log.info("Request fee from providers done after {} ms.", (System.currentTimeMillis() - startTime));
resultFuture.set(fee);
}
}
}
});
public void onFailure(@NotNull Throwable throwable) {
if (!resultFuture.isDone()) {
log.warn("Could not get the fee from any provider after repeated requests.");
resultFuture.setException(throwable);
}
}
});
}
} else {
// For regtest/testnet we dont care of the check and set the expected value
resultFuture.set(FeePolicy.getMinRequiredFeeForFundingTx());
}
return resultFuture;
}

View file

@ -31,18 +31,18 @@ public final class FinalizePayoutTxRequest extends TradeMessage implements Mailb
public final byte[] sellerSignature;
public final String sellerPayoutAddress;
public final long lockTime;
public final long lockTimeAsBlockHeight;
private final NodeAddress senderNodeAddress;
public FinalizePayoutTxRequest(String tradeId,
byte[] sellerSignature,
String sellerPayoutAddress,
long lockTime,
long lockTimeAsBlockHeight,
NodeAddress senderNodeAddress) {
super(tradeId);
this.sellerSignature = sellerSignature;
this.sellerPayoutAddress = sellerPayoutAddress;
this.lockTime = lockTime;
this.lockTimeAsBlockHeight = lockTimeAsBlockHeight;
this.senderNodeAddress = senderNodeAddress;
}
@ -59,7 +59,7 @@ public final class FinalizePayoutTxRequest extends TradeMessage implements Mailb
FinalizePayoutTxRequest that = (FinalizePayoutTxRequest) o;
if (lockTime != that.lockTime) return false;
if (lockTimeAsBlockHeight != that.lockTimeAsBlockHeight) return false;
if (!Arrays.equals(sellerSignature, that.sellerSignature)) return false;
if (sellerPayoutAddress != null ? !sellerPayoutAddress.equals(that.sellerPayoutAddress) : that.sellerPayoutAddress != null)
return false;
@ -72,7 +72,7 @@ public final class FinalizePayoutTxRequest extends TradeMessage implements Mailb
int result = super.hashCode();
result = 31 * result + (sellerSignature != null ? Arrays.hashCode(sellerSignature) : 0);
result = 31 * result + (sellerPayoutAddress != null ? sellerPayoutAddress.hashCode() : 0);
result = 31 * result + (int) (lockTime ^ (lockTime >>> 32));
result = 31 * result + (int) (lockTimeAsBlockHeight ^ (lockTimeAsBlockHeight >>> 32));
result = 31 * result + (senderNodeAddress != null ? senderNodeAddress.hashCode() : 0);
return result;
}

View file

@ -45,7 +45,7 @@ public class ProcessFinalizePayoutTxRequest extends TradeTask {
processModel.tradingPeer.setSignature(checkNotNull(message.sellerSignature));
processModel.tradingPeer.setPayoutAddressString(nonEmptyStringOf(message.sellerPayoutAddress));
trade.setLockTimeAsBlockHeight(nonNegativeLongOf(message.lockTime));
trade.setLockTimeAsBlockHeight(nonNegativeLongOf(message.lockTimeAsBlockHeight));
trade.setState(Trade.State.FIAT_PAYMENT_RECEIPT_MSG_RECEIVED);

View file

@ -72,7 +72,7 @@ public class Preferences implements Serializable {
private static Locale defaultLocale = Locale.getDefault();
//TODO test with other locales
//private static Locale defaultLocale = Locale.US;
// private static Locale defaultLocale = Locale.US;
public static Locale getDefaultLocale() {
return defaultLocale;
@ -416,5 +416,4 @@ public class Preferences implements Serializable {
public boolean getUseTorForBitcoinJ() {
return useTorForBitcoinJ;
}
}