diff --git a/besu/src/main/java/org/hyperledger/besu/cli/BesuCommand.java b/besu/src/main/java/org/hyperledger/besu/cli/BesuCommand.java index 4fad078baa..f277c63edd 100644 --- a/besu/src/main/java/org/hyperledger/besu/cli/BesuCommand.java +++ b/besu/src/main/java/org/hyperledger/besu/cli/BesuCommand.java @@ -163,6 +163,7 @@ import org.hyperledger.besu.nat.NatMethod; import org.hyperledger.besu.plugin.data.EnodeURL; import org.hyperledger.besu.plugin.services.BesuConfiguration; import org.hyperledger.besu.plugin.services.BesuEvents; +import org.hyperledger.besu.plugin.services.BlockchainService; import org.hyperledger.besu.plugin.services.MetricsSystem; import org.hyperledger.besu.plugin.services.PermissioningService; import org.hyperledger.besu.plugin.services.PicoCLIOptions; @@ -178,6 +179,7 @@ import org.hyperledger.besu.plugin.services.storage.PrivacyKeyValueStorageFactor import org.hyperledger.besu.plugin.services.storage.rocksdb.RocksDBPlugin; import org.hyperledger.besu.services.BesuEventsImpl; import org.hyperledger.besu.services.BesuPluginContextImpl; +import org.hyperledger.besu.services.BlockchainServiceImpl; import org.hyperledger.besu.services.PermissioningServiceImpl; import org.hyperledger.besu.services.PicoCLIOptionsImpl; import org.hyperledger.besu.services.PrivacyPluginServiceImpl; @@ -1731,6 +1733,10 @@ public class BesuCommand implements DefaultCommandValues, Runnable { besuController.getSyncState())); besuPluginContext.addService(MetricsSystem.class, getMetricsSystem()); + besuPluginContext.addService( + BlockchainService.class, + new BlockchainServiceImpl(besuController.getProtocolContext().getBlockchain())); + besuController.getAdditionalPluginServices().appendPluginServices(besuPluginContext); besuPluginContext.startPlugins(); } diff --git a/besu/src/main/java/org/hyperledger/besu/services/BlockchainServiceImpl.java b/besu/src/main/java/org/hyperledger/besu/services/BlockchainServiceImpl.java new file mode 100644 index 0000000000..745a179599 --- /dev/null +++ b/besu/src/main/java/org/hyperledger/besu/services/BlockchainServiceImpl.java @@ -0,0 +1,57 @@ +/* + * 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.services; + +import org.hyperledger.besu.ethereum.chain.Blockchain; +import org.hyperledger.besu.ethereum.core.BlockBody; +import org.hyperledger.besu.plugin.data.BlockContext; +import org.hyperledger.besu.plugin.data.BlockHeader; +import org.hyperledger.besu.plugin.services.BlockchainService; + +import java.util.Optional; +import java.util.function.Supplier; + +public class BlockchainServiceImpl implements BlockchainService { + + private final Blockchain blockchain; + + public BlockchainServiceImpl(final Blockchain blockchain) { + this.blockchain = blockchain; + } + + @Override + public Optional getBlockByNumber(final long number) { + return blockchain + .getBlockByNumber(number) + .map(block -> blockContext(block::getHeader, block::getBody)); + } + + private static BlockContext blockContext( + final Supplier blockHeaderSupplier, + final Supplier blockBodySupplier) { + return new BlockContext() { + @Override + public BlockHeader getBlockHeader() { + return blockHeaderSupplier.get(); + } + + @Override + public BlockBody getBlockBody() { + return blockBodySupplier.get(); + } + }; + } +} diff --git a/plugin-api/build.gradle b/plugin-api/build.gradle index a79bc87a9d..0f371254fc 100644 --- a/plugin-api/build.gradle +++ b/plugin-api/build.gradle @@ -67,7 +67,7 @@ Calculated : ${currentHash} tasks.register('checkAPIChanges', FileStateChecker) { description = "Checks that the API for the Plugin-API project does not change without deliberate thought" files = sourceSets.main.allJava.files - knownHash = 'LhQ25UazqtTJuZJN1bj76imJ7QdkFRR6dUyxt7Ig5cs=' + knownHash = 'sMLZpz/Rie6f/uUFwxaDvVHU8F61pcLnazAfIVwyyH0=' } check.dependsOn('checkAPIChanges') diff --git a/plugin-api/src/main/java/org/hyperledger/besu/plugin/data/AddedBlockContext.java b/plugin-api/src/main/java/org/hyperledger/besu/plugin/data/AddedBlockContext.java index 2a736138be..a5a354e594 100644 --- a/plugin-api/src/main/java/org/hyperledger/besu/plugin/data/AddedBlockContext.java +++ b/plugin-api/src/main/java/org/hyperledger/besu/plugin/data/AddedBlockContext.java @@ -17,22 +17,7 @@ package org.hyperledger.besu.plugin.data; import java.util.List; /** The minimum set of data for an AddedBlockContext. */ -public interface AddedBlockContext { - - /** - * A {@link BlockHeader} object. - * - * @return A {@link BlockHeader} - */ - BlockHeader getBlockHeader(); - - /** - * A {@link BlockBody} object. - * - * @return A {@link BlockBody} - */ - BlockBody getBlockBody(); - +public interface AddedBlockContext extends BlockContext { /** * A list of transaction receipts for the added block. * diff --git a/plugin-api/src/main/java/org/hyperledger/besu/plugin/data/BlockContext.java b/plugin-api/src/main/java/org/hyperledger/besu/plugin/data/BlockContext.java new file mode 100644 index 0000000000..caacc741f4 --- /dev/null +++ b/plugin-api/src/main/java/org/hyperledger/besu/plugin/data/BlockContext.java @@ -0,0 +1,33 @@ +/* + * 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.plugin.data; + +/** The minimum set of data for a BlockContext. */ +public interface BlockContext { + + /** + * A {@link BlockHeader} object. + * + * @return A {@link BlockHeader} + */ + BlockHeader getBlockHeader(); + + /** + * A {@link BlockBody} object. + * + * @return A {@link BlockBody} + */ + BlockBody getBlockBody(); +} diff --git a/plugin-api/src/main/java/org/hyperledger/besu/plugin/data/PropagatedBlockContext.java b/plugin-api/src/main/java/org/hyperledger/besu/plugin/data/PropagatedBlockContext.java index ad919cddaf..3387ca2fe7 100644 --- a/plugin-api/src/main/java/org/hyperledger/besu/plugin/data/PropagatedBlockContext.java +++ b/plugin-api/src/main/java/org/hyperledger/besu/plugin/data/PropagatedBlockContext.java @@ -17,22 +17,7 @@ package org.hyperledger.besu.plugin.data; import org.apache.tuweni.units.bigints.UInt256; /** The minimum set of data for a PropagatedBlockContext. */ -public interface PropagatedBlockContext { - - /** - * A {@link BlockHeader} object. - * - * @return A {@link BlockHeader} - */ - BlockHeader getBlockHeader(); - - /** - * A {@link BlockBody} object. - * - * @return A {@link BlockBody} - */ - BlockBody getBlockBody(); - +public interface PropagatedBlockContext extends BlockContext { /** * A scalar value corresponding to the total difficulty. * diff --git a/plugin-api/src/main/java/org/hyperledger/besu/plugin/services/BlockchainService.java b/plugin-api/src/main/java/org/hyperledger/besu/plugin/services/BlockchainService.java new file mode 100644 index 0000000000..0ee3deda5c --- /dev/null +++ b/plugin-api/src/main/java/org/hyperledger/besu/plugin/services/BlockchainService.java @@ -0,0 +1,23 @@ +/* + * 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.plugin.services; + +import org.hyperledger.besu.plugin.data.BlockContext; + +import java.util.Optional; + +public interface BlockchainService extends BesuService { + Optional getBlockByNumber(final long number); +}