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

Provide ASIC resistant PoW scheme for BSQ swaps #5858

Merged
merged 15 commits into from
Dec 7, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ static CompletableFuture<ProofOfWork> mint(byte[] payload,
result = toSha256Hash(payload, challenge, ++counter);
}
while (!testDifficulty.test(result, difficulty));
ProofOfWork proofOfWork = new ProofOfWork(payload, counter, challenge, difficulty, System.currentTimeMillis() - ts);
ProofOfWork proofOfWork = new ProofOfWork(payload, counter, challenge, difficulty, System.currentTimeMillis() - ts, 0);
log.info("Completed minting proofOfWork: {}", proofOfWork);
return proofOfWork;
});
Expand Down
36 changes: 21 additions & 15 deletions common/src/main/java/bisq/common/crypto/ProofOfWork.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,51 +38,55 @@ public final class ProofOfWork implements NetworkPayload {
private final byte[] difficulty;
@Getter
private final long duration;
@Getter
private final int version;

public ProofOfWork(byte[] payload,
long counter,
byte[] challenge,
int difficulty,
long duration) {
long duration,
int version) {
this(payload,
counter,
challenge,
BigInteger.valueOf(difficulty).toByteArray(),
duration);
duration,
version);
}

public ProofOfWork(byte[] payload,
long counter,
byte[] challenge,
BigInteger difficulty,
long duration) {
long duration,
int version) {
this(payload,
counter,
challenge,
difficulty.toByteArray(),
duration);
duration,
version);
}

public ProofOfWork(byte[] payload,
long counter,
byte[] challenge,
byte[] difficulty,
long duration) {
private ProofOfWork(byte[] payload,
long counter,
byte[] challenge,
byte[] difficulty,
long duration,
int version) {
this.payload = payload;
this.counter = counter;
this.challenge = challenge;
this.difficulty = difficulty;
this.duration = duration;
this.version = version;
}

public int getNumLeadingZeros() {
return new BigInteger(difficulty).intValue();
}

public BigInteger getTarget() {
return new BigInteger(difficulty);
}


///////////////////////////////////////////////////////////////////////////////////////////
// PROTO BUFFER
Expand All @@ -96,6 +100,7 @@ public protobuf.ProofOfWork toProtoMessage() {
.setChallenge(ByteString.copyFrom(challenge))
.setDifficulty(ByteString.copyFrom(difficulty))
.setDuration(duration)
.setVersion(version)
.build();
}

Expand All @@ -105,7 +110,8 @@ public static ProofOfWork fromProto(protobuf.ProofOfWork proto) {
proto.getCounter(),
proto.getChallenge().toByteArray(),
proto.getDifficulty().toByteArray(),
proto.getDuration()
proto.getDuration(),
proto.getVersion()
);
}

