Skip to content

Commit

Permalink
refactor(coinjoin): manually call CoinJoinManager.start and stop
Browse files Browse the repository at this point in the history
  • Loading branch information
HashEngineering committed Dec 24, 2023
1 parent b922cab commit 9a82930
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ public int compare(CompactTallyItem o, CompactTallyItem t1) {
boolean fCreateMixingCollaterals = !mixingWallet.hasCollateralInputs();

for (CompactTallyItem item : vecTally) {
if (!createDenominated(balanceToDenominate, item, fCreateMixingCollaterals)) continue;
if (!createDenominated(balanceToDenominate, item, fCreateMixingCollaterals, dryRun)) continue;
return true;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,11 +76,13 @@ public class CoinJoinManager {

private RequestKeyParameter requestKeyParameter;
private RequestDecryptedKey requestDecryptedKey;
private final ScheduledExecutorService scheduledExecutorService;

public CoinJoinManager(Context context) {
public CoinJoinManager(Context context, ScheduledExecutorService scheduledExecutorService) {
this.context = context;
coinJoinClientManagers = new HashMap<>();
coinJoinClientQueueManager = new CoinJoinClientQueueManager(context);
this.scheduledExecutorService = scheduledExecutorService;
}

public static boolean isCoinJoinMessage(Message message) {
Expand Down Expand Up @@ -141,12 +143,14 @@ public void run() {
}
};

public void start(ScheduledExecutorService scheduledExecutorService) {
public void start() {
log.info("CoinJoinManager starting...");
schedule = scheduledExecutorService.scheduleWithFixedDelay(
maintenanceRunnable, 1, 1, TimeUnit.SECONDS);
}

public void stop() {
log.info("CoinJoinManager stopping...");
if (schedule != null) {
schedule.cancel(false);
schedule = null;
Expand All @@ -159,6 +163,10 @@ public void stop() {
}
}

public boolean isRunning() {
return schedule != null && !schedule.isCancelled();
}

public void initMasternodeGroup(AbstractBlockChain blockChain) {
this.blockChain = blockChain;
masternodeGroup = new MasternodeGroup(context, blockChain);
Expand Down Expand Up @@ -231,6 +239,7 @@ public boolean disconnectMasternode(Masternode service) {
@VisibleForTesting
public void setMasternodeGroup(MasternodeGroup masternodeGroup) {
this.masternodeGroup = masternodeGroup;
masternodeGroup.setCoinJoinManager(this);
}

public SettableFuture<Boolean> getMixingFinishedFuture(Wallet wallet) {
Expand Down
5 changes: 1 addition & 4 deletions core/src/main/java/org/bitcoinj/core/Context.java
Original file line number Diff line number Diff line change
Expand Up @@ -282,7 +282,7 @@ public void initDash(boolean liteMode, boolean allowInstantX, @Nullable EnumSet<
chainLockHandler = new ChainLocksHandler(this);
llmqBackgroundThread = new LLMQBackgroundThread(this);
masternodeMetaDataManager = new MasternodeMetaDataManager(this);
coinJoinManager = new CoinJoinManager(this);
coinJoinManager = new CoinJoinManager(this, scheduledExecutorService);
initializedObjects = true;
}

Expand Down Expand Up @@ -532,9 +532,6 @@ public void start() {
scheduledGovernance = scheduledExecutorService.scheduleWithFixedDelay(
() -> governanceManager.doMaintenance(), 60, 5, TimeUnit.MINUTES);
}
if (initializedObjects) {
coinJoinManager.start(scheduledExecutorService);
}
}

public void shutdown() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
import org.bitcoinj.core.TransactionOutput;
import org.bitcoinj.core.Utils;
import org.bitcoinj.crypto.BLSLazyPublicKey;
import org.bitcoinj.crypto.BLSScheme;
import org.bitcoinj.crypto.BLSSecretKey;
import org.bitcoinj.evolution.SimplifiedMasternodeListDiff;
import org.bitcoinj.evolution.SimplifiedMasternodeListEntry;
Expand Down Expand Up @@ -108,7 +109,7 @@ public CoinJoinSessionTest(ClientType clientType) {
@Before
public void setUp() throws Exception {
super.setUp();

BLSScheme.setLegacyDefault(false);
BriefLogFormatter.initVerbose();
Utils.setMockClock(); // Use mock clock
wallet.freshReceiveKey();
Expand Down Expand Up @@ -164,6 +165,7 @@ public void setUp() throws Exception {

globalTimeout = Timeout.seconds(30);
mixingWallet = (WalletEx) wallet;
wallet.getContext().coinJoinManager.start();
}

protected Wallet createWallet(KeyChainGroup keyChainGroup) {
Expand All @@ -173,6 +175,7 @@ protected Wallet createWallet(KeyChainGroup keyChainGroup) {
@Override
@After
public void tearDown() {
wallet.getContext().coinJoinManager.stop();
super.tearDown();
}

Expand Down
1 change: 1 addition & 0 deletions tools/src/main/java/org/bitcoinj/tools/WalletTool.java
Original file line number Diff line number Diff line change
Expand Up @@ -1682,6 +1682,7 @@ private static void mix() {
wallet.getContext().coinJoinManager.addTransationListener (Threading.SAME_THREAD, reporter);
wallet.getContext().blockChain.addNewBestBlockListener(Threading.SAME_THREAD, reporter);

wallet.getContext().coinJoinManager.start();
// mix coins
try {
CoinJoinClientManager it = wallet.getContext().coinJoinManager.coinJoinClientManagers.get(wallet.getDescription());
Expand Down

0 comments on commit 9a82930

Please sign in to comment.