Changed trade period to time based instead of block based (cleanup)

This commit is contained in:
Manfred Karrer 2016-04-05 01:56:46 +02:00
parent a78163c735
commit 1790dbd542
10 changed files with 59 additions and 78 deletions

View File

@ -37,7 +37,7 @@ public final class PaymentMethod implements Persistable, Comparable {
protected final Logger log = LoggerFactory.getLogger(this.getClass());
// time in blocks (average 10 min for one block confirmation
private static final long HOUR = 3600 * 1000;
private static final long HOUR = 3600_000;
private static final long DAY = HOUR * 24;
public static final String OK_PAY_ID = "OK_PAY";

View File

@ -138,6 +138,7 @@ public abstract class Trade implements Tradable, Model {
@Nullable
transient private Storage<? extends TradableList> storage;
transient protected TradeProtocol tradeProtocol;
private transient Date maxTradePeriodDate, halfTradePeriodDate;
// Immutable
private final Offer offer;
@ -146,7 +147,6 @@ public abstract class Trade implements Tradable, Model {
// Mutable
private DecryptedMsgWithPubKey decryptedMsgWithPubKey;
private Date takeOfferDate;
private int takeOfferDateAsBlockHeight;
private Coin tradeAmount;
private NodeAddress tradingPeerNodeAddress;
protected State state;
@ -390,6 +390,22 @@ public abstract class Trade implements Tradable, Model {
return null;
}
@Nullable
public Date getMaxTradePeriodDate() {
if (maxTradePeriodDate == null && takeOfferDate != null)
maxTradePeriodDate = new Date(takeOfferDate.getTime() + getOffer().getPaymentMethod().getMaxTradePeriod());
return maxTradePeriodDate;
}
@Nullable
public Date getHalfTradePeriodDate() {
if (halfTradePeriodDate == null && takeOfferDate != null)
halfTradePeriodDate = new Date(takeOfferDate.getTime() + getOffer().getPaymentMethod().getMaxTradePeriod() / 2);
return halfTradePeriodDate;
}
public ReadOnlyObjectProperty<? extends State> stateProperty() {
return stateProperty;
@ -412,6 +428,7 @@ public abstract class Trade implements Tradable, Model {
return tradePeriodStateProperty;
}
///////////////////////////////////////////////////////////////////////////////////////////
// Getter/Setter for Mutable objects
///////////////////////////////////////////////////////////////////////////////////////////
@ -420,18 +437,6 @@ public abstract class Trade implements Tradable, Model {
return takeOfferDate;
}
/*public void setTakeOfferDate(Date takeOfferDate) {
this.takeOfferDate = takeOfferDate;
}*/
public int getTakeOfferDateAsBlockHeight() {
return takeOfferDateAsBlockHeight;
}
public void setTakeOfferDateAsBlockHeight(int blockHeight) {
takeOfferDateAsBlockHeight = blockHeight;
}
public void setTradingPeerNodeAddress(NodeAddress tradingPeerNodeAddress) {
if (tradingPeerNodeAddress == null)
log.error("tradingPeerAddress=null");

View File

@ -284,8 +284,6 @@ public class TradeManager {
else
trade = new BuyerAsTakerTrade(offer, amount, model.getPeerNodeAddress(), tradableListStorage);
//trade.setTakeOfferDate(new Date());
trade.setTakeOfferDateAsBlockHeight(tradeWalletService.getBestChainHeight());
trade.setTakerPaymentAccountId(paymentAccountId);
initTrade(trade, useSavingsWallet, fundsNeededForTrade);

View File

@ -62,8 +62,6 @@ public class SignAndPublishDepositTxAsBuyer extends TradeTask {
log.trace("takerSignAndPublishTx succeeded " + transaction);
trade.setDepositTx(transaction);
//trade.setTakeOfferDate(new Date());
trade.setTakeOfferDateAsBlockHeight(processModel.getTradeWalletService().getBestChainHeight());
trade.setState(Trade.State.TAKER_PUBLISHED_DEPOSIT_TX);
complete();

View File

@ -52,9 +52,6 @@ public class ProcessDepositTxPublishedMessage extends TradeTask {
// update with full tx
trade.setDepositTx(processModel.getTradeWalletService().addTransactionToWallet(transactionFromSerializedTx));
//trade.setTakeOfferDate(new Date());
trade.setTakeOfferDateAsBlockHeight(processModel.getTradeWalletService().getBestChainHeight());
if (trade instanceof OffererTrade)
processModel.getOpenOfferManager().closeOpenOffer(trade.getOffer());

View File

@ -60,8 +60,6 @@ public class SignAndPublishDepositTxAsSeller extends TradeTask {
log.trace("takerSignAndPublishTx succeeded " + transaction);
trade.setDepositTx(transaction);
//trade.setTakeOfferDate(new Date());
trade.setTakeOfferDateAsBlockHeight(processModel.getTradeWalletService().getBestChainHeight());
trade.setState(Trade.State.TAKER_PUBLISHED_DEPOSIT_TX);
complete();

View File

@ -66,17 +66,6 @@ public class ProcessPublishDepositTxRequest extends TradeTask {
processModel.tradingPeer.setRawTransactionInputs(checkNotNull(publishDepositTxRequest.offererInputs));
processModel.setPreparedDepositTx(checkNotNull(publishDepositTxRequest.preparedDepositTx));
checkArgument(publishDepositTxRequest.offererInputs.size() > 0);
/*if (publishDepositTxRequest.openDisputeTimeAsBlockHeight != 0) {
trade.setOpenDisputeTimeAsBlockHeight(publishDepositTxRequest.openDisputeTimeAsBlockHeight);
} else {
failed("waitPeriodForOpenDisputeAsBlockHeight = 0");
}
if (publishDepositTxRequest.checkPaymentTimeAsBlockHeight != 0) {
trade.setCheckPaymentTimeAsBlockHeight(publishDepositTxRequest.checkPaymentTimeAsBlockHeight);
} else {
failed("notificationTimeAsBlockHeight = 0");
}*/
// update to the latest peer address of our peer if the message is correct
trade.setTradingPeerNodeAddress(processModel.getTempTradingPeerNodeAddress());

View File

@ -547,45 +547,45 @@ public class MainViewModel implements ViewModel {
private void updateTradePeriodState() {
tradeManager.getTrades().stream().forEach(trade -> {
if (trade.getState().getPhase().ordinal() < Trade.Phase.PAYOUT_PAID.ordinal()) {
long maxTradePeriod = trade.getOffer().getPaymentMethod().getMaxTradePeriod();
Date maxTradePeriodDate = new Date(trade.getDate().getTime() + maxTradePeriod);
Date halfTradePeriodDate = new Date(trade.getDate().getTime() + maxTradePeriod / 2);
Date now = new Date();
Date maxTradePeriodDate = trade.getMaxTradePeriodDate();
Date halfTradePeriodDate = trade.getHalfTradePeriodDate();
if (maxTradePeriodDate != null && halfTradePeriodDate != null) {
Date now = new Date();
if (now.after(maxTradePeriodDate))
trade.setTradePeriodState(Trade.TradePeriodState.TRADE_PERIOD_OVER);
else if (now.after(halfTradePeriodDate))
trade.setTradePeriodState(Trade.TradePeriodState.HALF_REACHED);
if (now.after(maxTradePeriodDate))
trade.setTradePeriodState(Trade.TradePeriodState.TRADE_PERIOD_OVER);
else if (now.after(halfTradePeriodDate))
trade.setTradePeriodState(Trade.TradePeriodState.HALF_REACHED);
String key;
switch (trade.getTradePeriodState()) {
case NORMAL:
break;
case HALF_REACHED:
key = "displayHalfTradePeriodOver" + trade.getId();
if (preferences.showAgain(key)) {
preferences.dontShowAgain(key, true);
new Popup().warning("Your trade with ID " + trade.getShortId() +
" has reached the half of the max. allowed trading period and " +
"is still not completed.\n\n" +
"The trade period ends on " + formatter.formatDateTime(maxTradePeriodDate) + "\n\n" +
"Please check your trade state at \"Portfolio/Open trades\" for further information.")
.show();
}
break;
case TRADE_PERIOD_OVER:
key = "displayTradePeriodOver" + trade.getId();
if (preferences.showAgain(key)) {
preferences.dontShowAgain(key, true);
new Popup().warning("Your trade with ID " + trade.getShortId() +
" has reached the max. allowed trading period and is " +
"not completed.\n\n" +
"The trade period ended on " + formatter.formatDateTime(maxTradePeriodDate) + "\n\n" +
"Please check your trade at \"Portfolio/Open trades\" for contacting " +
"the arbitrator.")
.show();
}
break;
String key;
switch (trade.getTradePeriodState()) {
case NORMAL:
break;
case HALF_REACHED:
key = "displayHalfTradePeriodOver" + trade.getId();
if (preferences.showAgain(key)) {
preferences.dontShowAgain(key, true);
new Popup().warning("Your trade with ID " + trade.getShortId() +
" has reached the half of the max. allowed trading period and " +
"is still not completed.\n\n" +
"The trade period ends on " + formatter.formatDateTime(maxTradePeriodDate) + "\n\n" +
"Please check your trade state at \"Portfolio/Open trades\" for further information.")
.show();
}
break;
case TRADE_PERIOD_OVER:
key = "displayTradePeriodOver" + trade.getId();
if (preferences.showAgain(key)) {
preferences.dontShowAgain(key, true);
new Popup().warning("Your trade with ID " + trade.getShortId() +
" has reached the max. allowed trading period and is " +
"not completed.\n\n" +
"The trade period ended on " + formatter.formatDateTime(maxTradePeriodDate) + "\n\n" +
"Please check your trade at \"Portfolio/Open trades\" for contacting " +
"the arbitrator.")
.show();
}
break;
}
}
}
});

View File

@ -239,10 +239,6 @@ public class PendingTradesViewModel extends ActivatableWithDataModel<PendingTrad
return dataModel.getLockTime();
}
public int getBestChainHeight() {
return dataModel.getBestChainHeight();
}
public String getPaymentMethod() {
if (dataModel.getTrade() != null && dataModel.getTrade().getContract() != null)
return BSResources.get(dataModel.getTrade().getContract().getPaymentMethodName());

View File

@ -49,7 +49,7 @@ public class BuyerStep4View extends TradeStepView {
@Override
public void reorganize(StoredBlock splitPoint, List<StoredBlock> oldBlocks, List<StoredBlock> newBlocks) throws VerificationException {
updateDateFromBlockHeight(model.getBestChainHeight());
updateDateFromBlockHeight(model.dataModel.getBestChainHeight());
}
@Override
@ -75,7 +75,7 @@ public class BuyerStep4View extends TradeStepView {
super.activate();
model.addBlockChainListener(blockChainListener);
updateDateFromBlockHeight(model.getBestChainHeight());
updateDateFromBlockHeight(model.dataModel.getBestChainHeight());
}
@Override