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

Qbft migration protocol schedule #3069

Merged
merged 22 commits into from
Nov 22, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
7b3efcc
Initial implementation of SwitchableBesuControllerBuilder
lucassaldanha Nov 12, 2021
843759c
besu-2997 Create a Switchable BesuControllerBuilder without migration…
lucassaldanha Nov 12, 2021
9f6d6d6
Rename
lucassaldanha Nov 12, 2021
b08e816
Cleanup
lucassaldanha Nov 15, 2021
0cc10e2
Some refactoring...
lucassaldanha Nov 15, 2021
873a514
Refactoring
lucassaldanha Nov 15, 2021
64539e5
QBFT migration combined protocol schedule
jframe Nov 16, 2021
f26ef7b
Merge remote-tracking branch 'upstream/main' into qbft_migration_prot…
jframe Nov 17, 2021
634e22f
Only add milestones applicable to current consensus mechanism
jframe Nov 17, 2021
d968a3f
address review comments
jframe Nov 17, 2021
14d31c3
cleanup
jframe Nov 17, 2021
b71b682
spotless
jframe Nov 17, 2021
04f4657
cleanup
jframe Nov 17, 2021
3677270
move the combined protocol schedule into its own class
jframe Nov 19, 2021
d1dd813
test that besu controller builder creates set ordered by block and ca…
jframe Nov 19, 2021
bd67f45
fix errorprone warning
jframe Nov 19, 2021
90b88eb
Merge remote-tracking branch 'upstream/main' into qbft_migration_prot…
jframe Nov 19, 2021
653f240
Fix build after merge
jframe Nov 19, 2021
6605a09
Merge branch 'main' into qbft_migration_protocol_schedule
jframe Nov 22, 2021
d23f378
Merge remote-tracking branch 'upstream/main' into qbft_migration_prot…
jframe Nov 22, 2021
6417e23
call prepForBuild for delegate controller builders as well
jframe Nov 22, 2021
d5121da
Merge branch 'qbft_migration_protocol_schedule' of github.com:jframe/…
jframe Nov 22, 2021
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
Prev Previous commit
Next Next commit
QBFT migration combined protocol schedule
Signed-off-by: Jason Frame <jasonwframe@gmail.com>
  • Loading branch information
jframe committed Nov 16, 2021
commit 64539e5d422b9884c4c186436b9bb4bef1bb80c3
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,9 @@
import org.hyperledger.besu.ethereum.eth.sync.state.SyncState;
import org.hyperledger.besu.ethereum.eth.transactions.TransactionPool;
import org.hyperledger.besu.ethereum.eth.transactions.TransactionPoolConfiguration;
import org.hyperledger.besu.ethereum.mainnet.MutableProtocolSchedule;
import org.hyperledger.besu.ethereum.mainnet.ProtocolSchedule;
import org.hyperledger.besu.ethereum.mainnet.ScheduledProtocolSpec;
import org.hyperledger.besu.ethereum.p2p.config.SubProtocolConfiguration;
import org.hyperledger.besu.ethereum.storage.StorageProvider;
import org.hyperledger.besu.ethereum.worldstate.DataStorageConfiguration;
Expand All @@ -57,6 +59,7 @@
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Optional;

