mirror of https://github.com/hyperledger/besu
Refactor to async retrieve blocks, and change peer when retrying to get a block (#3326)
* Refactor to async retrieve blocks, and change peer when retrying to get a block Signed-off-by: Fabio Di Fabio <fabio.difabio@consensys.net> * Apply suggested changes Signed-off-by: Fabio Di Fabio <fabio.difabio@consensys.net> * Remove deprecated class Signed-off-by: Fabio Di Fabio <fabio.difabio@consensys.net> * Add more logs arond block synchronizer Signed-off-by: Fabio Di Fabio <fabio.difabio@consensys.net> * First try do download the block from the peer that announced it Signed-off-by: Fabio Di Fabio <fabio.difabio@consensys.net> * Avoid redownload non annunced blocks Signed-off-by: Fabio Di Fabio <fabio.difabio@consensys.net> * Conditionally log at trace level Signed-off-by: Fabio Di Fabio <fabio.difabio@consensys.net> * Use max number of peers when retrying to download a block to try all peers Signed-off-by: Fabio Di Fabio <fabio.difabio@consensys.net> * Use the shared Slf4jLambdaHelper, instead of custom helper Signed-off-by: Fabio Di Fabio <fabio.difabio@consensys.net>pull/3358/head
parent
d1d9e49ba0
commit
e6210f9209
@ -1,98 +0,0 @@ |
||||
/* |
||||
* Copyright ConsenSys AG. |
||||
* |
||||
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with |
||||
* the License. You may obtain a copy of the License at |
||||
* |
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
* |
||||
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on |
||||
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the |
||||
* specific language governing permissions and limitations under the License. |
||||
* |
||||
* SPDX-License-Identifier: Apache-2.0 |
||||
*/ |
||||
package org.hyperledger.besu.ethereum.eth.manager.task; |
||||
|
||||
import org.hyperledger.besu.datatypes.Hash; |
||||
import org.hyperledger.besu.ethereum.core.Block; |
||||
import org.hyperledger.besu.ethereum.eth.manager.EthContext; |
||||
import org.hyperledger.besu.ethereum.eth.manager.EthPeer; |
||||
import org.hyperledger.besu.ethereum.eth.manager.exceptions.IncompleteResultsException; |
||||
import org.hyperledger.besu.ethereum.mainnet.ProtocolSchedule; |
||||
import org.hyperledger.besu.plugin.services.MetricsSystem; |
||||
|
||||
import java.util.List; |
||||
import java.util.Optional; |
||||
|
||||
import org.slf4j.Logger; |
||||
import org.slf4j.LoggerFactory; |
||||
|
||||
/** Downloads a block from a peer. Will complete exceptionally if block cannot be downloaded. */ |
||||
public class GetBlockFromPeersTask extends AbstractEthTask<AbstractPeerTask.PeerTaskResult<Block>> { |
||||
private static final Logger LOG = LoggerFactory.getLogger(GetBlockFromPeersTask.class); |
||||
|
||||
private final List<EthPeer> peers; |
||||
private final EthContext ethContext; |
||||
private final ProtocolSchedule protocolSchedule; |
||||
private final Optional<Hash> hash; |
||||
private final long blockNumber; |
||||
private final MetricsSystem metricsSystem; |
||||
|
||||
protected GetBlockFromPeersTask( |
||||
final List<EthPeer> peers, |
||||
final ProtocolSchedule protocolSchedule, |
||||
final EthContext ethContext, |
||||
final Optional<Hash> hash, |
||||
final long blockNumber, |
||||
final MetricsSystem metricsSystem) { |
||||
super(metricsSystem); |
||||
this.peers = peers; |
||||
this.ethContext = ethContext; |
||||
this.blockNumber = blockNumber; |
||||
this.metricsSystem = metricsSystem; |
||||
this.protocolSchedule = protocolSchedule; |
||||
this.hash = hash; |
||||
} |
||||
|
||||
public static GetBlockFromPeersTask create( |
||||
final List<EthPeer> peers, |
||||
final ProtocolSchedule protocolSchedule, |
||||
final EthContext ethContext, |
||||
final Optional<Hash> hash, |
||||
final long blockNumber, |
||||
final MetricsSystem metricsSystem) { |
||||
return new GetBlockFromPeersTask( |
||||
peers, protocolSchedule, ethContext, hash, blockNumber, metricsSystem); |
||||
} |
||||
|
||||
@Override |
||||
protected void executeTask() { |
||||
LOG.debug("Downloading block {} from peers {}.", hash, peers.stream().map(EthPeer::toString)); |
||||
getBlockFromPeers(peers); |
||||
} |
||||
|
||||
private void getBlockFromPeers(final List<EthPeer> peers) { |
||||
if (peers.isEmpty()) { |
||||
result.completeExceptionally(new IncompleteResultsException()); |
||||
} |
||||
final EthPeer peer = peers.get(0); |
||||
if (peer.isDisconnected()) { |
||||
getBlockFromPeers(peers.subList(1, peers.size())); |
||||
} |
||||
LOG.debug("Trying downloading block {} from peer {}.", hash, peer); |
||||
final AbstractPeerTask<Block> getBlockTask = |
||||
GetBlockFromPeerTask.create(protocolSchedule, ethContext, hash, blockNumber, metricsSystem) |
||||
.assignPeer(peer); |
||||
getBlockTask |
||||
.run() |
||||
.whenComplete( |
||||
(r, t) -> { |
||||
if (t != null) { |
||||
getBlockFromPeers(peers.subList(1, peers.size())); |
||||
} else { |
||||
result.complete(r); |
||||
} |
||||
}); |
||||
} |
||||
} |
@ -1,31 +0,0 @@ |
||||
/* |
||||
* Copyright ConsenSys AG. |
||||
* |
||||
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with |
||||
* the License. You may obtain a copy of the License at |
||||
* |
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
* |
||||
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on |
||||
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the |
||||
* specific language governing permissions and limitations under the License. |
||||
* |
||||
* SPDX-License-Identifier: Apache-2.0 |
||||
*/ |
||||
package org.hyperledger.besu.ethereum.eth.manager; |
||||
|
||||
import org.hyperledger.besu.ethereum.eth.manager.DeterministicEthScheduler.TimeoutPolicy; |
||||
import org.hyperledger.besu.metrics.noop.NoOpMetricsSystem; |
||||
import org.hyperledger.besu.testutil.TestClock; |
||||
|
||||
public class EthContextTestUtil { |
||||
|
||||
private static final String PROTOCOL_NAME = "ETH"; |
||||
|
||||
public static EthContext createTestEthContext(final TimeoutPolicy timeoutPolicy) { |
||||
return new EthContext( |
||||
new EthPeers(PROTOCOL_NAME, TestClock.fixed(), new NoOpMetricsSystem()), |
||||
new EthMessages(), |
||||
new DeterministicEthScheduler(timeoutPolicy)); |
||||
} |
||||
} |
Loading…
Reference in new issue