Skip to content

Commit

Permalink
Merge pull request #4294 from sqrrm/add-multiple-fee-receivers
Browse files Browse the repository at this point in the history
Add multiple fee receivers
  • Loading branch information
ripcurlx authored Jun 25, 2020
2 parents 745178e + 9fcc4da commit 38f6af2
Show file tree
Hide file tree
Showing 12 changed files with 230 additions and 17 deletions.
20 changes: 16 additions & 4 deletions core/src/main/java/bisq/core/filter/Filter.java
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,10 @@ public final class Filter implements ProtectedStoragePayload, ExpirablePayload {
@Nullable
private final List<String> bannedSignerPubKeys;

// added in v1.3.2
@Nullable
private final List<String> btcFeeReceiverAddresses;

public Filter(List<String> bannedOfferIds,
List<String> bannedNodeAddress,
List<PaymentAccountFilter> bannedPaymentAccounts,
Expand All @@ -120,7 +124,8 @@ public Filter(List<String> bannedOfferIds,
@Nullable String disableTradeBelowVersion,
@Nullable List<String> mediators,
@Nullable List<String> refundAgents,
@Nullable List<String> bannedSignerPubKeys) {
@Nullable List<String> bannedSignerPubKeys,
@Nullable List<String> btcFeeReceiverAddresses) {
this.bannedOfferIds = bannedOfferIds;
this.bannedNodeAddress = bannedNodeAddress;
this.bannedPaymentAccounts = bannedPaymentAccounts;
Expand All @@ -137,6 +142,7 @@ public Filter(List<String> bannedOfferIds,
this.mediators = mediators;
this.refundAgents = refundAgents;
this.bannedSignerPubKeys = bannedSignerPubKeys;
this.btcFeeReceiverAddresses = btcFeeReceiverAddresses;
}


Expand All @@ -163,7 +169,8 @@ public Filter(List<String> bannedOfferIds,
@Nullable Map<String, String> extraDataMap,
@Nullable List<String> mediators,
@Nullable List<String> refundAgents,
@Nullable List<String> bannedSignerPubKeys) {
@Nullable List<String> bannedSignerPubKeys,
@Nullable List<String> btcFeeReceiverAddresses) {
this(bannedOfferIds,
bannedNodeAddress,
bannedPaymentAccounts,
Expand All @@ -179,7 +186,8 @@ public Filter(List<String> bannedOfferIds,
disableTradeBelowVersion,
mediators,
refundAgents,
bannedSignerPubKeys);
bannedSignerPubKeys,
btcFeeReceiverAddresses);
this.signatureAsBase64 = signatureAsBase64;
this.ownerPubKeyBytes = ownerPubKeyBytes;
this.extraDataMap = ExtraDataMapValidator.getValidatedExtraDataMap(extraDataMap);
Expand Down Expand Up @@ -215,6 +223,7 @@ public protobuf.StoragePayload toProtoMessage() {
Optional.ofNullable(mediators).ifPresent(builder::addAllMediators);
Optional.ofNullable(refundAgents).ifPresent(builder::addAllRefundAgents);
Optional.ofNullable(bannedSignerPubKeys).ifPresent(builder::addAllBannedSignerPubKeys);
Optional.ofNullable(btcFeeReceiverAddresses).ifPresent(builder::addAllBtcFeeReceiverAddresses);

return protobuf.StoragePayload.newBuilder().setFilter(builder).build();
}
Expand All @@ -241,7 +250,9 @@ public static Filter fromProto(protobuf.Filter proto) {
CollectionUtils.isEmpty(proto.getMediatorsList()) ? null : new ArrayList<>(proto.getMediatorsList()),
CollectionUtils.isEmpty(proto.getRefundAgentsList()) ? null : new ArrayList<>(proto.getRefundAgentsList()),
CollectionUtils.isEmpty(proto.getBannedSignerPubKeysList()) ?
null : new ArrayList<>(proto.getBannedSignerPubKeysList()));
null : new ArrayList<>(proto.getBannedSignerPubKeysList()),
CollectionUtils.isEmpty(proto.getBtcFeeReceiverAddressesList()) ? null :
new ArrayList<>(proto.getBtcFeeReceiverAddressesList()));
}


Expand Down Expand Up @@ -281,6 +292,7 @@ public String toString() {
",\n mediators=" + mediators +
",\n refundAgents=" + refundAgents +
",\n bannedSignerPubKeys=" + bannedSignerPubKeys +
",\n btcFeeReceiverAddresses=" + btcFeeReceiverAddresses +
"\n}";
}
}
7 changes: 6 additions & 1 deletion core/src/main/java/bisq/core/offer/OpenOfferManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import bisq.core.dao.DaoFacade;
import bisq.core.exceptions.TradePriceOutOfToleranceException;
import bisq.core.locale.Res;
import bisq.core.filter.FilterManager;
import bisq.core.offer.availability.DisputeAgentSelection;
import bisq.core.offer.messages.OfferAvailabilityRequest;
import bisq.core.offer.messages.OfferAvailabilityResponse;
Expand Down Expand Up @@ -111,6 +112,7 @@ public class OpenOfferManager implements PeerManager.Listener, DecryptedDirectMe
private final MediatorManager mediatorManager;
private final RefundAgentManager refundAgentManager;
private final DaoFacade daoFacade;
private final FilterManager filterManager;
private final Storage<TradableList<OpenOffer>> openOfferTradableListStorage;
private final Map<String, OpenOffer> offersToBeEdited = new HashMap<>();
private boolean stopped;
Expand Down Expand Up @@ -139,6 +141,7 @@ public OpenOfferManager(CreateOfferService createOfferService,
MediatorManager mediatorManager,
RefundAgentManager refundAgentManager,
DaoFacade daoFacade,
FilterManager filterManager,
Storage<TradableList<OpenOffer>> storage) {
this.createOfferService = createOfferService;
this.keyRing = keyRing;
Expand All @@ -156,6 +159,7 @@ public OpenOfferManager(CreateOfferService createOfferService,
this.mediatorManager = mediatorManager;
this.refundAgentManager = refundAgentManager;
this.daoFacade = daoFacade;
this.filterManager = filterManager;

openOfferTradableListStorage = storage;

Expand Down Expand Up @@ -361,7 +365,8 @@ public void placeOffer(Offer offer,
arbitratorManager,
tradeStatisticsManager,
daoFacade,
user);
user,
filterManager);
PlaceOfferProtocol placeOfferProtocol = new PlaceOfferProtocol(
model,
transaction -> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import bisq.core.btc.wallet.BtcWalletService;
import bisq.core.btc.wallet.TradeWalletService;
import bisq.core.dao.DaoFacade;
import bisq.core.filter.FilterManager;
import bisq.core.offer.Offer;
import bisq.core.offer.OfferBookService;
import bisq.core.support.dispute.arbitration.arbitrator.ArbitratorManager;
Expand Down Expand Up @@ -51,6 +52,8 @@ public class PlaceOfferModel implements Model {
private final TradeStatisticsManager tradeStatisticsManager;
private final DaoFacade daoFacade;
private final User user;
@Getter
private final FilterManager filterManager;

// Mutable
@Setter
Expand All @@ -68,7 +71,8 @@ public PlaceOfferModel(Offer offer,
ArbitratorManager arbitratorManager,
TradeStatisticsManager tradeStatisticsManager,
DaoFacade daoFacade,
User user) {
User user,
FilterManager filterManager) {
this.offer = offer;
this.reservedFundsForOffer = reservedFundsForOffer;
this.useSavingsWallet = useSavingsWallet;
Expand All @@ -80,6 +84,7 @@ public PlaceOfferModel(Offer offer,
this.tradeStatisticsManager = tradeStatisticsManager;
this.daoFacade = daoFacade;
this.user = user;
this.filterManager = filterManager;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,10 @@
import bisq.core.btc.wallet.TxBroadcaster;
import bisq.core.btc.wallet.WalletService;
import bisq.core.dao.exceptions.DaoDisabledException;
import bisq.core.dao.governance.param.Param;
import bisq.core.dao.state.model.blockchain.TxType;
import bisq.core.offer.Offer;
import bisq.core.offer.placeoffer.PlaceOfferModel;
import bisq.core.util.FeeReceiverSelector;

import bisq.common.UserThread;
import bisq.common.taskrunner.Task;
Expand Down Expand Up @@ -65,7 +65,8 @@ protected void run() {
Address changeAddress = walletService.getFreshAddressEntry().getAddress();

TradeWalletService tradeWalletService = model.getTradeWalletService();
String feeReceiver = model.getDaoFacade().getParamValue(Param.RECIPIENT_BTC_ADDRESS);

String feeReceiver = FeeReceiverSelector.getAddress(model.getDaoFacade(), model.getFilterManager());

if (offer.isCurrencyForMakerFeeBtc()) {
tradeWalletService.createBtcTradingFeeTx(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@
import bisq.core.btc.wallet.TradeWalletService;
import bisq.core.btc.wallet.WalletService;
import bisq.core.dao.exceptions.DaoDisabledException;
import bisq.core.dao.governance.param.Param;
import bisq.core.trade.Trade;
import bisq.core.trade.protocol.tasks.TradeTask;
import bisq.core.util.FeeReceiverSelector;

import bisq.common.taskrunner.TaskRunner;

Expand Down Expand Up @@ -65,7 +65,9 @@ protected void run() {
Address changeAddress = changeAddressEntry.getAddress();
TradeWalletService tradeWalletService = processModel.getTradeWalletService();
Transaction transaction;
String feeReceiver = processModel.getDaoFacade().getParamValue(Param.RECIPIENT_BTC_ADDRESS);

String feeReceiver = FeeReceiverSelector.getAddress(processModel.getDaoFacade(), processModel.getFilterManager());

if (trade.isCurrencyForTakerFeeBtc()) {
transaction = tradeWalletService.createBtcTradingFeeTx(
fundingAddress,
Expand Down
78 changes: 78 additions & 0 deletions core/src/main/java/bisq/core/util/FeeReceiverSelector.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
/*
* This file is part of Bisq.
*
* Bisq is free software: you can redistribute it and/or modify it
* under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or (at
* your option) any later version.
*
* Bisq is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public
* License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with Bisq. If not, see <http://www.gnu.org/licenses/>.
*/

package bisq.core.util;

import bisq.core.dao.DaoFacade;
import bisq.core.dao.governance.param.Param;
import bisq.core.filter.FilterManager;

import org.bitcoinj.core.Coin;

import com.google.common.annotations.VisibleForTesting;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.Random;

import lombok.extern.slf4j.Slf4j;

@Slf4j
public class FeeReceiverSelector {
public static String getAddress(DaoFacade daoFacade, FilterManager filterManager) {
return getAddress(daoFacade, filterManager, new Random());
}

@VisibleForTesting
static String getAddress(DaoFacade daoFacade, FilterManager filterManager, Random rnd) {
List<String> feeReceivers = Optional.ofNullable(filterManager.getFilter())
.flatMap(f -> Optional.ofNullable(f.getBtcFeeReceiverAddresses()))
.orElse(List.of());

List<Long> amountList = new ArrayList<>();
List<String> receiverAddressList = new ArrayList<>();

feeReceivers.forEach(e -> {
try {
String[] tokens = e.split("#");
amountList.add(Coin.parseCoin(tokens[1]).longValue()); // total amount the victim should receive
receiverAddressList.add(tokens[0]); // victim's receiver address
} catch (RuntimeException ignore) {
// If input format is not as expected we ignore entry
}
});

if (!amountList.isEmpty()) {
return receiverAddressList.get(weightedSelection(amountList, rnd));
}

// We keep default value as fallback in case no filter value is available or user has old version.
return daoFacade.getParamValue(Param.RECIPIENT_BTC_ADDRESS);
}

@VisibleForTesting
static int weightedSelection(List<Long> weights, Random rnd) {
long sum = weights.stream().mapToLong(n -> n).sum();
long target = rnd.longs(0, sum).findFirst().orElseThrow();
int i;
for (i = 0; i < weights.size() && target >= 0; i++) {
target -= weights.get(i);
}
return i - 1;
}
}
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 @@ -2429,6 +2429,7 @@ filterWindow.disableDaoBelowVersion=Min. version required for DAO
filterWindow.disableTradeBelowVersion=Min. version required for trading
filterWindow.add=Add filter
filterWindow.remove=Remove filter
filterWindow.btcFeeReceiverAddresses=BTC fee receiver addresses

offerDetailsWindow.minBtcAmount=Min. BTC amount
offerDetailsWindow.min=(min. {0})
Expand Down
8 changes: 3 additions & 5 deletions core/src/test/java/bisq/core/offer/OpenOfferManagerTest.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package bisq.core.offer;

import bisq.core.trade.TradableList;

import bisq.network.p2p.P2PService;
import bisq.network.p2p.peers.PeerManager;

Expand Down Expand Up @@ -47,7 +45,7 @@ public void testStartEditOfferForActiveOffer() {
final OpenOfferManager manager = new OpenOfferManager(null, null, null, p2PService,
null, null, null, offerBookService,
null, null, null,
null, null, null, null, null,
null, null, null, null, null, null,
new Storage<>(storageDir, null, corruptedDatabaseFilesHandler));

AtomicBoolean startEditOfferSuccessful = new AtomicBoolean(false);
Expand Down Expand Up @@ -83,7 +81,7 @@ public void testStartEditOfferForDeactivatedOffer() throws IOException {
final OpenOfferManager manager = new OpenOfferManager(null, null, null, p2PService,
null, null, null, offerBookService,
null, null, null,
null, null, null, null, null,
null, null, null, null, null, null,
new Storage<>(storageDir, null, corruptedDatabaseFilesHandler));

AtomicBoolean startEditOfferSuccessful = new AtomicBoolean(false);
Expand Down Expand Up @@ -111,7 +109,7 @@ public void testStartEditOfferForOfferThatIsCurrentlyEdited() {
final OpenOfferManager manager = new OpenOfferManager(null, null, null, p2PService,
null, null, null, offerBookService,
null, null, null,
null, null, null, null, null,
null, null, null, null, null, null,
new Storage<>(storageDir, null, corruptedDatabaseFilesHandler));

AtomicBoolean startEditOfferSuccessful = new AtomicBoolean(false);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@

import org.junit.Ignore;

@SuppressWarnings("UnusedAssignment")
public class UserPayloadModelVOTest {
@Ignore("TODO InvalidKeySpecException at bisq.common.crypto.Sig.getPublicKeyFromBytes(Sig.java:135)")
public void testRoundtrip() {
Expand Down Expand Up @@ -59,6 +58,7 @@ public void testRoundtripFull() {
null,
Lists.newArrayList(),
Lists.newArrayList(),
Lists.newArrayList(),
Lists.newArrayList()));
vo.setRegisteredArbitrator(ArbitratorTest.getArbitratorMock());
vo.setRegisteredMediator(MediatorTest.getMediatorMock());
Expand Down
Loading

0 comments on commit 38f6af2

Please sign in to comment.