Skip to content

Commit

Permalink
Extend Blockchain service (#6592)
Browse files Browse the repository at this point in the history
Signed-off-by: Fabio Di Fabio <[email protected]>
  • Loading branch information
fab-10 authored Feb 26, 2024
1 parent 4815be8 commit 3538c55
Show file tree
Hide file tree
Showing 6 changed files with 95 additions and 14 deletions.
11 changes: 11 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,16 @@
# Changelog

## 24.2.1-SNAPSHOT

### Breaking Changes

### Deprecations

### Additions and Improvements
- Extend `Blockchain` service [#6592](https://github.com/hyperledger/besu/pull/6592)

### Bug fixes

## 24.2.0-SNAPSHOT

### Breaking Changes
Expand Down
17 changes: 11 additions & 6 deletions besu/src/main/java/org/hyperledger/besu/cli/BesuCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -373,6 +373,7 @@ public class BesuCommand implements DefaultCommandValues, Runnable {

private final TransactionSelectionServiceImpl transactionSelectionServiceImpl;
private final PluginTransactionValidatorServiceImpl transactionValidatorServiceImpl;
private final BlockchainServiceImpl blockchainServiceImpl;

static class P2PDiscoveryOptionGroup {

Expand Down Expand Up @@ -957,7 +958,8 @@ public BesuCommand(
new PkiBlockCreationConfigurationProvider(),
new RpcEndpointServiceImpl(),
new TransactionSelectionServiceImpl(),
new PluginTransactionValidatorServiceImpl());
new PluginTransactionValidatorServiceImpl(),
new BlockchainServiceImpl());
}

/**
Expand All @@ -979,6 +981,7 @@ public BesuCommand(
* @param rpcEndpointServiceImpl instance of RpcEndpointServiceImpl
* @param transactionSelectionServiceImpl instance of TransactionSelectionServiceImpl
* @param transactionValidatorServiceImpl instance of TransactionValidatorServiceImpl
* @param blockchainServiceImpl instance of BlockchainServiceImpl
*/
@VisibleForTesting
protected BesuCommand(
Expand All @@ -997,7 +1000,8 @@ protected BesuCommand(
final PkiBlockCreationConfigurationProvider pkiBlockCreationConfigProvider,
final RpcEndpointServiceImpl rpcEndpointServiceImpl,
final TransactionSelectionServiceImpl transactionSelectionServiceImpl,
final PluginTransactionValidatorServiceImpl transactionValidatorServiceImpl) {
final PluginTransactionValidatorServiceImpl transactionValidatorServiceImpl,
final BlockchainServiceImpl blockchainServiceImpl) {
this.besuComponent = besuComponent;
this.logger = besuComponent.getBesuCommandLogger();
this.rlpBlockImporter = rlpBlockImporter;
Expand All @@ -1017,6 +1021,7 @@ protected BesuCommand(
this.rpcEndpointServiceImpl = rpcEndpointServiceImpl;
this.transactionSelectionServiceImpl = transactionSelectionServiceImpl;
this.transactionValidatorServiceImpl = transactionValidatorServiceImpl;
this.blockchainServiceImpl = blockchainServiceImpl;
}

/**
Expand Down Expand Up @@ -1208,6 +1213,7 @@ private void preparePlugins() {
TransactionSelectionService.class, transactionSelectionServiceImpl);
besuPluginContext.addService(
PluginTransactionValidatorService.class, transactionValidatorServiceImpl);
besuPluginContext.addService(BlockchainService.class, blockchainServiceImpl);

// register built-in plugins
rocksDBPlugin = new RocksDBPlugin();
Expand Down Expand Up @@ -1288,6 +1294,9 @@ private Runner buildRunner() {
}

private void startPlugins() {
blockchainServiceImpl.init(
besuController.getProtocolContext(), besuController.getProtocolSchedule());

besuPluginContext.addService(
BesuEvents.class,
new BesuEventsImpl(
Expand All @@ -1297,10 +1306,6 @@ private void startPlugins() {
besuController.getSyncState()));
besuPluginContext.addService(MetricsSystem.class, getMetricsSystem());

besuPluginContext.addService(
BlockchainService.class,
new BlockchainServiceImpl(besuController.getProtocolContext().getBlockchain()));

besuPluginContext.addService(
TraceService.class,
new TraceServiceImpl(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,13 @@

package org.hyperledger.besu.services;

import org.hyperledger.besu.ethereum.chain.Blockchain;
import org.hyperledger.besu.datatypes.Hash;
import org.hyperledger.besu.datatypes.Wei;
import org.hyperledger.besu.ethereum.ProtocolContext;
import org.hyperledger.besu.ethereum.core.BlockBody;
import org.hyperledger.besu.ethereum.mainnet.ProtocolSchedule;
import org.hyperledger.besu.ethereum.mainnet.feemarket.BaseFeeMarket;
import org.hyperledger.besu.ethereum.mainnet.feemarket.FeeMarket;
import org.hyperledger.besu.plugin.Unstable;
import org.hyperledger.besu.plugin.data.BlockContext;
import org.hyperledger.besu.plugin.data.BlockHeader;
Expand All @@ -29,15 +34,21 @@
@Unstable
public class BlockchainServiceImpl implements BlockchainService {

private final Blockchain blockchain;
private ProtocolContext protocolContext;
private ProtocolSchedule protocolSchedule;

/** Create a new instance */
public BlockchainServiceImpl() {}

/**
* Instantiates a new Blockchain service.
*
* @param blockchain the blockchain
* @param protocolContext the protocol context
* @param protocolSchedule the protocol schedule
*/
public BlockchainServiceImpl(final Blockchain blockchain) {
this.blockchain = blockchain;
public void init(final ProtocolContext protocolContext, final ProtocolSchedule protocolSchedule) {
this.protocolContext = protocolContext;
this.protocolSchedule = protocolSchedule;
}

/**
Expand All @@ -48,11 +59,39 @@ public BlockchainServiceImpl(final Blockchain blockchain) {
*/
@Override
public Optional<BlockContext> getBlockByNumber(final long number) {
return blockchain
return protocolContext
.getBlockchain()
.getBlockByNumber(number)
.map(block -> blockContext(block::getHeader, block::getBody));
}

@Override
public Hash getChainHeadHash() {
return protocolContext.getBlockchain().getChainHeadHash();
}

@Override
public BlockHeader getChainHeadHeader() {
return protocolContext.getBlockchain().getChainHeadHeader();
}

@Override
public Optional<Wei> getNextBlockBaseFee() {
final var chainHeadHeader = protocolContext.getBlockchain().getChainHeadHeader();
final var protocolSpec =
protocolSchedule.getForNextBlockHeader(chainHeadHeader, System.currentTimeMillis());
return Optional.of(protocolSpec.getFeeMarket())
.filter(FeeMarket::implementsBaseFee)
.map(BaseFeeMarket.class::cast)
.map(
feeMarket ->
feeMarket.computeBaseFee(
chainHeadHeader.getNumber() + 1,
chainHeadHeader.getBaseFee().orElse(Wei.ZERO),
chainHeadHeader.getGasUsed(),
feeMarket.targetGasUsed(chainHeadHeader)));
}

private static BlockContext blockContext(
final Supplier<BlockHeader> blockHeaderSupplier,
final Supplier<BlockBody> blockBodySupplier) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@
import org.hyperledger.besu.plugin.services.storage.SegmentIdentifier;
import org.hyperledger.besu.services.BesuConfigurationImpl;
import org.hyperledger.besu.services.BesuPluginContextImpl;
import org.hyperledger.besu.services.BlockchainServiceImpl;
import org.hyperledger.besu.services.PermissioningServiceImpl;
import org.hyperledger.besu.services.PluginTransactionValidatorServiceImpl;
import org.hyperledger.besu.services.PrivacyPluginServiceImpl;
Expand Down Expand Up @@ -564,7 +565,8 @@ public static class TestBesuCommand extends BesuCommand {
pkiBlockCreationConfigProvider,
rpcEndpointServiceImpl,
new TransactionSelectionServiceImpl(),
new PluginTransactionValidatorServiceImpl());
new PluginTransactionValidatorServiceImpl(),
new BlockchainServiceImpl());
}

@Override
Expand Down
2 changes: 1 addition & 1 deletion plugin-api/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,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 = 'f6P3+XG9GjPYEg7zrXHlujoE2/4axgd+EjKGDDJVVp8='
knownHash = 'Pi7Veo9W9kmjDJJNB89UTUXbYyRmoN6osK/tD163h3E='
}
check.dependsOn('checkAPIChanges')

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,11 @@
*/
package org.hyperledger.besu.plugin.services;

import org.hyperledger.besu.datatypes.Hash;
import org.hyperledger.besu.datatypes.Wei;
import org.hyperledger.besu.plugin.Unstable;
import org.hyperledger.besu.plugin.data.BlockContext;
import org.hyperledger.besu.plugin.data.BlockHeader;

import java.util.Optional;

Expand All @@ -29,4 +32,25 @@ public interface BlockchainService extends BesuService {
* @return the BlockContext
*/
Optional<BlockContext> getBlockByNumber(final long number);

/**
* Get the hash of the chain head
*
* @return chain head hash
*/
Hash getChainHeadHash();

/**
* Get the block header of the chain head
*
* @return chain head block header
*/
BlockHeader getChainHeadHeader();

/**
* Return the base fee for the next block
*
* @return base fee of the next block or empty if the fee market does not support base fee
*/
Optional<Wei> getNextBlockBaseFee();
}

0 comments on commit 3538c55

Please sign in to comment.