forked from bisq-network/bisq
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ProcessModel.java
359 lines (315 loc) · 15.7 KB
/
ProcessModel.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
/*
* This file is part of Bisq.
*
* Bisq is free software: you can redistribute it and/or modify it
* under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or (at
* your option) any later version.
*
* Bisq is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public
* License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with Bisq. If not, see <http://www.gnu.org/licenses/>.
*/
package bisq.core.trade.protocol;
import bisq.core.account.witness.AccountAgeWitnessService;
import bisq.core.btc.model.RawTransactionInput;
import bisq.core.btc.wallet.BsqWalletService;
import bisq.core.btc.wallet.BtcWalletService;
import bisq.core.btc.wallet.TradeWalletService;
import bisq.core.dao.DaoFacade;
import bisq.core.filter.FilterManager;
import bisq.core.network.MessageState;
import bisq.core.offer.Offer;
import bisq.core.offer.OpenOfferManager;
import bisq.core.payment.PaymentAccount;
import bisq.core.payment.payload.PaymentAccountPayload;
import bisq.core.proto.CoreProtoResolver;
import bisq.core.support.dispute.arbitration.arbitrator.ArbitratorManager;
import bisq.core.support.dispute.mediation.mediator.MediatorManager;
import bisq.core.support.dispute.refund.refundagent.RefundAgentManager;
import bisq.core.trade.MakerTrade;
import bisq.core.trade.Trade;
import bisq.core.trade.TradeManager;
import bisq.core.trade.messages.TradeMessage;
import bisq.core.trade.statistics.ReferralIdService;
import bisq.core.trade.statistics.TradeStatisticsManager;
import bisq.core.user.User;
import bisq.network.p2p.AckMessage;
import bisq.network.p2p.DecryptedMessageWithPubKey;
import bisq.network.p2p.MailboxMessage;
import bisq.network.p2p.NodeAddress;
import bisq.network.p2p.P2PService;
import bisq.common.crypto.KeyRing;
import bisq.common.crypto.PubKeyRing;
import bisq.common.proto.ProtoUtil;
import bisq.common.proto.persistable.PersistablePayload;
import bisq.common.taskrunner.Model;
import com.google.protobuf.ByteString;
import org.bitcoinj.core.Coin;
import org.bitcoinj.core.Transaction;
import javafx.beans.property.ObjectProperty;
import javafx.beans.property.SimpleObjectProperty;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;
import lombok.Getter;
import lombok.Setter;
import lombok.extern.slf4j.Slf4j;
import javax.annotation.Nullable;
// Fields marked as transient are only used during protocol execution which are based on directMessages so we do not
// persist them.
//todo clean up older fields as well to make most transient
@Getter
@Slf4j
public class ProcessModel implements Model, PersistablePayload {
// Transient/Immutable (net set in constructor so they are not final, but at init)
transient private TradeManager tradeManager;
transient private OpenOfferManager openOfferManager;
transient private BtcWalletService btcWalletService;
transient private BsqWalletService bsqWalletService;
transient private TradeWalletService tradeWalletService;
transient private DaoFacade daoFacade;
transient private Offer offer;
transient private User user;
transient private FilterManager filterManager;
transient private AccountAgeWitnessService accountAgeWitnessService;
transient private TradeStatisticsManager tradeStatisticsManager;
transient private ArbitratorManager arbitratorManager;
transient private MediatorManager mediatorManager;
transient private RefundAgentManager refundAgentManager;
transient private KeyRing keyRing;
transient private P2PService p2PService;
transient private ReferralIdService referralIdService;
// Transient/Mutable
transient private Transaction takeOfferFeeTx;
@Setter
transient private TradeMessage tradeMessage;
@Setter
transient private DecryptedMessageWithPubKey decryptedMessageWithPubKey;
// Added in v1.2.0
@Setter
@Nullable
transient private byte[] delayedPayoutTxSignature;
@Setter
@Nullable
transient private Transaction preparedDelayedPayoutTx;
// Persistable Immutable (private setter only used by PB method)
private TradingPeer tradingPeer = new TradingPeer();
private String offerId;
private String accountId;
private PubKeyRing pubKeyRing;
// Persistable Mutable
@Nullable
@Setter()
private String takeOfferFeeTxId;
@Nullable
@Setter
private byte[] payoutTxSignature;
@Nullable
@Setter
private byte[] preparedDepositTx;
@Nullable
@Setter
private List<RawTransactionInput> rawTransactionInputs;
@Setter
private long changeOutputValue;
@Nullable
@Setter
private String changeOutputAddress;
@Setter
private boolean useSavingsWallet;
@Setter
private long fundsNeededForTradeAsLong;
@Nullable
@Setter
private byte[] myMultiSigPubKey;
// that is used to store temp. the peers address when we get an incoming message before the message is verified.
// After successful verified we copy that over to the trade.tradingPeerAddress
@Nullable
@Setter
private NodeAddress tempTradingPeerNodeAddress;
// Added in v.1.1.6
@Nullable
@Setter
private byte[] mediatedPayoutTxSignature;
@Setter
private long buyerPayoutAmountFromMediation;
@Setter
private long sellerPayoutAmountFromMediation;
// The only trade message where we want to indicate the user the state of the message delivery is the
// CounterCurrencyTransferStartedMessage. We persist the state with the processModel.
@Setter
private ObjectProperty<MessageState> paymentStartedMessageStateProperty = new SimpleObjectProperty<>(MessageState.UNDEFINED);
public ProcessModel() {
}
///////////////////////////////////////////////////////////////////////////////////////////
// PROTO BUFFER
///////////////////////////////////////////////////////////////////////////////////////////
@Override
public protobuf.ProcessModel toProtoMessage() {
final protobuf.ProcessModel.Builder builder = protobuf.ProcessModel.newBuilder()
.setTradingPeer((protobuf.TradingPeer) tradingPeer.toProtoMessage())
.setOfferId(offerId)
.setAccountId(accountId)
.setPubKeyRing(pubKeyRing.toProtoMessage())
.setChangeOutputValue(changeOutputValue)
.setUseSavingsWallet(useSavingsWallet)
.setFundsNeededForTradeAsLong(fundsNeededForTradeAsLong)
.setPaymentStartedMessageState(paymentStartedMessageStateProperty.get().name())
.setBuyerPayoutAmountFromMediation(buyerPayoutAmountFromMediation)
.setSellerPayoutAmountFromMediation(sellerPayoutAmountFromMediation);
Optional.ofNullable(takeOfferFeeTxId).ifPresent(builder::setTakeOfferFeeTxId);
Optional.ofNullable(payoutTxSignature).ifPresent(e -> builder.setPayoutTxSignature(ByteString.copyFrom(payoutTxSignature)));
Optional.ofNullable(preparedDepositTx).ifPresent(e -> builder.setPreparedDepositTx(ByteString.copyFrom(preparedDepositTx)));
Optional.ofNullable(rawTransactionInputs).ifPresent(e -> builder.addAllRawTransactionInputs(
ProtoUtil.collectionToProto(rawTransactionInputs, protobuf.RawTransactionInput.class)));
Optional.ofNullable(changeOutputAddress).ifPresent(builder::setChangeOutputAddress);
Optional.ofNullable(myMultiSigPubKey).ifPresent(e -> builder.setMyMultiSigPubKey(ByteString.copyFrom(myMultiSigPubKey)));
Optional.ofNullable(tempTradingPeerNodeAddress).ifPresent(e -> builder.setTempTradingPeerNodeAddress(tempTradingPeerNodeAddress.toProtoMessage()));
Optional.ofNullable(mediatedPayoutTxSignature).ifPresent(e -> builder.setMediatedPayoutTxSignature(ByteString.copyFrom(e)));
return builder.build();
}
public static ProcessModel fromProto(protobuf.ProcessModel proto, CoreProtoResolver coreProtoResolver) {
ProcessModel processModel = new ProcessModel();
processModel.setTradingPeer(proto.hasTradingPeer() ? TradingPeer.fromProto(proto.getTradingPeer(), coreProtoResolver) : null);
processModel.setOfferId(proto.getOfferId());
processModel.setAccountId(proto.getAccountId());
processModel.setPubKeyRing(PubKeyRing.fromProto(proto.getPubKeyRing()));
processModel.setChangeOutputValue(proto.getChangeOutputValue());
processModel.setUseSavingsWallet(proto.getUseSavingsWallet());
processModel.setFundsNeededForTradeAsLong(proto.getFundsNeededForTradeAsLong());
processModel.setBuyerPayoutAmountFromMediation(proto.getBuyerPayoutAmountFromMediation());
processModel.setSellerPayoutAmountFromMediation(proto.getSellerPayoutAmountFromMediation());
// nullable
processModel.setTakeOfferFeeTxId(ProtoUtil.stringOrNullFromProto(proto.getTakeOfferFeeTxId()));
processModel.setPayoutTxSignature(ProtoUtil.byteArrayOrNullFromProto(proto.getPayoutTxSignature()));
processModel.setPreparedDepositTx(ProtoUtil.byteArrayOrNullFromProto(proto.getPreparedDepositTx()));
List<RawTransactionInput> rawTransactionInputs = proto.getRawTransactionInputsList().isEmpty() ?
null : proto.getRawTransactionInputsList().stream()
.map(RawTransactionInput::fromProto).collect(Collectors.toList());
processModel.setRawTransactionInputs(rawTransactionInputs);
processModel.setChangeOutputAddress(ProtoUtil.stringOrNullFromProto(proto.getChangeOutputAddress()));
processModel.setMyMultiSigPubKey(ProtoUtil.byteArrayOrNullFromProto(proto.getMyMultiSigPubKey()));
processModel.setTempTradingPeerNodeAddress(proto.hasTempTradingPeerNodeAddress() ? NodeAddress.fromProto(proto.getTempTradingPeerNodeAddress()) : null);
String paymentStartedMessageState = proto.getPaymentStartedMessageState().isEmpty() ? MessageState.UNDEFINED.name() : proto.getPaymentStartedMessageState();
ObjectProperty<MessageState> paymentStartedMessageStateProperty = processModel.getPaymentStartedMessageStateProperty();
paymentStartedMessageStateProperty.set(ProtoUtil.enumFromProto(MessageState.class, paymentStartedMessageState));
processModel.setMediatedPayoutTxSignature(ProtoUtil.byteArrayOrNullFromProto(proto.getMediatedPayoutTxSignature()));
return processModel;
}
///////////////////////////////////////////////////////////////////////////////////////////
// API
///////////////////////////////////////////////////////////////////////////////////////////
public void onAllServicesInitialized(Offer offer,
TradeManager tradeManager,
OpenOfferManager openOfferManager,
P2PService p2PService,
BtcWalletService walletService,
BsqWalletService bsqWalletService,
TradeWalletService tradeWalletService,
DaoFacade daoFacade,
ReferralIdService referralIdService,
User user,
FilterManager filterManager,
AccountAgeWitnessService accountAgeWitnessService,
TradeStatisticsManager tradeStatisticsManager,
ArbitratorManager arbitratorManager,
MediatorManager mediatorManager,
RefundAgentManager refundAgentManager,
KeyRing keyRing,
boolean useSavingsWallet,
Coin fundsNeededForTrade) {
this.offer = offer;
this.tradeManager = tradeManager;
this.openOfferManager = openOfferManager;
this.btcWalletService = walletService;
this.bsqWalletService = bsqWalletService;
this.tradeWalletService = tradeWalletService;
this.daoFacade = daoFacade;
this.referralIdService = referralIdService;
this.user = user;
this.filterManager = filterManager;
this.accountAgeWitnessService = accountAgeWitnessService;
this.tradeStatisticsManager = tradeStatisticsManager;
this.arbitratorManager = arbitratorManager;
this.mediatorManager = mediatorManager;
this.refundAgentManager = refundAgentManager;
this.keyRing = keyRing;
this.p2PService = p2PService;
this.useSavingsWallet = useSavingsWallet;
fundsNeededForTradeAsLong = fundsNeededForTrade.value;
offerId = offer.getId();
accountId = user.getAccountId();
pubKeyRing = keyRing.getPubKeyRing();
}
public void removeMailboxMessageAfterProcessing(Trade trade) {
if (tradeMessage instanceof MailboxMessage &&
decryptedMessageWithPubKey != null &&
decryptedMessageWithPubKey.getNetworkEnvelope().equals(tradeMessage)) {
log.debug("Remove decryptedMsgWithPubKey from P2P network. decryptedMsgWithPubKey = " + decryptedMessageWithPubKey);
p2PService.removeEntryFromMailbox(decryptedMessageWithPubKey);
trade.removeDecryptedMessageWithPubKey(decryptedMessageWithPubKey);
}
}
@Override
public void persist() {
log.warn("persist is not implemented in that class");
}
@Override
public void onComplete() {
}
public void setTakeOfferFeeTx(Transaction takeOfferFeeTx) {
this.takeOfferFeeTx = takeOfferFeeTx;
takeOfferFeeTxId = takeOfferFeeTx.getHashAsString();
}
@Nullable
public PaymentAccountPayload getPaymentAccountPayload(Trade trade) {
PaymentAccount paymentAccount;
if (trade instanceof MakerTrade)
paymentAccount = user.getPaymentAccount(offer.getMakerPaymentAccountId());
else
paymentAccount = user.getPaymentAccount(trade.getTakerPaymentAccountId());
return paymentAccount != null ? paymentAccount.getPaymentAccountPayload() : null;
}
public Coin getFundsNeededForTradeAsLong() {
return Coin.valueOf(fundsNeededForTradeAsLong);
}
public Transaction resolveTakeOfferFeeTx(Trade trade) {
if (takeOfferFeeTx == null) {
if (!trade.isCurrencyForTakerFeeBtc())
takeOfferFeeTx = bsqWalletService.getTransaction(takeOfferFeeTxId);
else
takeOfferFeeTx = btcWalletService.getTransaction(takeOfferFeeTxId);
}
return takeOfferFeeTx;
}
public NodeAddress getMyNodeAddress() {
return p2PService.getAddress();
}
public void setPaymentStartedAckMessage(AckMessage ackMessage) {
if (ackMessage.isSuccess()) {
setPaymentStartedMessageState(MessageState.ACKNOWLEDGED);
} else {
setPaymentStartedMessageState(MessageState.FAILED);
}
}
public void setPaymentStartedMessageState(MessageState paymentStartedMessageStateProperty) {
this.paymentStartedMessageStateProperty.set(paymentStartedMessageStateProperty);
}
private void setTradingPeer(TradingPeer tradingPeer) {
this.tradingPeer = tradingPeer;
}
private void setOfferId(String offerId) {
this.offerId = offerId;
}
private void setAccountId(String accountId) {
this.accountId = accountId;
}
private void setPubKeyRing(PubKeyRing pubKeyRing) {
this.pubKeyRing = pubKeyRing;
}
}