Skip to content

Commit

Permalink
Implement BM capping algorithm change (with delayed activation)
Browse files Browse the repository at this point in the history
Change the algorithm used to adjust & cap the burn share of each BM
candidate to use an unlimited number of 'rounds', as described in:

  bisq-network/proposals#412

That is, instead of capping the shares once, then distributing the
excess to the remaining BM, then capping again and giving any excess to
the Legacy Burning Man, we cap-redistribute-cap-redistribute-... an
unlimited number of times until no more candidates are capped. This has
the effect of reducing the LBM's share and increasing everyone else's,
alleviating the security risk of giving too much to the LBM (who is
necessarily uncapped).

Instead of implementing the new algorithm directly, we simply enlarge
the set of candidates who should be capped to include those who would
eventually be capped by the new algorithm, in order to determine how
much excess burn share should go to the remaining BM. Then we apply the
original method, 'candidate.calculateCappedAndAdjustedShares(..)', to
set each share to be equal to its respective cap or uniformly scaled
upwards from the starting amount accordingly.

To this end, the static method 'BurningManService.imposeCaps' is added,
which determines which candidates will eventually be capped, by sorting
them in descending order of burn-share/cap-share ratio, then marking all
the candidates in some suitable prefix of the list as capped, iterating
through them one-by-one & gradually increasing the virtual capping round
(starting at zero) until the end of the prefix is reached. (The method
also determines what the uncapped adjusted burn share of each BM should
be, but that only affects the BM view & burn targets.) In this way, the
new algorithm runs in guaranteed O(n * log n) time.

To prevent failed trades, the new algorithm is set to activate at time
'DelayedPayoutTxReceiverService.PROPOSAL_412_ACTIVATION_DATE', with a
placeholder value of 12am, 1st January 2024 (UTC). This simply toggles
whether the for-loop in 'imposeCaps' should stop after capping round 0,
since doing so will lead to identical behaviour to the original code
(even accounting for FP rounding errors).
  • Loading branch information
