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

Dump delayed tx #3925

Merged
merged 2 commits into from
Feb 3, 2020
Merged
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
9 changes: 9 additions & 0 deletions common/src/main/java/bisq/common/config/Config.java
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ public class Config {
public static final String GENESIS_BLOCK_HEIGHT = "genesisBlockHeight";
public static final String GENESIS_TOTAL_SUPPLY = "genesisTotalSupply";
public static final String DAO_ACTIVATED = "daoActivated";
public static final String DUMP_DELAYED_PAYOUT_TXS = "dumpDelayedPayoutTxs";

// Default values for certain options
public static final int UNSPECIFIED_PORT = -1;
Expand Down Expand Up @@ -195,6 +196,7 @@ public class Config {
public final String genesisTxId;
public final int genesisBlockHeight;
public final long genesisTotalSupply;
public final boolean dumpDelayedPayoutTxs;

// Properties derived from options but not exposed as options themselves
public final File torDir;
Expand Down Expand Up @@ -597,6 +599,12 @@ public Config(String defaultAppName, File defaultUserDataDir, String... args) {
.ofType(boolean.class)
.defaultsTo(true);

ArgumentAcceptingOptionSpec<Boolean> dumpDelayedPayoutTxsOpt =
parser.accepts(DUMP_DELAYED_PAYOUT_TXS, "Dump delayed payout transactions to file")
.withRequiredArg()
.ofType(boolean.class)
.defaultsTo(false);

try {
CompositeOptionSet options = new CompositeOptionSet();

Expand Down Expand Up @@ -701,6 +709,7 @@ public Config(String defaultAppName, File defaultUserDataDir, String... args) {
this.genesisBlockHeight = options.valueOf(genesisBlockHeightOpt);
this.genesisTotalSupply = options.valueOf(genesisTotalSupplyOpt);
this.daoActivated = options.valueOf(daoActivatedOpt) || !baseCurrencyNetwork.isMainnet();
this.dumpDelayedPayoutTxs = options.valueOf(dumpDelayedPayoutTxsOpt);
} catch (OptionException ex) {
throw new ConfigException("problem parsing option '%s': %s",
ex.options().get(0),
Expand Down
56 changes: 46 additions & 10 deletions core/src/main/java/bisq/core/trade/TradeManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -57,13 +57,16 @@
import bisq.network.p2p.SendMailboxMessageListener;

import bisq.common.ClockWatcher;
import bisq.common.config.Config;
import bisq.common.crypto.KeyRing;
import bisq.common.handlers.ErrorMessageHandler;
import bisq.common.handlers.FaultHandler;
import bisq.common.handlers.ResultHandler;
import bisq.common.proto.network.NetworkEnvelope;
import bisq.common.proto.persistable.PersistedDataHost;
import bisq.common.storage.JsonFileManager;
import bisq.common.storage.Storage;
import bisq.common.util.Utilities;

import org.bitcoinj.core.AddressFormatException;
import org.bitcoinj.core.Coin;
Expand All @@ -72,6 +75,7 @@
import org.bitcoinj.core.TransactionConfidence;

import javax.inject.Inject;
import javax.inject.Named;

import com.google.common.util.concurrent.FutureCallback;

Expand All @@ -86,6 +90,8 @@

import org.spongycastle.crypto.params.KeyParameter;

import java.io.File;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
Expand Down Expand Up @@ -141,6 +147,8 @@ public class TradeManager implements PersistedDataHost {
private final LongProperty numPendingTrades = new SimpleLongProperty();
@Getter
private final ObservableList<Trade> tradesWithoutDepositTx = FXCollections.observableArrayList();
private final boolean dumpDelayedPayoutTxs;
private final JsonFileManager jsonFileManager;


///////////////////////////////////////////////////////////////////////////////////////////
Expand All @@ -167,7 +175,9 @@ public TradeManager(User user,
RefundAgentManager refundAgentManager,
DaoFacade daoFacade,
ClockWatcher clockWatcher,
Storage<TradableList<Trade>> storage) {
Storage<TradableList<Trade>> storage,
@Named(Config.STORAGE_DIR) File storageDir,
@Named(Config.DUMP_DELAYED_PAYOUT_TXS) boolean dumpDelayedPayoutTxs) {
this.user = user;
this.keyRing = keyRing;
this.btcWalletService = btcWalletService;
Expand All @@ -187,6 +197,7 @@ public TradeManager(User user,
this.refundAgentManager = refundAgentManager;
this.daoFacade = daoFacade;
this.clockWatcher = clockWatcher;
this.dumpDelayedPayoutTxs = dumpDelayedPayoutTxs;

tradableListStorage = storage;

Expand Down Expand Up @@ -222,6 +233,9 @@ public TradeManager(User user,
}
}
});

jsonFileManager = new JsonFileManager(storageDir);

}

@Override
Expand All @@ -233,6 +247,7 @@ public void readPersisted() {
if (offer != null)
offer.setPriceFeedService(priceFeedService);
});
maybeDumpDelayedPayoutTxs();
}


Expand Down Expand Up @@ -285,13 +300,13 @@ private void initPendingTrades() {
removePreparedTradeList.add(trade);
}

if (trade.getDepositTx() == null) {
log.warn("Deposit tx for trader with ID {} is null at initPendingTrades. " +
"This can happen for valid transaction in rare cases (e.g. after a SPV resync). " +
"We leave it to the user to move the trade to failed trades if the problem persists.",
trade.getId());
tradesWithoutDepositTx.add(trade);
}
if (trade.getDepositTx() == null) {
log.warn("Deposit tx for trader with ID {} is null at initPendingTrades. " +
"This can happen for valid transaction in rare cases (e.g. after a SPV resync). " +
"We leave it to the user to move the trade to failed trades if the problem persists.",
trade.getId());
tradesWithoutDepositTx.add(trade);
}
}
);

Expand Down Expand Up @@ -460,7 +475,7 @@ public void onTakeOffer(Coin amount,
model,
tradeResultHandler);
},
errorMessageHandler::handleErrorMessage);
errorMessageHandler);
}

