Search in sources :

Example 1 with ReqBlocksHeaders

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);
            }
        }
    }
}
Also used : BigInteger(java.math.BigInteger) IP2pMgr(org.aion.p2p.IP2pMgr) java.util(java.util) Logger(org.slf4j.Logger) INode(org.aion.p2p.INode) ReqBlocksHeaders(org.aion.zero.impl.sync.msg.ReqBlocksHeaders) BigInteger(java.math.BigInteger) Collectors(java.util.stream.Collectors) INode(org.aion.p2p.INode) ReqBlocksHeaders(org.aion.zero.impl.sync.msg.ReqBlocksHeaders)

Example 2 with ReqBlocksHeaders

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);
}
Also used : BlockIdentifier(org.aion.mcf.types.BlockIdentifier) A0BlockHeader(org.aion.zero.types.A0BlockHeader) ResBlocksHeaders(org.aion.zero.impl.sync.msg.ResBlocksHeaders) ReqBlocksHeaders(org.aion.zero.impl.sync.msg.ReqBlocksHeaders)

Aggregations

ReqBlocksHeaders (org.aion.zero.impl.sync.msg.ReqBlocksHeaders)2 BigInteger (java.math.BigInteger)1 java.util (java.util)1 Collectors (java.util.stream.Collectors)1 BlockIdentifier (org.aion.mcf.types.BlockIdentifier)1 INode (org.aion.p2p.INode)1 IP2pMgr (org.aion.p2p.IP2pMgr)1 ResBlocksHeaders (org.aion.zero.impl.sync.msg.ResBlocksHeaders)1 A0BlockHeader (org.aion.zero.types.A0BlockHeader)1 Logger (org.slf4j.Logger)1