Skip to content

Commit

Permalink
Added parameterized tests to test Bonsai And Forest (hyperledger#2131)
Browse files Browse the repository at this point in the history
Signed-off-by: Karim TAAM <karim.t2am@gmail.com>
  • Loading branch information
matkt authored Apr 9, 2021
1 parent e82fd74 commit 16c5251
Show file tree
Hide file tree
Showing 14 changed files with 414 additions and 121 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,10 @@ public Optional<Bytes> getNodeData(final Hash hash) {
return Optional.empty();
}

public BonsaiWorldStateKeyValueStorage getWorldStateStorage() {
return worldStateStorage;
}

@Override
public Optional<WorldStateProof> getAccountProof(
final Hash worldStateRoot,
Expand Down

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
/*
* 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.sync;

import static org.mockito.Mockito.mock;

import org.hyperledger.besu.ethereum.ProtocolContext;
import org.hyperledger.besu.ethereum.chain.Blockchain;
import org.hyperledger.besu.ethereum.core.BlockchainSetupUtil;
import org.hyperledger.besu.ethereum.eth.EthProtocolConfiguration;
import org.hyperledger.besu.ethereum.eth.manager.EthProtocolManagerTestUtil;
import org.hyperledger.besu.ethereum.eth.sync.state.SyncState;
import org.hyperledger.besu.ethereum.worldstate.DataStorageFormat;

import org.junit.Before;
import org.junit.BeforeClass;

public class BonsaiBlockPropagationManagerTest extends AbstractBlockPropagationManagerTest {

private static Blockchain fullBlockchain;

@BeforeClass
public static void setupSuite() {
fullBlockchain = BlockchainSetupUtil.forTesting(DataStorageFormat.BONSAI).importAllBlocks();
}

@Before
public void setup() {
blockchainUtil = BlockchainSetupUtil.forTesting(DataStorageFormat.BONSAI);
blockchain = blockchainUtil.getBlockchain();
protocolSchedule = blockchainUtil.getProtocolSchedule();
final ProtocolContext tempProtocolContext = blockchainUtil.getProtocolContext();
protocolContext =
new ProtocolContext(
blockchain,
tempProtocolContext.getWorldStateArchive(),
tempProtocolContext.getConsensusState(Object.class));
ethProtocolManager =
EthProtocolManagerTestUtil.create(
blockchain,
blockchainUtil.getWorldArchive(),
blockchainUtil.getTransactionPool(),
EthProtocolConfiguration.defaultConfig());
syncConfig = SynchronizerConfiguration.builder().blockPropagationRange(-3, 5).build();
syncState = new SyncState(blockchain, ethProtocolManager.ethContext().getEthPeers());
blockBroadcaster = mock(BlockBroadcaster.class);
blockPropagationManager =
new BlockPropagationManager(
syncConfig,
protocolSchedule,
protocolContext,
ethProtocolManager.ethContext(),
syncState,
pendingBlocksManager,
metricsSystem,
blockBroadcaster);
}

@Override
public Blockchain getFullBlockchain() {
return fullBlockchain;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,34 +31,60 @@
import org.hyperledger.besu.ethereum.worldstate.DataStorageFormat;
import org.hyperledger.besu.metrics.noop.NoOpMetricsSystem;

import java.util.Arrays;
import java.util.Collection;

import org.assertj.core.api.Assertions;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;

@RunWith(Parameterized.class)
public class ChainHeadTrackerTest {

private final BlockchainSetupUtil blockchainSetupUtil =
BlockchainSetupUtil.forTesting(DataStorageFormat.FOREST);
private final MutableBlockchain blockchain = blockchainSetupUtil.getBlockchain();
private final EthProtocolManager ethProtocolManager =
EthProtocolManagerTestUtil.create(blockchain);
private final RespondingEthPeer respondingPeer =
RespondingEthPeer.builder()
.ethProtocolManager(ethProtocolManager)
.chainHeadHash(blockchain.getChainHeadHash())
.totalDifficulty(blockchain.getChainHead().getTotalDifficulty())
.estimatedHeight(0)
.build();
private BlockchainSetupUtil blockchainSetupUtil;
private MutableBlockchain blockchain;
private EthProtocolManager ethProtocolManager;
private RespondingEthPeer respondingPeer;
private ChainHeadTracker chainHeadTracker;

private final ProtocolSchedule protocolSchedule =
FixedDifficultyProtocolSchedule.create(
GenesisConfigFile.development().getConfigOptions(), false);

private final TrailingPeerLimiter trailingPeerLimiter = mock(TrailingPeerLimiter.class);
private final ChainHeadTracker chainHeadTracker =
new ChainHeadTracker(
ethProtocolManager.ethContext(),
protocolSchedule,
trailingPeerLimiter,
new NoOpMetricsSystem());

@Parameterized.Parameters
public static Collection<Object[]> data() {
return Arrays.asList(new Object[][] {{DataStorageFormat.BONSAI}, {DataStorageFormat.FOREST}});
}

private final DataStorageFormat storageFormat;

public ChainHeadTrackerTest(final DataStorageFormat storageFormat) {
this.storageFormat = storageFormat;
}

@Before
public void setup() {
blockchainSetupUtil = BlockchainSetupUtil.forTesting(storageFormat);
blockchain = blockchainSetupUtil.getBlockchain();
ethProtocolManager = EthProtocolManagerTestUtil.create(blockchain);
respondingPeer =
RespondingEthPeer.builder()
.ethProtocolManager(ethProtocolManager)
.chainHeadHash(blockchain.getChainHeadHash())
.totalDifficulty(blockchain.getChainHead().getTotalDifficulty())
.estimatedHeight(0)
.build();
chainHeadTracker =
new ChainHeadTracker(
ethProtocolManager.ethContext(),
protocolSchedule,
trailingPeerLimiter,
new NoOpMetricsSystem());
}

@Test
public void shouldRequestHeaderChainHeadWhenNewPeerConnects() {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
/*
* 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.sync;

import static org.mockito.Mockito.mock;

import org.hyperledger.besu.ethereum.ProtocolContext;
import org.hyperledger.besu.ethereum.chain.Blockchain;
import org.hyperledger.besu.ethereum.core.BlockchainSetupUtil;
import org.hyperledger.besu.ethereum.eth.EthProtocolConfiguration;
import org.hyperledger.besu.ethereum.eth.manager.EthProtocolManagerTestUtil;
import org.hyperledger.besu.ethereum.eth.sync.state.SyncState;
import org.hyperledger.besu.ethereum.worldstate.DataStorageFormat;

import org.junit.Before;
import org.junit.BeforeClass;

public class ForestBlockPropagationManagerTest extends AbstractBlockPropagationManagerTest {

private static Blockchain fullBlockchain;

@BeforeClass
public static void setupSuite() {
fullBlockchain = BlockchainSetupUtil.forTesting(DataStorageFormat.FOREST).importAllBlocks();
}

@Before
public void setup() {
blockchainUtil = BlockchainSetupUtil.forTesting(DataStorageFormat.FOREST);
blockchain = blockchainUtil.getBlockchain();
protocolSchedule = blockchainUtil.getProtocolSchedule();
final ProtocolContext tempProtocolContext = blockchainUtil.getProtocolContext();
protocolContext =
new ProtocolContext(
blockchain,
tempProtocolContext.getWorldStateArchive(),
tempProtocolContext.getConsensusState(Object.class));
ethProtocolManager =
EthProtocolManagerTestUtil.create(
blockchain,
blockchainUtil.getWorldArchive(),
blockchainUtil.getTransactionPool(),
EthProtocolConfiguration.defaultConfig());
syncConfig = SynchronizerConfiguration.builder().blockPropagationRange(-3, 5).build();
syncState = new SyncState(blockchain, ethProtocolManager.ethContext().getEthPeers());
blockBroadcaster = mock(BlockBroadcaster.class);
blockPropagationManager =
new BlockPropagationManager(
syncConfig,
protocolSchedule,
protocolContext,
ethProtocolManager.ethContext(),
syncState,
pendingBlocksManager,
metricsSystem,
blockBroadcaster);
}

@Override
public Blockchain getFullBlockchain() {
return fullBlockchain;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -41,18 +41,21 @@
import org.hyperledger.besu.metrics.noop.NoOpMetricsSystem;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import java.util.OptionalLong;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.atomic.AtomicInteger;

import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;

@RunWith(Parameterized.class)
public class FastSyncActionsTest {

private final BlockchainSetupUtil blockchainSetupUtil =
BlockchainSetupUtil.forTesting(DataStorageFormat.FOREST);
private final SynchronizerConfiguration.Builder syncConfigBuilder =
new SynchronizerConfiguration.Builder().syncMode(SyncMode.FAST).fastSyncPivotDistance(1000);

Expand All @@ -62,9 +65,22 @@ public class FastSyncActionsTest {
private FastSyncActions fastSyncActions;
private EthProtocolManager ethProtocolManager;
private MutableBlockchain blockchain;
private BlockchainSetupUtil blockchainSetupUtil;

@Parameterized.Parameters
public static Collection<Object[]> data() {
return Arrays.asList(new Object[][] {{DataStorageFormat.BONSAI}, {DataStorageFormat.FOREST}});
}

private final DataStorageFormat storageFormat;

public FastSyncActionsTest(final DataStorageFormat storageFormat) {
this.storageFormat = storageFormat;
}

@Before
public void setUp() {
blockchainSetupUtil = BlockchainSetupUtil.forTesting(storageFormat);
blockchainSetupUtil.importAllBlocks();
blockchain = blockchainSetupUtil.getBlockchain();
ethProtocolManager =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,19 @@
import org.hyperledger.besu.ethereum.worldstate.DataStorageFormat;
import org.hyperledger.besu.metrics.noop.NoOpMetricsSystem;

import java.util.Arrays;
import java.util.Collection;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.locks.LockSupport;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import org.junit.runners.Parameterized.Parameters;

@RunWith(Parameterized.class)
public class FastSyncChainDownloaderTest {

private final FastSyncValidationPolicy validationPolicy = mock(FastSyncValidationPolicy.class);
Expand All @@ -59,13 +65,23 @@ public class FastSyncChainDownloaderTest {
private BlockchainSetupUtil otherBlockchainSetup;
protected Blockchain otherBlockchain;

@Parameters
public static Collection<Object[]> data() {
return Arrays.asList(new Object[][] {{DataStorageFormat.BONSAI}, {DataStorageFormat.FOREST}});
}

private final DataStorageFormat storageFormat;

public FastSyncChainDownloaderTest(final DataStorageFormat storageFormat) {
this.storageFormat = storageFormat;
}

@Before
public void setup() {
when(validationPolicy.getValidationModeForNextBlock()).thenReturn(LIGHT_SKIP_DETACHED);
final BlockchainSetupUtil localBlockchainSetup =
BlockchainSetupUtil.forTesting(DataStorageFormat.FOREST);
final BlockchainSetupUtil localBlockchainSetup = BlockchainSetupUtil.forTesting(storageFormat);
localBlockchain = localBlockchainSetup.getBlockchain();
otherBlockchainSetup = BlockchainSetupUtil.forTesting(DataStorageFormat.FOREST);
otherBlockchainSetup = BlockchainSetupUtil.forTesting(storageFormat);
otherBlockchain = otherBlockchainSetup.getBlockchain();

protocolSchedule = localBlockchainSetup.getProtocolSchedule();
Expand Down Expand Up @@ -145,8 +161,7 @@ public void shouldSyncToPivotBlockInSingleSegment() {

@Test
public void recoversFromSyncTargetDisconnect() {
final BlockchainSetupUtil shorterChainUtil =
BlockchainSetupUtil.forTesting(DataStorageFormat.FOREST);
final BlockchainSetupUtil shorterChainUtil = BlockchainSetupUtil.forTesting(storageFormat);
final MutableBlockchain shorterChain = shorterChainUtil.getBlockchain();

otherBlockchainSetup.importFirstBlocks(30);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,14 +36,20 @@
import org.hyperledger.besu.metrics.noop.NoOpMetricsSystem;
import org.hyperledger.besu.plugin.services.MetricsSystem;

import java.util.Arrays;
import java.util.Collection;
import java.util.Optional;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.atomic.AtomicBoolean;

import org.apache.tuweni.bytes.Bytes;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import org.junit.runners.Parameterized.Parameters;

@RunWith(Parameterized.class)
public class PivotBlockConfirmerTest {

private static final long PIVOT_BLOCK_NUMBER = 10;
Expand All @@ -58,10 +64,20 @@ public class PivotBlockConfirmerTest {
private PivotBlockConfirmer pivotBlockConfirmer;
private ProtocolSchedule protocolSchedule;

@Parameters
public static Collection<Object[]> data() {
return Arrays.asList(new Object[][] {{DataStorageFormat.BONSAI}, {DataStorageFormat.FOREST}});
}

private final DataStorageFormat storageFormat;

public PivotBlockConfirmerTest(final DataStorageFormat storageFormat) {
this.storageFormat = storageFormat;
}

@Before
public void setUp() {
final BlockchainSetupUtil blockchainSetupUtil =
BlockchainSetupUtil.forTesting(DataStorageFormat.FOREST);
final BlockchainSetupUtil blockchainSetupUtil = BlockchainSetupUtil.forTesting(storageFormat);
blockchainSetupUtil.importAllBlocks();
blockchain = blockchainSetupUtil.getBlockchain();
transactionPool = blockchainSetupUtil.getTransactionPool();
Expand Down
Loading

0 comments on commit 16c5251

Please sign in to comment.