private void createTrade(Coin amount,
Expand Down Expand Up @@ -530,7 +545,7 @@ public void onWithdrawRequest(String toAddress, Coin amount, Coin fee, KeyParame
Trade trade, ResultHandler resultHandler, FaultHandler faultHandler) {
String fromAddress = btcWalletService.getOrCreateAddressEntry(trade.getId(),
AddressEntry.Context.TRADE_PAYOUT).getAddressString();
FutureCallback<Transaction> callback = new FutureCallback<Transaction>() {
FutureCallback<Transaction> callback = new FutureCallback<>() {
@Override
public void onSuccess(@javax.annotation.Nullable Transaction transaction) {
if (transaction != null) {
Expand Down Expand Up @@ -778,4 +793,25 @@ else if (now.after(halfTradePeriodDate))
public void persistTrades() {
tradableList.persist();
}

@SuppressWarnings("InnerClassMayBeStatic")
class DelayedPayoutHash {
String tradeId;
String delayedPayoutTx;
DelayedPayoutHash(String tradeId, String delayedPayoutTx) {
this.tradeId = tradeId;
this.delayedPayoutTx = delayedPayoutTx;
}
}

private void maybeDumpDelayedPayoutTxs() {
if (!dumpDelayedPayoutTxs)
return;

var delayedPayoutHashes = tradableList.stream()
.map(trade -> new DelayedPayoutHash(trade.getId(),
Utilities.bytesAsHexString(trade.getDelayedPayoutTxBytes())))
.collect(Collectors.toList());
jsonFileManager.writeToDisc(Utilities.objectToJson(delayedPayoutHashes), "delayed_payout_txs");
}
}
2 changes: 2 additions & 0 deletions core/src/main/java/bisq/core/trade/TradeModule.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@

import com.google.inject.Singleton;

import static bisq.common.config.Config.DUMP_DELAYED_PAYOUT_TXS;
import static bisq.common.config.Config.DUMP_STATISTICS;
import static com.google.inject.name.Names.named;

Expand All @@ -56,5 +57,6 @@ protected void configure() {
bind(ReferralIdService.class).in(Singleton.class);
bind(AssetTradeActivityCheck.class).in(Singleton.class);
bindConstant().annotatedWith(named(DUMP_STATISTICS)).to(config.dumpStatistics);
bindConstant().annotatedWith(named(DUMP_DELAYED_PAYOUT_TXS)).to(config.dumpDelayedPayoutTxs);
}
}