Example #1
0
  private void initPendingTrades() {
    if (firstPeerAuthenticatedListener != null)
      p2PService.removeP2PServiceListener(firstPeerAuthenticatedListener);

    List<Trade> failedTrades = new ArrayList<>();
    for (Trade trade : trades) {
      // We continue an interrupted trade.
      // TODO if the peer has changed its IP address, we need to make another findPeer request. At
      // the moment we use the peer stored in trade to
      // continue the trade, but that might fail.

      // TODO
      /* if (trade.isFailedState()) {
          failedTrades.add(trade);
      }
      else {*/
      trade.setStorage(tradableListStorage);
      trade.updateDepositTxFromWallet(tradeWalletService);
      initTrade(trade);

      // after we are authenticated we remove mailbox messages.
      DecryptedMsgWithPubKey mailboxMessage = trade.getMailboxMessage();
      if (mailboxMessage != null) {
        log.trace("initPendingTrades/removeEntryFromMailbox mailboxMessage = " + mailboxMessage);
        p2PService.removeEntryFromMailbox(mailboxMessage);
        trade.setMailboxMessage(null);
      }
      // }
    }
    pendingTradesInitialized.set(true);

    failedTrades.stream().filter(Trade::isTakerFeePaid).forEach(this::addTradeToFailedTrades);
  }
Example #2
0
 public void removeArbitrator(
     Arbitrator arbitrator, ResultHandler resultHandler, ErrorMessageHandler errorMessageHandler) {
   log.debug("removeArbitrator arbitrator.hashCode() " + arbitrator.hashCode());
   if (p2PService.removeData(arbitrator)) {
     log.trace("Remove arbitrator from network was successful. Arbitrator = " + arbitrator);
     resultHandler.handleResult();
   } else {
     errorMessageHandler.handleErrorMessage("Remove arbitrator failed");
   }
 }
Example #3
0
 public void addArbitrator(
     Arbitrator arbitrator,
     final ResultHandler resultHandler,
     ErrorMessageHandler errorMessageHandler) {
   log.debug("addArbitrator arbitrator.hashCode() " + arbitrator.hashCode());
   boolean result = p2PService.addData(arbitrator);
   if (result) {
     log.trace("Add arbitrator to network was successful. Arbitrator = " + arbitrator);
     resultHandler.handleResult();
   } else {
     errorMessageHandler.handleErrorMessage("Add arbitrator failed");
   }
 }
Example #4
0
  public Map<Address, Arbitrator> getArbitrators() {
    Set<Arbitrator> arbitratorSet =
        p2PService
            .getDataMap()
            .values()
            .stream()
            .filter(e -> e.expirablePayload instanceof Arbitrator)
            .map(e -> (Arbitrator) e.expirablePayload)
            .collect(Collectors.toSet());

    Map<Address, Arbitrator> map = new HashMap<>();
    for (Arbitrator arbitrator : arbitratorSet) {
      Address arbitratorAddress = arbitrator.getArbitratorAddress();
      if (!map.containsKey(arbitratorAddress)) map.put(arbitratorAddress, arbitrator);
      else
        log.warn(
            "arbitratorAddress already exist in arbitrator map. Seems an arbitrator object is already registered with the same address.");
    }
    return map;
  }
Example #5
0
 public void addHashSetChangedListener(HashMapChangedListener hashMapChangedListener) {
   p2PService.addHashSetChangedListener(hashMapChangedListener);
 }
Example #6
0
  @Inject
  public TradeManager(
      User user,
      KeyRing keyRing,
      WalletService walletService,
      TradeWalletService tradeWalletService,
      OpenOfferManager openOfferManager,
      ClosedTradableManager closedTradableManager,
      FailedTradesManager failedTradesManager,
      ArbitratorManager arbitratorManager,
      P2PService p2PService,
      @Named("storage.dir") File storageDir) {
    this.user = user;
    this.keyRing = keyRing;
    this.walletService = walletService;
    this.tradeWalletService = tradeWalletService;
    this.openOfferManager = openOfferManager;
    this.closedTradableManager = closedTradableManager;
    this.failedTradesManager = failedTradesManager;
    this.arbitratorManager = arbitratorManager;
    this.p2PService = p2PService;

    tradableListStorage = new Storage<>(storageDir);
    this.trades = new TradableList<>(tradableListStorage, "PendingTrades");

    p2PService.addDecryptedMailListener(
        new DecryptedMailListener() {
          @Override
          public void onMailMessage(
              DecryptedMsgWithPubKey decryptedMsgWithPubKey, Address peerAddress) {
            Message message = decryptedMsgWithPubKey.message;

            // Handler for incoming initial messages from taker
            if (message instanceof PayDepositRequest) {
              log.trace("Received PayDepositRequest: " + message);
              handleInitialTakeOfferRequest((PayDepositRequest) message, peerAddress);
            }
          }
        });
    p2PService.addDecryptedMailboxListener(
        new DecryptedMailboxListener() {
          @Override
          public void onMailboxMessageAdded(
              DecryptedMsgWithPubKey decryptedMsgWithPubKey, Address senderAddress) {
            log.trace(
                "onMailboxMessageAdded decryptedMessageWithPubKey: " + decryptedMsgWithPubKey);
            log.trace("onMailboxMessageAdded senderAddress: " + senderAddress);
            Message message = decryptedMsgWithPubKey.message;
            if (message instanceof PayDepositRequest) {
              // TODO is that used????
              PayDepositRequest payDepositRequest = (PayDepositRequest) message;
              log.trace("Received payDepositRequest: " + payDepositRequest);
              if (payDepositRequest.getSenderAddress().equals(senderAddress))
                handleInitialTakeOfferRequest(payDepositRequest, senderAddress);
              else log.warn("Peer address not matching for payDepositRequest");
            } else if (message instanceof TradeMessage) {
              log.trace("Received TradeMessage: " + message);
              String tradeId = ((TradeMessage) message).tradeId;
              Optional<Trade> tradeOptional =
                  trades.stream().filter(e -> e.getId().equals(tradeId)).findAny();
              if (tradeOptional.isPresent())
                tradeOptional.get().setMailboxMessage(decryptedMsgWithPubKey);
            }
          }
        });

    firstPeerAuthenticatedListener =
        new FirstPeerAuthenticatedListener() {
          @Override
          public void onFirstPeerAuthenticated() {
            // give a bit delay to be sure other listeners have executed its work
            UserThread.runAfter(() -> initPendingTrades(), 100, TimeUnit.MILLISECONDS);
          }
        };
    p2PService.addP2PServiceListener(firstPeerAuthenticatedListener);
  }
 boolean isBootstrapped() {
   return p2PService.isBootstrapped();
 }