예제 #1
1
 public <T> Job createJob(
     Path basePath,
     Path outputPath,
     long beginNS,
     long endNS,
     int numReducers,
     T fields,
     Configuration conf,
     FileSystem fs,
     PcapFilterConfigurator<T> filterImpl)
     throws IOException {
   conf.set(START_TS_CONF, Long.toUnsignedString(beginNS));
   conf.set(END_TS_CONF, Long.toUnsignedString(endNS));
   conf.set(WIDTH_CONF, "" + findWidth(beginNS, endNS, numReducers));
   filterImpl.addToConfig(fields, conf);
   Job job = new Job(conf);
   job.setJarByClass(PcapJob.class);
   job.setMapperClass(PcapJob.PcapMapper.class);
   job.setMapOutputKeyClass(LongWritable.class);
   job.setMapOutputValueClass(BytesWritable.class);
   job.setNumReduceTasks(numReducers);
   job.setReducerClass(PcapReducer.class);
   job.setPartitionerClass(PcapPartitioner.class);
   job.setOutputKeyClass(LongWritable.class);
   job.setOutputValueClass(BytesWritable.class);
   SequenceFileInputFormat.addInputPaths(
       job, Joiner.on(',').join(getPaths(fs, basePath, beginNS, endNS)));
   job.setInputFormatClass(SequenceFileInputFormat.class);
   job.setOutputFormatClass(SequenceFileOutputFormat.class);
   SequenceFileOutputFormat.setOutputPath(job, outputPath);
   return job;
 }
예제 #2
1
 @Override
 void validateAttachment(Transaction transaction) throws NxtException.ValidationException {
   Attachment.MonetarySystemCurrencyDeletion attachment =
       (Attachment.MonetarySystemCurrencyDeletion) transaction.getAttachment();
   Currency currency = Currency.getCurrency(attachment.getCurrencyId());
   CurrencyType.validate(currency, transaction);
   if (!currency.canBeDeletedBy(transaction.getSenderId())) {
     throw new NxtException.NotCurrentlyValidException(
         "Currency "
             + Long.toUnsignedString(currency.getId())
             + " cannot be deleted by account "
             + Long.toUnsignedString(transaction.getSenderId()));
   }
 }
예제 #3
1
  @Test
  public void accountPollsIncrease() {
    APICall apiCall =
        new APICall.Builder("getPolls")
            .param("includeVoters", "false")
            .param("account", Long.toUnsignedString(DAVE.getId()))
            .param("firstIndex", 0)
            .param("lastIndex", 100)
            .build();

    JSONObject jsonResponse = apiCall.invoke();
    Logger.logMessage("getPollsResponse:" + jsonResponse.toJSONString());
    JSONArray polls = (JSONArray) jsonResponse.get("polls");
    int initialSize = polls.size();

    APICall createPollApiCall =
        new TestCreatePoll.CreatePollBuilder().secretPhrase(DAVE.getSecretPhrase()).build();
    String poll = TestCreatePoll.issueCreatePoll(createPollApiCall, false);
    generateBlock();

    jsonResponse = apiCall.invoke();
    Logger.logMessage("getPollsResponse:" + jsonResponse.toJSONString());
    polls = (JSONArray) jsonResponse.get("polls");
    int size = polls.size();

    JSONObject lastPoll = (JSONObject) polls.get(0);
    Assert.assertEquals(poll, lastPoll.get("poll"));
    Assert.assertEquals(size, initialSize + 1);
  }
예제 #4
1
 @Override
 JSONStreamAware processRequest(HttpServletRequest req) {
   JSONObject response = new JSONObject();
   long longId = Convert.fullHashToId(Convert.parseHexString(req.getParameter("fullHash")));
   response.put("longId", String.valueOf(longId));
   response.put("stringId", Long.toUnsignedString(longId));
   return response;
 }
예제 #5
1
파일: Users.java 프로젝트: andriybobyr/lnk
  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);
    }
  }