public Wei gasPrice() {
    final Block chainHeadBlock = blockchain.getChainHeadBlock();
    final var chainHeadHeader = chainHeadBlock.getHeader();
    final long blockHeight = chainHeadHeader.getNumber();

    final var nextBlockProtocolSpec =
        protocolSchedule.getForNextBlockHeader(chainHeadHeader, System.currentTimeMillis());
    final var nextBlockFeeMarket = nextBlockProtocolSpec.getFeeMarket();

    final Wei[] gasCollection =
        Stream.concat(
                LongStream.range(
                        Math.max(0, blockHeight - apiConfig.getGasPriceBlocks() + 1), blockHeight)
                    .mapToObj(
                        l ->
                            blockchain
                                .getBlockByNumber(l)
                                .orElseThrow(
                                    () ->
                                        new IllegalStateException(
                                            "Could not retrieve block #" + l))),
                Stream.of(chainHeadBlock))
            .map(Block::getBody)
            .map(BlockBody::getTransactions)
            .flatMap(Collection::stream)
            .filter(t -> t.getGasPrice().isPresent())
            .map(t -> t.getGasPrice().get())
            .sorted()
            .toArray(Wei[]::new);

    return gasCollection.length == 0
        ? gasPriceLowerBound(chainHeadHeader, nextBlockFeeMarket)
        : UInt256s.max(
            gasPriceLowerBound(chainHeadHeader, nextBlockFeeMarket),
            UInt256s.min(
                apiConfig.getGasPriceMax(),
                gasCollection[
                    Math.min(
                        gasCollection.length - 1,
                        (int) ((gasCollection.length) * apiConfig.getGasPriceFraction()))]));
  }
Wraps an operation on MutableWorldState with try-with-resources the corresponding block number

@param <U> return type of the operation on the MutableWorldState
@param blockNumber the block number
@param mapper Function which performs an operation on a MutableWorldState returning type U
@return the world state at the block number
ethereum/api/src/main/java/org/hyperledger/besu/ethereum/api/query/BlockchainQueries.java