stejbac committed Nov 26, 2023
1 parent 7dfd6aa commit 4c0c11b
Show file tree
Hide file tree
Showing 5 changed files with 150 additions and 42 deletions.
48 changes: 44 additions & 4 deletions core/src/main/java/bisq/core/dao/burningman/BurningManService.java
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,12 @@
import com.google.common.base.Charsets;
import com.google.common.collect.ImmutableList;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Comparator;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
Expand Down Expand Up @@ -112,6 +115,10 @@ public BurningManService(DaoStateService daoStateService,
///////////////////////////////////////////////////////////////////////////////////////////

Map<String, BurningManCandidate> getBurningManCandidatesByName(int chainHeight) {
return getBurningManCandidatesByName(chainHeight, !DelayedPayoutTxReceiverService.isProposal412Activated());
}

Map<String, BurningManCandidate> getBurningManCandidatesByName(int chainHeight, boolean limitCappingRounds) {
Map<String, BurningManCandidate> burningManCandidatesByName = new TreeMap<>();
Map<P2PDataStorage.ByteArray, Set<TxOutput>> proofOfBurnOpReturnTxOutputByHash = getProofOfBurnOpReturnTxOutputByHash(chainHeight);

Expand Down Expand Up @@ -187,25 +194,58 @@ Map<String, BurningManCandidate> getBurningManCandidatesByName(int chainHeight)
.sum();
burningManCandidates.forEach(candidate -> candidate.calculateShares(totalDecayedCompensationAmounts, totalDecayedBurnAmounts));

int numRoundsWithCapsApplied = imposeCaps(burningManCandidates, limitCappingRounds);

double sumAllCappedBurnAmountShares = burningManCandidates.stream()
.filter(candidate -> candidate.getBurnAmountShare() >= candidate.getMaxBoostedCompensationShare())
.filter(candidate -> candidate.getRoundCapped().isPresent())
.mapToDouble(BurningManCandidate::getMaxBoostedCompensationShare)
.sum();
double sumAllNonCappedBurnAmountShares = burningManCandidates.stream()
.filter(candidate -> candidate.getBurnAmountShare() < candidate.getMaxBoostedCompensationShare())
.filter(candidate -> candidate.getRoundCapped().isEmpty())
.mapToDouble(BurningManCandidate::getBurnAmountShare)
.sum();
burningManCandidates.forEach(candidate -> candidate.calculateCappedAndAdjustedShares(sumAllCappedBurnAmountShares, sumAllNonCappedBurnAmountShares));
burningManCandidates.forEach(candidate -> candidate.calculateCappedAndAdjustedShares(
sumAllCappedBurnAmountShares, sumAllNonCappedBurnAmountShares, numRoundsWithCapsApplied));

return burningManCandidatesByName;
}

private static int imposeCaps(Collection<BurningManCandidate> burningManCandidates, boolean limitCappingRounds) {
List<BurningManCandidate> candidatesInDescendingBurnCapRatio = new ArrayList<>(burningManCandidates);
candidatesInDescendingBurnCapRatio.sort(Comparator.comparing(BurningManCandidate::getBurnCapRatio).reversed());
double thresholdBurnCapRatio = 1.0;
double remainingBurnShare = 1.0;
double remainingCapShare = 1.0;
int cappingRound = 0;
for (BurningManCandidate candidate : candidatesInDescendingBurnCapRatio) {
double invScaleFactor = remainingBurnShare / remainingCapShare;
double burnCapRatio = candidate.getBurnCapRatio();
if (remainingCapShare <= 0.0 || burnCapRatio <= 0.0 || burnCapRatio < invScaleFactor ||
limitCappingRounds && burnCapRatio < 1.0) {
cappingRound++;
break;
}
if (burnCapRatio < thresholdBurnCapRatio) {
thresholdBurnCapRatio = invScaleFactor;
cappingRound++;
}
candidate.imposeCap(cappingRound, candidate.getBurnAmountShare() / thresholdBurnCapRatio);
remainingBurnShare -= candidate.getBurnAmountShare();
remainingCapShare -= candidate.getMaxBoostedCompensationShare();
}
return cappingRound;
}

String getLegacyBurningManAddress(int chainHeight) {
return daoStateService.getParamValue(Param.RECIPIENT_BTC_ADDRESS, chainHeight);
}

Set<BurningManCandidate> getActiveBurningManCandidates(int chainHeight) {
return getBurningManCandidatesByName(chainHeight).values().stream()
return getActiveBurningManCandidates(chainHeight, !DelayedPayoutTxReceiverService.isProposal412Activated());
}

Set<BurningManCandidate> getActiveBurningManCandidates(int chainHeight, boolean limitCappingRounds) {
return getBurningManCandidatesByName(chainHeight, limitCappingRounds).values().stream()
.filter(burningManCandidate -> burningManCandidate.getCappedBurnAmountShare() > 0)
.filter(candidate -> candidate.getReceiverAddress().isPresent())
.collect(Collectors.toSet());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,11 +56,18 @@ public class DelayedPayoutTxReceiverService implements DaoStateListener {
// requests change address.
// See: https://github.com/bisq-network/bisq/issues/6699
public static final Date BUGFIX_6699_ACTIVATION_DATE = Utilities.getUTCDate(2023, GregorianCalendar.JULY, 24);
// See: https://github.com/bisq-network/proposals/issues/412
public static final Date PROPOSAL_412_ACTIVATION_DATE = Utilities.getUTCDate(2024, GregorianCalendar.JANUARY, 1);

public static boolean isBugfix6699Activated() {
return new Date().after(BUGFIX_6699_ACTIVATION_DATE);
}

@SuppressWarnings("BooleanMethodIsAlwaysInverted")
public static boolean isProposal412Activated() {
return new Date().after(PROPOSAL_412_ACTIVATION_DATE);
}

// We don't allow to get further back than 767950 (the block height from Dec. 18th 2022).
static final int MIN_SNAPSHOT_HEIGHT = Config.baseCurrencyNetwork().isRegtest() ? 0 : 767950;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import java.util.HashSet;
import java.util.Map;
import java.util.Optional;
import java.util.OptionalInt;
import java.util.Set;
import java.util.stream.Collectors;

Expand Down Expand Up @@ -68,6 +69,7 @@ public class BurningManCandidate {
// The burnAmountShare adjusted in case there are cappedBurnAmountShare.
// We redistribute the over-burned amounts to the group of not capped candidates.
protected double adjustedBurnAmountShare;
private OptionalInt roundCapped = OptionalInt.empty();

public BurningManCandidate() {
}
Expand Down Expand Up @@ -142,11 +144,19 @@ public void calculateShares(double totalDecayedCompensationAmounts, double total
burnAmountShare = totalDecayedBurnAmounts > 0 ? accumulatedDecayedBurnAmount / totalDecayedBurnAmounts : 0;
}

public void imposeCap(int cappingRound, double adjustedBurnAmountShare) {
roundCapped = OptionalInt.of(cappingRound);
// NOTE: The adjusted burn share set here will not affect the final capped burn share, only
// the presentation service, so we need not worry about rounding errors affecting consensus.
this.adjustedBurnAmountShare = adjustedBurnAmountShare;
}

public void calculateCappedAndAdjustedShares(double sumAllCappedBurnAmountShares,
double sumAllNonCappedBurnAmountShares) {
double sumAllNonCappedBurnAmountShares,
int numAppliedCappingRounds) {
double maxBoostedCompensationShare = getMaxBoostedCompensationShare();
adjustedBurnAmountShare = burnAmountShare;
if (burnAmountShare < maxBoostedCompensationShare) {
if (roundCapped.isEmpty()) {
adjustedBurnAmountShare = burnAmountShare;
if (sumAllCappedBurnAmountShares == 0) {
// If no one is capped we do not need to do any adjustment
cappedBurnAmountShare = burnAmountShare;
Expand All @@ -165,7 +175,11 @@ public void calculateCappedAndAdjustedShares(double sumAllCappedBurnAmountShares
} else {
// We exceeded the cap by the adjustment. This will lead to the legacy BM getting the
// difference of the adjusted amount and the maxBoostedCompensationShare.
// NOTE: When the number of capping rounds are unlimited (that is post- Proposal 412
// activation), we should only get to this branch as a result of floating point rounding
// errors. In that case, the extra amount the LBM gets is negligible.
cappedBurnAmountShare = maxBoostedCompensationShare;
roundCapped = OptionalInt.of(roundCapped.orElse(numAppliedCappingRounds));
}
}
}
Expand All @@ -174,6 +188,12 @@ public void calculateCappedAndAdjustedShares(double sumAllCappedBurnAmountShares
}
}

public double getBurnCapRatio() {
// NOTE: This is less than 1.0 precisely when burnAmountShare < maxBoostedCompensationShare,
// in spite of any floating point rounding errors, since 1.0 is proportionately at least as
// close to the previous double as any two consecutive nonzero doubles on the number line.
return burnAmountShare > 0.0 ? burnAmountShare / getMaxBoostedCompensationShare() : 0.0;
}

public double getMaxBoostedCompensationShare() {
return Math.min(BurningManService.MAX_BURN_SHARE, compensationShare * BurningManService.ISSUANCE_BOOST_FACTOR);
Expand All @@ -194,6 +214,7 @@ public String toString() {
",\r\n burnAmountShare=" + burnAmountShare +
",\r\n cappedBurnAmountShare=" + cappedBurnAmountShare +
",\r\n adjustedBurnAmountShare=" + adjustedBurnAmountShare +
",\r\n roundCapped=" + roundCapped +
"\r\n}";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,15 @@ public void calculateShares(double totalDecayedCompensationAmounts, double total
// do nothing
}

@Override
public void imposeCap(int cappingRound, double adjustedBurnAmountShare) {
// do nothing
}

@Override
public void calculateCappedAndAdjustedShares(double sumAllCappedBurnAmountShares,
double sumAllNonCappedBurnAmountShares) {
double sumAllNonCappedBurnAmountShares,
int numAppliedCappingRounds) {
// do nothing
}

Expand Down
Loading

0 comments on commit 4c0c11b

Please sign in to comment.