Merge pull request #4294 from sqrrm/add-multiple-fee-receivers

Add multiple fee receivers
This commit is contained in:
Christoph Atteneder 2020-06-25 13:23:53 +02:00 committed by GitHub
commit 38f6af26ee
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
12 changed files with 230 additions and 17 deletions

View File

@ -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,
@ -120,7 +124,8 @@ public final class Filter implements ProtectedStoragePayload, ExpirablePayload {
@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;
@ -137,6 +142,7 @@ public final class Filter implements ProtectedStoragePayload, ExpirablePayload {
this.mediators = mediators;
this.refundAgents = refundAgents;
this.bannedSignerPubKeys = bannedSignerPubKeys;
this.btcFeeReceiverAddresses = btcFeeReceiverAddresses;
}
@ -163,7 +169,8 @@ public final class Filter implements ProtectedStoragePayload, ExpirablePayload {
@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,
@ -179,7 +186,8 @@ public final class Filter implements ProtectedStoragePayload, ExpirablePayload {
disableTradeBelowVersion,
mediators,
refundAgents,
bannedSignerPubKeys);
bannedSignerPubKeys,
btcFeeReceiverAddresses);
this.signatureAsBase64 = signatureAsBase64;
this.ownerPubKeyBytes = ownerPubKeyBytes;
this.extraDataMap = ExtraDataMapValidator.getValidatedExtraDataMap(extraDataMap);
@ -215,6 +223,7 @@ public final class Filter implements ProtectedStoragePayload, ExpirablePayload {
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();
}
@ -241,7 +250,9 @@ public final class Filter implements ProtectedStoragePayload, ExpirablePayload {
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()));
}
@ -281,6 +292,7 @@ public final class Filter implements ProtectedStoragePayload, ExpirablePayload {
",\n mediators=" + mediators +
",\n refundAgents=" + refundAgents +
",\n bannedSignerPubKeys=" + bannedSignerPubKeys +
",\n btcFeeReceiverAddresses=" + btcFeeReceiverAddresses +
"\n}";
}
}

View File

