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

feat(coinjoin): add tracking of transactions #222

Merged
merged 12 commits into from
Aug 1, 2023
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
17 changes: 17 additions & 0 deletions .run/WalletTool Regular Empty.run.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<component name="ProjectRunConfigurationManager">
<configuration default="false" name="WalletTool Regular Empty" type="Application" factoryName="Application">
<option name="MAIN_CLASS_NAME" value="org.bitcoinj.tools.WalletTool" />
<module name="dashj-master-three.tools.main" />
<option name="PROGRAM_PARAMETERS" value="send --wallet=coinjoin.testnet.wallet --net=TEST --output=yWwJjYLf7B9rY3M2TyBVs1Gw8drNWs8SNz:ALL " />
<option name="VM_PARAMETERS" value="-Djava.library.path=contrib/dashj-bls/bls/target/cmake" />
<extension name="coverage">
<pattern>
<option name="PATTERN" value="org.bitcoinj.examples.*" />
<option name="ENABLED" value="true" />
</pattern>
</extension>
<method v="2">
<option name="Make" enabled="true" />
</method>
</configuration>
</component>
19 changes: 14 additions & 5 deletions core/src/main/java/org/bitcoinj/coinjoin/CoinJoin.java
Original file line number Diff line number Diff line change
Expand Up @@ -144,10 +144,14 @@ public static boolean isCollateralValid(Transaction txCollateral) {
}

public static boolean isCollateralValid(Transaction txCollateral, boolean checkInputs) {
if (txCollateral.getOutputs().isEmpty())
if (txCollateral.getOutputs().isEmpty()) {
log.info("coinjoin: Collateral invalid due to no outputs: {}", txCollateral.getTxId());
return false;
if (txCollateral.getLockTime() != 0)
}
if (txCollateral.getLockTime() != 0) {
log.info("coinjoin: Collateral invalid due to lock time != 0: {}", txCollateral.getTxId());
return false;
}

Coin nValueIn = Coin.ZERO;
Coin nValueOut = Coin.ZERO;
Expand Down Expand Up @@ -180,10 +184,13 @@ public static boolean isCollateralValid(Transaction txCollateral, boolean checkI
}
}

log.info("coinjoin: collateral: {}", txCollateral); /* Continued */

// the collateral tx must not have been seen on the network
return txCollateral.getConfidence().getConfidenceType() == TransactionConfidence.ConfidenceType.UNKNOWN;
boolean hasBeenSeen = txCollateral.getConfidence().getConfidenceType() == TransactionConfidence.ConfidenceType.UNKNOWN;
if (hasBeenSeen) {
log.info("coinjoin: collateral has been spent, need to recreate txCollateral={}", txCollateral.getTxId());
}
log.info("coinjoin: collateral: {}", txCollateral); /* Continued */
return hasBeenSeen;
}
public static Coin getCollateralAmount() { return getSmallestDenomination().div(10); }
public static Coin getMaxCollateralAmount() { return getCollateralAmount().multiply(4); }
Expand Down Expand Up @@ -333,6 +340,8 @@ public static String getMessageByID(PoolMessage nMessageID) {
return "Inputs vs outputs size mismatch.";
case ERR_TIMEOUT:
return "Session has timed out.";
case ERR_CONNECTION_TIMEOUT:
return "Connection attempt has timed out (" + PendingDsaRequest.TIMEOUT + " ms).";
default:
return "Unknown response.";
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

import com.google.common.collect.Lists;
import com.google.common.util.concurrent.SettableFuture;
import org.bitcoinj.coinjoin.listeners.CoinJoinTransactionListener;
import org.bitcoinj.coinjoin.listeners.MixingCompleteListener;
import org.bitcoinj.coinjoin.listeners.MixingStartedListener;
import org.bitcoinj.coinjoin.listeners.SessionCompleteListener;
Expand Down Expand Up @@ -47,11 +48,9 @@
import java.util.Collections;
import java.util.Deque;
import java.util.EnumSet;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Random;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.concurrent.Executor;
Expand Down Expand Up @@ -96,6 +95,8 @@ public class CoinJoinClientManager {
= new CopyOnWriteArrayList<>();
private final CopyOnWriteArrayList<ListenerRegistration<MixingCompleteListener>> mixingCompleteListeners
= new CopyOnWriteArrayList<>();
private final CopyOnWriteArrayList<ListenerRegistration<CoinJoinTransactionListener>> transactionListeners
= new CopyOnWriteArrayList<>();

private boolean waitForAnotherBlock() {
if (context.masternodeSync.hasSyncFlag(MasternodeSync.SYNC_FLAGS.SYNC_GOVERNANCE) &&
Expand All @@ -106,6 +107,10 @@ private boolean waitForAnotherBlock() {
return cachedBlockHeight - cachedLastSuccessBlock < minBlocksToWait;
}

public boolean isWaitingForNewBlock() {
return waitForAnotherBlock();
}

// Make sure we have enough keys since last backup
private boolean checkAutomaticBackup() {
return CoinJoinClientOptions.isEnabled() && isMixing();
Expand Down Expand Up @@ -253,12 +258,16 @@ public boolean doAutomaticDenominating(boolean dryRun) {
try {
if (deqSessions.size() < CoinJoinClientOptions.getSessions()) {
CoinJoinClientSession newSession = new CoinJoinClientSession(mixingWallet);
log.info("creating new session: {}: ", newSession.getId());
for (ListenerRegistration<SessionCompleteListener> listener : sessionCompleteListeners) {
newSession.addSessionCompleteListener(listener.executor, listener.listener);
}
for (ListenerRegistration<SessionStartedListener> listener : sessionStartedListeners) {
newSession.addSessionStartedListener(listener.executor, listener.listener);
}
for (ListenerRegistration<CoinJoinTransactionListener> listener : transactionListeners) {
newSession.addTransationListener (listener.executor, listener.listener);
}
deqSessions.addLast(newSession);
}
for (CoinJoinClientSession session: deqSessions) {
Expand Down Expand Up @@ -595,6 +604,37 @@ public void run() {
}
}

/**
* Adds an event listener object. Methods on this object are called when something interesting happens,
* like receiving money. Runs the listener methods in the user thread.
*/
public void addTransationListener (CoinJoinTransactionListener listener) {
addTransationListener (Threading.USER_THREAD, listener);
}

/**
* Adds an event listener object. Methods on this object are called when something interesting happens,
* like receiving money. The listener is executed by the given executor.
*/
public void addTransationListener (Executor executor, CoinJoinTransactionListener listener) {
// This is thread safe, so we don't need to take the lock.
transactionListeners.add(new ListenerRegistration<>(listener, executor));
for (CoinJoinClientSession session: deqSessions) {
session.addTransationListener (executor, listener);
}
}

/**
* Removes the given event listener object. Returns true if the listener was removed, false if that listener
* was never added.
*/
public boolean removeTransationListener(CoinJoinTransactionListener listener) {
for (CoinJoinClientSession session: deqSessions) {
session.removeTransactionListener(listener);
}
return ListenerRegistration.removeFromList(listener, transactionListeners);
}

public List<PoolStatus> getSessionsStatus() {
ArrayList<PoolStatus> sessionsStatus = Lists.newArrayList();
for (CoinJoinClientSession session : deqSessions) {
Expand Down
Loading
Loading