Search in sources :

Example 1 with ResBlocksBodies

use of org.aion.zero.impl.sync.msg.ResBlocksBodies in project aion by aionnetwork.

the class ReqBlocksBodiesHandler method receive.

@Override
public void receive(int _nodeIdHashcode, String _displayId, final byte[] _msgBytes) {
    if (isSyncOnlyNode)
        return;
    ReqBlocksBodies reqBlocks = ReqBlocksBodies.decode(_msgBytes);
    if (reqBlocks != null) {
        // limit number of blocks
        List<byte[]> hashes = reqBlocks.getBlocksHashes();
        hashes = hashes.size() > MAX_NUM_OF_BLOCKS ? hashes.subList(0, MAX_NUM_OF_BLOCKS) : hashes;
        // results
        List<byte[]> blockBodies = new ArrayList<>();
        // read from cache, then block store
        int out = 0;
        for (byte[] hash : hashes) {
            // ref for add.
            byte[] blockBytesForadd;
            byte[] blockBytes = cache.get(ByteArrayWrapper.wrap(hash));
            // if cached , add.
            if (blockBytes != null) {
                blockBytesForadd = blockBytes;
            } else {
                Block block = blockchain.getBlockByHash(hash);
                if (block != null) {
                    blockBytesForadd = block.getEncodedBody();
                    cache.put(ByteArrayWrapper.wrap(hash), block.getEncodedBody());
                } else {
                    // not found
                    break;
                }
            }
            if ((out += blockBytesForadd.length) > P2pConstant.MAX_BODY_SIZE) {
                log.debug("<req-blocks-bodies-max-size-reach size={}/{}>", out, P2pConstant.MAX_BODY_SIZE);
                break;
            }
            blockBodies.add(blockBytesForadd);
        }
        this.p2pMgr.send(_nodeIdHashcode, _displayId, new ResBlocksBodies(blockBodies.toArray()));
        this.syncMgr.getSyncStats().updateTotalBlockRequestsByPeer(_displayId, blockBodies.size());
        if (log.isDebugEnabled()) {
            this.log.debug("<req-bodies req-size={} res-size={} node={}>", reqBlocks.getBlocksHashes().size(), blockBodies.size(), _displayId);
        }
    } else {
        this.log.error("<req-bodies decode-error, unable to decode bodies from {}, len: {}>", _displayId, _msgBytes.length);
        if (this.log.isTraceEnabled()) {
            this.log.trace("req-bodies dump: {}", ByteUtil.toHexString(_msgBytes));
        }
    }
}
Also used : ResBlocksBodies(org.aion.zero.impl.sync.msg.ResBlocksBodies) ArrayList(java.util.ArrayList) Block(org.aion.zero.impl.types.Block) ReqBlocksBodies(org.aion.zero.impl.sync.msg.ReqBlocksBodies)

Example 2 with ResBlocksBodies

use of org.aion.zero.impl.sync.msg.ResBlocksBodies in project aion by aionnetwork.

the class ResBlocksBodiesHandler method receive.

@Override
public void receive(int _nodeIdHashcode, String _displayId, final byte[] _msgBytes) {
    // for runtime survey information
    long startTime, duration;
    startTime = System.nanoTime();
    ResBlocksBodies resBlocksBodies = ResBlocksBodies.decodeUsingRef(_msgBytes);
    duration = System.nanoTime() - startTime;
    surveyLog.debug("Receive Stage 4: decode bodies, duration = {} ns.", duration);
    startTime = System.nanoTime();
    List<SharedRLPList> bodies = resBlocksBodies.getBlocksBodies();
    this.syncMgr.getSyncStats().updateResponseTime(_displayId, System.nanoTime(), RequestType.BODIES);
    if (bodies.size() == 0) {
        p2pMgr.errCheck(_nodeIdHashcode, _displayId);
        log.error("<res-bodies-empty node={}>", _displayId);
    } else {
        syncMgr.getSyncStats().updatePeerBlocks(_displayId, bodies.size(), BlockType.RECEIVED);
        syncMgr.validateAndAddBlocks(_nodeIdHashcode, _displayId, bodies);
    }
    duration = System.nanoTime() - startTime;
    surveyLog.debug("Receive Stage 5: validate bodies, duration = {} ns.", duration);
}
Also used : ResBlocksBodies(org.aion.zero.impl.sync.msg.ResBlocksBodies) SharedRLPList(org.aion.rlp.SharedRLPList)

Aggregations

ResBlocksBodies (org.aion.zero.impl.sync.msg.ResBlocksBodies)2 ArrayList (java.util.ArrayList)1 SharedRLPList (org.aion.rlp.SharedRLPList)1 ReqBlocksBodies (org.aion.zero.impl.sync.msg.ReqBlocksBodies)1 Block (org.aion.zero.impl.types.Block)1