Search in sources :

Example 1 with Node

use of io.nuls.network.model.Node in project nuls by nuls-io.

the class DownloadThreadManager method checkRollback.

private boolean checkRollback(Block localBestBlock, int rollbackCount) throws NulsException {
    if (rollbackCount >= 20) {
        // resetNetwork("number of rollbackTx blocks greater than 10 during download");
        return false;
    }
    List<Node> nodes = newestInfos.getNodes();
    long localHeight = localBestBlock.getHeader().getHeight();
    NulsDigestData localBestHash = localBestBlock.getHeader().getHash();
    for (Node node : nodes) {
        Block block = DownloadUtils.getBlockByHash(localBestHash, node);
        if (block != null && localHeight == block.getHeader().getHeight()) {
            return true;
        }
    }
    if (newestInfos.getNodes().size() > 0) {
        consensusService.rollbackBlock(localBestBlock);
    } else {
        // resetNetwork("the number of available nodes is insufficient for rollbackTx blocks");
        return false;
    }
    localBestBlock = blockService.getBestBlock().getData();
    return checkRollback(localBestBlock, rollbackCount + 1);
}
Also used : Node(io.nuls.network.model.Node) NulsDigestData(io.nuls.kernel.model.NulsDigestData) Block(io.nuls.kernel.model.Block)

Example 2 with Node

use of io.nuls.network.model.Node in project nuls by nuls-io.

the class DownloadProcessor method getNetworkNewestBlock.

public NetworkNewestBlockInfos getNetworkNewestBlock() {
    Collection<Node> nodeList = networkService.getAvailableNodes();
    Map<NulsDigestData, Integer> statisticsMaps = new HashMap<>();
    Map<NulsDigestData, List<Node>> nodeMaps = new HashMap<>();
    // System.out.println("--------------start download-------------------");
    for (Node node : nodeList) {
        // System.out.println(node.getId() + " : " + node.getBestBlockHeight() + " : " + node.getBestBlockHash());
        NulsDigestData hash = node.getBestBlockHash();
        Integer statistics = statisticsMaps.get(hash);
        if (statistics == null) {
            statisticsMaps.put(hash, 0);
        }
        statisticsMaps.put(hash, statisticsMaps.get(hash) + 1);
        List<Node> nodes = nodeMaps.get(hash);
        if (nodes == null) {
            nodes = new ArrayList<>();
            nodeMaps.put(hash, nodes);
        }
        nodes.add(node);
    }
    // max number
    int max = 0;
    long bestHeight = 0;
    NulsDigestData bestHash = null;
    List<Node> nodes = null;
    for (Map.Entry<NulsDigestData, Integer> entry : statisticsMaps.entrySet()) {
        int count = entry.getValue();
        NulsDigestData hash = entry.getKey();
        List<Node> tempNodes = nodeMaps.get(hash);
        long height = tempNodes.get(0).getBestBlockHeight();
        if (count > max || (count == max && bestHeight < height)) {
            max = count;
            bestHash = hash;
            bestHeight = height;
            nodes = tempNodes;
        }
    }
    if (nodes == null || nodes.size() == 0) {
        throw new NulsRuntimeException(NetworkErrorCode.NET_NODE_NOT_FOUND);
    }
    return new NetworkNewestBlockInfos(bestHeight, bestHash, nodes);
}
Also used : Node(io.nuls.network.model.Node) NulsRuntimeException(io.nuls.kernel.exception.NulsRuntimeException) NetworkNewestBlockInfos(io.nuls.protocol.base.download.entity.NetworkNewestBlockInfos) NulsDigestData(io.nuls.kernel.model.NulsDigestData)

Example 3 with Node

use of io.nuls.network.model.Node in project nuls by nuls-io.

the class RequestThread method downloadRound.

private void downloadRound() {
    for (int i = nodeList.size() - 1; i >= 0; i--) {
        Node node = nodeList.get(i);
        if (!node.isHandShake()) {
            nodeList.remove(i);
            continue;
        }
        int size = count;
        if ((startHeight + size) > endHeight) {
            size = (int) (endHeight - startHeight + 1);
        }
        if (size <= 0) {
            break;
        }
        // Log.info("request:{}-{} ,from {}.", startHeight, size, node.getId());
        boolean result = request(node, startHeight, size);
        if (result) {
            startHeight += size;
        }
    }
}
Also used : Node(io.nuls.network.model.Node)

Example 4 with Node

use of io.nuls.network.model.Node in project nuls by nuls-io.

the class ClientChannelHandler method channelRead0.

@Override
protected void channelRead0(ChannelHandlerContext ctx, Object msg) throws Exception {
    try {
        Attribute<Node> nodeAttribute = ctx.channel().attr(NodeAttributeKey.NODE_KEY);
        Node node = nodeAttribute.get();
        ByteBuf buf = (ByteBuf) msg;
        messageProcessor.processor(buf, node);
    } catch (Exception e) {
        Log.error("----------------exceptionCaught   111 ---------");
        throw e;
    }
}
Also used : Node(io.nuls.network.model.Node) ByteBuf(io.netty.buffer.ByteBuf) TooLongFrameException(io.netty.handler.codec.TooLongFrameException) IOException(java.io.IOException)

Example 5 with Node

use of io.nuls.network.model.Node in project nuls by nuls-io.

the class ClientChannelHandler method channelActive.

@Override
public void channelActive(ChannelHandlerContext ctx) throws Exception {
    super.channelActive(ctx);
    Attribute<Node> nodeAttribute = ctx.channel().attr(NodeAttributeKey.NODE_KEY);
    Node node = nodeAttribute.get();
    if (node != null) {
        node.setChannel(ctx.channel());
    }
    if (node != null && node.getConnectedListener() != null) {
        node.getConnectedListener().action();
    }
}
Also used : Node(io.nuls.network.model.Node)

Aggregations

Node (io.nuls.network.model.Node)48 Result (io.nuls.kernel.model.Result)4 NodesContainer (io.nuls.network.netty.container.NodesContainer)4 Block (io.nuls.kernel.model.Block)3 NulsDigestData (io.nuls.kernel.model.NulsDigestData)3 RpcClientResult (io.nuls.kernel.model.RpcClientResult)3 BroadcastResult (io.nuls.network.model.BroadcastResult)3 BlockMessage (io.nuls.protocol.message.BlockMessage)3 ApiOperation (io.swagger.annotations.ApiOperation)3 ApiResponses (io.swagger.annotations.ApiResponses)3 IOException (java.io.IOException)3 GET (javax.ws.rs.GET)3 Path (javax.ws.rs.Path)3 Produces (javax.ws.rs.Produces)3 Test (org.junit.Test)3 ByteBuf (io.netty.buffer.ByteBuf)2 SocketChannel (io.netty.channel.socket.SocketChannel)2 TooLongFrameException (io.netty.handler.codec.TooLongFrameException)2 NodeMessageBody (io.nuls.network.protocol.message.NodeMessageBody)2 NodesMessage (io.nuls.network.protocol.message.NodesMessage)2