@ -23,6 +23,7 @@ import bisq.core.btc.wallet.TradeWalletService;
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;
@ -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;
@ -139,6 +141,7 @@ public class OpenOfferManager implements PeerManager.Listener, DecryptedDirectMe
MediatorManager mediatorManager,
RefundAgentManager refundAgentManager,
DaoFacade daoFacade,
FilterManager filterManager,
Storage<TradableList<OpenOffer>> storage) {
this.createOfferService = createOfferService;
this.keyRing = keyRing;
@ -156,6 +159,7 @@ public class OpenOfferManager implements PeerManager.Listener, DecryptedDirectMe
this.mediatorManager = mediatorManager;
this.refundAgentManager = refundAgentManager;
this.daoFacade = daoFacade;
this.filterManager = filterManager;
openOfferTradableListStorage = storage;
@ -361,7 +365,8 @@ public class OpenOfferManager implements PeerManager.Listener, DecryptedDirectMe
arbitratorManager,
tradeStatisticsManager,
daoFacade,
user);
user,
filterManager);
PlaceOfferProtocol placeOfferProtocol = new PlaceOfferProtocol(
model,
transaction -> {

View File

@ -21,6 +21,7 @@ import bisq.core.btc.wallet.BsqWalletService;
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;
@ -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
@ -68,7 +71,8 @@ public class PlaceOfferModel implements Model {
ArbitratorManager arbitratorManager,
TradeStatisticsManager tradeStatisticsManager,
DaoFacade daoFacade,
User user) {
User user,
FilterManager filterManager) {
this.offer = offer;
this.reservedFundsForOffer = reservedFundsForOffer;
this.useSavingsWallet = useSavingsWallet;
@ -80,6 +84,7 @@ public class PlaceOfferModel implements Model {
this.tradeStatisticsManager = tradeStatisticsManager;
this.daoFacade = daoFacade;
this.user = user;
this.filterManager = filterManager;
}
@Override

View File

@ -25,10 +25,10 @@ import bisq.core.btc.wallet.TradeWalletService;
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;
@ -65,7 +65,8 @@ public class CreateMakerFeeTx extends Task<PlaceOfferModel> {
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(

View File

@ -22,9 +22,9 @@ import bisq.core.btc.wallet.BtcWalletService;
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;
@ -65,7 +65,9 @@ public class CreateTakerFeeTx extends TradeTask {
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,

View File

@ -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;
}
}

View File

@ -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})

View File

@ -1,7 +1,5 @@
package bisq.core.offer;
import bisq.core.trade.TradableList;
import bisq.network.p2p.P2PService;
import bisq.network.p2p.peers.PeerManager;
@ -47,7 +45,7 @@ public class OpenOfferManagerTest {
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);
@ -83,7 +81,7 @@ public class OpenOfferManagerTest {
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);
@ -111,7 +109,7 @@ public class OpenOfferManagerTest {
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);

View File

@ -27,7 +27,6 @@ import com.google.common.collect.Lists;
import org.junit.Ignore;
@SuppressWarnings("UnusedAssignment")
public class UserPayloadModelVOTest {
@Ignore("TODO InvalidKeySpecException at bisq.common.crypto.Sig.getPublicKeyFromBytes(Sig.java:135)")
public void testRoundtrip() {
@ -59,6 +58,7 @@ public class UserPayloadModelVOTest {
null,
Lists.newArrayList(),
Lists.newArrayList(),
Lists.newArrayList(),
Lists.newArrayList()));
vo.setRegisteredArbitrator(ArbitratorTest.getArbitratorMock());
vo.setRegisteredMediator(MediatorTest.getMediatorMock());

View File

@ -0,0 +1,107 @@
/*
* 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.Filter;
import bisq.core.filter.FilterManager;
import com.google.common.primitives.Longs;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;
import org.mockito.Mock;
import org.mockito.junit.MockitoJUnitRunner;
import org.junit.Test;
import org.junit.runner.RunWith;
import static org.junit.Assert.assertEquals;
import static org.mockito.Mockito.when;
@RunWith(MockitoJUnitRunner.class)
public class FeeReceiverSelectorTest {
@Mock
private DaoFacade daoFacade;
@Mock
private FilterManager filterManager;
@Test
public void testGetAddress() {
Random rnd = new Random(123);
when(filterManager.getFilter()).thenReturn(filterWithReceivers(
List.of("", "foo#0.001", "ill-formed", "bar#0.002", "baz#0.001", "partial#bad")));
Map<String, Integer> selectionCounts = new HashMap<>();
for (int i = 0; i < 400; i++) {
String address = FeeReceiverSelector.getAddress(daoFacade, filterManager, rnd);
selectionCounts.compute(address, (k, n) -> n != null ? n + 1 : 1);
}
assertEquals(3, selectionCounts.size());
// Check within 2 std. of the expected values (95% confidence each):
assertEquals(100.0, selectionCounts.get("foo"), 18);
assertEquals(200.0, selectionCounts.get("bar"), 20);
assertEquals(100.0, selectionCounts.get("baz"), 18);
}
@Test
public void testGetAddress_noValidReceivers() {
when(daoFacade.getParamValue(Param.RECIPIENT_BTC_ADDRESS)).thenReturn("default");
when(filterManager.getFilter()).thenReturn(null);
assertEquals("default", FeeReceiverSelector.getAddress(daoFacade, filterManager));
when(filterManager.getFilter()).thenReturn(filterWithReceivers(null));
assertEquals("default", FeeReceiverSelector.getAddress(daoFacade, filterManager));
when(filterManager.getFilter()).thenReturn(filterWithReceivers(List.of()));
assertEquals("default", FeeReceiverSelector.getAddress(daoFacade, filterManager));
when(filterManager.getFilter()).thenReturn(filterWithReceivers(List.of("ill-formed")));
assertEquals("default", FeeReceiverSelector.getAddress(daoFacade, filterManager));
}
@Test
public void testWeightedSelection() {
Random rnd = new Random(456);
int[] selections = new int[3];
for (int i = 0; i < 6000; i++) {
selections[FeeReceiverSelector.weightedSelection(Longs.asList(1, 2, 3), rnd)]++;
}
// Check within 2 std. of the expected values (95% confidence each):
assertEquals(1000.0, selections[0], 58);
assertEquals(2000.0, selections[1], 74);
assertEquals(3000.0, selections[2], 78);
}
private static Filter filterWithReceivers(List<String> btcFeeReceiverAddresses) {
return new Filter(null, null, null, null,
null, null, null, null,
false, null, false, null,
null, null, null, null,
btcFeeReceiverAddresses);
}
}

View File

@ -130,6 +130,7 @@ public class FilterWindow extends Overlay<FilterWindow> {
InputTextField arbitratorsInputTextField = addInputTextField(gridPane, ++rowIndex, Res.get("filterWindow.arbitrators"));
InputTextField mediatorsInputTextField = addInputTextField(gridPane, ++rowIndex, Res.get("filterWindow.mediators"));
InputTextField refundAgentsInputTextField = addInputTextField(gridPane, ++rowIndex, Res.get("filterWindow.refundAgents"));
InputTextField btcFeeReceiverAddressesInputTextField = addInputTextField(gridPane, ++rowIndex, Res.get("filterWindow.btcFeeReceiverAddresses"));
InputTextField seedNodesInputTextField = addInputTextField(gridPane, ++rowIndex, Res.get("filterWindow.seedNode"));
InputTextField priceRelayNodesInputTextField = addInputTextField(gridPane, ++rowIndex, Res.get("filterWindow.priceRelayNode"));
InputTextField btcNodesInputTextField = addInputTextField(gridPane, ++rowIndex, Res.get("filterWindow.btcNode"));
@ -149,6 +150,7 @@ public class FilterWindow extends Overlay<FilterWindow> {
setupFieldFromList(arbitratorsInputTextField, filter.getArbitrators());
setupFieldFromList(mediatorsInputTextField, filter.getMediators());
setupFieldFromList(refundAgentsInputTextField, filter.getRefundAgents());
setupFieldFromList(btcFeeReceiverAddressesInputTextField, filter.getBtcFeeReceiverAddresses());
setupFieldFromList(seedNodesInputTextField, filter.getSeedNodes());
setupFieldFromList(priceRelayNodesInputTextField, filter.getPriceRelayNodes());
setupFieldFromList(btcNodesInputTextField, filter.getBtcNodes());
@ -177,7 +179,8 @@ public class FilterWindow extends Overlay<FilterWindow> {
disableTradeBelowVersionInputTextField.getText(),
readAsList(mediatorsInputTextField),
readAsList(refundAgentsInputTextField),
readAsList(bannedSignerPubKeysInputTextField)
readAsList(bannedSignerPubKeysInputTextField),
readAsList(btcFeeReceiverAddressesInputTextField)
),
keyInputTextField.getText())
)

View File

@ -629,6 +629,7 @@ message Filter {
repeated string mediators = 17;
repeated string refundAgents = 18;
repeated string bannedSignerPubKeys = 19;
repeated string btc_fee_receiver_addresses = 20;
}
// not used anymore from v0.6 on. But leave it for receiving TradeStatistics objects from older