|
| 1 | +/* |
| 2 | + * Copyright contributors to Besu. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with |
| 5 | + * the License. You may obtain a copy of the License at |
| 6 | + * |
| 7 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | + * |
| 9 | + * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on |
| 10 | + * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the |
| 11 | + * specific language governing permissions and limitations under the License. |
| 12 | + * |
| 13 | + * SPDX-License-Identifier: Apache-2.0 |
| 14 | + */ |
| 15 | +package org.hyperledger.besu.services; |
| 16 | + |
| 17 | +import org.hyperledger.besu.datatypes.AccountOverrideMap; |
| 18 | +import org.hyperledger.besu.datatypes.Transaction; |
| 19 | +import org.hyperledger.besu.ethereum.chain.Blockchain; |
| 20 | +import org.hyperledger.besu.ethereum.core.BlockHeader; |
| 21 | +import org.hyperledger.besu.ethereum.core.MiningConfiguration; |
| 22 | +import org.hyperledger.besu.ethereum.core.MutableWorldState; |
| 23 | +import org.hyperledger.besu.ethereum.mainnet.ProtocolSchedule; |
| 24 | +import org.hyperledger.besu.ethereum.transaction.BlockSimulationResult; |
| 25 | +import org.hyperledger.besu.ethereum.transaction.BlockSimulator; |
| 26 | +import org.hyperledger.besu.ethereum.transaction.BlockStateCall; |
| 27 | +import org.hyperledger.besu.ethereum.transaction.CallParameter; |
| 28 | +import org.hyperledger.besu.ethereum.transaction.TransactionSimulator; |
| 29 | +import org.hyperledger.besu.ethereum.worldstate.WorldStateArchive; |
| 30 | +import org.hyperledger.besu.plugin.Unstable; |
| 31 | +import org.hyperledger.besu.plugin.data.BlockOverrides; |
| 32 | +import org.hyperledger.besu.plugin.data.PluginBlockSimulationResult; |
| 33 | +import org.hyperledger.besu.plugin.data.TransactionSimulationResult; |
| 34 | +import org.hyperledger.besu.plugin.services.BlockSimulationService; |
| 35 | + |
| 36 | +import java.util.List; |
| 37 | + |
| 38 | +/** This class is a service that simulates the processing of a block */ |
| 39 | +public class BlockSimulatorServiceImpl implements BlockSimulationService { |
| 40 | + private final BlockSimulator blockSimulator; |
| 41 | + private final WorldStateArchive worldStateArchive; |
| 42 | + private final Blockchain blockchain; |
| 43 | + |
| 44 | + /** |
| 45 | + * This constructor creates a BlockSimulatorServiceImpl object |
| 46 | + * |
| 47 | + * @param worldStateArchive the world state archive |
| 48 | + * @param miningConfiguration the mining configuration |
| 49 | + * @param transactionSimulator the transaction simulator |
| 50 | + * @param protocolSchedule the protocol schedule |
| 51 | + * @param blockchain the blockchain |
| 52 | + */ |
| 53 | + public BlockSimulatorServiceImpl( |
| 54 | + final WorldStateArchive worldStateArchive, |
| 55 | + final MiningConfiguration miningConfiguration, |
| 56 | + final TransactionSimulator transactionSimulator, |
| 57 | + final ProtocolSchedule protocolSchedule, |
| 58 | + final Blockchain blockchain) { |
| 59 | + this.blockchain = blockchain; |
| 60 | + blockSimulator = |
| 61 | + new BlockSimulator( |
| 62 | + worldStateArchive, protocolSchedule, transactionSimulator, miningConfiguration); |
| 63 | + this.worldStateArchive = worldStateArchive; |
| 64 | + } |
| 65 | + |
| 66 | + /** |
| 67 | + * Simulate the processing of a block given a header, a list of transactions, and blockOverrides. |
| 68 | + * |
| 69 | + * @param blockNumber the block number |
| 70 | + * @param transactions the transactions to include in the block |
| 71 | + * @param blockOverrides the blockSimulationOverride of the block |
| 72 | + * @param accountOverrides state overrides of the block |
| 73 | + * @return the block context |
| 74 | + */ |
| 75 | + @Override |
| 76 | + public PluginBlockSimulationResult simulate( |
| 77 | + final long blockNumber, |
| 78 | + final List<? extends Transaction> transactions, |
| 79 | + final BlockOverrides blockOverrides, |
| 80 | + final AccountOverrideMap accountOverrides) { |
| 81 | + return processSimulation(blockNumber, transactions, blockOverrides, accountOverrides, false); |
| 82 | + } |
| 83 | + |
| 84 | + /** |
| 85 | + * This method is experimental and should be used with caution. Simulate the processing of a block |
| 86 | + * given a header, a list of transactions, and blockOverrides and persist the WorldState |
| 87 | + * |
| 88 | + * @param blockNumber the block number |
| 89 | + * @param transactions the transactions to include in the block |
| 90 | + * @param blockOverrides block overrides for the block |
| 91 | + * @param accountOverrides state overrides of the block |
| 92 | + * @return the PluginBlockSimulationResult |
| 93 | + */ |
| 94 | + @Unstable |
| 95 | + @Override |
| 96 | + public PluginBlockSimulationResult simulateAndPersistWorldState( |
| 97 | + final long blockNumber, |
| 98 | + final List<? extends Transaction> transactions, |
| 99 | + final BlockOverrides blockOverrides, |
| 100 | + final AccountOverrideMap accountOverrides) { |
| 101 | + return processSimulation(blockNumber, transactions, blockOverrides, accountOverrides, true); |
| 102 | + } |
| 103 | + |
| 104 | + private PluginBlockSimulationResult processSimulation( |
| 105 | + final long blockNumber, |
| 106 | + final List<? extends Transaction> transactions, |
| 107 | + final BlockOverrides blockOverrides, |
| 108 | + final AccountOverrideMap accountOverrides, |
| 109 | + final boolean persistWorldState) { |
| 110 | + BlockHeader header = getBlockHeader(blockNumber); |
| 111 | + List<CallParameter> callParameters = |
| 112 | + transactions.stream().map(CallParameter::fromTransaction).toList(); |
| 113 | + BlockStateCall blockStateCall = |
| 114 | + new BlockStateCall(callParameters, blockOverrides, accountOverrides, true); |
| 115 | + try (final MutableWorldState ws = getWorldState(header, persistWorldState)) { |
| 116 | + List<BlockSimulationResult> results = |
| 117 | + blockSimulator.process(header, List.of(blockStateCall), ws); |
| 118 | + BlockSimulationResult result = results.getFirst(); |
| 119 | + if (persistWorldState) { |
| 120 | + ws.persist(result.getBlock().getHeader()); |
| 121 | + } |
| 122 | + return response(result); |
| 123 | + } catch (final Exception e) { |
| 124 | + throw new RuntimeException("Error simulating block", e); |
| 125 | + } |
| 126 | + } |
| 127 | + |
| 128 | + private BlockHeader getBlockHeader(final long blockNumber) { |
| 129 | + return blockchain |
| 130 | + .getBlockHeader(blockNumber) |
| 131 | + .orElseThrow( |
| 132 | + () -> |
| 133 | + new IllegalArgumentException( |
| 134 | + "Block header not found for block number: " + blockNumber)); |
| 135 | + } |
| 136 | + |
| 137 | + private MutableWorldState getWorldState(final BlockHeader header, final boolean isPersisting) { |
| 138 | + return worldStateArchive |
| 139 | + .getMutable(header, isPersisting) |
| 140 | + .orElseThrow( |
| 141 | + () -> |
| 142 | + new IllegalArgumentException( |
| 143 | + "World state not available for block number (block hash): " |
| 144 | + + header.toLogString())); |
| 145 | + } |
| 146 | + |
| 147 | + private PluginBlockSimulationResult response(final BlockSimulationResult result) { |
| 148 | + return new PluginBlockSimulationResult( |
| 149 | + result.getBlockHeader(), |
| 150 | + result.getBlockBody(), |
| 151 | + result.getReceipts(), |
| 152 | + result.getTransactionSimulations().stream() |
| 153 | + .map( |
| 154 | + simulation -> |
| 155 | + new TransactionSimulationResult(simulation.transaction(), simulation.result())) |
| 156 | + .toList()); |
| 157 | + } |
| 158 | +} |
0 commit comments