Search in sources :

Example 1 with ErrorMessageHandler

use of bisq.common.handlers.ErrorMessageHandler in project bisq-core by bisq-network.

the class Offer method checkOfferAvailability.

// /////////////////////////////////////////////////////////////////////////////////////////
// Availability
// /////////////////////////////////////////////////////////////////////////////////////////
public void checkOfferAvailability(OfferAvailabilityModel model, ResultHandler resultHandler, ErrorMessageHandler errorMessageHandler) {
    availabilityProtocol = new OfferAvailabilityProtocol(model, () -> {
        cancelAvailabilityRequest();
        resultHandler.handleResult();
    }, (errorMessage) -> {
        if (availabilityProtocol != null)
            availabilityProtocol.cancel();
        log.error(errorMessage);
        errorMessageHandler.handleErrorMessage(errorMessage);
    });
    availabilityProtocol.sendOfferAvailabilityRequest();
}
Also used : Altcoin(bisq.core.monetary.Altcoin) PubKeyRing(bisq.common.crypto.PubKeyRing) Setter(lombok.Setter) OfferAvailabilityModel(bisq.core.offer.availability.OfferAvailabilityModel) Getter(lombok.Getter) Utilities(bisq.common.util.Utilities) Coin(org.bitcoinj.core.Coin) SimpleStringProperty(javafx.beans.property.SimpleStringProperty) Date(java.util.Date) Volume(bisq.core.monetary.Volume) PersistablePayload(bisq.common.proto.persistable.PersistablePayload) PB(io.bisq.generated.protobuffer.PB) Preconditions.checkArgument(com.google.common.base.Preconditions.checkArgument) TradePriceOutOfToleranceException(bisq.core.exceptions.TradePriceOutOfToleranceException) ReadOnlyStringProperty(javafx.beans.property.ReadOnlyStringProperty) Map(java.util.Map) CurrencyUtil(bisq.core.locale.CurrencyUtil) ErrorMessageHandler(bisq.common.handlers.ErrorMessageHandler) NetworkPayload(bisq.common.proto.network.NetworkPayload) OfferAvailabilityProtocol(bisq.core.offer.availability.OfferAvailabilityProtocol) Nullable(javax.annotation.Nullable) ObjectProperty(javafx.beans.property.ObjectProperty) ResultHandler(bisq.common.handlers.ResultHandler) JsonExclude(bisq.common.util.JsonExclude) Preconditions.checkNotNull(com.google.common.base.Preconditions.checkNotNull) Fiat(org.bitcoinj.utils.Fiat) PublicKey(java.security.PublicKey) MathUtils(bisq.common.util.MathUtils) PaymentMethod(bisq.core.payment.payload.PaymentMethod) List(java.util.List) Slf4j(lombok.extern.slf4j.Slf4j) PriceFeedService(bisq.core.provider.price.PriceFeedService) SimpleObjectProperty(javafx.beans.property.SimpleObjectProperty) NodeAddress(bisq.network.p2p.NodeAddress) Price(bisq.core.monetary.Price) Optional(java.util.Optional) KeyRing(bisq.common.crypto.KeyRing) StringProperty(javafx.beans.property.StringProperty) MarketPrice(bisq.core.provider.price.MarketPrice) OfferAvailabilityProtocol(bisq.core.offer.availability.OfferAvailabilityProtocol)

Example 2 with ErrorMessageHandler

use of bisq.common.handlers.ErrorMessageHandler in project bisq-core by bisq-network.

the class BtcWalletService method doubleSpendTransaction.

