Skip to content

Commit

Permalink
refactor: update WalletTool to use DashSystem
Browse files Browse the repository at this point in the history
  • Loading branch information
HashEngineering committed Nov 26, 2024
1 parent 69fdb65 commit e8f52ee
Showing 1 changed file with 24 additions and 21 deletions.
45 changes: 24 additions & 21 deletions tools/src/main/java/org/bitcoinj/tools/WalletTool.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import org.bitcoinj.coinjoin.utils.CoinJoinReporter;
import org.bitcoinj.core.MasternodeSync;
import org.bitcoinj.crypto.*;
import org.bitcoinj.manager.DashSystem;
import org.bitcoinj.net.discovery.ThreeMethodPeerDiscovery;
import org.bitcoinj.params.AbstractBitcoinNetParams;
import org.bitcoinj.params.MainNetParams;
Expand Down Expand Up @@ -161,6 +162,7 @@ public class WalletTool {
private static String password;
private static org.bitcoin.protocols.payments.Protos.PaymentRequest paymentRequest;
private static AuthenticationGroupExtension authenticationGroupExtension;
private static DashSystem system;

public static class Condition {
public enum Type {
Expand Down Expand Up @@ -351,9 +353,10 @@ public static void main(String[] args) throws Exception {
}
Context context = new Context(params);
Context.propagate(context);
context.initDash(true, true);
context.masternodeSync.syncFlags.add(MasternodeSync.SYNC_FLAGS.SYNC_HEADERS_MN_LIST_FIRST);
context.initDashSync(".", "coinjoin-" + params.getNetworkName());
system = new DashSystem(context);
system.initDash(true, true);
system.masternodeSync.syncFlags.add(MasternodeSync.SYNC_FLAGS.SYNC_HEADERS_MN_LIST_FIRST);
system.initDashSync(".", "coinjoin-" + params.getNetworkName());

mode = modeFlag.value(options);

Expand Down Expand Up @@ -1353,7 +1356,7 @@ private static void setup() throws BlockStoreException {
} else {
//TODO: peerGroup.setRequiredServices(0); was used here previously, which is better
//for now, however peerGroup doesn't work with masternodeListManager, but it should
ThreeMethodPeerDiscovery peerDiscovery = new ThreeMethodPeerDiscovery(params, Context.get().masternodeListManager);
ThreeMethodPeerDiscovery peerDiscovery = new ThreeMethodPeerDiscovery(params, system.masternodeListManager);
peerDiscovery.setIncludeDNS(false);
peerGroup.addPeerDiscovery(peerDiscovery);
}
Expand All @@ -1363,7 +1366,7 @@ private static void syncChain() {
try {
setup();
int startTransactions = wallet.getTransactions(true).size();
DownloadProgressTracker listener = new DownloadProgressTracker();
DownloadProgressTracker listener = new DownloadProgressTracker(system.hasSyncFlag(MasternodeSync.SYNC_FLAGS.SYNC_BLOCKS_AFTER_PREPROCESSING));
peerGroup.start();
peerGroup.startBlockChainDownload(listener);
try {
Expand All @@ -1389,7 +1392,7 @@ private static void shutdown() {
peerGroup.stop();
saveWallet(walletFile);
store.close();
wallet.getContext().close();
system.close();
wallet = null;
} catch (BlockStoreException e) {
throw new RuntimeException(e);
Expand Down Expand Up @@ -1679,20 +1682,20 @@ private static void mix() {
System.out.println("Mixing Configuration:");
System.out.println(CoinJoinClientOptions.getString());

wallet.getContext().coinJoinManager.coinJoinClientManagers.put(wallet.getDescription(), new CoinJoinClientManager(wallet));
wallet.getContext().coinJoinManager.addSessionStartedListener(Threading.SAME_THREAD, reporter);
wallet.getContext().coinJoinManager.addSessionCompleteListener(Threading.SAME_THREAD, reporter);
wallet.getContext().coinJoinManager.addMixingCompleteListener(Threading.SAME_THREAD, reporter);
wallet.getContext().coinJoinManager.addTransationListener (Threading.SAME_THREAD, reporter);
wallet.getContext().blockChain.addNewBestBlockListener(Threading.SAME_THREAD, reporter);
system.coinJoinManager.coinJoinClientManagers.put(wallet.getDescription(), new CoinJoinClientManager(wallet, system.masternodeSync, system.coinJoinManager, system.masternodeListManager, system.masternodeMetaDataManager));
system.coinJoinManager.addSessionStartedListener(Threading.SAME_THREAD, reporter);
system.coinJoinManager.addSessionCompleteListener(Threading.SAME_THREAD, reporter);
system.coinJoinManager.addMixingCompleteListener(Threading.SAME_THREAD, reporter);
system.coinJoinManager.addTransationListener (Threading.SAME_THREAD, reporter);
system.blockChain.addNewBestBlockListener(Threading.SAME_THREAD, reporter);

wallet.getContext().coinJoinManager.start();
system.coinJoinManager.start();
// mix coins
try {
CoinJoinClientManager it = wallet.getContext().coinJoinManager.coinJoinClientManagers.get(wallet.getDescription());
CoinJoinClientManager it = system.coinJoinManager.coinJoinClientManagers.get(wallet.getDescription());
wallet.getCoinJoin().refreshUnusedKeys();
it.setStopOnNothingToDo(true);
it.setBlockChain(wallet.getContext().blockChain);
it.setBlockChain(system.blockChain);

{
if (wallet.isEncrypted()) {
Expand All @@ -1711,14 +1714,14 @@ private static void mix() {
System.out.println("Mixing " + (result ? "started successfully" : ("start failed: " + it.getStatuses() + ", will retry")));

// wait until finished mixing
SettableFuture<Boolean> mixingFinished = wallet.getContext().coinJoinManager.getMixingFinishedFuture(wallet);
SettableFuture<Boolean> mixingFinished = system.coinJoinManager.getMixingFinishedFuture(wallet);
mixingFinished.addListener(() -> System.out.println("Mixing complete."), Threading.SAME_THREAD);
mixingFinished.get();
wallet.getContext().coinJoinManager.removeSessionCompleteListener(reporter);
wallet.getContext().coinJoinManager.removeMixingCompleteListener(reporter);
wallet.getContext().coinJoinManager.removeSessionStartedListener(reporter);
wallet.getContext().coinJoinManager.removeTransactionListener(reporter);
wallet.getContext().coinJoinManager.stop();
system.coinJoinManager.removeSessionCompleteListener(reporter);
system.coinJoinManager.removeMixingCompleteListener(reporter);
system.coinJoinManager.removeSessionStartedListener(reporter);
system.coinJoinManager.removeTransactionListener(reporter);
system.coinJoinManager.stop();
} catch (ExecutionException | InterruptedException x) {
throw new RuntimeException(x);
}
Expand Down

0 comments on commit e8f52ee

Please sign in to comment.