Skip to content

Commit

Permalink
schedule import multisig hex on deposit confirmation msg
Browse files Browse the repository at this point in the history
  • Loading branch information
woodser committed Mar 10, 2025
1 parent cb69d06 commit d4eb30b
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 19 deletions.
41 changes: 35 additions & 6 deletions core/src/main/java/haveno/core/trade/Trade.java
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,7 @@ public abstract class Trade extends XmrWalletBase implements Tradable, Model {
private static final long DELETE_AFTER_NUM_BLOCKS = 2; // if deposit requested but not published
private static final long EXTENDED_RPC_TIMEOUT = 600000; // 10 minutes
private static final long DELETE_AFTER_MS = TradeProtocol.TRADE_STEP_TIMEOUT_SECONDS;
private static final int NUM_CONFIRMATIONS_FOR_SCHEDULED_IMPORT = 10;
protected final Object pollLock = new Object();
protected static final Object importMultisigLock = new Object();
private boolean pollInProgress;
Expand Down Expand Up @@ -741,6 +742,11 @@ public void initialize(ProcessModelServiceProvider serviceProvider) {
}
}

// handle confirmations
walletHeight.addListener((observable, oldValue, newValue) -> {
importMultisigHexIfScheduled();
});

// trade is initialized
isInitialized = true;

Expand Down Expand Up @@ -1077,6 +1083,26 @@ public void importMultisigHexIfNeeded() {
}
}

public void scheduleImportMultisigHex() {
processModel.setImportMultisigHexScheduled(true);
requestPersistence();
}

private void importMultisigHexIfScheduled() {
if (!isInitialized || isShutDownStarted) return;
if (!isDepositsConfirmed() || getMaker().getDepositTx() == null) return;
if (walletHeight.get() - getMaker().getDepositTx().getHeight() < NUM_CONFIRMATIONS_FOR_SCHEDULED_IMPORT) return;
ThreadUtils.execute(() -> {
if (!isInitialized || isShutDownStarted) return;
synchronized (getLock()) {
if (processModel.isImportMultisigHexScheduled()) {
processModel.setImportMultisigHexScheduled(false);
ThreadUtils.submitToPool(() -> importMultisigHex());
}
}
}, getId());
}

public void importMultisigHex() {
synchronized (walletLock) {
synchronized (HavenoUtils.getDaemonLock()) { // lock on daemon because import calls full refresh
Expand Down Expand Up @@ -1141,6 +1167,9 @@ private void doImportMultisigHex() {
if (removed) wallet.importMultisigHex(multisigHexes.toArray(new String[0]));
if (wallet.isMultisigImportNeeded()) throw new IllegalStateException(errorMessage);
}

// remove scheduled import
processModel.setImportMultisigHexScheduled(false);
} catch (MoneroError e) {

// import multisig hex individually if one is invalid
Expand Down Expand Up @@ -2350,7 +2379,12 @@ public boolean shouldPublishTradeStatistics() {
return tradeAmountTransferred();
}

public boolean tradeAmountTransferred() {

///////////////////////////////////////////////////////////////////////////////////////////
// Private
///////////////////////////////////////////////////////////////////////////////////////////

private boolean tradeAmountTransferred() {
return isPaymentReceived() || (getDisputeResult() != null && getDisputeResult().getWinner() == DisputeResult.Winner.SELLER);
}

Expand All @@ -2366,11 +2400,6 @@ private void doPublishTradeStatistics() {
}
}


///////////////////////////////////////////////////////////////////////////////////////////
// Private
///////////////////////////////////////////////////////////////////////////////////////////

// lazy initialization
private ObjectProperty<BigInteger> getAmountProperty() {
if (tradeAmountProperty == null)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,9 @@ public class ProcessModel implements Model, PersistablePayload {
@Getter
@Setter
private long tradeProtocolErrorHeight;
@Getter
@Setter
private boolean importMultisigHexScheduled;

// We want to indicate the user the state of the message delivery of the
// PaymentSentMessage. As well we do an automatic re-send in case it was not ACKed yet.
Expand Down Expand Up @@ -207,7 +210,8 @@ public protobuf.ProcessModel toProtoMessage() {
.setPaymentSentMessageStateArbitrator(paymentSentMessageStatePropertyArbitrator.get().name())
.setBuyerPayoutAmountFromMediation(buyerPayoutAmountFromMediation)
.setSellerPayoutAmountFromMediation(sellerPayoutAmountFromMediation)
.setTradeProtocolErrorHeight(tradeProtocolErrorHeight);
.setTradeProtocolErrorHeight(tradeProtocolErrorHeight)
.setImportMultisigHexScheduled(importMultisigHexScheduled);
Optional.ofNullable(maker).ifPresent(e -> builder.setMaker((protobuf.TradePeer) maker.toProtoMessage()));
Optional.ofNullable(taker).ifPresent(e -> builder.setTaker((protobuf.TradePeer) taker.toProtoMessage()));
Optional.ofNullable(arbitrator).ifPresent(e -> builder.setArbitrator((protobuf.TradePeer) arbitrator.toProtoMessage()));
Expand All @@ -231,6 +235,7 @@ public static ProcessModel fromProto(protobuf.ProcessModel proto, CoreProtoResol
processModel.setBuyerPayoutAmountFromMediation(proto.getBuyerPayoutAmountFromMediation());
processModel.setSellerPayoutAmountFromMediation(proto.getSellerPayoutAmountFromMediation());
processModel.setTradeProtocolErrorHeight(proto.getTradeProtocolErrorHeight());
processModel.setImportMultisigHexScheduled(proto.getImportMultisigHexScheduled());

// nullable
processModel.setPayoutTxSignature(ProtoUtil.byteArrayOrNullFromProto(proto.getPayoutTxSignature()));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
package haveno.core.trade.protocol.tasks;


import haveno.common.ThreadUtils;
import haveno.common.taskrunner.TaskRunner;
import haveno.core.trade.Trade;
import haveno.core.trade.messages.DepositsConfirmedMessage;
Expand Down Expand Up @@ -63,17 +62,7 @@ protected void run() {
// update multisig hex
if (sender.getUpdatedMultisigHex() == null) {
sender.setUpdatedMultisigHex(request.getUpdatedMultisigHex());

// try to import multisig hex (retry later)
if (!trade.isPayoutPublished()) {
ThreadUtils.submitToPool(() -> {
try {
trade.importMultisigHex();
} catch (Exception e) {
log.warn("Error importing multisig hex on deposits confirmed for trade " + trade.getId() + ": " + e.getMessage() + "\n", e);
}
});
}
trade.scheduleImportMultisigHex();
}

// persist
Expand Down
1 change: 1 addition & 0 deletions proto/src/main/proto/pb.proto
Original file line number Diff line number Diff line change
Expand Up @@ -1581,6 +1581,7 @@ message ProcessModel {
int64 seller_payout_amount_from_mediation = 17;
int64 trade_protocol_error_height = 18;
string trade_fee_address = 19;
bool import_multisig_hex_scheduled = 20;
}

message TradePeer {
Expand Down

0 comments on commit d4eb30b

Please sign in to comment.