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

Mixing fixes #1

Open
wants to merge 21 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
948a431
Fixed bunch of bugs, seems to work now.
sfriebe Aug 3, 2017
38bea60
Made eclipse classpath relative, removed temporary files from reposit…
sfriebe Sep 6, 2017
8193f28
Fixed some bugs and added a few methods to make library
sfriebe Sep 6, 2017
1f17e7d
Removed unneeded files.
sfriebe Sep 6, 2017
fcc99ba
Update BitNymWrapper, MixingTabController and BitNymWallet to allow m…
Oct 11, 2017
69008fc
Improve mixing
Oct 24, 2017
a08678f
Put doMix() functionality inside BitNymWallet
Oct 30, 2017
458e801
Improve mixing
Nov 9, 2017
8dff6da
Recognize when both mix partner try to mix active simultaneously
Nov 9, 2017
6fdc45e
Use context as parameter and Context.propagate on certain methods to …
Nov 14, 2017
edaa924
Update mixing
PhilbertM May 25, 2018
fb1cd59
Add error codes and gui output when mixing fails
PhilbertM Jun 15, 2018
e4e08f1
Fix mixing problems due to inconsistency in transaction chain
PhilbertM Jul 23, 2018
3535ab1
Avoid some null pointers and remove listeners properly on mixing abor…
PhilbertM Sep 4, 2018
6895da7
Test requesting blocks on multiple peers, clean up
PhilbertM Nov 9, 2018
f0d36f5
Change WaitForDataListener
PhilbertM Nov 21, 2018
d486d2c
Removing calls to javax.xml.bind.DatatypeConverter.printHexBinary().
sfriebe Dec 19, 2018
157b1f6
Improve code style, add enum for abort messages.
PhilbertM Jan 8, 2019
55db1ab
Remove TestMessage and debug outputs
PhilbertM Jan 11, 2019
437d822
Merge remote-tracking branch 'origin/remove-javax' into mixing-fixes
PhilbertM Jan 11, 2019
aeab376
Fix merge
PhilbertM Jan 11, 2019
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
45 changes: 42 additions & 3 deletions src/bitnymWallet/BitNymWallet.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import java.util.List;
import java.util.concurrent.TimeUnit;

