forked from hyperledger/besu
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Qbft migration protocol schedule (hyperledger#3069)
Signed-off-by: Jason Frame <jasonwframe@gmail.com>
- Loading branch information
Showing
6 changed files
with
312 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
62 changes: 62 additions & 0 deletions
62
.../src/main/java/org/hyperledger/besu/consensus/common/CombinedProtocolScheduleFactory.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
/* | ||
* 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.consensus.common; | ||
|
||
import static com.google.common.base.Preconditions.checkState; | ||
|
||
import org.hyperledger.besu.ethereum.mainnet.MutableProtocolSchedule; | ||
import org.hyperledger.besu.ethereum.mainnet.ProtocolSchedule; | ||
import org.hyperledger.besu.ethereum.mainnet.ScheduledProtocolSpec; | ||
|
||
import java.math.BigInteger; | ||
import java.util.NavigableSet; | ||
import java.util.Optional; | ||
import java.util.function.Predicate; | ||
|
||
public class CombinedProtocolScheduleFactory { | ||
|
||
public ProtocolSchedule create( | ||
final NavigableSet<ForkSpec<ProtocolSchedule>> forkSpecs, | ||
final Optional<BigInteger> chainId) { | ||
final MutableProtocolSchedule combinedProtocolSchedule = new MutableProtocolSchedule(chainId); | ||
for (ForkSpec<ProtocolSchedule> spec : forkSpecs) { | ||
checkState( | ||
spec.getValue() instanceof MutableProtocolSchedule, | ||
"Consensus migration requires a MutableProtocolSchedule"); | ||
final MutableProtocolSchedule protocolSchedule = (MutableProtocolSchedule) spec.getValue(); | ||
|
||
final Optional<Long> endBlock = | ||
Optional.ofNullable(forkSpecs.higher(spec)).map(ForkSpec::getBlock); | ||
protocolSchedule.getScheduledProtocolSpecs().stream() | ||
.filter(protocolSpecMatchesConsensusBlockRange(spec.getBlock(), endBlock)) | ||
.forEach(s -> combinedProtocolSchedule.putMilestone(s.getBlock(), s.getSpec())); | ||
|
||
// When moving to a new consensus mechanism we want to use the last milestone but created by | ||
// our consensus mechanism's BesuControllerBuilder so any additional rules are applied | ||
if (spec.getBlock() > 0) { | ||
combinedProtocolSchedule.putMilestone( | ||
spec.getBlock(), protocolSchedule.getByBlockNumber(spec.getBlock())); | ||
} | ||
} | ||
return combinedProtocolSchedule; | ||
} | ||
|
||
private Predicate<ScheduledProtocolSpec> protocolSpecMatchesConsensusBlockRange( | ||
final long startBlock, final Optional<Long> endBlock) { | ||
return scheduledProtocolSpec -> | ||
scheduledProtocolSpec.getBlock() >= startBlock | ||
&& endBlock.map(b -> scheduledProtocolSpec.getBlock() < b).orElse(true); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
154 changes: 154 additions & 0 deletions
154
.../test/java/org/hyperledger/besu/consensus/common/CombinedProtocolScheduleFactoryTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,154 @@ | ||
/* | ||
* 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.consensus.common; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
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.List; | ||
import java.util.NavigableSet; | ||
import java.util.Optional; | ||
import java.util.TreeSet; | ||
import java.util.function.Function; | ||
import java.util.stream.Collectors; | ||
|
||
import org.junit.Test; | ||
import org.junit.runner.RunWith; | ||
import org.mockito.junit.MockitoJUnitRunner; | ||
|
||
@RunWith(MockitoJUnitRunner.class) | ||
public class CombinedProtocolScheduleFactoryTest { | ||
|
||
private final CombinedProtocolScheduleFactory combinedProtocolScheduleFactory = | ||
new CombinedProtocolScheduleFactory(); | ||
|
||
@Test | ||
public void createsCombinedProtocolScheduleWithMilestonesFromSingleProtocolSchedule() { | ||
final StubGenesisConfigOptions genesisConfigOptions = new StubGenesisConfigOptions(); | ||
genesisConfigOptions.homesteadBlock(5L); | ||
genesisConfigOptions.constantinopleBlock(10L); | ||
genesisConfigOptions.chainId(BigInteger.TEN); | ||
final ProtocolSchedule protocolSchedule = createProtocolSchedule(genesisConfigOptions); | ||
|
||
final NavigableSet<ForkSpec<ProtocolSchedule>> consensusSchedule = | ||
new TreeSet<>(ForkSpec.COMPARATOR); | ||
consensusSchedule.add(new ForkSpec<>(0, protocolSchedule)); | ||
|
||
final ProtocolSchedule combinedProtocolSchedule = | ||
combinedProtocolScheduleFactory.create(consensusSchedule, Optional.of(BigInteger.TEN)); | ||
|
||
assertThat(combinedProtocolSchedule.getByBlockNumber(0L).getName()).isEqualTo("Frontier"); | ||
assertThat(combinedProtocolSchedule.getByBlockNumber(0L)) | ||
.isSameAs(protocolSchedule.getByBlockNumber(0L)); | ||
|
||
assertThat(combinedProtocolSchedule.getByBlockNumber(5L).getName()).isEqualTo("Homestead"); | ||
assertThat(combinedProtocolSchedule.getByBlockNumber(5L)) | ||
.isSameAs(protocolSchedule.getByBlockNumber(5L)); | ||
|
||
assertThat(combinedProtocolSchedule.getByBlockNumber(10L).getName()) | ||
.isEqualTo("Constantinople"); | ||
assertThat(combinedProtocolSchedule.getByBlockNumber(10L)) | ||
.isSameAs(protocolSchedule.getByBlockNumber(10L)); | ||
|
||
assertThat(combinedProtocolSchedule.streamMilestoneBlocks().collect(Collectors.toList())) | ||
.isEqualTo(List.of(0L, 5L, 10L)); | ||
} | ||
|
||
@Test | ||
public void createsCombinedProtocolScheduleWithMilestonesFromMultipleSchedules() { | ||
final StubGenesisConfigOptions genesisConfigOptions = new StubGenesisConfigOptions(); | ||
genesisConfigOptions.homesteadBlock(5L); | ||
genesisConfigOptions.constantinopleBlock(10L); | ||
genesisConfigOptions.byzantiumBlock(105L); | ||
genesisConfigOptions.berlinBlock(110L); | ||
genesisConfigOptions.londonBlock(220L); | ||
genesisConfigOptions.chainId(BigInteger.TEN); | ||
|
||
final ProtocolSchedule protocolSchedule1 = createProtocolSchedule(genesisConfigOptions); | ||
final ProtocolSchedule protocolSchedule2 = createProtocolSchedule(genesisConfigOptions); | ||
final ProtocolSchedule protocolSchedule3 = createProtocolSchedule(genesisConfigOptions); | ||
|
||
final NavigableSet<ForkSpec<ProtocolSchedule>> consensusSchedule = | ||
new TreeSet<>(ForkSpec.COMPARATOR); | ||
consensusSchedule.add(new ForkSpec<>(0, protocolSchedule1)); | ||
consensusSchedule.add(new ForkSpec<>(100L, protocolSchedule2)); | ||
consensusSchedule.add(new ForkSpec<>(200L, protocolSchedule3)); | ||
|
||
final ProtocolSchedule combinedProtocolSchedule = | ||
combinedProtocolScheduleFactory.create(consensusSchedule, Optional.of(BigInteger.TEN)); | ||
|
||
// consensus schedule 1 | ||
assertThat(combinedProtocolSchedule.getByBlockNumber(0L).getName()).isEqualTo("Frontier"); | ||
assertThat(combinedProtocolSchedule.getByBlockNumber(0L)) | ||
.isSameAs(protocolSchedule1.getByBlockNumber(0L)); | ||
|
||
assertThat(combinedProtocolSchedule.getByBlockNumber(5L).getName()).isEqualTo("Homestead"); | ||
assertThat(combinedProtocolSchedule.getByBlockNumber(5L)) | ||
.isSameAs(protocolSchedule1.getByBlockNumber(5L)); | ||
assertThat(combinedProtocolSchedule.getByBlockNumber(10L).getName()) | ||
.isEqualTo("Constantinople"); | ||
assertThat(combinedProtocolSchedule.getByBlockNumber(10L)) | ||
.isSameAs(protocolSchedule1.getByBlockNumber(10L)); | ||
|
||
// consensus schedule 2 migration block | ||
assertThat(combinedProtocolSchedule.getByBlockNumber(100L).getName()) | ||
.isEqualTo("Constantinople"); | ||
assertThat(combinedProtocolSchedule.getByBlockNumber(100L)) | ||
.isSameAs(protocolSchedule2.getByBlockNumber(10L)); | ||
|
||
// consensus schedule 2 | ||
assertThat(combinedProtocolSchedule.getByBlockNumber(105L).getName()).isEqualTo("Byzantium"); | ||
assertThat(combinedProtocolSchedule.getByBlockNumber(105L)) | ||
.isSameAs(protocolSchedule2.getByBlockNumber(105L)); | ||
assertThat(combinedProtocolSchedule.getByBlockNumber(110L).getName()).isEqualTo("Berlin"); | ||
assertThat(combinedProtocolSchedule.getByBlockNumber(110L)) | ||
.isSameAs(protocolSchedule2.getByBlockNumber(110L)); | ||
|
||
// consensus schedule 3 migration block | ||
assertThat(combinedProtocolSchedule.getByBlockNumber(200L).getName()).isEqualTo("Berlin"); | ||
assertThat(combinedProtocolSchedule.getByBlockNumber(200L)) | ||
.isSameAs(protocolSchedule3.getByBlockNumber(110L)); | ||
|
||
// consensus schedule 3 | ||
assertThat(combinedProtocolSchedule.getByBlockNumber(220L).getName()).isEqualTo("London"); | ||
assertThat(combinedProtocolSchedule.getByBlockNumber(220L)) | ||
.isSameAs(protocolSchedule3.getByBlockNumber(220L)); | ||
|
||
assertThat(combinedProtocolSchedule.streamMilestoneBlocks().collect(Collectors.toList())) | ||
.isEqualTo(List.of(0L, 5L, 10L, 100L, 105L, 110L, 200L, 220L)); | ||
} | ||
|
||
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(); | ||
} | ||
} |
Oops, something went wrong.