Skip to content

Commit

Permalink
Fix issues with delayed mailbox messages, handle ui state when peer i…
Browse files Browse the repository at this point in the history
…s offline
  • Loading branch information
ManfredKarrer committed Mar 11, 2016
1 parent 4fc3b00 commit 7c63e5a
Show file tree
Hide file tree
Showing 11 changed files with 55 additions and 49 deletions.
2 changes: 1 addition & 1 deletion common/src/main/java/io/bitsquare/app/Log.java
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ public static void setup(String fileName, boolean useDetailedLogging) {

logbackLogger = loggerContext.getLogger(org.slf4j.Logger.ROOT_LOGGER_NAME);
//TODO for now use always trace
logbackLogger.setLevel(useDetailedLogging ? Level.TRACE : Level.INFO);
logbackLogger.setLevel(useDetailedLogging ? Level.TRACE : Level.TRACE);
// logbackLogger.setLevel(useDetailedLogging ? Level.TRACE : Level.DEBUG);
logbackLogger.addAppender(appender);
}
Expand Down
3 changes: 2 additions & 1 deletion core/src/main/java/io/bitsquare/trade/Trade.java
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,7 @@ public void init(P2PService p2PService,
OpenOfferManager openOfferManager,
User user,
KeyRing keyRing) {

Log.traceCall();
processModel.onAllServicesInitialized(offer,
tradeManager,
openOfferManager,
Expand All @@ -234,6 +234,7 @@ public void init(P2PService p2PService,

createProtocol();

log.trace("decryptedMsgWithPubKey = " + decryptedMsgWithPubKey);
if (decryptedMsgWithPubKey != null) {
tradeProtocol.applyMailboxMessage(decryptedMsgWithPubKey, this);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ private void updateSpinnerInfo() {
isSpinnerVisible.set(false);
spinnerInfoText.set("");
} else if (showPayFundsScreenDisplayed) {
spinnerInfoText.set("Waiting for funds...");
spinnerInfoText.set("Waiting for receiving funds...");
isSpinnerVisible.set(true);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -453,7 +453,7 @@ private void updateSpinnerInfo() {
isSpinnerVisible.set(false);
spinnerInfoText.set("");
} else if (showPayFundsScreenDisplayed) {
spinnerInfoText.set("Waiting for funds...");
spinnerInfoText.set("Waiting for receiving funds...");
isSpinnerVisible.set(true);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -301,6 +301,7 @@ private void onTradeStateChanged(Trade.State tradeState) {
case BUYER_RECEIVED_FIAT_PAYMENT_RECEIPT_MSG:
case BUYER_COMMITTED_PAYOUT_TX:
case BUYER_STARTED_SEND_PAYOUT_TX:
// TODO would need extra state for wait until msg arrived and PAYOUT_BROAD_CASTED gets called.
buyerState.set(PendingTradesViewModel.BuyerState.WAIT_FOR_BROADCAST_AFTER_UNLOCK);
break;
case SELLER_RECEIVED_AND_COMMITTED_PAYOUT_TX:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,9 +93,13 @@ public void deactivate() {
@Override
protected void addContent() {
addTradeInfoBlock();
blockTextField = addLabelTextField(gridPane, gridRow, "Block(s) to wait until lock time elapsed:", "").second;
timeTextField = addLabelTextField(gridPane, ++gridRow, "Approx. date when payout gets unlocked:").second;
GridPane.setRowSpan(tradeInfoTitledGroupBg, 5);
if (model.getLockTime() > 0) {
blockTextField = addLabelTextField(gridPane, gridRow, "Block(s) to wait until lock time elapsed:", "").second;
timeTextField = addLabelTextField(gridPane, ++gridRow, "Approx. date when payout gets unlocked:").second;
GridPane.setRowSpan(tradeInfoTitledGroupBg, 5);
} else {
GridPane.setRowSpan(tradeInfoTitledGroupBg, 3); //TODO should never reach
}

addInfoBlock();
}
Expand All @@ -107,15 +111,21 @@ protected void addContent() {

@Override
protected String getInfoBlockTitle() {
return "Wait until payout lock time is over";
if (model.getLockTime() > 0)
return "Wait until payout lock time is over";
else
return "Sending payout transaction to peer";
}

@Override
protected String getInfoText() {
return "The payout transaction is signed and finalized by both parties.\n" +
"For reducing bank chargeback risks the payout transaction is blocked by a lock time.\n" +
"After that lock time is over the payout transaction gets published and you receive " +
"your bitcoin.";
if (model.getLockTime() > 0)
return "The payout transaction is signed and finalized by both parties.\n" +
"For reducing bank chargeback risks the payout transaction is blocked by a lock time.\n" +
"After that lock time is over the payout transaction gets published and you receive " +
"your bitcoin.";
else
return "We are sending the payout transaction to the other peer.";
}


Expand All @@ -137,9 +147,12 @@ protected String getWarningText() {
///////////////////////////////////////////////////////////////////////////////////////////

private void updateDateFromBlockHeight(long bestBlocKHeight) {
long missingBlocks = model.getLockTime() - bestBlocKHeight;
blockTextField.setText(String.valueOf(missingBlocks));
timeTextField.setText(model.getOpenDisputeTimeAsFormattedDate());
if (model.getLockTime() > 0) {
long missingBlocks = model.getLockTime() - bestBlocKHeight;

blockTextField.setText(String.valueOf(missingBlocks));
timeTextField.setText(model.getOpenDisputeTimeAsFormattedDate());
}
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@ public void testIsAnyPaymentAccountValidForOffer() {
Offer offer;
Collection<PaymentAccount> paymentAccounts;

paymentAccounts = new ArrayList<>(Arrays.asList(getSepaAccount("EUR", "DE", "1212324", new ArrayList<>(Arrays.asList("AT", "DE")))));
assertTrue(OfferBookViewModel.isAnyPaymentAccountValidForOffer(
getSEPAPaymentMethod("EUR", "AT", new ArrayList(Arrays.asList("AT", "DE")), "PSK"), paymentAccounts));


// empty paymentAccounts
paymentAccounts = new ArrayList<>();
Expand Down Expand Up @@ -56,15 +60,20 @@ public void testIsAnyPaymentAccountValidForOffer() {
assertTrue(OfferBookViewModel.isAnyPaymentAccountValidForOffer(
getSameBankPaymentMethod("EUR", "AT", "PSK"), paymentAccounts));

// offer: SpecificBanks paymentAccount: SpecificBanks - same country, same currency
paymentAccounts = new ArrayList<>(Arrays.asList(getSpecificBanksAccount("EUR", "AT", "PSK",
new ArrayList<>(Arrays.asList("PSK", "Raika")))));
// offer: sepa paymentAccount: sepa - diff. country, same currency
paymentAccounts = new ArrayList<>(Arrays.asList(getSepaAccount("EUR", "DE", "1212324", new ArrayList<>(Arrays.asList("AT", "DE")))));
assertTrue(OfferBookViewModel.isAnyPaymentAccountValidForOffer(
getSpecificBanksPaymentMethod("EUR", "AT", "PSK",
new ArrayList<>(Arrays.asList("PSK", "Raika"))), paymentAccounts));
getSEPAPaymentMethod("EUR", "AT", new ArrayList(Arrays.asList("AT", "DE")), "PSK"), paymentAccounts));


//////

// offer: sepa paymentAccount: sepa - same country, same currency
paymentAccounts = new ArrayList<>(Arrays.asList(getSepaAccount("EUR", "AT", "1212324", new ArrayList<>(Arrays.asList("AT", "DE")))));
assertTrue(OfferBookViewModel.isAnyPaymentAccountValidForOffer(
getSEPAPaymentMethod("EUR", "AT", new ArrayList(Arrays.asList("AT", "DE")), "PSK"), paymentAccounts));


// offer: sepa paymentAccount: nationalBank - same country, same currency
// wrong method
paymentAccounts = new ArrayList<>(Arrays.asList(getNationalBankAccount("EUR", "AT", "PSK")));
Expand Down
13 changes: 3 additions & 10 deletions network/src/main/java/io/bitsquare/p2p/network/Connection.java
Original file line number Diff line number Diff line change
Expand Up @@ -407,7 +407,7 @@ public void shutDown(CloseConnectionReason closeConnectionReason, @Nullable Runn
}
} else {
//TODO find out why we get called that
log.warn("stopped was already true at shutDown call");
log.debug("stopped was already at shutDown call");
UserThread.execute(() -> doShutDown(closeConnectionReason, shutDownCompleteHandler));
}
}
Expand Down Expand Up @@ -536,20 +536,13 @@ public void handleConnectionException(Throwable e) {
closeConnectionReason = CloseConnectionReason.RESET;
} else if (e instanceof SocketTimeoutException || e instanceof TimeoutException) {
closeConnectionReason = CloseConnectionReason.SOCKET_TIMEOUT;
log.warn("SocketTimeoutException at socket " + socket.toString() + "\n\tconnection={}" + this);
log.debug("SocketTimeoutException at socket " + socket.toString() + "\n\tconnection={}" + this);
} else if (e instanceof EOFException || e instanceof StreamCorruptedException) {
closeConnectionReason = CloseConnectionReason.TERMINATED;
} else {
closeConnectionReason = CloseConnectionReason.UNKNOWN_EXCEPTION;

String message;
if (e.getMessage() != null)
message = e.getMessage();
else
message = e.toString();

log.warn("Unknown reason for exception at socket {}\n\tconnection={}\n\tException=",
socket.toString(), this, message);
socket.toString(), this, e.toString());
e.printStackTrace();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,14 +112,14 @@ public void broadcast(BroadcastMessage message, @Nullable NodeAddress sender, Re
numOfPeers = connectedPeersList.size();
int factor = 1;
if (!isDataOwner) {
// for not data owner (relay nodes) we send to max. 4 nodes and use a longer delay
numOfPeers = Math.min(4, connectedPeersList.size());
// for not data owner (relay nodes) we send to max. 5 nodes and use a longer delay
numOfPeers = Math.min(5, connectedPeersList.size());
factor = 2;
}
log.info("Broadcast message to {} peers out of {} total connected peers.", numOfPeers, connectedPeersSet.size());
for (int i = 0; i < numOfPeers; i++) {
final long minDelay = i * 50 * factor + 1;
final long maxDelay = minDelay * 2 + 50 * factor;
final long minDelay = i * 30 * factor + 1;
final long maxDelay = minDelay * 2 + 30 * factor;
final Connection connection = connectedPeersList.get(i);
UserThread.runAfterRandomDelay(() -> sendToPeer(connection, message), minDelay, maxDelay, TimeUnit.MILLISECONDS);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,12 @@
import io.bitsquare.p2p.peers.getdata.messages.GetUpdatedDataRequest;
import io.bitsquare.p2p.peers.getdata.messages.PreliminaryGetDataRequest;
import io.bitsquare.p2p.storage.P2PDataStorage;
import io.bitsquare.p2p.storage.storageentry.ProtectedStorageEntry;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import java.util.concurrent.TimeUnit;

import static com.google.common.base.Preconditions.checkArgument;

Expand Down Expand Up @@ -160,17 +156,10 @@ public void onMessage(Message message, Connection connection) {
"RequestDataHandler.onMessage: connection.getPeersNodeAddressOptional() must be present " +
"at that moment");

final List<ProtectedStorageEntry> dataList = new ArrayList<>(((GetDataResponse) message).dataSet);
final NodeAddress sender = connection.getPeersNodeAddressOptional().get();
for (int i = 0; i < dataList.size(); i++) {
// roughly 3-6 sec for 100 entries
final long minDelay = i * 30 + 1;
final long maxDelay = minDelay * 2 + 30;
final ProtectedStorageEntry protectedData = dataList.get(i);
// TODO questionable if it is needed to relay the data to our peers
UserThread.runAfterRandomDelay(() -> dataStorage.add(protectedData, sender, null, false),
minDelay, maxDelay, TimeUnit.MILLISECONDS);
}
((GetDataResponse) message).dataSet.stream().forEach(protectedStorageEntry -> {
dataStorage.add(protectedStorageEntry, sender, null, false);
});

cleanup();
listener.onComplete();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -254,7 +254,7 @@ public boolean refreshTTL(RefreshTTLMessage refreshTTLMessage, @Nullable NodeAdd
return result;
}
} else {
log.warn("We don't have data for that refresh message in our map.");
log.debug("We don't have data for that refresh message in our map. That is expected if we missed the data publishing.");
return false;
}
}
Expand Down

0 comments on commit 7c63e5a

Please sign in to comment.