import com.google.common.base.Preconditions;
Expand Down Expand Up @@ -102,7 +105,21 @@ protected MiningCoordinator createMiningCoordinator(

@Override
protected ProtocolSchedule createProtocolSchedule() {
return besuControllerBuilders.get(0L).createProtocolSchedule();
final MutableProtocolSchedule combinedProtocolSchedule =
new MutableProtocolSchedule(genesisConfig.getConfigOptions().getChainId());

for (Entry<Long, BesuControllerBuilder> builderSchedule : besuControllerBuilders.entrySet()) {
jframe marked this conversation as resolved.
Show resolved Hide resolved
final BesuControllerBuilder builder = builderSchedule.getValue();
final long block = builderSchedule.getKey();
final MutableProtocolSchedule protocolSchedule =
(MutableProtocolSchedule) builder.createProtocolSchedule();
for (ScheduledProtocolSpec scheduledProtocolSpec :
protocolSchedule.getScheduledProtocolSpecs()) {
final long milestoneBlock = block + scheduledProtocolSpec.getBlock();
combinedProtocolSchedule.putMilestone(milestoneBlock, scheduledProtocolSpec.getSpec());
}
}
return combinedProtocolSchedule;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,149 @@
/*
* Copyright Hyperledger Besu Contributors.
*
* 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.controller;

import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.when;

import org.hyperledger.besu.config.GenesisConfigFile;
import org.hyperledger.besu.config.GenesisConfigOptions;
import org.hyperledger.besu.config.StubGenesisConfigOptions;
import org.hyperledger.besu.ethereum.core.PrivacyParameters;
import org.hyperledger.besu.ethereum.mainnet.ProtocolSchedule;
import org.hyperledger.besu.ethereum.mainnet.ProtocolScheduleBuilder;
import org.hyperledger.besu.ethereum.mainnet.ProtocolSpecAdapters;
import org.hyperledger.besu.evm.internal.EvmConfiguration;

import java.math.BigInteger;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.junit.MockitoJUnitRunner;

@RunWith(MockitoJUnitRunner.class)
public class ConsensusScheduleBesuControllerBuilderTest {
@Mock private GenesisConfigFile genesisConfigFile;
@Mock private BesuControllerBuilder delegateBesuControllerBuilder1;
@Mock private BesuControllerBuilder delegateBesuControllerBuilder2;

@Test
public void createsCombinedProtocolScheduleWithMilestonesFromSingleProtocolSchedule() {
final StubGenesisConfigOptions genesisConfigOptions = new StubGenesisConfigOptions();
genesisConfigOptions.homesteadBlock(5L);
genesisConfigOptions.constantinopleBlock(10L);
final ProtocolSchedule protocolSchedule = createProtocolSchedule(genesisConfigOptions);

final Map<Long, BesuControllerBuilder> schedule = new HashMap<>();
jframe marked this conversation as resolved.
Show resolved Hide resolved
schedule.put(0L, delegateBesuControllerBuilder1);
when(delegateBesuControllerBuilder1.createProtocolSchedule()).thenReturn(protocolSchedule);

final StubGenesisConfigOptions genesisConfigOptionsForChainId = new StubGenesisConfigOptions();
genesisConfigOptionsForChainId.chainId(BigInteger.TEN);
when(genesisConfigFile.getConfigOptions()).thenReturn(genesisConfigOptionsForChainId);

final ConsensusScheduleBesuControllerBuilder controllerBuilder =
new ConsensusScheduleBesuControllerBuilder(schedule);
controllerBuilder.genesisConfigFile(genesisConfigFile);
final ProtocolSchedule combinedProtocolSchedule = controllerBuilder.createProtocolSchedule();
assertThat(combinedProtocolSchedule.getByBlockNumber(0L))
.usingRecursiveComparison()
.isEqualTo(protocolSchedule.getByBlockNumber(0L));
assertThat(combinedProtocolSchedule.getByBlockNumber(5L))
.usingRecursiveComparison()
.isEqualTo(protocolSchedule.getByBlockNumber(5L));
assertThat(combinedProtocolSchedule.getByBlockNumber(10L))
.usingRecursiveComparison()
.isEqualTo(protocolSchedule.getByBlockNumber(10L));
assertThat(combinedProtocolSchedule.streamMilestoneBlocks().collect(Collectors.toList()))
.isEqualTo(List.of(0L, 5L, 10L));
}

@Test
public void createsCombinedProtocolScheduleWithMilestonesFromMultipleSchedules() {
final StubGenesisConfigOptions genesisConfigOptions1 = new StubGenesisConfigOptions();
genesisConfigOptions1.homesteadBlock(5L);
genesisConfigOptions1.constantinopleBlock(10L);
genesisConfigOptions1.chainId(BigInteger.TEN);
final ProtocolSchedule protocolSchedule1 = createProtocolSchedule(genesisConfigOptions1);

final StubGenesisConfigOptions genesisConfigOptions2 = new StubGenesisConfigOptions();
genesisConfigOptions2.byzantiumBlock(0L);
genesisConfigOptions2.berlinBlock(5L);
final ProtocolSchedule protocolSchedule2 = createProtocolSchedule(genesisConfigOptions2);

final Map<Long, BesuControllerBuilder> schedule = new HashMap<>();
schedule.put(0L, delegateBesuControllerBuilder1);
schedule.put(100L, delegateBesuControllerBuilder2);
when(delegateBesuControllerBuilder2.createProtocolSchedule()).thenReturn(protocolSchedule2);
when(delegateBesuControllerBuilder1.createProtocolSchedule()).thenReturn(protocolSchedule1);

final StubGenesisConfigOptions genesisConfigOptionsForChainId = new StubGenesisConfigOptions();
genesisConfigOptionsForChainId.chainId(BigInteger.TEN);
when(genesisConfigFile.getConfigOptions()).thenReturn(genesisConfigOptionsForChainId);

final ConsensusScheduleBesuControllerBuilder controllerBuilder =
new ConsensusScheduleBesuControllerBuilder(schedule);
controllerBuilder.genesisConfigFile(genesisConfigFile);
final ProtocolSchedule combinedProtocolSchedule = controllerBuilder.createProtocolSchedule();

// schedule 1
assertThat(combinedProtocolSchedule.getByBlockNumber(0L).getName()).isEqualTo("Frontier");
assertThat(combinedProtocolSchedule.getByBlockNumber(0L))
.usingRecursiveComparison()
.isEqualTo(protocolSchedule1.getByBlockNumber(0L));
assertThat(combinedProtocolSchedule.getByBlockNumber(5L).getName()).isEqualTo("Homestead");
assertThat(combinedProtocolSchedule.getByBlockNumber(5L))
.usingRecursiveComparison()
.isEqualTo(protocolSchedule1.getByBlockNumber(5L));
assertThat(combinedProtocolSchedule.getByBlockNumber(10L).getName())
.isEqualTo("Constantinople");
assertThat(combinedProtocolSchedule.getByBlockNumber(10L))
.usingRecursiveComparison()
.isEqualTo(protocolSchedule1.getByBlockNumber(10L));

// schedule 2
assertThat(combinedProtocolSchedule.getByBlockNumber(100L).getName()).isEqualTo("Byzantium");
assertThat(combinedProtocolSchedule.getByBlockNumber(100L))
.usingRecursiveComparison()
.isEqualTo(protocolSchedule2.getByBlockNumber(0L));
assertThat(combinedProtocolSchedule.getByBlockNumber(105L).getName()).isEqualTo("Berlin");
assertThat(combinedProtocolSchedule.getByBlockNumber(105L))
.usingRecursiveComparison()
.isEqualTo(protocolSchedule2.getByBlockNumber(5L));

assertThat(combinedProtocolSchedule.streamMilestoneBlocks().collect(Collectors.toList()))
.isEqualTo(List.of(0L, 5L, 10L, 100L, 105L));
}

private ProtocolSchedule createProtocolSchedule(final GenesisConfigOptions genesisConfigOptions) {
final ProtocolScheduleBuilder protocolScheduleBuilder =
new ProtocolScheduleBuilder(
genesisConfigOptions,
BigInteger.ONE,
ProtocolSpecAdapters.create(0, Function.identity()),
new PrivacyParameters(),
false,
false,
EvmConfiguration.DEFAULT);

return protocolScheduleBuilder.createProtocolSchedule();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

import java.math.BigInteger;
import java.util.Comparator;
import java.util.List;
import java.util.NavigableSet;
import java.util.Optional;
import java.util.TreeSet;
Expand Down Expand Up @@ -100,4 +101,8 @@ public void setPublicWorldStateArchiveForPrivacyBlockProcessor(
.setPublicWorldStateArchive(publicWorldStateArchive);
});
}

public List<ScheduledProtocolSpec> getScheduledProtocolSpecs() {
return protocolSpecs.stream().collect(Collectors.toUnmodifiableList());
}
}