Skip to content

Commit 6371970

Browse files
committed
Add get hardforkId by block number to Blockchain service
Signed-off-by: Fabio Di Fabio <fabio.difabio@consensys.net>
1 parent ede86e4 commit 6371970

File tree

5 files changed

+33
-14
lines changed

5 files changed

+33
-14
lines changed

acceptance-tests/test-plugins/src/main/java/org/hyperledger/besu/tests/acceptance/plugins/TestBlockchainServicePlugin.java

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -75,10 +75,12 @@ public void start() {
7575
private HardforkSeen queryHardfork(
7676
final BlockchainService blockchainService, final BlockHeader header) {
7777
final var currentHardfork = blockchainService.getHardforkId(header);
78+
final var currentHardforkByNumber = blockchainService.getHardforkId(header.getNumber());
7879
final var nextHardfork =
7980
blockchainService.getNextBlockHardforkId(header, header.getTimestamp() + 1);
8081

81-
return new HardforkSeen(header.getNumber(), currentHardfork, nextHardfork);
82+
return new HardforkSeen(
83+
header.getNumber(), currentHardfork, currentHardforkByNumber, nextHardfork);
8284
}
8385

8486
@Override
@@ -97,7 +99,11 @@ private void writeSeenHardforks() {
9799
.map(
98100
r ->
99101
String.join(
100-
",", String.valueOf(r.blockNumber), r.current.name(), r.next.name()))
102+
",",
103+
String.valueOf(r.blockNumber),
104+
r.current.name(),
105+
r.currentByNumber().name(),
106+
r.next.name()))
101107
.collect(Collectors.joining("\n"));
102108

103109
Files.write(callbackFile.toPath(), content.getBytes(UTF_8));
@@ -107,5 +113,6 @@ private void writeSeenHardforks() {
107113
}
108114
}
109115

110-
private record HardforkSeen(long blockNumber, HardforkId current, HardforkId next) {}
116+
private record HardforkSeen(
117+
long blockNumber, HardforkId current, HardforkId currentByNumber, HardforkId next) {}
111118
}

acceptance-tests/tests/src/test/java/org/hyperledger/besu/tests/acceptance/plugins/BlockchainServicePluginTest.java

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818

1919
import org.hyperledger.besu.tests.acceptance.dsl.AcceptanceTestBase;
2020
import org.hyperledger.besu.tests.acceptance.dsl.node.BesuNode;
21-
import org.hyperledger.besu.tests.acceptance.dsl.node.configuration.genesis.GenesisConfigurationFactory;
2221

2322
import java.io.IOException;
2423
import java.nio.file.Files;
@@ -30,25 +29,18 @@
3029
import org.junit.jupiter.api.Test;
3130

3231
public class BlockchainServicePluginTest extends AcceptanceTestBase {
33-
private BesuNode minerNode;
3432
private BesuNode pluginNode;
3533

3634
@BeforeEach
3735
public void setUp() throws Exception {
38-
minerNode =
39-
besu.createQbftNode(
40-
"minerNode",
41-
besuNodeConfigurationBuilder ->
42-
besuNodeConfigurationBuilder.genesisConfigProvider(
43-
GenesisConfigurationFactory::createQbftLondonGenesisConfig));
4436
pluginNode =
4537
besu.createQbftPluginsNode(
4638
"pluginNode",
4739
Collections.singletonList("testPlugins"),
4840
Collections.singletonList("--plugin-tx-validator-test-enabled=true"),
4941
"DEBUG");
5042

51-
cluster.start(pluginNode, minerNode);
43+
cluster.start(pluginNode);
5244
}
5345

5446
@Test
@@ -57,7 +49,8 @@ public void hardforkListIsCorrect() throws IOException {
5749
waitForFile(hardforkListFile);
5850
final var fileContents = Files.readAllLines(hardforkListFile);
5951

60-
final var expectedLines = List.of("0,BERLIN,BERLIN", "1,BERLIN,LONDON", "2,LONDON,LONDON");
52+
final var expectedLines =
53+
List.of("0,BERLIN,BERLIN,BERLIN", "1,BERLIN,BERLIN,LONDON", "2,LONDON,LONDON,LONDON");
6154

6255
for (int i = 0; i < expectedLines.size(); i++) {
6356
assertThat(fileContents.get(i)).isEqualTo(expectedLines.get(i));

app/src/main/java/org/hyperledger/besu/services/BlockchainServiceImpl.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -233,6 +233,15 @@ public HardforkId getHardforkId(final BlockHeader blockHeader) {
233233
return protocolSchedule.getByBlockHeader(blockHeader).getHardforkId();
234234
}
235235

236+
@Override
237+
public HardforkId getHardforkId(final long blockNumber) {
238+
return blockchain
239+
.getBlockHeader(blockNumber)
240+
.map(this::getHardforkId)
241+
.orElseThrow(
242+
() -> new IllegalArgumentException("Block not found for number: " + blockNumber));
243+
}
244+
236245
@Override
237246
public HardforkId getNextBlockHardforkId(
238247
final BlockHeader parentBlockHeader, final long timestampForNextBlock) {

plugin-api/build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ Calculated : ${currentHash}
7171
tasks.register('checkAPIChanges', FileStateChecker) {
7272
description = "Checks that the API for the Plugin-API project does not change without deliberate thought"
7373
files = sourceSets.main.allJava.files
74-
knownHash = '5BI+4JAKgBsTClKWIeF86C3kS/RVT3Cdwyju/HydR9o='
74+
knownHash = 'fhbRRCXyfMDwSkyEe+YhgUhzIu2/OWSMhttKCnmTqlc='
7575
}
7676
check.dependsOn('checkAPIChanges')
7777

plugin-api/src/main/java/org/hyperledger/besu/plugin/services/BlockchainService.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,16 @@ void setFinalizedBlock(Hash blockHash)
150150
@Unstable
151151
HardforkId getHardforkId(BlockHeader blockHeader);
152152

153+
/**
154+
* Get the hardfork identifier for the given block number
155+
*
156+
* @param blockNumber the block number to determine the hardfork for
157+
* @return the hardfork identifier applicable to the given block number
158+
* @throws IllegalArgumentException if no block with that number exists
159+
*/
160+
@Unstable
161+
HardforkId getHardforkId(long blockNumber);
162+
153163
/**
154164
* Get the hardfork identifier for the next block based on the parent block and timestamp
155165
*

0 commit comments

Comments
 (0)