use of org.aion.zero.impl.sync.msg.ReqBlocksHeaders in project aion by aionnetwork.
the class TaskGetHeaders method run.
@Override
public void run() {
Set<Integer> ids = new HashSet<>();
Collection<INode> preFilter = this.p2p.getActiveNodes().values();
List<INode> filtered = preFilter.stream().filter((n) -> n.getTotalDifficulty() != null && n.getTotalDifficulty().compareTo(this.selfTd) >= 0).collect(Collectors.toList());
if (filtered.size() > 0) {
Random r = new Random(System.currentTimeMillis());
for (int i = 0; i < 2; i++) {
INode node = filtered.get(r.nextInt(filtered.size()));
if (!ids.contains(node.getIdHash())) {
ids.add(node.getIdHash());
ReqBlocksHeaders rbh = new ReqBlocksHeaders(this.fromBlock, this.syncMax);
if (log.isDebugEnabled()) {
log.debug("<get-headers from-num={} size={} node={}>", fromBlock, syncMax, node.getIdShort());
}
this.p2p.send(node.getIdHash(), rbh);
}
}
}
}
use of org.aion.zero.impl.sync.msg.ReqBlocksHeaders in project aion by aionnetwork.
the class ReqBlocksHeadersHandler method receive.
@Override
public void receive(int _nodeIdHashcode, String _displayId, final byte[] _msgBytes) {
ReqBlocksHeaders reqHeaders = ReqBlocksHeaders.decode(_msgBytes);
if (reqHeaders != null) {
long fromBlock = reqHeaders.getFromBlock();
int take = reqHeaders.getTake();
this.log.debug("<req-headers from-number={} size={} node={}>", fromBlock, take, _displayId);
List<A0BlockHeader> headers = this.blockchain.getListOfHeadersStartFrom(new BlockIdentifier(null, fromBlock), 0, Math.min(take, max), false);
ResBlocksHeaders rbhs = new ResBlocksHeaders(headers);
this.p2pMgr.send(_nodeIdHashcode, rbhs);
} else
this.log.error("<req-headers decode-msg msg-bytes={} node={}>", _msgBytes == null ? 0 : _msgBytes.length, _nodeIdHashcode);
}
Aggregations