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;
}
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;
}
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);
}
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;
}
Aggregations