use of org.aion.p2p.INode in project aion by aionnetwork.
the class SyncMgr method requestStatus.
/**
* Makes a status request to each active peer.
*/
private void requestStatus() {
Thread.currentThread().setName("sync-gs");
for (INode node : p2pMgr.getActiveNodes().values()) {
p2pMgr.send(node.getIdHash(), node.getIdShort(), cachedReqStatus);
stats.updateTotalRequestsToPeer(node.getIdShort(), RequestType.STATUS);
stats.updateRequestTime(node.getIdShort(), System.nanoTime(), RequestType.STATUS);
}
}
use of org.aion.p2p.INode in project aion by aionnetwork.
the class SyncHeaderRequestManagerTest method generateMockPeers.
/**
* Generates {@code count} mock peers with ids from {@code start} to {@code start + count - 1}
* and best block number {@code id * 1_000L}.
*/
private Map<Integer, INode> generateMockPeers(int start, int count) {
Map<Integer, INode> current = new HashMap<>();
for (int i = start; i < count + start; i++) {
INode mockPeer = mock(INode.class);
when(mockPeer.getIdHash()).thenReturn(i);
when(mockPeer.getIdShort()).thenReturn("peer" + i);
when(mockPeer.getBestBlockNumber()).thenReturn(i * 1_000L);
current.put(i, mockPeer);
}
return current;
}
use of org.aion.p2p.INode in project aion by aionnetwork.
the class ResActiveNodes method encode.
@Override
public byte[] encode() {
ByteBuffer buf = ByteBuffer.allocate(NODE_BYTES_LENGTH * this.count + 1);
buf.put((byte) this.count);
for (INode n : this.nodes) {
buf.put(n.getId());
buf.put(n.getIp());
buf.putInt(n.getPort());
}
return buf.array();
}
use of org.aion.p2p.INode in project aion by aionnetwork.
the class P2pMgr method handleP2pMessage.
private void handleP2pMessage(final SelectionKey sk, byte act, final byte[] msgBytes) {
ChannelBuffer rb = (ChannelBuffer) sk.attachment();
switch(act) {
case Act.REQ_HANDSHAKE:
if (msgBytes.length > ReqHandshake.LEN) {
ReqHandshake1 reqHandshake1 = ReqHandshake1.decode(msgBytes, p2pLOG);
if (reqHandshake1 != null) {
handleHandshakeRequest(rb, sk.channel().hashCode(), reqHandshake1.getNodeId(), reqHandshake1.getNetId(), reqHandshake1.getPort(), reqHandshake1.getRevision());
}
}
break;
case Act.RES_HANDSHAKE:
if (rb.getNodeIdHash() != 0) {
if (msgBytes.length > ResHandshake.LEN) {
ResHandshake1 resHandshake1 = ResHandshake1.decode(msgBytes, p2pLOG);
if (resHandshake1 != null && resHandshake1.getSuccess()) {
handleHandshakeResponse(rb.getNodeIdHash(), resHandshake1.getBinaryVersion());
}
}
}
break;
case Act.REQ_ACTIVE_NODES:
if (rb.getNodeIdHash() != 0) {
INode node = nodeMgr.getActiveNode(rb.getNodeIdHash());
if (node != null) {
ResActiveNodes resActiveNodes = new ResActiveNodes(p2pLOG, nodeMgr.getActiveNodesList());
send(node.getIdHash(), node.getIdShort(), resActiveNodes);
}
}
break;
case Act.RES_ACTIVE_NODES:
if (isSyncSeedsOnly() || rb.getNodeIdHash() == 0) {
break;
}
INode node = nodeMgr.getActiveNode(rb.getNodeIdHash());
if (node != null) {
node.refreshTimestamp();
ResActiveNodes resActiveNodes = ResActiveNodes.decode(msgBytes, p2pLOG);
if (resActiveNodes != null) {
List<INode> incomingNodes = resActiveNodes.getNodes();
for (INode incomingNode : incomingNodes) {
if (nodeMgr.tempNodesSize() >= getMaxTempNodes()) {
return;
}
if (validateNode(incomingNode)) {
nodeMgr.addTempNode(incomingNode);
}
}
}
}
break;
default:
p2pLOG.debug("unknown-route act={}", act);
break;
}
}
use of org.aion.p2p.INode in project aion by aionnetwork.
the class P2pMgr method handleHandshakeResponse.
private void handleHandshakeResponse(int nodeIdHash, String binaryVersion) {
INode node = nodeMgr.getOutboundNode(nodeIdHash);
if (node != null && node.getPeerMetric().notBan()) {
node.refreshTimestamp();
node.setBinaryVersion(binaryVersion);
nodeMgr.movePeerToActive(node.getIdHash(), "outbound");
}
}
Aggregations