mirror of https://github.com/hyperledger/besu
Rename BftForksSchedule to ForksSchedule and prepare for some more generic use cases (#3108)
* Rename BftForksSchedule to ForkSchedule * Make ForksSchedule generic beyond BftConfigOptions to allow it to work for MiningCoordinator * Extract BftForksScheduleFactory from ForksSchedule * Move ForksSchedule to common package Signed-off-by: Simon Dudley <simon.dudley@consensys.net>pull/3121/head
parent
795d0d01ea
commit
a154b48f23
@ -0,0 +1,49 @@ |
||||
/* |
||||
* 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 java.util.Collection; |
||||
import java.util.Collections; |
||||
import java.util.Comparator; |
||||
import java.util.NavigableSet; |
||||
import java.util.Set; |
||||
import java.util.TreeSet; |
||||
import java.util.function.Function; |
||||
|
||||
public class ForksSchedule<C> { |
||||
|
||||
private final NavigableSet<ForkSpec<C>> forks = |
||||
new TreeSet<>( |
||||
Comparator.comparing((Function<ForkSpec<C>, Long>) ForkSpec::getBlock).reversed()); |
||||
|
||||
public ForksSchedule(final ForkSpec<C> genesisFork, final Collection<ForkSpec<C>> forks) { |
||||
this.forks.add(genesisFork); |
||||
this.forks.addAll(forks); |
||||
} |
||||
|
||||
public ForkSpec<C> getFork(final long blockNumber) { |
||||
for (final ForkSpec<C> f : forks) { |
||||
if (blockNumber >= f.getBlock()) { |
||||
return f; |
||||
} |
||||
} |
||||
|
||||
return forks.first(); |
||||
} |
||||
|
||||
public Set<ForkSpec<C>> getForks() { |
||||
return Collections.unmodifiableSet(forks); |
||||
} |
||||
} |
@ -0,0 +1,68 @@ |
||||
/* |
||||
* 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.BftConfigOptions; |
||||
import org.hyperledger.besu.config.JsonBftConfigOptions; |
||||
import org.hyperledger.besu.consensus.common.bft.MutableBftConfigOptions; |
||||
|
||||
import java.util.List; |
||||
|
||||
import org.junit.Test; |
||||
|
||||
public class ForksScheduleTest { |
||||
|
||||
@Test |
||||
public void retrievesGenesisFork() { |
||||
final ForkSpec<BftConfigOptions> genesisForkSpec = |
||||
new ForkSpec<>(0, JsonBftConfigOptions.DEFAULT); |
||||
|
||||
final ForksSchedule<BftConfigOptions> schedule = |
||||
new ForksSchedule<>(genesisForkSpec, List.of()); |
||||
assertThat(schedule.getFork(0)).isEqualTo(genesisForkSpec); |
||||
assertThat(schedule.getFork(1)).isEqualTo(genesisForkSpec); |
||||
} |
||||
|
||||
@Test |
||||
public void retrievesLatestFork() { |
||||
final ForkSpec<BftConfigOptions> genesisForkSpec = |
||||
new ForkSpec<>(0, JsonBftConfigOptions.DEFAULT); |
||||
final ForkSpec<BftConfigOptions> forkSpec1 = createForkSpec(1, 10); |
||||
final ForkSpec<BftConfigOptions> forkSpec2 = createForkSpec(2, 20); |
||||
|
||||
final ForksSchedule<BftConfigOptions> schedule = |
||||
new ForksSchedule<>(genesisForkSpec, List.of(forkSpec1, forkSpec2)); |
||||
|
||||
assertThat(schedule.getFork(0)).isEqualTo(genesisForkSpec); |
||||
assertThat(schedule.getFork(1)).isEqualTo(forkSpec1); |
||||
assertThat(schedule.getFork(2)).isEqualTo(forkSpec2); |
||||
assertThat(schedule.getFork(3)).isEqualTo(forkSpec2); |
||||
} |
||||
|
||||
private ForkSpec<BftConfigOptions> createForkSpec( |
||||
final long block, final int blockPeriodSeconds) { |
||||
final MutableBftConfigOptions bftConfigOptions = createBftConfigOptions(blockPeriodSeconds); |
||||
return new ForkSpec<>(block, bftConfigOptions); |
||||
} |
||||
|
||||
private MutableBftConfigOptions createBftConfigOptions(final int blockPeriodSeconds) { |
||||
final MutableBftConfigOptions bftConfigOptions = |
||||
new MutableBftConfigOptions(JsonBftConfigOptions.DEFAULT); |
||||
bftConfigOptions.setBlockPeriodSeconds(blockPeriodSeconds); |
||||
return bftConfigOptions; |
||||
} |
||||
} |
Loading…
Reference in new issue