Expand All @@ -115,8 +121,8 @@ public String toString() {
return "ProofOfWork{" +
",\r\n counter=" + counter +
",\r\n numLeadingZeros=" + getNumLeadingZeros() +
",\r\n target=" + getTarget() +
",\r\n duration=" + duration +
",\r\n version=" + version +
"\r\n}";
}
}
11 changes: 11 additions & 0 deletions core/src/main/java/bisq/core/filter/Filter.java
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,8 @@ public final class Filter implements ProtectedStoragePayload, ExpirablePayload {
// Number of leading zeros for pow for BSQ swap offers. Difficulty of 8 requires 0.856 ms in average, 15 about 100 ms.
// See ProofOfWorkTest for more info.
private final int powDifficulty;
// Enabled PoW version numbers in reverse order of preference, starting with 0 for Hashcash.
private final List<Integer> enabledPowVersions;

// Added at v 1.8.0
// BSQ fee gets updated in proposals repo (e.g. https://github.com/bisq-network/proposals/issues/345)
Expand Down Expand Up @@ -148,6 +150,7 @@ static Filter cloneWithSig(Filter filter, String signatureAsBase64) {
filter.isDisableApi(),
filter.isDisablePowMessage(),
filter.getPowDifficulty(),
filter.getEnabledPowVersions(),
filter.getMakerFeeBtc(),
filter.getTakerFeeBtc(),
filter.getMakerFeeBsq(),
Expand Down Expand Up @@ -186,6 +189,7 @@ static Filter cloneWithoutSig(Filter filter) {
filter.isDisableApi(),
filter.isDisablePowMessage(),
filter.getPowDifficulty(),
filter.getEnabledPowVersions(),
filter.getMakerFeeBtc(),
filter.getTakerFeeBtc(),
filter.getMakerFeeBsq(),
Expand Down Expand Up @@ -219,6 +223,7 @@ public Filter(List<String> bannedOfferIds,
boolean disableApi,
boolean disablePowMessage,
int powDifficulty,
List<Integer> enabledPowVersions,
long makerFeeBtc,
long takerFeeBtc,
long makerFeeBsq,
Expand Down Expand Up @@ -253,6 +258,7 @@ public Filter(List<String> bannedOfferIds,
disableApi,
disablePowMessage,
powDifficulty,
enabledPowVersions,
makerFeeBtc,
takerFeeBtc,
makerFeeBsq,
Expand Down Expand Up @@ -295,6 +301,7 @@ public Filter(List<String> bannedOfferIds,
boolean disableApi,
boolean disablePowMessage,
int powDifficulty,
List<Integer> enabledPowVersions,
long makerFeeBtc,
long takerFeeBtc,
long makerFeeBsq,
Expand Down Expand Up @@ -329,6 +336,7 @@ public Filter(List<String> bannedOfferIds,
this.disableApi = disableApi;
this.disablePowMessage = disablePowMessage;
this.powDifficulty = powDifficulty;
this.enabledPowVersions = enabledPowVersions;
this.makerFeeBtc = makerFeeBtc;
this.takerFeeBtc = takerFeeBtc;
this.makerFeeBsq = makerFeeBsq;
Expand Down Expand Up @@ -376,6 +384,7 @@ public protobuf.StoragePayload toProtoMessage() {
.setDisableApi(disableApi)
.setDisablePowMessage(disablePowMessage)
.setPowDifficulty(powDifficulty)
.addAllEnabledPowVersions(enabledPowVersions)
.setMakerFeeBtc(makerFeeBtc)
.setTakerFeeBtc(takerFeeBtc)
.setMakerFeeBsq(makerFeeBsq)
Expand Down Expand Up @@ -422,6 +431,7 @@ public static Filter fromProto(protobuf.Filter proto) {
proto.getDisableApi(),
proto.getDisablePowMessage(),
proto.getPowDifficulty(),
proto.getEnabledPowVersionsList(),
proto.getMakerFeeBtc(),
proto.getTakerFeeBtc(),
proto.getMakerFeeBsq(),
Expand Down Expand Up @@ -473,6 +483,7 @@ public String toString() {
",\n disableApi=" + disableApi +
",\n disablePowMessage=" + disablePowMessage +
",\n powDifficulty=" + powDifficulty +
",\n enabledPowVersions=" + enabledPowVersions +
",\n makerFeeBtc=" + makerFeeBtc +
",\n takerFeeBtc=" + takerFeeBtc +
",\n makerFeeBsq=" + makerFeeBsq +
Expand Down
1 change: 1 addition & 0 deletions core/src/main/resources/i18n/displayStrings.properties
Original file line number Diff line number Diff line change
Expand Up @@ -2782,6 +2782,7 @@ filterWindow.disableApi=Disable API
filterWindow.disableMempoolValidation=Disable Mempool Validation
filterWindow.disablePowMessage=Disable messages requiring Proof of Work
filterWindow.powDifficulty=Proof of work difficulty (BSQ swap offers)
filterWindow.enabledPowVersions=Enabled proof of work versions (comma sep. integers)
filterWindow.makerFeeBtc=Min. BTC maker fee (e.g. 0.001)
filterWindow.takerFeeBtc=Min. BTC taker fee (e.g. 0.007)
filterWindow.makerFeeBsq=Min. BSQ maker fee (e.g. 15.14)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ public void testRoundtripFull() {
false,
false,
0,
Lists.newArrayList(),
0,
0,
0,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,7 @@ private static Filter filterWithReceivers(List<String> btcFeeReceiverAddresses)
false,
false,
0,
Lists.newArrayList(),
0,
0,
0,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,8 @@

import javax.inject.Named;

import org.apache.commons.lang3.StringUtils;
import com.google.common.base.Joiner;
import com.google.common.base.Splitter;

import javafx.scene.Scene;
import javafx.scene.control.Button;
Expand All @@ -55,9 +56,6 @@
import javafx.geometry.HPos;
import javafx.geometry.Insets;

import javafx.collections.FXCollections;

import java.util.Arrays;
import java.util.Collection;
import java.util.HashSet;
import java.util.List;
Expand Down Expand Up @@ -190,6 +188,8 @@ private void addContent() {
InputTextField powDifficultyTF = addInputTextField(gridPane, ++rowIndex,
Res.get("filterWindow.powDifficulty"));
powDifficultyTF.setText("0");
InputTextField enabledPowVersionsTF = addInputTextField(gridPane, ++rowIndex,
Res.get("filterWindow.enabledPowVersions"));
InputTextField makerFeeBtcTF = addInputTextField(gridPane, ++rowIndex,
Res.get("filterWindow.makerFeeBtc"));
InputTextField takerFeeBtcTF = addInputTextField(gridPane, ++rowIndex,
Expand Down Expand Up @@ -217,6 +217,7 @@ private void addContent() {
setupFieldFromList(btcNodesTF, filter.getBtcNodes());
setupFieldFromList(bannedPrivilegedDevPubKeysTF, filter.getBannedPrivilegedDevPubKeys());
setupFieldFromList(autoConfExplorersTF, filter.getBannedAutoConfExplorers());
setupFieldFromList(enabledPowVersionsTF, filter.getEnabledPowVersions());

preventPublicBtcNetworkCheckBox.setSelected(filter.isPreventPublicBtcNetwork());
disableDaoCheckBox.setSelected(filter.isDisableDao());
Expand Down Expand Up @@ -270,6 +271,7 @@ private void addContent() {
disableApiCheckBox.isSelected(),
disablePowMessage.isSelected(),
Integer.parseInt(powDifficultyTF.getText()),
readAsList(enabledPowVersionsTF).stream().map(Integer::parseInt).collect(Collectors.toList()),
ParsingUtils.parseToCoin(makerFeeBtcTF.getText(), btcFormatter).value,
ParsingUtils.parseToCoin(takerFeeBtcTF.getText(), btcFormatter).value,
ParsingUtils.parseToCoin(makerFeeBsqTF.getText(), bsqFormatter).value,
Expand Down Expand Up @@ -325,9 +327,10 @@ private void addDevFilter(Button removeFilterMessageButton, String privKeyString
hide();
}

private void setupFieldFromList(InputTextField field, Collection<String> values) {
if (values != null)
field.setText(String.join(", ", values));
private void setupFieldFromList(InputTextField field, Collection<?> values) {
if (values != null) {
field.setText(Joiner.on(", ").join(values));
}
}

private void setupFieldFromPaymentAccountFiltersList(InputTextField field, List<PaymentAccountFilter> values) {
Expand All @@ -349,11 +352,7 @@ private void setupFieldFromPaymentAccountFiltersList(InputTextField field, List<
}

private List<String> readAsList(InputTextField field) {
if (field.getText().isEmpty()) {
return FXCollections.emptyObservableList();
} else {
return Arrays.asList(StringUtils.deleteWhitespace(field.getText()).split(","));
}
return Splitter.on(',').trimResults().omitEmptyStrings().splitToList(field.getText());
}

private List<PaymentAccountFilter> readAsPaymentAccountFiltersList(InputTextField field) {
Expand Down
2 changes: 2 additions & 0 deletions proto/src/main/proto/pb.proto
Original file line number Diff line number Diff line change
Expand Up @@ -770,6 +770,7 @@ message Filter {
bool disable_mempool_validation = 28;
bool disable_pow_message = 29;
int32 pow_difficulty = 30;
repeated int32 enabled_pow_versions = 35;
stejbac marked this conversation as resolved.
Show resolved Hide resolved
int64 maker_fee_btc = 31;
int64 taker_fee_btc = 32;
int64 maker_fee_bsq = 33;
Expand Down Expand Up @@ -883,6 +884,7 @@ message ProofOfWork {
bytes challenge = 3;
bytes difficulty = 4;
int64 duration = 5;
int32 version = 6;
}

message AccountAgeWitness {
Expand Down