Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions Qora/src/api/PeersResource.java
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,9 @@ else if(DBSet.getInstance().getPeerMap().contains(peer.getAddress().getAddress()
o.put("lastGray", "never");
}
o.put("whitePingCounter", peerInfo.getWhitePingCouner());

if (peerInfo.isBlacklisted())
o.put("isBlacklisted", "true");
}

if(o.size() == 0){
Expand Down Expand Up @@ -276,4 +279,29 @@ public String clearPeers()

return "OK";
}

@POST
@Path("/blacklist")
public String blacklistPeer(String address) {
APIUtils.askAPICallAllowed("POST peers/blacklist " + address, request);

// CHECK WALLET UNLOCKED
if (Controller.getInstance().doesWalletExists() && !Controller.getInstance().isWalletUnlocked()) {
throw ApiErrorFactory.getInstance().createError(
ApiErrorFactory.ERROR_WALLET_LOCKED);
}

Peer peer;
try {
peer = new Peer(InetAddress.getByName(address));
} catch (UnknownHostException e) {
throw ApiErrorFactory.getInstance().createError(
ApiErrorFactory.ERROR_INVALID_NETWORK_ADDRESS);
}

PeerManager.getInstance().blacklistPeer(peer);

return "OK";
}

}
50 changes: 34 additions & 16 deletions Qora/src/controller/Controller.java
Original file line number Diff line number Diff line change
Expand Up @@ -91,11 +91,11 @@
public class Controller extends Observable {

private static final Logger LOGGER = LogManager.getLogger(Controller.class);
private String version = "0.26.7";
private String buildTime = "2018-03-09 08:56:00 UTC";
private String version = "0.26.9";
private String buildTime = "2018-04-18 16:58:00 UTC";
private long buildTimestamp;

public static final String releaseVersion = "0.26.7";
public static final String releaseVersion = "0.26.9";

// TODO ENUM would be better here
public static final int STATUS_NO_CONNECTIONS = 0;
Expand Down Expand Up @@ -158,16 +158,17 @@ public long getBuildTimestamp() {
if (this.buildTimestamp == 0) {
Date date = new Date();
URL resource = getClass().getResource(getClass().getSimpleName() + ".class");
if (resource != null) {
if (!resource.getProtocol().equals("file")) {
DateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss z");
try {
date = (Date) formatter.parse(this.buildTime);
} catch (ParseException e) {
LOGGER.error(e.getMessage(), e);
}

if (resource == null || !resource.getProtocol().equals("file")) {
// Use compiled buildTime
DateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss z");
try {
date = (Date) formatter.parse(this.buildTime);
} catch (ParseException e) {
LOGGER.error(e.getMessage(), e);
}
}

this.buildTimestamp = date.getTime();
}
return this.buildTimestamp;
Expand Down Expand Up @@ -681,7 +682,8 @@ public void onConnect(Peer peer) {
peer.sendMessage(MessageFactory.getInstance().createVersionMessage(Controller.getInstance().getVersion(), this.getBuildTimestamp()));
}

// SEND HEIGTH MESSAGE
// SEND HEIGHT MESSAGE
LOGGER.trace("Sending our height " + height + " to peer " + peer.getAddress());
peer.sendMessage(MessageFactory.getInstance().createHeightMessage(height));

if (this.status == STATUS_NO_CONNECTIONS) {
Expand Down Expand Up @@ -753,7 +755,7 @@ public void onError(Peer peer) {
this.onDisconnect(peer);
}

// SYNCHRONIZED DO NOT PROCESSS MESSAGES SIMULTANEOUSLY
// SYNCHRONIZED DO NOT PROCESS MESSAGES SIMULTANEOUSLY
public void onMessage(Message message) {
Message response;
Block block;
Expand All @@ -776,6 +778,7 @@ public void onMessage(Message message) {
case Message.HEIGHT_TYPE:

HeightMessage heightMessage = (HeightMessage) message;
LOGGER.trace("Received height " + heightMessage.getHeight() + " from peer " + heightMessage.getSender().getAddress());

// ADD TO LIST
synchronized (this.peerHeight) {
Expand All @@ -786,10 +789,15 @@ public void onMessage(Message message) {

case Message.GET_SIGNATURES_TYPE:

// Don't send if we're synchronizing
if (this.status == STATUS_SYNCHRONIZING)
break;

GetSignaturesMessage getHeadersMessage = (GetSignaturesMessage) message;

// ASK SIGNATURES FROM BLOCKCHAIN
List<byte[]> headers = this.blockChain.getSignatures(getHeadersMessage.getParent());
LOGGER.trace("Found " + headers.size() + " block signatures to send to " + message.getSender().getAddress());

// CREATE RESPONSE WITH SAME ID
response = MessageFactory.getInstance().createHeadersMessage(headers);
Expand All @@ -802,6 +810,10 @@ public void onMessage(Message message) {

case Message.GET_BLOCK_TYPE:

// Don't send if we're synchronizing
if (this.status == STATUS_SYNCHRONIZING)
break;

GetBlockMessage getBlockMessage = (GetBlockMessage) message;

// ASK BLOCK FROM BLOCKCHAIN
Expand Down Expand Up @@ -836,7 +848,7 @@ public void onMessage(Message message) {

// CHECK IF VALID
if (isNewBlockValid && this.synchronizer.process(block)) {
LOGGER.info(Lang.getInstance().translate("received new valid block"));
LOGGER.info(Lang.getInstance().translate("received new valid block") + " " + block.getHeight());

// PROCESS
// this.synchronizer.process(block);
Expand Down Expand Up @@ -972,11 +984,16 @@ public void update() {

if (peer != null) {
// SYNCHRONIZE FROM PEER
LOGGER.info("Synchronizing using peer " + peer.getAddress().getHostAddress() + " with height " + peerHeight.get(peer) + " - ping " + peer.getPing() + "ms");
this.synchronizer.synchronize(peer);
}

Thread.sleep(5 * 1000);
}
} catch (InterruptedException e) {
return;
} catch (Exception e) {
LOGGER.error(e.getMessage(), e);
LOGGER.debug(e.getMessage());

if (peer != null) {
// DISHONEST PEER
Expand Down Expand Up @@ -1005,7 +1022,8 @@ public void update() {

private Peer getMaxHeightPeer() {
Peer highestPeer = null;
int bestHeight = this.blockChain.getHeight();
// needs to be better than our height
int bestHeight = this.blockChain.getHeight() + 1;
long bestPing = Long.MAX_VALUE;

try {
Expand Down
Loading