Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add eth protocol manager test builder #7954

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import org.hyperledger.besu.ethereum.core.MutableWorldState;
import org.hyperledger.besu.ethereum.eth.manager.EthContext;
import org.hyperledger.besu.ethereum.eth.manager.EthProtocolManager;
import org.hyperledger.besu.ethereum.eth.manager.EthProtocolManagerTestBuilder;
import org.hyperledger.besu.ethereum.eth.manager.EthProtocolManagerTestUtil;
import org.hyperledger.besu.ethereum.eth.manager.EthScheduler;
import org.hyperledger.besu.ethereum.eth.manager.RespondingEthPeer;
Expand Down Expand Up @@ -94,12 +95,14 @@ public void setUpUnchangedState() {

tempDir = Files.createTempDir().toPath();
ethProtocolManager =
EthProtocolManagerTestUtil.create(
new EthScheduler(
syncConfig.getDownloaderParallelism(),
syncConfig.getTransactionsParallelism(),
syncConfig.getComputationParallelism(),
metricsSystem));
EthProtocolManagerTestBuilder.builder()
.setEthScheduler(
new EthScheduler(
syncConfig.getDownloaderParallelism(),
syncConfig.getTransactionsParallelism(),
syncConfig.getComputationParallelism(),
metricsSystem))
.build();

peer = EthProtocolManagerTestUtil.createPeer(ethProtocolManager, blockHeader.getNumber());

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ public class EthPeersTest {
@BeforeEach
public void setup() throws Exception {
when(peerRequest.sendRequest(any())).thenReturn(responseStream);
ethProtocolManager = EthProtocolManagerTestUtil.create();
ethProtocolManager = EthProtocolManagerTestBuilder.builder().build();
ethPeers = ethProtocolManager.ethContext().getEthPeers();
final ChainHeadTracker mock = mock(ChainHeadTracker.class);
final BlockHeader blockHeader = mock(BlockHeader.class);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,235 @@
/*
* Copyright contributors to Besu.
*
* 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 static org.hyperledger.besu.ethereum.core.InMemoryKeyValueStorageProvider.createInMemoryBlockchain;
import static org.mockito.Mockito.mock;

import org.hyperledger.besu.config.GenesisConfigFile;
import org.hyperledger.besu.ethereum.chain.Blockchain;
import org.hyperledger.besu.ethereum.chain.GenesisState;
import org.hyperledger.besu.ethereum.core.BlockchainSetupUtil;
import org.hyperledger.besu.ethereum.core.ProtocolScheduleFixture;
import org.hyperledger.besu.ethereum.eth.EthProtocolConfiguration;
import org.hyperledger.besu.ethereum.eth.peervalidation.PeerValidator;
import org.hyperledger.besu.ethereum.eth.sync.SyncMode;
import org.hyperledger.besu.ethereum.eth.sync.SynchronizerConfiguration;
import org.hyperledger.besu.ethereum.eth.transactions.TransactionPool;
import org.hyperledger.besu.ethereum.forkid.ForkIdManager;
import org.hyperledger.besu.ethereum.mainnet.ProtocolSchedule;
import org.hyperledger.besu.ethereum.worldstate.WorldStateArchive;
import org.hyperledger.besu.metrics.noop.NoOpMetricsSystem;
import org.hyperledger.besu.plugin.services.storage.DataStorageFormat;
import org.hyperledger.besu.testutil.DeterministicEthScheduler;
import org.hyperledger.besu.testutil.TestClock;

import java.math.BigInteger;
import java.util.Collections;
import java.util.List;
import java.util.Optional;

import org.apache.tuweni.bytes.Bytes;

public class EthProtocolManagerTestBuilder {
private static final BigInteger DEFAULT_NETWORK_ID = BigInteger.ONE;
private static final ProtocolSchedule DEFAULT_PROTOCOL_SCHEDULE = ProtocolScheduleFixture.MAINNET;

private ProtocolSchedule protocolSchedule;
private GenesisConfigFile genesisConfigFile;
private GenesisState genesisState;
private Blockchain blockchain;
private BigInteger networkId;
private WorldStateArchive worldStateArchive;
private TransactionPool transactionPool;
private EthProtocolConfiguration ethereumWireProtocolConfiguration;
private EthPeers ethPeers;
private EthMessages ethMessages;
private EthMessages snapMessages;
private EthScheduler ethScheduler;
private EthContext ethContext;
private List<PeerValidator> peerValidators;
private Optional<MergePeerFilter> mergePeerFilter;
private SynchronizerConfiguration synchronizerConfiguration;

public static EthProtocolManagerTestBuilder builder() {
return new EthProtocolManagerTestBuilder();
}

public EthProtocolManagerTestBuilder setProtocolSchedule(
final ProtocolSchedule protocolSchedule) {
this.protocolSchedule = protocolSchedule;
return this;
}

public EthProtocolManagerTestBuilder setGenesisConfigFile(
final GenesisConfigFile genesisConfigFile) {
this.genesisConfigFile = genesisConfigFile;
return this;
}

public EthProtocolManagerTestBuilder setGenesisState(final GenesisState genesisState) {
this.genesisState = genesisState;
return this;
}

public EthProtocolManagerTestBuilder setBlockchain(final Blockchain blockchain) {
this.blockchain = blockchain;
return this;
}

public EthProtocolManagerTestBuilder setNetworkId(final BigInteger networkId) {
this.networkId = networkId;
return this;
}

public EthProtocolManagerTestBuilder setWorldStateArchive(
final WorldStateArchive worldStateArchive) {
this.worldStateArchive = worldStateArchive;
return this;
}

public EthProtocolManagerTestBuilder setTransactionPool(final TransactionPool transactionPool) {
this.transactionPool = transactionPool;
return this;
}

public EthProtocolManagerTestBuilder setEthereumWireProtocolConfiguration(
final EthProtocolConfiguration ethereumWireProtocolConfiguration) {
this.ethereumWireProtocolConfiguration = ethereumWireProtocolConfiguration;
return this;
}

public EthProtocolManagerTestBuilder setEthPeers(final EthPeers ethPeers) {
this.ethPeers = ethPeers;
return this;
}

public EthProtocolManagerTestBuilder setEthMessages(final EthMessages ethMessages) {
this.ethMessages = ethMessages;
return this;
}

public EthProtocolManagerTestBuilder setSnapMessages(final EthMessages snapMessages) {
this.snapMessages = snapMessages;
return this;
}

public EthProtocolManagerTestBuilder setEthContext(final EthContext ethContext) {
this.ethContext = ethContext;
return this;
}

public EthProtocolManagerTestBuilder setPeerValidators(final List<PeerValidator> peerValidators) {
this.peerValidators = peerValidators;
return this;
}

public EthProtocolManagerTestBuilder setMergePeerFilter(
final Optional<MergePeerFilter> mergePeerFilter) {
this.mergePeerFilter = mergePeerFilter;
return this;
}

public EthProtocolManagerTestBuilder setSynchronizerConfiguration(
final SynchronizerConfiguration synchronizerConfiguration) {
this.synchronizerConfiguration = synchronizerConfiguration;
return this;
}

public EthProtocolManagerTestBuilder setEthScheduler(final EthScheduler ethScheduler) {
this.ethScheduler = ethScheduler;
return this;
}

public EthProtocolManager build() {
if (protocolSchedule == null) {
protocolSchedule = DEFAULT_PROTOCOL_SCHEDULE;
}
if (genesisConfigFile == null) {
genesisConfigFile = GenesisConfigFile.mainnet();
}
if (genesisState == null) {
genesisState = GenesisState.fromConfig(genesisConfigFile, protocolSchedule);
}
if (blockchain == null) {
blockchain = createInMemoryBlockchain(genesisState.getBlock());
}
if (networkId == null) {
networkId = DEFAULT_NETWORK_ID;
}
if (worldStateArchive == null) {
worldStateArchive =
BlockchainSetupUtil.forTesting(DataStorageFormat.FOREST).getWorldArchive();
}
if (transactionPool == null) {
transactionPool = mock(TransactionPool.class);
}
if (ethereumWireProtocolConfiguration == null) {
ethereumWireProtocolConfiguration = EthProtocolConfiguration.defaultConfig();
}
if (ethPeers == null) {
ethPeers =
new EthPeers(
() -> protocolSchedule.getByBlockHeader(blockchain.getChainHeadHeader()),
TestClock.fixed(),
new NoOpMetricsSystem(),
EthProtocolConfiguration.DEFAULT_MAX_MESSAGE_SIZE,
Collections.emptyList(),
Bytes.random(64),
25,
25,
false,
SyncMode.FAST,
new ForkIdManager(
blockchain, Collections.emptyList(), Collections.emptyList(), false));
ethPeers.setChainHeadTracker(EthProtocolManagerTestUtil.getChainHeadTrackerMock());
}
if (ethMessages == null) {
ethMessages = new EthMessages();
}
if (snapMessages == null) {
snapMessages = new EthMessages();
}
if (ethScheduler == null) {
ethScheduler =
new DeterministicEthScheduler(DeterministicEthScheduler.TimeoutPolicy.NEVER_TIMEOUT);
}
if (ethContext == null) {
ethContext = new EthContext(ethPeers, ethMessages, snapMessages, ethScheduler);
}
if (peerValidators == null) {
peerValidators = Collections.emptyList();
}
if (mergePeerFilter == null) {
mergePeerFilter = Optional.of(new MergePeerFilter());
}
if (synchronizerConfiguration == null) {
synchronizerConfiguration = SynchronizerConfiguration.builder().build();
}
return new EthProtocolManager(
blockchain,
networkId,
worldStateArchive,
transactionPool,
ethereumWireProtocolConfiguration,
ethPeers,
ethMessages,
ethContext,
peerValidators,
mergePeerFilter,
synchronizerConfiguration,
ethScheduler);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,19 +15,15 @@
package org.hyperledger.besu.ethereum.eth.manager;

import static com.google.common.base.Preconditions.checkArgument;
import static org.hyperledger.besu.ethereum.core.InMemoryKeyValueStorageProvider.createInMemoryBlockchain;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.mock;

import org.hyperledger.besu.config.GenesisConfigFile;
import org.hyperledger.besu.datatypes.Hash;
import org.hyperledger.besu.ethereum.chain.Blockchain;
import org.hyperledger.besu.ethereum.chain.ChainHead;
import org.hyperledger.besu.ethereum.chain.GenesisState;
import org.hyperledger.besu.ethereum.core.BlockHeader;
import org.hyperledger.besu.ethereum.core.BlockchainSetupUtil;
import org.hyperledger.besu.ethereum.core.Difficulty;
import org.hyperledger.besu.ethereum.core.ProtocolScheduleFixture;
import org.hyperledger.besu.ethereum.eth.EthProtocol;
import org.hyperledger.besu.ethereum.eth.EthProtocolConfiguration;
import org.hyperledger.besu.ethereum.eth.manager.snap.SnapProtocolManager;
Expand Down Expand Up @@ -170,13 +166,6 @@ public static EthProtocolManager create(
forkIdManager);
}

public static EthProtocolManager create(final Blockchain blockchain) {
return create(
ProtocolScheduleFixture.MAINNET,
blockchain,
new DeterministicEthScheduler(TimeoutPolicy.NEVER_TIMEOUT));
}

public static EthProtocolManager create(
final ProtocolSchedule protocolSchedule,
final Blockchain blockchain,
Expand All @@ -192,14 +181,6 @@ public static EthProtocolManager create(
ethProtocolConfiguration);
}

public static EthProtocolManager create(final EthScheduler ethScheduler) {
final ProtocolSchedule protocolSchedule = ProtocolScheduleFixture.MAINNET;
final GenesisConfigFile config = GenesisConfigFile.mainnet();
final GenesisState genesisState = GenesisState.fromConfig(config, protocolSchedule);
final Blockchain blockchain = createInMemoryBlockchain(genesisState.getBlock());
return create(protocolSchedule, blockchain, ethScheduler);
}

public static EthProtocolManager create(
final ProtocolSchedule protocolSchedule,
final Blockchain blockchain,
Expand Down Expand Up @@ -319,14 +300,6 @@ public static EthProtocolManager create(
new EthContext(ethPeers, messages, ethScheduler));
}

public static EthProtocolManager create() {
return create(TimeoutPolicy.NEVER_TIMEOUT);
}

public static EthProtocolManager create(final TimeoutPolicy timeoutPolicy) {
return create(new DeterministicEthScheduler(timeoutPolicy));
}

// Utility to prevent scheduler from automatically running submitted tasks
public static void disableEthSchedulerAutoRun(final EthProtocolManager ethProtocolManager) {
final EthScheduler scheduler = ethProtocolManager.ethContext().getScheduler();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

import org.hyperledger.besu.ethereum.eth.manager.EthContext;
import org.hyperledger.besu.ethereum.eth.manager.EthProtocolManager;
import org.hyperledger.besu.ethereum.eth.manager.EthProtocolManagerTestBuilder;
import org.hyperledger.besu.ethereum.eth.manager.EthProtocolManagerTestUtil;
import org.hyperledger.besu.metrics.noop.NoOpMetricsSystem;
import org.hyperledger.besu.plugin.services.MetricsSystem;
Expand All @@ -36,7 +37,7 @@ public class WaitForPeerTaskTest {

@BeforeEach
public void setupTest() {
ethProtocolManager = EthProtocolManagerTestUtil.create();
ethProtocolManager = EthProtocolManagerTestBuilder.builder().build();
ethContext = ethProtocolManager.ethContext();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

import org.hyperledger.besu.ethereum.eth.manager.EthContext;
import org.hyperledger.besu.ethereum.eth.manager.EthProtocolManager;
import org.hyperledger.besu.ethereum.eth.manager.EthProtocolManagerTestBuilder;
import org.hyperledger.besu.ethereum.eth.manager.EthProtocolManagerTestUtil;
import org.hyperledger.besu.metrics.noop.NoOpMetricsSystem;
import org.hyperledger.besu.plugin.services.MetricsSystem;
Expand All @@ -36,7 +37,7 @@ public class WaitForPeersTaskTest {

@BeforeEach
public void setupTest() {
ethProtocolManager = EthProtocolManagerTestUtil.create();
ethProtocolManager = EthProtocolManagerTestBuilder.builder().build();
ethContext = ethProtocolManager.ethContext();
}

Expand Down
Loading
Loading