Skip to content

Commit

Permalink
fix (coinjoin): remove ProTxToOutPoint and update CoinJoinQueue and C…
Browse files Browse the repository at this point in the history
…oinJoinBroadcastTx
  • Loading branch information
HashEngineering committed Dec 21, 2023
1 parent af78e03 commit c6527ce
Show file tree
Hide file tree
Showing 7 changed files with 59 additions and 747 deletions.
56 changes: 3 additions & 53 deletions core/src/main/java/org/bitcoinj/coinjoin/CoinJoinBroadcastTx.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
package org.bitcoinj.coinjoin;

import com.google.common.annotations.VisibleForTesting;
import org.bitcoinj.coinjoin.utils.ProTxToOutpoint;
import org.bitcoinj.core.Context;
import org.bitcoinj.core.MasternodeSignature;
import org.bitcoinj.core.Message;
Expand Down Expand Up @@ -47,8 +46,6 @@ public class CoinJoinBroadcastTx extends Message {
private static final Logger log = LoggerFactory.getLogger(CoinJoinQueue.class);

private Transaction tx;
@Deprecated
private TransactionOutPoint masternodeOutpoint;
private Sha256Hash proTxHash;
private MasternodeSignature signature;
private long signatureTime;
Expand All @@ -61,34 +58,6 @@ public CoinJoinBroadcastTx(NetworkParameters params, byte[] payload, int protoco
super(params, payload, 0, protocolVersion);
}

@Deprecated
public CoinJoinBroadcastTx(
NetworkParameters params,
Transaction tx,
TransactionOutPoint masternodeOutpoint,
MasternodeSignature signature,
long signatureTime
) {
super(params);
this.tx = tx;
this.masternodeOutpoint = masternodeOutpoint;
this.signature = signature;
this.signatureTime = signatureTime;
}

@Deprecated
public CoinJoinBroadcastTx(
NetworkParameters params,
Transaction tx,
TransactionOutPoint masternodeOutpoint,
long signatureTime
) {
super(params);
this.tx = tx;
this.masternodeOutpoint = masternodeOutpoint;
this.signatureTime = signatureTime;
}

public CoinJoinBroadcastTx(
NetworkParameters params,
Transaction tx,
Expand All @@ -105,12 +74,7 @@ public CoinJoinBroadcastTx(
protected void parse() throws ProtocolException {
tx = new Transaction(params, payload, cursor);
cursor += tx.getMessageSize();
if (protocolVersion >= params.getProtocolVersionNum(NetworkParameters.ProtocolVersion.COINJOIN_PROTX_HASH)) {
proTxHash = readHash();
} else {
masternodeOutpoint = new TransactionOutPoint(params, payload, cursor);
cursor += masternodeOutpoint.getMessageSize();
}
proTxHash = readHash();
signature = new MasternodeSignature(params, payload, cursor);
cursor += signature.getMessageSize();
signatureTime = readInt64();
Expand All @@ -121,11 +85,7 @@ protected void parse() throws ProtocolException {
@Override
protected void bitcoinSerializeToStream(OutputStream stream) throws IOException {
tx.bitcoinSerialize(stream);
if (protocolVersion >= params.getProtocolVersionNum(NetworkParameters.ProtocolVersion.COINJOIN_PROTX_HASH)) {
stream.write(proTxHash.getReversedBytes());
} else {
masternodeOutpoint.bitcoinSerialize(stream);
}
stream.write(proTxHash.getReversedBytes());
signature.bitcoinSerialize(stream);
Utils.int64ToByteStreamLE(signatureTime, stream);
}
Expand All @@ -134,9 +94,7 @@ public Sha256Hash getSignatureHash() {
try {
ByteArrayOutputStream bos = new UnsafeByteArrayOutputStream();
tx.bitcoinSerialize(bos);
// this still requires the masternode output
if (masternodeOutpoint == null)
masternodeOutpoint = ProTxToOutpoint.getMasternodeOutpoint(proTxHash);
bos.write(proTxHash.getReversedBytes());
Utils.int64ToByteStreamLE(signatureTime, bos);

return Sha256Hash.twiceOf(bos.toByteArray());
Expand Down Expand Up @@ -175,14 +133,6 @@ public Transaction getTx() {
return tx;
}

@Deprecated
public TransactionOutPoint getMasternodeOutpoint() {
if (masternodeOutpoint == null) {
masternodeOutpoint = ProTxToOutpoint.getMasternodeOutpoint(proTxHash);
}
return masternodeOutpoint;
}

public Sha256Hash getProTxHash() {
return proTxHash;
}
Expand Down
73 changes: 11 additions & 62 deletions core/src/main/java/org/bitcoinj/coinjoin/CoinJoinQueue.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,11 @@
package org.bitcoinj.coinjoin;

import com.google.common.annotations.VisibleForTesting;
import org.bitcoinj.coinjoin.utils.ProTxToOutpoint;
import org.bitcoinj.core.MasternodeSignature;
import org.bitcoinj.core.Message;
import org.bitcoinj.core.NetworkParameters;
import org.bitcoinj.core.ProtocolException;
import org.bitcoinj.core.Sha256Hash;
import org.bitcoinj.core.TransactionOutPoint;
import org.bitcoinj.core.UnsafeByteArrayOutputStream;
import org.bitcoinj.core.Utils;
import org.bitcoinj.crypto.BLSPublicKey;
Expand All @@ -45,7 +43,6 @@ public class CoinJoinQueue extends Message {
private static final Logger log = LoggerFactory.getLogger(CoinJoinQueue.class);

private int denomination;
private TransactionOutPoint masternodeOutpoint;
private Sha256Hash proTxHash;
private long time;
private boolean ready; // Ready to submit
Expand All @@ -57,65 +54,38 @@ public CoinJoinQueue(NetworkParameters params, byte[] payload, int protocolVersi
super(params, payload, 0, protocolVersion);
}

public CoinJoinQueue(
NetworkParameters params,
int denomination,
TransactionOutPoint masternodeOutpoint,
long time,
boolean ready,
MasternodeSignature signature
) {
super(params);
this.denomination = denomination;
this.masternodeOutpoint = masternodeOutpoint;
this.time = time;
this.ready = ready;
this.signature = signature;
this.protocolVersion = params.getProtocolVersionNum(NetworkParameters.ProtocolVersion.BLS_LEGACY);
}

@Deprecated
public CoinJoinQueue(
NetworkParameters params,
int denomination,
TransactionOutPoint masternodeOutpoint,
Sha256Hash proTxHash,
long time,
boolean ready) {

super(params);
this.denomination = denomination;
this.masternodeOutpoint = masternodeOutpoint;
this.proTxHash = proTxHash;
this.time = time;
this.ready = ready;
this.signature = null;
this.protocolVersion = params.getProtocolVersionNum(NetworkParameters.ProtocolVersion.BLS_LEGACY);
this.protocolVersion = params.getProtocolVersionNum(NetworkParameters.ProtocolVersion.CURRENT);
}

public CoinJoinQueue(
NetworkParameters params,
int denomination,
Sha256Hash proTxHash,
long time,
boolean ready) {
boolean ready,
MasternodeSignature signature) {

super(params);
this.denomination = denomination;
this.proTxHash = proTxHash;
this.time = time;
this.ready = ready;
this.signature = null;
this.protocolVersion = params.getProtocolVersionNum(NetworkParameters.ProtocolVersion.CURRENT);
this(params, denomination, proTxHash, time, ready);
this.signature = signature;
}

@Override
protected void parse() throws ProtocolException {
denomination = (int)readUint32();
if (protocolVersion >= params.getProtocolVersionNum(NetworkParameters.ProtocolVersion.COINJOIN_PROTX_HASH)) {
proTxHash = readHash();
} else {
masternodeOutpoint = new TransactionOutPoint(params, payload, cursor);
cursor += masternodeOutpoint.getMessageSize();
}
proTxHash = readHash();
time = readInt64();
ready = readBytes(1)[0] == 1;
signature = new MasternodeSignature(params, payload, cursor);
Expand All @@ -127,11 +97,7 @@ protected void parse() throws ProtocolException {
@Override
protected void bitcoinSerializeToStream(OutputStream stream) throws IOException {
Utils.uint32ToByteStreamLE(denomination, stream);
if (protocolVersion >= params.getProtocolVersionNum(NetworkParameters.ProtocolVersion.COINJOIN_PROTX_HASH)) {
stream.write(proTxHash.getReversedBytes());
} else {
masternodeOutpoint.bitcoinSerialize(stream);
}
stream.write(proTxHash.getReversedBytes());
Utils.int64ToByteStreamLE(time, stream);
stream.write(ready ? 1 : 0);
signature.bitcoinSerialize(stream);
Expand All @@ -141,25 +107,16 @@ public Sha256Hash getSignatureHash() {
try {
ByteArrayOutputStream bos = new UnsafeByteArrayOutputStream();
Utils.uint32ToByteStreamLE(denomination, bos);

// this still requires the masternode output
if (masternodeOutpoint == null)
masternodeOutpoint = ProTxToOutpoint.getMasternodeOutpoint(proTxHash);
masternodeOutpoint.bitcoinSerialize(bos);
bos.write(proTxHash.getReversedBytes());
Utils.int64ToByteStreamLE(time, bos);
bos.write(ready ? 1 : 0);

return Sha256Hash.twiceOf(bos.toByteArray());
} catch (IOException e) {
throw new RuntimeException(e);
}
}

public boolean checkSignature(BLSPublicKey pubKey) {
// return true until signature hash uses the proTxHash
if (masternodeOutpoint == null)
return true;

Sha256Hash hash = getSignatureHash();

// use the currently active scheme
Expand All @@ -176,7 +133,7 @@ public boolean checkSignature(BLSPublicKey pubKey) {
@VisibleForTesting
public boolean sign(BLSSecretKey blsKeyOperator) {
Sha256Hash hash = getSignatureHash();
BLSSignature sig = blsKeyOperator.Sign(hash);
BLSSignature sig = blsKeyOperator.sign(hash);
if (!sig.isValid()) {
return false;
}
Expand All @@ -202,14 +159,6 @@ public int getDenomination() {
return denomination;
}

@Deprecated
public TransactionOutPoint getMasternodeOutpoint() {
if (masternodeOutpoint == null) {
masternodeOutpoint = ProTxToOutpoint.getMasternodeOutpoint(proTxHash);
}
return masternodeOutpoint;
}

public long getTime() {
return time;
}
Expand Down
Loading

0 comments on commit c6527ce

Please sign in to comment.