// /////////////////////////////////////////////////////////////////////////////////////////
// Double spend unconfirmed transaction (unlock in case we got into a tx with a too low mining fee)
// /////////////////////////////////////////////////////////////////////////////////////////
public void doubleSpendTransaction(String txId, Runnable resultHandler, ErrorMessageHandler errorMessageHandler) throws InsufficientFundsException {
    AddressEntry addressEntry = getOrCreateUnusedAddressEntry(AddressEntry.Context.AVAILABLE);
    checkNotNull(addressEntry.getAddress(), "addressEntry.getAddress() must not be null");
    Optional<Transaction> transactionOptional = wallet.getTransactions(true).stream().filter(t -> t.getHashAsString().equals(txId)).findAny();
    if (transactionOptional.isPresent()) {
        Transaction txToDoubleSpend = transactionOptional.get();
        Address toAddress = addressEntry.getAddress();
        final TransactionConfidence.ConfidenceType confidenceType = txToDoubleSpend.getConfidence().getConfidenceType();
        if (confidenceType == TransactionConfidence.ConfidenceType.PENDING) {
            log.debug("txToDoubleSpend no. of inputs " + txToDoubleSpend.getInputs().size());
            Transaction newTransaction = new Transaction(params);
            txToDoubleSpend.getInputs().stream().forEach(input -> {
                final TransactionOutput connectedOutput = input.getConnectedOutput();
                if (connectedOutput != null && connectedOutput.isMine(wallet) && connectedOutput.getParentTransaction() != null && connectedOutput.getParentTransaction().getConfidence() != null && input.getValue() != null) {
                    // if (connectedOutput.getParentTransaction().getConfidence().getConfidenceType() == TransactionConfidence.ConfidenceType.BUILDING) {
                    newTransaction.addInput(new TransactionInput(params, newTransaction, new byte[] {}, new TransactionOutPoint(params, input.getOutpoint().getIndex(), new Transaction(params, connectedOutput.getParentTransaction().bitcoinSerialize())), Coin.valueOf(input.getValue().value)));
                /* } else {
                                    log.warn("Confidence of parent tx is not of type BUILDING: ConfidenceType=" +
                                            connectedOutput.getParentTransaction().getConfidence().getConfidenceType());
                                }*/
                }
            });
            log.info("newTransaction no. of inputs " + newTransaction.getInputs().size());
            log.info("newTransaction size in kB " + newTransaction.bitcoinSerialize().length / 1024);
            if (!newTransaction.getInputs().isEmpty()) {
                Coin amount = Coin.valueOf(newTransaction.getInputs().stream().mapToLong(input -> input.getValue() != null ? input.getValue().value : 0).sum());
                newTransaction.addOutput(amount, toAddress);
                try {
                    Coin fee;
                    int counter = 0;
                    int txSize = 0;
                    Transaction tx;
                    SendRequest sendRequest;
                    Coin txFeeForWithdrawalPerByte = getTxFeeForWithdrawalPerByte();
                    do {
                        counter++;
                        fee = txFeeForWithdrawalPerByte.multiply(txSize);
                        newTransaction.clearOutputs();
                        newTransaction.addOutput(amount.subtract(fee), toAddress);
                        sendRequest = SendRequest.forTx(newTransaction);
                        sendRequest.fee = fee;
                        sendRequest.feePerKb = Coin.ZERO;
                        sendRequest.ensureMinRequiredFee = false;
                        sendRequest.aesKey = aesKey;
                        sendRequest.coinSelector = new BtcCoinSelector(toAddress);
                        sendRequest.changeAddress = toAddress;
                        wallet.completeTx(sendRequest);
                        tx = sendRequest.tx;
                        txSize = tx.bitcoinSerialize().length;
                        printTx("FeeEstimationTransaction", tx);
                        sendRequest.tx.getOutputs().forEach(o -> log.debug("Output value " + o.getValue().toFriendlyString()));
                    } while (feeEstimationNotSatisfied(counter, tx));
                    if (counter == 10)
                        log.error("Could not calculate the fee. Tx=" + tx);
                    Wallet.SendResult sendResult = null;
                    try {
                        sendRequest = SendRequest.forTx(newTransaction);
                        sendRequest.fee = fee;
                        sendRequest.feePerKb = Coin.ZERO;
                        sendRequest.ensureMinRequiredFee = false;
                        sendRequest.aesKey = aesKey;
                        sendRequest.coinSelector = new BtcCoinSelector(toAddress);
                        sendRequest.changeAddress = toAddress;
                        sendResult = wallet.sendCoins(sendRequest);
                    } catch (InsufficientMoneyException e) {
                        // in some cases getFee did not calculate correctly and we still get an InsufficientMoneyException
                        log.warn("We still have a missing fee " + (e.missing != null ? e.missing.toFriendlyString() : ""));
                        amount = amount.subtract(e.missing);
                        newTransaction.clearOutputs();
                        newTransaction.addOutput(amount, toAddress);
                        sendRequest = SendRequest.forTx(newTransaction);
                        sendRequest.fee = fee;
                        sendRequest.feePerKb = Coin.ZERO;
                        sendRequest.ensureMinRequiredFee = false;
                        sendRequest.aesKey = aesKey;
                        sendRequest.coinSelector = new BtcCoinSelector(toAddress, false);
                        sendRequest.changeAddress = toAddress;
                        try {
                            sendResult = wallet.sendCoins(sendRequest);
                            printTx("FeeEstimationTransaction", newTransaction);
                        } catch (InsufficientMoneyException e2) {
                            errorMessageHandler.handleErrorMessage("We did not get the correct fee calculated. " + (e2.missing != null ? e2.missing.toFriendlyString() : ""));
                        }
                    }
                    if (sendResult != null) {
                        log.info("Broadcasting double spending transaction. " + sendResult.tx);
                        Futures.addCallback(sendResult.broadcastComplete, new FutureCallback<Transaction>() {

                            @Override
                            public void onSuccess(Transaction result) {
                                log.info("Double spending transaction published. " + result);
                                resultHandler.run();
                            }

                            @Override
                            public void onFailure(@NotNull Throwable t) {
                                log.error("Broadcasting double spending transaction failed. " + t.getMessage());
                                errorMessageHandler.handleErrorMessage(t.getMessage());
                            }
                        });
                    }
                } catch (InsufficientMoneyException e) {
                    throw new InsufficientFundsException("The fees for that transaction exceed the available funds " + "or the resulting output value is below the min. dust value:\n" + "Missing " + (e.missing != null ? e.missing.toFriendlyString() : "null"));
                }
            } else {
                String errorMessage = "We could not find inputs we control in the transaction we want to double spend.";
                log.warn(errorMessage);
                errorMessageHandler.handleErrorMessage(errorMessage);
            }
        } else if (confidenceType == TransactionConfidence.ConfidenceType.BUILDING) {
            errorMessageHandler.handleErrorMessage("That transaction is already in the blockchain so we cannot double spend it.");
        } else if (confidenceType == TransactionConfidence.ConfidenceType.DEAD) {
            errorMessageHandler.handleErrorMessage("One of the inputs of that transaction has been already double spent.");
        }
    }
}
Also used : Arrays(java.util.Arrays) Transaction(org.bitcoinj.core.Transaction) TransactionConfidence(org.bitcoinj.core.TransactionConfidence) Coin(org.bitcoinj.core.Coin) Wallet(org.bitcoinj.wallet.Wallet) LoggerFactory(org.slf4j.LoggerFactory) ArrayList(java.util.ArrayList) Inject(javax.inject.Inject) Preconditions.checkArgument(com.google.common.base.Preconditions.checkArgument) TransactionVerificationException(bisq.core.btc.exceptions.TransactionVerificationException) ImmutableList(com.google.common.collect.ImmutableList) AddressEntryList(bisq.core.btc.AddressEntryList) SendRequest(org.bitcoinj.wallet.SendRequest) ErrorMessageHandler(bisq.common.handlers.ErrorMessageHandler) KeyCrypterScrypt(org.bitcoinj.crypto.KeyCrypterScrypt) KeyParameter(org.spongycastle.crypto.params.KeyParameter) DeterministicKey(org.bitcoinj.crypto.DeterministicKey) Nullable(javax.annotation.Nullable) ScriptBuilder(org.bitcoinj.script.ScriptBuilder) AddressFormatException(org.bitcoinj.core.AddressFormatException) AddressEntryException(bisq.core.btc.AddressEntryException) WalletException(bisq.core.btc.exceptions.WalletException) TransactionOutPoint(org.bitcoinj.core.TransactionOutPoint) Logger(org.slf4j.Logger) InsufficientFundsException(bisq.core.btc.InsufficientFundsException) Preconditions.checkNotNull(com.google.common.base.Preconditions.checkNotNull) Set(java.util.Set) InsufficientMoneyException(org.bitcoinj.core.InsufficientMoneyException) Collectors(java.util.stream.Collectors) FutureCallback(com.google.common.util.concurrent.FutureCallback) Futures(com.google.common.util.concurrent.Futures) List(java.util.List) AddressEntry(bisq.core.btc.AddressEntry) TransactionInput(org.bitcoinj.core.TransactionInput) Preferences(bisq.core.user.Preferences) TransactionOutput(org.bitcoinj.core.TransactionOutput) Optional(java.util.Optional) FeeService(bisq.core.provider.fee.FeeService) Address(org.bitcoinj.core.Address) Preconditions(com.google.common.base.Preconditions) NotNull(org.jetbrains.annotations.NotNull) Restrictions(bisq.core.btc.Restrictions) TransactionOutput(org.bitcoinj.core.TransactionOutput) SendRequest(org.bitcoinj.wallet.SendRequest) Address(org.bitcoinj.core.Address) AddressEntry(bisq.core.btc.AddressEntry) Wallet(org.bitcoinj.wallet.Wallet) InsufficientMoneyException(org.bitcoinj.core.InsufficientMoneyException) TransactionInput(org.bitcoinj.core.TransactionInput) TransactionOutPoint(org.bitcoinj.core.TransactionOutPoint) Coin(org.bitcoinj.core.Coin) Transaction(org.bitcoinj.core.Transaction) InsufficientFundsException(bisq.core.btc.InsufficientFundsException) TransactionConfidence(org.bitcoinj.core.TransactionConfidence) TransactionOutPoint(org.bitcoinj.core.TransactionOutPoint)

Aggregations

ErrorMessageHandler (bisq.common.handlers.ErrorMessageHandler)2 Preconditions.checkArgument (com.google.common.base.Preconditions.checkArgument)2 Preconditions.checkNotNull (com.google.common.base.Preconditions.checkNotNull)2 List (java.util.List)2 Optional (java.util.Optional)2 Nullable (javax.annotation.Nullable)2 Coin (org.bitcoinj.core.Coin)2 KeyRing (bisq.common.crypto.KeyRing)1 PubKeyRing (bisq.common.crypto.PubKeyRing)1 ResultHandler (bisq.common.handlers.ResultHandler)1 NetworkPayload (bisq.common.proto.network.NetworkPayload)1 PersistablePayload (bisq.common.proto.persistable.PersistablePayload)1 JsonExclude (bisq.common.util.JsonExclude)1 MathUtils (bisq.common.util.MathUtils)1 Utilities (bisq.common.util.Utilities)1 AddressEntry (bisq.core.btc.AddressEntry)1 AddressEntryException (bisq.core.btc.AddressEntryException)1 AddressEntryList (bisq.core.btc.AddressEntryList)1 InsufficientFundsException (bisq.core.btc.InsufficientFundsException)1 Restrictions (bisq.core.btc.Restrictions)1