Search in sources :

Example 16 with Channel

use of org.ethereum.net.server.Channel in project rskj by rsksmart.

the class SyncPool method heartBeat.

private void heartBeat() {
    for (Channel peer : this) {
        if (!peer.isIdle() && peer.getSyncStats().secondsSinceLastUpdate() > config.peerChannelReadTimeout()) {
            logger.info("Peer {}: no response after %d seconds", peer.getPeerIdShort(), config.peerChannelReadTimeout());
            peer.dropConnection();
        }
    }
}
Also used : Channel(org.ethereum.net.server.Channel)

Example 17 with Channel

use of org.ethereum.net.server.Channel in project rskj by rsksmart.

the class SyncPool method prepareActive.

private void prepareActive() {
    synchronized (peers) {
        List<Channel> active = new ArrayList<>(peers.values());
        if (active.isEmpty()) {
            return;
        }
        // filtering by 20% from top difficulty
        Collections.sort(active, new Comparator<Channel>() {

            @Override
            public int compare(Channel c1, Channel c2) {
                return c2.getTotalDifficulty().compareTo(c1.getTotalDifficulty());
            }
        });
        BigInteger highestDifficulty = active.get(0).getTotalDifficulty();
        int thresholdIdx = min(config.syncPeerCount(), active.size()) - 1;
        for (int i = thresholdIdx; i >= 0; i--) {
            if (isIn20PercentRange(active.get(i).getTotalDifficulty(), highestDifficulty)) {
                thresholdIdx = i;
                break;
            }
        }
        List<Channel> filtered = active.subList(0, thresholdIdx + 1);
        // sorting by latency in asc order
        Collections.sort(filtered, new Comparator<Channel>() {

            @Override
            public int compare(Channel c1, Channel c2) {
                return Double.valueOf(c1.getPeerStats().getAvgLatency()).compareTo(c2.getPeerStats().getAvgLatency());
            }
        });
        synchronized (activePeers) {
            activePeers.clear();
            activePeers.addAll(filtered);
        }
    }
}
Also used : Channel(org.ethereum.net.server.Channel) BigInteger(java.math.BigInteger)

Aggregations

Channel (org.ethereum.net.server.Channel)17 ChannelManager (org.ethereum.net.server.ChannelManager)13 Test (org.junit.Test)13 SimpleMessageChannel (co.rsk.net.simples.SimpleMessageChannel)10 SimpleChannelManager (org.ethereum.rpc.Simples.SimpleChannelManager)10 InvocationOnMock (org.mockito.invocation.InvocationOnMock)10 RskSystemProperties (co.rsk.config.RskSystemProperties)7 ProofOfWorkRule (co.rsk.validators.ProofOfWorkRule)7 BlockDifficulty (co.rsk.core.BlockDifficulty)4 NodeID (co.rsk.net.NodeID)3 DummyBlockValidationRule (co.rsk.validators.DummyBlockValidationRule)3 BlockGenerator (co.rsk.blockchain.utils.BlockGenerator)2 DownloadingHeadersSyncState (co.rsk.net.sync.DownloadingHeadersSyncState)2 SyncConfiguration (co.rsk.net.sync.SyncConfiguration)2 BigInteger (java.math.BigInteger)2 ArrayList (java.util.ArrayList)2 Coin (co.rsk.core.Coin)1 DifficultyCalculator (co.rsk.core.DifficultyCalculator)1 BlockExecutor (co.rsk.core.bc.BlockExecutor)1 MessageChannel (co.rsk.net.MessageChannel)1