Plugin API - Blockchain Service (#5247)

Signed-off-by: Gabriel-Trintinalia <gabriel.trintinalia@consensys.net>
pull/5248/head
Gabriel-Trintinalia 2 years ago committed by GitHub
parent 1f3d5ea2bc
commit 8d31050164
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 6
      besu/src/main/java/org/hyperledger/besu/cli/BesuCommand.java
  2. 57
      besu/src/main/java/org/hyperledger/besu/services/BlockchainServiceImpl.java
  3. 2
      plugin-api/build.gradle
  4. 17
      plugin-api/src/main/java/org/hyperledger/besu/plugin/data/AddedBlockContext.java
  5. 33
      plugin-api/src/main/java/org/hyperledger/besu/plugin/data/BlockContext.java
  6. 17
      plugin-api/src/main/java/org/hyperledger/besu/plugin/data/PropagatedBlockContext.java
  7. 23
      plugin-api/src/main/java/org/hyperledger/besu/plugin/services/BlockchainService.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();
}

@ -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<BlockContext> getBlockByNumber(final long number) {
return blockchain
.getBlockByNumber(number)
.map(block -> blockContext(block::getHeader, block::getBody));
}
private static BlockContext blockContext(
final Supplier<BlockHeader> blockHeaderSupplier,
final Supplier<BlockBody> blockBodySupplier) {
return new BlockContext() {
@Override
public BlockHeader getBlockHeader() {
return blockHeaderSupplier.get();
}
@Override
public BlockBody getBlockBody() {
return blockBodySupplier.get();
}
};
}
}

@ -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')

@ -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.
*

@ -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();
}

@ -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.
*

@ -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<BlockContext> getBlockByNumber(final long number);
}
Loading…
Cancel
Save