import edu.kit.tm.ptp.Identifier;
import org.bitcoinj.core.*;
import org.bitcoinj.core.TransactionConfidence.Listener;
import org.bitcoinj.core.listeners.NewBestBlockListener;
Expand Down Expand Up @@ -78,7 +79,8 @@ public BitNymWallet() {
//MainClass.params = TestNet3Params.get();
MainClass.params = RegTestParams.get();
params = MainClass.params;
//Context context = new Context(params);
// TODO(PM) use context as parameter at certain methods to avoid bitcoin warinings
Context context = new Context(params);
proofChangeConfidenceListeners = new ArrayList<ProofConfidenceChangeEventListener>();
proofChangeListeners = new ArrayList<ProofChangeEventListener>();
timeChangedListeners = new ArrayList<TimeChangedEventListener>();
Expand All @@ -89,7 +91,7 @@ public BitNymWallet() {
wallet = null;
walletFile = new File("./wallet.wa");
if(!walletFile.exists()) {
wallet = new Wallet(params);
wallet = new Wallet(context);
try {
wallet.saveToFile(walletFile);
} catch (IOException e) {
Expand Down Expand Up @@ -295,7 +297,13 @@ private void insertIdentifierIntoFilter() {
filter.insert(BroadcastAnnouncement.magicNumber);
downloadpeer.setBloomFilter(filter);
}


private void mixAbort() {
for (MixAbortEventListener listener : m.getMaListeners()) {
listener.onMixAborted();
}
}



public void exit() {
Expand Down Expand Up @@ -392,9 +400,11 @@ public void mixWith(String onionAdress, int lockTime) {
public void mixWithNewestBroadcast(int lockTime) throws NoBroadcastAnnouncementsException {
if(pm.isEmpty()) {
System.out.println("mixWithNewestBroadcast aborted: No own proof message");
mixAbort();
return;
} else if(pm.getLastTransaction().getConfidence().getDepthInBlocks() == 0) {
System.out.println("mixWithNewestBroadcast aborted: Last transaction is not yet verified " + pm.getLastTransaction().toString());
mixAbort();
return;
} else {
m.setBroadcastAnnouncement(mpd.getNewestBroadcast());
Expand All @@ -409,9 +419,11 @@ public void mixWithRandomBroadcast(int lockTime) throws NoBroadcastAnnouncements
//if(!mpd.hasBroadcasts() || pm.isEmpty()) {
if(pm.isEmpty()) {
System.out.println("mixWithRandomBroadcast aborted: No own proof message");
mixAbort();
return;
} else if(pm.getLastTransaction().getConfidence().getDepthInBlocks() == 0) {
System.out.println("mixWithRandomBroadcast aborted: Last transaction is not yet verified " + pm.getLastTransaction().toString());
mixAbort();
return;
} else {
m.setBroadcastAnnouncement(mpd.getRandomBroadcast());
Expand Down Expand Up @@ -460,6 +472,10 @@ public void addBroadcastAnnouncementChangeEventListener(BroadcastAnnouncementCha
public void addMixFinishedEventListener(MixFinishedEventListener listener) {
m.addMixFinishedEventListener(listener);
}

public void addMixAbortEventListener(MixAbortEventListener listener) {m.addMixAbortEventListener(listener);}
PhilbertM marked this conversation as resolved.
Show resolved Hide resolved

public void addMixPassiveEventListener(MixPassiveEventListener listener) {m.addMixPassiveEventListener(listener);}

public void removeProofConfidenceChangeEventListener(ProofConfidenceChangeEventListener listener) {
pm.removeProofConfidenceChangeEventListener(listener);
Expand All @@ -479,6 +495,17 @@ public void removeMixFinishedEventListener(MixFinishedEventListener listener) {
m.removeMixFinishedEventListener(listener);
}

public void removeMixAbortEventListener(MixAbortEventListener listener) {
m.removeMixAbortEventListener(listener);
}

public void removeMixPassiveEventListener(MixPassiveEventListener listener) {
m.removeMixPassiveEventListener(listener);
}

public void mixPassive(byte[] arg0, Identifier arg1) {
m.passiveMix(arg0,arg1);
}


public Transaction getLastTransaction() {
Expand Down Expand Up @@ -628,4 +655,16 @@ public ECKey getPseudonym() {
return wallet.findKeyFromPubHash(pm.getScriptPair().getPubKeyHash());
}

/**
* Only for testing
* @return ptp identifier
*/
public Identifier getIdentifier() {
if (ptp.isInitialized()) {
return ptp.getIdentifier();
} else {
return null;
}
}

}
7 changes: 7 additions & 0 deletions src/bitnymWallet/MixAbortEventListener.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package bitnymWallet;

public interface MixAbortEventListener {

public void onMixAborted();

}
9 changes: 9 additions & 0 deletions src/bitnymWallet/MixPassiveEventListener.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package bitnymWallet;

import edu.kit.tm.ptp.Identifier;

public interface MixPassiveEventListener {

public void onMixPassive(byte[] arg0, Identifier arg1);

}
57 changes: 52 additions & 5 deletions src/bitnymWallet/Mixer.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
import org.bitcoinj.wallet.Wallet;

import edu.kit.tm.ptp.Identifier;
import edu.kit.tm.ptp.PTP;
import edu.kit.tm.ptp.ReceiveListener;
import edu.kit.tm.ptp.SendListener;

Expand All @@ -44,6 +43,8 @@ public class Mixer {
private PeerGroup pg;
private BlockChain bc;
private List<MixFinishedEventListener> mfListeners;
private List<MixAbortEventListener> maListeners;
private List<MixPassiveEventListener> mpListeners;
private int lockTime;
private boolean successful;

Expand All @@ -58,6 +59,8 @@ public Mixer(BitNymWallet wallet, BroadcastAnnouncement bca, ProofMessage pm, Wa
this.pg = pg;
this.bc = bc;
this.mfListeners = new ArrayList<MixFinishedEventListener>();
this.maListeners = new ArrayList<MixAbortEventListener>();
this.mpListeners = new ArrayList<MixPassiveEventListener>();
this.lockTime = 0;
}

Expand All @@ -72,6 +75,8 @@ public Mixer(BitNymWallet wallet, String onionAddress, ProofMessage pm, Wallet w
this.pg = pg;
this.bc = bc;
this.mfListeners = new ArrayList<MixFinishedEventListener>();
this.maListeners = new ArrayList<MixAbortEventListener>();
this.mpListeners = new ArrayList<MixPassiveEventListener>();
this.lockTime = 0;

}
Expand All @@ -85,10 +90,13 @@ public Mixer(BitNymWallet wallet, ProofMessage pm, Wallet w, NetworkParameters p
this.pg = pg;
this.bc = bc;
this.mfListeners = new ArrayList<MixFinishedEventListener>();
this.maListeners = new ArrayList<MixAbortEventListener>();
this.mpListeners = new ArrayList<MixPassiveEventListener>();
this.lockTime = 0;
}

public void passiveMix(byte[] arg0) {
public void passiveMix(byte[] arg0, Identifier arg1) {
mixPartnerAdress = arg1;
wallet.ptp.setSendListener(new SendListener() {

@Override
Expand All @@ -112,6 +120,7 @@ public void messageSent(long id, Identifier destination, State state) {
this.partnerProof = (ProofMessage) deserialize(arg0);
if(!partnerProof.isProbablyValid(params, bc, pg)) {
System.out.println("proof of mix partner is invalid");
mixAbort();
return;
}
//send own proof to partner
Expand All @@ -126,6 +135,7 @@ public void messageReceived(byte[] arg0, Identifier arg1) {
final Transaction rcvdTx = deserializeTransaction(arg0);
if(!checkTxInputIsFromProof(rcvdTx, 0)) {
System.out.println("checktxinput is from proof failed");
mixAbort();
return;
}
final int outputOrder = rcvdTx.getOutputs().size();
Expand Down Expand Up @@ -267,6 +277,7 @@ public void messageReceived(byte[] arg0, Identifier arg1) {
System.out.println("check partner proof");
if(!partnerProof.isProbablyValid(params, bc, pg)) {
System.out.println("proof of mix partner is invalid, abort");
mixAbort();
return;
}
challengeResponse();
Expand Down Expand Up @@ -299,6 +310,17 @@ private void ping() {
// }
}
}
private void mixAbort() {
for (MixAbortEventListener listener : maListeners) {
listener.onMixAborted();
}
}

private void mixPassiveStarted(byte[] arg0, Identifier arg1) {
for (MixPassiveEventListener listener : mpListeners) {
listener.onMixPassive(arg0,arg1);
}
}


private void challengeResponse() {
Expand Down Expand Up @@ -352,8 +374,8 @@ public void listenForMix() {

@Override
public void messageReceived(byte[] arg0, Identifier arg1) {
mixPartnerAdress = arg1;
passiveMix(arg0);
System.out.println("Mix request received, mixing passive");
mixPassiveStarted(arg0, arg1);
}
});

Expand Down Expand Up @@ -486,7 +508,8 @@ public void messageReceived(byte[] arg0, Identifier arg1) {
@Override
public void messageReceived(byte[] arg0, Identifier arg1) {
// add our output, sign and send to partner then
final Transaction penFinalTx = deserializeTransaction(arg0);
try {
final Transaction penFinalTx = deserializeTransaction(arg0);
if(!checkTxInputIsFromProof(penFinalTx, 1)) {
System.out.println("checktxinputisfromproof failed");
}
Expand All @@ -511,6 +534,11 @@ public void messageReceived(byte[] arg0, Identifier arg1) {
});
wallet.ptp.sendMessage(penFinalTx.bitcoinSerialize(), mixPartnerAdress);
System.out.println("done");
} catch (Exception e) {
e.printStackTrace();
mixAbort();
return;
}
}
});
this.wallet.ptp.sendMessage(serializedTx, this.mixPartnerAdress);
Expand Down Expand Up @@ -653,6 +681,22 @@ public void addMixFinishedEventListener(MixFinishedEventListener listener) {
public void removeMixFinishedEventListener(MixFinishedEventListener listener) {
mfListeners.remove(listener);
}

public void addMixAbortEventListener(MixAbortEventListener listener) {
maListeners.add(listener);
}

public void removeMixAbortEventListener(MixAbortEventListener listener) {
maListeners.remove(listener);
}

public void addMixPassiveEventListener(MixPassiveEventListener listener) {
mpListeners.add(listener);
}

public void removeMixPassiveEventListener(MixPassiveEventListener listener) {
mpListeners.remove(listener);
}

public void setMixPartnerAdress(String onion) {
this.mixPartnerAdress = new Identifier(onion);
Expand All @@ -666,6 +710,9 @@ public void setBroadcastAnnouncement(BroadcastAnnouncement bca) {
this.bca = bca;
this.mixPartnerAdress = new Identifier(bca.getOnionAdress() + ".onion");
}
public List<MixAbortEventListener> getMaListeners() {
return maListeners;
}


public void setLockTime(int lockTime) {
Expand Down