Search in sources :

Example 1 with ChainState

use of org.hyperledger.besu.ethereum.eth.manager.ChainState in project besu by hyperledger.

the class SyncStateTest method mockChainIsBetterThan.

private EthPeer mockChainIsBetterThan(final EthPeer peer, final boolean isBetter) {
    final ChainState chainState = spy(peer.chainState());
    final ChainHeadEstimate chainStateSnapshot = spy(peer.chainStateSnapshot());
    lenient().doReturn(isBetter).when(chainState).chainIsBetterThan(any());
    lenient().doReturn(isBetter).when(chainStateSnapshot).chainIsBetterThan(any());
    final EthPeer mockedPeer = spy(peer);
    lenient().doReturn(chainStateSnapshot).when(chainState).getSnapshot();
    lenient().doReturn(chainStateSnapshot).when(mockedPeer).chainStateSnapshot();
    lenient().doReturn(chainState).when(mockedPeer).chainState();
    return mockedPeer;
}
Also used : ChainHeadEstimate(org.hyperledger.besu.ethereum.eth.manager.ChainHeadEstimate) ChainState(org.hyperledger.besu.ethereum.eth.manager.ChainState) EthPeer(org.hyperledger.besu.ethereum.eth.manager.EthPeer) RespondingEthPeer(org.hyperledger.besu.ethereum.eth.manager.RespondingEthPeer)

Example 2 with ChainState

use of org.hyperledger.besu.ethereum.eth.manager.ChainState in project besu by hyperledger.

the class BetterSyncTargetEvaluatorTest method peer.

private EthPeer peer(final long chainHeight, final long totalDifficulty) {
    final EthPeer peer = mock(EthPeer.class);
    final ChainState chainState = new ChainState();
    chainState.updateHeightEstimate(chainHeight);
    chainState.statusReceived(Hash.EMPTY, Difficulty.of(totalDifficulty));
    when(peer.chainState()).thenReturn(chainState);
    return peer;
}
Also used : ChainState(org.hyperledger.besu.ethereum.eth.manager.ChainState) EthPeer(org.hyperledger.besu.ethereum.eth.manager.EthPeer)

Example 3 with ChainState

use of org.hyperledger.besu.ethereum.eth.manager.ChainState in project besu by hyperledger.

the class BetterSyncTargetEvaluator method shouldSwitchSyncTarget.

public boolean shouldSwitchSyncTarget(final EthPeer currentSyncTarget) {
    final ChainState currentPeerChainState = currentSyncTarget.chainState();
    final Optional<EthPeer> maybeBestPeer = ethPeers.bestPeer();
    return maybeBestPeer.map(bestPeer -> {
        if (EthPeers.BEST_CHAIN.compare(bestPeer, currentSyncTarget) <= 0) {
            // Our current target is better or equal to the best peer
            return false;
        }
        // Require some threshold to be exceeded before switching targets to keep some
        // stability when multiple peers are in range of each other
        final ChainState bestPeerChainState = bestPeer.chainState();
        final Difficulty tdDifference = bestPeerChainState.getEstimatedTotalDifficulty().subtract(currentPeerChainState.getBestBlock().getTotalDifficulty());
        if (tdDifference.compareTo(config.getDownloaderChangeTargetThresholdByTd()) > 0) {
            return true;
        }
        final long heightDifference = bestPeerChainState.getEstimatedHeight() - currentPeerChainState.getEstimatedHeight();
        return heightDifference > config.getDownloaderChangeTargetThresholdByHeight();
    }).orElse(false);
}
Also used : Difficulty(org.hyperledger.besu.ethereum.core.Difficulty) EthPeer(org.hyperledger.besu.ethereum.eth.manager.EthPeer) EthPeers(org.hyperledger.besu.ethereum.eth.manager.EthPeers) ChainState(org.hyperledger.besu.ethereum.eth.manager.ChainState) Optional(java.util.Optional) SynchronizerConfiguration(org.hyperledger.besu.ethereum.eth.sync.SynchronizerConfiguration) ChainState(org.hyperledger.besu.ethereum.eth.manager.ChainState) EthPeer(org.hyperledger.besu.ethereum.eth.manager.EthPeer) Difficulty(org.hyperledger.besu.ethereum.core.Difficulty)

Example 4 with ChainState

use of org.hyperledger.besu.ethereum.eth.manager.ChainState in project besu by hyperledger.

the class TrailingPeerLimiterTest method addPeerWithEstimatedHeight.

private EthPeer addPeerWithEstimatedHeight(final long height) {
    final EthPeer peer = mock(EthPeer.class);
    final ChainState chainState = new ChainState();
    chainState.statusReceived(Hash.EMPTY, Difficulty.ONE);
    chainState.update(Hash.EMPTY, height);
    when(peer.chainState()).thenReturn(chainState);
    peers.add(peer);
    return peer;
}
Also used : ChainState(org.hyperledger.besu.ethereum.eth.manager.ChainState) EthPeer(org.hyperledger.besu.ethereum.eth.manager.EthPeer)

Aggregations

ChainState (org.hyperledger.besu.ethereum.eth.manager.ChainState)4 EthPeer (org.hyperledger.besu.ethereum.eth.manager.EthPeer)4 Optional (java.util.Optional)1 Difficulty (org.hyperledger.besu.ethereum.core.Difficulty)1 ChainHeadEstimate (org.hyperledger.besu.ethereum.eth.manager.ChainHeadEstimate)1 EthPeers (org.hyperledger.besu.ethereum.eth.manager.EthPeers)1 RespondingEthPeer (org.hyperledger.besu.ethereum.eth.manager.RespondingEthPeer)1 SynchronizerConfiguration (org.hyperledger.besu.ethereum.eth.sync.SynchronizerConfiguration)1