Skip to content

Commit

Permalink
randomize completed trade info, fixes #1099
Browse files Browse the repository at this point in the history
  • Loading branch information
fa2a5qj3 authored Jul 16, 2024
1 parent 40421ee commit 0a469db
Showing 1 changed file with 23 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@
import java.util.HashMap;
import java.util.Map;
import java.util.Optional;
import java.util.concurrent.TimeUnit;
import java.util.Random;

import static com.google.common.base.Preconditions.checkNotNull;

Expand Down Expand Up @@ -88,13 +90,32 @@ public static TradeStatistics3 from(Trade trade,
Offer offer = checkNotNull(trade.getOffer());
return new TradeStatistics3(offer.getCurrencyCode(),
trade.getPrice().getValue(),
trade.getAmount().longValueExact(),
fuzzTradeAmountReproducibly(trade),
offer.getPaymentMethod().getId(),
trade.getTakeOfferDate().getTime(),
fuzzTradeDateReproducibly(trade),
truncatedArbitratorNodeAddress,
extraDataMap);
}

private static long fuzzTradeAmountReproducibly(Trade trade) { // randomize completed trade info #1099
long originalTimestamp = trade.getTakeOfferDate().getTime();
long exactAmount = trade.getAmount().longValueExact();
Random random = new Random(originalTimestamp); // pseudo random generator seeded from take offer datestamp
long adjustedAmount = (long) random.nextDouble(
exactAmount * 0.95, exactAmount * 1.05);
log.debug("trade {} fuzzed trade amount for tradeStatistics is {}", trade.getShortId(), adjustedAmount);
return adjustedAmount;
}

private static long fuzzTradeDateReproducibly(Trade trade) { // randomize completed trade info #1099
long originalTimestamp = trade.getTakeOfferDate().getTime();
Random random = new Random(originalTimestamp); // pseudo random generator seeded from take offer datestamp
long adjustedTimestamp = random.nextLong(
originalTimestamp-TimeUnit.HOURS.toMillis(24), originalTimestamp);
log.debug("trade {} fuzzed trade datestamp for tradeStatistics is {}", trade.getShortId(), new Date(adjustedTimestamp));
return adjustedTimestamp;
}

// This enum must not change the order as we use the ordinal for storage to reduce data size.
// The payment method string can be quite long and would consume 15% more space.
// When we get a new payment method we can add it to the enum at the end. Old users would add it as string if not
Expand Down

0 comments on commit 0a469db

Please sign in to comment.