@@ -2273,6 +2273,38 @@ public Volume getVolume() {
2273
2273
}
2274
2274
}
2275
2275
2276
+ public void maybeUpdateTradePeriod () {
2277
+ if (startTime > 0 ) return ; // already set
2278
+ if (getTakeOfferDate () == null ) return ; // trade not started yet
2279
+ if (!isDepositsFinalized ()) return ; // deposits not finalized yet
2280
+
2281
+ long now = System .currentTimeMillis ();
2282
+ long tradeTime = getTakeOfferDate ().getTime ();
2283
+ MoneroDaemon monerod = xmrWalletService .getMonerod ();
2284
+ if (monerod == null ) throw new RuntimeException ("Cannot set start time for trade " + getId () + " because it has no connection to monerod" );
2285
+
2286
+ // get finalize time of last deposit tx
2287
+ long finalizeHeight = getDepositsFinalizedHeight ();
2288
+ long finalizeTime = monerod .getBlockByHeight (finalizeHeight ).getTimestamp () * 1000 ;
2289
+
2290
+ // If block date is in future (Date in blocks can be off by +/- 2 hours) we use our current date.
2291
+ // If block date is earlier than our trade date we use our trade date.
2292
+ if (finalizeTime > now )
2293
+ startTime = now ;
2294
+ else
2295
+ startTime = Math .max (finalizeTime , tradeTime );
2296
+
2297
+ log .debug ("We set the start for the trade period to {}. Trade started at: {}. Block got mined at: {}" ,
2298
+ new Date (startTime ), new Date (tradeTime ), new Date (finalizeTime ));
2299
+ }
2300
+
2301
+ private long getDepositsFinalizedHeight () {
2302
+ MoneroTxWallet makerDepositTx = getMakerDepositTx ();
2303
+ MoneroTxWallet takerDepositTx = getTakerDepositTx ();
2304
+ if (makerDepositTx == null || (takerDepositTx == null && !hasBuyerAsTakerWithoutDeposit ())) throw new RuntimeException ("Cannot get finalized height for trade " + getId () + " because its deposit tx is null. Is client connected to a daemon?" );
2305
+ return Math .max (makerDepositTx .getHeight () + NUM_BLOCKS_DEPOSITS_FINALIZED - 1 , hasBuyerAsTakerWithoutDeposit () ? 0l : takerDepositTx .getHeight () + NUM_BLOCKS_DEPOSITS_FINALIZED - 1 );
2306
+ }
2307
+
2276
2308
public long getMaxTradePeriod () {
2277
2309
return getOffer ().getPaymentMethod ().getMaxTradePeriod ();
2278
2310
}
@@ -2294,38 +2326,7 @@ public Date getStartDate() {
2294
2326
* Returns the current time until the deposits are finalized.
2295
2327
*/
2296
2328
private long getEffectiveStartTime () {
2297
- if (startTime > 0 ) return startTime ;
2298
- if (getTakeOfferDate () == null ) return System .currentTimeMillis (); // trade not started yet
2299
- if (isDepositsFinalized ()) {
2300
- setStartTimeFromFinalizedTxs (); // save to model
2301
- return startTime ;
2302
- } else {
2303
- return System .currentTimeMillis ();
2304
- }
2305
- }
2306
-
2307
- private void setStartTimeFromFinalizedTxs () {
2308
- long now = System .currentTimeMillis ();
2309
- final long tradeTime = getTakeOfferDate ().getTime ();
2310
- MoneroDaemon monerod = xmrWalletService .getMonerod ();
2311
- MoneroTxWallet makerDepositTx = getMakerDepositTx ();
2312
- MoneroTxWallet takerDepositTx = getTakerDepositTx ();
2313
- if (monerod == null ) throw new RuntimeException ("Cannot set start time for trade " + getId () + " because it has no connection to monerod" );
2314
- if (makerDepositTx == null || (takerDepositTx == null && !hasBuyerAsTakerWithoutDeposit ())) throw new RuntimeException ("Cannot set start time for trade " + getId () + " because its finalized deposit tx is null. Is client connected to a daemon?" );
2315
-
2316
- // get finalize time of last deposit tx
2317
- long finalizeHeight = Math .max (makerDepositTx .getHeight () + NUM_BLOCKS_DEPOSITS_FINALIZED - 1 , hasBuyerAsTakerWithoutDeposit () ? 0l : takerDepositTx .getHeight () + NUM_BLOCKS_DEPOSITS_FINALIZED - 1 );
2318
- long finalizeTime = monerod .getBlockByHeight (finalizeHeight ).getTimestamp () * 1000 ;
2319
-
2320
- // If block date is in future (Date in blocks can be off by +/- 2 hours) we use our current date.
2321
- // If block date is earlier than our trade date we use our trade date.
2322
- if (finalizeTime > now )
2323
- startTime = now ;
2324
- else
2325
- startTime = Math .max (finalizeTime , tradeTime );
2326
-
2327
- log .debug ("We set the start for the trade period to {}. Trade started at: {}. Block got mined at: {}" ,
2328
- new Date (startTime ), new Date (tradeTime ), new Date (finalizeTime ));
2329
+ return startTime > 0 ? startTime : System .currentTimeMillis ();
2329
2330
}
2330
2331
2331
2332
public boolean hasFailed () {
@@ -3250,7 +3251,7 @@ private void setStateDepositsUnlocked() {
3250
3251
private void setStateDepositsFinalized () {
3251
3252
if (!isDepositsFinalized ()) {
3252
3253
setStateIfValidTransitionTo (State .DEPOSIT_TXS_FINALIZED_IN_BLOCKCHAIN );
3253
- setStartTimeFromFinalizedTxs ( );
3254
+ ThreadUtils . submitToPool (() -> maybeUpdateTradePeriod () );
3254
3255
}
3255
3256
}
3256
3257
0 commit comments