Refactor:

- Rename XmrProofInfo to XmrTxProofModel
This commit is contained in:
chimp1984 2020-08-30 19:37:22 -05:00
parent 87070531dd
commit a758880211
No known key found for this signature in database
GPG key ID: 9801B4EC591F90E3
6 changed files with 54 additions and 54 deletions

View file

@ -187,7 +187,7 @@ public class XmrAutoConfirmationManager {
List<String> serviceAddresses = preferences.getAutoConfirmSettings().serviceAddresses;
txProofResultsPending.put(trade.getId(), serviceAddresses.size()); // need result from each service address
for (String serviceAddress : serviceAddresses) {
XmrProofInfo xmrProofInfo = new XmrProofInfo(
XmrTxProofModel xmrTxProofModel = new XmrTxProofModel(
txHash,
txKey,
address,
@ -195,10 +195,10 @@ public class XmrAutoConfirmationManager {
trade.getDate(),
confirmsRequired,
serviceAddress);
xmrTransferProofService.requestProof(xmrProofInfo,
xmrTransferProofService.requestProof(xmrTxProofModel,
result -> {
if (!handleProofResult(result, trade))
xmrTransferProofService.terminateRequest(xmrProofInfo);
xmrTransferProofService.terminateRequest(xmrTxProofModel);
},
(errorMsg, throwable) -> {
log.warn(errorMsg);

View file

@ -45,7 +45,7 @@ class XmrTransferProofRequest {
private final ListeningExecutorService executorService = Utilities.getListeningExecutorService(
"XmrTransferProofRequester", 3, 5, 10 * 60);
private final XmrTxProofHttpClient httpClient;
private final XmrProofInfo xmrProofInfo;
private final XmrTxProofModel xmrTxProofModel;
private final Consumer<XmrTxProofResult> resultHandler;
private final FaultHandler faultHandler;
@ -58,16 +58,16 @@ class XmrTransferProofRequest {
///////////////////////////////////////////////////////////////////////////////////////////
XmrTransferProofRequest(Socks5ProxyProvider socks5ProxyProvider,
XmrProofInfo xmrProofInfo,
XmrTxProofModel xmrTxProofModel,
Consumer<XmrTxProofResult> resultHandler,
FaultHandler faultHandler) {
this.httpClient = new XmrTxProofHttpClient(socks5ProxyProvider);
this.httpClient.setBaseUrl("http://" + xmrProofInfo.getServiceAddress());
if (xmrProofInfo.getServiceAddress().matches("^192.*|^localhost.*")) {
log.info("Ignoring Socks5 proxy for local net address: {}", xmrProofInfo.getServiceAddress());
this.httpClient.setBaseUrl("http://" + xmrTxProofModel.getServiceAddress());
if (xmrTxProofModel.getServiceAddress().matches("^192.*|^localhost.*")) {
log.info("Ignoring Socks5 proxy for local net address: {}", xmrTxProofModel.getServiceAddress());
this.httpClient.setIgnoreSocks5Proxy(true);
}
this.xmrProofInfo = xmrProofInfo;
this.xmrTxProofModel = xmrTxProofModel;
this.resultHandler = resultHandler;
this.faultHandler = faultHandler;
this.terminated = false;
@ -91,14 +91,14 @@ class XmrTransferProofRequest {
return;
}
ListenableFuture<XmrTxProofResult> future = executorService.submit(() -> {
Thread.currentThread().setName("XmrTransferProofRequest-" + xmrProofInfo.getUID());
String param = "/api/outputs?txhash=" + xmrProofInfo.getTxHash() +
"&address=" + xmrProofInfo.getRecipientAddress() +
"&viewkey=" + xmrProofInfo.getTxKey() +
Thread.currentThread().setName("XmrTransferProofRequest-" + xmrTxProofModel.getUID());
String param = "/api/outputs?txhash=" + xmrTxProofModel.getTxHash() +
"&address=" + xmrTxProofModel.getRecipientAddress() +
"&viewkey=" + xmrTxProofModel.getTxKey() +
"&txprove=1";
log.info("Requesting from {} with param {}", httpClient.getBaseUrl(), param);
String json = httpClient.requestWithGET(param, "User-Agent", "bisq/" + Version.VERSION);
XmrTxProofResult autoConfirmResult = XmrTxProofParser.parse(xmrProofInfo, json);
XmrTxProofResult autoConfirmResult = XmrTxProofParser.parse(xmrTxProofModel, json);
log.info("Response json {} resulted in autoConfirmResult {}", json, autoConfirmResult);
return autoConfirmResult;
});

View file

@ -42,10 +42,10 @@ class XmrTransferProofService {
socks5ProxyProvider = provider;
}
void requestProof(XmrProofInfo xmrProofInfo,
void requestProof(XmrTxProofModel xmrTxProofModel,
Consumer<XmrTxProofResult> resultHandler,
FaultHandler faultHandler) {
String uid = xmrProofInfo.getUID();
String uid = xmrTxProofModel.getUID();
if (map.containsKey(uid)) {
log.warn("We started a proof request for uid {} already", uid);
return;
@ -54,7 +54,7 @@ class XmrTransferProofService {
XmrTransferProofRequest requester = new XmrTransferProofRequest(
socks5ProxyProvider,
xmrProofInfo,
xmrTxProofModel,
result -> {
if (result.isSuccessState()) {
cleanup(uid);
@ -69,8 +69,8 @@ class XmrTransferProofService {
requester.request();
}
void terminateRequest(XmrProofInfo xmrProofInfo) {
String uid = xmrProofInfo.getUID();
void terminateRequest(XmrTxProofModel xmrTxProofModel) {
String uid = xmrTxProofModel.getUID();
XmrTransferProofRequest requester = map.getOrDefault(uid, null);
if (requester != null) {
log.info("Terminating API request for request with uid {}", uid);

View file

@ -24,7 +24,7 @@ import lombok.extern.slf4j.Slf4j;
@Slf4j
@Value
class XmrProofInfo {
class XmrTxProofModel {
private final String txHash;
private final String txKey;
private final String recipientAddress;
@ -33,7 +33,7 @@ class XmrProofInfo {
private final int confirmsRequired;
private final String serviceAddress;
XmrProofInfo(
XmrTxProofModel(
String txHash,
String txKey,
String recipientAddress,

View file

@ -33,8 +33,8 @@ import lombok.extern.slf4j.Slf4j;
@Slf4j
class XmrTxProofParser {
static XmrTxProofResult parse(XmrProofInfo xmrProofInfo, String jsonTxt) {
String txHash = xmrProofInfo.getTxHash();
static XmrTxProofResult parse(XmrTxProofModel xmrTxProofModel, String jsonTxt) {
String txHash = xmrTxProofModel.getTxHash();
try {
JsonObject json = new Gson().fromJson(jsonTxt, JsonObject.class);
if (json == null) {
@ -59,7 +59,7 @@ class XmrTxProofParser {
if (jsonAddress == null) {
return new XmrTxProofResult(XmrTxProofResult.State.API_INVALID, "Missing address field");
} else {
String expectedAddressHex = CryptoNoteUtils.convertToRawHex(xmrProofInfo.getRecipientAddress());
String expectedAddressHex = CryptoNoteUtils.convertToRawHex(xmrTxProofModel.getRecipientAddress());
if (!jsonAddress.getAsString().equalsIgnoreCase(expectedAddressHex)) {
log.warn("address {}, expected: {}", jsonAddress.getAsString(), expectedAddressHex);
return new XmrTxProofResult(XmrTxProofResult.State.ADDRESS_INVALID, null);
@ -82,8 +82,8 @@ class XmrTxProofParser {
if (jsonViewkey == null) {
return new XmrTxProofResult(XmrTxProofResult.State.API_INVALID, "Missing viewkey field");
} else {
if (!jsonViewkey.getAsString().equalsIgnoreCase(xmrProofInfo.getTxKey())) {
log.warn("viewkey {}, expected: {}", jsonViewkey.getAsString(), xmrProofInfo.getTxKey());
if (!jsonViewkey.getAsString().equalsIgnoreCase(xmrTxProofModel.getTxKey())) {
log.warn("viewkey {}, expected: {}", jsonViewkey.getAsString(), xmrTxProofModel.getTxKey());
return new XmrTxProofResult(XmrTxProofResult.State.TX_KEY_INVALID, null);
}
}
@ -94,7 +94,7 @@ class XmrTxProofParser {
if (jsonTimestamp == null) {
return new XmrTxProofResult(XmrTxProofResult.State.API_INVALID, "Missing tx_timestamp field");
} else {
long tradeDateSeconds = xmrProofInfo.getTradeDate().getTime() / 1000;
long tradeDateSeconds = xmrTxProofModel.getTradeDate().getTime() / 1000;
long difference = tradeDateSeconds - jsonTimestamp.getAsLong();
// Accept up to 2 hours difference. Some tolerance is needed if users clock is out of sync
if (difference > TimeUnit.HOURS.toSeconds(2) && !DevEnv.isDevMode()) {
@ -124,8 +124,8 @@ class XmrTxProofParser {
if (out.get("match").getAsBoolean()) {
anyMatchFound = true;
long jsonAmount = out.get("amount").getAsLong();
if (jsonAmount == xmrProofInfo.getAmount() || DevEnv.isDevMode()) { // any amount ok in dev mode
int confirmsRequired = xmrProofInfo.getConfirmsRequired();
if (jsonAmount == xmrTxProofModel.getAmount() || DevEnv.isDevMode()) { // any amount ok in dev mode
int confirmsRequired = xmrTxProofModel.getConfirmsRequired();
if (confirmations < confirmsRequired)
// we return TX_NOT_CONFIRMED which will cause a retry later
return new XmrTxProofResult(XmrTxProofResult.State.TX_NOT_CONFIRMED, confirmations, confirmsRequired);

View file

@ -11,7 +11,7 @@ import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
public class XmrTxProofParserTest {
private XmrProofInfo xmrProofInfo;
private XmrTxProofModel xmrTxProofModel;
private String recipientAddressHex = "e957dac72bcec80d59b2fecacfa7522223b6a5df895b7e388e60297e85f3f867b42f43e8d9f086a99a997704ceb92bd9cd99d33952de90c9f5f93c82c62360ae";
private String txHash = "488e48ab0c7e69028d19f787ec57fd496ff114caba9ab265bfd41a3ea0e4687d";
private String txKey = "6c336e52ed537676968ee319af6983c80b869ca6a732b5962c02748b486f8f0f";
@ -25,7 +25,7 @@ public class XmrTxProofParserTest {
int confirmsRequired = 10;
String serviceAddress = "127.0.0.1:8081";
xmrProofInfo = new XmrProofInfo(
xmrTxProofModel = new XmrTxProofModel(
txHash,
txKey,
recipientAddress,
@ -37,44 +37,44 @@ public class XmrTxProofParserTest {
@Test
public void testKey() {
assertTrue(xmrProofInfo.getUID().contains(xmrProofInfo.getTxHash()));
assertTrue(xmrProofInfo.getUID().contains(xmrProofInfo.getServiceAddress()));
assertFalse(xmrProofInfo.getUID().contains(xmrProofInfo.getRecipientAddress()));
assertTrue(xmrTxProofModel.getUID().contains(xmrTxProofModel.getTxHash()));
assertTrue(xmrTxProofModel.getUID().contains(xmrTxProofModel.getServiceAddress()));
assertFalse(xmrTxProofModel.getUID().contains(xmrTxProofModel.getRecipientAddress()));
}
@Test
public void testJsonRoot() {
// checking what happens when bad input is provided
assertTrue(XmrTxProofParser.parse(xmrProofInfo,
assertTrue(XmrTxProofParser.parse(xmrTxProofModel,
"invalid json data").getState() == XmrTxProofResult.State.API_INVALID);
assertTrue(XmrTxProofParser.parse(xmrProofInfo,
assertTrue(XmrTxProofParser.parse(xmrTxProofModel,
"").getState() == XmrTxProofResult.State.API_INVALID);
assertTrue(XmrTxProofParser.parse(xmrProofInfo,
assertTrue(XmrTxProofParser.parse(xmrTxProofModel,
"[]").getState() == XmrTxProofResult.State.API_INVALID);
assertTrue(XmrTxProofParser.parse(xmrProofInfo,
assertTrue(XmrTxProofParser.parse(xmrTxProofModel,
"{}").getState() == XmrTxProofResult.State.API_INVALID);
}
@Test
public void testJsonTopLevel() {
// testing the top level fields: data and status
assertTrue(XmrTxProofParser.parse(xmrProofInfo,
assertTrue(XmrTxProofParser.parse(xmrTxProofModel,
"{'data':{'title':''},'status':'fail'}" )
.getState() == XmrTxProofResult.State.TX_NOT_FOUND);
assertTrue(XmrTxProofParser.parse(xmrProofInfo,
assertTrue(XmrTxProofParser.parse(xmrTxProofModel,
"{'data':{'title':''},'missingstatus':'success'}" )
.getState() == XmrTxProofResult.State.API_INVALID);
assertTrue(XmrTxProofParser.parse(xmrProofInfo,
assertTrue(XmrTxProofParser.parse(xmrTxProofModel,
"{'missingdata':{'title':''},'status':'success'}" )
.getState() == XmrTxProofResult.State.API_INVALID);
}
@Test
public void testJsonAddress() {
assertTrue(XmrTxProofParser.parse(xmrProofInfo,
assertTrue(XmrTxProofParser.parse(xmrTxProofModel,
"{'data':{'missingaddress':'irrelevant'},'status':'success'}" )
.getState() == XmrTxProofResult.State.API_INVALID);
assertTrue(XmrTxProofParser.parse(xmrProofInfo,
assertTrue(XmrTxProofParser.parse(xmrTxProofModel,
"{'data':{'address':'e957dac7'},'status':'success'}" )
.getState() == XmrTxProofResult.State.ADDRESS_INVALID);
}
@ -82,11 +82,11 @@ public class XmrTxProofParserTest {
@Test
public void testJsonTxHash() {
String missing_tx_hash = "{'data':{'address':'" + recipientAddressHex + "'}, 'status':'success'}";
assertTrue(XmrTxProofParser.parse(xmrProofInfo, missing_tx_hash).getState()
assertTrue(XmrTxProofParser.parse(xmrTxProofModel, missing_tx_hash).getState()
== XmrTxProofResult.State.API_INVALID);
String invalid_tx_hash = "{'data':{'address':'" + recipientAddressHex + "', 'tx_hash':'488e48'}, 'status':'success'}";
assertTrue(XmrTxProofParser.parse(xmrProofInfo, invalid_tx_hash).getState()
assertTrue(XmrTxProofParser.parse(xmrTxProofModel, invalid_tx_hash).getState()
== XmrTxProofResult.State.TX_HASH_INVALID);
}
@ -94,13 +94,13 @@ public class XmrTxProofParserTest {
public void testJsonTxKey() {
String missing_tx_key = "{'data':{'address':'" + recipientAddressHex + "', " +
"'tx_hash':'" + txHash + "'}, 'status':'success'}";
assertTrue(XmrTxProofParser.parse(xmrProofInfo, missing_tx_key).getState()
assertTrue(XmrTxProofParser.parse(xmrTxProofModel, missing_tx_key).getState()
== XmrTxProofResult.State.API_INVALID);
String invalid_tx_key = "{'data':{'address':'" + recipientAddressHex + "', " +
"'tx_hash':'" + txHash + "', " +
"'viewkey':'cdce04'}, 'status':'success'}";
assertTrue(XmrTxProofParser.parse(xmrProofInfo, invalid_tx_key).getState()
assertTrue(XmrTxProofParser.parse(xmrTxProofModel, invalid_tx_key).getState()
== XmrTxProofResult.State.TX_KEY_INVALID);
}
@ -109,14 +109,14 @@ public class XmrTxProofParserTest {
String missing_tx_timestamp = "{'data':{'address':'" + recipientAddressHex + "', " +
"'tx_hash':'" + txHash + "'," +
"'viewkey':'" + txKey + "'}, 'status':'success'}";
assertTrue(XmrTxProofParser.parse(xmrProofInfo, missing_tx_timestamp).getState()
assertTrue(XmrTxProofParser.parse(xmrTxProofModel, missing_tx_timestamp).getState()
== XmrTxProofResult.State.API_INVALID);
String invalid_tx_timestamp = "{'data':{'address':'" + recipientAddressHex + "', " +
"'tx_hash':'" + txHash + "', " +
"'viewkey':'" + txKey + "'," +
"'tx_timestamp':'12345'}, 'status':'success'}";
assertTrue(XmrTxProofParser.parse(xmrProofInfo, invalid_tx_timestamp).getState()
assertTrue(XmrTxProofParser.parse(xmrTxProofModel, invalid_tx_timestamp).getState()
== XmrTxProofResult.State.TRADE_DATE_NOT_MATCHING);
}
@ -134,26 +134,26 @@ public class XmrTxProofParserTest {
"'viewkey':'" + txKey + "', " +
"'tx_timestamp':'" + Long.toString(epochDate) + "'}" +
"}";
assertTrue(XmrTxProofParser.parse(xmrProofInfo, json).getState()
assertTrue(XmrTxProofParser.parse(xmrTxProofModel, json).getState()
== XmrTxProofResult.State.PROOF_OK);
json = json.replaceFirst("777", "0");
assertTrue(XmrTxProofParser.parse(xmrProofInfo, json).getState()
assertTrue(XmrTxProofParser.parse(xmrTxProofModel, json).getState()
== XmrTxProofResult.State.TX_NOT_CONFIRMED);
json = json.replaceFirst("100000000000", "100000000001");
assertTrue(XmrTxProofParser.parse(xmrProofInfo, json).getState()
assertTrue(XmrTxProofParser.parse(xmrTxProofModel, json).getState()
== XmrTxProofResult.State.AMOUNT_NOT_MATCHING);
// Revert change of amount
json = json.replaceFirst("100000000001", "100000000000");
json = json.replaceFirst("'match':true", "'match':false");
assertTrue(XmrTxProofParser.parse(xmrProofInfo, json).getState()
assertTrue(XmrTxProofParser.parse(xmrTxProofModel, json).getState()
== XmrTxProofResult.State.NO_MATCH_FOUND);
}
@Test
public void testJsonFail() {
String failedJson = "{\"data\":null,\"message\":\"Cant parse tx hash: a\",\"status\":\"error\"}";
assertTrue(XmrTxProofParser.parse(xmrProofInfo, failedJson).getState()
assertTrue(XmrTxProofParser.parse(xmrTxProofModel, failedJson).getState()
== XmrTxProofResult.State.API_INVALID);
}
}