From c13bf7779b92968ed9ff4970cd2a99b6c2e58935 Mon Sep 17 00:00:00 2001 From: Sally MacFarlane Date: Mon, 16 Sep 2024 07:55:41 +1000 Subject: [PATCH] use peer reputation to compare current peers Signed-off-by: Sally MacFarlane --- CHANGELOG.md | 1 + .../hyperledger/besu/ethereum/eth/manager/EthPeers.java | 4 ++-- .../besu/ethereum/eth/manager/EthPeersTest.java | 2 ++ .../eth/sync/AbstractBlockPropagationManagerTest.java | 8 +++++++- .../eth/sync/backwardsync/BackwardSyncAlgSpec.java | 3 +++ .../ethereum/eth/sync/fastsync/FastSyncActionsTest.java | 4 ++++ .../eth/sync/fullsync/FullSyncChainDownloaderTest.java | 3 +++ 7 files changed, 22 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 1da225c56c9..ceaeaa91fe2 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,6 +8,7 @@ ### Additions and Improvements - Remove privacy test classes support [#7569](https://github.com/hyperledger/besu/pull/7569) +- Use peer reputation (then chain height) to compare current peers ### Bug fixes - Fix for `debug_traceCall` to handle transactions without specified gas price. [#7510](https://github.com/hyperledger/besu/pull/7510) diff --git a/ethereum/eth/src/main/java/org/hyperledger/besu/ethereum/eth/manager/EthPeers.java b/ethereum/eth/src/main/java/org/hyperledger/besu/ethereum/eth/manager/EthPeers.java index d1a54d4d3d3..8d2df7f1f3e 100644 --- a/ethereum/eth/src/main/java/org/hyperledger/besu/ethereum/eth/manager/EthPeers.java +++ b/ethereum/eth/src/main/java/org/hyperledger/besu/ethereum/eth/manager/EthPeers.java @@ -137,7 +137,7 @@ public EthPeers( this.clock = clock; this.permissioningProviders = permissioningProviders; this.maxMessageSize = maxMessageSize; - this.bestPeerComparator = HEAVIEST_CHAIN; + this.bestPeerComparator = MOST_USEFUL_PEER; this.localNodeId = localNodeId; this.peerUpperBound = peerUpperBound; this.maxRemotelyInitiatedConnections = maxRemotelyInitiatedConnections; @@ -369,7 +369,7 @@ public Optional bestPeerMatchingCriteria(final Predicate match } public void setBestPeerComparator(final Comparator comparator) { - LOG.info("Updating the default best peer comparator"); + LOG.info("Updating the default best peer comparator to {}", comparator); bestPeerComparator = comparator; } diff --git a/ethereum/eth/src/test/java/org/hyperledger/besu/ethereum/eth/manager/EthPeersTest.java b/ethereum/eth/src/test/java/org/hyperledger/besu/ethereum/eth/manager/EthPeersTest.java index f6c635aa51f..68dd0c3316f 100644 --- a/ethereum/eth/src/test/java/org/hyperledger/besu/ethereum/eth/manager/EthPeersTest.java +++ b/ethereum/eth/src/test/java/org/hyperledger/besu/ethereum/eth/manager/EthPeersTest.java @@ -85,6 +85,7 @@ public void comparesPeersWithHeightAndTd() { assertThat(EthPeers.HEAVIEST_CHAIN.compare(peerA, peerA)).isEqualTo(0); assertThat(EthPeers.HEAVIEST_CHAIN.compare(peerB, peerB)).isEqualTo(0); + ethProtocolManager.ethContext().getEthPeers().setBestPeerComparator(EthPeers.HEAVIEST_CHAIN); assertThat(ethProtocolManager.ethContext().getEthPeers().bestPeer()).contains(peerB); assertThat(ethProtocolManager.ethContext().getEthPeers().bestPeerWithHeightEstimate()) .contains(peerB); @@ -113,6 +114,7 @@ public void comparesPeersWithTdAndNoHeight() { assertThat(EthPeers.HEAVIEST_CHAIN.compare(peerA, peerA)).isEqualTo(0); assertThat(EthPeers.HEAVIEST_CHAIN.compare(peerB, peerB)).isEqualTo(0); + ethProtocolManager.ethContext().getEthPeers().setBestPeerComparator(EthPeers.HEAVIEST_CHAIN); assertThat(ethProtocolManager.ethContext().getEthPeers().bestPeer()).contains(peerA); assertThat(ethProtocolManager.ethContext().getEthPeers().bestPeerWithHeightEstimate()) .isEmpty(); diff --git a/ethereum/eth/src/test/java/org/hyperledger/besu/ethereum/eth/sync/AbstractBlockPropagationManagerTest.java b/ethereum/eth/src/test/java/org/hyperledger/besu/ethereum/eth/sync/AbstractBlockPropagationManagerTest.java index c576b7f8e26..bbde6f1846e 100644 --- a/ethereum/eth/src/test/java/org/hyperledger/besu/ethereum/eth/sync/AbstractBlockPropagationManagerTest.java +++ b/ethereum/eth/src/test/java/org/hyperledger/besu/ethereum/eth/sync/AbstractBlockPropagationManagerTest.java @@ -117,7 +117,13 @@ protected void setup(final DataStorageFormat dataStorageFormat) { blockchainUtil.getTransactionPool(), EthProtocolConfiguration.defaultConfig()); syncConfig = SynchronizerConfiguration.builder().blockPropagationRange(-3, 5).build(); - syncState = new SyncState(blockchain, ethProtocolManager.ethContext().getEthPeers()); + + // for tests use simple peer comparator + final EthPeers ethPeers = ethProtocolManager.ethContext().getEthPeers(); + ethPeers.setBestPeerComparator(EthPeers.HEAVIEST_CHAIN); + + syncState = new SyncState(blockchain, ethPeers); + blockBroadcaster = mock(BlockBroadcaster.class); blockPropagationManager = new BlockPropagationManager( diff --git a/ethereum/eth/src/test/java/org/hyperledger/besu/ethereum/eth/sync/backwardsync/BackwardSyncAlgSpec.java b/ethereum/eth/src/test/java/org/hyperledger/besu/ethereum/eth/sync/backwardsync/BackwardSyncAlgSpec.java index bd5a9e7ec9c..aabd371223a 100644 --- a/ethereum/eth/src/test/java/org/hyperledger/besu/ethereum/eth/sync/backwardsync/BackwardSyncAlgSpec.java +++ b/ethereum/eth/src/test/java/org/hyperledger/besu/ethereum/eth/sync/backwardsync/BackwardSyncAlgSpec.java @@ -29,6 +29,7 @@ import org.hyperledger.besu.ethereum.core.Block; import org.hyperledger.besu.ethereum.core.BlockDataGenerator; import org.hyperledger.besu.ethereum.core.TransactionReceipt; +import org.hyperledger.besu.ethereum.eth.manager.EthPeers; import org.hyperledger.besu.plugin.services.BesuEvents; import java.nio.charset.StandardCharsets; @@ -176,6 +177,8 @@ public void shouldAwokeWhenConditionReachedAndReady() throws Exception { when(context.getSyncState().subscribeTTDReached(any())).thenReturn(88L); when(context.getSyncState().subscribeCompletionReached(any())).thenReturn(99L); when(context.getEthContext().getEthPeers().peerCount()).thenReturn(1); + when(context.getEthContext().getEthPeers().getBestPeerComparator()) + .thenReturn(EthPeers.HEAVIEST_CHAIN); final CompletableFuture voidCompletableFuture = algorithm.waitForReady(); Thread.sleep(50); diff --git a/ethereum/eth/src/test/java/org/hyperledger/besu/ethereum/eth/sync/fastsync/FastSyncActionsTest.java b/ethereum/eth/src/test/java/org/hyperledger/besu/ethereum/eth/sync/fastsync/FastSyncActionsTest.java index 68caf2182c0..5e73b3e29da 100644 --- a/ethereum/eth/src/test/java/org/hyperledger/besu/ethereum/eth/sync/fastsync/FastSyncActionsTest.java +++ b/ethereum/eth/src/test/java/org/hyperledger/besu/ethereum/eth/sync/fastsync/FastSyncActionsTest.java @@ -100,7 +100,11 @@ public void setUp(final DataStorageFormat storageFormat) { blockchainSetupUtil.getTransactionPool(), EthProtocolConfiguration.defaultConfig()); ethContext = ethProtocolManager.ethContext(); + ethPeers = ethContext.getEthPeers(); + // for tests use the heaviest chain comparator + ethPeers.setBestPeerComparator(EthPeers.HEAVIEST_CHAIN); + syncState = new SyncState(blockchain, ethPeers); metricsSystem = new NoOpMetricsSystem(); fastSyncActions = diff --git a/ethereum/eth/src/test/java/org/hyperledger/besu/ethereum/eth/sync/fullsync/FullSyncChainDownloaderTest.java b/ethereum/eth/src/test/java/org/hyperledger/besu/ethereum/eth/sync/fullsync/FullSyncChainDownloaderTest.java index ac7f0fb8257..f24f236abb2 100644 --- a/ethereum/eth/src/test/java/org/hyperledger/besu/ethereum/eth/sync/fullsync/FullSyncChainDownloaderTest.java +++ b/ethereum/eth/src/test/java/org/hyperledger/besu/ethereum/eth/sync/fullsync/FullSyncChainDownloaderTest.java @@ -30,6 +30,7 @@ import org.hyperledger.besu.ethereum.core.TransactionReceipt; import org.hyperledger.besu.ethereum.eth.EthProtocolConfiguration; import org.hyperledger.besu.ethereum.eth.manager.EthContext; +import org.hyperledger.besu.ethereum.eth.manager.EthPeers; import org.hyperledger.besu.ethereum.eth.manager.EthProtocolManager; import org.hyperledger.besu.ethereum.eth.manager.EthProtocolManagerTestUtil; import org.hyperledger.besu.ethereum.eth.manager.EthScheduler; @@ -104,6 +105,8 @@ public void setupTest(final DataStorageFormat storageFormat) { localBlockchainSetup.getTransactionPool(), EthProtocolConfiguration.defaultConfig()); ethContext = ethProtocolManager.ethContext(); + // for tests use the heaviest chain comparator + ethContext.getEthPeers().setBestPeerComparator(EthPeers.HEAVIEST_CHAIN); syncState = new SyncState(protocolContext.getBlockchain(), ethContext.getEthPeers()); }