use of org.xel.peer.Peer in project elastic-core-maven by OrdinaryDude.
the class GetPeer method processRequest.
@Override
protected JSONStreamAware processRequest(HttpServletRequest req) {
String peerAddress = req.getParameter("peer");
if (peerAddress == null) {
return MISSING_PEER;
}
Peer peer = Peers.findOrCreatePeer(peerAddress, false);
if (peer == null) {
return UNKNOWN_PEER;
}
return JSONData.peer(peer);
}
use of org.xel.peer.Peer in project elastic-core-maven by OrdinaryDude.
the class GetInitialData method processRequest.
@Override
JSONStreamAware processRequest(HttpServletRequest req, User user) throws IOException {
JSONArray unconfirmedTransactions = new JSONArray();
JSONArray activePeers = new JSONArray(), knownPeers = new JSONArray(), blacklistedPeers = new JSONArray();
JSONArray recentBlocks = new JSONArray();
try (DbIterator<? extends Transaction> transactions = Nxt.getTransactionProcessor().getAllUnconfirmedTransactions()) {
while (transactions.hasNext()) {
Transaction transaction = transactions.next();
JSONObject unconfirmedTransaction = new JSONObject();
unconfirmedTransaction.put("index", Users.getIndex(transaction));
unconfirmedTransaction.put("timestamp", transaction.getTimestamp());
unconfirmedTransaction.put("deadline", transaction.getDeadline());
unconfirmedTransaction.put("recipient", Long.toUnsignedString(transaction.getRecipientId()));
unconfirmedTransaction.put("amountNQT", transaction.getAmountNQT());
unconfirmedTransaction.put("feeNQT", transaction.getFeeNQT());
unconfirmedTransaction.put("sender", Long.toUnsignedString(transaction.getSenderId()));
unconfirmedTransaction.put("id", transaction.getStringId());
unconfirmedTransactions.add(unconfirmedTransaction);
}
}
for (Peer peer : Peers.getAllPeers()) {
if (peer.isBlacklisted()) {
JSONObject blacklistedPeer = new JSONObject();
blacklistedPeer.put("index", Users.getIndex(peer));
blacklistedPeer.put("address", peer.getHost());
blacklistedPeer.put("announcedAddress", Convert.truncate(peer.getAnnouncedAddress(), "-", 25, true));
blacklistedPeer.put("software", peer.getSoftware());
blacklistedPeers.add(blacklistedPeer);
} else if (peer.getState() == Peer.State.NON_CONNECTED) {
JSONObject knownPeer = new JSONObject();
knownPeer.put("index", Users.getIndex(peer));
knownPeer.put("address", peer.getHost());
knownPeer.put("announcedAddress", Convert.truncate(peer.getAnnouncedAddress(), "-", 25, true));
knownPeer.put("software", peer.getSoftware());
knownPeers.add(knownPeer);
} else {
JSONObject activePeer = new JSONObject();
activePeer.put("index", Users.getIndex(peer));
if (peer.getState() == Peer.State.DISCONNECTED) {
activePeer.put("disconnected", true);
}
activePeer.put("address", peer.getHost());
activePeer.put("announcedAddress", Convert.truncate(peer.getAnnouncedAddress(), "-", 25, true));
activePeer.put("weight", peer.getWeight());
activePeer.put("downloaded", peer.getDownloadedVolume());
activePeer.put("uploaded", peer.getUploadedVolume());
activePeer.put("software", peer.getSoftware());
activePeers.add(activePeer);
}
}
try (DbIterator<? extends Block> lastBlocks = Nxt.getBlockchain().getBlocks(0, 59)) {
for (Block block : lastBlocks) {
JSONObject recentBlock = new JSONObject();
recentBlock.put("index", Users.getIndex(block));
recentBlock.put("timestamp", block.getTimestamp());
recentBlock.put("numberOfTransactions", block.getTransactions().size());
recentBlock.put("totalAmountNQT", block.getTotalAmountNQT());
recentBlock.put("totalFeeNQT", block.getTotalFeeNQT());
recentBlock.put("payloadLength", block.getPayloadLength());
recentBlock.put("generator", Long.toUnsignedString(block.getGeneratorId()));
recentBlock.put("height", block.getHeight());
recentBlock.put("version", block.getVersion());
recentBlock.put("block", block.getStringId());
recentBlock.put("baseTarget", BigInteger.valueOf(block.getBaseTarget()).multiply(BigInteger.valueOf(100000)).divide(BigInteger.valueOf(Constants.INITIAL_BASE_TARGET)));
recentBlocks.add(recentBlock);
}
}
JSONObject response = new JSONObject();
response.put("response", "processInitialData");
response.put("version", Nxt.VERSION);
if (unconfirmedTransactions.size() > 0) {
response.put("unconfirmedTransactions", unconfirmedTransactions);
}
if (activePeers.size() > 0) {
response.put("activePeers", activePeers);
}
if (knownPeers.size() > 0) {
response.put("knownPeers", knownPeers);
}
if (blacklistedPeers.size() > 0) {
response.put("blacklistedPeers", blacklistedPeers);
}
if (recentBlocks.size() > 0) {
response.put("recentBlocks", recentBlocks);
}
return response;
}
use of org.xel.peer.Peer in project elastic-core-maven by OrdinaryDude.
the class RemoveKnownPeer method processRequest.
@Override
JSONStreamAware processRequest(HttpServletRequest req, User user) throws IOException {
if (Users.allowedUserHosts == null && !InetAddress.getByName(req.getRemoteAddr()).isLoopbackAddress()) {
return LOCAL_USERS_ONLY;
} else {
int index = Integer.parseInt(req.getParameter("peer"));
Peer peer = Users.getPeer(index);
if (peer != null) {
peer.remove();
}
}
return null;
}
use of org.xel.peer.Peer in project elastic-core-maven by OrdinaryDude.
the class JSONData method peer.
static JSONObject peer(Peer peer) {
JSONObject json = new JSONObject();
json.put("address", peer.getHost());
json.put("port", peer.getPort());
json.put("state", peer.getState().ordinal());
json.put("announcedAddress", peer.getAnnouncedAddress());
json.put("shareAddress", peer.shareAddress());
if (peer.getHallmark() != null) {
json.put("hallmark", peer.getHallmark().getHallmarkString());
}
json.put("weight", peer.getWeight());
json.put("downloadedVolume", peer.getDownloadedVolume());
json.put("uploadedVolume", peer.getUploadedVolume());
json.put("application", peer.getApplication());
json.put("version", peer.getVersion());
json.put("platform", peer.getPlatform());
if (peer.getApiPort() != 0) {
json.put("apiPort", peer.getApiPort());
}
if (peer.getApiSSLPort() != 0) {
json.put("apiSSLPort", peer.getApiSSLPort());
}
json.put("blacklisted", peer.isBlacklisted());
json.put("lastUpdated", peer.getLastUpdated());
json.put("lastConnectAttempt", peer.getLastConnectAttempt());
json.put("inbound", peer.isInbound());
json.put("inboundWebSocket", peer.isInboundWebSocket());
json.put("outboundWebSocket", peer.isOutboundWebSocket());
if (peer.isBlacklisted()) {
json.put("blacklistingCause", peer.getBlacklistingCause());
}
JSONArray servicesArray = new JSONArray();
for (Peer.Service service : Peer.Service.values()) {
if (peer.providesService(service)) {
servicesArray.add(service.name());
}
}
json.put("services", servicesArray);
json.put("blockchainState", peer.getBlockchainState());
return json;
}
use of org.xel.peer.Peer in project elastic-core-maven by OrdinaryDude.
the class BlockchainProcessorImpl method restorePrunedTransaction.
@Override
public Transaction restorePrunedTransaction(long transactionId) {
TransactionImpl transaction = TransactionDb.findTransaction(transactionId);
if (transaction == null) {
throw new IllegalArgumentException("Transaction not found");
}
boolean isPruned = false;
for (Appendix.AbstractAppendix appendage : transaction.getAppendages(true)) {
if ((appendage instanceof Appendix.Prunable) && !((Appendix.Prunable) appendage).hasPrunableData()) {
isPruned = true;
break;
}
}
if (!isPruned) {
return transaction;
}
List<Peer> peers = Peers.getPeers(chkPeer -> chkPeer.providesService(Peer.Service.PRUNABLE) && !chkPeer.isBlacklisted() && chkPeer.getAnnouncedAddress() != null);
if (peers.isEmpty()) {
Logger.logDebugMessage("Cannot find any archive peers");
return null;
}
JSONObject json = new JSONObject();
JSONArray requestList = new JSONArray();
requestList.add(Long.toUnsignedString(transactionId));
json.put("requestType", "getTransactions");
json.put("transactionIds", requestList);
JSONStreamAware request = JSON.prepareRequest(json);
for (Peer peer : peers) {
if (peer.getState() != Peer.State.CONNECTED) {
Peers.connectPeer(peer);
}
if (peer.getState() != Peer.State.CONNECTED) {
continue;
}
Logger.logDebugMessage("Connected to archive peer " + peer.getHost());
JSONObject response = peer.send(request);
if (response == null) {
continue;
}
JSONArray transactions = (JSONArray) response.get("transactions");
if (transactions == null || transactions.isEmpty()) {
continue;
}
try {
List<Transaction> processed = Nxt.getTransactionProcessor().restorePrunableData(transactions);
if (processed.isEmpty()) {
continue;
}
synchronized (prunableTransactions) {
prunableTransactions.remove(transactionId);
}
return processed.get(0);
} catch (NxtException.NotValidException e) {
Logger.logErrorMessage("Peer " + peer.getHost() + " returned invalid prunable transaction", e);
peer.blacklist(e);
}
}
return null;
}
Aggregations