Exemple #1
1
  static {
    if (userServer != null) {
      Account.addListener(
          account -> {
            JSONObject response = new JSONObject();
            response.put("response", "setBalance");
            response.put("balanceNQT", account.getUnconfirmedBalanceNQT());
            byte[] accountPublicKey = account.getPublicKey();
            Users.users
                .values()
                .forEach(
                    user -> {
                      if (user.getSecretPhrase() != null
                          && Arrays.equals(user.getPublicKey(), accountPublicKey)) {
                        user.send(response);
                      }
                    });
          },
          Account.Event.UNCONFIRMED_BALANCE);

      Peers.addListener(
          peer -> {
            JSONObject response = new JSONObject();
            JSONArray removedActivePeers = new JSONArray();
            JSONObject removedActivePeer = new JSONObject();
            removedActivePeer.put("index", Users.getIndex(peer));
            removedActivePeers.add(removedActivePeer);
            response.put("removedActivePeers", removedActivePeers);
            JSONArray removedKnownPeers = new JSONArray();
            JSONObject removedKnownPeer = new JSONObject();
            removedKnownPeer.put("index", Users.getIndex(peer));
            removedKnownPeers.add(removedKnownPeer);
            response.put("removedKnownPeers", removedKnownPeers);
            JSONArray addedBlacklistedPeers = new JSONArray();
            JSONObject addedBlacklistedPeer = new JSONObject();
            addedBlacklistedPeer.put("index", Users.getIndex(peer));
            addedBlacklistedPeer.put("address", peer.getHost());
            addedBlacklistedPeer.put(
                "announcedAddress", Convert.truncate(peer.getAnnouncedAddress(), "-", 25, true));
            addedBlacklistedPeer.put("software", peer.getSoftware());
            addedBlacklistedPeers.add(addedBlacklistedPeer);
            response.put("addedBlacklistedPeers", addedBlacklistedPeers);
            Users.sendNewDataToAll(response);
          },
          Peers.Event.BLACKLIST);

      Peers.addListener(
          peer -> {
            JSONObject response = new JSONObject();
            JSONArray removedActivePeers = new JSONArray();
            JSONObject removedActivePeer = new JSONObject();
            removedActivePeer.put("index", Users.getIndex(peer));
            removedActivePeers.add(removedActivePeer);
            response.put("removedActivePeers", removedActivePeers);
            JSONArray addedKnownPeers = new JSONArray();
            JSONObject addedKnownPeer = new JSONObject();
            addedKnownPeer.put("index", Users.getIndex(peer));
            addedKnownPeer.put("address", peer.getHost());
            addedKnownPeer.put(
                "announcedAddress", Convert.truncate(peer.getAnnouncedAddress(), "-", 25, true));
            addedKnownPeer.put("software", peer.getSoftware());
            addedKnownPeers.add(addedKnownPeer);
            response.put("addedKnownPeers", addedKnownPeers);
            Users.sendNewDataToAll(response);
          },
          Peers.Event.DEACTIVATE);

      Peers.addListener(
          peer -> {
            JSONObject response = new JSONObject();
            JSONArray removedBlacklistedPeers = new JSONArray();
            JSONObject removedBlacklistedPeer = new JSONObject();
            removedBlacklistedPeer.put("index", Users.getIndex(peer));
            removedBlacklistedPeers.add(removedBlacklistedPeer);
            response.put("removedBlacklistedPeers", removedBlacklistedPeers);
            JSONArray addedKnownPeers = new JSONArray();
            JSONObject addedKnownPeer = new JSONObject();
            addedKnownPeer.put("index", Users.getIndex(peer));
            addedKnownPeer.put("address", peer.getHost());
            addedKnownPeer.put(
                "announcedAddress", Convert.truncate(peer.getAnnouncedAddress(), "-", 25, true));
            addedKnownPeer.put("software", peer.getSoftware());
            addedKnownPeers.add(addedKnownPeer);
            response.put("addedKnownPeers", addedKnownPeers);
            Users.sendNewDataToAll(response);
          },
          Peers.Event.UNBLACKLIST);

      Peers.addListener(
          peer -> {
            JSONObject response = new JSONObject();
            JSONArray removedKnownPeers = new JSONArray();
            JSONObject removedKnownPeer = new JSONObject();
            removedKnownPeer.put("index", Users.getIndex(peer));
            removedKnownPeers.add(removedKnownPeer);
            response.put("removedKnownPeers", removedKnownPeers);
            Users.sendNewDataToAll(response);
          },
          Peers.Event.REMOVE);

      Peers.addListener(
          peer -> {
            JSONObject response = new JSONObject();
            JSONArray changedActivePeers = new JSONArray();
            JSONObject changedActivePeer = new JSONObject();
            changedActivePeer.put("index", Users.getIndex(peer));
            changedActivePeer.put("downloaded", peer.getDownloadedVolume());
            changedActivePeers.add(changedActivePeer);
            response.put("changedActivePeers", changedActivePeers);
            Users.sendNewDataToAll(response);
          },
          Peers.Event.DOWNLOADED_VOLUME);

      Peers.addListener(
          peer -> {
            JSONObject response = new JSONObject();
            JSONArray changedActivePeers = new JSONArray();
            JSONObject changedActivePeer = new JSONObject();
            changedActivePeer.put("index", Users.getIndex(peer));
            changedActivePeer.put("uploaded", peer.getUploadedVolume());
            changedActivePeers.add(changedActivePeer);
            response.put("changedActivePeers", changedActivePeers);
            Users.sendNewDataToAll(response);
          },
          Peers.Event.UPLOADED_VOLUME);

      Peers.addListener(
          peer -> {
            JSONObject response = new JSONObject();
            JSONArray changedActivePeers = new JSONArray();
            JSONObject changedActivePeer = new JSONObject();
            changedActivePeer.put("index", Users.getIndex(peer));
            changedActivePeer.put("weight", peer.getWeight());
            changedActivePeers.add(changedActivePeer);
            response.put("changedActivePeers", changedActivePeers);
            Users.sendNewDataToAll(response);
          },
          Peers.Event.WEIGHT);

      Peers.addListener(
          peer -> {
            JSONObject response = new JSONObject();
            JSONArray removedKnownPeers = new JSONArray();
            JSONObject removedKnownPeer = new JSONObject();
            removedKnownPeer.put("index", Users.getIndex(peer));
            removedKnownPeers.add(removedKnownPeer);
            response.put("removedKnownPeers", removedKnownPeers);
            JSONArray addedActivePeers = new JSONArray();
            JSONObject addedActivePeer = new JSONObject();
            addedActivePeer.put("index", Users.getIndex(peer));
            if (peer.getState() != Peer.State.CONNECTED) {
              addedActivePeer.put("disconnected", true);
            }
            addedActivePeer.put("address", peer.getHost());
            addedActivePeer.put(
                "announcedAddress", Convert.truncate(peer.getAnnouncedAddress(), "-", 25, true));
            addedActivePeer.put("weight", peer.getWeight());
            addedActivePeer.put("downloaded", peer.getDownloadedVolume());
            addedActivePeer.put("uploaded", peer.getUploadedVolume());
            addedActivePeer.put("software", peer.getSoftware());
            addedActivePeers.add(addedActivePeer);
            response.put("addedActivePeers", addedActivePeers);
            Users.sendNewDataToAll(response);
          },
          Peers.Event.ADDED_ACTIVE_PEER);

      Peers.addListener(
          peer -> {
            JSONObject response = new JSONObject();
            JSONArray changedActivePeers = new JSONArray();
            JSONObject changedActivePeer = new JSONObject();
            changedActivePeer.put("index", Users.getIndex(peer));
            changedActivePeer.put(
                peer.getState() == Peer.State.CONNECTED ? "connected" : "disconnected", true);
            changedActivePeer.put(
                "announcedAddress", Convert.truncate(peer.getAnnouncedAddress(), "-", 25, true));
            changedActivePeers.add(changedActivePeer);
            response.put("changedActivePeers", changedActivePeers);
            Users.sendNewDataToAll(response);
          },
          Peers.Event.CHANGED_ACTIVE_PEER);

      Peers.addListener(
          peer -> {
            JSONObject response = new JSONObject();
            JSONArray addedKnownPeers = new JSONArray();
            JSONObject addedKnownPeer = new JSONObject();
            addedKnownPeer.put("index", Users.getIndex(peer));
            addedKnownPeer.put("address", peer.getHost());
            addedKnownPeer.put(
                "announcedAddress", Convert.truncate(peer.getAnnouncedAddress(), "-", 25, true));
            addedKnownPeer.put("software", peer.getSoftware());
            addedKnownPeers.add(addedKnownPeer);
            response.put("addedKnownPeers", addedKnownPeers);
            Users.sendNewDataToAll(response);
          },
          Peers.Event.NEW_PEER);

      Nxt.getTransactionProcessor()
          .addListener(
              transactions -> {
                JSONObject response = new JSONObject();
                JSONArray removedUnconfirmedTransactions = new JSONArray();
                for (Transaction transaction : transactions) {
                  JSONObject removedUnconfirmedTransaction = new JSONObject();
                  removedUnconfirmedTransaction.put("index", Users.getIndex(transaction));
                  removedUnconfirmedTransactions.add(removedUnconfirmedTransaction);
                }
                response.put("removedUnconfirmedTransactions", removedUnconfirmedTransactions);
                Users.sendNewDataToAll(response);
              },
              TransactionProcessor.Event.REMOVED_UNCONFIRMED_TRANSACTIONS);

      Nxt.getTransactionProcessor()
          .addListener(
              transactions -> {
                JSONObject response = new JSONObject();
                JSONArray addedUnconfirmedTransactions = new JSONArray();
                for (Transaction transaction : transactions) {
                  JSONObject addedUnconfirmedTransaction = new JSONObject();
                  addedUnconfirmedTransaction.put("index", Users.getIndex(transaction));
                  addedUnconfirmedTransaction.put("timestamp", transaction.getTimestamp());
                  addedUnconfirmedTransaction.put("deadline", transaction.getDeadline());
                  addedUnconfirmedTransaction.put(
                      "recipient", Long.toUnsignedString(transaction.getRecipientId()));
                  addedUnconfirmedTransaction.put("amountNQT", transaction.getAmountNQT());
                  addedUnconfirmedTransaction.put("feeNQT", transaction.getFeeNQT());
                  addedUnconfirmedTransaction.put(
                      "sender", Long.toUnsignedString(transaction.getSenderId()));
                  addedUnconfirmedTransaction.put("id", transaction.getStringId());
                  addedUnconfirmedTransactions.add(addedUnconfirmedTransaction);
                }
                response.put("addedUnconfirmedTransactions", addedUnconfirmedTransactions);
                Users.sendNewDataToAll(response);
              },
              TransactionProcessor.Event.ADDED_UNCONFIRMED_TRANSACTIONS);

      Nxt.getTransactionProcessor()
          .addListener(
              transactions -> {
                JSONObject response = new JSONObject();
                JSONArray addedConfirmedTransactions = new JSONArray();
                for (Transaction transaction : transactions) {
                  JSONObject addedConfirmedTransaction = new JSONObject();
                  addedConfirmedTransaction.put("index", Users.getIndex(transaction));
                  addedConfirmedTransaction.put("blockTimestamp", transaction.getBlockTimestamp());
                  addedConfirmedTransaction.put("transactionTimestamp", transaction.getTimestamp());
                  addedConfirmedTransaction.put(
                      "sender", Long.toUnsignedString(transaction.getSenderId()));
                  addedConfirmedTransaction.put(
                      "recipient", Long.toUnsignedString(transaction.getRecipientId()));
                  addedConfirmedTransaction.put("amountNQT", transaction.getAmountNQT());
                  addedConfirmedTransaction.put("feeNQT", transaction.getFeeNQT());
                  addedConfirmedTransaction.put("id", transaction.getStringId());
                  addedConfirmedTransactions.add(addedConfirmedTransaction);
                }
                response.put("addedConfirmedTransactions", addedConfirmedTransactions);
                Users.sendNewDataToAll(response);
              },
              TransactionProcessor.Event.ADDED_CONFIRMED_TRANSACTIONS);

      Nxt.getBlockchainProcessor()
          .addListener(
              block -> {
                JSONObject response = new JSONObject();
                JSONArray addedOrphanedBlocks = new JSONArray();
                JSONObject addedOrphanedBlock = new JSONObject();
                addedOrphanedBlock.put("index", Users.getIndex(block));
                addedOrphanedBlock.put("timestamp", block.getTimestamp());
                addedOrphanedBlock.put("numberOfTransactions", block.getTransactions().size());
                addedOrphanedBlock.put("totalAmountNQT", block.getTotalAmountNQT());
                addedOrphanedBlock.put("totalFeeNQT", block.getTotalFeeNQT());
                addedOrphanedBlock.put("payloadLength", block.getPayloadLength());
                addedOrphanedBlock.put("generator", Long.toUnsignedString(block.getGeneratorId()));
                addedOrphanedBlock.put("height", block.getHeight());
                addedOrphanedBlock.put("version", block.getVersion());
                addedOrphanedBlock.put("block", block.getStringId());
                addedOrphanedBlock.put(
                    "baseTarget",
                    BigInteger.valueOf(block.getBaseTarget())
                        .multiply(BigInteger.valueOf(100000))
                        .divide(BigInteger.valueOf(Constants.INITIAL_BASE_TARGET)));
                addedOrphanedBlocks.add(addedOrphanedBlock);
                response.put("addedOrphanedBlocks", addedOrphanedBlocks);
                Users.sendNewDataToAll(response);
              },
              BlockchainProcessor.Event.BLOCK_POPPED);

      Nxt.getBlockchainProcessor()
          .addListener(
              block -> {
                JSONObject response = new JSONObject();
                JSONArray addedRecentBlocks = new JSONArray();
                JSONObject addedRecentBlock = new JSONObject();
                addedRecentBlock.put("index", Users.getIndex(block));
                addedRecentBlock.put("timestamp", block.getTimestamp());
                addedRecentBlock.put("numberOfTransactions", block.getTransactions().size());
                addedRecentBlock.put("totalAmountNQT", block.getTotalAmountNQT());
                addedRecentBlock.put("totalFeeNQT", block.getTotalFeeNQT());
                addedRecentBlock.put("payloadLength", block.getPayloadLength());
                addedRecentBlock.put("generator", Long.toUnsignedString(block.getGeneratorId()));
                addedRecentBlock.put("height", block.getHeight());
                addedRecentBlock.put("version", block.getVersion());
                addedRecentBlock.put("block", block.getStringId());
                addedRecentBlock.put(
                    "baseTarget",
                    BigInteger.valueOf(block.getBaseTarget())
                        .multiply(BigInteger.valueOf(100000))
                        .divide(BigInteger.valueOf(Constants.INITIAL_BASE_TARGET)));
                addedRecentBlocks.add(addedRecentBlock);
                response.put("addedRecentBlocks", addedRecentBlocks);
                Users.sendNewDataToAll(response);
              },
              BlockchainProcessor.Event.BLOCK_PUSHED);

      Generator.addListener(
          generator -> {
            JSONObject response = new JSONObject();
            response.put("response", "setBlockGenerationDeadline");
            response.put("deadline", generator.getDeadline());
            users
                .values()
                .forEach(
                    user -> {
                      if (Arrays.equals(generator.getPublicKey(), user.getPublicKey())) {
                        user.send(response);
                      }
                    });
          },
          Generator.Event.GENERATION_DEADLINE);
    }
  }
  @Override
  JSONStreamAware processRequest(HttpServletRequest req) {

    String accountIdString = Convert.emptyToNull(req.getParameter("account"));
    Long accountId = null;

    if (accountIdString != null) {
      try {
        accountId = Convert.parseUnsignedLong(accountIdString);
      } catch (RuntimeException e) {
        return INCORRECT_ACCOUNT;
      }
    }

    JSONArray transactionIds = new JSONArray();
    for (Transaction transaction : Nxt.getTransactionProcessor().getAllUnconfirmedTransactions()) {
      if (accountId != null
          && !(accountId.equals(transaction.getSenderId())
              || accountId.equals(transaction.getRecipientId()))) {
        continue;
      }
      transactionIds.add(transaction.getStringId());
    }

    JSONObject response = new JSONObject();
    response.put("unconfirmedTransactionIds", transactionIds);
    return response;
  }
  @Override
  JSONStreamAware processRequest(HttpServletRequest req) throws NxtException.ValidationException {

    String transactionBytes = Convert.emptyToNull(req.getParameter("unsignedTransactionBytes"));
    String transactionJSON = Convert.emptyToNull(req.getParameter("unsignedTransactionJSON"));
    if (transactionBytes == null && transactionJSON == null) {
      return MISSING_UNSIGNED_BYTES;
    }
    String secretPhrase = Convert.emptyToNull(req.getParameter("secretPhrase"));
    if (secretPhrase == null) {
      return MISSING_SECRET_PHRASE;
    }

    try {
      Transaction transaction;
      if (transactionBytes != null) {
        byte[] bytes = Convert.parseHexString(transactionBytes);
        transaction = Nxt.getTransactionProcessor().parseTransaction(bytes);
      } else {
        JSONObject json = (JSONObject) JSONValue.parse(transactionJSON);
        transaction = Nxt.getTransactionProcessor().parseTransaction(json);
      }
      transaction.validate();
      if (transaction.getSignature() != null) {
        JSONObject response = new JSONObject();
        response.put("errorCode", 4);
        response.put(
            "errorDescription",
            "Incorrect \"unsignedTransactionBytes\" - transaction is already signed");
        return response;
      }
      transaction.sign(secretPhrase);
      JSONObject response = new JSONObject();
      response.put("transaction", transaction.getStringId());
      response.put("fullHash", transaction.getFullHash());
      response.put("transactionBytes", Convert.toHexString(transaction.getBytes()));
      response.put(
          "signatureHash", Convert.toHexString(Crypto.sha256().digest(transaction.getSignature())));
      response.put("verify", transaction.verifySignature());
      return response;
    } catch (NxtException.ValidationException | RuntimeException e) {
      // Logger.logDebugMessage(e.getMessage(), e);
      return INCORRECT_UNSIGNED_BYTES;
    }
  }
 @Override
 JSONStreamAware processRequest(HttpServletRequest req) {
   JSONObject response = new JSONObject();
   try {
     Nxt.getTransactionProcessor().clearUnconfirmedTransactions();
     response.put("done", true);
   } catch (RuntimeException e) {
     JSONData.putException(response, e);
   }
   return response;
 }
 @Override
 JSONStreamAware processRequest(HttpServletRequest req) {
   JSONObject response = new JSONObject();
   JSONArray jsonArray = new JSONArray();
   response.put("transactions", jsonArray);
   Transaction[] transactions = Nxt.getTransactionProcessor().getAllWaitingTransactions();
   for (Transaction transaction : transactions) {
     jsonArray.add(JSONData.unconfirmedTransaction(transaction));
   }
   return response;
 }
  @Override
  JSONStreamAware processRequest(JSONObject request, Peer peer) {

    JSONObject response = new JSONObject();

    JSONArray transactionsData = new JSONArray();
    try (DbIterator<? extends Transaction> transactions =
        Nxt.getTransactionProcessor().getAllUnconfirmedTransactions()) {
      while (transactions.hasNext()) {
        Transaction transaction = transactions.next();
        transactionsData.add(transaction.getJSONObject());
      }
    }
    response.put("unconfirmedTransactions", transactionsData);

    return response;
  }
  public static Transaction create(
      IAccount sender,
      Long order,
      short deadline,
      int fee,
      Long referencedTransaction,
      NXTService nxt)
      throws TransactionException, ValidationException {

    String secretPhrase = sender.getPrivateKey();
    byte[] publicKey = Crypto.getPublicKey(secretPhrase);

    if ((fee <= 0) || (fee >= 1000000000L))
      throw new TransactionException(TransactionException.INCORRECT_FEE);

    if ((deadline < 1) || (deadline > 1440))
      throw new TransactionException(TransactionException.INCORRECT_DEADLINE);

    Account account = Account.getAccount(publicKey);
    if (account == null) throw new TransactionException(TransactionException.INTERNAL_ERROR);

    Order.Ask orderData = Order.Ask.getAskOrder(order);
    if (orderData == null || !orderData.getAccount().getId().equals(account.getId()))
      throw new TransactionException(TransactionException.UNKNOWN_ORDER);

    Attachment attachment = new Attachment.ColoredCoinsAskOrderCancellation(order);

    Transaction transaction =
        Nxt.getTransactionProcessor()
            .newTransaction(
                deadline, publicKey, Genesis.CREATOR_ID, 0, fee, referencedTransaction, attachment);
    transaction.sign(secretPhrase);

    nxt.broacastTransaction(transaction);

    return transaction;
  }