Skip to content

Commit

Permalink
Adding node ids and counting them
Browse files Browse the repository at this point in the history
  • Loading branch information
fireduck64 committed May 23, 2018
1 parent ed66714 commit 55e27f6
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 5 deletions.
3 changes: 2 additions & 1 deletion proto/snowblossom.proto
Expand Up @@ -179,6 +179,7 @@ message PeerInfo {
int64 last_passed = 4;
int64 learned = 5;
string version = 6;
bytes node_id = 7;
}

message RequestBlock {
Expand Down Expand Up @@ -243,8 +244,8 @@ message NullRequest {
message NodeStatus {
int32 mem_pool_size = 1;
int32 connected_peers = 2;

BlockSummary head_summary = 3;
int32 estimated_nodes = 4;
}

// -------------------------------------------------------------------
Expand Down
2 changes: 2 additions & 0 deletions src/Globals.java
Expand Up @@ -38,6 +38,8 @@ public class Globals
public static final int MAX_BLOCK_SIZE = 3800000;
public static final int MAX_TX_SIZE = 1000000;

public static final int MAX_NODE_ID_SIZE = 8;


public static void addCryptoProvider()
{
Expand Down
5 changes: 3 additions & 2 deletions src/PeerUtil.java
Expand Up @@ -15,8 +15,8 @@ public static PeerInfo mergePeers(PeerInfo a, PeerInfo b)

if (a.getLearned() > b.getLearned())
{
n.mergeFrom(b);
n.mergeFrom(a);
n.mergeFrom(b);
n.mergeFrom(a);
}
else
{
Expand All @@ -35,4 +35,5 @@ public static String getString(PeerInfo a)
{
return a.getHost() + ":" + a.getPort();
}

}
40 changes: 38 additions & 2 deletions src/Peerage.java
Expand Up @@ -28,6 +28,7 @@
import java.util.List;
import java.util.LinkedList;
import java.net.URL;
import java.util.HashSet;
import java.util.Random;
import java.net.InetAddress;

Expand All @@ -52,6 +53,7 @@ public class Peerage
private Map<String, PeerInfo> peer_rumor_list;

private ImmutableSet<String> self_peer_names;
private ImmutableList<PeerInfo> self_peer_info;

public Peerage(SnowBlossomNode node)
{
Expand Down Expand Up @@ -121,14 +123,14 @@ private PeerChainTip getTip()
}
}

for(int i=0; i<8; i++)
tip.addAllPeers(self_peer_info);
for(int i=0; i<4; i++)
{
if (peer_map.size() > 0)
{
tip.addPeers(peer_map.pollFirstEntry().getValue());
}
}


return tip.build();
}
Expand All @@ -141,6 +143,20 @@ public int getConnectedPeerCount()
}
}

public int getEstimateUniqueNodes()
{
HashSet<ByteString> set = new HashSet<>();
synchronized(peer_rumor_list)
{
for(PeerInfo info : peer_rumor_list.values())
{
set.add(info.getNodeId());
}
}
return set.size();

}

private ImmutableList<PeerLink> getLinkList()
{
synchronized(links)
Expand Down Expand Up @@ -219,6 +235,7 @@ public void learnSelfAndSeed()
public void learnPeer(PeerInfo info)
{
if (info.getLearned() + PEER_EXPIRE_TIME < System.currentTimeMillis()) return;
if (info.getNodeId().size() > Globals.MAX_NODE_ID_SIZE) return;

synchronized(peer_rumor_list)
{
Expand Down Expand Up @@ -339,6 +356,21 @@ private void runPrune()
}
}

private ByteString getNodeId()
{
ByteString id = node.getDB().getSpecialMap().get("node_id");
if (id == null)
{
Random rnd = new Random();
byte[] b = new byte[Globals.MAX_NODE_ID_SIZE];
rnd.nextBytes(b);
id = ByteString.copyFrom(b);
node.getDB().getSpecialMap().put("node_id", id);
}

return id;
}

private List<PeerInfo> getSelfPeers()
{
List<String> advertise_hosts= new LinkedList<>();
Expand All @@ -362,6 +394,8 @@ private List<PeerInfo> getSelfPeers()
}
catch(Throwable t){}

ByteString node_id = getNodeId();

int port = node.getConfig().getInt("service_port");
for(String host : advertise_hosts)
{
Expand All @@ -370,6 +404,7 @@ private List<PeerInfo> getSelfPeers()
.setPort(port)
.setLearned(System.currentTimeMillis())
.setVersion(Globals.VERSION)
.setNodeId(node_id)
.build();

self_peers.add(pi);
Expand All @@ -380,6 +415,7 @@ private List<PeerInfo> getSelfPeers()

}
self_peer_names = ImmutableSet.copyOf(self_names);
self_peer_info = ImmutableList.copyOf(self_peers);

return self_peers;

Expand Down
1 change: 1 addition & 0 deletions src/SnowUserService.java
Expand Up @@ -222,6 +222,7 @@ public void getNodeStatus(NullRequest null_request, StreamObserver<NodeStatus> r
.setMemPoolSize(node.getMemPool().getMemPoolSize())
.setConnectedPeers(node.getPeerage().getConnectedPeerCount())
.setHeadSummary(node.getBlockIngestor().getHead())
.setEstimatedNodes(node.getPeerage().getEstimateUniqueNodes())
.build();

responseObserver.onNext(ns);
Expand Down
1 change: 1 addition & 0 deletions src/shackleton/WebServer.java
Expand Up @@ -137,6 +137,7 @@ private void displayStatus(PrintStream out)
out.println("<pre>");
out.println("mem_pool_size: " + node_status.getMemPoolSize());
out.println("connected_peers: " + node_status.getConnectedPeers());
out.println("estimated_nodes: " + node_status.getEstimatedNodes());
out.println("</pre>");

BlockSummary summary = node_status.getHeadSummary();
Expand Down

0 comments on commit 55e27f6

Please sign in to comment.